# What's the right space to right the alignment of a right aligned align environment?

As Hilbert would have put it, had he lived in the TeX-era:

No one shall expel us from the Paradise that AMS has created.

and one aspect of that paradise is release from the tyranny of eqnarray by use of the magnificent align environment (and its siblings).

One feature of the align environment is that it saves us considerable time and effort by only requiring one alignment character in, for example,

\begin{align*}
x &= y + z \\
a &= b + c
\end{align*}


and all the spaces are just right.

But just occasionally I want to align things by the other side of the relation sign. And then the automatic spacing doesn't work:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

$x = y + z$

\begin{align*}
x &= y + z
\end{align*}

\begin{align*}
x =& y + z
\end{align*}

\end{document}


Clearly, I can just add the right amount of space. But equally clearly there's a right amount of space to be added. What is it?

-
Isn't it just x ={}& y + z? Or am I missing something? Great title BTW. – Peter Grill Nov 7 '12 at 10:11
@PeterGrill That is it! Evidently me that was missing something. – Loop Space Nov 7 '12 at 10:22
(I'm still curious as to the right amount of spacing, though.) – Loop Space Nov 7 '12 at 10:22
The missing space is \mskip\thickmuskip, the space TeX inserts between a Rel and an Ord atom. Writing ={} automatically supplies it, because any subformula (even an empty one) is considered as an Ord atom. – egreg Nov 7 '12 at 10:34
@egreg That looks awfully like an answer. If you feel it is a bit short, you could add (a link/reference to - is it in TeXbyTopic?) a list of the spaces that TeX inserts between the various elements. – Loop Space Nov 7 '12 at 10:37

In order to understand why a space is missing, one needs to know a bit how TeX deals with formulas and what spacings it inserts between objects.

First of all, TeX dismantles a formula into a sequence of math atoms, which can be of thirteen types

Ord Op Bin Rel Open Close Punct Inner

The atoms in the second line are actually considered as Ord atoms as far as spacing is concerned. For instance, $a+b=c$ becomes the sequence

Ord Bin Ord Rel Ord

Then TeX inserts spaces according to the following table, where rows and columns are indexed with numbers: 0 = Ord, 1 = Op, 2 = Bin, 3 = Rel, 4 = Open, 5 = Close, 6 = Punct, I = Inner

(The table is from my paper "Simboli matematici in TeX e LaTeX", ArsTeXnica 8 (2009), pp. 7–24; it's an unabridged version of a similar table in the TeXbook). You find the left atom in the row and the right atom in the columns; then the number you find is interpreted as

• 0 = no space
• 1 = thin space (\thinmuskip)
• 2 = medium space (\medmuskip)
• 3 = thick space (\thickmuskip)
• * = impossible combination

If the number is in parentheses, then the space is inserted only if the formula (or subformula) is eventually typeset in display or text style, but not in subscript/superscript styles.

Another example: the formula $(a+b)\cdot c=ac+bc$ becomes

Open Ord Bin Ord Close Bin Ord Rel Ord Ord Bin Ord Ord

A subformula is anything in braces; it is eventually treated as an Ord atom.

How does align guess the right spacing? When you type

\begin{align*}
a &= b+c
\end{align*}


LaTeX transforms this into an alignment basically with the following template:

\hfil $\displaystyle #$ & $\displaystyle {}#$ \hfil


where # represents the actual contents of the cell. So we have a first column with right alignment and the second column left aligned, but an empty subformula is always added before the actual contents. So the formula that's typeset in the right column is $\displaystyle {}=b+c$ that get read as

Ord Rel Ord Bin Ord

and the spacing is just right.

The same would happen with

\begin{align*}
a ={}& b+c \\
a=\mskip\thickmuskip & b+c
\end{align*}


Note that explicit spacing commands (or rules) do not appear in the list of atoms and are inserted along with the automatically provided spaces.

Abbreviations for \mskip\thinmuskip, \mskip\medmuskip and \mskip\thickmuskip are \, \: \; respectively; \! is an abbreviation fo \mskip-\thinmuskip (which can come handy for removing a thin space automatically added).

One can force a single symbol or subformula to be considered as one of the above atom types by feeding it as argument to

\mathord \mathop \mathbin \mathrel \mathopen \mathclose \mathpunct \mathinner


By adding an empty subformula, you let TeX do the job which it's paid for and don't need to remember that table. Well, that table can come handy in some tough situation when we end up scratching our head, asking where that damn space is coming from or doesn't show up.

-
Brilliant (as always)! Thanks to this I will now remember that sticking a {} in is the right thing to do because now I understand what's going on - without that remembering {} is about as easy as remembering that six times nine is forty-two. – Loop Space Nov 7 '12 at 12:02
Very nice! Morten Høgholm is discussing something similar in his bachelor thesis: sites.google.com/site/mortenhoegholm/breqn-thesis.pdf (See for example Table 1 on page 15.) – Svend Tveskæg Nov 7 '12 at 12:51
@SvendMortensen Thanks for the pointer! – egreg Nov 7 '12 at 12:57
How does TeX "know" to treat the - in $-1$ as a unary rather than a binary operator -- and thus not insert \medmuskip between - and 1? Is the atom type of - maybe converted "on the fly" from mathbin to mathord? – Mico May 29 at 16:40
@Mico That's the rule implemented by Knuth: the cases marked * in the table are declared “impossible” and the atom is turned into an ordinary one. Perhaps also the “start or end” of formula should also be treated, but, basically, an object in the Bin type with no left atom is turned into Ord: to be spaced as Bin, an object must have something compatible on either side. For instance, ”Rel Bin Ord“ is not possible and the Bin is turned into Ord. The same is for “nothing Bin Ord”; the same for “end of formula” (or subformula, of course): “Ord Bin nothing” is impossible. – egreg May 29 at 16:52