6 <expand|tmdoc-title|Mathematical typesetting>
10 In this chapter we describe the algorithms used by <apply|TeXmacs> in order
11 to typeset mathematical formulas. This is a difficult subject, because
12 esthetics and effectiveness do not always go hand in hand. Until now,
13 <apply|TeX> is widely accepted for having achieved an optimal compromise in
14 this respect. Nevertheless, we thought that several improvements could
15 still be made, which have now been implemented in <apply|TeXmacs>. We will
16 shortly describe the motivations behind them.
18 In order to obtain esthetic formulas, what criteria should we use? It is
19 often stressed that good typesetting allows the reader to concentrate on
20 what he reads, without being distracted by ugly typesetting details. Such
21 distracting details arise when distinct, though similar parts of text are
22 typesetted in a non uniform way:
25 <expand|item*|Different base lines.>The eye expects text of a similar
26 nature to be typesetted with respect to a same base line. For instance,
27 in <with|mode|math|x+y+z>, the bottoms of the <with|mode|math|x> and
28 <with|mode|math|z> should be at the same height as the bottom of the
29 <with|mode|math|u>-part in the <with|mode|math|y>. This should again be
30 the case in <with|mode|math|2<rsup|x>+2<rsup|y>+2<rsup|z>>.
32 <expand|item*|Unequal spacing.>Different components of text with
33 approximately the same function should be separated by equal amounts of
34 space. For instance, in <with|mode|math|a<rsup|2>+f<rsup|2>>, the
35 typesetter should notice the hangover of the <with|mode|math|f>. This
36 should again be the case in <with|mode|math|e<rsup|a>+e<rsup|f>+e<rsup|x>\
37 >. Similarly, the distance between the baselines of the
38 <with|mode|math|a> and the <with|mode|math|i> in
39 <with|mode|math|a<rsub|i>> should not be disproportially large with
40 respect to the height of an <with|mode|math|x>.
43 Additional difficulties may arise when considering automatically generated
44 formulas, in which case line breaking has to be dealt with in a
47 Unfortunately, the different esthetic criteria may enter into conflict with
48 each other. For instance, consider the formula
49 <with|mode|math|x<rsub|p>+x<rsub|p><rsup|2>>. On the one hand, the
50 baselines of the scripts should be the same, but the other hand, the first
51 subscript should not be ``disproportionally low'' with respect to the
52 <with|mode|math|x>. Unfortunately, this dilemma can not been solved in a
53 completely satisfactory way without the help of a human for the simple
54 reason that the computer has no way to know whether the
55 <with|mode|math|x<rsub|p>> and <with|mode|math|x<rsub|p><rsup|i>> are
56 ``related''. Indeed, if the <with|mode|math|x<rsub|p>> and
57 <with|mode|math|x<rsub|p><rsup|i>> are close (like in
58 <with|mode|math|x<rsub|p>+x<rsub|p><rsup|i>>), then it is natural to opt
59 for a common base line. However, if they are further away from each other
60 (like in <with|mode|math|x<rsub|p>+<big|sum><rsub|i=0><rsup|\<infty\>>c<rsu\
61 b|i>x<rsub|p><rsup|i>>), then we might want to opt for different base lines
62 and locally optimize the rendering of the first <with|mode|math|x<rsub|p>>.
64 Consequently, <apply|TeXmacs> should offer a reasonable compromise for the
65 most frequent cases, while offering methods for the user to make finer
66 adjustments in the remaining ones. Currently, we just provided the
67 <apply|menu|Insert|Special|Move> and <apply|menu|Insert|Special|Resize>
68 constructs to move and resize boxes in order to perform such adjustments.
69 For instance, if the brackets around the two sums
72 \<phi\><left|(><big|sum><rsub|i>a<rsub|i>x<rsup|i><right|)>=\<psi\><left|\
73 (><big|sum><rsub|j>b<rsub|j>y<rsup|j><right|)>
76 have different sizes, then one may resize the bottom of the subscript
77 <with|mode|math|j> of the second sum to <verbatim|0fn>. Alternatively, one
78 may resize the bottoms of both the <with|mode|math|i> and
79 <with|mode|math|j> subscripts to (say) <verbatim|-0.3fn>.
81 Notice that one should adjust by preference in a structural and not visual
82 way. For instance, one should prefer <verbatim|-0.3fn> to <verbatim|-2mm>
83 in the above example, because the second option disallows you to switch to
84 another font size for your document. Similarly, you should try not change
85 the semantics of the formula. For instance, in the above example, you might
86 have added a ``dummy subscript'' to the <with|mode|math|i> subscript of the
87 sum. However, this would alter the meaning of the formula (whence make it
88 non suitable as input to a computer algebra system) In the future, we plan
89 to provide additional constructs in order to facilitate structural
90 adjusting. For instance, in the case of a formula like
93 1+x<rsub|1>+x<rsub|1><rsup|2>+\<cdots\>+x<rsub|2>+x<rsub|1>x<rsub|2>+x<rs\
94 ub|1><rsup|2>x<rsub|2>+\<cdots\>x<rsub|2><rsup|2>+x<rsub|1>x<rsub|2><rsup\
95 |2>+x<rsub|1><rsup|2>x<rsub|2><rsup|2>+\<cdots\>,
98 one might think of a construct to enclose the entire formula into an area,
99 where all scripts are forced to be double (using dummy superscripts
100 whereever necessary).
102 <section|The font parameters>
104 Several font parameters are crucial for the correct positioning of the
105 different components. The following are often needed:
108 <expand|item*|<verbatim|quad>.>The main font reference space
109 <verbatim|1fn>, which can be taken as the distance between successive
112 <expand|item*|<verbatim|y1> and <verbatim|y2>.>The bottom and top level
113 for the font (we have <verbatim|y2-y1=quad>).
115 <expand|item*|<verbatim|sep>.>The reference minimal space between
116 distinct components, like the minimal distance between a subscript and a
117 superscript. In fact, <verbatim|sep=quad/10>.
119 <expand|item*|<verbatim|wline>.>The width of several types of lines, like
120 the fraction and square root bars, wide accents, etc.
122 <expand|item*|<verbatim|yfrac>.>The height of the fraction bar, which is
123 needed for the positioning of fractions and big delimiters. Usually,
124 <verbatim|yfrac> is almost equal to <verbatim|yx/2> below.
127 The following parameters are mainly needed in order to deal with scripts:
130 <expand|item*|<verbatim|yx>.>The height of the <with|mode|math|x>
131 character, which is needed for the positioning of scripts. All the
132 remaining parameters are actually computed as a function of
135 <expand|item*|<verbatim|ysub lo base>.>Logical base line for subscripts.
137 <expand|item*|<verbatim|ysub hi lim>.>Subscripts may never physically
138 exceed this top height.
140 <expand|item*|<verbatim|ysup lo base>.>Logical base line for
143 <expand|item*|<verbatim|ysup lo lim>.>Superscripts may never physically
144 exceed this bottom height.
146 <expand|item*|<verbatim|ysup hi lim>.>Suggestion for a physical top line
149 <expand|item*|<verbatim|yshift>.>Possible shift of the base lines when we
150 are inside fractions or scripts.
153 The individual strings in a font also have several important positioning
154 properties. First of all, they always admit left and right slopes.
155 Furthermore, they admit left and right italic corrections, which are needed
156 for the positioning of scripts or when passing from text in upright to text
157 in italics (or vice versa).
159 <section|Some major mathematical constructs>
161 <subsection|Fractions>
163 The following heuristics are used:
166 <item>The horizontal middles of the numerator and the denominator are
167 taken to be the same.
169 <item>The vertical spaces between the numerator resp. denominator and the
170 fraction bar is at least <verbatim|sep>.
172 <item>The depth (resp. height) of the numerator (resp. denominator) is
173 descended (resp. increased) to <verbatim|y1> (resp. <verbatim|y2>) if
174 necessary. This forces the base lines of not too large numerators resp.
175 denominators to be the same in presence of multiple fractions.
177 <item>The fraction bar has a overhang of <verbatim|sep/2> to both sides
178 and the logical limits of the fraction are another <verbatim|sep/2>
179 further. The logical left limit is zero.
182 The italic corrections are not taken into account during the positioning
183 algorithms, because this may create the impression that the numerator and
184 denominator are not correctly centered with respect to each other.
185 Nevertheless, the italic corrections are taken into account in order to
186 compute the logical bounding box of the fraction (whose has italic slopes
187 vanish at both sides).
191 The following heuristics are used:
194 <item>The vertical space between the main argument and the upper bar is
195 at least <verbatim|sep>.
197 <item>The root itself is typesetted like a large delimiter. The
198 positioning of a potential script works only is very dependent on the
199 usage of <apply|TeX> fonts.
201 <item>The upper bar has a overhang of <verbatim|sep/2> at the right and
202 the logical right limit of the root is situated another <verbatim|sep/2>
203 further to the right.
206 We take the logical right border plus the italic correction of the main
207 argument in order to determine the right hand limit of the upper bar. The
208 left italic correction is not needed.
210 <subsection|Negations>
212 The following heuristics are used:
215 <item>The negation bar passes through the logical center of the argument.
217 <item>The italic corrections of the argument are only taken into account
218 during the computation of the logical limits of the negation box (which
219 has zero left and right slopes).
222 <subsection|Wide boxes>
224 The following heuristics are used:
227 <item>We use <apply|TeX> fonts for small accents and an <with|font
228 shape|italic|ad hoc> algorithm for the wider ones.
230 <item>The distance between the main argument and the accent is at least
231 <verbatim|sep> (or a distance which depends on the <apply|TeX> font for
234 <item>The accent is positioned horizintally according to the right slope
235 of the main argument.
237 <item>The slopes for the accented box are inherited from those of the
238 main argument and the italic corrections are adjusted accordingly.
240 <item>All script height parameters of the accented box are inherited from
241 the main argument. The only exception is <verbatim|ysup_hi_lim>, which
242 may be increased by the height of the accent, or determined in the
243 generic way, whichever leads to the least value. It is indeed better to
244 keep superscripts positioned reasonably low, whenever possible.
247 <section|Subscripts and superscripts>
249 The positioning of subscripts and superscripts is a complicated affair, due
250 to the conflict between locally and globally optimal esthetics mentioned
251 above. The base line for a subscript is determined as follows:
254 <item>Always pretend that the subscript has height at least
255 <verbatim|y2-yshift> in the script font (actually we should use the
256 height of an <with|mode|math|M> instead).
258 <item>Try to position the script at the base line given by the main
261 <item>If the top limit (given by the main argument) is physically
262 exceeded by the subscript, then the base line is moved further down
266 The base line for a superscript is determined as follows:
269 <item>Try to physically position the superscript beneath the suggested
270 top line. Usually, this will place the superscript to far down.
272 <item>Move the superscript up to the logical base line if necessary. This
273 will usually occur: most of the time, the logical base line is the just
274 the height of an <with|mode|math|x>-script below the suggested top line.
276 <item>If the superscript physically descends below the physical under
277 limit given by the main box, then we move the superscript further
281 If both a subscript and a superscript were present, then we still have to
282 adjust the base lines: if the top of the subscript and the bottom of the
283 superscript are not physically separated by <verbatim|sep>, then we both
284 move the subscript and the superscript by the same amount away from each
285 other. Because of step 1 in the positioning of the subscript, the base
286 lines of double scripts will usually be the same in formulas with several
289 The right slope and italic correction of a script box may be non trivial.
290 In order to compute them, we first determine the script (or main argument),
291 whose right limit (taking into account its italic correction) is furthest
292 to the right (this may be the main box, in the case of a big integral with
293 a tiny subscript). Then the right slope of the main box is inherited by the
294 right slope of this script (or main argument). As to the italic correction,
295 it is precisely the difference between the right offset of the script plus
296 its italic correction minus the logical right coordinate of the entire box.
297 The italic correction should be at least zero though. The left slope and
298 italic correction are computed in a similar way.
300 <section|Big delimiters>
302 The automatic positioning and computation of sizes of big delimiters is
303 again complicated because of potential conflicts between locally and
304 globally optimal esthetics.
306 First of all, <apply|TeX> fonts come only with a discrete set of possible
307 sizes for large delimiters. This is an advantage from the point of view
308 that it favorites delimiters around slightly different expressions to have
309 the same baselines. However, it has the disadvantage that delimiters are
310 easily made ``one size to large''. For this reason, we actually diminish
311 the height and the depth of the delimited expression by the small amount
312 <verbatim|sep>, before computing the sizes of the delimiters.
314 Secondly, it is best when the vertical middles of big delimiters occur at
315 the height of fraction bars. However, in a formula like
318 f<left|(><frac|1|1+<frac|1|1+<frac|1|1+<frac|1|x>>>><right|)>,
321 it may be worth it to descend the delimiters a bit. On the other hand,
322 slight vertical shifts in the middles of the delimiters potentially have a
323 bad effect on base lines, like in
326 f<left|(><big|sum><rsub|i=1><rsup|b>X<rsub|i><right|)>+g<left|(><big|sum>\
327 <rsub|j=1><rsup|a>Y<rsub|j><right|)>.
330 In <apply|TeXmacs>, we use the following compromise: we start with the
331 middle of the delimited expression as a first approximation to the middle
332 of the delimiters. The real middle is obtained by shifting this middle
333 towards the height of fraction bars by an amount which cannot exceed
336 From a horizontal point of view, we finally have to notice that we adapted
337 the metrics of the big delimiters in a way that potential scripts are
338 positioned in a better way. For instance, according to the <apply|TeX>
339 <verbatim|tfm> file, in a formula like
342 <left|(>A+<left|(><big|sum><rsub|i=1><rsup|10>B<rsub|i><right|)><rsup|2><\
346 the square rather seems to be a left superscript of the second closing
347 bracket than a right superscript of the first one. This is particularly
348 annoying in the case of automatically generated formulas, where this
349 situation occurs quite often.
351 <apply|tmdoc-copyright|1998--2002|Joris van der Hoeven>
353 <expand|tmdoc-license|Permission is granted to copy, distribute and/or
354 modify this document under the terms of the GNU Free Documentation License,
355 Version 1.1 or any later version published by the Free Software Foundation;
356 with no Invariant Sections, with no Front-Cover Texts, and with no
357 Back-Cover Texts. A copy of the license is included in the section entitled
358 "GNU Free Documentation License".>
363 <associate|paragraph width|150mm>
364 <associate|odd page margin|30mm>
365 <associate|shrinking factor|4>
366 <associate|page right margin|30mm>
367 <associate|page top margin|30mm>
368 <associate|reduction page right margin|25mm>
369 <associate|page type|a4>
370 <associate|reduction page bottom margin|15mm>
371 <associate|even page margin|30mm>
372 <associate|reduction page left margin|25mm>
373 <associate|page bottom margin|30mm>
374 <associate|reduction page top margin|15mm>
375 <associate|language|english>
381 <associate|toc-10|<tuple|3.|?>>
382 <associate|idx-1|<tuple|<uninit>|?>>
383 <associate|toc-1|<tuple|<uninit>|?>>
384 <associate|toc-2|<tuple|<uninit>|?>>
385 <associate|idx-2|<tuple|<uninit>|?>>
386 <associate|toc-3|<tuple|<uninit>|?>>
387 <associate|toc-4|<tuple|<uninit>|?>>
388 <associate|toc-5|<tuple|<uninit>|?>>
389 <associate|toc-6|<tuple|<uninit>|?>>
390 <associate|toc-7|<tuple|<uninit>|?>>
391 <associate|toc-8|<tuple|<uninit>|?>>
392 <associate|toc-9|<tuple|3.|?>>