[Tex/LaTex] Alignment in an alignat environment

alignat

The following code has three alignments in an alignat environment. I am displaying equivalent expressions for $\vert xy + xz + yz \vert$ and for $\vert x + y + z \vert$. I want all the "=" to be aligned. They are. I want the first "+" in lines 1, 2, and 5 after $\vert xy + xz + yz \vert$ to be aligned with each other and with the first "+" in lines 2, 3, 4, 5 and 7 after $\vert x + y + z \vert$. Most of them are aligned. The first "+" in lines 2 and 3 after $\vert x + y + z \vert$ are slightly off due to the unseemly short space between the "+" and the "[". Why is the spacing not appropriate in these two lines? Why are all these "+" artificially shifted rightward? Do I have to insert an empty space "{}" to the left of the "+" in these lines to get the appropriate space? In another post, someone suggested using the "\mathrlap" command to get proper alignment. It gave me the proper alignment in that post. I don't know what it does, and so I don't know how to adapt it for this new code. I thought that it meant to make a box of no width and to put the text to the right of it. Is that right? (The third alignment is correct.)

\documentclass[10pt]{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{newlfont}
\usepackage{mathtools}
\usepackage{tikz}

\begin{document}

\begin{alignat*}{3}
\MoveEqLeft[2] \vert xy + xz + yz \vert^{2} \\
&= 3 &&+ \bigl[&&\cos(2a + b + c) + \cos(a + 2b + c) + \cos(a + b + 2c) \\
&&&&&+ \cos(a - b) + \cos(a - c) + \cos(b - c)\bigr] \\
&&&+ \bigl[&&\cos(a - b) + \cos(a - c) + \cos(b - c) \\
&&&&&- \bigl(\cos(2a + b + c) + \cos(a + 2b + c) + \cos(a + b + 2c)\bigr) \bigr] \\
&= 3&&\mathrlap{{}+ 2\bigl(\cos(a - b) + \cos(a - c) + \cos(b - c)\bigr).}
\intertext{Likewise,}
\MoveEqLeft[2] \vert x + y + z \vert^{2} \\
&= \mathrlap{[\cos{a} + \cos{b} + \cos{c}]^{2} + [\sin{a} + \sin{b} + \sin{c}]^{2}} \\
&= 3 &&\mathrlap{+ 2\cos{a}\cos{b} + 2\cos{a}\cos{c} + 2\cos{b}\cos{c}} \\
&&&\mathrlap{+ 2\sin{a}\sin{b} + 2\sin{a}\sin{c} + 2\sin{b}\sin{c}} \\
&= 3&& + \bigl[&&\cos(a + b) + \cos(a + c) + \cos(b + c) \\
&&&&&+ \cos(a - b) + \cos(a - c) + \cos(b - c)\bigr] \\
&&&+ \bigl[&&\cos(a - b) + \cos(a - c) + \cos(b - c) \\
&&&&&- \bigl(\cos(a + b) + \cos(a + c) + \cos(b + c)\bigr) \bigr] \\
&= 3&&\mathrlap{{}+ 2\bigl(\cos(a - b) + \cos(a - c) + \cos(b - c)\bigr).}
\end{alignat*}
\end{document}

Best Answer

I guess you are looking for the following layout

enter image description here

Change

\MoveEqLeft[2] \abs{xy + xz + yz}^{2} \\

to

\mathmakebox[2em][l]{\abs{xy + xz + yz}^{2}}

Then \abs{xy + xz + yz}^{2} is set in a 2em width mathbox.

Code:

\documentclass[10pt]{amsart}
\usepackage{mathtools}% loads amsmath
\DeclarePairedDelimiter\abs{\lvert}{\rvert}

\begin{document}
\begin{alignat*}{3}
\mathmakebox[2em][l]{\abs{xy + xz + yz}^{2}}\\
&= 3 &&+ \bigl[&&\cos(2a + b + c) + \cos(a + 2b + c) + \cos(a + b + 2c) \\
&&&&&+ \cos(a - b) + \cos(a - c) + \cos(b - c)\bigr] \\
&&&+ \bigl[&&\cos(a - b) + \cos(a - c) + \cos(b - c) \\
&&&&&- \bigl(\cos(2a + b + c) + \cos(a + 2b + c) + \cos(a + b + 2c)\bigr) \bigr] \\
&= 3&&\mathrlap{{}+ 2\bigl(\cos(a - b) + \cos(a - c) + \cos(b - c)\bigr).}
\intertext{Likewise,}
\mathmakebox[2em][l]{\abs{x + y + z}^{2}} \\
&= \mathrlap{[\cos a + \cos b + \cos c]^{2} + [\sin a + \sin b + \sin c]^{2}} \\
&= 3 &&\mathrlap{{}+ 2\cos a\cos b + 2\cos a\cos c + 2\cos b\cos c} \\
&&&\mathrlap{{}+ 2\sin a\sin b + 2\sin a\sin c + 2\sin b\sin c} \\
&= 3&& + \bigl[&&\cos(a + b) + \cos(a + c) + \cos(b + c) \\
&&&&&+ \cos(a - b) + \cos(a - c) + \cos(b - c)\bigr] \\
&&&+ \bigl[&&\cos(a - b) + \cos(a - c) + \cos(b - c) \\
&&&&&- \bigl(\cos(a + b) + \cos(a + c) + \cos(b + c)\bigr) \bigr] \\
&= 3&&\mathrlap{{}+ 2\bigl(\cos(a - b) + \cos(a - c) + \cos(b - c)\bigr).}
\end{alignat*}
\end{document}

All align... environments work like tables with a couple of rl column pairs. The & have a similar meaning as in tables.

Here is a picture to show how \mathmakebox[2em][l]{...} and \mathrlap{...} work.

enter image description here

There are three rl column pairs. Because the second and the third r column is completely empty their column width in the alignat* environment is 0pt.

The green boxes show the real width of the arguments of \(math)makebox[2em][l]{...} in the first line and \(math)rlap{...} in the last line. But the red boxes show the used width for this boxes: in the first line 2em and in the last line 0pt (and of course the width of the red lines) . The additional width of the contents is simple ignored and does not influence the width of the columns.

In the following picture you can see what happens if \(math)makebox[2em][l]{...} and \(math)rlap{...} are removed

enter image description here

Code for the first picture:

\documentclass{article}
\usepackage{mathtools}% loads amsmath
\DeclarePairedDelimiter\abs{\lvert}{\rvert}

\usepackage{array}
\usepackage[table]{xcolor}
\arrayrulecolor{gray}
\renewcommand\arraystretch{1.5}
\setlength\fboxsep{0pt}
\setlength\fboxrule{.5mm}

\begin{document}
\begin{tabular}{|*{3}{@{}r@{}|@{}l@{}|}}
r&l&&l&&l\\[2\baselineskip]
\hline
\fcolorbox{red!}{white}{\makebox[2em][l]{\fcolorbox{green}{white}{$\abs{xy + xz + yz}^{2}$}}}&&&&&\\
&$= 3 $&&$+ \bigl[$&&$\cos(2a + b + c) + \cos(a + 2b + c) + \cos(a + b + 2c)$ \\
&$= 3$ &&$+ \bigl[$&&$\cos(2a + b + c) + \cos(a + 2b + c) + \cos(a + b + 2c)$ \\
&&&&&$+ \cos(a - b) + \cos(a - c) + \cos(b - c)\bigr]$ \\
&&&$+ \bigl[$&&$\cos(a - b) + \cos(a - c) + \cos(b - c)$ \\
&&&&&$- \bigl(\cos(2a + b + c) + \cos(a + 2b + c) + \cos(a + b + 2c)\bigr) \bigr]$ \\
&$= 3$&&\fcolorbox{red}{white}{\rlap{\fcolorbox{green}{white}{${}+ 2\bigl(\cos(a - b) + \cos(a - c) + \cos(b - c)\bigr).$}}}&&
\end{tabular}
\end{document}