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?
\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.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.