12

In the comments on Martin Scharrer's answer to Why does an environment's label have to appear after the caption? there is a discussion of whether one ought to put \label inside of or after \caption. From reading the comments, one gets the sense that, in most cases, it is inconsequential whether one puts \label either inside the argument to a command or after the command and that putting it after is possibly preferred, as, according to Axel's quote of Leslie Lamport, "A label can appear in the argument of a sectioning or \caption command, but in no other moving arguments".

That is to say, this quote might suggest, then, that putting \label after a command is a best practice, as it will always work, even in the case of "moving arguments".

(Note: I realize this is not literally true (else I wouldn't be asking this question), but it seems to me that one might reasonably assume that this is so, based on that quote.)

Yet, when \labeling \footnotes, \ref only works properly when the \label is inside the \footnote, not after. Why is this the case?

\documentclass{article}

\begin{document}

Text.\footnote{\label{fn:test-1}This is a footnote.}

More text.\footnote{This is another footnote.}\label{fn:test-2}

Let's reference fn.~\ref{fn:test-1}, and let's also reference fn.~\ref{fn:test-2}

\end{document}

enter image description here

An answer that touches on both the reason for why this is so as well as why it was designed to be so would be preferred to an answer that just touches on why this is so.

0
12

Let's see some code in the kernel:

% latex.ltx, line 6031:
\long\def\@footnotetext#1{\insert\footins{%
    \reset@font\footnotesize
    \interlinepenalty\interfootnotelinepenalty
    \splittopskip\footnotesep
    \splitmaxdepth \dp\strutbox \floatingpenalty \@MM
    \hsize\columnwidth \@parboxrestore
    \protected@edef\@currentlabel{%
       \csname p@footnote\endcsname\@thefnmark
    }%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}%

When one does \label{foo}, LaTeX associates to foo the current value of \@currentlabel; most of the times, this is done by \refstepcounter

% latex.ltx, line 3888:
\def\refstepcounter#1{\stepcounter{#1}%
    \protected@edef\@currentlabel
       {\csname p@#1\endcsname\csname the#1\endcsname}%
}

but in the case of footnotes, the job is done manually because of the optional argument to \footnote and the fact that one can split the footnote into \footnotemark and \footnotetext.

Now the thing to be noticed is that \protected@edef\@currentlabel{...} happens after

\insert\footins{

The command \insert is a TeX primitive; it does several things, one of which is equivalent to doing

\setbox\footins=\vbox{

and we've found the “crime scene”: the material from { up to the matching } is evaluated (expanded and executed) in a group, including the definition of \@currentlabel.

Thus, if you say

\footnote{Some text}\label{foo}

the value of \@currentlabel referring to the footnote number has been forgotten because the group where it was set has been closed. The only way out is to do

\footnote{\label{foo}Some text}

so that \label will be executed when the value of \@currentlabel is the right one.

1
  • 1
    I would be inclined to say that this is status-by-design, since I think it's rare that one would have multiple references to the same footnote. Rare, not impossible, so the handling may be different from the end-user's perspective.
    – Werner
    Feb 13 '14 at 20:23
3

The \label command always generates a label that will result in the number of the lowest-level numbered environment (or command) that contains it. In your example, this is the document itself, which doesn't have a number---so no number appears when the \ref command is issued.

The confusion in the case of the \caption command is that the label there actually corresponds to the table, figure, etc. that contains it. Captions themselves are not numbered, but they control whether or not other environments that contain them are.

If you create three tables, but only put a caption on the final one, it will be labelled as "Table 1", not "Table 3". This is as it should be. If it weren't that way, I'd wonder whether I was missing Tables 1 and 2, spend some time thinking the typesetters had goofed during editing, or both.

For this reason, \begin{table} can't be relied upon to increment the table number because not all tables are numbered. But all tables with captions are numbered, which is why the task of generating the appropriate number is delegated to the \caption command. Again though, while \caption handles the counters for its containing environments, it isn't actually numbered itself. This also is how it has to be, to avoid having one figure and one table in a document, labelled "Figure 1" and "Table 2".


That was basically the entire answer, but here are a couple of examples that clarify what I meant.

First, here's one that shows that the \label{fn:test-2} command in your example does work, it just doesn't have anything to display. Look at what happens when some \section commands mean that the \label has something to number:

\documentclass{article}

\begin{document}

\section{A section}
\section{Another}
\section{One more}
Text.\footnote{\label{fn:test-1}This is a footnote.}

More text.\footnote{This is another footnote.}\label{fn:test-2}

Let's reference fn.~\ref{fn:test-1}, and let's also reference fn.~\ref{fn:test-2}

\end{document}

Question code now in the third section

Note that the result of \ref{fn:test-2} is now 3, the number of the section the label command appears in. This is because \section was the last command that generated a number at the same or higher grouping level to the text within that section, and the \label command appears within that section.


Now consider the equivalent of your example for a table environment (which, remember, is what the \caption command is actually generating the number for). It would look something like this,

\documentclass{article}

\begin{document}

\section{Demo}
\begin{table}
    \begin{tabular}{c}
        cell 1
    \end{tabular}
    \caption{This is the \emph{Table}'s caption}
\end{table}\label{tab:test}

\end{document}

The \label command comes immediately after the thing it might ostensibly be expected to label, just as it does when you write \footnote{}\label{}. But hopefully it's fairly obvious to you that this won't (and shouldn't) create a label for the table that it's next to, but not part of.

2
  • + 1 for the example showing that my example does work when there is something for \label to label. Though I'm not sure the analogy with tables and figures at the beginning of your answer is the most apt. It's true that not all tables and figures need be given a number in a document, but all footnotes are numbered.
    – Adam Liter
    Feb 17 '14 at 5:35
  • But to me that's the entire point of why you can put the label either in or after the caption, but it has to be inside the footnote, not after it. It's not an analogy to tables, it's that when you use the caption and label commands together, you are actually labeling the table... Which the label does have to be inside. Feb 17 '14 at 9:07

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.