220

I am having the following equation:

\begin{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)
\end{equation}

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.

  • 4
    Read this article on TUGboat and be enlightened. – Willie Wong Oct 5 '10 at 14:19
  • 2
    For those who come to this question looking for a way to center each line of a multi-line equation, see this answer to another question--or one of the other answers to that question. – Mars Aug 17 '17 at 21:02
  • \begin{gather} ... \end{gather} – ahorn Mar 30 at 12:23
153

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:

rendered

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.

  • 3
    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
  • 4
    The package unfortunately has huge incompatibility issues. – Troy Woo Jan 30 '15 at 8:33
  • @MateusAraújo What is cleaner about split? – Travis Bemrose Oct 30 '15 at 10:35
  • 11
    Why is it called multline instead of multiline. I misspelt it at first :( – xji Jun 13 '18 at 21:48
84

You can use multline or split provided by amsmath package.

  • Use multline to split equations without alignment (first line left, last line right)
  • Use split to split equations with alignment

Here are examples:

enter image description here

The corresponding source code is as follows:

(i).Use equation:
\begin{equation}
1+2+3+4+8x+7=1+2+3+4+4x+35 \\
\Rightarrow x=7
\end{equation}

(ii).Use \emph{multline} to split equations without alignment:
\begin{multline}
1+2+3+4+8x+7=1+2+3+4+4x+35 \\
\Rightarrow x=7
\end{multline}

(iii).Use \emph{split} to split equations with alignment
\begin{equation}
\begin{split}
1+2+3+4+8x+7 & =1+2+3+4+4x+35 \\
& \Rightarrow x=7
\end{split}
\end{equation}

For more info, you can refer to User’s Guide for the amsmath Package.

  • 1
    Why does multline give the equation number on the second line rather than in the middle like split does? – Siwel Sep 15 '16 at 13:58
  • For multline the tag placement is fixed: first line if leqn, last line if reqno. For split the placement is customizable. By default, the centertags option, tags are centered vertically. The tbtags option makes the placement the same as multline. – Andrew Swann Feb 16 '17 at 10:02
  • 7
    Tip: Look carefully: There is no "i" in "multline". – Mars Aug 17 '17 at 20:43
  • 1
    I think that was at least the second time that I found this answer (which I'd forgotten) and was puzzled when I got an error from "multiline". – Mars Aug 18 '17 at 16:07
  • But without '&' split is similar to multi-line. – sreeraj t Apr 1 at 1:10
40

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}
  • 1
    Environment multiline undefined. \begin{multiline} – pushpen.paul Sep 24 '18 at 6:56
  • 2
    @pushpen.paul There's only one i in multline. This seems to be one of the most awful choices of naming in all of latex, given how many people instinctively want to put that second i in there. – zibadawa timmy Feb 8 at 14:36
19

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
12

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}
  • 10
    Haha I think I added this answer just for myself, since I kept forgetting the name of the multline environment, since it's so stupid to not just call it multiline. – gakera Sep 29 '14 at 16:32
  • They saved on command name length! I used to work with someone who abbreviated things like "width", by writing "wdth" instead. – drozzy Nov 29 '16 at 16:11
1

One thing while working with multline if you are using \displaystyle is that you have to type it on each line otherwise you'll get an error

\begin{multline*}  
K=\displaystyle{\frac{1}{2}m_1 L_1^2 \dot{\theta_1}^2+\frac{1}{2} m_2[L_1^2 \dot{\theta_1}^2+L_2^2 \dot{\theta_2}^2+2 L_1 L_2 \dot{\theta_1}\dot{\theta_2}\cos(\theta_1-\theta_2)]} \\
 \displaystyle{+\frac{1}{2}m_3[L_1^2 \dot{\theta_1}^2+L_2^2 \dot{\theta_2}^2+L_3^2+ \dot{\theta_3}^2+2 L_1 L_2 \dot{\theta_1}\dot{\theta_2}\cos(\theta_1-\theta_2)}
\end{multline*}

Notice the \displaystyle for each new line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.