109

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.

3
  • 13
    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
    Commented Jun 1, 2011 at 21:31
  • To clarify, it's an problem throughout the section, not just on one figure (only the one broken section, though).
    – hoyland
    Commented Jun 1, 2011 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 Commented Jun 1, 2011 at 21:44

1 Answer 1

144

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}
5
  • 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
    Commented Jun 3, 2011 at 12:36
  • 14
    What a simple solution to a problem that was driving me crazy!
    – Jus12
    Commented Nov 28, 2015 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 Commented Feb 25, 2019 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
    Commented Feb 25, 2019 at 11:48
  • 2
    Faced with the problem in 2020, found the solution right away.
    – Erdem Tuna
    Commented Jul 15, 2020 at 8:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .