# How to prevent aligned equations to shift when adding a non-aligned line break?

I have an equation in the aligned environment to align the equal signs together. However, the last equation is too long, and I want to break it up. When I insert a linebreak, all the equations above the last one shift to the right. The code:

\begin{aligned} \log\left(p\left(s|\alpha\right)\right) & = \sum\limits_{i=1}^O\left(\log\left(\frac{1}{\prod\limits_{k=1}^K\Gamma\left(\hat{\mathcal{S}}_k^i+1\right)}\right) + \log\left(\frac{\Gamma\left(\alpha_0\right)}{\Gamma\left(\sum\limits_{k=1}^K\left(\hat{\mathcal{S}}_k^i+a_k\right)\right)}\right) + \log\left(\prod\limits_{k=1}^K \frac{\Gamma\left(\hat{\mathcal{S}}_k^i+\alpha_k\right)}{\Gamma\left(\alpha_k\right)}\right)\right) \\ &= \sum\limits_{i=1}^O\left(-\log\left(\prod\limits_{k=1}^K\Gamma\left(\hat{\mathcal{S}}_k^i+1\right)\right) + \log\left(\frac{\Gamma\left(\alpha_0\right)}{\Gamma\left(\sum\limits_{k=1}^K\left(\hat{\mathcal{S}}_k^i+a_k\right)\right)}\right) + \log\left(\prod\limits_{k=1}^K \frac{\Gamma\left(\hat{\mathcal{S}}_k^i+\alpha_k\right)}{\Gamma\left(\alpha_k\right)}\right)\right) \\ &= \sum\limits_{i=1}^O\left(\sum\limits_{k=1}^K\left(-\log\left(\Gamma\left(\hat{\mathcal{S}}_k^i+1\right)\right)\right) + \log\left(\frac{\Gamma\left(\alpha_0\right)}{\Gamma\left(\sum\limits_{k=1}^K\left(\hat{\mathcal{S}}_k^i+a_k\right)\right)}\right) + \log\left(\prod\limits_{k=1}^K \frac{\Gamma\left(\hat{\mathcal{S}}_k^i+\alpha_k\right)}{\Gamma\left(\alpha_k\right)}\right)\right) \\ &= \sum\limits_{i=1}^O\left(\sum\limits_{k=1}^K\left(-\log\left(\Gamma\left(\hat{\mathcal{S}}_k^i+1\right)\right)\right) + \log\left(\Gamma\left(\alpha_0\right)\right) -\log\left(\Gamma\left(\sum\limits_{k=1}^K\left(\hat{\mathcal{S}}_k^i+a_k\right)\right)\right) + \log\left(\prod\limits_{k=1}^K \frac{\Gamma\left(\hat{\mathcal{S}}_k^i+\alpha_k\right)}{\Gamma\left(\alpha_k\right)}\right)\right) \end{aligned}


What I want

\begin{aligned} \log\left(p\left(s|\alpha\right)\right) & = \sum\limits_{i=1}^O\left(\log\left(\frac{1}{\prod\limits_{k=1}^K\Gamma\left(\hat{\mathcal{S}}_k^i+1\right)}\right) + \log\left(\frac{\Gamma\left(\alpha_0\right)}{\Gamma\left(\sum\limits_{k=1}^K\left(\hat{\mathcal{S}}_k^i+a_k\right)\right)}\right) + \log\left(\prod\limits_{k=1}^K \frac{\Gamma\left(\hat{\mathcal{S}}_k^i+\alpha_k\right)}{\Gamma\left(\alpha_k\right)}\right)\right) \\ &= \sum\limits_{i=1}^O\left(-\log\left(\prod\limits_{k=1}^K\Gamma\left(\hat{\mathcal{S}}_k^i+1\right)\right) + \log\left(\frac{\Gamma\left(\alpha_0\right)}{\Gamma\left(\sum\limits_{k=1}^K\left(\hat{\mathcal{S}}_k^i+a_k\right)\right)}\right) + \log\left(\prod\limits_{k=1}^K \frac{\Gamma\left(\hat{\mathcal{S}}_k^i+\alpha_k\right)}{\Gamma\left(\alpha_k\right)}\right)\right) \\ &= \sum\limits_{i=1}^O\left(\sum\limits_{k=1}^K\left(-\log\left(\Gamma\left(\hat{\mathcal{S}}_k^i+1\right)\right)\right) + \log\left(\frac{\Gamma\left(\alpha_0\right)}{\Gamma\left(\sum\limits_{k=1}^K\left(\hat{\mathcal{S}}_k^i+a_k\right)\right)}\right) + \log\left(\prod\limits_{k=1}^K \frac{\Gamma\left(\hat{\mathcal{S}}_k^i+\alpha_k\right)}{\Gamma\left(\alpha_k\right)}\right)\right) \\ &= \sum\limits_{i=1}^O\left(\sum\limits_{k=1}^K\left(-\log\left(\Gamma\left(\hat{\mathcal{S}}_k^i+1\right)\right)\right) + \log\left(\Gamma\left(\alpha_0\right)\right) -\log\left(\Gamma\left(\sum\limits_{k=1}^K\left(\hat{\mathcal{S}}_k^i+a_k\right)\right)\right) \\ \qquad\qquad + \log\left(\prod\limits_{k=1}^K \frac{\Gamma\left(\hat{\mathcal{S}}_k^i+\alpha_k\right)}{\Gamma\left(\alpha_k\right)}\right)\right) \end{aligned}


I don't want to use &+ because then it gets aligned with the equal signs, and I want it indented a bit further.

