5

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

enter image description here

Chuang
  • 579

3 Answers3

7

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 infinitely 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}
David Carlisle
  • 757,742
yannisl
  • 117,160
3

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}
David Carlisle
  • 757,742
ipavlic
  • 8,091
  • 2
    @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 04 '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 04 '11 at 12:25
  • 2
    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 04 '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 http://en.wikibooks.org/wiki/LaTeX/Tables page, and it is quite useful. – ipavlic Mar 04 '11 at 12:48
  • 1
    @Seamus @ipavlic you can find more info in the array package docs. – yannisl Mar 04 '11 at 12:50
  • @ipavlic: Simple change tabular to array then the >{$} <{$} are already implied. You need to put array in math-mode, e.g. put $ around it. – Martin Scharrer Mar 04 '11 at 13:57
3

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:

aligned equations

Moriambar
  • 11,466
Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751