2

In my work as LaTeX typesetter I often deal with multiply defined labels warnings. Most of them are about equations.

My question is, do the \ref command "always" refer to the last labelled item? In my experience it is so, but I'd like to know if there are cases (e.g. settings o packages) in which this is not true.

E.g. if I have:

\documentclass[11pt]{article}

\begin{document}

\section{Introduction}

Equation~(\ref{eq1})
\begin{equation}
1 +1 =3
\label{eq1}
\end{equation}
Equation~(\ref{eq1})
\begin{equation}
2 +2 =5
\label{eq1}
\end{equation}

\end{document}

both refs refers to the last equation.

Note. We always ask the author(s) to check about the warning but my company policy is to remove the warning "before" sending the proofs. I'm searching for a "scripting" way to manage these cases, commenting all the multiply defined labels except for the last one, yet I need to know if this is a safe approach.

  • The \ref{somelabel} command refers to a counter labelled somelabel. So if you have this message, it means you have some equations with the same label. B.t.w., for equations, you should use \eqef if you want to have the parentheses around the equation number. – Bernard Jul 2 '17 at 14:42
  • 2
    If one of the label is referenced you can't know which one the author meant. As a typesetter you really shouldn't delete one of the \labels. Make a clear note and let the author sort this out. – Ulrike Fischer Jul 2 '17 at 15:15
  • I'm not the guy that makes the rules in my company and, yes, we always ask the authors to check. (See the comment to egreg answer). – Gabriele Nicolardi Jul 2 '17 at 15:21
2

If you look at the .aux file, then you see

\relax 
\@writefile{toc}{\contentsline {section}{\numberline {1}Introduction}{1}}
\newlabel{eq1}{{1}{1}}
\newlabel{eq1}{{2}{1}}

As part of \begin{document}, LaTeX reads the .aux file (as produced in the previous run) and the command \newlabel{eq1}{{1}{1}} essentially does

\global\@namedef{r@eq1}{{1}{1}}

which is then overridden by the later

\global\@namedef{r@eq1}{{2}{1}}

with the effect that \ref{eq1} will point to equation 2, because \ref{eq1} checks for the macro \r@eq1 (produced with \csname, so numbers are legal in it) to be defined and, in case, it uses the contents in the first brace pair from its replacement text (\pageref uses the second).

When \end{document} is processed, the .aux file produced in the current run is read in, but \newlabel does different things, among which checking whether the label has already appeared.

The solution is very simple: never duplicate labels. In case there are, you need to ask the author to fix them. Using the last one may not be the author’s intention.

  • I agree with you about "never duplicate labels", but I'm only a typesetter, not the author of the papers I work on. I know about the .aux file stuff but I need to know if some package or some sort of "break-command" within the document can change this behaviour. – Gabriele Nicolardi Jul 2 '17 at 14:52
  • @GabrieleNicolardi In my experience, duplicate labels usually stem from authors using a “template” or editor macros that automatically provide a \label. If the argument to \newlabel is clearly a dummy (empty, a bullet or whatnot), you can safely ignore it. Otherwise you, as the typesetter, cannot guess. – egreg Jul 2 '17 at 15:03
  • In my experience often this happens when authors copy and paste an equation. Let me explain. Of course we always ask a final check to the authors, but this is a "minor issue" and we can publish the paper in case we do not receive an answer. The guidelines tell me to refer to the received .pdf copy of the manuscript. I'm trying to understand if it is really necessary to look at this pdf or if I can safely keep the last \label command. – Gabriele Nicolardi Jul 2 '17 at 15:17
  • 1
    @GabrieleNicolardi No, you can't, in my opinion. – egreg Jul 2 '17 at 15:22
  • 1
    @GabrieleNicolardi There is code on the site attempting at allowing duplicate labels (with different references, of course), but in this case the warning should not appear. – egreg Jul 4 '17 at 6:52

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.