5

I'm having troubles in aligning some items in this MWE :

\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}

\begin{document}

\begin{alignat}{2}
    AAAAA &= BBBBBB \\
    &&= CCCCCC + DDDDDDD,
\\[1ex]
    EEE &= FFFFFFF \\
    &&= 38 \, GGGGGGGG + 45 \, HHHHHHH,
\\[1ex]
    KKKKKKKKK &= LL \\
    &&= MM + N.
\end{alignat}

\end{document}

Preview:

enter image description here

The real lines in my code that I identified with $CCC\dots + DDD\dots$, $38 GGG \dots$ and $MM + N$ are actually pretty long and cannot be aligned with the parts above them ($BBB\dots$, etc). However, I would like them to be aligned like this:

enter image description here

So how can I achieve this?

In other words, the short lines (1), (3) and (5) should stay aligned together, while the much longer lines (2), (4) and (6) should be aligned together.


EDIT Here's the real life maths output I have, from three choices. The first one is a simple align environment, but the long parts goes in the right marging. The second version is a gather environment with three seperately aligned parts. The third version is a simple align, with a negative space, as suggested by some of the answers below. So which one is the best less confusing output?

enter image description here

As requested by egreg in the comments, I'm adding the (simplified) code that makes the last picture:

\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage{tensor}

\begin{document}

Blabla bla bla bla blablabla bla blabla bla bla blablabla:
\begin{align*}
    \tensor{\gamma}{_i} \, \tensor{\gamma}{_j} + \tensor{\gamma}{_j} \, \tensor{\gamma}{_i} &= (\, \tensor{a}{_i} + a_i^{\dag} )(\, \tensor{a}{_j} + a_j^{\dag} ) + (\, \tensor{a}{_j} + a_j^{\dag} )(\, \tensor{a}{_i} + a_i^{\dag} ) \\
    &= \tensor{a}{_i} \: \tensor{a}{_j} + \tensor{a}{_i} \: a_j^{\dag} + a_i^{\dag} \: \tensor{a}{_j} + a_i^{\dag} \: a_j^{\dag} + \tensor{a}{_j} \: \tensor{a}{_i} + \tensor{a}{_j} \: a_i^{\dag} + a_j^{\dag} \: \tensor{a}{_i} + a_j^{\dag} \: a_i^{\dag} = 2 \, \tensor{\delta}{_{ij}}, \\[1ex]
    \tensor{\gamma}{_{n \,+\, i}} \, \tensor{\gamma}{_{n \,+\, j}} + \tensor{\gamma}{_{n \,+\, j}} \, \tensor{\gamma}{_{n \,+\, i}} &= -\: (\, \tensor{a}{_i} - a_i^{\dag} )(\, \tensor{a}{_j} - a_j^{\dag} ) - (\, \tensor{a}{_j} - a_j^{\dag} )(\, \tensor{a}{_i} - a_i^{\dag} ) \\
    &= -\: \tensor{a}{_i} \: \tensor{a}{_j} + \tensor{a}{_i} \: a_j^{\dag} + a_i^{\dag} \: \tensor{a}{_j} - a_i^{\dag} \: a_j^{\dag} - \tensor{a}{_j} \: \tensor{a}{_i} + \tensor{a}{_j} \: a_i^{\dag} + a_j^{\dag} \: \tensor{a}{_i} - a_j^{\dag} \: a_i^{\dag} = 2 \, \tensor{\delta}{_{ij}}, \\[1ex]
    \tensor{\gamma}{_{n \,+\, i}} \, \tensor{\gamma}{_j} + \tensor{\gamma}{_j} \, \tensor{\gamma}{_{n \,+\, i}} &= -\: i  (\, \tensor{a}{_i} - a_i^{\dag} )(\, \tensor{a}{_j} + a_j^{\dag} ) - i  (\, \tensor{a}{_j} + a_j^{\dag} )(\, \tensor{a}{_i} - a_i^{\dag} ) \\
    &= -\: i \, \tensor{a}{_i} \: \tensor{a}{_j} - i \, \tensor{a}{_i} \: a_j^{\dag} + i \, a_i^{\dag} \: \tensor{a}{_j} + i \, a_i^{\dag} \: a_j^{\dag} - i \, \tensor{a}{_j} \: \tensor{a}{_i} + i \, \tensor{a}{_j} \: a_i^{\dag} - i \, a_j^{\dag} \: \tensor{a}{_i} + i \, a_j^{\dag} \: a_i^{\dag} = 0.
