# Aligning stackrel signs beneath each other using split

I'm typesetting a proof by induction using split to align equality signs precisely beneath each other. One of the signs should have 'IH' on top of it to indicate that the induction hypothesis has been used in that step. I'm using stackrel for that. However, since \stackrel{IH}{=} is wider than =, it doesn't align nicely. I tried two things with split (the & before or after the =sign) and also with alignedat (but that would only work if the center column would be center-aligned, which it's not, it's left-aligned. See the code example and its output:

\documentclass{minimal}
\usepackage{amsmath}

\begin{document}
\begin{equation*}
\begin{split}
S_n &=                 2^iS_{n-i}+2^i-1\\
&\stackrel{IH}{=}  2^i(2^1S_{n-i-1}+2^1-1)+2^i-1\\
&=                 2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{split}
\end{equation*}

\begin{equation*}
\begin{split}
S_n =&                2^iS_{n-i}+2^i-1\\
\stackrel{IH}{=}&     2^i(2^1S_{n-i-1}+2^1-1)+2^i-1\\
=&                    2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{split}
\end{equation*}

\begin{equation*}
\begin{alignedat}{2}
S_n &=&&              2^iS_{n-i}+2^i-1\\
&\stackrel{IH}{=}&&   2^i(2^1S_{n-i-1}+2^1-1)+2^i-1\\
&=&&                  2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{alignedat}
\end{equation*}
\end{document}


As you can see, the equality signs and the equality signs with 'IH' above it are nowhere precisely beneath each other.

I don't need to use split, so solutions without split would be perfect as well. How can I make this work?

You can use \mathmakebox or \mathclap (thanks to Andrew) from mathtools

\documentclass{article}
\usepackage{mathtools}

\begin{document}
\begin{equation*}
\begin{split}
S_n &=                 2^iS_{n-i}+2^i-1\\
&\stackrel{\mathmakebox[\widthof{=}]{\mathrm{IH}}}{=}  2^i(2^1S_{n-i-1}+2^1-1)+2^i-1\\
&=                 2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{split}
\end{equation*}
\end{document}


You can use align* also instead of equation* and split

\documentclass{article}
\usepackage{mathtools}

\begin{document}
\begin{align*}
S_n &=                 2^iS_{n-i}+2^i-1\\
&\stackrel{\mathclap{\mathrm{IH}}}{=}  2^i(2^1S_{n-i-1}+2^1-1)+2^i-1\\
&=                 2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{align*}
\end{document}


Please note that I have changed IH to \mathrm{IH}. (Thanks to egreg).

• Thanks for the quick answer and the tip. I was already getting tired of writing \begin{equation*}\begin{split}...\end{split}\end{equation*} all the time. – Keelan Dec 11 '14 at 11:21
• If loading mathtools you could just use \mathclap on the symbol "IH" to hide is width. – Andrew Swann Dec 11 '14 at 11:23
• @AndrewSwann Nice suggestion, I have added it to the answer.thank you. – user11232 Dec 11 '14 at 11:26
• IH with math letters is really bad. Use either \mathrm{IH} or \mathit{IH} – egreg Dec 11 '14 at 11:30
• @egreg I have no idea to be honest. What may be IH? – user11232 Dec 11 '14 at 11:31

In fact, the solutions above are not optimal because the example given is a very special case: IH has almost the same width as =, so there is no problem on the right side. But consider what happens when we need something significantly larger than IH, like in

\begin{align*}
S_n &= 2^iS_{n-1}+2^i-1\\
&\stackrel{\mathrm{IH,IG,IK,IL}}{=}2^i(2^1S_{n-i-1}+2^1-1)+2^i-1\\
&= 2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{align*}


Then the right part of IH,IG,IK,IL will overlap on the formula!

My solution is to define \leftstackrel, which aligns perfectly on the left but adds the necessary white space on the right so that there is no overlap. Here is the code:

\newlength{\leftstackrelawd}
\newlength{\leftstackrelbwd}
\def\leftstackrel#1#2{\settowidth{\leftstackrelawd}%
{${{}^{#1}}$}\settowidth{\leftstackrelbwd}{$#2$}%
\leavevmode\ifthenelse{\lengthtest{\leftstackrelawd>0pt}}%
{\kern-.5\leftstackrelawd}{}\mathrel{\mathop{#2}\limits^{#1}}}


and it can be used simply as:

\begin{align*}
S_n &= 2^iS_{n-1}+2^i-1\\
&\leftstackrel{\mathrm{IH,IG,IK,IL}}{=}2^i(2^1S_{n-i-1}+2^1-1)+2^i-1\\
&= 2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{align*}


Compare (1.1) (legacy \stackrel), (1.2) (\stackrel proposed above) and (1.3) (\leftstackrel) in the figure below

(source: fluxus-virus.com)

I suggest three strategies. The first one is to define a command \iheq that prints an equals sign with some padding to become the same width as \overset{\mathrm{IH}}{=}, which can be simply obtained with \iheq*.

The second strategy is adding “(IH)” to the side.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\iheq}{s}{%
\overset{\IfBooleanTF{#1}{\mathrm{IH}}{\hphantom{\mathrm{IH}}}}{=}%
}

\begin{document}

\begin{equation*}
\begin{split}
S_n &\iheq  2^iS_{n-i}+2^i-1\\
&\iheq* 2^i(2^1S_{n-i-1}+2^1-1)+2^i-1\\
&\iheq  2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{split}
\end{equation*}

\begin{equation*}
\begin{aligned}
S_n &= 2^iS_{n-i}+2^i-1\\
&= 2^i(2^1S_{n-i-1}+2^1-1)+2^i-1 && \makebox[0pt][l]{(IH)}\\
&= 2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{aligned}
\end{equation*}

\end{document}


Third strategy: make IH smaller and ensure it's zero width.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\iheq}{s}{%
\IfBooleanTF{#1}{\overset{\IH}{=}}{=}%
}
\NewDocumentCommand{\IH}{}{%
\hidewidth\scriptscriptstyle\mathrm{IH}\hidewidth
}

\begin{document}

\begin{equation*}
\begin{split}
S_n &\iheq  2^iS_{n-i}+2^i-1\\
&\iheq* 2^i(2^1S_{n-i-1}+2^1-1)+2^i-1\\
&\iheq  2^{i+1}S_{n-i-1}+2^{i+1}-1
\end{split}
\end{equation*}

\end{document}