[Tex/LaTex] Alignment in aligned and aligned[t] environments

align

The following code instructs LaTeX to typeset two different expressions for the square of the absolute value of a certain sum. I have three alignments – one of them is "almost" correct and two of them are correct.

The "=" are aligned properly. Though, the spaces to the right of the two equal signs are different. (This should shift the other alignments, but it doesn't.) I think the space to the right of the second equal sign is too little. How do I correct that?

I would like the first "+" in lines 2, 4, and 6 to be aligned. It seems that if LaTeX did not put an unreasonable amount of space after the "3" in line 2, these plus signs would have been aligned. In the aligned[t] environment, I have two alignments. It seems that since I have "3 &&+" to begin coding for line 2, the second "&" artificially put in the extra space.

The third alignment is to align cos(2a + b + c) in line 2, the first "+" in line 3, cos(a – b) in line 4, and the first "+" in line 5. This is correct. (I wanted to say that to help those looking at the code.)

\begin{align*}
\MoveEqLeft[3] \vert xy + xz + yz \vert^{2} \\
&= \begin{aligned}[t] 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] \\
\end{aligned} \\
&= 3 + 2\bigl(\cos(a - b) + \cos(a - c) + \cos(b - c)\bigr) .
\end{align*}

Best Answer

Here is a suggestion using a combination of alignat* and \mathrlap

\documentclass{scrartcl}
\usepackage{mathtools}% loads also amsmath
\begin{document}
\begin{alignat*}{3}
  \MoveEqLeft[3] \mathrlap{\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).} 
\end{alignat*}
\end{document}

enter image description here

Or maybe better

\documentclass{scrartcl}
\usepackage{mathtools}% loads also amsmath
\begin{document}
\begin{alignat*}{3}
  \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).} 
\end{alignat*}
\end{document}

enter image description here

\mathrlap is provided by the mathtools package. It 'hides' the width of its argument and so the last equation line does not influence the 4th and 5th &.

The inserted space around a + depends on its current meaning. The {}+ in the last line shows latex that the + represents an addition and not only a positive number.