6

In using the align* environment, how can I get specific numbers to align with each other?

In the following example, I want each term of the top sum and the bottom sum to be aligned with each other:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
2S &= 1 &+ 2 &+ 3 &+ \ldots &+ (N-2) &+ (N-1) &+ N \\
&= N &+ (N-1) &+ (N-2) &+ \ldots &+ 3 &+ 2 &+ 1
\end{align*}
\end{document}

Currently, this looks ugly: alignpic

jamaicanworm
  • 29,114

2 Answers2

5

A simple array with some tweaks:

\documentclass{article}
\usepackage{amsmath,array}
\begin{document}
\[
\begin{array}{@{}r<{{}}@{}c*{6}{@{}>{{}}c<{{}}@{}c}@{}}
2S = & 1 & + &   2   & + &   3   & + & \cdots & + & (N-2) & + & (N-1) & + & N \\
{} + & N & + & (N-1) & + & (N-2) & + & \cdots & + &   3   & + &   2   & + & 1
\end{array}
\]
\end{document}

enter image description here

If you want to align the numbers to the minus signs, add new columns.

\documentclass{article}
\usepackage{amsmath,array}
\begin{document}
\[
\begin{array}{@{}r<{{}}*{21}{@{}>{{}}c<{{}}}@{}}
2S = & 1 &+&    & 2 &    &+&    & 3 &    &+& \cdots &+& (N & - & 2) &+& (N & - & 1) &+& N \\
{} + & N &+& (N & - & 1) &+& (N & - & 2) &+& \cdots &+&    & 3 &    &+&    & 2 &    &+& 1
\end{array}
\]
\end{document}

enter image description here

I also fixed the math.

egreg
  • 1,121,712
4

Using the under-development tabstackengine, introduced at Writing a table with equally spaced columns, based on the widest column (sty file found at Measuring align), the process is straightforward:

\documentclass{article}
\usepackage{tabstackengine}
\stackMath
\renewcommand\stackalignment{l}
\begin{document}
\tabbedLongstack{%
  2S &= 1 &+ 2 &+ 3 &+ \ldots &+ (N-2) &+ (N-1) &+ N \\
  &= N &+ (N-1) &+ (N-2) &+ \ldots &+ 3 &+ 2 &+ 1}
\end{document}

enter image description here


OR, as the OP requests, a slightly different look:

\documentclass{article}
\usepackage{tabstackengine}
\stackMath
\begin{document}
\tabbedLongstack{%
  2S &=& 1 &+& 2 &+& 3 &+& \ldots &+& (N-2) &+& (N-1) &+& N \\
  &=& N &+& (N-1) &+& (N-2) &+& \ldots &+& 3 &+& 2 &+& 1}
\end{document}

enter image description here