# Referenced equation numbers not shown in unreferenced equations

I like very much the mathtools option showonlyrefs which allows that labels of an equation are displayed only, if there is a reference to the equation. The problem is, that references in an equation are supressed, if the equation has a label that is never referred. The following MWE is more understandable:

\documentclass{article}

\usepackage{mathtools}
\mathtoolsset{showonlyrefs}

\begin{document}

CASE A: equation with reference has no label
$$1 = 1 \label{eq1a}$$
$$1 = 1 \qquad \text{(see \eqref{eq1a})}$$

\hrulefill

CASE B: equation with reference has label and is referenced itself \eqref{eq2b}
$$1 = 1 \label{eq1b}$$
$$\label{eq2b} 1 = 1 \qquad \text{(see \eqref{eq1b})}$$

\hrulefill

CASE C: equation with reference has label but is not referenced itself
$$1 = 1 \label{eq1c}$$
$$\label{eq2c} 1 = 1 \qquad \text{(see \eqref{eq1c})}$$

\end{document}


Which produces (after enough compilation runs)

Case A and B behave like expected, in case C however there is no reference (4).

While it is easy to find a workaround (just delete the label) if there is no reference to the equation, I would prefer to have a automatic solution.

• Even though the name invites you to do so, don't use minimal for minimal examples. – Johannes_B Mar 31 '15 at 15:59
• @Johannes_B Thanks for the hint, with article the problem is the same, I will update the MWE – crixstox Mar 31 '15 at 16:01
• i tried it with amsart both with multiple equations and with gather. all with the same result. looks like a bug to me. – barbara beeton Mar 31 '15 at 16:46
• @barbarabeeton OK, I will write a bug report to the maintainer of the mathtools package. – crixstox Mar 31 '15 at 17:20
• @barbarabeeton, see my answer below. If interested the problem tracks down to \MT_extended_maketag:n. – daleif Apr 13 '15 at 10:53

It is recommended to use

\mathtoolsset{showonlyrefs,showmanualtags}


whenever you use showonlyrefs

I'm not quite sure what the best solution is to avoid having to add showmanualtags (the original author always recommended using both).

The problem here ends up in the internal logic. The showonlyrefs feature hooks into the typesetting of the equation number. This is used in two places:

1. typesetting the equation number in the displayed math, and
2. in \eqref

The problem here is that \eqref is actually being used inside a displayed math env, and thus ends up testing against the env label and not the given label.

Issuing showmanualtags circumvents this.

An even better solution might be to simply patch the \eqref version being used when showonlyrefs is active, using this:

\MHInternalSyntaxOn
\def\MT_extended_eqref:n #1{
\protected@write\@auxout{}
{\string\MT@newlabel{#1}}
\textup{\let\df@label\@empty\MT_prev_tagform:n {\ref{#1}}}
}
\MHInternalSyntaxOff


The trick is the emptying of \df@label when ever the tagform is being used in a \eqref context.

I think I'll add this to the next mathtools release.