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]
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.
Greedy line breaking 2026-11-05
Greedy line breaking commits each line after filling it with the longest available fitting prefix of the remaining input. In a fixed-width model, the input consists of ordered, indivisible typesetting boxes, a common typesetting line width , and nonnegative fixed word spacing. Assume every individual box fits. The greedy algorithm scans forward, adds the next item if it fits, and otherwise starts a new line. Incrementally maintaining the used width gives time complexity for input boxes.
This rule minimizes the number of lines under these assumptions. To see why, let be the last item covered by the first greedy lines, and the last item covered by the first lines of any feasible alternative. After a layout finishes, take its endpoint to remain for subsequent indices. The first greedy line reaches at least as far as the alternative's first line. Suppose . If the greedy layout has already reached , the next inequality is immediate. Otherwise, removing the alternative's already covered items from its next line leaves a fitting suffix, because widths and gaps are nonnegative. The next greedy line can reach at least that endpoint. By mathematical induction,
Thus an alternative cannot finish in fewer lines.
Line count and paragraph appearance are different objectives. With widths , gap 1 and width 7, greedy grouping has lengths . The alternative also uses three lines. The line breaking with squared slack cost model assigns cost 20 to this alternative and 26 to the greedy layout when the final line is free. The line breaking diagram shows the two layouts. Global paragraph costs can justify dynamic programming even when greedy packing already uses the minimum line count.
The fixed-width assumptions matter when applying this result. Hyphenation introduces additional break opportunities, while adjustable typesetting glue changes how a candidate line fits. Those choices belong to richer layout models such as the Knuth-Plass line-breaking algorithm.
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]
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 is
Each 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]
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]
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. Define
It 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]
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.
Natural line width 2026-11-05
The natural line width is the width a candidate line occupies before any stretch or shrink of its typesetting glue. In a simple horizontal model with fixed typesetting box widths and nominal glue widths ,
Additional fixed positioning adjustments must be included when the model contains them. If a discretionary break inserts a hyphen at the chosen endpoint, its width belongs to that candidate's natural width.
For example, boxes of widths 8, 6 and 10 and two nominal gaps of width 3 give . To reach a typesetting line width , the glue must absorb . A total finite stretch coefficient gives line adjustment ratio . The typesetting glue diagram shows the resulting adjustment.
Computing the natural width first separates the dimensions of the selected material from its later spacing adjustment. This also lets a line breaking algorithm compare several endpoints before committing to a final line. In the fixed-space model, prefix sums make repeated width calculations efficient.
Text justification 2026-11-05
Text justification adjusts the contents of a line to occupy its available typesetting line width, often aligning both ends of the text. Changing word spacing is one method; permissible adjustments depend on the writing system. Typographic alignment more generally includes leaving the opposite edge ragged or centring a line.[ref]
In an illustrative line with natural line width and target width , two equally flexible spaces can each gain 1. If their stretch coefficients are instead 1 and 3, proportional adjustment allocates the required extra 2 as
Both choices reach the same width; their internal spacing differs. Typesetting glue expresses that flexibility, and the line adjustment ratio computes the common proportional change.
Line breaking and text justification interact: a different break changes the number of spaces available to absorb the width difference. A good paragraph layout therefore considers spacing quality when choosing breaks, rather than merely stretching an already chosen set of lines. The Knuth-Plass line-breaking algorithm makes that interaction explicit.
Typesetting box 2026-11-05
A typesetting box is a piece of material treated as one unbreakable item at a given stage of line breaking. The simple paragraph model assigns it a width. A word already assembled from glyphs, or a small diagram placed within text, can be represented by such an item.[ref]
For example, three boxes of widths 8, 6 and 10 separated by two spaces of width 3 occupy . Keeping the boxes fixed while changing only the spaces is the model used by typesetting glue. If hyphenation makes an internal split available, the word must instead be represented with that discretionary break; a single indivisible box would hide the choice.
A box's width describes layout advance, which need not equal the exact ink extent of its glyphs. A more complete TeX box also has height and depth relative to a text baseline. These measurements allow boxes to be combined while maintaining baseline geometry.[ref] The CSS box model uses its own rules for content, padding, borders and margins; interpreting every CSS box as an indivisible paragraph item would lose those distinctions.
Typesetting glue 2026-11-05
Typesetting glue represents adjustable space between pieces of typeset material. In a finite, single-order model, describe a glue item by a nominal width , stretch coefficient and shrink coefficient . A shared line adjustment ratio gives
Thus a negative reduces the gap. The coefficients describe relative flexibility; they need not be identical for all spaces. This model is a useful part of text justification.[ref]
For example, two nominal gaps of width 3 with stretch coefficients 1 and 3 become and when . Their total extra width is 2. With shrink coefficients 1 and 1, makes both gaps width 2. The same natural line can therefore reach different targets by using the appropriate coefficients.
Figure 1.
Proportional stretching and shrinking of typesetting glue
. Original finite-glue example: boxes of widths 8,6,10; two gaps with nominal width 3, stretch coefficients 1 and 3, and shrink coefficients 1 each. The generator computes the adjustment ratios and resulting widths. Details.
TeX also distinguishes typesetting glue orders, so the finite model above does not describe every glue setting. In a horizontal line, typesetting glue changes gaps rather than stretching the outlines of the glyphs.
Typesetting glue order 2026-11-05
A typesetting glue order assigns a priority to stretch or shrink in TeX. The orders are normal finite flexibility, fil, fill and filll, in increasing priority. With nonnegative coefficients, the highest present order absorbs the adjustment; lower-order flexibility is left unused.[ref]
For example, suppose a line has natural width 22 and must reach width 34. Two zero-width glue items have stretch coefficients 1fil and 2fil. Their added widths are
A finite-order glue item in the same line keeps its nominal width during this adjustment. The symbols define priorities for distributing a finite width difference. Within the selected order, the coefficients still determine proportional sharing.
The simple finite model in typesetting glue uses only the normal order. Higher orders make it possible to place flexible space at selected locations, such as before or after a line's content, without requiring the ordinary word spacing to absorb that particular adjustment.
Typesetting line width 2026-11-05
The typesetting line width is the horizontal measure that a layout assigns to a particular line. It supplies a target for text justification and a fitting constraint for line breaking. Write it as when it can depend on line number . The Knuth-Plass line-breaking algorithm permits a sequence of target widths, rather than requiring the entire text paragraph to be rectangular.[ref]
For example, suppose a text column is 40 units wide and first-line indentation reserves 10 units on its first line. Then
With a fixed gap of width 2, three typesetting boxes of widths 12, 10 and 8 use units. They exceed the first line's available width, although they fit a later line in the fixed-space model. When typesetting glue can shrink, feasibility must also account for that permitted shrinkage.
Width and line height measure different directions. Changing the font size can alter both the widths of the typesetting boxes and their vertical metrics, so preserving the same column width does not preserve the same breaks.
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.
Typographic alignment 2026-11-05
Typographic alignment places a line's content relative to its available horizontal space. Common choices align its left edge, align its right edge, centre it or apply text justification. In CSS, text-align specifies alignment within the line's container, rather than positioning the entire container in the page.[ref]
For a rigid line with natural line width , no indentation, and a left boundary at , the starting positions are
Thus and give starts 0, 8 and 16. These three choices preserve the internal word spacing. Text justification can instead change that spacing through typesetting glue so the content reaches the intended edges. The alignments therefore solve different geometric tasks even when the same words and computer font are used.
Word spacing 2026-11-05
Word spacing is the separation between adjacent words in a line of text. Its nominal value comes from the computer font and layout settings; text justification can adjust it through typesetting glue. Letter spacing instead changes spacing within the text's sequence of letters. A script without visible word separators needs a different spacing strategy from an English sentence.[ref]
Suppose three typesetting boxes have widths 8, 6 and 10, and the two intervening spaces each have width 3. The natural line width is
Adding 1 to each space gives width 32. Adding 1 to every glyph advance would generally produce a different layout, because there need not be one glyph per word or per stored character. Word spacing must therefore be applied to the intended separators rather than inferred from a count of glyphs.
In CSS, word-spacing: 1px specifies additional spacing at the relevant word separators; it does not set every interword gap to exactly one CSS pixel.[ref]