144
\documentclass{article}
\usepackage{algorithm}

\begin{document}
\setcounter{section}{54}
\section{A section with no label}
\begin{algorithm}
\label{myalg}
\caption{An algorithm with the label `myalg'.}
\end{algorithm}
Reference to myalg: \ref{myalg}.
\end{document}

With this code, the reference givens 55 (the section number); if I move the label after the caption command, I get the correct reference (1).

Why should this be the case? I find it confusing: when thinking about an algorithm as an environment, you expect to be able to label it right after you began it. At the very least I would expect some kind of error or "??" to appear, rather than the previous environment or reference-able entity being used.

2 Answers 2

184

This is in common with all captions: it's the \caption command that generates the number, so a \label before it can't know that number.

This could have been designed differently, of course, but you can think to a figure environment where two figures with distinct captions are placed, side by side in order to save space, for example.

So, always place the \label after the \caption.

Since \label is a "general purpose" command, it will always refer to the last previously generated number (be it a chapter, section, figure caption number or directly from the user with a \refstepcounter command) and to the page where it eventually falls. Actually it's not really the last generated number, but the one lastly generated at the same or higher grouping level.

7
  • 11
    @EyalRozenberg The example was just to explain the rationale for the programmer's choice. An algorithm environment doesn't generate a number; it's the \caption that does it: maybe you won't, but someone else might want to keep together two algorithmic environments (each one with its caption), to show the differences between them, for instance.
    – egreg
    Commented Oct 22, 2011 at 9:03
  • 9
    When someone asks me why the label has to be after something and not before I always answer: "Well, the label of a bottle is also on it's outside and not on it's inside to make sense."
    – Foo Bar
    Commented Mar 8, 2013 at 19:19
  • 4
    This is why LaTex needs to be replaced. Commented Aug 11, 2015 at 18:28
  • 8
    @Konstantin Feel free not to use it. What do you propose?
    – egreg
    Commented Aug 11, 2015 at 19:11
  • 4
    @egreg I am using it for the lack of a better alternative. It is the best of all bad solutions. I respect and appreciate all those who work on it. It just isn't good (yet). Commented Aug 11, 2015 at 20:22
34

The \caption macro increases the used counter and sets the current label text which is used by \label. If you use \label before it the old label text is used instead, which leads to a wrong number. Always use \label after \caption and not before or in it.

8
  • 2
    Why not use \label in \caption? Laslie Lamport writes: "A label can appear in the argument of a sectioning or \caption command, but in no other moving arguments."
    – user2574
    Commented Oct 22, 2011 at 10:06
  • 3
    The rule of thumb is to put it behind it. Feel free to do it otherwise. I'm not the caption police. Special stuff like \subfloat work anyway different. I just have the feeling that beginners get explicitly told to put it inside, and there is no need for that. Commented Oct 22, 2011 at 10:39
  • 5
    "The rule of thumb is to put it behind it." Do you have any reference for that? Sorry for being so insistent, the problem I have is that I'm actually the caption police for many people. Currently I tell beginners to put the \label inside the \caption, and if this is wrong in some way I really would like to know. (Yes, there is no need for putting \label inside \caption, but there is IMHO no need for putting \label after \caption, either.)
    – user2574
    Commented Oct 22, 2011 at 11:19
  • 3
    No, there is nothing technically wrong to put \label inside \caption. But because it isn't part of the caption text it, IMHO, doesn't belong there from a logical point of view. Also from a programming style point of view it is good to keep things consistent: If you can't put \label always into the argument of the to-be-labeled macro then you shouldn't do it in any case. Of course you can disagree with this and take \caption (and related) different than sectioning macros etc. Commented Oct 22, 2011 at 11:31
  • 2
    One can easily reverse that: If you can't put \label always after the to-be-labeled macro then you shouldn't do it in any case. IMHO both are wrong, because you simply can't always do it the same way, because macros are implemented in a non-consistent manner regarding labeling. (Another example: supertabular) \refstepcounter will increase the counter globally, but unfortunately defining the labeling stuff only locally. I wonder why Lamport has implemented in this way. Hmm, maybe I should rewrite \captionbox and \subcaptionbox so a \label after them would give a valid result, too?
    – user2574
    Commented Oct 22, 2011 at 11:59

You must log in to answer this question.

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