5

The TeXbook describes (in chapter 14) in detail how TeX calculates the total demerits associated with breaking a paragraph into lines and that TeX chooses the sequence of breakpoints that yield the fewest total demerits (in a process with three passes). However, it only briefly describes what exactly happens if no sequence of breakpoints exists that satisfies the requirements:

Roughly speaking, \TeX\ breaks paragraphs into lines in the following way: Breakpoints are inserted between words or after hyphens so as to produce lines whose badnesses do not exceed the current ^|\tolerance|. If there's no way to insert such breakpoints, an ^{overfull box} is set.

No further details on the overfull box scenario follow. I am interested in these details: Can one describe in simple terms which words TeX chooses to stick out into the margin in such a case? (Or do you have to read the TeX source code to understand that?) Since TeX considers a paragraph as a whole, the issue of not having a permissible sequence of breakpoints is not really caused by a single word.

Furthermore, I think that the above statement from the TeXbook is not entirely correct: When a paragraph contains a \linebreak, it may be set with underfull lines instead. This scenario basically satisfies the premise "there's no way to insert such breakpoints", yet TeX sets underfull lines instead of overfull lines as stated above.

(This question is mainly out of interest. But there is indeed a scenario where a paragraph with overfull lines may end up in a finished document, namely if the excess size of the overfull lines is not more than \hfuzz.)

1
  • @DavidCarlisle Well, \penalty-10000 adds a penalty, but \tolerance sets a limit for the badness which the lines of the paragraph must not exceed. When you e. g. place a \linebreak early in the first line of a paragraph, the spaces in the first line need to be stretched a lot, so this line gets a high badness exceeding \tolerance.
    – user227621
    Oct 11, 2022 at 12:27

1 Answer 1

5

TeX chooses line breaks based on a calculation of line demerits that are totaled in an amount called total demerits.

A first pass that doesn't apply the hyphenation capability of TeX might fail to obey the limits that are set by an integer parameter \pretolerance. But the second pass of TeX always succeed. It might not be successful in the sense that TeX has to create overfull lines. Underfull lines are only created if the user wants them.

What I mean. You can set \parfillskip=0pt and give TeX a short line with, say, just one space and TeX reports and underfull line. The author is always responsible for the last line break; nothing is inserted by TeX in a one-line paragraph. Similar, a line break command that outputs \penalty-10000 forces TeX to obey the wish of the author to break the line at this point. It's a forced break nothing that TeX inserts.

Here is how TeX operates; I try to use simple words but it is a technical topic: TeX breaks lines at glue (including at kern followed by glue and at the end of inline math followed by glue) or at a penalty, i.e., a hyphen in the text, a hyphen inserted by TeX in the second pass, after a relation in math mode, etc. as well as a \penalty-10000 as explained above.

Of course, TeX counts the beginning of the text as the initial breakpoint. To create a paragraph TeX goes from a place where a break is possible---in the second pass the place might be created by TeX through an inserted hyphen---to the next until the width of the collected material including either shrink- or stretchability allows TeX to build a line with a badness obeying the current tolerance together with one of the earlier found (or forced) breakpoints. TeX marks such a place as a breakpoint and remembers for it the associated previous breakpoint so that it can later go from the end of the paragraph back to the start with the lines that minimize the total demerits.

TeX also creates a breakpoint if it sees a user inserted \penalty-10000. Then it starts the process as if it were at the start of the paragraph. No following breakpoint can reach over this breakpoint, i.e., the next breakpoint must record this user forced breakpoint as the previous breakpoint.

If no place can be found to build a line with a badness at most as large as the current tolerance TeX stops the processing if it is in the first pass and then it starts the second pass. Otherwise if TeX is in the second pass it looks for the next place where a break is possible without building an underfull line. This becomes a breakpoint although the line is overfull. Next, TeX continues the process. Thus the second pass goes through the whole paragraph. Note, a paragraph can have more than one overfull line.

If \emergencystretch isn't 0pt and the second pass couldn't do what was requested, i.e., find breakpoints without an overfull line or reduce the number of lines if \looseness=-1, then TeX starts a third pass that works like the second. The difference is that TeX adds the dimension of the \emergencystretch to the stretchability of every line. Thus, TeX accepts a place as a breakpoint although the width of the collected material and the stretchability in the material cannot fill the line with a badness less or equal to the current tolerance. But the width plus the stretchability plus the dimension \emergencystretch is large enough to do that; visually such a line is underfull.

There are several parameters that influence the reporting and the marking. As you said the \hfuzz dimen can omit the overfull rule but it doesn't change the fact: there is an overfull line. Another parameter called \hbadness is responsible if a warning is issued.

Thus, I don't see why the cited text of the TeXbook is wrong as it is a high level informal description.

5
  • Thanks for your answer. It may be a bit hair-splitting, but the reason I think the cited statement is not entirely correct is this: The cited statement has a premise ("If there is no way to insert breakpoints such that no line has a badness exceeding \tolerance...") and a conclusion ("... an overfull box is set.") and the text states that whenever the premise is satisfied, the conclusion holds (because the conclusion is not "... an overfull box may be set.", but "... an overfull box is set."). [Continued in next comment]
    – user227621
    Oct 12, 2022 at 6:35
  • [continuation] But when you e. g. have a multi-line paragraph and place a \linebreak early in the first line of the paragraph, the premise is satistied: There is no way to insert breakpoints such that the first line has a badness not exceeding \tolerance. Yet the output does not contain an overfull line (but an underfull line instead). So the conclusion does not hold despite the fact that the premise is satisfied.
    – user227621
    Oct 12, 2022 at 6:36
  • 1
    I see your point but one can also read the word "produces" in a way that only those lines are meant that receive their breakpoint by TeX. Moreover the text starts with "Roughly speaking" and the next paragraph of the TeXbook has this as its first sentence: "But the informal description of line breaking in the previous paragraph is an oversimplification of what really happens." I thought your question asks for an explanation that stays on the informal level but has more details. Oct 12, 2022 at 12:33
  • I am very sorry if you did unnecessary work on your answer because my question was not clear enough. Perhaps I should have emphasized that I understand the basic workings of the line breaking algorithm and was interested in the details of the overfull box scenario. I have edited the question a bit to avoid misunderstandings for future readers.
    – user227621
    Oct 12, 2022 at 19:12
  • In addition to what you wrote about the context of the cited text ("Roughly speaking ...", "... But the informal description of line breaking in the previous paragraph is an oversimplification of what really happens."), I just remembered what Knuth wrote in the preface: "Another noteworthy characteristic of this manual is that it doesn't always tell the ^{truth}. When certain concepts of \TeX\ are introduced informally, general rules will be stated; afterwards you will find that the rules aren't strictly true." So one should indeed not take the cited text too seriously.
    – user227621
    Oct 12, 2022 at 19:43

You must log in to answer this question.

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