How can I prevent inline math formulas from overflowing into the margin?

When I compile my document in LaTeX, some formulas in the document in math mode, between , go into the right margin area beyond all the text. Is there a way to fix this aesthetic problem?

If your formular is not too long for a line in the first place, you can use

\sloppy


in the begining of your paragraph:

The lineshape of the imaginary part of the susceptibility
(e.g. $\mathrm{\mathfrak{Im}}\left(\left.\chi_{\phi\phi}\left(\omega\right)\right|_{B=B_{0}}\right)$
or $\mathfrak{Im}\left(\left.\chi_{\phi\phi}\left(B\right)\right|_{\omega=\omega_{0}}\right)$)
for sufficiently small damping and a symmetric excitation measurement
projection ...


Produces

Whereas

\sloppy The lineshape of the imaginary part of the susceptibility
(e.g. $\mathrm{\mathfrak{Im}}\left(\left.\chi_{\phi\phi}\left(\omega\right)\right|_{B=B_{0}}\right)$
or $\mathfrak{Im}\left(\left.\chi_{\phi\phi}\left(B\right)\right|_{\omega=\omega_{0}}\right)$)
for sufficiently small damping and a symmetric excitation measurement
projection ...


Produces

If your formula is too long to fit in a line there are several ways of introducing \newlines within formulas. For instance Allowing line break at ',' in inline math mode?

• I had a lot of \left and \right stuff in my equations (the advantage being that it re-sizes the height of the delimiter based on what's inside), and \alowbreak won't work inside those. This option seems to be a great compromise. Commented Oct 2, 2016 at 21:49

LaTeX can break lines within inline formulas, however it does it only at certain places. You could insert \allowbreak at places were it should be allowed as well.

If you would like to allow line breaks within inline math formulas at certain symbols, such as commas for example, here's a solution: Allowing line break at ',' in inline math mode.

Generally, when I notice a line breaking problem with inline math because it's long, I would consider making a displayed formula of it, using $...$.

• For convenience I sometimes use \let\ab\allowbreak so I can use the shorter \ab where I need it. For my taste \allowbreak is too long for what it does and distracts from other parts in a formula. Otherwise, I second Stefan's recommendation to use $...$. Commented Sep 18, 2011 at 20:11

You have three options, two thereof have been mentioned already:

1. Allow more line breaks within your equation.

2. Typeset the equation on a separate line, i.e. don't use the math environment, but equation, displaymath or variants thereof from extra packages such as amsmath. If necessary, force or allow line breaks.

3. The problem, you describe is not directly related to formulas. LaTeX typesets text (and inline math) according to a set of rules. LaTeX inserts whitespace between words (or parts of the formula in your case, generally blocks) to obtain a justified text layout. There's an upper bound on how much whitespace can be used. Your formula is a large block. It cannot be split on two lines, because line breaks are not permitted. However, it cannot be moved to the next line either, because that would cause too much white space on the previous line. In LaTeX language, an "overfull hbox" has occurred and you are supposed to fix it using one of the techniques mentioned above. However, you can also tell LaTeX to be less restrictive with the whitespace using the command \sloppy anywhere before the paragraph in question.

If you are fine with equations being on lines of their own and you want to automate line breaking in a semi-sane way, then check out the breqn package. It provides for example the dmath environment which is fairly clever when it comes to automatically splitting an equation across lines.

For example:

\begin{dmath}
a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a
\end{dmath}


will be split across lines with nice indenting from the second line onwards. Beware that the breqn package tends to break other packages, so read the documentation.

• That only applies to display math. Commented Oct 3, 2017 at 22:26

As you've noticed (to your chagrin!), TeX (and LaTeX) won't break an inline math expression just anywhere. Favored breakpoints are equal signs and binary relation signs (such as pluses and minuses). You'll have to look at your code and decide if you could (i) break up some longer formulas into smaller parts and/or (ii) set them in display-math format on a separate line.

The latter method may be especially beneficial if the formulas in question are quite long, say, longer than about one third of the line length. To place a formula on a separate line, you have several options. The oldest method, noted by Knuth in the TeXbook, is to enclose the formula in question in double-dollar ($$) signs. However, this method is rightfully deprecated by now and there's no longer any justification for using it; see this question for a listing of all of its failings. Instead of $$, one should use (a) the $ and $ control words (LaTeX's default mechanism) or (b) the commands \begin{equation*} and \end{equation*}; you'll need to load the amsmath package to use the latter method. Both methods will generate an un-numbered equation (or equation fragment -- no need for there to be an equals sign anywhere in the formula) on a line by itself.

The reason I like option (b) is that it very clearly draws attention to itself, making it much less likely (at least for me!) that the formula in question ends up in display mode by accident. In addition, should you decide you want to change the equation from an unnumbered to a numbered form, it's trivially easy to just remove the asterisks from the equation* strings; the opposite (going from a numbered to an un-numbered equation) is equally possible.

• $$...$$ should completely be avoided!
– user2478
Commented Sep 18, 2011 at 17:34
• $$...$$ gives a wrong vertical spacing
– user2478
Commented Sep 18, 2011 at 18:00
• @Mico It's not "fallen out of favor": the fact is that in LaTeX it should not be used (but LaTeX uses it internally, of course, and the TeXbook deals with Plain TeX which is not LaTeX) by very good reasons; there is at least one thread here on TeX.SX that discusses why. Please, delete it from your answer. Commented Sep 18, 2011 at 19:38