[Tex/LaTex] Cleveref mislabels figures when using \ContinuedFloat

cleverefcross-referencingsubfloats

When using subfloats over multiple pages I have used \ContinuedFloat to carry on the figure environment which also preserves the numbering. However, when I try to reference the whole figure, cleveref does not get the label correct.

There are numerous answers available for misnumbering but not mislabeling.

Have I put the \label in the wrong place?

MWE as follows with all the packages I'm using:

\documentclass[12pt,demo]{elsarticle}

\usepackage{enumerate}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage{tabu}
\usepackage{multirow}
\usepackage{textcomp}
\usepackage[utf8]{inputenc}
\usepackage[capitalise]{cleveref}
\usepackage{booktabs}
\usepackage{comment}
\usepackage[section]{placeins}
\usepackage{float}
\usepackage{subfig}
\usepackage{caption}


\begin{document}

\section{Section Title}
\Cref{fig:First} and \cref{fig:Second} can be seen in \cref{fig:Both}.


\begin{figure}
\centering
\subfloat[Caption for the first image]{
\includegraphics[width=.4\columnwidth]{Img1}%
\label{fig:First}%
}
\phantomcaption
\end{figure}

\begin{figure}
\ContinuedFloat
\centering
\subfloat[Caption for the second image]{
\includegraphics[width=.4\columnwidth]{Img2}%
\label{fig:Second}%
}
\caption{Main Caption}%
\label{fig:Both}%
\end{figure}


\end{document}

Best Answer

I can offer a solution that uses the subcaption package and its subfigure environment instead of the subfig package and its \subfloat macro. I suggest making this change because the \ContinuedFloat macro (which is provided by the caption package, which is loaded automatically by the subcaption package) seems to interact in an unfortunate way with the subfig package's macros, ending up confusing cleveref. Possibly because the subcaption and caption packages were created by the same author, labeling does not get messed up if you employ \ContinuedFloat along with subfigure environments.

The syntax for creating subfigures needs to be adjusted a bit if you use the macros of the subcaption package. However, I trust the adjustments won't be too burdensome.

enter image description here

\documentclass[12pt,demo]{elsarticle}
%% commented out the following packages as they aren't needed for the MWE
%\usepackage{enumerate}
%\usepackage{amsmath}
%\usepackage{amsfonts}
%\usepackage{mathtools}
%\usepackage{tabu}
%\usepackage{multirow}
%\usepackage{textcomp}
%\usepackage[utf8]{inputenc}
%\usepackage{booktabs}
%\usepackage{comment}
%\usepackage[section]{placeins}
%\usepackage{float}

%%%\usepackage{subfig} do not use load this package

\usepackage{subcaption} %%% instead, load the 'subcaption' package
     %%% 'subcaption' loads 'caption', which provides the macro \ContinuedFloat

\usepackage[capitalise,noabbrev]{cleveref} %%% load 'cleveref' last

\begin{document}

\section{Section Title}

\Cref{fig:First,fig:Second} can be seen in \cref{fig:Both}.

\begin{figure}
\centering
\begin{subfigure}{.4\columnwidth}
\includegraphics[width=\linewidth]{Img1}%
\caption{Caption for the first image}
\label{fig:First}%
\end{subfigure}
%%% \phantomcaption %% no need for this statement
\end{figure}

\begin{figure}
\ContinuedFloat
\centering
\begin{subfigure}{.4\columnwidth}
\includegraphics[width=\linewidth]{Img2}%
\caption{Caption for the second image}
\label{fig:Second}%
\end{subfigure}
\caption{Main Caption}%
\label{fig:Both}%
\end{figure}

\end{document}
Related Question