A line-breaking fitness class is a coarse description of how tightly or loosely a line is spaced. The Knuth-Plass line-breaking algorithm distinguishes tight, normal, loose and very loose lines. A transition between sufficiently different classes adds typesetting demerits, even when each line's own spacing is individually acceptable.[ref]
For example, imagine one feasible line has line adjustment ratio and the next has . One compresses its spaces substantially, while the other expands them. That contrast can be more conspicuous than two lines with similar spacing. A paragraph objective can express this using an added term
where indexes the classes. The indicator function adds the charge only for a sufficiently large class change.
Consequently, the dynamic programming state needs the previous fitness information. Suppose two routes to the same break cost 10 and 12, but the cheaper route's class would add a transition charge of 5 to the next line while the other adds none. Their relevant totals become 15 and 12: discarding the second route solely because would be premature. The principle of optimality applies to states that retain the information the future cost needs.
TeX computes its categories using integer typesetting badness thresholds and handles special glue cases explicitly. Its implementation should therefore be consulted for exact boundary behaviour rather than inferring it from rounded decimal adjustment ratios.[ref]
TeX line penalty 2026-11-05
The TeX line penalty, set by \linepenalty, enters the per-line base used to calculate typesetting demerits. In the ordinary finite calculation, a line with typesetting badness contributes before breakpoint and neighbour-dependent terms, where denotes the chosen line penalty.[ref]
The algebra shows how it affects a candidate with lines:
Changing therefore changes both the line-count term and the way spacing costs enter the comparison.
For a chosen example, assume two feasible layouts have badness profiles and , with all other costs zero and values below saturation. At ,
At ,
The preferred candidate changes. These are illustrative profiles and parameter values. Actual line breaking must also account for feasible endpoints, typesetting penalties and possible line-breaking fitness class changes.
Text paragraph 2026-11-05
A text paragraph is a group of text treated as one unit for paragraph layout. Its line breaking may change when the available typesetting line width or computer font changes, while its underlying paragraph boundary remains the same. A line in the source file therefore need not be a line of displayed output.[ref]
For example, a sequence of four typesetting boxes may occupy two lines in a narrow container and one line in a wider container. Reflow changes the grouping of boxes without requiring a new paragraph. In TeX, \par ends the current paragraph; in HTML, a p element marks a paragraph. Those mechanisms let the layout system work with a semantic group instead of relying on the writer to insert each visible line break.
The Knuth-Plass line-breaking algorithm uses this grouping to compare costs across the whole paragraph. Its typesetting demerits can depend on both individual lines and changes between neighbouring lines, which cannot be evaluated by inspecting each source line in isolation.
Typesetting badness 2026-11-05
Typesetting badness scores the spacing strain needed to fit a line. For ordinary finite stretch or permitted finite shrink, a useful smooth guide is
where 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 in the routine's internal units, excess widths give ratios and routine outputs . In particular, the first differs from the smooth guide value 12.5.[ref]
Figure 1.
Cubic spacing cost and integer typesetting badness
. Original calculation comparing the smooth guide 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. Details.
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.
Typesetting penalty 2026-11-05
A typesetting penalty expresses how desirable a candidate break is. In the Knuth-Plass line-breaking algorithm's paragraph representation, a penalty item can also have a width that is included only if that break is taken. This lets the model account for a hyphen introduced by hyphenation without printing it in the unbroken word.[ref]
For example, suppose the material before an optional break uses 29 units and taking the break introduces a hyphen of width 2. At a typesetting line width of 30, the broken line needs units before any permitted adjustment of typesetting glue. Testing only the pre-hyphen width would wrongly declare it a fit. A finite penalty can discourage a break while still leaving it available when other choices are worse.
In TeX, values prohibit a penalty break and force it. Intermediate positive values discourage and negative values encourage it. These are control conventions of the implementation, not physical lengths.[ref]
The penalty value is an input to the typesetting demerit calculation. It is not the entire paragraph cost: typesetting badness, line-breaking fitness classes and consecutive hyphenated breaks can contribute too. Comparing two penalties alone therefore cannot choose the best text paragraph layout.