2

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

5

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}

ducks

2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.