357

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.

3
  • 5
    Read this article on TUGboat and be enlightened. Commented Oct 5, 2010 at 14:19
  • 5
    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
    Commented Aug 17, 2017 at 21:02
  • 1
    \begin{gather} ... \end{gather}
    – ahorn
    Commented Mar 30, 2019 at 12:23

10 Answers 10

222

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.

7
  • 6
    You should also mention split, that does in a cleaner way what I used to do with align. Commented Oct 6, 2010 at 1:57
  • 1
    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
    Commented Dec 4, 2012 at 15:51
  • 9
    The package unfortunately has huge incompatibility issues.
    – Troy Woo
    Commented Jan 30, 2015 at 8:33
  • 1
    breqn and \begin{dmath} do not work with sidewaystable or when equations in tabular environment for me, are you able to confirm that? How can you have automatic equation linebreaks in tabular environment? Moved the issue here.
    – hhh
    Commented Jan 31, 2016 at 21:27
  • 87
    Why is it called multline instead of multiline. I misspelt it at first :(
    – xji
    Commented Jun 13, 2018 at 21:48
183

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.

7
  • 3
    Why does multline give the equation number on the second line rather than in the middle like split does?
    – Siwel
    Commented Sep 15, 2016 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. Commented Feb 16, 2017 at 10:02
  • 40
    Tip: Look carefully: There is no "i" in "multline".
    – Mars
    Commented Aug 17, 2017 at 20:43
  • 3
    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
    Commented Aug 18, 2017 at 16:07
  • 2
    @Mars thank you so much for that. God forbid we waste a single character to make a command easy to remember! (Whose terrible idea was it to chop out a random character in the middle???)
    – mcmuffin6o
    Commented Dec 5, 2023 at 20:33
50

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}
2
  • 4
    Environment multiline undefined. \begin{multiline}
    – hola
    Commented Sep 24, 2018 at 6:56
  • 24
    @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. Commented Feb 8, 2019 at 14:36
22

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]

4
  • Could you share why? Is my english confusing? Commented Oct 6, 2010 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. Commented Oct 6, 2010 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. Commented Oct 6, 2010 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
    Commented May 10, 2011 at 19:34
16

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}
2
  • 14
    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
    Commented Sep 29, 2014 at 16:32
  • 3
    They saved on command name length! I used to work with someone who abbreviated things like "width", by writing "wdth" instead. Commented Nov 29, 2016 at 16:11
7

The easiest way to do this is with the split command

\begin{equation}
\begin{split}
\vec{A} \cdot \vec{B} & = (A_x \hat{x} + A_y \hat{y}+ A_z\hat{z}) \cdot (B_x \hat{x} + B_y \hat{y}+ B_z\hat{z}) \\
& = A_xB_x(\hat{x} \cdot \hat{x}) + A_yB_y(\hat{y} \cdot \hat{y}) + A_zB_z (\hat{z} \cdot \hat{z}) \\
&= A_xB_x + A_yB_y+A_zB_z
\end{split}
\end{equation}

This will yield a nicely formatted three line equation: enter image description here

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

1

I'm not an expert in LaTeX, but I get nice results using equation* in each line and equation in the last one. For example:

\begin{equation*}
    \Delta H_{f_{comb}} + \Delta H_{T_0 \rightarrow T_1}^{aire} + \Delta H_{T_0 \rightarrow T_1}^{comb} = \sum_i{\left[h_{f}^{comb_i} + \int_{T_0}^{T_1}{C_{p}^{comb_i}(T)\cdot dT} + \right.}
\end{equation*}
\begin{equation}
    + \left.\left(\int_{T_0}^{T_1}{\left[C_{p}^{O_{2}}(T)+C_{p}^{N_{2}}(T)\cdot\frac{78}{21}+C_{p}^{Ar}(T)\cdot\frac{1}{21}\right]\cdot dT}\right)\cdot \left(x_i + \frac{y_i}{4}\right)\cdot (1+p_{extra})\right]\cdot n_{comb_i}
\end{equation}

Which gives me the following

enter image description here

You can then align the equations as you like.

And also, if you have any \left or \right brackets or parenthesis you can just close them or open them using \left. or \right.

Maybe this is way too complicated and there may be easier workarounds, but it works for me.

1
  • 2
    One should never use two consecutive math display environments.
    – egreg
    Commented Aug 2, 2023 at 8:09
0

Kind of a hack solution (uses \hspace) from me which however, sticks to the amsmath alignat environment in order to maintain the alignment between the first and any further lines of the split equations. The first line (equation) in the attached image and the code is to indicate the alignment.

enter image description here

\begin{alignat}{1}
    \frac{d\tilde{x}}{dt} &= -(\tilde{x}+x_0)\{\tilde{x}^2 + (2x_0-2)\tilde{x} + (x_0^2-2x_0-\mu_0-\tilde{\mu})\} \nonumber \\      
    \frac{d\tilde{x}}{dt} &= -\{\tilde{x}^3 + (3x_0-2)\tilde{x}^2 + (3x_0^2-4x_0-\mu_0-\tilde{\mu})\tilde{x} \\
                          &{\hspace{12pt}} +(x_0^3 -2x_0^2 -x_0\mu_0 -x_0\tilde{\mu})\} \nonumber
\end{alignat}   
1
  • Welcome to tex.sx. However, the question asked for the first line of the broken equation to be shifted to the left, and the second line to the right. When multiple equations appear in the same display, the multlined environment can be used to set a broken one with left/right positioning (requires the mathtools package, which loads amsmath). Unfortunately, this doesn't automatically allow alignment of multiple equations; that can be done, but is more complicated. Commented May 2, 2022 at 2:22
-1
\documentclass[1pt]{article}

\begin{document}
\begin{tabular}{p{0.5cm}p{0.5cm}p{10cm}} 
K &=&  $\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)]$\\ 
&&$ +\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{tabular}
\end{document}

This does not need any packages. You could try. Please send me your response.

1
  • 4
    To much space around equal (=) sign. This is considered as bad typography.
    – Zarko
    Commented May 8, 2020 at 15:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .