1

I'm trying to align some equations, but I want a variable number of anchors per line.

\documentclass[]{article}
\usepackage{amsmath}
\begin{document}
\begin{alignat*}{2}
    a &= a',& \quad b &= b' \\
    c &= c',& \quad d &= d' \\
    e &= (e'_1 + \cdots + e'_n)(e''_1 + \cdots + e''_n)(e'''_1 + \cdots + e'''_n)
\end{alignat*}
\end{document}

The first two lines should have two anchors, aligning their = signs. The final line is much longer and only has a single = on it. I want this to be aligned with the left-most = of the other two lines.

The way it is currently written, the second = sign on the first two lines is far off to the right, after the entirety of the bottom line. In essence, adding the bottom line shouldn't change the relative positioning of the other four = signs. It may change their position on the page as the whole thing should be centred.

I've tried this with every incarnation of align/ed/at that I can think of, but have not been successful! I'd rather not use things like \phantom{a} to manually move stuff around if I can avoid it. Also, things like \mathrlap aren't so helpful because they aren't taken into account when doing the centralising.


To be completely clear, I want the entire display to be centralised. This includes the fact that the last row is the longest, so it should be centralised. I want all three of the left-most = signs aligned and the right two also aligned, even though a' and c' are different lengths---of course, in my actual application, they're more complicated than just a' and c'.

The precise code follow.

\documentclass[]{article}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}
\begin{alignat*}{2}
    N_0 &\coloneqq L_0,& \quad N_k &\coloneqq L_{f(k+1)} / L_{f(k)} \\
    S_0 &\coloneqq 0,& \quad S_{k+1} &\coloneqq S_k + 2 d^2 L_{f(k)} T_{f(k)} N_k; \\
    C_k &\coloneqq \cap_{i \in \overline{\mathcal Q}(i_0; N_k), \: m \in [2 d^2 L_{f(k)} N_k]} \bigl( \mathcal P_{f(k),k}(i) \cap \mathcal G_{f(k),k}(i; S_k + (m-1) T_k) \bigr)
\end{alignat*}
\end{document}
2
  • The alignment tag should indicate horizontal, not vertical. Jan 27, 2022 at 16:07
  • @barbarabeeton oops, yes!
    – Sam OT
    Jan 27, 2022 at 16:13

4 Answers 4

2

Here is a solution using the IEEEeqnarray environment from the IEEEtrantools package.

enter image description here

