# mhchem/amsmath seems to change the @eqnnum macro so I can't change equation number colors globally. How would I fix this issue?

Thanks to cfr How would I get custom-colored equation numbers as simply as possible? for providing a hint on how to do this without the mhchem/amsmath packages installed. To see what I mean, Here is a MWE. If I comment out the \usepackage[version=4]{mhchem} line, I get the desired result. It seems that mhchem must redefine the \@eqnnum macro somehow.

\documentclass{article}
\usepackage{xcolor,etoolbox}
\usepackage[version=4]{mhchem}
\definecolor{SAEblue}{rgb}{0, .62, .91}
\makeatletter
\patchcmd{\@eqnnum}{\normalcolor}{\color{SAEblue}}{\typeout{eqnnum patch: OK!}}{\typeout{eqnnum patch: Oh, dear!}}
\begin{document}
$$1 + 2 = 3\label{eq:siml}$$
\end{document}


I searched through mhchem.sty, mhchem.tex, hpstatement.sty, and rsphrase.sty. It's all a little above my head, but I didn't see anything obvious that would cause this issue. My next step was to see what packages were required by mhchem, and I came up with the following list:

expl3, l3keys2e, l3regex, calc[1998/07/07], amsmath, chemgreek[2015/04/09], graphics, ifthen, textcomp, babel, twoopt


None of these cause problems except for amsmath. The following MWE works fine until you uncomment the amsmath line:

\documentclass{article}
\usepackage{xcolor,etoolbox}
\usepackage{expl3}
\usepackage{l3keys2e}
\usepackage{l3regex}
\usepackage{calc}[1998/07/07]
%\usepackage{amsmath}
\usepackage{chemgreek}[2015/04/09]
\usepackage{graphics}
\usepackage{ifthen}
\usepackage{textcomp}
\usepackage[american]{babel}
\usepackage{twoopt}
%\usepackage[version=4]{mhchem}
\definecolor{SAEblue}{rgb}{0, .62, .91}
\makeatletter
\patchcmd{\@eqnnum}{\normalcolor}{\color{SAEblue}}{\typeout{eqnnum patch: OK!}}{\typeout{eqnnum patch: Oh, dear!}}
\begin{document}
$$1 + 2 = 3\label{eq:siml}$$
\end{document}


Uncommenting amsmath will make the color go back to black (but there is no error).

In the amsmath.ins, I see the following:

\iftagsleft@
\def\@eqnnum{\hbox to1sp{}\rlap{\normalfont\normalcolor
\hskip -\displaywidth\tagform@\theequation}}
\else
\def\@eqnnum{{\normalfont\normalcolor \tagform@\theequation}}
\fi


I would think that by me redefining the \@eqnnum through the patch, this would not make a difference, but I honestly have only a small glimpse of how all this stuff really works.

At this point, I am seeking the guidance of more experience community members to get this to work with the amsmath/mhchem packages in use.

• not an expert on patching, but it's possible that the multiple definition of \@eqnnum in amsmath is relevant. your tags are probably on the right, and if only the first instance of \@eqnnum + \normalcolor is patched by your code, the second one would remain in the original form. you don't say whether this just doesn't work, leaving the numbers black, or whether it results in an error. that is necessary information. – barbara beeton Feb 12 '16 at 18:04
• mhchem loads amsmath, so the real 'villain' is amsmath here, but I could not figure out the cause for this – user31729 Feb 12 '16 at 18:28
• @barbarabeeton, there is no error, it just leaves the numbers black if amsmath is loaded. – R.K.G. Feb 12 '16 at 19:16
• I also tried just repeating the patch multple times... this has no effect. – R.K.G. Feb 12 '16 at 19:25
• you might try, rather than patching, just redefining \@eqnnum, replacing \normalcolor by the color you want. be sure to wrap it between \makeatletter ... \makeatother. – barbara beeton Feb 12 '16 at 20:17

You're using the wrong patch, because, when amsmath is loaded, the printing of the equation number is not delegated to \@eqnnum any more or, at least, it is in a different way. See, for example, adjust alignment of equation numbers with leqno

\documentclass{article}
\usepackage{xcolor,etoolbox}
\usepackage[version=4]{mhchem}

\definecolor{SAEblue}{rgb}{0, .62, .91}

\makeatletter
% detach \eqref and \tag making
\renewcommand{\eqref}[1]{\textup{\eqreftagform@{\ref{#1}}}}
\let\eqreftagform@\tagform@
\def\tagform@#1{%
\maketag@@@{\color{SAEblue}(\ignorespaces#1\unskip\@@italiccorr)}%
}
\makeatother

\begin{document}

In equation~\eqref{eq:siml} we state a truth.
$$1 + 2 = 3\label{eq:siml}$$

\end{document}


If you want that also \eqref prints the equation number in blue, comment out the two lines before \def\@tagform@.

• This worked perfectly in my class file, thanks! – R.K.G. Feb 12 '16 at 21:02

I have a 'hacky' solution:

mhchem loads amsmath, so omitting amsmath will cause the wrong colour setting anyway if mhchem is included.

The culprit is \gdef\df@tag{\tagform@\theequation} in \endmathdisplay@, in my point of view, omitting the colour change done by \@eqnnum. I've used another patch and introduced a general \mathtagcolor, that's defined to be the SAEblue colour here.

\documentclass{article}
\usepackage{xcolor}
\usepackage{xpatch}
\usepackage{expl3}
\usepackage{l3keys2e}
\usepackage{l3regex}
\usepackage{calc}[1998/07/07]

\usepackage{amsmath}
\usepackage{chemgreek}[2015/04/09]
\usepackage{graphicx}
\usepackage{ifthen}
\usepackage{textcomp}
\usepackage[american]{babel}
\usepackage{twoopt}

\newcommand{\mathtagcolor}{\normalcolor}

\definecolor{SAEblue}{rgb}{0, .62, .91}

\renewcommand{\mathtagcolor}{SAEblue}

\usepackage[version=4]{mhchem}

\makeatletter
\xpatchcmd{\@eqnnum}{\normalcolor}{\color{\mathtagcolor}}{\typeout{eqnnumpatch: OK!}}{\typeout{eqnnum patch: Oh, dear!}}
\xpatchcmd{\endmathdisplay@a}{%
\tagform@\theequation
}{%
\color{\mathtagcolor}\tagform@\theequation
}{\typeout{patching \protect\endmathdisplay@a\ succeeded}}{\typeout{patching \protect\endmathdisplay@a\ failed}}
}{}
\makeatother

\begin{document}

$$1 + 2 = 3\label{eq:siml}$$
\end{document}