Discretionary break 2026-11-05
A discretionary break describes an optional line breaking choice whose printed material may depend on whether the break is selected. In TeX,
\discretionary{pre}{post}{none} supplies material before a chosen break, after it, or in the unbroken case.[ref]TeX source code by Donald E. Knuth. disc_node: pre_break, post_break and replacement material
For a purely mechanical example,
ab\discretionary{-}{X}{Y}cd can produce abYcd without the break, or ab- followed on the next line by Xcd when it is selected. The arbitrary strings make all three branches visible. A common hyphenation case simply inserts a hyphen before the break and supplies no material afterward.Each alternative has its own geometry. A typesetting box used only in the pre-break branch must contribute its width when that branch is tested. The natural line width, typesetting glue and typesetting penalty calculation must therefore use the selected representation. Treating the choice merely as an invisible marker could miss the width of the inserted hyphen.
Hyphenation opportunity 2026-11-05
A hyphenation opportunity is a position where the language and chosen hyphenation rules permit a word to be divided across lines. An explicit soft hyphen can supply one; automatic hyphenation can find others using a language-appropriate resource. These permitted positions become choices for line breaking.[ref]
CSS Text Module Level 3 by World Wide Web Consortium. §5.3: explicit and automatic language-appropriate hyphenation opportunities
Two tests must then be applied. First, is the division permitted by the linguistic rules? Second, does the resulting geometry and cost make it a useful choice? If the material before a break has width 20, an inserted hyphen adds 2, and the available typesetting line width is 21, the resulting 22-unit line fails the rigid-width fitting test despite the linguistic permission.
A discretionary break represents the alternative printed material. The Knuth-Plass line-breaking algorithm can compare that option with later or earlier permitted breaks using typesetting badness and typesetting penalties.
Knuth-Plass line-breaking algorithm 2026-11-05
The Knuth-Plass line-breaking algorithm chooses paragraph breaks using dynamic programming over a representation of the material as typesetting boxes, typesetting glue and typesetting penalties. Boxes supply unbreakable widths; glue supplies normal spacing together with stretch and shrink allowances; penalties express preferences about permitted breaks and can account for a hyphen added when a break is taken.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. pp.1119–1122, summary and formulation
Text justification makes the glue geometry useful. In an illustrative line, suppose box widths total 24 and two glues each have normal width 3 and stretch allowance 2. To fill width 32, the line adjustment ratio isEach glue then becomes , giving . This example demonstrates proportional stretching; it is one ingredient of paragraph optimization.
The dynamic programming state retains information needed to score later choices. In particular, a line-breaking fitness class records a spacing category, allowing an abrupt change between neighbouring lines to affect the cost. Break position alone would discard that relevant history. The original active nodes include line number and fitness class, with the best accumulated demerits stored as their value.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. pp.1158–1159, active nodes, line numbers and fitness classes
The line breaking with squared slack cost example isolates break selection with fixed word spacing. Here, adjustable typesetting glue and typesetting penalties participate in the paragraph optimization. Donald E. Knuth and Michael F. Plass's 1981 paper documents the method and its development in the TeX project.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. pp.1175–1176, authors' historical account
Line adjustment ratio 2026-11-05
The line adjustment ratio measures the stretch or shrink required to make a candidate line fit. In the finite-glue model, let be its natural line width, its target typesetting line width, and and the total stretch and shrink coefficients of its typesetting glue. DefineIt is a dimensionless quantity: numerator and denominator are widths measured in the same units. Each glue item uses the same , scaled by its own appropriate coefficient.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. pp.1125–1127: adjustment ratio and feasible stretching/shrinking
For , and , targets 32 and 28 give and respectively. If a nonzero width difference requires a coefficient total of zero, this model cannot make the adjustment. With ordinary bounded shrinkage, asks the glue to shrink beyond its total allowance and is infeasible. Positive stretch is usually limited by an acceptable-quality threshold rather than by treating as an absolute geometric limit.
The typesetting badness scores spacing quality from the required adjustment, while a line-breaking fitness class records a coarser category for comparison with neighbouring lines. Neither measure alone incorporates every typesetting penalty.
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]TeX source code by Donald E. Knuth. try_break: line_penalty+b is squared before other contributions
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.
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 iswhere 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]
TeX source code by Donald E. Knuth. badness(t,s): fixed-point approximation, rounding constants and inf_bad; chosen input values evaluated directly
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 demerit 2026-11-05
A typesetting demerit is a contribution to the paragraph cost minimized by the Knuth-Plass line-breaking algorithm. It combines spacing quality with break preferences and, when applicable, costs involving neighbouring lines. Thus typesetting badness is one input to the cost rather than the entire objective.[ref]
TeX source code by Donald E. Knuth. try_break: line_penalty, badness, break penalties and adjacent/hyphen demerits
Consider the ordinary finite-penalty case in TeX, away from its integer saturation and recovery cases. Let be the TeX line penalty, the typesetting badness and the typesetting penalty, with and . The base contribution isCharges for incompatible line-breaking fitness classes or consecutive hyphenated lines can then be added.
For an illustrative choice , and , the base is . Changing only to gives . If an adjacency charge of 100 applies, those become 1025 and 975. These are chosen example parameters, not asserted defaults.
Dynamic programming compares accumulated costs while retaining the dynamic programming state needed by future charges. The lowest-cost break considered alone need not produce the lowest-cost complete text paragraph.
