GNU TeXmacsUser manualReferenceAboutEnglish
Hyphenation

TeXmacs hyphenates words with Liang's algorithm, the one used by TeX, and with the same pattern files. The implementation is in

System/Language/hyphenate.cpp

; the text languages of

text_language.cpp

load the patterns in their constructors and call the algorithm from get_hyphens and hyphenate.

1.Pattern files

The patterns live in

src/TeXmacs/langs/natural/hyphen/

as files hyphen.name, where name is the second argument given to the language constructor in text_language (for instance us for English and ukenglish for British English). They are ordinary TeX pattern files in UTF-8, with a \patterns{…} block and an optional \hyphenation{…} block of exceptions.

load_hyphen_tables (name, patterns, hyphenations, toCork) reads such a file into two hash tables:

patterns

For each pattern such as 1ba or .ach4, the key is the pattern without its digits (ba, .ach) and the value is the pattern itself. The TeX escapes ^^xx are decoded first.

hyphenations

For each exception such as uni-ver-sity, the key is the word without hyphens and the value is the word with hyphens.

The file is cut into tokens at white space; % starts a comment up to the end of the line; a block starts at the token \patterns{ or \hyphenation{ and ends at a token consisting of a single }. If toCork is set, which is the case for all text languages except those of ucs_text_language_rep (Bulgarian, Russian, Ukrainian), the file is converted from UTF-8 to the Cork encoding before parsing, so that patterns can be compared directly with TeXmacs strings. For the other languages the patterns stay in UTF-8 and words are converted to UTF-8 before hyphenation.

2.The algorithm

get_hyphens (s, patterns, hyphenations, utf8) returns an array of penalties, one for each position between two consecutive characters of the word s: the entry with index \(k\) is the penalty for breaking after the first \(k + 1\) characters. It proceeds as follows:

  1. The word is converted to lower case (and to UTF-8 in the utf8 case).

  2. If the word is an exception, the penalties are read from the hyphenated form: HYPH_STD where the exception has a hyphen, HYPH_INVALID elsewhere.

  3. Otherwise, the word is surrounded by dots, and for every substring of length 1 to 9 the pattern table is consulted. The digits of each matching pattern are stored in an array of the inter-letter positions, keeping the maximum. A position with an odd value is a hyphenation point (penalty HYPH_STD), any other position is forbidden (HYPH_INVALID).

  4. Finally, breaks after the first one or two characters and before the last one, two or three characters are forbidden. As implemented, a hyphenated word therefore keeps at least three characters before the hyphen and four after it. This restriction does not apply to exceptions.

std_hyphenate (s, after, left, right, penalty, utf8) performs the actual split: left receives the first after+1 characters followed by a hyphen -, and right receives the rest. In the utf8 case positions are counted in characters, where a <…> entity counts as one character. (If the given penalty is HYPH_INVALID, a backslash is appended instead of the hyphen; the line breaker never asks for such a split.)

3.Variants

Oriental and verbatim languages

Their get_hyphens forbids all breaks; lines are broken between the units returned by advance instead.

hyphenless_language

Forbids all breaks inside words; used by hgroup.

ad_hoc_language

Uses an explicit hyphenation for one word, given by the primitive hyphenate-as. Its break penalty is 0 rather than HYPH_STD, so these breaks are preferred to ordinary hyphenation points.

4.Use by the line breaker

The concatenation typesetter stores the language in every string line item. When the line breaker (

Typeset/Line/line_breaker.cpp

) looks for break points inside a word that does not fit, it calls item->lan->get_hyphens (s) and tries the positions with a penalty below HYPH_INVALID, splitting the item with item->lan->hyphenate. The penalty enters the cost of the line break. The details, including the influence of the par-hyphen variable, are in line breaking.

5.Pitfalls

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".