# Align on equal sign when using stackrel (by defining a single command)

Edit. I realized that my anwer was not complete. Indeed I missed to state that I need a solution that works together with normal aligned stuff (i.e. without the \stackrel command).

The first equation of the example must not be modified.

I have this MWE:

\documentclass[11pt]{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
x   &=   x \\
x   &\stackrel{\text{I'd like}}{=}  x\\
x   &\stackrel{\text{to align}}{=}  x\\
x   &\stackrel{\text{these equations}}{=}  x\\
x   &\stackrel{\text{on equal signs}}{=}  x
\end{align}

\end{document}


by defining a single command. I figure out something like that (please be merciful if you find it horrible!!!):

\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{mathtools}

\newcommand{\StackRel}[2]{\phantom{\stackrel{#1}{#2}}%
&\stackrel{\mathclap{#1}}{#2}%
\phantom{\stackrel{#1}{#2}}}

\begin{document}

\begin{align}
x   &=  x
x   \StackRel{\text{I'd like}}{=}  x\\
x   \StackRel{\text{to align}}{=}  x\\
x   \StackRel{\text{these equations}}{=}  x\\
x   \StackRel{\text{on equal signs}}{=}  x
\end{align}

\end{document}


but yet I need to halve (and to add to it the extra correct space) the \phantom width value (or, maybe, some other command).

The layout I'd like to achieve is something like:

Note. I need a quick-"single command" solution so I will define it in the preamble and than fix the aligned equations with a query-replace regexp.

• ...and each equation needs separate numbering, as shown. Correct? Aug 22 '17 at 17:32
• @StevenB.Segletes... Yes. But, in some case, I could encounter \nonumber or \notag commands. Aug 22 '17 at 17:51

\documentclass[11pt]{article}
\usepackage{mathtools,stackengine}
\stackMath
\newcommand{\stackEq}[1]{%
\setbox0=\hbox{${}\mathrel{\stackon[-1pt]{=}{\scriptstyle\text{#1\strut}}}{}$}
\xdef\tmpwd{\dimexpr\the\wd0\relax}
\kern.5\tmpwd\mathclap{\box0}&\kern.5\tmpwd
}

\begin{document}

\begin{align}
x   \stackEq{I'd like}  x\\
x   \stackEq{to align}  x\\
x   \stackEq{these equations}  x\\
x   \stackEq{on equal signs}  x
\end{align}

\end{document}


• I'm sorry. Your solution is right but my answer was wrong. Please read my editing to it. Also, I took a look to the stackengine doc and I'm wondering if it is better to use \ensurestackMath (in some way) instead of \stackMath. Aug 22 '17 at 20:50
• @GabrieleNicolardi If you are going to macro-ize it for frequent use, then for sure, \ensurestackMath would be the proper way to go. It would wrap around the \stackon macro...In fact, for a more permanent macro, I would replace \stackon[-1pt]{=}{\scriptstyle\text{#1\strut}} with \stackengine{-1pt}{=}{\scriptstyle\text{#1\strut}}{O}{c}{F}{F}{S}, so as to be immune from local changes in \stacktype and \stackalignment. Aug 23 '17 at 0:01

You can't have a “quick single command”. For this ugly typesetting you can use IEEEtrantools:

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{IEEEtrantools}

\begin{document}

\begin{IEEEeqnarray}{r;c;l}
x   &=&   x \\
x   &\overset{\text{I'd like}}{=}&  x\\
x   &\overset{\text{to align}}{=}&  x\\
x   &\overset{\text{these equations}}{=}&  x\\
x   &\overset{\text{on equal signs}}{=}&  x
\end{IEEEeqnarray}

\end{document}


Of course, a better setting would be with the explanations on the side; I know why you ask, but I can't refrain from suggesting a better setting. ;-)

\documentclass[11pt]{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
x &= x \\
x &= x && \text{because foo} \\
x &= x && \text{because bar} \\
x &= x && \text{easy} \\
x &= x && \text{obvious}
\end{align}

\end{document}


• Ok, but what should be, typographically speaking, the best layout in these cases? I mean, is your solution oriented by your own taste or do you refer to some book...? I'd like to have a typography manual/book as a reference for cases like this one. Aug 23 '17 at 5:42
• @GabrieleNicolardi No book, but logic.. Aug 23 '17 at 7:56

Like this? Note it has to be compiled twice. Also, if you have to align several independent such groups, you have to use the optional argument (a simple tag, set to R by default) to differentiate them:

\documentclass[11pt]{article}
\usepackage{mathtools, eqparbox}

\newcommand{\mystackrel}[3][T]{\stackrel{\eqmakebox[#1]{\scriptsize#2}}{#3}}

\begin{document}

\begin{align}
x \mystackrel{I'd like}{=} x\\
x \mystackrel{to align}{=} x\\
x \mystackrel{these equations}{=} x\\
x \mystackrel{on equal signs}{=} x
\end{align}

\end{document}


Or like this:

\documentclass[11pt]{article}
\usepackage{mathtools, eqparbox}

\newcommand{\varstackrel}[3][T]{\stackrel{\raisebox{0.5ex}{\clap{\scriptsize#2}}}{#3}}

\begin{document}

\begin{align}
x \varstackrel{I'd like}{=} x\\
x \varstackrel{to align}{=} x\\
x \varstackrel{these equations}{=} x\\
x \varstackrel{on equal signs}{=} x
\end{align}

\end{document}


Or, if you have no other alignment points, you can use a simple gather with \mathllap and \mathrlap:

\documentclass[11pt]{article}
\usepackage{mathtools}

\newcommand{\varstackrel}[3][T]{\stackrel{\raisebox{0.5ex}{\scriptsize#2}}{#3}}

\begin{document}

\begin{gather}
\mathllap{x} \varstackrel{I'd like}{=} \mathrlap{x}\\
\mathllap{x} \varstackrel{to align}{=} \mathrlap{x}\\
\mathllap{x} \varstackrel{these equations}{=} \mathrlap{x}\\
\mathllap{x} \varstackrel{on equal signs}{=} \mathrlap{x}
\end{gather}

\end{document}


• Mmm... I'd prefer no extra space between the terms of the equation and the relation operator. But I will consider your solution. Aug 22 '17 at 17:17
• @GabrieleNicolardi: I've added another code. Aug 22 '17 at 17:28
• About the second example: I obtain the same result by using align, \stackrel and \mathclap... but I want to obtain the layout of the last figure I posted. Aug 22 '17 at 17:56
• @GabrieleNicolardi:In this case, I propose another solution based on gather Aug 22 '17 at 19:03
• The layout is correct... but the solution is quite overkill! ;-) Aug 22 '17 at 20:19