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}
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}
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}
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.