\end{align*}
Blabla bla bla bla blablabla bla blabla bla bla blablabla:
\begin{gather*}
\begin{aligned}
    \tensor{\gamma}{_i} \, \tensor{\gamma}{_j} + \tensor{\gamma}{_j} \, \tensor{\gamma}{_i} &= (\, \tensor{a}{_i} + a_i^{\dag} )(\, \tensor{a}{_j} + a_j^{\dag} ) + (\, \tensor{a}{_j} + a_j^{\dag} )(\, \tensor{a}{_i} + a_i^{\dag} ) \\
    &= \tensor{a}{_i} \: \tensor{a}{_j} + \tensor{a}{_i} \: a_j^{\dag} + a_i^{\dag} \: \tensor{a}{_j} + a_i^{\dag} \: a_j^{\dag} + \tensor{a}{_j} \: \tensor{a}{_i} + \tensor{a}{_j} \: a_i^{\dag} + a_j^{\dag} \: \tensor{a}{_i} + a_j^{\dag} \: a_i^{\dag} = 2 \, \tensor{\delta}{_{ij}},
\end{aligned}
\\[1ex]
\begin{aligned}
    \tensor{\gamma}{_{n \,+\, i}} \, \tensor{\gamma}{_{n \,+\, j}} + \tensor{\gamma}{_{n \,+\, j}} \, \tensor{\gamma}{_{n \,+\, i}} &= -\: (\, \tensor{a}{_i} - a_i^{\dag} )(\, \tensor{a}{_j} - a_j^{\dag} ) - (\, \tensor{a}{_j} - a_j^{\dag} )(\, \tensor{a}{_i} - a_i^{\dag} ) \\
    &= -\: \tensor{a}{_i} \: \tensor{a}{_j} + \tensor{a}{_i} \: a_j^{\dag} + a_i^{\dag} \: \tensor{a}{_j} - a_i^{\dag} \: a_j^{\dag} - \tensor{a}{_j} \: \tensor{a}{_i} + \tensor{a}{_j} \: a_i^{\dag} + a_j^{\dag} \: \tensor{a}{_i} - a_j^{\dag} \: a_i^{\dag} = 2 \, \tensor{\delta}{_{ij}},
\end{aligned}
\\[1ex]
\begin{aligned}
    \tensor{\gamma}{_{n \,+\, i}} \, \tensor{\gamma}{_j} + \tensor{\gamma}{_j} \, \tensor{\gamma}{_{n \,+\, i}} &= -\: i  (\, \tensor{a}{_i} - a_i^{\dag} )(\, \tensor{a}{_j} + a_j^{\dag} ) - i  (\, \tensor{a}{_j} + a_j^{\dag} )(\, \tensor{a}{_i} - a_i^{\dag} ) \\
    &= -\: i  \bigl( \, \tensor{a}{_i} \: \tensor{a}{_j} + \tensor{a}{_i} \: a_j^{\dag} - a_i^{\dag} \: \tensor{a}{_j} - a_i^{\dag} \: a_j^{\dag} + \tensor{a}{_j} \: \tensor{a}{_i} - \tensor{a}{_j} \: a_i^{\dag} + a_j^{\dag} \: \tensor{a}{_i} - a_j^{\dag} \: a_i^{\dag}  \bigr) = 0.
