# Knuth's line breaking algorithm

I'm trying to understand the basics of Knuth's line breaking algorithm. At the beginning of Chapter 12: Glue of his TeXbook, there is a figure like the one below.

My question is: where did those Stretchs and Shrinks come from?

-
It's just an example, where the glue between boxes is explicitly inserted as shown. The first can be specified as \hskip 9pt plus 3pt minus 1pt, assuming points as the unit of measure. –  egreg May 27 '11 at 13:33
@egreg So you're saying these are just arbitrary values given by the user. –  bellochio May 27 '11 at 13:41
@bellochio Yes, just to give a flavor of what happens, in a controlled case. –  egreg May 27 '11 at 13:47
What would be those values if you didn't use the \hskip command ? –  bellochio May 27 '11 at 13:59
@bellochio: What do you mean? If you don't put glue between boxes, there is none added. –  egreg May 27 '11 at 14:14

The example in the TeXbook has just arbitrary values; assuming units of points, it may can be emulated by the input

\def\example{%
\hbox to 5pt{\hrulefill}%
\hskip 9pt plus 3pt minus 1pt
\hbox to 6pt{\hrulefill}%
\hskip 9pt plus 6pt minus 2pt
\hbox to 3pt{\hrulefill}%
\hskip 12pt plus 0pt minus 0pt
\hbox to 8pt{\hrulefill}}


If you say \hbox{\example} the resulting box width will be 5+9+6+9+12+8=52 points. If you say \hbox to 58pt{\example}, the resulting box will be indeed 58pt wide, and the glue inside will stretch as explained with a 6/9 stretch ratio.

If you say \hbox to 51pt{\example}, the glue will shrink with a shrink ratio of 1/3, in order to get a 51pt wide box.

When you do a real paragraph, the glue is inserted by the spaces in the input. The amount of glue is font dependent: for example, with cmr10, each space will insert glue equivalent to saying

\hskip 3.33pt plus 1.67pt minus 1.11pt


(but there is the space factor to consider, that might change this amount in some places). The line breaking algorithm decides where each line starts and ends and what's done to each line is exactly analogous to the example before: the goal width is generally the \hsize and the glue will be set according to the rules outlined in the example.

The values for cmr10 can be found in Appendix F, page 433. For a general font one can see the amount of glue inserted by a normal space by saying

\skip0 = \fontdimen2\font plus \fontdimen3\font minus \fontdimen3\font


as in this context \font refers to the current font. With \showthe\skip0 you'll see the value for the interword glue.

-
Now is becoming more clear. Where did you get the values 3.33pt plus 1.67pt minus 1.11pt for the case where the glue is inserted automatically by the spaces in the line. Is there any Tex command that I can use to get these values ? –  bellochio May 27 '11 at 16:04
Thanks for your help. Really appreciated this. –  bellochio May 27 '11 at 16:51
@bellochio: The interword space depends on some \fontdimens. –  Martin Schröder May 27 '11 at 20:24
@bellochio: something like \setbox0=\hbox{abc def} then \showbox0 and examine the log (or \begingroup\tracingall\showbox0\endgroup if you prefer seeing the result in the terminal and like me you are too lazy to figure out which one of TeX's tracing settings will do that for you. –  Bruno Le Floch May 27 '11 at 21:26