# How can I align numbers in equations?

What is the best way to produce the following: (Image shows a typo)

-
Wait a minute... 1 + 20 \neq 13 ... –  Seamus Mar 4 '11 at 12:27
Incidentally, last time I checked, 1 + 20 = 21. But then, they may have reset reality since I last did that sum. –  Loop Space Mar 4 '11 at 12:33
@Seamus @Andrew Sorry about the typo edited. –  Chuang Mar 4 '11 at 12:39

Although using a tabular environment can solve some of the alignment issues easily, I would rather opt for using the align environment from the amsmath package, as it would require less typing in the end.

Some manual manipulation of the last three lines would be necessary, in order to get the single digit numbers 9,8 and 7 to line up properly with the double digit numbers on the top three lines. To achieve this you need to use a phantom to push them to the right.

A \phantom is a macro that does not display its contents but produces an inﬁnitely thin horizontal line just as wide as the original material. This can be used to push these numbers to the right in order to get them aligned with the double digit ones on top.

We define this as a macro called \Z:

\def\Z{\hphantom{1}}


and the full code:

\documentclass[12pt]{article}
\usepackage{amsmath,array}
\def\Z{\hphantom{1}}
\begin{document}
\begin{align*}
1 +  12 &= 13\\
2 +  11 &= 13\\
3 +  10 &= 13\\
4 + \Z9 &= 13\\
5 + \Z8 &= 13\\
6 + \Z7 &= 13
\end{align*}
\end{document}

-

Some options are amsmath's align(or align*, without numbering):

\usepackage{amsmath}
...
\begin{align*}
1 &+& 20 &= 13
2 &+& 11 &= 13
...
\end{align*}


(I've edited the example, thanks Martin Tapankov).

or a simple tabular environment. @ expressions automatically insert what's given between the appropriate columns.

\begin{tabular}{r@{+}r@{=}r}
1 & 20 & 13 \\
2 & 11 & 13 \\
...
\end{tabular}


You might also want to include extra spacing in @ arguments (like \;, \: or \,).

Following Martin Scharrer's advice, you could also use array package for ensuring mathmode. >{$}r<{$} means: Prepend a  sign before a right aligned column, and then append a  sign after that. @{+} means that a + gets inserted between columns defined left and right of the @{+}.

\usepackage{array}
\begin{tabular}{>{$}r<{$}@{+}>{$}r<{$}@{=}>{$}r<{$}}
1 & 20 & 13 \\
2 & 11 & 13 \\
...
\end{tabular}

-
@ipavlic: With arrangement like that in the align* environment, the second column will be left-justified instead of right-justified. You'll need to add extra alignment sign after the plus sign. –  Martin Tapankov Mar 4 '11 at 12:25
Darn, you beat me to the tabular answer. But you did it better than I would have! Could you explain a little more about the @ mechanism? That sounds really useful! –  Seamus Mar 4 '11 at 12:25
ipavlic, @Seamus: For math the array environment should be used instead of tabular. Both are using the some code internally, but array uses math-mode. In this example with only positive number it doesn't make a difference but as soon as negative sign appear it does. –  Martin Scharrer Mar 4 '11 at 12:28
@Seamus - I don't know the inner workings. But it receives the argument which it inputs between columns instead of the default space. I first found it on the en.wikibooks.org/wiki/LaTeX/Tables page, and it is quite useful. –  ipavlic Mar 4 '11 at 12:48
@Seamus @ipavlic you can find more info in the array package docs. –  Yiannis Lazarides Mar 4 '11 at 12:50

Okay, let me start by saying that I do not recommend this solution! But I've just been learning a bit about catcodes and felt like trying them out. So here's a solution that allows you to type the equations as they are and typesets them as you want. The only extra typing is the \\ at the end of the lines (I tried to make the newlines into \\s but that got nasty ...)

Code (the extra equation is to show that the alignments are correct):

\documentclass{minimal}

\newenvironment{alignedeqns}{\catcode\+4\catcode\=4\array{r@{{}+{}}r@{{}={}}l}}{\endarray}

\begin{document}

$1 + 100 = 101$

\begin{alignedeqns} 1 + 100 = 101 \\ 1 + 20 = 21 \\ 2 + 11 = 13 \\ 3 + 10 = 13 \\ 4 + 9 = 13 \\ 5 + 8 = 13 \\ 6 + 7 = 13 \end{alignedeqns}

\end{document}


Result:

-
but what if the OP wants the last "column" right-aligned too? –  barbara beeton Mar 4 '11 at 13:29
@barbara: (as I'm sure you know!) change the l which occurs at the end of the argument to the \array command to an r. (I agree that it should be right-aligned. I'm not sure why I chose left; maybe I felt that the equals sign would be lonely without all the 13s next to it). –  Loop Space Mar 4 '11 at 13:39
@Andrew: Why don't you recommend your solution? The catcode changes are temporary, aren't they? –  Hendrik Vogt Mar 5 '11 at 10:03
@Hendrik: Maybe too tricky for this question. –  Leo Liu Mar 5 '11 at 13:29
@Hendrik: because it changes the meaning of '+' and '=' in a way that an inexperienced writer wouldn't be expecting, so it would be hard to figure out what had gone wrong if you put in something a little different. So it violates the "No surprises" rule. –  Loop Space Mar 5 '11 at 17:59