[Tex/LaTex] Wrong figure reference number

cross-referencingsubfloats

I'm trying to insert a figure in a LaTeX file, but LaTeX always gives the reference number of this figure as figure 1 instead of 2, although there is already a figure 1 earlier in the document. I also can't reference this figure using ~\ref{fig:regress}. The figure number just doesn't print.

Here's the code I used to insert the image in LaTeX:

\documentclass[11pt]{article}
\usepackage{graphicx}    
\usepackage{caption}
\usepackage{subcaption}
\usepackage{chngcntr}

\begin{document}  

I'm trying to reference Figure~\ref{fig:regress} here...

\begin{figure}[htp]
        \centering
        \begin{subfigure}{0.8\textwidth}
                \centering
                \includegraphics[width=\textwidth]{im1.png}
                \caption{Scatter plot fitted with straight line}
                \label{fig:linfit}
        \end{subfigure}%

        \begin{subfigure}{0.8\textwidth}
                \centering
                \includegraphics[width=\textwidth]{im2.png}
                \caption{Scatter plot fitted with 2nd degree polynomial}
                \label{fig:quadfit}
        \end{subfigure}

\end{figure}

\newpage
\begin{figure}
\ContinuedFloat
\centering
        \begin{subfigure}{0.8\textwidth}
                \centering
                \includegraphics[width=\textwidth]{im3.png}
                \caption{Scatter plot fitted with 3rd degree polynomial} 
                \label{fig:cubicfit}
        \end{subfigure}

        \caption{Scatter plots of wavelength vs. pixel position for the spectrum from night 1, fitted with polynomials of orders 1-3.}   \label{fig:regress}
\end{figure}
\end{document}

I checked that I have \label and \caption in the right order. What's going on?

Best Answer

When compiling your MWE with pdflatex I get the error

! Package caption Error: Continued `figure' after `??'.

Whatever that means … (I have used neither caption nor subcaption before.)

Apparently caption is confused when you use \ContinuedFloat in a figure environment when the preceding one doesn’t had a \caption; which isn’t very surprising because there isn’t much to continue. To fix this, add a \phantomcaption to the first figure environment[See Axel Sommerfeldt's comment.]

If you do want to include a caption in the first figure environment, of course, you must not use \phantomcaption.

Code

Note that I have removed every reference to an external graphic because

  • I don’t have them.
  • I don’t need them.

I also removed \usepackage{caption}, it does get already loaded by subcaption.
The chngcntr package does not interfere with this MWE.


\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{chngcntr}
\begin{document}
\begin{figure}
  \caption{fig:before}\label{fig:before}
\end{figure}
Figure~\ref{fig:before}.

I'm trying to reference Figure~\ref{fig:regress}, subfigure~\ref{fig:linfit} and \ref{fig:quadfit} as well as \ref{fig:cubicfit} here.

\hrulefill
\begin{figure}[htp]
  \centering
  \begin{subfigure}{0.8\textwidth}
    \caption{Scatter plot fitted with straight line}
    \label{fig:linfit}
  \end{subfigure}%

  \begin{subfigure}{0.8\textwidth}
    \caption{Scatter plot fitted with 2nd degree polynomial}
    \label{fig:quadfit}
  \end{subfigure}
  \phantomcaption%                                                                                                                  either a phantom caption
%  \caption{Scatteru plots of wavelength vs. pixel position for the spectrum from night 1, fitted with polynomials of orders 1-3.}% or a real one
\end{figure}

\hrulefill
\begin{figure}[htp]
  \ContinuedFloat
  \centering
  \begin{subfigure}{0.8\textwidth}
    \caption{Scatter plot fitted with 3rd degree polynomial} 
    \label{fig:cubicfit}
  \end{subfigure}
  \caption{Scatteru plots of wavelength vs. pixel position for the spectrum from night 1, fitted with polynomials of orders 1-3.}   \label{fig:regress}
\end{figure}
\end{document}

Output

enter image description here