# Label in caption

For certain styles it appears that the \label statement must be included in \caption to preserve good referencing. Why is that?

• Could you give an example? – Daniel Egeberg Jul 26 '10 at 19:24
• For instance JSS template: jstatsoft.org . – user7 Jul 26 '10 at 19:58

Actually, \label must appear after (not necessarily inside) \caption, because the \caption command increments the counter to which \label's argument associates itself.

• +1: exactly. Putting it inside the \caption additionally guarantees that it can't get lost. I find myself putting \labels inside \section commands equally redundantly, for the same reason. – Norman Gray Jul 26 '10 at 20:27

\caption (re)defines a macro \@currentlabel, which \label then uses.

i've recently seen a post from someone who had written something like

{\itshape \caption{foo bar}}\label{foobar}


(anyone with any experience would baulk at that, but it has happened.)

then since \@currentlabel was defined inside a group, \label ended up referring to the last \caption that wasn't inside a group, since the value hadn't been exported from inside the group.

putting \label inside the caption solves the problem for this class of misguided people, in a "robust" way.

for only ordinarily-misguided people, it's enough just to say "put the \label immediately after the \caption (or whatever); don't have spaces or line breaks in between"

the grouping effect applies equally well for sections and other things that can be labelled.

• The problem is that you must sometimes use a template that messes that up. – user7 Dec 11 '10 at 12:46
• Thanks for the extra description of exactly how labeling works. +1. – Dom Mar 7 '11 at 17:10

mbq claims that some templates mess the label system up. if that's so, you just have not to use labels.

fwiw, i've downloaded and scanned the jss stuff that mbq mentioned, and see no indication that label processing is anything other than standard.