[Tex/LaTex] Wrong reference number for subfloat

cross-referencingnumberingsubfloats

I am trying to reference a subfigure in my text but it is numbered incorrectly.
Here is a minimal working example:

enter image description here

\documentclass{scrartcl}

\usepackage[demo]{graphicx}
\usepackage{subfig}

\begin{document}
    \begin{figure}
        \caption{Two pictures side by side}
        \label{img:both}
        \subfloat[first picture]{
            \includegraphics[width=5cm]{name}
            \label{img:first}
        }
        \subfloat[second picture]{
            \includegraphics[width=5cm]{name}
            \label{img:second}
        }
    \end{figure}

    This example shows that figure \ref{img:second} has a wrong reference number.
    But the complete float is referenced correctly: \ref{img:both}
\end{document}

Best Answer

The subfig package is strongly dependent on the knowledge of the caption position -- is it above or below the figure or table?

An except of the subfig package documentation:

3.1.5 The Caption Position Option

The caption package ‘position’ option specifies whether the caption appears before or after the figure or table. This can adjust the relative spacing used to separate the float from the surrounding text. However, for the subfig package, it serves a more important function. That is it determines if the sub-floats belong to or are associated with the last \caption command to be given, or the next one to be executed sometime in the future. If you find that you sub-references do not agree with the top-level labels, than you may need to specifically set the ‘position’. This is best done when loading the caption package, but may be done at anytime with the \captionsetup command.

So usually this can be fixed using the correct position settings:

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{subfig}

% Telling the subfig package the desired caption positions
\captionsetup[figure]{position=t}
\captionsetup[subfigure]{position=b}

\begin{document}
    \begin{figure}
        \caption{Two pictures side by side}
        \label{img:both}
        \subfloat[first picture]{
            \includegraphics[width=5cm]{name}
            \label{img:first}
        }
        \subfloat[second picture]{
            \includegraphics[width=5cm]{name}
            \label{img:second}
        }
    \end{figure}

    This example shows that figure \ref{img:second} has a wrong reference number.
    But the complete float is referenced correctly: \ref{img:both}
\end{document}

However, KOMA-Script is some kind of special here because it has its own options and commands for specifying the main caption position. The caption/subfig package respects (and supports) the KOMA-Script approach, making the code line \captionsetup[figure]{position=t} not only superfluous but without any effect. (See caption package documentation for details, section about "KOMA-Script".)

So with KOMA-Script classes, one need to use the options and/or commands offered by KOMA-Script instead, at least for the main caption, for example with "captions=figureheading":

\documentclass[captions=figureheading]{scrartcl}

\usepackage[demo]{graphicx}
\usepackage{subfig}

% Telling the subfig package the desired caption position
\captionsetup[subfigure]{position=b}

\begin{document}
    \begin{figure}
        \caption{Two pictures side by side}
        \label{img:both}
        \subfloat[first picture]{
            \includegraphics[width=5cm]{name}
            \label{img:first}
        }
        \subfloat[second picture]{
            \includegraphics[width=5cm]{name}
            \label{img:second}
        }
    \end{figure}

    This example shows that figure \ref{img:second} has a wrong reference number.
    But the complete float is referenced correctly: \ref{img:both}
\end{document}

And voilà the reference will be correct, even with KOMA-Script. (Please note that because of a bug in KOMA-Script the option "captions=figureheading" used to have no effect. This was fixed in version 3.11b of KOMA-Script. If you have to deal with older versions of KOMA-Script and updating is not an option, try using \captionabove instead of \caption.)