\end{aligned}
\end{gather*}
Blabla bla bla bla blablabla bla blabla bla bla blablabla:
\begin{align*}
    \tensor{\gamma}{_i} \, \tensor{\gamma}{_j} + \tensor{\gamma}{_j} \, \tensor{\gamma}{_i} &= (\, \tensor{a}{_i} + a_i^{\dag} )(\, \tensor{a}{_j} + a_j^{\dag} ) + (\, \tensor{a}{_j} + a_j^{\dag} )(\, \tensor{a}{_i} + a_i^{\dag} ) \\
    &\hspace{-2cm}= \tensor{a}{_i} \: \tensor{a}{_j} + \tensor{a}{_i} \: a_j^{\dag} + a_i^{\dag} \: \tensor{a}{_j} + a_i^{\dag} \: a_j^{\dag} + \tensor{a}{_j} \: \tensor{a}{_i} + \tensor{a}{_j} \: a_i^{\dag} + a_j^{\dag} \: \tensor{a}{_i} + a_j^{\dag} \: a_i^{\dag} = 2 \, \tensor{\delta}{_{ij}},
\\[1ex]
    \tensor{\gamma}{_{n \,+\, i}} \, \tensor{\gamma}{_{n \,+\, j}} + \tensor{\gamma}{_{n \,+\, j}} \, \tensor{\gamma}{_{n \,+\, i}} &= -\: (\, \tensor{a}{_i} - a_i^{\dag} )(\, \tensor{a}{_j} - a_j^{\dag} ) - (\, \tensor{a}{_j} - a_j^{\dag} )(\, \tensor{a}{_i} - a_i^{\dag} ) \\
    &\hspace{-2cm}= -\: \tensor{a}{_i} \: \tensor{a}{_j} + \tensor{a}{_i} \: a_j^{\dag} + a_i^{\dag} \: \tensor{a}{_j} - a_i^{\dag} \: a_j^{\dag} - \tensor{a}{_j} \: \tensor{a}{_i} + \tensor{a}{_j} \: a_i^{\dag} + a_j^{\dag} \: \tensor{a}{_i} - a_j^{\dag} \: a_i^{\dag} = 2 \, \tensor{\delta}{_{ij}},
\\[1ex]
    \tensor{\gamma}{_{n \,+\, i}} \, \tensor{\gamma}{_j} + \tensor{\gamma}{_j} \, \tensor{\gamma}{_{n \,+\, i}} &= -\: i  (\, \tensor{a}{_i} - a_i^{\dag} )(\, \tensor{a}{_j} + a_j^{\dag} ) - i  (\, \tensor{a}{_j} + a_j^{\dag} )(\, \tensor{a}{_i} - a_i^{\dag} ) \\
    &\hspace{-2cm}= -\: i  \bigl( \, \tensor{a}{_i} \: \tensor{a}{_j} + \tensor{a}{_i} \: a_j^{\dag} - a_i^{\dag} \: \tensor{a}{_j} - a_i^{\dag} \: a_j^{\dag} + \tensor{a}{_j} \: \tensor{a}{_i} - \tensor{a}{_j} \: a_i^{\dag} + a_j^{\dag} \: \tensor{a}{_i} - a_j^{\dag} \: a_i^{\dag}  \bigr) = 0.
\end{align*}
\end{document}
6
  • Could you please be move specific as to what you are trying, it is not particularly clear, the last image in particularly does not make any sense to me. Additionally, you specified 2 alignment columns, thus each row needs 3 &'s, two to make alignments and one to separate the alignment groups.
    – daleif
    Mar 4, 2020 at 20:38
  • @daleif, the alignat environment was used only to built this MWE. It doesn't work well for what I want to achieve, but currently it was the best bet I had.
    – Cham
    Mar 4, 2020 at 20:39
  • @Cham Can you please add the code for the picture, so we have something to play with?
    – egreg
    Mar 4, 2020 at 21:04
  • @egreg, you mean the full code of the last picture? It's really a complicated code, with several packages loaded!
    – Cham
    Mar 4, 2020 at 21:05
  • @Cham I see no reason for several packages. Just add the code for the displays.
    – egreg
    Mar 4, 2020 at 21:08

