[Tex/LaTex] Continuous subfigure numbering

floatsnumberingsubfloats

I have a lot of files including images and I want them to be numbered continuously.

I use many images next to each other, which looks like

\begin{figure}[H]
    \begin{subfigure}[t]{0.45\textwidth}
            \includegraphics[bla]{bla1.jpg}
            \caption{bla}
            \label{fig:bla1}
    \end{subfigure}
    \begin{subfigure}[t]{0.45\textwidth}
        \includegraphics[bla]{bla2.jpg}
            \caption{bla}
            \label{fig:bla2}
    \end{subfigure}
\end{figure}

Their caption labels are (1) and (2).

Then I have single images like:

\begin{figure}[H]
\begin{subfigure}{0.62\textwidth}
    \includegraphics[bla]{bla3.jpg}
    \caption{bla}
    \label{fig:bla3}
\end{subfigure}
\end{figure}

Its label is (1) again, but I would like it to have (3).

My attempt was to use:

\usepackage{chngcntr}
\counterwithout{subfigure}{figure}

so that it ignores the figure-environment. It does not work.

The problem is that I am using memoir as documentclass, without using chapters. Originally, the pictures were numbered continuously like 0.1, 0.2, 0.3 etc., because they were in minipages instead of subfigures. But I wanted to remove the 0, so that I replaced the minipages with subfigures and

\renewcommand{\thesubfigure}{\arabic{subfigure}}

I hope this is understandable and I appreciate your help!

Best Answer

Here is my solution using the etoolbox package and a new counter which will be increased by one each time the subfigure environment is called.

\documentclass{memoir}

\usepackage[draft]{graphicx}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{etoolbox}
\newcounter{fignumber}
\setcounter{fignumber}{0}

\renewcommand{\thesubfigure}{\arabic{fignumber}}

\AtBeginEnvironment{subfigure}{\refstepcounter{fignumber}}

\begin{document}

\begin{figure}[H]
    \begin{subfigure}[t]{0.45\textwidth}
            \includegraphics{bla1.jpg}
            \caption{bla}
            \label{fig:bla1}
    \end{subfigure}
    \begin{subfigure}[t]{0.45\textwidth}
        \includegraphics{bla2.jpg}
            \caption{bla}
            \label{fig:bla2}
    \end{subfigure}
\end{figure}

\begin{figure}[H]
\begin{subfigure}{0.62\textwidth}
    \includegraphics{bla3.jpg}
    \caption{bla}
    \label{fig:bla3}
\end{subfigure}
\end{figure}

\end{document}

EDIT It turned out that the above solution is not entirely satisfying when calling \ref{} with one of the labels defined (see this question). In order to fix that, just add these lines to your preamble:

\makeatletter
\renewcommand{\p@subfigure}{}
\makeatother