# Alignment in aligned and aligned[t] environments

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*}

• Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Jul 16, 2014 at 19:28
• Sure. I have only started using this site days ago. Jul 16, 2014 at 21:21
• Well, welcome to TeX.SX, too, then! :) Jul 16, 2014 at 21:22
• Thanks. It is nice that so many people are willing to take the time to code for others. I have been using LaTeX for years and I have never heard of many of the commands or packages that are used in the code that is suggested. Jul 16, 2014 at 23:54

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

\documentclass{scrartcl}
\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}


Or maybe better

\documentclass{scrartcl}
\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}


\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.

• Nice code. I had not heard of "\mathrlap" command. What does it instruct LaTeX to do? I see that you used it once. You have "{}+2" in your code. I guess that you are using this to get the proper space on both sides of the plus sign. Do you know how much space is on each side of the plus sign? I am guessing that two thin spaces "\,\," are on each side. Jul 16, 2014 at 21:17

I think it would look better with multlined: the formula is easier to read, has a simpler syntax, and the 3+ signs are aligned!:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[showframe, nomarginpar]{geometry}
\usepackage{amssymb,mathtools}
\usepackage{amsfonts}

\begin{document}

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

\end{document}


Concerning the different spacing on the right of the = signs, it's the first one that is a little too large. If you a negative thin space (\!), it's OK.

• This is not how I wanted it displayed. I also do not like using "\!\!" unless I know that there are two thin spaces on each side of the plus sign. How many em's are there in a \quad? (I asked you this in another post.) Thanks. Jul 16, 2014 at 21:25
• I know it's not exactly what you want. It was only as suggestion of what I would do for myself. I think that the formula is more readable due to the smaller vertical space between the lines ot the multined environments (it makes groups). For the number of em in a \quad (1) I answered in a comment to the post you mention. The \!\!is here to compensate a small space introduced by the multlined environment. The previous solutions by @esdd also have a small space that ought to be compensated, especially the second one (~ 1.7pt). But you may think it's not very important and not compensate… Jul 16, 2014 at 21:43
• But you need only \! to compensate, if I'm not mistaken. Jul 17, 2014 at 0:21
• I just looked at a web site, the \quad command inserts a horizontal space equal to the width of the letter "M." (The width of "M" is also called an em.) The \! command removes 3/18 of a quad, and the \, command adds 3/18 of a quad. \: adds 4/18 of a quad, and _, the "\" followed by a space, adds 6/18 of a quad. Bernard, I magnified the equation that @esdd's code instructed LaTeX to display. All the alignments seem to be precise. I am trying to learn enough about the coding to make the displayed equations precise. Why does introducing the multilined environment introduce any space?! Jul 17, 2014 at 0:47
• I don't know enough of the internals of the amsmath environments to say why. Anyway te difference in spacing is very small (~ 0.5mm). Jul 17, 2014 at 4:41