Typesetting penalty (source code)

= Typesetting penalty
{title2=$p$}

A \b[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.<Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass>[pp.1123–1125: penalty values, widths and flags]{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 $29+2=31$ 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 $p\geq10000$ prohibit a penalty break and $p\leq-10000$ force it. Intermediate positive values discourage and negative values encourage it. These are control conventions of the implementation, not physical lengths.<TeX source code by Donald E. Knuth>[penalty_node definition: inf_penalty and eject_penalty thresholds]{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.