[Tex/LaTex] empheq and changing location of equation numbers

empheq

I am using the empheq/tcolorbox packages to put colored boxes around my equations but would like to place the equation numbers in the right margin.

In the past, I have used the following code to put equation numbers in the right margin and it has worked outside of the empheq environment. If I use it within empheq, I get no equation numbers but the references exist.

\makeatletter
\let\mytagform@=\tagform@
\def\tagform@#1{\maketag@@@{\hbox{\rlap{\hspace{0.5in}(\ignorespaces#1\unskip\@@italiccorr)}}}\kern1sp}
\renewcommand{\eqref}[1]{{\mytagform@{\ref{#1}}}}
\makeatother

Below is a MWE. \margtrueplaces equation numbers in margin while \margfalse will exhibit the "normal" behavior.

\documentclass{article}
\usepackage{showframe}
\usepackage{amsmath}
\usepackage{empheq}
\usepackage[most]{tcolorbox}
\newif\ifmarg
\margtrue

\ifmarg
    \makeatletter
    \let\mytagform@=\tagform@
    \def\tagform@#1{\maketag@@@{\hbox{\rlap{\hspace{0.5in}(\ignorespaces#1\unskip\@@italiccorr)}}}\kern1sp}
    \renewcommand{\eqref}[1]{{\mytagform@{\ref{#1}}}}
    \makeatother
\fi

\begin{document}
    An equation with number in the right margin.
    \begin{gather}
        F = ma.
    \end{gather}
    And now an equation within an \verb|empheq| environment.
    \begin{empheq}[box=\tcbhighmath]{gather}
        F = ma.
    \end{empheq}
\end{document}

The \margtrue behavior is shown below. Note the second equation does not have any equation number displayed.

enter image description here

The \margfalse behavior is shown below. Note the second equation does have an equation number displayed (not in margin though).

enter image description here

Best Answer

Not initially sure why (see FOLLOW UP following this initial discussion), but I found that placing an \mbox{~} (or even just a ~) before the \hbox in the definition of \maketag@@@ makes it work. However, \mbox{} does not fix the problem (which rules out the issue being one of vertical versus horizontal mode.).

\documentclass{article}
\usepackage{showframe}
\usepackage{amsmath}
\usepackage{empheq}
\usepackage[most]{tcolorbox}
\newif\ifmarg
\margtrue

\ifmarg
    \makeatletter
    \let\mytagform@=\tagform@
    \def\tagform@#1{\maketag@@@{\mbox{~}\hbox{\rlap{\hspace{0.5in}(\ignorespaces#1\unskip\@@italiccorr)}}}\kern1sp}
    \renewcommand{\eqref}[1]{{\mytagform@{\ref{#1}}}}
    \makeatother
\fi

\begin{document}
    An equation with number in the right margin.
    \begin{gather}
        F = ma.
    \end{gather}
    And now an equation within an \verb|empheq| environment.
    \begin{empheq}[box=\tcbhighmath]{gather}
        F = ma.
    \end{empheq}
\end{document}

enter image description here

FOLLOW UP

The behavior seems dependent on whether the \maketag@@@ definition has non-zero width. For example, \makebox[0.001pt]{} or even \kern1pt prior to the \hbox works, too, but \makebox[0pt]{} does not. I suspect an \ifdim...>0pt test somewhere.

And here, Why isn't a zero-width box in math mode aligned on the baseline?, David says "A zero width equation number is used as a flag to get special handling" and then references the TeXbook (p.189): "One consequence of these rules is that you can force an equation number to appear on a line by itself by making its width zero."

I suspect it is this "special handling" that is interfering with the OP's original attempt, since the \margtrue setting forces the tag width to be zero, by virtue of it being an \rlap.

In fact, the TeXbook discussion, on page 188, defines e as the width of the equation number:

"Let q and e be zero if there is no equation number; otherwise let e be the width of the equation number

...

"If e [not equal] 0 and if there is enough shrinkability in the displayed formula h to reduce its width to zq, then list h is packaged in an hbox b of width zq. Otherwise e is set to zero, and list h is packaged in a (possibly overfull) hbox b of width min(w_0, z).

...

"If there was an \eqno and if e = 0, an infinite penalty is placed on the vertical list, followed by the equation number box a shifted right by s+ z minus its width, followed by a penalty item whose cost is the value of \postdisplaypenalty."

Related Question