Typesetting badness (source code)

= Typesetting badness
{title2=$b$}

\b[Typesetting badness] scores the spacing strain needed to fit a line. For ordinary finite stretch or permitted finite shrink, a useful smooth guide is
$$
\boxed{b\approx100|r|^3,}
$$
where $r$ is the <line adjustment ratio>. Doubling the required ratio multiplies this guide by eight, so it strongly penalizes large adjustments. The <convex function> on nonnegative ratios rewards more evenly distributed spacing strain.

<TeX> computes an <integer> approximation with saturation rather than evaluating that real-valued expression exactly. With stretch coefficient $s=65536$ in the routine's internal units, excess widths $t=32768,65536,131072$ give ratios $1/2,1,2$ and routine outputs $12,100,800$. In particular, the first differs from the smooth guide value 12.5.<TeX source code by Donald E. Knuth>[badness(t,s): fixed-point approximation, rounding constants and inf_bad; chosen input values evaluated directly]{ref}

\Image[/typesetting-badness-curve.png]
{title=Cubic spacing cost and integer <typesetting badness>}
{description=Original calculation comparing the smooth guide $100r^3$ with TeX's badness routine at chosen finite stretch coefficients. The right panel enlarges the integer steps near r=0.5. All plotted ratios describe stretching.}

The sentinel 10000 represents saturated badness in the routine. Excessive shrinking is tested separately in paragraph breaking; higher-order stretch has its own treatment. A paragraph's <typesetting demerits> also include <typesetting penalties> and possible neighbour-dependent charges, so choosing solely by badness can miss the best layout.