![]() |
Mathematical typesetting | ![]() |
In this chapter we describe the algorithms used by TeXmacs in order to typeset mathematical formulas. This is a difficult subject, because esthetics and effectiveness do not always go hand in hand. Until now, TeX is widely accepted for having achieved an optimal compromise in this respect. Nevertheless, we thought that several improvements could still be made, which have now been implemented in TeXmacs. We will shortly describe the motivations behind them.
In order to obtain esthetic formulas, what criteria should we use? It is often stressed that good typesetting allows the reader to concentrate on what he reads, without being distracted by ugly typesetting details. Such distracting details arise when distinct, though similar parts of text are typeset in a non uniform way:
The eye expects text of a similar nature to be typeset with respect to a same base line. For instance, in \(x + y + z\), the bottoms of the \(x\) and \(z\) should be at the same height as the bottom of the \(u\)-part in the \(y\). This should again be the case in \(2^x + 2^y + 2^z\).
Different components of text with approximately the same function should be separated by equal amounts of space. For instance, in \(a^2 + f^2\), the typesetter should notice the hangover of the \(f\). This should again be the case in \(e^a + e^f + e^x\). Similarly, the distance between the baselines of the \(a\) and the \(i\) in \(a_i\) should not be disproportionally large with respect to the height of an \(x\).
Additional difficulties may arise when considering automatically generated formulas, in which case line breaking has to be dealt with in a satisfactory way.
Unfortunately, the different esthetic criteria may enter into conflict with each other. For instance, consider the formula \(x_p + x_p^2\). On the one hand, the baselines of the scripts should be the same, but the other hand, the first subscript should not be “disproportionally low” with respect to the \(x\). Unfortunately, this dilemma can not been solved in a completely satisfactory way without the help of a human for the simple reason that the computer has no way to know whether the \(x_p\) and \(x_p^i\) are “related”. Indeed, if the \(x_p\) and \(x_p^i\) are close (like in \(x_p + x_p^i\)), then it is natural to opt for a common base line. However, if they are further away from each other (like in \(x_p + \sum_{i = 0}^{\infty} c_i x_p^i\)), then we might want to opt for different base lines and locally optimize the rendering of the first \(x_p\).
Consequently, TeXmacs should offer a reasonable compromise for the most frequent cases, while offering methods for the user to make finer adjustments in the remaining ones. We provide the constructs Format\(\rightarrow\)Transform\(\rightarrow\)Move and Format\(\rightarrow\)Transform\(\rightarrow\)Resize (tags move and resize) to move and resize boxes in order to perform such adjustments. For instance, if the brackets around the two sums
have different sizes, then one may resize the bottom of the subscript \(j\) of the second sum to 0fn. Alternatively, one may resize the bottoms of both the \(i\) and \(j\) subscripts to (say) -0.3fn. For easier adjustments you may use Format\(\rightarrow\)Transform\(\rightarrow\)Smash and Format\(\rightarrow\)Transform\(\rightarrow\)Inflate to automatically adjust the size of the contents to the height of the character “x” and the largest one in the font respectively.
Notice that one should adjust by preference in a structural and not visual way. For instance, one should prefer -0.3fn to -2mm in the above example, because the second option disallows you to switch to another font size for your document. Similarly, you should try not change the semantics of the formula. For instance, in the above example, you might have added a “dummy subscript” to the \(i\) subscript of the sum. However, this would alter the meaning of the formula (whence make it non suitable as input to a computer algebra system) In the future, we plan to provide additional constructs in order to facilitate structural adjusting. For instance, in the case of a formula like
one might think of a construct to enclose the entire formula into an area, where all scripts are forced to be double (using dummy superscripts wherever necessary).
Several font parameters are crucial for the correct positioning of the different components. They are stored as fields of the class font_rep (see
Graphics/Fonts/font.hpp
and the chapter on fonts). The following are often needed:
The main font reference space 1fn, which can be taken as the distance between successive lines of text.
The bottom and top level for the font (for TeX fonts, we have y2-y1=wfn).
The reference minimal space between distinct components, like the minimal distance between a subscript and a superscript. In fact, sep=wfn/10.
The width of several types of lines, like the fraction and square root bars, wide accents, etc.
The height of the fraction bar, which is needed for the positioning of fractions and big delimiters. For TeX fonts, yfrac is equal to yx/2 below; for Unicode fonts, it is the vertical middle of the minus sign.
The following parameters are mainly needed in order to deal with scripts:
The height of the \(x\) character, which is needed for the positioning of scripts. All the remaining parameters are actually computed as a function of yx.
Logical base line for subscripts.
Subscripts may never physically exceed this top height.
Logical base line for superscripts.
Superscripts may never physically exceed this bottom height.
Suggestion for a physical top line for superscripts.
Possible shift of the base lines when we are inside fractions or scripts.
A font with an OpenType MATH table (ot_math) carries about forty further parameters, read from the constants of the table: fraction, radical, limit and stretch stack geometry, script shifts, drops and gaps, script percentages, accent base heights, and the over- and underbar constants. When they are set, the constructs below take their geometry from them; hand-tuned tables (TeX Gyre, Stix) keep precedence wherever they say something. See mathematics from the MATH table.
The individual strings in a font also have several important positioning properties. First of all, they always admit left and right slopes. Furthermore, they admit left and right italic corrections, which are needed for the positioning of scripts or when passing from text in upright to text in italics (or vice versa). These are computed by the virtual methods get_left_slope, get_right_slope, get_left_correction, get_right_correction of the font and, at the level of boxes, by the methods left_slope, right_slope, left_correction, right_correction, lsub_correction, rsup_correction, etc. of box_rep. More recent versions of TeXmacs also implement font-specific microtypographic corrections for the positioning of scripts and wide accents (see the files Plugins/Freetype/adjust_*.cpp). For OpenType math fonts the corrections may also depend on the height at which the script sits (get_rsup_correction_at and its siblings, rsup_correction_at at the level of boxes), which is how the cut-in kerns of the MATH table are applied, and accents are placed at the top accent attachment of the glyph (get_top_accent, top_accent).
The following heuristics are used:
The horizontal middles of the numerator and the denominator are taken to be the same.
The vertical spaces between the numerator resp. denominator and the fraction bar is at least sep.
The depth (resp. height) of the numerator (resp. denominator) is descended (resp. increased) to y1 (resp. y2) if necessary. This forces the base lines of not too large numerators resp. denominators to be the same in presence of multiple fractions.
The fraction bar has a overhang of sep/2 to both sides and the logical limits of the fraction are another sep/2 further. The logical left limit is zero.
The italic corrections are not taken into account during the positioning algorithms, because this may create the impression that the numerator and denominator are not correctly centered with respect to each other. Nevertheless, the italic corrections are taken into account in order to compute the logical bounding box of the fraction (whose has italic slopes vanish at both sides).
The following heuristics are used:
The vertical space between the main argument and the upper bar is at least sep.
The root itself is typeset like a large delimiter. The positioning of a potential script (the index of the root) depends on the font, with special cases for some math fonts (see sqrt_box_rep in
math_boxes.cpp
).
The upper bar has a overhang of sep/2 at the right and the logical right limit of the root is situated another sep/2 further to the right.
We take the logical right border plus the italic correction of the main argument in order to determine the right hand limit of the upper bar. The left italic correction is not needed.
The following heuristics are used:
The negation bar passes through the logical center of the argument.
The italic corrections of the argument are only taken into account during the computation of the logical limits of the negation box (which has zero left and right slopes).
The following heuristics are used:
We use the accents of the font for small accents and extensible glyphs or an ad hoc algorithm for the wider ones.
The distance between the main argument and the accent is at least sep (or a distance which depends on the font for small accents).
The accent is positioned horizontally according to the right slope of the main argument, with an additional font-specific correction (wide_correction).
The slopes for the accented box are inherited from those of the main argument and the italic corrections are adjusted accordingly.
All script height parameters of the accented box are inherited from the main argument. The only exception is ysup_hi_lim, which may be increased by the height of the accent, or determined in the generic way, whichever leads to the least value. It is indeed better to keep superscripts positioned reasonably low, whenever possible.
The positioning of subscripts and superscripts is a complicated affair, due to the conflict between locally and globally optimal esthetics mentioned above. The base line for a subscript is determined as follows:
Always pretend that the subscript has height at least y2-yshift in the script font (actually we should use the height of an \(M\) instead).
Try to position the script at the base line given by the main argument.
If the top limit (given by the main argument) is physically exceeded by the subscript, then the base line is moved further down accordingly.
The base line for a superscript is determined as follows:
Try to physically position the superscript beneath the suggested top line. Usually, this will place the superscript to far down.
Move the superscript up to the logical base line if necessary. This will usually occur: most of the time, the logical base line is the just the height of an \(x\)-script below the suggested top line.
If the superscript physically descends below the physical under limit given by the main box, then we move the superscript further upwards.
If both a subscript and a superscript were present, then we still have to adjust the base lines: if the top of the subscript and the bottom of the superscript are not physically separated by sep, then we both move the subscript and the superscript by the same amount away from each other. Because of step 1 in the positioning of the subscript, the base lines of double scripts will usually be the same in formulas with several of them.
The right slope and italic correction of a script box may be non trivial. In order to compute them, we first determine the script (or main argument), whose right limit (taking into account its italic correction) is furthest to the right (this may be the main box, in the case of a big integral with a tiny subscript). Then the right slope of the main box is inherited by the right slope of this script (or main argument). As to the italic correction, it is precisely the difference between the right offset of the script plus its italic correction minus the logical right coordinate of the entire box. The italic correction should be at least zero though. The left slope and italic correction are computed in a similar way.
The automatic positioning and computation of sizes of big delimiters is again complicated because of potential conflicts between locally and globally optimal esthetics.
First of all, fonts come only with a discrete set of possible sizes for large delimiters (see the section on string encodings in the chapter on fonts), before the delimiters are assembled from pieces; for an OpenType math font, these are the size variants and the glyph assemblies of its MATH table (see stretchable glyphs). This is an advantage from the point of view that it favorites delimiters around slightly different expressions to have the same baselines. However, it has the disadvantage that delimiters are easily made “one size too large”. For this reason, we actually diminish the height and the depth of the delimited expression by the small amount sep/2, before computing the sizes of the delimiters.
Secondly, it is best when the vertical middles of big delimiters occur at the height of fraction bars. However, in a formula like
it may be worth it to descend the delimiters a bit. On the other hand, slight vertical shifts in the middles of the delimiters potentially have a bad effect on base lines, like in
In TeXmacs, we use the following compromise: we start with the middle of the delimited expression as a first approximation to the middle of the delimiters. We then try to make the delimiters symmetric with respect to the middle of a delimiter of normal size (which is close to the height of fraction bars), by extending them at the top or at the bottom, but the corresponding shift of the middle may not exceed 2 sep. This algorithm is implemented in concater_rep::handle_matching in
Typeset/Concat/concat_post.cpp
.
From a horizontal point of view, we finally have to notice that we adapted the metrics of the big delimiters in a way that potential scripts are positioned in a better way. For instance, according to the TeX tfm file, in a formula like
the square rather seems to be a left superscript of the second closing bracket than a right superscript of the first one. This is particularly annoying in the case of automatically generated formulas, where this situation occurs quite often.
The mathematical constructs are typeset by the routines in
Typeset/Concat/concat_math.cpp
(fractions, roots, scripts, big operators, delimiters, wide accents, etc.), which produce the boxes implemented in
Typeset/Boxes/Composite/math_boxes.cpp
(fractions, roots, negations, wide accents) and
Typeset/Boxes/Composite/script_boxes.cpp
(scripts and limits). Brackets are resized in a post-processing step, in
Typeset/Concat/concat_post.cpp
, once the entire line is known. The spacing between symbols depends on their types (operators, relations, etc.), as provided by the mathematical language (see
System/Language/math_language.cpp
and the grammar
progs/language/std-math.scm
), and on spacing tables provided by the font. Notice that the directory
Graphics/Mathematics
is unrelated to mathematical typesetting: it contains generic templates for polynomials, vectors, matrices and formal expressions.