\documentclass[]{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{IEEEtrantools}
\usepackage{showframe}

\begin{document}
\begin{IEEEeqnarray*}{rClrCl}
    a &=& a',& \quad b &=& b' \\
    c &=& c',& \quad d &=& d' \\
    e &=& \IEEEeqnarraymulticol{4}{l}{(e'_1 + \cdots + e'_n)(e''_1 + \cdots + e''_n)(e'''_1 + \cdots + e'''_n)}
\end{IEEEeqnarray*}

\begin{IEEEeqnarray*}{rClrCl}
    N_0 &\coloneqq& L_0,& \quad N_k &\coloneqq& L_{f(k+1)} / L_{f(k)} \\
    S_0 &\coloneqq& 0,  & \quad S_{k+1} &\coloneqq& S_k + 2 d^2 L_{f(k)} T_{f(k)} N_k; \\
    C_k &\coloneqq& \IEEEeqnarraymulticol{4}{l}{\cap_{i \in \overline{\mathcal Q}(i_0; N_k), \: m \in [2 d^2 L_{f(k)} N_k]} \bigl( \mathcal P_{f(k),k}(i) \cap \mathcal G_{f(k),k}(i; S_k + (m-1) T_k) \bigr)}
\end{IEEEeqnarray*}
\end{document}
1
  • Now this is almost basically perfect! I would prefer not to use the IEEE packages, sticking with just align/ed/at, but maybe this is necessary. It's a shame that the align/ed/at ones doesn't have an analogous 'multi-column' option
    – Sam OT
    Jan 27, 2022 at 20:53
2

Cheat!

\documentclass{article}
\usepackage{amsmath,mathtools}
\usepackage{showframe} % to show centering

\begin{document}

\begin{align*}
&\begin{alignedat}{2}
  \mathllap{N_0} &\coloneqq L_0, &\qquad\qquad N_k &\coloneqq L_{f(k+1)} / L_{f(k)} \\
  \mathllap{S_0} &\coloneqq 0,   &\qquad\qquad S_{k+1} &\coloneqq S_k + 2 d^2 L_{f(k)} T_{f(k)} N_k; \\
  \end{alignedat}
\\
C_k &\coloneqq \bigcap\nolimits_{i \in \overline{\mathcal Q}(i_0; N_k), \: 
  m \in [2 d^2 L_{f(k)} N_k]} \bigl( \mathcal P_{f(k),k}(i) \cap \mathcal G_{f(k),k}(i; S_k + (m-1) T_k) \bigr)
\end{align*}

\end{document}

I use an outer align and the \coloneqq symbols are the point of alignments; the inner alignedat has zero width entries in the first column and the last line happily has the widest item.

enter image description here

5
  • This appeared to me to be the best option. It doesn't really matter what the spacing between the two parts on the first line is---\quad was somewhat arbitrary---and more important to have it correctly centralised. But it does have some issues. As I said, my actual displays are more complicated. My final line is actually $$e = \cap_{i \in \overline Q(i_0; N_k), \: m \in [2 d^2 L_{f(k)} N_k]} \big( ... \bigr)$$. (Don't judge: it's still in draft notation!) I can't put the && inside the subscript...
    – Sam OT
    Jan 27, 2022 at 17:33
  • @SamOT I can't answer what you don't ask, sorry…
    – egreg
    Jan 27, 2022 at 17:57
  • Indeed. But I did say that the things were more complicated and you did say that yours is a 'cheat'. This 'cheat' works in a simple case, but doesn't work in more complicated cases. Perhaps you can edit your answer to acknowledge this? Otherwise it looks like it's a solution
    – Sam OT
    Jan 27, 2022 at 18:45
  • @SamOT I provided a better cheat for the real formula.
    – egreg
    Jan 27, 2022 at 20:57
  • Ah, that's very good! I was trying to use \aligned/at inside another align environment, but the issue was that I could only anchor the left-most part of the first two :=. I didn't think of using \mathllap for the very first parts, as you did! Sure, the centralisation is still wrong: it's too far to the left, since it doesn't take into account the \mathllap---you can see this by replacing N_0 with some long formula---but it's pretty minor in this case. Excellent! It's a shame there isn't some way of having some 'persistent' anchor, but your cheat is very close to the desired result!
    – Sam OT
    Jan 27, 2022 at 21:01
1

Okay, this is cheating. I've simply copied @Zarko's example and made the adjustment I suggested in my comment. (However, this answer, along with my comment, have been deleted by the author.

The suggestion was to examine the aligned display with lines of uneven length, determine what part of the longest line was "too long", and insert that as a \phantom on the next-longest line (within the subsidiary alignment). Granted, this is a bit of a hack, but it's much easier to accomplish than trying to construct a custom environment.

 \begin{document}
\thispagestyle{empty}
Text above the display.  Make it two lines so that the width of the text
block is obvious.  Make it two lines so that the width of the text
block is obvious.
\begin{alignat*}{2}
    a & = a',   &\qquad   b &= b' \\
    c & = c',   &\qquad   d &= d'
         \phantom{\quad (e''_1 + \cdots + e''_n)(e'''_1 + \cdots + e'''_n)}\\
    e & = \mathrlap{(e'_1 + \cdots + e'_n)(e''_1 + \cdots + e''_n)(e'''_1 + \cd\
ots + e'''_n)}
\end{alignat*}
Some more text directly following the display.

Let's do this again, without adding the \verb+\phantom+.
\begin{alignat*}{2}
    a & = a',   &\qquad   b &= b' \\
    c & = c',   &\qquad   d &= d' \\
    e & = \mathrlap{(e'_1 + \cdots + e'_n)(e''_1 + \cdots + e''_n)(e'''_1 + \cdots + e'''_n)}
\end{alignat*}
Some final text.
\end{document}

The reason for the \phantom in the second line is to make all lines of the display the same width. The effect of doing this can be seen by comparing the second display, which is what we started with.

output of example code

7
  • I guess this is approximately right. But it's still a bit wrong, yeah? You've added all that spacing to the second row. But now the text is too far to the left---it is positioned as if the \phantom part were not phantom but written normally, yeah?
    – Sam OT
    Jan 27, 2022 at 17:31
  • @SamOT -- The text of the first line is to the left because I applied \noindent. It would have been more "natural" if I had added two lines of text. (Okay; I'll consider changing that.) The purpose was to establish the location of the edges of the text block. The line following the display is intended as "continuation of the paragraph", thus not inented. Jan 27, 2022 at 18:45
  • Sure, I wasn't thinking that it's an indent. If you take away the \phantom command, then you'll see that the second line is now longer than the last. Thus, it is incorrectly centred. I mean, it's really not a big deal at all! But is is till incorrect. I'll probably just go with your answer, though, as it's the closest to correct :) -- I was just surprised that there isn't a simple solution to this simple question! I can't be the first person to want to do this =P
    – Sam OT
    Jan 27, 2022 at 18:52
  • @SamOT -- I was part of the technical support team at the American Math Society since TeX was adopted there. The suggested "adjustment" is the approach taken in the AMS book and journal production for final cleanup. As noted in my answer, this is much easier than trying to create a custom environment. And it follows the philosophy in the TeXbook, which is that sometimes manual adjustments are needed or desirable. Jan 27, 2022 at 19:18
  • Ah, that's cool! But yeah, my point still stands that it is incorrectly centred. You can see this by taking away your \phantom command---leave what inside it, though! Sorry for not making that clear. This way, you can see the spacing. I can just replace the \phantom{...} command with \hspace{...} and find the approximate spacing. If you remove the \phantom part, so that the text inside it is printed, you can see that it's actually qutie a long way off correct
    – Sam OT
    Jan 27, 2022 at 20:52
0

It seems that there isn't a simple solution, perhaps surprisingly. @Imran shows how to do it properly, but with the IEEE package, which is a bit different.

Barbara and egreg have shown some approximations. Both get the centralisation slightly wrong, but only bit a minor amount, and this could be manually adjusted. So, since there isn't a solution, I thought I'd put in my best cheat. This does get the exact desired result---including correct centralisation---but it needs slight refinement each time. It's basically doing the horizontal alignment manually.

\documentclass[]{article}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}
Without any spacing adjustment, the second $\coloneqq$ on the second row would be further to the right and on the first row. If it were the other way around, then the \verb|\mathrlap|/\verb|\phantom|/\verb|\mathllap| needs to be done on the second line.
\begin{align*}
    N_0 &\coloneqq \mathrlap{L_0,} \phantom{0, \quad S_{k+1}} \mathllap{N_k} \coloneqq L_{f(k+1)} / L_{f(k)} \\
    S_0 &\coloneqq 0, \quad S_{k+1} \coloneqq S_k + 2 d^2 L_{f(k)} T_{f(k)} N_k; \\
    C_k &\coloneqq \cap_{i \in \overline{\mathcal Q}(i_0; N_k), \: m \in [2 d^2 L_{f(k)} N_k]} \bigl( \mathcal P_{f(k),k}(i) \cap \mathcal G_{f(k),k}(i; S_k + (m-1) T_k) \bigr)
\end{align*}
\end{document}

enter image description here

5
  • In this alignment, you have only one & per line, so using the \*laps in the way you've done here, you could use a single amsmath align structure. Not trying to push that alternative, just suggesting that there are quite a few different ways to look at the problem. (I don't see an approach that avoids use of some \*lap somewhere in the coding. Consider what you might have to do if one of the elements preceding the first relation were quite a bit longer than the others) Jan 27, 2022 at 21:22
  • @barbarabeeton Indeed. I hav eactually used align, if you notice! :) -- I agree, it seems that \math*lap is needed somewhere. This way has the advantage of getting the spacing perfect, but it is quite fiddly! In the real world, yours and egreg's solutions are probably better. Yours gets closer to correct, but his is a bit more "algorithmic": no guessing of lengths.
    – Sam OT
    Jan 27, 2022 at 21:25
  • I feel I've learnt quite a bit from this question! It's a shame there's no simple functionality for this. Some kind of 'multi-column' option. Ah well, I've learnt some stuff so that's good! =D
    – Sam OT
    Jan 27, 2022 at 21:26
  • Oops! I read the text, not the code. Other people could learn something if you gave a link to the IEEE approach that you mention. (Learning new things is usually good.) Jan 27, 2022 at 21:31
  • It's Imran's answer. I don't know any more than he wrote there. I've never used the IEEE stuff, apart from one quick submission I did once---got rejected :Sadge:
    – Sam OT
    Jan 27, 2022 at 21:41

You must log in to answer this question.

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