&\qquad\qquad

But remember that a \left...\right cannot be broken, thus to have to rewrite the last to lines to say

  &=
\sum\limits_{i=1}^O\Bigl(\sum\limits_{k=1}^K\left(-\log\left(\Gamma  \left(\hat{\mathcal{S}}_k^i+1\right)\right)\right)
+ \log\left(\Gamma\left(\alpha_0\right)\right)
-\log\left(\Gamma\left(\sum\limits_{k=1}^K  \left(\hat{\mathcal{S}}_k^i+a_k\right)\right)\right)
\\
\frac{\Gamma\left(\hat{\mathcal{S}}_k^i+\alpha_k\right)}{\Gamma   \left(\alpha_k\right)}\right)\Bigr)


With some more appropriate sizes.

Besides, are those massive ()'s really needed. To me they do not help much when ready the formula, instead they really annoy.

Addition, I would even go as far as (using mathtools) the code below. There is no need for all those autoscalling it ends up looking horrible. Also no need for those \limits.

\begin{aligned} \MoveEqLeft \log\bigl(p(s|\alpha)\bigr) \\ & = \sum\limits_{i=1}^O\left( \log\Bigl( \frac{1}{\prod_{k=1}^K \Gamma(\hat{\mathcal{S}}_k^i+1)} \Bigr) + \log\Bigl( \frac{ \Gamma(\alpha_0) }{ \Gamma\bigl( \sum_{k=1}^K(\hat{\mathcal{S}}_k^i+a_k) \bigr) } \Bigr) + \log\Bigl( \prod_{k=1}^K \frac{ \Gamma(\hat{\mathcal{S}}_k^i+\alpha_k) }{ \Gamma(\alpha_k) } \Bigr) \right) \\ &= \sum\limits_{i=1}^O \left( -\log\Bigl( \prod_{k=1}^K \Gamma(\hat{\mathcal{S}}_k^i+1) \Bigr) + \log\Bigl( \frac{ \Gamma(\alpha_0) }{ \Gamma\bigl( \sum_{k=1}^K (\hat{\mathcal{S}}_k^i+a_k) \bigr) } \Bigr) + \log\Bigl( \prod_{k=1}^K \frac{ \Gamma(\hat{\mathcal{S}}_k^i+\alpha_k) }{ \Gamma(\alpha_k) } \Bigr) \right) \\ &= \sum_{i=1}^O \left( \sum_{k=1}^K \Bigl( -\log \bigl(\Gamma(\hat{\mathcal{S}}_k^i+1) \bigr) \Bigr) + \log\Bigl( \frac{ \Gamma(\alpha_0) }{ \Gamma\bigl( \sum_{k=1}^K (\hat{\mathcal{S}}_k^i+a_k) \bigr) } \Bigr) + \log\Bigl( \prod_{k=1}^K \frac{ \Gamma(\hat{\mathcal{S}}_k^i+\alpha_k) }{ \Gamma(\alpha_k) } \Bigr) \right) \\ &= \sum_{i=1}^O\Biggl( \sum_{k=1}^K \Bigl( -\log\bigl( \Gamma(\hat{\mathcal{S}}_k^i+1) \bigr) \Bigr) + \log\left( \Gamma(\alpha_0 ) \right) -\log\biggl( \Gamma\Bigl( \sum_{k=1}^K (\hat{\mathcal{S}}_k^i+a_k) \Bigr ) \biggr) \\ &\qquad\qquad + \log\Bigl( \prod_{k=1}^K \frac{ \Gamma(\hat{\mathcal{S}}_k^i+\alpha_k) }{ \Gamma(\alpha_k) } \Bigr) \Biggr) \end{aligned}


Or even better, don't write it all out. It does not help the reader. Define helpers:

So ease notation, we define:
\begin{align*}
A &= \prod_{k=1}^K  \Gamma(\hat{\mathcal{S}}_k^i+1) \\
B &= \sum_{k=1}^K(\hat{\mathcal{S}}_k^i+a_k)\\
C &= \prod_{k=1}^K  \frac{ \Gamma(\hat{\mathcal{S}}_k^i+\alpha_k)}{ \Gamma(\alpha_k) }
\end{align*}

\begin{aligned} \MoveEqLeft \log\bigl(p(s|\alpha)\bigr) \\ & = \sum\limits_{i=1}^O\left( \log\Bigl( \frac{1}{A} \Bigr) + \log\Bigl( \frac{ \Gamma(\alpha_0) }{ \Gamma\bigl( B \bigr) } \Bigr) + \log(C) \right) \\ &= \sum\limits_{i=1}^O \left( -\log( A ) + \log\Bigl( \frac{ \Gamma(\alpha_0) }{ \Gamma( B ) } \Bigr) + \log( C ) \right) \\ &= \sum_{i=1}^O \left( \sum_{k=1}^K \Bigl( -\log \bigl(\Gamma(\hat{\mathcal{S}}_k^i+1) \bigr) \Bigr) + \log\Bigl( \frac{ \Gamma(\alpha_0) }{ \Gamma( B ) } \Bigr) + \log( C ) \right) \\ &= \sum_{i=1}^O\biggl( \sum_{k=1}^K \Bigl( -\log\bigl( \Gamma(\hat{\mathcal{S}}_k^i+1) \bigr) \Bigr) + \log\left( \Gamma(\alpha_0 ) \right) -\log\bigl( \Gamma( B ) \bigr) + \log( C ) \biggr) \end{aligned}


• Thanks a lot for the extra edit, that does look much better.
– Niek
Oct 8, 2013 at 10:32