First-line indentation 2026-11-05
First-line indentation shifts the beginning of the first formatted line of a text paragraph relative to its normal starting edge. In a left-to-right layout, a positive indent reserves space on the left; the corresponding starting edge is on the right in a right-to-left layout. In CSS,
text-indent controls this placement.[ref]Text in Cascading Style Sheets Level 2 Revision 2 by World Wide Web Consortium. §16.1: first formatted line, starting edge and indentation
In a simple left-to-right column of width with indentation , the first line begins at and has available typesetting line width . Later lines can begin at and use all 40 units. A negative indent instead places the start at , extending into the margin if no other layout constraint intervenes.
This changes the first line's geometry without adding printable glyphs. Replacing the indent by several stored spaces would couple the result to word spacing, white-space processing and the chosen computer font. A line breaking model can represent the change explicitly through its sequence of typesetting line widths.
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.
Hyphenation 2026-11-05
Hyphenation permits a word to be divided at an appropriate internal position during line breaking, usually displaying a hyphen when that division is taken. A hyphenation opportunity is a permitted choice, rather than an instruction to break every occurrence. The choice can help a narrow text paragraph fit without excessive word spacing.
A soft hyphen, Unicode U+00AD, marks such an optional position: in a suitable context,
ex followed by a soft hyphen and ample can display as example on one line or as ex- followed by ample on the next. The soft hyphen normally remains invisible when no split is taken. An always-visible hyphen already present in the word has different display behaviour.[ref]CSS Text Module Level 3 by World Wide Web Consortium. §5.3: conditional hyphens, visible hyphens and language-sensitive hyphenation
In CSS,
hyphens: manual uses explicit opportunities; hyphens: auto also permits opportunities supplied by a resource for the text's known language. The hyphenation resource and the line breaking algorithm answer different questions: where a split is linguistically allowed, and which allowed split best fits the layout. A discretionary break can also account for material inserted or replaced at the chosen split.[ref]CSS Text Module Level 3 by World Wide Web Consortium. §5.3: manual/auto values and content-language requirement
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
Letter spacing 2026-11-05
Letter spacing, also called typographic tracking, adjusts spacing within a run of text. It is distinct from word spacing and from context-sensitive kerning. In CSS, the
letter-spacing property controls additional spacing, with rules that respect the writing system rather than blindly separating every stored character.[ref]Text in Cascading Style Sheets Level 2 Revision 2 by World Wide Web Consortium. §16.4: letter-spacing and distinction from kerning
[ref]CSS Text Module Level 3 by World Wide Web Consortium. §7.2: typographic character units and cursive scripts
A simple model illustrates the geometry. Suppose four already shaped units have combined advance , and three selected internal gaps each receive an extra . ThenThis is an explicitly chosen three-gap model. A glyph need not correspond to one stored character, so counting source characters is generally insufficient to determine those gaps. Text shaping, typographic ligatures and the script's joining behaviour must be considered when deciding where spacing may change.
Line breaking with squared slack cost 2026-11-05
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 usesA line is feasible when . Charge for a nonfinal line and zero for the final line. If are the breaks, the objective isThe 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 andAn 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 areFollowing 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.
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]TeX source code by Donald E. Knuth. get_next: Handle situations involving spaces, braces, changes of state
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]
TeX source code by Donald E. Knuth. Input stacks: mid_line, skip_blanks and new_line; get_next whitespace cases
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]TeX source code by Donald E. Knuth. Cases where character is ignored; Handle situations involving spaces, braces, changes of state
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.
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]
CSS Text Module Level 3 by World Wide Web Consortium. §6: alignment versus justification; §6.4: script-sensitive expansion and compression
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 asBoth 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 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]TeX source code by Donald E. Knuth. glue_spec fields and hpack/vpack: highest nonzero order for glue setting
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 areA 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.
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]Text in Cascading Style Sheets Level 2 Revision 2 by World Wide Web Consortium. §16.2: alignment within line boxes and text-align values
For a rigid line with natural line width , no indentation, and a left boundary at , the starting positions areThus 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 separator 2026-11-05
A word separator is a text unit whose primary role is to separate words. Unicode SPACE U+0020 and non-breaking space U+00A0 are common examples. In CSS, the word-separator classification helps determine where word spacing is applied after white-space processing.[ref]
CSS Text Module Level 3 by World Wide Web Consortium. §7.1: word-separator characters and extra spacing
Separation and line breaking are related but distinct. In a suitable ordinary text context, a normal space between
10 and kg provides a possible break. Using a non-breaking space there keeps the number and unit together at that boundary. Both characters can still provide visible separation.[ref]Unicode Line Breaking Algorithm by the Unicode Consortium. Line-breaking properties: GL class and rules LB12/LB12a
A language can also write words without visible separators, so a count of space characters is not a universal word-counting method. Text shaping, word spacing and permitted breaks must follow the appropriate text-processing rules. A separator's stored character and the selected glyph advance jointly participate in the geometry.
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]
CSS Text Module Level 3 by World Wide Web Consortium. §§6.4 and 7.1–7.2: script-dependent justification and word/letter spacing
Suppose three typesetting boxes have widths 8, 6 and 10, and the two intervening spaces each have width 3. The natural line width isAdding 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.