5

1. Desired outcome

I'm frequently finding myself trying to align parts of an equation block independently of other parts. For example, here is a left-aligned equation block, where some of the equations span multiple lines and have their terms aligned independently of the other equations. I'm using words as placeholders here but these are actually mathematical expresssions:

   lorem + ipsum
= (dolor       +sit        +amet
   consectetur -adipiscing -elit)
= (sed     -do
   siusmod +tempor)

2. Single aligned env

Doing this naively with aligned adds undesired additional whitespace because it shares the columns among all three equations. For example, the second terms don't start until the end of the first equation. Similarly, the second terms in the third equation are pushed right to start with the longer second terms in the equation above:

\begin{gather}
\begin{aligned}
   &\text{lorem} + \text{ipsum} \\
= (&\text{dolor}       &&+\text{sit}        &&+\text{amet} \\
   &\text{consectetur} &&-\text{adipiscing} &&-\text{elit}) \\
= (&\text{sed}     &&-\text{do} \\
   &\text{siusmod} &&+\text{tempor}).
\end{aligned}
\end{gather}

shared align additional whitespace

3. Nested aligned envs

I've also tried nesting aligned environments. This does not quite give the correct result, because the = ( and ) outside of the inner aligned environment are vertically centered while they should be at the top and bottom, respectively:

\begin{gather}
\begin{aligned}
   &\text{lorem} + \text{ipsum} \\
= (&\begin{aligned}
    &\text{dolor}       &&+\text{sit}        &&+\text{amet} \\
    &\text{consectetur} &&-\text{adipiscing} &&-\text{elit}
\end{aligned}) \\
= (&\begin{aligned}
    &\text{sed}     &&-\text{do} \\
    &\text{siusmod} &&+\text{tempor}
\end{aligned}).
\end{aligned}
\end{gather}

nested align with misaligned parens

4. Prefix and postfix inside

Moving the =( and ) inside the inner align environment breaks the first column that aligns all five lines. In general, the prefix and postfix is not the same width for all equations so I can't fix this by simply indenting the first equation more:

\begin{gather}
\begin{aligned}
   & \text{lorem} + \text{ipsum} \\
&\begin{aligned}
 =( & \text{dolor}       &&+\text{sit}        &&+\text{amet} \\
    & \text{consectetur} &&-\text{adipiscing} &&-\text{elit})
\end{aligned} \\
&\begin{aligned}
 = (& \text{sed}     &&-\text{do} \\
    & \text{siusmod} &&+\text{tempor}).
\end{aligned}
\end{aligned}
\end{gather}

nested aligns including prefix and postfix

5. Idea for a solution

Ideally, I could use multiple sets of anchors. For example, &1 for the first column that left-aligns all lines, &2 for the two additional columns in the second equation, and &3 for the one additional column in the third equation:

\begin{gather}
\begin{multi_anchor_aligned}  % Does not exist.
   &1 \text{lorem} + \text{ipsum} \\
= (&1 \text{dolor}       &2&2+\text{sit}        &2&2+\text{amet} \\
   &1 \text{consectetur} &2&2-\text{adipiscing} &2&2-\text{elit}) \\
= (&1 \text{sed}     &3&3-\text{do} \\
   &1 \text{siusmod} &3&3+\text{tempor}).
\end{multi_anchor_aligned}
\end{gather}

I have two questions:

  • Is there a better way of achieving this using the existing functionality of amsmath or another common package? I've posted a similar question for a more specific case earlier but it does not generalize to the example here.
  • How can I go about implementing an environment that supports multiple anchor types as imagined above? I assume I would have to parse the & and \\ delimiters to iterate over the terms in between and place them into multiple arrays.
4
  • 1
    Except for the equation numbers, you could use an array. Commented Feb 14, 2020 at 21:19
  • If this is really about math, using all text in your example is misleading. Commented Feb 14, 2020 at 21:53
  • Agree, thanks for pointing it out. The words in my examples are simply placeholders for mathematical expressions.
    – danijar
    Commented Feb 14, 2020 at 21:54
  • @danijar with forest can be done --have a look at -- tex.stackexchange.com/a/528226/197451
    – js bibra
    Commented Feb 15, 2020 at 1:08

2 Answers 2

6

Is it something like this you're after? I used alignedat to have full control on the columns spacing, and the optional [t] for the placement of the opening parenthesis, the closing being added at the end of the last line:

\documentclass{article}
\usepackage{amsmath, amssymb}

\begin{document}

\begin{gather}
\begin{aligned}
   &\text{lorem} + \text{ipsum} \\
= (&\begin{alignedat}[t]{3}
    &\text{dolor} &&+\text{sit} &&+\text{amet} \\
    &\text{consectetur} &&-\text{adipiscing} &&-\text{elit})
\end{alignedat} \\
= (&\begin{alignedat}[t]{2}
    &\text{sed} &&-\text{do} \\
    &\text{siusmod} &&+\text{tempor}).
\end{alignedat}
\end{aligned}
\end{gather}

\end{document} 

enter image description here

3
  • Thanks, this seems to work well. I don't even need alignedat --- it works fine with aligned. Placing the prefixes outside together with the [t] option and placing the postfixes inside does the trick.
    – danijar
    Commented Feb 14, 2020 at 21:57
  • I know it works as well with aligned. I just found alignedat had a better spacing (but this depends on what you want to have as a final result).
    – Bernard
    Commented Feb 14, 2020 at 22:03
  • Thanks for the pointer --- you're right, the aligned introduces additional spacing that can be avoided with alignedat.
    – danijar
    Commented Feb 14, 2020 at 22:10
3

Here is an array solution, and a tabular* solution including equation numbers.

Note that \extracolsep does not apply before the first or after the last column.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}

\newcommand{\mytag}{\refstepcounter{equation}\llap{(\theequation)}}

\begin{document}

\begin{equation}
\begin{array}{rlll}
   & \text{lorem} + \text{ipsum} \\
 = (& \text{dolor}       &+\text{sit}        &+\text{amet} \\
   & \text{consectetur} &-\text{adipiscing} &-\text{elit}) \\
= (& \text{sed}     &-\text{do} \\
   & \text{siusmod} &+\text{tempor}).
\end{array}
\end{equation}

\noindent\begin{tabular*}{\textwidth}{@{}l% left margin, leave empty
  @{\extracolsep{\fill}}>{$\displaystyle}r<{$}
  @{\extracolsep{0pt}\hspace{2\tabcolsep}}>{$\displaystyle}l<{$}
  >{$\displaystyle}l<{$}
  >{$\displaystyle}l<{$}
  @{\extracolsep{\fill}}r@{}}
 &   & \text{lorem} + \text{ipsum} \\
 &= (& \text{dolor}       &+\text{sit}        &+\text{amet} \\
 &   & \text{consectetur} &-\text{adipiscing} &-\text{elit}) &\mytag \\
 &= (& \text{sed}     &-\text{do} \\
 &   & \text{siusmod} &+\text{tempor}). &&\mytag 
\end{tabular*}

\end{document}
2
  • Thank you, this looks interesting. I'm getting the following compilation error: Illegal character in array arg. Is there a problem with the curly braces in following \displaystyle?
    – danijar
    Commented Feb 22, 2020 at 20:37
  • \displaystyle is only allowed in math mode, so \hbox{\displaystyle} (for example) will give an error. Commented Feb 23, 2020 at 16:15

You must log in to answer this question.

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