[Tex/LaTex] Referencing subfigures/Increasing figure counter

cross-referencingfloatssubcaptionsubfloats

There is a problem with referencing subfigures.

enter image description here

When I add the line containing \caption{}, everything works perfect, but caption of a figure must be below the figure.

How can I solve this problem?

Here is the code:

\documentclass{article}
\usepackage{subcaption}
\begin{document}
\renewcommand{\thesubfigure}{(\alph{subfigure})}
\begin{figure}
\caption{} % When I add this line, everything works perfect, but caption of a figure must be below the figure.
\begin{minipage}[b]{0.50\textwidth}
1A
\captionof{subfigure}{1A}
\label{fig:1:A}  
\end{minipage}
\begin{minipage}[b]{0.50\textwidth}
1B
\captionof{subfigure}{1B}
\label{fig:1:B}  
\end{minipage}
\caption{First~\ref{fig:1:A}~\ref{fig:1:B}}
\end{figure}
\begin{figure}
\begin{minipage}[b]{0.50\textwidth}
2A
\captionof{subfigure}{2A}
\label{fig:2:A}  
\end{minipage}
\begin{minipage}[b]{0.50\textwidth}
2B
\captionof{subfigure}{2B}
\label{fig:2:B}  
\end{minipage}
\caption{First~\ref{fig:2:A}~\ref{fig:2:B}}
\end{figure}
\end{document}

Note 1: The real document has 3500 lines so I can not set the value of the figure counter manually via \renewcommand\thefigure{2}.

Note 2: In a single figure of the real document, there are plenty of minipages that contain tikz pictures so I prefer using minipage.

Best Answer

The cubcaption manual hints to use the command subcaption instead of captionof. It increases the counters internally. Another possibility would be to use the provided environment subfigure (it uses a minipage itself and got the same syntax, you could just do a string replace) along with the caption command we all know.

Both of the following MWEs produce the same end result.

enter image description here

\documentclass{article}
\usepackage{subcaption}
\begin{document}
%\renewcommand{\thesubfigure}{(\alph{subfigure})}
\begin{figure}
\begin{minipage}[b]{0.50\textwidth}
1A
\subcaption{1A}
\label{fig:1:A}  
\end{minipage}
\begin{minipage}[b]{0.50\textwidth}
1B
\subcaption{1B}
\label{fig:1:B}  
\end{minipage}
\caption{First~\ref{fig:1:A}~\ref{fig:1:B}}
\end{figure}
\begin{figure}
\begin{minipage}[b]{0.50\textwidth}
2A
\subcaption{2A}
\label{fig:2:A}  
\end{minipage}
\begin{minipage}[b]{0.50\textwidth}
2B
\subcaption{2B}
\label{fig:2:B}  
\end{minipage}
\caption{First~\ref{fig:2:A}~\ref{fig:2:B}}
\end{figure}
\end{document}

Using subfigure:

\documentclass{article}
\usepackage{subcaption}
\begin{document}
\begin{figure}
    \begin{subfigure}[b]{0.50\textwidth}
        1A
        \caption{1A}
        \label{fig:1:A}  
    \end{subfigure}
    \begin{subfigure}[b]{0.50\textwidth}
        1B
        \caption{1B}
        \label{fig:1:B}  
    \end{subfigure}
    \caption{First~\ref{fig:1:A}~\ref{fig:1:B}}
\end{figure}
\begin{figure}
    \begin{subfigure}[b]{0.50\textwidth}
        2A
        \caption{2A}
        \label{fig:2:A}  
    \end{subfigure}
    \begin{subfigure}[b]{0.50\textwidth}
        2B
        \caption{2B}
        \label{fig:2:B}  
    \end{subfigure}
    \caption{First~\ref{fig:2:A}~\ref{fig:2:B}}
\end{figure}
\end{document}