2011-11-17

Where to put a label on a LaTeX figure

I just found out that LaTeX is sensitive as to where you put the \label command in a figure environment. For example if you do something like this:
\begin{figure}
  \centering
  \includegraphics[width=0.95\columnwidth]{sompic}
  \label{fig:some-pic}
  \caption{Some caption.}
\end{figure}
if you now do a \ref{fig:some-pic}, the reference will point to the parent element of the figure environment. For me, I got a reference to the subsection enclosing the figure. This was very irritating, because the text then said "see Figure 3.4.2.4 for details."
Instead you need to place the label in or after the \caption command. For some reason the \ref will then point to the figure:
\begin{figure}
  \centering
  \includegraphics[width=0.95\columnwidth]{sompic}
  \caption{Some caption.}
  \label{fig:some-pic}
\end{figure}
Update: And here is also the answer to the question why it is necessary to do it this way.