Generating correct labels for referencing forms part of a two-step process:
A counter is increased using \refstepcounter
(a "referencable" form of counter incrementation). This typically happens when you call a macro (like \section
) or environment. Here's a snapshot of \refstepcounter
from latex.ltx
:
\def\refstepcounter#1{\stepcounter{#1}%
\protected@edef\@currentlabel
{\csname p@#1\endcsname\csname the#1\endcsname}%
}
See that \refstepcounter
is a "modified \stepcounter
", since it also updates \@currentlabel
.
When inserting a \label
, the default behaviour of LaTeX is to write the macro \@currentlabel
and \thepage
to the .aux
file as arguments to \newlabel
. Here's a snapshot of \label
from latex.ltx
:
\def\label#1{\@bsphack
\protected@write\@auxout{}%
{\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
\@esphack}
Generally speaking, \newlabel
here is just a mechanism built in to check whether a label already exists/not and warn you thereof (in the form of "There were multiply-defined labels" or "Label `...' multiply defined").
So, whenever a \refstepcounter
is performed, \@currentlabel
is updated and therefore would change any reference created with a \label
. The most commonly identified problem of this incorrect labelling is when users perform (say)
\begin{figure}
% Image stuff here
\label{mylabel}
\caption{My caption}
\end{figure}
and receive an incorrect reference to their figure. This is because \caption
executes \refstepcounter
and not the figure
environment itself, resulting in an incorrect \@currentlabel
to be written to the .aux
file (whatever that may be). A practical example:

\documentclass{article}
\begin{document}
\section{A section}
See Figure~\ref{fig:ref} in section~\ref{sec:ref}.
\section{A section} Here is some text. \label{sec:ref}
\begin{figure}[bh!]
\centering\rule{.8\textwidth}{.2\textheight}
\label{fig:ref}\caption{A figure caption}
\end{figure}
\end{document}
Note how the figure reference \ref{fig:ref}
returns 2
instead of 1
. That's because an issue of \label{fig:ref}
write \@currentlabel
which is still set to 2
by the preceding \section
. Switching around \label
and \caption
would yield the desired result (however simplistic).
Bottom line is that there's an appropriate placement of \label
with respect to counters, otherwise references might be incorrect. The emphasis here denotes the fact that some environments gather content a post-process them, possibly correcting for inappropriate placements of things like \label
.
\label
; in your example the reference works as expected... Presumably this is a stripped-down version of the actual problem?~
, what you get is a regular breakable space.