Fix Overfull \hbox in Inline Math Without Line Breaks

inline()math-mode

To prevent line break within equation I use following LaTeX command:

${r^2 = x^2 + y^2}$.

Unfortunately it gives me Overfull \hbox (12.4459pt too wide) warning. Why LaTeX doesn't move whole equation to next line?

Best Answer

Using ${....}$ does indeed prevent a formula from being broken across lines but at the cost that you freeze up all spacing within the formula at its natural width. If you write $a=b+c$ then LaTeX generates the following list for you:

\mathon
\OML/cmm/m/it/10 a
\glue(\thickmuskip) 2.77771 plus 2.77771
\OT1/cmr/m/n/10 =
\penalty 500
\glue(\thickmuskip) 2.77771 plus 2.77771
\OML/cmm/m/it/10 b
\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
\OT1/cmr/m/n/10 +
\penalty 700
\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
\OML/cmm/m/it/10 c
\mathoff

As you can see this list has a number of "glue" items that can stretch (and possibly shrink) and it also shows two break points with associated penalties 500 and 700.

In contrast if you do ${a=b+c}$ you get

\hbox(0.0+0.0)x15.0
\mathon
\hbox(6.94444+0.83333)x39.46046
.\OML/cmm/m/it/10 a
.\glue(\thickmuskip) 2.77771 plus 2.77771
.\OT1/cmr/m/n/10 =
.\glue(\thickmuskip) 2.77771 plus 2.77771
.\OML/cmm/m/it/10 b
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OT1/cmr/m/n/10 +
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OML/cmm/m/it/10 c
\mathoff

which looks similar but really isn't. Now the break points are gone (TeX drops them inside hboxes) and the whole formula is in a box of a defined size. So even though the glue items are still around they do not help any more because the size of this hbox is frozen.

In short, if a paragraph line gets slightly stretched a normal formula would stretch too and the same when a line gets squeezed. But in the second case the formula would just stay frozen at its natural width.

I should add that the suggestion of using ${...}$ in formulas was originally given by Don Knuth and can be found in the TeX book. Nevertheless, from a typographic point of view it is a questionable advice (or worse) as it strongly break what TeX normally is attempting to achieve: a uniform and high quality appearance of textual material.

An alternative that doesn't have this defect is to set the two parameters that generate the break points to different values:

\newcommand\nomathlinebreaks{\relpenalty=10000
                             \binoppenalty=10000 } % the space in the def is deliberate

Used inside a formula this would prevent linebreaks inside the formula, used outside would prevent linebreak in following formulas. \allowbreak could be used to provide an explicit line break possibility inside a formula and \nobreak would prevent one, e.g., $a=b+\nobreak c$.

Now why made TeX an overfull line rather than moving the whole formula to the next line? That is just the standard way the paragraph breaking algorithm works. If it can't find a solution that satisfies its current settings of \tolerance etc. it will produce an overfull line rather than making the paragraph longer. The basic reason if that this is the only way to deal with the scenario where the offending object is bigger than the whole line. If that would be moved, the same problem would happen on the next line ... and you would end up with a paragraph consisting of endless lines being empty.

So the answer is: change the paragraph parameters to allow for more flexibility if that is desired. A possible candiate to look at is \emergencystretch, but also allowing a slightly higher value for \tolerance often improves the overall results without producing really bad effects. A word of warning though: the paragraph algorithm is very complex and has a lot of bells and wistles and starting to change its parameter setting should be done with care and only after studying the algorithm in some detail.

Related Question