4

I have nested gather and align according to

\begin{gather}
       \sum\limits_{i=1}^n i + \text{expression} = \dfrac{n(n-1)}{2} \label{l1}\\
       \begin{align}
            a+\text{expression}&=b \\ %\label{l2} doesnt work
            c&=d+\text{expression} \\
            e+\text{long expression}&=f-\text{expression}
       \end{align}
 \end{gather}

to get an output looking like this: enter image description here

The problem is that it is not possible to label all equations separately. The problem is identical to that discussed in How do I get this strange alignment?, except that in my problem the first line needs to be centered.

Thanks for any help.

2
  • Thanks for your reply. I do not use equation for (1) and align for (2)-(4) as it will result in too much space between (1) and (2). Of course, I could use \vspace to reduce the gap, but I wonder if there is a more elegant solution. Commented Jan 26, 2018 at 14:32
  • The possibility to use align inside gather is explicitly mentioned in the official guide to amsmath (revision 2016-11-14 and, I'm pretty sure, also in the next revision).
    – egreg
    Commented Jan 26, 2018 at 21:16

2 Answers 2

3

The possibility of using align inside gather is explicitly mentioned in the official guide to amsmath.

However it appears there is a bug with \label; the errors seem innocuous, in the sense that the labels are absorbed correctly notwithstanding, so I guess it should be possible to fix the issue.

In the meantime, you can trick amsmath into centering the line with a low level hack:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
\multispan{2}\hfill$\displaystyle
  \sum_{i=1}^n i + \text{expression} = \dfrac{n(n-1)}{2}
$\hfill \label{l1}\\
a+\text{expression}&=b \label{l2} \\
c&=d+\text{expression} \label{l3} \\
e+\text{long expression}&=f-\text{expression}
\end{align}

\eqref{l1} and \eqref{l2} and \eqref{l3}

\end{document}

which will work for the present display, but is not guaranteed to work generally.

enter image description here

Update August 2019

It turns out that the issue is essentially cosmetic:

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\endalign}{\restorealignstate@}{\global\let\df@label\@empty\restorealignstate@}{}{}
\makeatother

\begin{document}

\begin{gather}
  \sum_{i=1}^n i + \text{expression} = \frac{n(n-1)}{2} \label{l1}\\
  \begin{align}
    a+\text{expression}&=b \label{l2} \\
    c&=d+\text{expression} \\
    e+\text{long expression}&=f-\text{expression}
  \end{align}
\end{gather}

\eqref{l1} and \eqref{l2}

\end{document}

enter image description here

2
  • This doesn't seem to work with more than one \label within {align}. What do you think of my idea in tex.stackexchange.com/a/653587/82917?
    – campa
    Commented Aug 11, 2022 at 10:07
  • @campa I'm aware that there are problems with align in gather.
    – egreg
    Commented Aug 11, 2022 at 10:29
0

Here are two ways to align equations in a gather. The first uses \phantom, \mathllap and \mathrlap. The second uses saveboxes and \mathmakebox. The trick is to locate the longest expressions to the left and right of the = and make everything the same width.

Note: for the last line to work the saveboxes need to be defined outside the gather.

Of course, I would hardly call either of these elegant.

\documentclass{article}
\usepackage{mathtools}

\begin{document}
\begin{gather}
       \sum\limits_{i=1}^n i + \text{expression} = \dfrac{n(n-1)}{2} \label{l1}\\
       \phantom{e+\text{long expression}}\mathllap{a+\text{expression}}
         =\mathrlap{b}\phantom{f-\text{expression}} \label{l2} \\
       \phantom{e+\text{long expression}}\mathllap{c}
         =\mathrlap{d+\text{expression}}\phantom{f-\text{expression}} \\
       e+\text{long expression}=f-\text{expression}
\end{gather}

\eqref{l1} and \eqref{l2}

\sbox0{$\displaystyle e+\text{long expression}$}%
\sbox1{$\displaystyle f-\text{expression}$}%
\begin{gather}
       \sum\limits_{i=1}^n i + \text{expression} = \dfrac{n(n-1)}{2} \\
       \mathmakebox[\wd0][r]{a+\text{expression}}
         =\mathmakebox[\wd1][l]{b} \\
       \mathmakebox[\wd0][r]{c}
         =\mathmakebox[\wd1][l]{d+\text{expression}} \\
       \usebox0=\usebox1
 \end{gather}

\begin{equation}
       \sum\limits_{i=1}^n i + \text{expression} = \dfrac{n(n-1)}{2} \label{l1}
\end{equation}
\vspace{\dimexpr \smallskipamount-\abovedisplayskip-\belowdisplayskip}%
\begin{align}
a+\text{expression}&=b \\ %\label{l2} doesnt work
            c&=d+\text{expression} \\
            e+\text{long expression}&=f-\text{expression}
\end{align}

\end{document}

demo

You must log in to answer this question.

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