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.
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 2026-11-05
TeX is Donald E. Knuth's programmable system for typography, particularly mathematical typesetting. A document supplies text and instructions; TeX macros and TeX primitives build material whose dimensions determine the eventual page layout. Computer fonts supply glyphs and font metrics, while line breaking selects how a text paragraph is divided. The Knuth-Plass line-breaking algorithm is one of the system's central layout methods.[ref]
A TeX format supplies a prepared collection of definitions. Plain TeX and LaTeX provide different document interfaces built on TeX technology. For example, in Plain TeX the input $\sqrt{2}$\bye requests a square root expression, , and ends the document. The computer font, glyph positioning and mathematical typesetting rules determine the printed geometry; the source characters alone are not a picture of the result.
The TeXbook by Donald E. Knuth is the system's user and reference manual. TeX source code by Donald E. Knuth exposes the implementation as a documented computer program, making the paragraph algorithm and its numerical conventions inspectable.[ref]
TeX character code 2026-11-05
A TeX character code is the numerical component identifying a character in TeX's character model. An ordinary character TeX token combines that code with a TeX category code. Classic TeX uses an eight-bit character-code range, 0–255.[ref]
For example, under the usual ASCII mapping, a has code 97. Reading it once with category 11 and again with category 12 produces the pairs and . The first component stays the same; the syntactic classification changes. This is why a TeX macro can distinguish inputs that look alike on the page.
The number does not identify a universal glyph. A computer font supplies the printed form, and a glyph index belongs to that font. The classic range should also not be treated as a restriction on every extended TeX engine.
TeX control symbol 2026-11-05
A TeX control symbol is a TeX control sequence formed by an escape followed by one character whose TeX category code is not letter. Under ordinary conventions, \% is one such command.[ref]
For an invented example, define \def\!{Q} and then read \! X. The resulting replacement starts with Q, one ordinary space token and X. The nonletter ends the command name immediately; the first following space is processed in state M and survives. Compare \def\word{Q} followed by \word X, which produces QX.
Control space, written as an escape followed by a space, has special handling: it enters state S. Thus a rule that discards every space after every command would predict the wrong result for the \! example.[ref]
TeX control word 2026-11-05
A TeX control word is a TeX control sequence scanned from an escape followed by a maximal run of characters with TeX category code 11. The scanner then enters the space-skipping TeX input state S.[ref]
For example, after \def\word{Q}, the input \word X yields QX; the spaces terminate the name and are skipped. The input \word X does the same. By contrast, \wordX names a single different command, so removing the separator does not preserve the token sequence.
Token identity and current meaning are separate. Define \def\call{\word}, then redefine \word to yield R. Expanding \call now yields R, because its stored command token still names word. This is useful when reading TeX macros: the spelling identifies the command, while its current definition controls expansion.
TeX input processor 2026-11-05
The TeX input processor converts input characters into TeX tokens using their current TeX category codes. Its whitespace handling is described by TeX input states: new line N, skipping spaces S and middle of line M.[ref]
Under ordinary conventions, the invented input A B produces an A character token, one space token and a B character token. After A, state M allows the first space through and switches to S; the next two spaces disappear. Reading B returns to M. A space-only line begins in N and its end produces \par, whereas the end of an ordinary text line in M supplies a space.[ref]
This explains why source indentation and repeated spaces need not survive as visible word spacing. The complete scanner also handles comments, TeX control sequences and character-code conversions; the N/S/M diagram describes the whitespace part of the process.
TeX input state 2026-11-05
A TeX input state is the N, S or M state used by the TeX input processor while reading character input: new line, skipping blanks or middle of line. It helps determine whether a space or end-of-line character becomes a TeX token.[ref]
Assume ordinary TeX category codes. In N, leading spaces are ignored. An ordinary letter emits its character token and enters M. The first space in M emits one space token and enters S; further spaces in S are ignored. An end-of-line character emits a space from M, emits \par from N and emits nothing from S. The next line starts in N.[ref]
For an original trace, take a line containing two leading spaces, A, three spaces and B, followed by a space-only line. The first two spaces disappear in N. A enters M; one space survives and the next two disappear in S; B returns to M. The first line end supplies another space. The blank line then supplies \par. The resulting sequence is A, space, B, space, \par.
Thus source whitespace is processed before word spacing and text paragraph layout. This state records part of scanning, not whether TeX is currently building a text line or a mathematical formula; TeX control sequences, comments and changed TeX category codes have further scanning rules.
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.
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 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 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]
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 is
Charges 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.
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 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.