2

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}
  • What's newlfont for? It's been obsolete for 22 years. – egreg Jul 17 '14 at 21:53
  • 1
    And in what does this differ from the other two similar questions you made? – egreg Jul 17 '14 at 21:55
  • I will delete "newlfont" from the preamble. I copied the preamble years ago and used the same preamble for every LaTeX file. This question is slightly different than my other two questions on alignment in an alignat environment. I changed how I wanted some equalities to be displayed. Since I did not know what the \mathrlap command instructs LaTeX to typeset, I didn't know how to adjust the code. @esdd has given me the code to get exactly what I wanted, and he has given me some explanation for the command \mathrlap. I will ask him for further clarification. – user143462 Jul 18 '14 at 13:06
  • Can you help me with one command as it is used in the code written by esdd in this post? He wrote two codes. I am referring to the second code to emphasize the use of the \mathrlap command. How does the \mathrlap command prevent part of the mathematical expressions in line 7 from shifting to the right margin? Lines 2, 3, 4, 5, and 6 do not have this command and part of the mathematical expressions in these lines are shifted to the right margin. – user143462 Jul 18 '14 at 16:25
4

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}
  • Thanks for both codes! That is exactly what I wanted in an efficient code. I need clarification on three commands in your (first) code. What does \mathmakebox[2em][l]{blah} in the first line instruct LaTeX to typeset? I guess that this shifts the first line 2 ems to the left with respect to the remaining lines. (You put a red box about it.) Please explain how LaTeX is instructed to do this. Why isn't the first line all the way to the right margin. Why is the \mathrlap command needed? I see what happens when you don't have these commands - the line gets shifted to the right margin. Why? – user143462 Jul 18 '14 at 14:04
  • I have a question about your use of the alignat environment. There are 4 alignments to be made but only 3 will be encoded into the file using "&." The first & is placed at the "=" in line 2. How is LaTeX instructed to move the expression in line 1 to the left of the "&" in line 2 by 2 em's? The first "+" in lines 2 and 3 after \abs{x + y + z}^{2} begin as "&= 3 &&\mathrlap{{}+ 2\cos a ..." and as "&&&\mathrlap{{}+ 2\sin a ..." respectively. Is the empty space {} needed so that LaTeX gives the proper space about "+"? – user143462 Jul 18 '14 at 18:14
  • As I have explained the alignat environment works like a table with rl column pairs. The code in the first line is placed before the first & that means in the first column (type r, right aligned). The other lines starts in the second column (type l, left aligned). – esdd Jul 18 '14 at 19:45
  • The {} in \mathrlap{{}+...} is needed for the proper space. It shows LaTeX that the + means addition like a+b and not a sign to a positive number like +2. \mathrlap{...} sets its argument in a left aligned zero width box. That means the contents will overlap to the right. \mathmakebox[2em][l]{...} places ist Argument in a 2em width left aligned box. If the contents is longer then 2em it will overlap to the right. – esdd Jul 18 '14 at 20:01
  • Now, I understand that the kth column width in the alignat environment is determined by the widest space that is demanded by the kth columns in all the rows. In your second code in which the \mathrlap command is removed from the last line, the second column becomes very wide, and that shifts the third column for all the previous rows ridiculously far to the right. Putting the expression in the last row in a box of no width avoids that rightward shift. Fine. Thanks for your other explanations, too. – user143462 Jul 18 '14 at 23:50
6

I'm not sure if I've fully understood your layout requirements. The following solution dispenses with the \rlap directives and uses an align* environment as the outermost math environment. Inside the align* environment, there are a few aligned environments, with the alignment points chosen so as to line up the various cos strings, and a couple of \phantom statements to achieve alignment of the preceding + and - symbols. (There are \! -- negative thin space -- directives in front of each aligned environment per @egreg's suggestion.)

A couple of comments about your code. First, the macros \cos and \sin do not take an argument, so there's no advantage to writing \sin{a} and \cos{b} rather than simply \sin a and \cos b. Second, it's useful to define a macro such as \abs (e.g., via the macro \DeclarePairedDelimiter that's provided by the mathtools package); doing so makes your code much more readable.

enter image description here

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

\begin{document}
\begin{align*}
\MoveEqLeft[2] \abs{ xy + xz + yz }^{2} \\
&= \!\begin{aligned}[t]
3+ \bigl[&\cos(2a + b + c) + \cos(a + 2b + c) + \cos(a + b + 2c) \\
{}+ \phantom{\bigl[}&\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)\,.
\intertext{Likewise,}
\MoveEqLeft[2] \abs{x + y + z}^{2} \\
&= [\cos a + \cos b + \cos c]^{2} + [\sin a + \sin b + \sin c]^{2} \\
&= \!\begin{aligned}[t]
3 &+ 2\cos a\cos b + 2\cos a\cos c + 2\cos b\cos c \\
  &+ 2\sin a\sin b + 2\sin a\sin c + 2\sin b\sin c 
\end{aligned}\\
&= \!\begin{aligned}[t]
3 + \bigl[&\cos(a + b) + \cos(a + c) + \cos(b + c) \\
{}+ \phantom{\bigl[}&\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] 
\end{aligned}\\
&= 3+ 2\bigl(\cos(a - b) + \cos(a - c) + \cos(b - c)\bigr).
\end{align*}
\end{document}
  • The only thing you should add is \! in front of \begin{aligned} – egreg Jul 17 '14 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.