3 Answers 3

5

Sorry, but you're the cause of your own troubles. 😉

There are too many arbitrary added spaces, that make the formulas harder to read, both in the code and in the output. The use of \tensor can be avoided and the subscripts be vertically aligned nonetheless.

I define a \ts command that prints a phantom superscript dag, if not called as \ts* (that also recalls transposition).

By removing the added spacings, the formulas fit in the assigned text width. I also decided to align also the last equals signs in the formulas, so they're not lost by the reader at the end of a very long term.

enter image description here

The code:

\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage{xparse}

\NewDocumentCommand{\ts}{sm}{%
 #2\IfBooleanTF{#1}{^{\dag}}{^{\vphantom{\dag}}}%
}

\begin{document}

Blabla bla bla bla blablabla bla blabla bla bla blablabla
bla bla bla blablabla bla blabla bla bla blablabla
bla bla bla blablabla bla blabla bla bla blablabla
\begin{align*}
\ts{\gamma}_{i} \ts{\gamma}_{j} + \ts{\gamma}_{j} \ts{\gamma}_{i} 
&= ( \ts{a}_{i} + \ts*{a}_i )( \ts{a}_{j} + \ts*{a}_j ) 
 +( \ts{a}_{j} + \ts*{a}_j )( \ts{a}_{i} + \ts*{a}_i )
\\
&= \ts{a}_{i} \ts{a}_{j} + \ts{a}_{i} \ts*{a}_j
  +\ts*{a}_i \ts{a}_{j} + \ts*{a}_i \ts*{a}_j
  +\ts{a}_{j} \ts{a}_{i} + \ts{a}_{j} \ts*{a}_i + \ts*{a}_j \ts{a}_{i} 
  +\ts*{a}_j \ts*{a}_i
\\
&= 2\ts{\delta}_{ij},
\\[1ex]
\ts{\gamma}_{n + i} \ts{\gamma}_{n + j} + \ts{\gamma}_{n + j} \ts{\gamma}_{n + i}
&=-( \ts{a}_{i} - \ts*{a}_i )( \ts{a}_{j} - \ts*{a}_j )
  -( \ts{a}_{j} - \ts*{a}_j )( \ts{a}_{i} - \ts*{a}_i )
\\
&=-\ts{a}_{i} \ts{a}_{j} + \ts{a}_{i} \ts*{a}_j + \ts*{a}_i \ts{a}_{j}
  -\ts*{a}_i \ts*{a}_j - \ts{a}_{j} \ts{a}_{i} + \ts{a}_{j} \ts*{a}_i
  +\ts*{a}_j \ts{a}_{i} - \ts*{a}_j \ts*{a}_i
\\
&= 2\ts{\delta}_{ij},
\\[1ex]
\ts{\gamma}_{n + i} \ts{\gamma}_{j} + \ts{\gamma}_{j} \ts{\gamma}_{n + i}
&=-i( \ts{a}_{i} - \ts*{a}_i )( \ts{a}_{j} + \ts*{a}_j )
  -i( \ts{a}_{j} + \ts*{a}_j )( \ts{a}_{i} - \ts*{a}_i )
\\
&=-i\ts{a}_{i} \ts{a}_{j} - i \ts{a}_{i} \ts*{a}_j
  +i\ts*{a}_i \ts{a}_{j} + i \ts{a}_i \ts*{a}_j
  -i\ts{a}_{j} \ts{a}_{i} + i \ts{a}_{j} \ts*{a}_i
  -i\ts*{a}_j \ts{a}_{i} + i \ts*{a}_j \ts*{a}_i
\\
&= 0.
\end{align*}

\end{document}
7
  • Very very nice....the emoji 😁😁😁😁😁😁😁 and the answer.
    – Sebastiano
    Mar 4, 2020 at 22:18
  • Why the \ts macro? Why not the tensor command, which gives the right index position anyway?
    – Cham
    Mar 4, 2020 at 22:39
  • @Cham A much more efficient one.
    – egreg
    Mar 4, 2020 at 22:40
  • More efficient in what respect? The tensor package does everything I need for the index positions.
    – Cham
    Mar 4, 2020 at 22:43
  • Oh, and I fully agree about the last equality, which should be on a separate line. I've done it this way so save vertical space, but it appears that adding a new line is actually much better!
    – Cham
    Mar 4, 2020 at 22:45
