# Aligning different parts of an equation with certain spots in the line above

I am trying to get certain parts in the second line of an equation to line up with certain parts in the first line. For example,

X=AAA+BBB+CCC

= a + b + c


where here I am wanting the lower-case letter to line up in the middle of its three upper-case counterparts and all of the operators to align. I have tried using align in the following way:

\begin{align}

X&=A&AA &+ B&BB &+ C&CC \\

&= &a &+ &b &+ &c

\end{align}


This results in horrible spacing. I have also tried using tabular, but the spacing is bad there also. I would like the first line to be written as it would be normally and then the second line to line up as desired.

-

You can use the alignat environment from the amsmath package.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{alignat}{6}
X&={}& A&AA  &{}+{}& B&B&B &{}+{}& C&C&C \\
&={}&  &a   &{}+{}&  &b&  &{}+{}&  &c&
\end{alignat}

-
Beautiful, thank you very much! – clh Feb 23 '12 at 22:45

Note that the align environment uses a rcl alignment for each of the components. That's why your output seems to be bunched together in "mini-equations".

Here are a couple of options, using amsmath or not:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\makeatletter
\newcommand{\cmathbox}[2][x]{%
\settowidth{\@tempdima}{$#1$}%
\makebox[\@tempdima][c]{$#2$}%
}
\makeatother
\begin{document}
Original using \verb|align*|:
\begin{align*}
X&=A&AA &+ B&BB &+ C&CC \\
&= &a &+ &b &+ &c
\end{align*}

Using \verb|alignat*|:
\begin{alignat*}{3}
X&=AAA &&+ BBB &&+ CCC \\
&=\cmathbox[AAA]{a} &&+ \cmathbox[BBB]{b} &&+ \cmathbox[CCC]{c}
\end{alignat*}

Using \verb|array|:
$\begin{array}{r@{{}\mathrel{=}{}}c*{2}{@{{}\mathbin{+}{}}c}} X & AAA & BBB & CCC \\[\jot] & a & b & c \end{array}$
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


My alignat solution differs slightly from @IanThompson's. His would be more general if you don't want to horizontally centre the expression components. The array solution mimics the behaviour via correct spacing around the operators (relational and binary), as well as vertically between expressions (using \jot which is 3pt).

-
Very nice to know, thank you! – clh Feb 23 '12 at 22:46

I often resort to using \makebox[<size>]{<new text>} which uses a box as wide as <size} to place the <new text>.

To compute the <size> I use \withdof{<old text>} form the calc package. By default this will center the <new text>, but you can also specify the alignment with [l] for left, [r] for right, or [c] for center. In the second example below I left aligned the a and b, and right aligned the c for illustrative purposes (not suggesting that that looks good).

\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}
\begin{document}
\begin{align*}
X &= AAA + BBB + CCC \\
&= \makebox[\widthof{$AAA$}]{$a$}
+ \makebox[\widthof{$BBB$}]{$b$}
+ \makebox[\widthof{$CCC$}]{$c$}
\end{align*}

\begin{align*}
X &= AAA + BBB + CCC \\
&= \makebox[\widthof{$AAA$}][l]{$a$}
+ \makebox[\widthof{$BBB$}][l]{$b$}
+ \makebox[\widthof{$CCC$}][r]{$c$}
\end{align*}
\end{document}

-
Cool, thanks a lot! – clh Feb 23 '12 at 22:47