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]
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 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]
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.
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.
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.
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.
Soft hyphen 2026-11-05
A soft hyphen, or SHY, is the Unicode character U+00AD. It marks a preferred optional division inside a word and normally has no visible shape when that division is unused. Selecting it invokes the language-appropriate presentation for an intraword break, which can involve more than adding one hyphen.[ref]
For example, a suitable renderer can show ex followed by SHY and ample as example on one line, or as ex- and ample on successive lines. The visible hyphen then belongs to the chosen layout. In languages with spelling changes at a division, the layout implementation may also need to modify the divided parts.[ref]
A soft hyphen supplies a hyphenation opportunity; it does not force the line breaking algorithm to use it. The algorithm can retain the unbroken word when its typesetting line width and paragraph cost permit that choice.
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.
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.
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.