Figure/Table floats and \label placement: must it follow \caption to bind the right number?
0 reputation · 05 Aug 2026, 04:31 UTC
0 reputation · 05 Aug 2026, 04:31 UTC
I want a reliable rule for where \label belongs inside figure and table environments so that \ref always prints the current float's number, across pdfLaTeX, XeLaTeX, and LuaLaTeX.
My understanding is that \caption steps the float counter, so a \label placed before it would record the previous counter value. Some templates I work with put \label first for readability, and I am unsure whether that is merely a style choice or an actual correctness bug. I am also unclear how this interacts with unnumbered \caption* and with caption-related packages or custom float environments that may redefine \caption.
\label-after-\caption strictly required for correct numbering in standard classes, or only in some package combinations?\label that follows an unnumbered \caption* — does it bind to anything usable?.aux entry for my class and package set?35275 reputation · 05 Aug 2026, 12:56 UTC
Yes. In standard LaTeX classes (article, report, book), \label must come after \caption — or be placed inside the caption argument — to bind to the float's number. A \label placed before \caption is not a style choice; it is a correctness bug that silently references the wrong counter (typically the current section number).
\label does not know which counter you intend. It records whatever counter was most recently stepped by \refstepcounter. Inside a figure or table environment, it is \caption that steps the float counter. So the sequence determines the binding:
\caption{...}\label{fig:x} — label captures the figure/table number. Correct.\caption{\label{fig:x} ...} — also correct; the counter is stepped before the caption text is typeset.\label{fig:x}\caption{...} — label captures whatever was stepped last, usually the section counter. \ref{fig:x} then prints something like "3.2" instead of the figure number, with no warning.If a template places \label before \caption and its cross-references appear correct, the likely explanation is that the references were never actually checked against the printed float numbers, or the document's section and figure numbers happened to coincide in early drafts. It is not engine- or class-dependent magic. Treat it as a latent bug and move the label.
\caption* (from the caption package) typesets a caption without stepping the counter. A \label after it therefore has no new float number to bind to — it captures the previously stepped counter, exactly like the before-\caption case. If you need a referenceable float, use a numbered \caption. For an unnumbered float, referencing by number is not meaningful; use \pageref if you only need the page.
The counter-stepping behavior is identical across pdfLaTeX, XeLaTeX, and LuaLaTeX, because it lives in LaTeX's macro layer, not the engine. Packages such as caption, hyperref, or custom float environments may redefine \caption, but well-behaved redefinitions still step the counter inside the caption command, so the after-\caption rule holds. (If you use a heavily customized float package, verify as below rather than assuming.)
Compile twice, then inspect the .aux entry for your label:
grep 'fig:x' mydoc.aux
You will see a \newlabel{fig:x}{{1}{2}...} entry (or the \newtarget equivalent with hyperref). The first brace group is the captured counter value. If it matches the printed float number, placement is correct; if it shows the section number, the label is in the wrong position. A minimal test document with one figure and deliberately swapped label positions makes the difference obvious in a single compile.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.