# Sums and differences spanning multiple lines

I'm trying to achieve a formatting similar to this:

So essentially I've got two points of alignment here, one between right end of = and left end of + resp. -, the other between right end of + resp. - and the actual terms. I've used monomials as terms here as an example, but in my actual application there might be arbitrary expressions in each of the terms. The example above I did typeset like this:

\begin{align*}
A =& \mathbin{\phantom+} 17\,a^2\,b^{16}\,c^5 \\
&+ 3\,a^2\,c^4\,d \\
&- a\,b^6 \\
B =& \mathbin{\phantom+} a^2\,b^6 \\
&- 5\,a\,b^7
\end{align*}

The \mathbin was vital to get the spacing in the first row match that of the latter rows. One thing I like about this is that the implicit {} after the alignment makes my + and - use spacing appropriate for binary use, instead of unary as you'd see at the beginning of a line. Nevertheless, the way I wrote this doesn't satisfy all my expectations.

1. Conventionally one places the alignment point before the equals sign, i.e. writes &= instead of =&. Looking at Spacing after equals sign in align for details, I think that with my approach above, the spacing between the = and the + will be that around a binary operator, not that around a relation. It's not as if I could tell just by looking at the result, though, particularly as they are not on the same line, so perhaps I need not worry.

2. If I were to add equation numbering, then each row of this equation would be numbered independently. I think it would be cleaner to write something like A &= \begin{…}…\end{…} but I don't know of a suitable environment here. {gathered} will center its content, both vertically and horizontally, while I would need it flushed left and aligned with the first line.

3. In my context it would be even better if I could use this for inline math as well, in a way that aligns with the baseline of the first line. That's because if possible I'd like to place this formula inside a {description} environment, so I'd want the description label to line up with the first line of the formula. Might try a minipage if that doesn't work out another way, though.

Can you think of a way to improve one or more of the points mentioned above?

here are a couple of possibilities using alignat, with split added in to allow numbering.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{alignat*}{2}
A ={}&&& 17\,a^2\,b^{16}\,c^5 \\
&+{}&& 3\,a^2\,c^4\,d \\
&-{}&& a\,b^6 \\
B ={}&&& a^2\,b^6 \\
&-{}&& 5\,a\,b^7
\end{alignat*}
\begin{alignat}{2}
\begin{split}
A = \phantom{{}+{}}& 17\,a^2\,b^{16}\,c^5 \\
{}+{}& 3\,a^2\,c^4\,d \\
{}-{}& a\,b^6
\end{split} \\
\begin{split}
B = \phantom{{}+{}}& a^2\,b^6 \\
{}-{}& 5\,a\,b^7
\end{split}
\end{alignat}
\end{document}

edit: the second example would more simply be entered with align rather than alignat. as @egreg explains in a comment, the single alignment point in the split is also taken as the alignment point for the align.

• I must confess that I don't understand how the alignment between the equations works in that second example. The way I understand it, all your & there refer to the split, so how dows alignat know where to align? – MvG Dec 7 '16 at 13:50
• hmmm. as i was working it out, i was just modifying a copy of the first example. you are on the right track -- alignat isn't necessary; align will do just as well. but neither equation nor gather would work, equation because more than one "line" is involved, and gather because the widths of the chunks are different. – barbara beeton Dec 7 '16 at 14:13
• @MvG split knows whether it is called in align or alignat, so it sets the alignment point to the overall one. – egreg Dec 7 '16 at 14:48
• Your answer made me look at the difference between split and aligned, and egreg's answer there made me aware that I could write \begin{aligned}[t] to get top alignment, to cater for my use case in the definition list. Even found that option documented in amsldoc now. I haven't found docs for the alignment of split to its surroundings that @egreg described. Very useful to know, all of this! – MvG Dec 7 '16 at 15:04