# How can I split an equation over two lines

I am having the following equation:

$$Q(\lambda,\hat{\lambda}) = -\frac{1}{2} P(O \mid \lambda ) \sum_s \sum_m \sum_t \gamma_m^{(s)} (t) \left( n \log(2 \pi ) + \log \left| C_m^{(s)} \right| + \left( \mathbf{o}_t - \hat{\mu}_m^{(s)} \right) ^T C_m^{(s)-1} \left(\mathbf{o}_t - \hat{\mu}_m^{(s)}\right) \right)$$


which does not very well fit on one line. How can I split this over two lines? What I have in mind is that I specify the splitting place, and that the first line is left aligned and the second line right aligned to make clear that it is still the same equation.

The linebreak \\ does not work.

-

Use either breqn to break lines automatically or use amsmath and its many environments exactly for this purpose. For example, with breqn:

\documentclass{article}
\usepackage{breqn}
\begin{document}
\begin{dmath}
Q(\lambda,\hat{\lambda}) = -\frac{1}{2} P{(O \mid \lambda )} \sum_s \sum_m \sum_t \gamma_m^{(s)} (t) \left( n \log(2 \pi ) + \log \left| C_m^{(s)} \right| + \left( \mathbf{o}_t - \hat{\mu}_m^{(s)} \right) ^T C_m^{(s)-1} \left(\mathbf{o}_t - \hat{\mu}_m^{(s)}\right) \right)
\end{dmath}
\end{document}


Note, the expression around \mid required braces to prevent it from breaking at this point; I'm sure there is a better way to do that; anyway, here's the output:

With amsmath, you need to specify the break points manually: (as others have also mentioned)

\usepackage{amsmath}
...
\begin{multline}
A+B+C+ \\ +D+E+F
\end{multline}


The users guide to amsmath is called amsldoc.pdf, but you can access it by typing texdoc amsmath on the command line. The main environments you'll use there would be align, split, and multline.

-
You should also mention split, that does in a cleaner way what I used to do with align. –  Mateus Araújo Oct 6 '10 at 1:57
I noticed that breqn does not align the second part(broken part) of the equation to the right by itself. This can be noticed when you have a short first term in which case it is center aligned. Is there a way to align it to the right explicitly? –  Shafi Dec 4 '12 at 15:51
Note that the environment is multline not multiline. I spent about an hour trying to figure out what was wrong. –  user545424 Mar 26 at 2:34

First line left, last line right—that is the multline environment:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{multline}
Q(\lambda,\hat{\lambda}) = -\frac{1}{2} P(O \mid \lambda ) \sum_s \sum_m \sum_t \gamma_m^{(s)} (t) \biggl( n \log(2 \pi ) \\
+ \log \left| C_m^{(s)} \right| + \left( \mathbf{o}_t - \hat{\mu}_m^{(s)} \right) ^T C_m^{(s)-1} \left(\mathbf{o}_t - \hat{\mu}_m^{(s)}\right) \biggr)
\end{multline}
\end{document}

-

I often have the same problem, but opt for left-aligning on subsequent lines. In any case, I would suggest using the amsmath align environment. If I wanted right-aligning, here's what I would try (with white space liberally applied):

\begin{align}
Q(\lambda,\hat{\lambda})
= -\frac{1}{2} P(O \mid \lambda )
\sum_s \sum_m \sum_t \gamma_m^{(s)} (t)
\Biggl( n\log(2\pi)
\mspace{150mu}
\notag\\
+ \log \left| C_m^{(s)} \right|
+ \left( \mathbf{o}_t - \hat{\mu}_m^{(s)} \right)^T C_m^{(s)-1}
\left(\mathbf{o}_t - \hat{\mu}_m^{(s)}\right)
\Biggr)
\end{align}


Remarks:

• I replaced the outermost parentheses with large fixed-size parentheses, \Biggl( and \Biggr); scale these according to your taste.
• The \mspace command adds horizontal space in math mode, on the first line. Thus, the first line is not so much left-aligned, as it is right-aligned with a fixed amount of white-space added at the end. Vary this according to taste as well.
• You can split your equation across several lines by employing \notag\\ several times where desired.
• If you would prefer the equation number to straddle the two lines, as opposed to being placed on the last line, nest the mathematics inside a split environment (and omit the \notag commands); this would otherwise work verbatim.

[Edited to stand alone as a response]

-
Could you share why? Is my english confusing? –  Mateus Araújo Oct 6 '10 at 1:56
@Mateus: Your English seemed fine to me, sorry if it seemed as though I were critiquing it! I meant the LaTeX code itself, which I had difficulty reading. From scanning it, it seemed to be mostly-different from what I was recommending; but as I was not totally certain, I was leaving open the possibility that I was posting essentially a duplicate answer. — I note that your answer is no longer visible: I hope you did not delete your response on my account. –  Niel de Beaudrap Oct 6 '10 at 6:48
The LaTeX code was similar, but not equal. For starters I used the alignment characters & and you didn't. But I deleted because it was a bad answer; with the split environment from amsmath you can manually specify the line breaks and positions, without having wrong numbering. Robertson's answer is way better. –  Mateus Araújo Oct 6 '10 at 14:29
If you indent lines by 4 spaces (as I did in my edit) instead of using <pre> and </pre>, then they are marked as a LaTeX code sample, which means they also get the nice syntax coloring. You can also highlight the code and click the "code" button (with "101010" on it). –  doncherry May 10 '11 at 19:34

For future reference, when trying to remember the name for the multiline environment, which is very handy and does this automatically (or about as close to it as Latex can go) - just remember, it's multline:

\begin{multline}
I want my awesome formula to split lines here \\
so this next part is aligned to the right in the following line, looking smart.
\end{multline}

-