3

The following MWE produces an undesirable result, shown below:

\documentclass[11pt, a4paper, twoside]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[notref, notcite]{showkeys} 

\begin{document}

\begin{equation}\label{eqA}
b = 1
\end{equation}

\begin{equation}
\label{eqB}
x^p 
\overset{ \eqref{eqA} }{=} b^p
\overset{ \eqref{eqA} }{=} 1
\end{equation}

\end{document}

toomanylabels

As you can see, when using \eqref, it seems that showkeys reprints the label of eqB, which is not what I want (despite using notref). Does anyone know why this works like so, and if they is a way to correct this (better than replacing \eqref by \ref, which does somehow work properly)?

1
  • @campa : thank you! Interesting to see that the exact place of label matters!
    – Watson
    Jan 25, 2021 at 8:30

1 Answer 1

3

I illustrate the situation by a modified code:

\documentclass[twocolumn]{article} % twocolumn for smaller snapshot

\usepackage{amsmath}
\usepackage[notref, notcite]{showkeys} 

\begin{document}
A numbered equation
\begin{equation}\label{eqA}
b = 1
\end{equation}
Cite using \verb+\ref+
\begin{equation}\label{eqB}
\ref{eqA}
\end{equation}
Cite using \verb+\eqref+
\begin{equation}\label{eqC}
\eqref{eqA}
\end{equation}
Cite using \verb+\eqref+ with \verb+\label+ at the end
\begin{equation}
\eqref{eqA}
\label{eqD}
\end{equation}
Cite using \verb+\eqref+ without \verb+\label+
\begin{equation}
\eqref{eqA}
\end{equation}

\end{document}

enter image description here

As you see, the problem occurs whenever \label precedes \eqref, while \ref isn't touched.

What is happening here? The package showkeys redefines \label to do its original job and then to define a macro called \SK@lab:

% \label{FOO} does the usual stuff and also
\gdef\SK@lab{\smash{\SK@labelcolor\showkeyslabelformat{FOO}}}%

This macro \SK@lab will be then used when the equation number is printed. When the package amsmath is used, printing the equation number involves a macro called \tagform@ which, as the name suggests, control the appearance of the equation tag. To ensure that equations are cited with the same tag form, amsmath defines \eqref as a wrapper around \ref

\newcommand{\eqref}[1]{\textup{\tagform@{\ref{#1}}}}

Now the package showkeys comes into play: since printing the tag should also print the label besides it, showkeys redefines \tagform@ to

\let\SK@tagform@\tagform@ % make a copy of original definition
\def\tagform@#1{%
        % [...] other stuff
        \SK@tagform@{#1}%
        \rlap{\kern\marginparsep\SK@lab}\SK@lab@relax}%

where \SK@tagform@ is the original (amsmath's) definition of \tagform@. The \rlap stuff is what prints the label on the side. After this is done, the macro \SK@lab@relax (as the name suggests) \lets \SK@lab to \relax.

Now we see what happens: when you call \label inside an equation, the macro \SK@lab saves the current label to be used later by \tagform@ when the equation tag is printed; but \eqref also uses \tagform@, so the current value of \SK@lab is printed by mistake.

Long story short: if your equation contains some \eqref then put its \label after those \eqrefs.

6
  • Thank you very much for your detailed and complete answer. I will read it carefully later today, and try to understand it ;-)
    – Watson
    Jan 25, 2021 at 9:12
  • (and accept it ;-))
    – Watson
    Jan 25, 2021 at 9:12
  • @Watson Well, wait with the accepting. The author of showkeys is a regular user here so I'm pretty sure he'll have something to say ;-)
    – campa
    Jan 25, 2021 at 9:14
  • hmm possibly \SK@ref could locally restore \tagform@, possibly... Jan 26, 2021 at 8:56
  • @DavidCarlisle My message in chat was more of a joke... I think this situation should present itself seldom enough that putting \label later shouldn't be an issue. But maybe a caveat in the documentation...
    – campa
    Jan 26, 2021 at 8:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .