[Tex/LaTex] Why does TeX prefer an overfull box to hyphenating here

hyphenationline-breaking

Here is a short file which produces an overfull hbox:

\documentclass[12pt]{report}
\usepackage[left=1.5in,right=1in,paperwidth=8.5in,paperheight=11in]{geometry}
\begin{document}
\trivlist
\item[\hskip\labelsep{\bf2.3.12 Corollary [Isomorphisms are indexed bijections]:}]
Consider the functions $f$ and $g$
\endtrivlist
\end{document}

If I replace functions with func-\linebreak tions, the word is split across two lines, resulting in neither an overfull nor an underfull hbox. However, neither func\-tions nor func-tions produces this behavior. Why not? Is there a better way to convince TeX that it's okay to hyphenate this word to prevent an overfull box?

Best Answer

It's important to remember that TeX doesn't build a page line by line, but paragraph by paragraph. This is a major difference from the behavior of most word processing programs.

TeX applies various rules -- resulting in penalties or demerits if they're not satisfied -- to come up with an "overall best" appearance of a paragraph. TeX assigns penalties not only to overfull lines but also to irregular inter-word spaces. Specifically, TeX will try very hard to produce roughly the same amount of interword space across all lines of a paragraph, and it tries to keep the amount of interword space fairly "tight" by default. TeX does so by assigning substantial penalties if the amount of interword space varies noticeably across lines within one and the same paragraph and also if there's too much whitespace between words.

In the example you've provided, the unhyphenated word "functions" happens to protrude by "only" two characters ("ns") into the right-hand margin. In TeX's view, this outcome appears to be preferable to hyphenating the word "functions", as hyphenating the word would also generate two very large interword spaces -- between "Consider" and "the" as well as between "the" and "functions. (TeX isn't allowed to adjust any of the interword spaces between the words that are in the argument of \item, because that "box" has already been closed and is no longer modifiable by TeX's paragraph building routine. As a result, there are only two interword spaces in the first line that TeX is allowed to modify while it's building the paragraph.)

To override TeX's default settings related to building paragraphs, one could issue the command \sloppy. Sure enough, as is shown in the code and screenshot below, if one inserts the instruction \sloppy before \item, TeX has no problem hyphenating the word "functions"; the cost is the creation of two rather large interword gaps in the first line of the paragraph. (If you don't want to go quite as far as \sloppy, you might try typing \tolerance=1000. The default value of \tolerance is 200. See pp. 29f. of the TeXbook for more information on this parameter.)

A different, and in my view much better, solution starts by recognizing that the real cause of the overfull-line issue is that TeX is initially being given only two interword gaps to adjust. Limiting the scope of \item and placing \textbf{[Isomorphisms are indexed bijections]:} after \item[\hskip\labelsep{\bfseries 2.3.12 Corollary}] creates many more ``degrees of freedom'' for TeX's paragraph-building algorithm. This immediately leads to a much more satisfying solution, as is also shown in the screenshot below. (The black frame lines are inserted courtesy of the showframe option of the geometry package.)

Finally, yet another solution, which may or may not be to your liking, is to forgo full justification: load the package ragged2e and issue the command \RaggedRight to allow hyphenation. Without full justification, you're virtually assured that there will be no overfull lines.

In summary, the problem you describe arises because (i) TeX has only 2 interword spaces to play with on the first line and (ii) TeX generally assigns fairly high penalties to irregular and large values of interword spaces. In the case of your example, these penalties appear to outweigh the penalty created by letting a word protrude slightly beyond the right-hand edge of the text block. To solve the issue, you could either use \sloppy or simply give TeX's paragraph-building algorithm more degrees of freedom (by limiting the scope of the \item directive).

enter image description here

\documentclass[12pt]{report}
\usepackage[left=1.5in,right=1in,
  paperwidth=8.5in,paperheight=11in,
  showframe,nomarginpar]{geometry}
\setlength\parindent{0pt} % just for this example
\begin{document}

Initial (``tight'') setting---overfull line.
\trivlist
\item[\hskip\labelsep{\bfseries 2.3.12 Corollary 
  [Isomorphisms are indexed bijections]:}]
Consider the functions $f$ and $g$
\endtrivlist

\medskip
\begingroup
Solution 1: Use \verb+\sloppy+ directive---not great.
\sloppy
\trivlist
\item[\hskip\labelsep{\bfseries 2.3.12 Corollary   
  [Isomorphisms are indexed bijections]:}]
Consider the functions $f$ and $g$
\endtrivlist
\endgroup

\medskip
Solution 2: Limit extent of argument of \verb+\item+---best.
\trivlist
\item[\hskip\labelsep{\bfseries 2.3.12 Corollary}] 
  \textbf{[Isomorphisms are indexed bijections]:}
Consider the functions $f$ and $g$
\endtrivlist

\end{document}
Related Question