Algorithm Search⌘K

Definitioncore

Badness of a line

Also called adjustment ratio, line badness.

A measure of how far a line's glue must stretch or shrink, about 100 times the cube of the adjustment ratio, used in the cost of a line break.

The badness of a line tells how much the spaces of a justified line must change from their natural widths to make the line exactly as long as the column. It is about 100 times the cube of the ratio of the change to the amount the spaces can change, and it is capped at 10000. A line set at its natural width has badness 0, a line whose spaces stretch or shrink by their full allowed amount has badness 100, and a line whose spaces would have to shrink more than allowed has no finite badness. Knuth and Plass defined it for TeX, where it is the main part of the cost of a line in Knuth-Plass line breaking.

Definition

Work in the box, glue and penalty model. A line has a natural width LL, a total stretchability YY and a total shrinkability ZZ (sums over its glue), and it must be set to the length ll. Its adjustment ratio is

r=l−LY if L<l,r=l−LZ if L>l,r=0 if L=l,r = \frac{l - L}{Y} \text{ if } L < l, \qquad r = \frac{l - L}{Z} \text{ if } L > l, \qquad r = 0 \text{ if } L = l,

with r=∞r = \infty when L<lL < l and Y=0Y = 0, and r=−∞r = -\infty when L>lL > l and Z=0Z = 0. Each glue ii is then set to width wi+ryiw_i + r y_i (if r≥0r \ge 0) or wi+rziw_i + r z_i (if r<0r < 0).

Real badness. The badness of the line is

β(r)={min⁡(100∣r∣3,10000)if r≥−1,∞if r<−1 (the line is overfull).\beta(r) = \begin{cases} \min(100 |r|^3, 10000) & \text{if } r \ge -1, \\ \infty & \text{if } r < -1 \text{ (the line is overfull).} \end{cases}

If the line stretches or shrinks glue of an infinite order (fil, fill or filll), only that glue moves and β=0\beta = 0.

The integer badness of TeX. TeX computes the badness in integers, from the amount t≥0t \ge 0 that the line must stretch or shrink and the total ss of the stretch or shrink, both in scaled points (units of 2−162^{-16} point):

  • badness(0,s)=0\mathrm{badness}(0, s) = 0, and badness(t,s)=10000\mathrm{badness}(t, s) = 10000 if t>0t > 0 and s≤0s \le 0;
  • otherwise let q=⌊297t/s⌋q = \lfloor 297 t / s \rfloor (TeX uses another order of operations for very large tt, to avoid an overflow);
  • if q>1290q > 1290 the badness is 10000, else it is ⌊(q3+217)/218⌋\lfloor (q^3 + 2^{17}) / 2^{18} \rfloor.

Since 2973≈100⋅218297^3 \approx 100 \cdot 2^{18}, this is 100(t/s)3100 (t/s)^3 rounded, with an error of a few units. The value 10000 means "infinitely bad", and the line breaker gives an overfull line a value above it, so it loses to every line that fits.

A line is feasible for a tolerance ρ\rho when r≥−1r \ge -1 and β≤ρ\beta \le \rho.

Examples and non-examples

t/st/s real badness TeX badness note
0 0 0 natural width
1/3 3.70 4 badness(1,3)\mathrm{badness}(1, 3)
3/10 2.7 3 badness(3,10)\mathrm{badness}(3, 10)
1/2 12.5 12 badness(1,2)\mathrm{badness}(1, 2)
1 100 100 full stretch or full shrink
2 800 800 badness(2,1)\mathrm{badness}(2, 1); only for stretch, since shrink stops at 1
3 2700 2698
9/2 9112.5 10000 TeX caps already above q=1290q = 1290
  • Stretch and shrink are symmetric. A line 1 unit too long with shrink 3 and a line 1 unit too short with stretch 3 both have r=±1/3r = \pm 1/3 and badness 3.70 (4 in TeX).
  • The cap in TeX comes early. The largest finite value of the integer form is 8189, at q=1290q = 1290, that is t/st/s just under 4.35. From there on TeX gives 10000, while the real form reaches 10000 only at t/s=102/3≈4.64t/s = 10^{2/3} \approx 4.64. So the two forms can disagree on which of two very loose lines is worse.
  • A line that cannot stretch. A line with one word and no glue, shorter than ll, has Y=0Y = 0, so r=∞r = \infty and the badness is 10000. In TeX it is an underfull box. It is still allowed if the tolerance is 10000.
  • A line that is too long. A line with L=20L = 20, Z=3Z = 3 and l=15l = 15 has r=−5/3<−1r = -5/3 < -1. It is overfull, and its badness is not 10000 but infinite: no tolerance makes it feasible.
  • The last line. The last line of a TeX paragraph holds the glue \parfillskip with stretch 1fil, so its badness is 0 however short it is.
  • Not the adjustment ratio. The ratio rr and the badness are different numbers: r=1/2r = 1/2 gives badness 12.5. The badness is a cubic function of rr, so it grows slowly near 0 and fast beyond 1.
  • Not the slack. The cost of printing neatly is the cube of the empty space at the end of a line, which does not depend on the number of spaces. The badness divides by the stretch first, so the same empty space costs less in a line with more spaces.
  • Adjustment ratio. The number rr above; it gives the widths of the glue.
  • Demerits. The cost of a line in the Knuth-Plass algorithm, about (λ+β)2+p2(\lambda + \beta)^2 + p^2 for the line penalty λ\lambda and the penalty pp at the break, with extra terms for hyphens and fitness classes. The square of the badness makes one very bad line cost more than several slightly bad ones.
  • Fitness class. Knuth and Plass put a line into one of four classes by rr: tight (r<−1/2r < -1/2), decent (−1/2≤r≤1/2-1/2 \le r \le 1/2), loose (1/2<r≤11/2 < r \le 1) and very loose (r>1r > 1). The bounds 1/21/2 and 11 are the badness values 12.5 and 100, and TeX tests its integer badness against 12 and 99.
  • Tolerance. The largest badness of a feasible line; plain TeX uses 100 for its first try without hyphens (\pretolerance) and 200 for the second (\tolerance).
  • Page badness. The page builder of TeX uses the same function for the glue of a page, as in best-fit page breaking.
  • Line cost in least weight subsequence. With rigid spaces and a cost that is a convex function of the width of the line, line breaking is a least weight subsequence problem with the quadrangle inequality. The badness is not of this form, because it divides by the stretch of the line, which changes from line to line.

References

  1. Donald E. Knuth and Michael F. Plass, Breaking paragraphs into lines, Software: Practice and Experience 11(11), 1119-1184, 1981, the definition of the adjustment ratio, the badness 100 |r|^3, feasible lines and fitness classes
  2. Donald E. Knuth, The TeXbook, Computers and Typesetting volume A, Addison-Wesley, Chapter 14, How TeX breaks paragraphs into lines (badness, tolerance and demerits)
  3. Donald E. Knuth, TeX: The Program, Computers and Typesetting volume B, Addison-Wesley, 1986, the badness function in the part on basic arithmetic, and its use in the line breaker

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/4N5WKK/text.yaml