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 breaking with squared slack cost is a simplified mathematical optimization model for paragraph layout. Treat the input as indivisible typesetting boxes with nonnegative widths , keep their order, use fixed word spacing , and require each line to fit a common typesetting line width . This model isolates the choice of breaks from adjustable spacing and hyphenation.
Let be the prefix sum, with . Placing items through on one line uses
A line is feasible when . Charge for a nonfinal line and zero for the final line. If are the breaks, the objective is
The free final line is an explicit modelling choice. The convex function makes a doubled slack cost four times as much. For two slacks with fixed total,
so greater imbalance increases their sum of squares. Actual break choices can also alter the line count and total slack; the whole paragraph still needs to be optimized.
Apply dynamic programming with and
An endpoint with no feasible predecessor has cost . The same recurrence finds a shortest path through the directed acyclic graph of feasible line choices. The principle of optimality explains the recurrence. Once the final breakpoint of a prefix is chosen, replacing its preceding layout by a cheaper layout with the same endpoint would improve the whole candidate. Conversely, each feasible predecessor and final line produces a valid candidate. Induction over therefore gives the optimum. Prefix states with charge their last line; only the final state gets the paragraph's free-last-line convention.
For widths , spacing 1 and capacity 7, the states are
Following minimizing predecessors gives line lengths and cost 20. The line breaking diagram compares this with the cost 26 of greedy line breaking. With prefix sums, each candidate length takes constant time; at most candidates give time complexity and space complexity for costs and predecessors. The big O notation describes this fixed-space model; the Knuth-Plass line-breaking algorithm includes additional typography-specific choices.
Mathematical typesetting 2026-11-05
Mathematical typesetting arranges symbolic expressions in two dimensions. It must coordinate computer fonts, glyph positioning, typesetting boxes and text baselines while preserving the expression's visible structure. A typeset fraction positions its components above and below a bar; superscripts, subscripts, square roots and mathematical delimiters impose other geometric relationships.[ref]
For example,
contains a superscript inside the upper component and a square root inside the lower component. Their dimensions help determine the height of the complete typeset fraction. A larger expression may then require larger surrounding mathematical delimiters. Laying out the smallest parts first makes this a recursive geometric construction.
An OpenType MATH table supplies font-specific information such as placement constants and suitable glyph variants. The table supplies data for layout; different implementations can use those data in different algorithms. TeX and LaTeX provide established authoring systems for expressing such mathematical structure.[ref]
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.
TeX expansion 2026-11-05
TeX expansion replaces expandable TeX tokens with other tokens according to their current meanings and the scanning context. TeX macros provide replacement lists; some TeX primitives also expand. Subsequent processing can execute commands or build typesetting boxes.[ref]
For example, \def\firstdemo{AB}\def\seconddemo{\firstdemo} establishes two macros. Expanding \seconddemo first produces the token for \firstdemo; expanding that yields the character tokens A and B. The intermediate result is a token stream. The printed shapes are determined later by computer fonts and layout.
Meaning can change between definition and use. With \def\value{A}\def\callvalue{\value}, redefining \value to expand to B changes the later expansion of \callvalue to B. The stored TeX control sequence still identifies the same name, while its current definition supplies the replacement. This makes the distinction between token identity and current meaning practically useful.
TeX primitive 2026-11-05
A TeX primitive is an operation supplied by TeX itself, such as defining a TeX macro, changing a register or constructing a typesetting box. A TeX control sequence can name a primitive or a macro. The distinction concerns the current meaning of the command, rather than the appearance of its name.[ref]
For example, \hbox constructs a horizontal typesetting box. The assignment \let\mkbox=\hbox gives another name to its current meaning. Defining a macro with \def\label{sample} creates a replacement list instead. Some primitives participate in TeX expansion, while others perform operations during later processing; primitive does not mean unexpandable. This distinction helps explain why a command can appear in an expansion trace without immediately drawing a glyph.
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.
Typeset fraction 2026-11-05
A typeset fraction is a two-dimensional layout that places one expression above another, commonly with a horizontal rule between them. The components are laid out as typesetting boxes before they are assembled. In MathML, an mfrac element supplies the two components; mathematical typesetting style affects their presentation.[ref]
For example,
has a superscript in its upper component and a square root in its lower component. Their dimensions influence the space above and below the fraction rule. The expression's algebraic meaning and its geometry are related but distinct: the layout can contain arbitrary expressions, including ones whose values are not rational numbers.
A simple own box model makes the horizontal placement explicit. Suppose the component widths are and , and the layout adds a chosen side allowance . If both components are centred in width
their left offsets are and . This is an illustrative centring model; real layouts also use vertical clearances, rule thickness and font data from an OpenType MATH table.
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.
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]