# Is there any difference between nesting \label in \caption and putting \label outside \caption?

Is there any difference in behavior between the following coding styles?

## Coding style A: \caption does not enclose \label.

\begin{figure}
\includegraphics{cat.eps}
\caption{This is a cat.}
\label{fig:cat}
\end{figure}


## Coding style B: \caption encloses \label.

\begin{figure}
\includegraphics{cat.eps}
\caption{\label{fig:cat}This is a cat.}
\end{figure}

-
Does B not put the label also in the List of Figures? –  Martin Scharrer Jun 15 '11 at 16:01
I'm not sure about how LaTeX views the differences technically, but from a syntax or logical view one is labeling a figure, and one is labeling a figure caption. Presumably style A makes more sense. –  mankoff Jun 15 '11 at 17:53

I can't say for sure with ordinary LaTeX, but if you use the showlabels package (very useful when in draft mode) then the label is shown in a different place for the two.

Code:

\documentclass{article}
\thispagestyle{empty}
\usepackage{graphicx}
\usepackage{showlabels}
\begin{document}

\begin{figure}
\includegraphics{vignettes}
\caption{This is a cat.}
\label{fig:cat}
\end{figure}

\begin{figure}
\includegraphics{vignettes}
\caption{\label{fig:cat}This is a cat.}
\end{figure}
\end{document}


Result:

-
+1 for demonstrating a non-trivial difference. –  xport Jun 15 '11 at 13:11
Interesting. Style A makes sens for readability of drafts with showlables then. –  N.N. Jun 15 '11 at 13:16

As far as I know there are only two differences between coding style A and B:

1. A is the conventional order of the figure environment.
2. A is easier to read.
-
(2) is less objective. :-) –  xport Jun 15 '11 at 12:24
I think it is quite objective. It is easier to read just like the label produced by style A is easier to read in the answer of @Andrew Stacey. –  N.N. Jun 15 '11 at 13:18
I you leave your answer as is, it is still less objective. –  xport Jun 15 '11 at 14:22

There are no differences from a Latex internal command view. For example the command \addtocontents is defined as

\long\def\addtocontents#1#2{%
\protected@write\@auxout
{\let\label\@gobble \let\index\@gobble \let\glossary\@gobble}%
{\string\@writefile{#1}{#2}}}


In other word \label is made a no-op when \caption or \section pass arguments to the TOC and LOF. The same goes for \markboth, etc. commands to write section headers to the runnings head. On the other hand is the \if@nobreak switch used internaly at a lot of places to prevent \label to cause a page break, for example in the \@afterheading command after a section command. It is clear that LaTeX was designed to put labels inside or after the \caption and \sectionxxx commands.

Note that a label must NOT be put in the short argument of \caption or \section

\caption[\label{xx}Short capt]{Long capt}% DO NOT DO THIS


because the label will diseapear.

EXCEPTION: For footnotes the labels must be inside the footnote itself otherwise it is not defined and will refer to the current active counter, which will probably be a section counter.

-
+1 for emphasizing the critical cases. –  xport Jun 15 '11 at 14:44
What information can I get when using a footnote reference? Just curious... –  Christoph Jun 15 '11 at 14:46
@Christoph: It is usefull to have multiple footnote marks that refer to the same footnote see my answer in the post Multiple references to the same footnote ... The answer that were marked correct was not the best one :-( In the documentation of the refstyle package I give examples of this inside tables –  Danie Els Jun 15 '11 at 14:59
IIRC Lamport suggests putting \label inside \caption. –  Martin Schröder Sep 1 '11 at 19:50