LaTeX: the order of \caption and \label matters in floats

13 December, 2005 - 18:14
Categories:

The order of \caption and \label declarations matter in LaTeX floats (tables and figures). It is important to get references to the float right. The \label should come after the \caption or even inside the \caption environment. If you put the \label before the \caption you will get a reference to the (sub)section where float is declared instead of a reference to the float itself.

Consider the following LaTeX code:

\documentclass{article}
 
\begin{document}
 
\section{Introduction} %%%%%%%%%%%%%%%%%%%%%%%%%%
Introductions are boring.
 
\section{Animals} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Animals are nice.
 
\subsection{Lion} % % % % % % % % % % % % % % % %
Lions are yellow.
\begin{figure}
    \begin{center}\fbox{I'm a Lion, ghhrawlll}\end{center}
    \label{fig:lion}\caption{a lion}
\end{figure}
 
\subsection{Goat} % % % % % % % % % % % % % % % %
Goats are green.
\begin{figure}
    \begin{center}\fbox{I'm a goat, mheee}\end{center}
    \caption{a goat}\label{fig:goat}
\end{figure}
 
\section{Conclusions} %%%%%%%%%%%%%%%%%%%%%%%%%%%
For lions see Figure~\ref{fig:lion}.
For goats see Figure~\ref{fig:goat}.
 
\end{document}

As you can see the lion figure has a \label before its \caption. The goat has it the other way around.
Running it through latex gives the following result:

the resulting dvi

19 June, 2006 - 10:23

Jouw homepagina in IE

Erik (not verified)

Beste Stefaan,

Jouw homepagina wordt niet helemaal correct weergegeven in de IE browser ...
Ook de "Valiadte XHTML" ins niet helemaal correct.

Voor de rest mijn waardering voor de prachtige website ...

Erik

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options