25

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}
lockstep
  • 250,273
Display Name
  • 46,933
  • 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
  • This is relevant when using R-markdown (.Rmd) to generate pdf files with latex. The result of Figure caption \label{fig:1} is variant A. Thank you for asking this so we figured out that this is trivial. – Trygve Feb 04 '18 at 00:21

4 Answers4

20

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:

labels and showlabels

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
6

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.

Danie Els
  • 19,694
5

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.
N.N.
  • 36,163
1

How to fix paragraph's run-in heading on a single page before \afterpage and the text being on the first page after \afterpage?

The thread above illustrates a case for which the use of coding style B at the top (label within marker being labeled as in \caption{\label{fig:cat}This is a cat.}) is necessary to get correct result. Specifically, in that particular case, coding of \paragraph{title}\label{text} leads to bad page breaking in conjunction with the \afterpage package, whereas \paragraph{title\label{text}} does not.

Also, according to that thread, it is preferred to put the label within the item being tagged.

So it may be that location of label has other ramifications?

John
  • 2,400
  • It's better if you make this answer complete, instead of making us go to another thread. But that thread is dealing with \paragraph instead of \caption, and it seems that "within or immediately after" is preferred over "sometime later". That says to me that style A or B is preferred over "put the label a paragraph later" (which would be weird for labeling a figure). – Teepeemm Feb 09 '17 at 16:52
  • Ok, let me rephrase this a bit. – John Feb 10 '17 at 01:03