5

Your alignment is out as on the even numbered rows you are missing a & so the fragments are right aligned not left, this is shown in the first block, but it appears you want the alignment in the second block (which seems a bit confusing but...)

The negative spacing in the requested form completely obscures the mathematics: the = that are referring to the same value are not aligned, but = referring to different values are aligned.

If the values are too wide to align all the = I would align = that refer to the same value, but allow the individual equations to align separately as in ccc

enter image description here

\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}

\begin{document}

aaa
\begin{alignat}{2}
    AAAAA &= BBBBBB \\
    &&&= CCCCCC + DDDDDDD,
\\[1ex]
    EEE &= FFFFFFF \\
    &&&= 38 \, GGGGGGGG + 45 \, HHHHHHH,
\\[1ex]
    KKKKKKKKK &= LL \\
    &&&= MM + N.
\end{alignat}


bbb
\begin{alignat}{1}
    AAAAA &= BBBBBB \\
    &\hspace{-1cm}= CCCCCC + DDDDDDD,
\\[1ex]
    EEE &= FFFFFFF \\
    &\hspace{-1cm}= 38 \, GGGGGGGG + 45 \, HHHHHHH,
\\[1ex]
    KKKKKKKKK &= LL \\
    &\hspace{-1cm}= MM + N.
\end{alignat}



ccc
\begin{align}
    AAAAA &= BBBBBB \\
    &= CCCCCC + DDDDDDD,
\end{align}
\begin{align}
    EEE &= FFFFFFF \\
    &= 38 \, GGGGGGGG + 45 \, HHHHHHH,
\end{align}
\begin{align}
    KKKKKKKKK &= LL \\
    &= MM + N.
\end{align}
\end{document}
7
  • Then the alignat becomes useless, and should be replaced with a simple align instead? But then, the lines (2), (4) and (6) are a bit too long and goes outside the right margin.
    – Cham
    Mar 4, 2020 at 20:44
  • @Cham I would use align rather than alignat for both of these forms but I kept alignat as that is what you had. The examples here are not too long and do not go out of the margin, if your your real examples in your real document do then that is a possibility, but then you can use the second more compact form. Mar 4, 2020 at 20:47
  • I admit that the real maths are getting confusing without all equal signs aligned for all lines. But then, should I let some of the lines getting a bit inside the right margin? It looks a bit ugly!
    – Cham
    Mar 4, 2020 at 20:51
  • 1
    @Cham some journals would simply refuse to print if the text is sticking in the margin so that may be completely forbidden and will be ugly as you say. Without seeing the real example it's hard to say, usually you can break the right hand side somewhere Mar 4, 2020 at 20:54
  • The negative space is a nifty trick. I may use it.
    – Cham
    Mar 4, 2020 at 20:55
4
\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}
\def\z#1{\llap{$\mathrlap{\displaystyle#1}$\hspace{3.5em}}}
\begin{document}

\begin{alignat}{2}
    AAAAA &= BBBBBB \\
    &\z{= CCCCCC + DDDDDDD,}
\\[1ex]
    EEE &= FFFFFFF \\
    &\z{= 38 \, GGGGGGGG + 45 \, HHHHHHH,}
\\[1ex]
    KKKKKKKKK &= LL \\
    &\z{= MM + N.}
\end{alignat}

\end{document}

enter image description here

2
  • you might want to add \displaystyle inside your rlap as you can't tell here with just AAA but you are setting rows alternating textstyle then displaystyle, but+1 for the excellent macro name Mar 4, 2020 at 20:49
  • @DavidCarlisle Will do. I was merely paying homage to the master. Mar 4, 2020 at 20:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .