4

I'm in the process of importing independent papers into a thesis document. Since I'm not as forward-looking as I wish I were, I have used some of the same labels in the papers (for introduction section, conclusions section, probably some equations too).

Now, when compiling the whole thesis, how does the compiler choose between which, say, \ref{sec:intro}, to refer to? Is it the closest \label{sec:intro} it will refer to? Or will it rank labels within the same chapter over same labels in other chapters?

  • 1
    My guess is the last label defined takes precedence, as they are written to the aux file, and re-read during the 2nd-pass compilation. The last definition will therefore be the one that holds through the recompilation. (I could be wrong). – Steven B. Segletes May 10 '16 at 15:04
  • 1
    I confirm what Steven says: the last one has the precedence. You'll receive warnings about multiply defined labels. – egreg May 10 '16 at 15:13
4

The \label command uses \newlabel in the background which does a check (well, actually \@newl@bel does this) about the labels (issues a warning if the label is already known) and generates the label anyway, using the last 'entity' that has provided the label name. (See the \label and \newlabel commands in latex.ltx.

The crucial point here is the \global\@namedef usage here:

\def\@newl@bel#1#2#3{{%
  \@ifundefined{#1@#2}%
    \relax
    {\gdef \@multiplelabels {%
       \@latex@warning@no@line{There were multiply-defined labels}}%
     \@latex@warning@no@line{Label `#2' multiply defined}}%
  \global\@namedef{#1@#2}{#3}}}

i.e. the label name is 'overwritten' again if the label already exists and only the last value is stored.

This means that preceding values are overwritten and always the last value us used.

In the following example, the value of 2 is printed, since the 2nd chapter label overrules the first one.

\documentclass{book}


\begin{document}

In \ref{firstchapter} we will see that

\chapter{Chapter First}\label{firstchapter}

\chapter{Second Chapter} \label{firstchapter}


\end{document}

Here's the corresponding .aux file:

\relax 
\@writefile{toc}{\contentsline {chapter}{\numberline {1}Chapter First}{3}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
\newlabel{firstchapter}{{1}{3}}
\@writefile{toc}{\contentsline {chapter}{\numberline {2}Second Chapter}{5}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
\newlabel{firstchapter}{{2}{5}}

enter image description here

Some edit -- The reference to the conclusion label will print 3.1 since this has been made for the section 3.1 (in this example)

\documentclass{book}


\begin{document}

In \ref{firstchapter} we will see that -- use the \ref{conclusion} for this!

\chapter{Chapter First}\label{firstchapter}

\section{Conclusion} \label{conclusion}
\chapter{Second Chapter} \label{firstchapter}


\section{Other conclusion} \label{conclusion}


\chapter{Last chapter}

\section{Conclusion that's not wanted} \label{conclusion}

\end{document}
  • (misunderstood, edited) Thank you for detailed answer. So when I write Section \ref{conclusion} concludes in the intro of Chapter 1, it will refer to the conclusion section in Chapter 2 (given same label in both)? – user3218615 May 10 '16 at 15:26
  • 1
    @user3218615: Yes, that's the way it's is set up. The (at least two) runs of latex will provide the relevant label/reference information, at the begin of the second run, the .aux file is read and will be used in typesetting, i.e. the last \newlabel (for a given label), that is found in the .aux file will be used to set the reference, even if there are (additional \label with the same name -- they will come into action after the next run) – user31729 May 10 '16 at 15:44

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.