# How to vertically align subscripts in mathmode [duplicate]

I would like the "AB" subscripts in my formula to be vertically aligned with each other--ideally, by adjusting the second subscript to match the current placement of the first.

MWE:

\documentclass{article}
\usepackage{braket}
\begin{document}
$$\rho_{AB}=\ket{0}_{AB}\otimes_{AB}\bra{0}$$
\end{document}


I have attempted to use \vphantom to remedy this in the following way:

\documentclass{article}
\usepackage{braket}
\begin{document}
$$\rho_{AB}=\ket{0}_{AB}\otimes\vphantom{\ket{0}}_{AB}\bra{0}$$
\end{document}


However, this did not produce a discernible difference, so I'm stumped at the moment.

## Proposal for another MWE (Manuel Kuehner)

Even with a full \phantom the vertical position changes...

\documentclass{article}
\usepackage{braket}
\begin{document}
$$\rho_{AB} = \ket{0}_{AB} \otimes_{AB} \bra{0}$$
$$\rho_{AB} = \ket{0}_{AB} \otimes\ket{0}_{AB} \bra{0}$$
$$\rho_{AB} = \ket{0}_{AB} \otimes\phantom{\ket{0}}_{AB} \bra{0}$$
\end{document}


The reason why \vphantom does not work is subtle, and has to do with some TeXnical details concerning the way it is implemented.

The definition of \vphantom is

\def\vphantom{\v@true\h@false\ph@nt}


that invokes the helper macro \ph@nt after having set two switches to record the fact that the vertical dimension of the phantom is to be retained, and the horizontal one discarded. By comparison, the definition of the other two flavors of phantom are

\def\hphantom{\v@false\h@true\ph@nt}
\def\phantom{\v@true\h@true\ph@nt}


Next, here is the definition of \ph@nt:

\def\ph@nt{%
\ifmmode
\expandafter\mathpalette\expandafter\mathph@nt
\else
\expandafter\makeph@nt
\fi}


If we are in math mode (and we’ll suppose so, we are not going to follow the other branch), the two \expandafters cause the conditional to be completed, with the following token remaining to be executed next:

\mathpalette\mathph@nt


Now, the use of \mathpalette is already explained, for example, in The mysteries of \mathpalette. What matters here is that it inserts in the current math list a device called a \mathchoice node, which is used to record the height (and/or width) of the phantom in all the four possible math styles, from \displaystyle to \scriptscriptstyle (this is necessary, because the phantom is implemented as an empty horizontal box, not as a math subformula). Now, a thing that seems to be easily forgotten is that not every item in a math list can carry a sub-/superscript: only “atoms” can, and a \mathchoice node is not an atom. According to the rules stated on p. 291 of The TeXbook, a superscript (or subscript) command that occurs when the current math list does not end with an atom appends a new Ord atom, with all fields empty, to the list, and then adds the superscript (subscript) to that atom. In our case, this means that the _{AB} subscript is applied to an empty atom, and not to the phantom, as it was intended.

The remedy is simple: just wrap up the \vphantom in a pair of curly braces: this will generate a subformula that contains the \mathchoice that contains the phantom, and the subscript will be appended to that subformula.

Addition: We show, in the code below, how the particular problem raised in the question could be addressed. One point, in particular, is worth noting: the construction for \oplus needs to be enclosed in a \mathbin, since it is meant to be used as a binary operator (Bin) atom, not as an Ord atom. Uncomment the commented lines to see diagnostic messages that prove the claims set forth above. I am indebted to Peter Grill for part of the following code, which I borrowed from his (now deleted) answer.

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{braket}

{\vphantom{#1}}%
}

\makeatletter

\newcommand*\ShowLists{%
%   \showboxdepth   \sixt@@n
%   \tracingonline  \@ne
%   \showlists
}

\makeatother

\begin{document}

Some text $\rho\AdjustSubscript_{AB} = \ket{0}_{AB}\mathbin{\otimes\AdjustSubscript_{AB}}\bra{0}\ShowLists$.\ShowLists
%
$$\rho\AdjustSubscript_{AB} = \ket{0}_{AB}\mathbin{\otimes\AdjustSubscript_{AB}}\bra{0}$$
%
Some more text.

\end{document}


And here’s the output:

This question has been asked before, more than once: indeed, I remembered having answered a similar one previously, but I couldn’t recollect exactly when and where. Now I’ve made some searches, and it has turned out that the question seems to have a pretty interesting history.

If you search on this site for “\vphantom \mathchoice”, one of the first results is an answer by @wipet to a question entitled “\vphantom and superscripts”: this answer gives the same explanation, and the same remedy, as this one. But there had already been some previous answers that addressed, and solved, the question in essentially the same way; of these, allow me to draw your attention to this answer of mine to Bug in xelatex + unicode+math + vphantom.

Of course, @egreg had answered similar questions too, always suggesting the same solution; see for example:

However, seeing the comments to Bug in xelatex + unicode+math + vphantom, cited above, it seems that, after all, I can claim some merits in having pointed out the rôle of \mathchoice. Since it certainly doesn’t happen every day to find yourself in a position to be able to suggest humbly something to @egreg, I hope I’ll be pardoned if I brag a little about it… :-)

• wow, phenomenal answer! – user143410 May 26 '18 at 0:28

A simple \smash does the trick:

\documentclass{article}
\usepackage{braket}
\begin{document}

$$\rho_{AB}=\smash{\ket{0}}_{AB}\otimes_{AB}\bra{0}$$

\end{document}


Strangely, if you define your own \bra and \ket using \DeclarePairedDelimiter from mathtools, there is no alignment problem:

\documentclass{article}
\usepackage{mathtools}
\DeclarePairedDelimiter{\myket}\lvert\rangle
\DeclarePairedDelimiter{\mybra}\langle\rvert

\begin{document}

$$\rho_{AB}=\myket{0}_{AB}\otimes_{AB}\mybra{0}$$

\end{document}


• Related (What is \smash?): tex.stackexchange.com/questions/424200 – Dr. Manuel Kuehner May 25 '18 at 23:41
• Can you explain why the \phantom approach does not work here? – Dr. Manuel Kuehner May 25 '18 at 23:41
• Unfortunately, not really. I don't know the internals of the braket package. – Bernard May 25 '18 at 23:50
• @Dr.ManuelKuehner: See my comment to Peter Grill’s answer. – GuM May 25 '18 at 23:55
• @Dr.ManuelKuehner: A solution with mathtools, redefining \bra and \ket as paired delimiters is fine without having whatever to do. – Bernard May 26 '18 at 0:05