80

One of my sections seems to be using the section number rather than the appropriate figure number for \ref's that point to figures. (Theorems and the like work fine.) As far as I can tell, there's nothing differentiating the problematic section from the other sections, but there must be as the problem doesn't go away when I compile that section on its own. Any ideas?

Edit: an example of a broken figure. (Every figure in the section is broken.)

\begin{example}
  Figure~\ref{fig: hasse} depicts ...
\begin{figure}[htbp]\label{fig: hasse}
\begin{center}
\begin{tikzpicture}
    \tikzstyle{every node}=[draw,circle,fill=black,minimum size=4pt,
                            inner sep=0pt]
    [bunch of lines for the picture]    
\end{tikzpicture}
\caption{Hasse diagram}
\end{center}
\end{figure}
\end{example}

This particular instance had the figure inside the example, but that doesn't make a difference.

  • 9
    Perhaps you did not use \caption before \label in that figure environment. Please show the code, so we don't have to guess. – Stefan Kottwitz Jun 1 '11 at 21:31
  • To clarify, it's an problem throughout the section, not just on one figure (only the one broken section, though). – hoyland Jun 1 '11 at 21:41
  • 9
    Do as Stefan has suggested: place the \label command after the corresponding \caption command (same remark applies to all floating environments). Instead of the center environment you could use the \centering command right after \begin{figure}[htbp]; the environment adds extra vertical space which (in most cases) is undesired – Gonzalo Medina Jun 1 '11 at 21:44
104

Move \label{fig: hasse} after \caption{Hasse diagram} since \caption has to come before \label. This applies to figures and tables in general. I would not use spaces in label names. Also note Gonzalos comment regarding \centering.

\begin{example}
  Figure~\ref{fig:hasse} depicts ...
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
    \tikzstyle{every node}=[draw,circle,fill=black,minimum size=4pt,
                            inner sep=0pt]
    [bunch of lines for the picture]    
\end{tikzpicture}
\caption{Hasse diagram}
\label{fig:hasse}
\end{figure}
\end{example}
  • Thanks, I completely failed to parse your original comment correctly. The single section thing was a red herring--I was the one who did the figures in that section. Someone else did the others. – hoyland Jun 3 '11 at 12:36
  • 7
    What a simple solution to a problem that was driving me crazy! – Jus12 Nov 28 '15 at 7:31
  • But this results in the pdf not moving to the image but its caption (so image might still be hidden) when clicking on the reference link – Parthiban Rajendran Feb 25 at 11:24
  • @ParthibanRajendran Sure! If you use hyperref, there's an adjustment so links lead to the top of the image. But this question is not about hyperref, so your link click question should be separate (and probably exists on this site with the solution). – Stefan Kottwitz Feb 25 at 11:48

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.