# Problem with label and references and subfig/subcaption

Dear stackexchange users,

I want to use subfigures in my document but the solution I got so far scrambles my ability to use labels and references in my documents.

\documentclass{scrartcl}

\usepackage{subcaption} % both packages throw me an error
\usepackage{subfig} % so choose one

\begin{document}
\begin{figure}
\label{fig:first}
\caption{First figure}
\end{figure}

Now I refer to it the first figure by~\ref{fig:first}.

\end{document}


No matter how often I compile my document, I always get the messages

Package caption Warning: \label without proper reference on input line 8.

LaTeX Warning: Reference fig:first' on page 1 undefined on input line 12.

LaTeX Warning: There were undefined references.


and the documents shows ?? instead of the references. The caption package also has no solution to that specific error message.

Regards Jakob

• May 16, 2018 at 17:05

There are basically three way to add a label:

• before caption - wrong (the caption increments the counter of figures, so you have to not put the label before the counter is updated)
• inside caption - right
• after caption - right

In the following a basic example with ducks and showlabels package should help you understand.

\documentclass[convert]{article}
\usepackage{tikzducks}
\usepackage[inline]{showlabels}
\begin{document}
\begin{figure}[h]
\centering
\label{fig:before}
\begin{tikzpicture}
\randuck
\end{tikzpicture}
\caption{A duck with a label before caption.}
\end{figure}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\randuck
\end{tikzpicture}
\caption{A duck with a label inside caption.\label{fig:inside}}
\end{figure}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\randuck
\end{tikzpicture}
\caption{A duck with a label after caption.}
\label{fig:after}
\end{figure}
There are three ways in which you can put a label. The first one is in fig.~\ref{fig:before} and as you can see is wrong. The other two, the ones in fig.~\ref{fig:inside} and fig.~\ref{fig:after} are both right. But as you can see using the package \texttt{showlabels} they are in different places.
\end{document}


• Thanks, that worked. Aug 30, 2019 at 9:33

If you place the label after the caption, the code should work:

    \begin{figure}
\caption{First figure}\label{fig:first}
\end{figure}
`