1

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, there is a tiny detail that is not mentioned in the TeXbook: What happens if two (or more) sequences of breakpoints yield the same total demerits? Can one describe in simple terms which sequence of breakpoints TeX chooses in such a case? Or do you have to read the TeX source code to understand that?

3
  • I guess that the first (or the last) sequence is chosen. I also guess that the chances this happens are really negligible.
    – egreg
    Oct 11, 2022 at 14:13
  • @egreg Well, I think this is relevant e. g. for \raggedright. When using \raggedright, \rightskip is infinite, so all lines have zero badness. TeX usually will not consider hyphenation (as that adds penalty) and use as few lines as possible (because of \linepenalty). But there may be quite a lot of different sequences of breakpoints that satisfy these conditions and then have the same total demerits. So \raggedright probably quite heavily relies on how TeX handles such a scenario.
    – user227621
    Oct 11, 2022 at 14:33
  • Probably need to read the source code. (it's at texdoc tex by the way.) Intuitively I'd guess it tries to fit as much as possible on the first line, then as much as possible on the second line, etc.
    – user202729
    Oct 11, 2022 at 16:14

1 Answer 1

1

TeX has at the end of the line-breaking a list of breakpoints that all start with "@\par". This list is looked at from the last to the first and the first "@\par" breakpoint with the smallest total demerits is taken to find the previous breakpoint, etc., up to the start of the paragraph.

The sequence of breakpoints in this list is changed by \looseness (and other primitives, for example, \parshape). In this article https://tug.org/TUGboat/tb37-3/tb117wermuth.pdf, pages 372--373, a short paragraph is presented that is typeset by TeX differently depending on the setting of \looseness, i.e., 0 or -1, although the number of lines in the paragraph is always the same.

2
  • Thank you for your answer. The link to your article is also very useful.
    – user227621
    Oct 15, 2022 at 7:30
  • Related: This answer provides a detailed analysis of a situation where TeX typesets a paragraph differently when you use a "neutral" \parshape specification (i.e. a \parshape specification that does not change the shape of the paragraph). The example there uses LaTeX's \raggedright which adds infinite stretchability to all lines, so that usually all lines have badness 0 and all sequences of breakpoints yielding the minimum number of lines have the same total demerits. The use of \parshape then changes which of these sequences is selected.
    – user227621
    Jul 10 at 13:11

You must log in to answer this question.

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