fonts-math.tex /size: 41 Kb    last modification: 2024-01-16 09:02
1% language=us runpath=texruns:manuals/fonts
2
3% todo:
4
5% \starttext
6%     \setupbodyfont[cambria]
7%     \setupmathematics[kernpairs=yes]
8%     $abcd$ % 𝑎𝑏
9% \stoptext
10%
11% kernpairs = {
12%     [0x1D44E] = {
13%         [0x1D44F] = 1000, -- 𝑎𝑏 demo
14%     }
15% },
16
17\startcomponent fonts-math
18
19\environment fonts-environment
20
21\startchapter[title=Math][color=darkmix-3]
22
23\startsection[title=Introduction]
24
25As one can expect, math support in \CONTEXT\ is to some extend modelled after
26what plain \TEX\ provides, plus what was later decided to be standard. This
27mostly concerns the way fonts behave and what names are used to access glyphs or
28special constructs. It means that when you come from another macro package you
29can stick to coding math the way you did before, at least the basic coding. In
30addition to this, \CONTEXT\ gives control over fonts, structure and rendering and
31most of that was either driven by personal need or user demand. To be honest,
32many of the options are probably of not much interest to the average user.
33
34As we focus on fonts we will only touch this aspect of math here. Right from when
35we started with developing \LUATEX, cleaning op the math part of \CONTEXT\ was
36part of the game. Some primitives were added that would make it possible to avoid
37unnecessary complex macros to get certain glyphs rendered, like radicals, accents
38and extensibles. This was made easy because we also support \OPENTYPE\ math and
39because we knew that eventually the Latin Modern and Gyre fonts would also
40support \OPENTYPE. In order to move forward and get rid of traditional eight bit
41fonts \CONTEXT\ \MKIV\ can construct a virtual \OPENTYPE\ font from traditional
42math fonts. It makes not much sense to discuss that here as by now this method is
43only provided for reasons of compatibility and a reference to the past. As a lot
44of time went into this it will always stay around if only to remind us of what
45we went through to get where we are now.
46
47\stopsection
48
49\startsection[title=\UNICODE\ math]
50
51Due to the limited amount of glyphs in a \TYPEONE\ font a macro package has to
52jump through loops in order to get traditional \TEX\ engines behave well. As a
53practical consequence these fonts are often a mixture of characters, symbols,
54punctuation and snippets that make up larger shapes. The font dimensions in these
55files have often special meanings too.
56
57This has all changed considerably with math being part of \UNICODE. It was however
58\MICROSOFT\ where the real action took place: the development of the first font that
59combined \UNICODE\ with \OPENTYPE\ technology. The Cambria font can be considered
60the benchmark for fonts that surfaced later. The characteristic of a math font are
61the following:
62
63\startitemize[packed]
64    \startitem All math alphabets are present: latin as well as greek, in regular,
65    italic, bold, fraktur and script variants as well as some combinations of these. \stopitem
66    \startitem The symbols that make sense are present (read: the more obscure shapes
67    can be omited). \stopitem
68    \startitem For the characters that make sense, there are two variants that render well
69    at smaller sizes: script and scriptscript. In the font they have the same size but
70    the application will scale them down. This feature is named \type {ssty}. \stopitem
71    \startitem Characters that can extend horizontally (for instance accents and arrows) or vertically
72    (like radicals and fences) have associated larger variants and carries information about
73    how to grow indefinitely. \stopitem
74    \startitem There is a whole lot of special math dimensions. Most of the ones
75    already used in \TEX\ are present. \stopitem
76    \startitem Some glyphs come in variants in order to please special usage. There
77    can also be variants for script or fraktur alphabets. \stopitem
78\stopitemize
79
80This means that in practice an \OPENTYPE\ math font is quite large. We easily
81have thousands of glyphs. It also means that creating such a font involves some
82expertise and this is one of the reasons why \TEX\ usergroups have joined forces
83in developing a suite of fonts. There are also other initiatives in the \TEX\
84community, of which Xits is an example. \footnote {This is a useable
85variant of Stix fonts with proper math features, some extra glyphs and
86experimental right||to||left shapes.} The well known Lucida Bright math font
87package has also been upgraded to a set of \OPENTYPE\ math fonts.
88
89The fact that there are not that many math fonts out there has a positive side as
90well: \CONTEXT\ comes with them pre|-|configured. Because during the development
91of \LUATEX\ we needed to have at least a couple of fonts for testing, and because
92it makes no sense to waste time on traditional fonts, the Latin Modern, Palatino,
93Times and a few more fonts were (and still are) provided as virtual \UNICODE\
94fonts.
95
96In a regular text font, what you key in is what you get out. So, when you've
97chosen a font with an italic shape, you get italic shapes, even if the smallcaps
98feature is enabled. In math, if you use the right unicodes you also get the shape
99you expect. Because in this case italic shapes are situated in one of the
100alphabets you explicitly choose a rendering. You can enter the right codepoints
101directly, so for instance if you enter \UNICODE\ character \type {U+1D434} you
102will get \mathematics {\utfchar {"1D434}}. In practice something like \type {$\bi
103A$} should also give that character if only because that is what we have been
104doing for over three decades. This means that the engine has to map a regular
105\type {A} onto the bold italic alphabet. In a traditional approach you will use
106math families for this, but in \CONTEXT\ \MKIV\ we simply use one font and one
107family and let the \MKIV\ machinery do the rest.
108
109In text mode we switch fonts styles in the following way:
110
111\startbuffer
112regular {\it italic} {\bf bold} {\bi bold italic} and so on
113\stopbuffer
114
115\typebuffer
116
117The three commands shown here are shortcuts for font switches. This input is
118converted into an internal representation and after whatever manipulations
119are applied end up as:
120
121\blank \getbuffer  \blank
122
123If we look at what fonts we end up with we get:
124
125\blank \markfonts{\getbuffer} \blank
126
127Now lets do the same in math mode:
128
129\startbuffer
130$regular {\it italic} {\bf bold} {\bi bold italic} and so on$
131\stopbuffer
132
133\typebuffer
134
135This time we get a different result:
136
137\blank \getbuffer \blank
138
139If again we analyze the fonts you see this:
140
141\blank \markfonts{\getbuffer} \blank
142
143All glyphs come from the same font. Instead of regular we get italic simply because
144math characters are italic by nature. The two character style switches are not
145really font switches but just make sure that the given input is mapped onto the
146right alphabet.
147
148A traditional approach using \TYPEONE\ fonts is to use a so called math family for
149each alphabet. In that case each alphabet maps one||to||one onto the font: when
150we switch to a bold family we just take the glyph that sits in that slot. In \MKIV\
151we have all characters in one family so behind the screens a given character is
152remapped.
153
154Now take a look at the following example:
155
156\startbuffer
157$text^{script^{scriptscript}}$
158\stopbuffer
159
160\typebuffer
161
162This renders to this, with the characters marked by font:
163
164\blank \markfonts{\getbuffer} \blank
165
166This time we have three different fonts: one for each of the three math sizes.
167But this representation is not entirely honest, because indeed we have three font
168instances for math, but the glyphs come from the same \OPENTYPE\ math font. We
169just load the same font three times, once for each size. In fact we load the
170font once, but use three copies, scaled accordingly to the relative scale the
171font prescribes.
172
173There is a whole bunch of commands to choose specific characters in math mode
174using a regular input. These are state switching commands.
175
176\def\SampleLine#1#2#3%
177  {\NC \type{#1}\space
178       \ifx#2\empty\else\type{#2}\fi\space
179       \ifx#3\empty\else\type{#3}\fi
180   \NC $#1 a$
181   \NC $#1 A$
182   \NC \NR}
183
184\starttabulate[|||||]
185\HL
186\SampleLine \mr            \empty  \empty
187\HL
188\SampleLine \mathdefault   \empty  \empty
189\SampleLine \mathscript    \empty  \empty
190\SampleLine \mathfraktur   \empty  \empty
191\SampleLine \mathblackboard\empty  \empty
192\HL
193\SampleLine \rm            \mathrm \empty
194\SampleLine \ss            \mathss \empty
195\SampleLine \tt            \mathtt \empty
196\HL
197\SampleLine \tf            \mathtf \tfmath
198\SampleLine \sl            \mathsl \slmath
199\SampleLine \it            \mathit \itmath
200\HL
201\SampleLine \bf            \mathbf \bfmath
202\SampleLine \bs            \mathbs \bsmath
203\SampleLine \bi            \mathbi \bimath
204\HL
205\stoptabulate
206
207As you can see here, some commands have synonyms. The short commands adapt
208themselves to text and mathmode, the longer ones are meant for use in math mode
209only.
210
211In text mode distinctive shapes are either a font property (the whole font looks
212that way) or a stylistic alternate (an extra feature of a font). In math mode we
213can have alternates, but in addition to the previously mentioned alphabet
214switchers we have a few more:
215
216\starttabulate[|||||]
217\HL
218\SampleLine \frak       \empty \empty
219\SampleLine \cal        \empty \empty
220\SampleLine \bbd        \empty \empty
221\SampleLine \blackboard \empty \empty
222\SampleLine \fraktur    \empty \empty
223\SampleLine \gothic     \empty \empty
224\HL
225\stoptabulate
226
227This chapter is not meant as an introduction to math but it is good to know
228that math font support in \CONTEXT\ is rather flexible. There are several
229mechanisms for remapping and converting characters and sequences into
230others and more is possible. Here is one:
231
232\startbuffer
233\startformula
234\reals {\mathbf R} \utfchar{"0211D} \utfchar{"1D411}
235\stopformula
236\stopbuffer
237
238\typebuffer \blank \getbuffer \blank
239
240Compare this to:
241
242\startbuffer
243\setupmathematics[symbolset=blackboard-to-bold]
244\startformula
245\reals {\mathbf R} \utfchar{"0211D} \utfchar{"1D411}
246\stopformula
247\stopbuffer
248
249\typebuffer \blank \start \getbuffer \stop \blank
250
251Greek is always troublesome because instead of regular text shapes math uses a
252few variants. Because in \UNICODE\ characters are only included once, we have
253gaps in the math alphabets but \MKIV\ will take care of this. \footnote {This is
254a typical example of where exceptions in a standard force all applications that
255deal with it have to implement tweaks.} Depending on the field an author has to
256choose between upright and italic greek:
257
258\startbuffer
259$\nabla \alpha \mathgreekupright \nabla \alpha \mathgreekitalic \nabla \alpha$
260\stopbuffer
261
262\typebuffer \blank \start \getbuffer \stop \blank
263
264By default \CONTEXT\ is set up as follows:
265
266\starttyping
267\setupmathematics
268  [sygreek=normal,
269   lcgreek=italic,
270   ucgreek=normal]
271\stoptyping
272
273Again, these are not features of a font. The font just provides the glyphs and
274the \TEX\ engine, controlled by \CONTEXT\ takes care of mapping characters to
275glyphs and building special constructs. The same is true for spacing. Although
276math fonts do have kerning information, most of the math spacing is controlled
277by properties of characters and not by the font.
278
279\startmkivmode
280
281\unexpanded\def\SampleLine#1%
282  {\NC
283   \type{$a #1{+} b$}
284   \NC
285   \ruledhbox{$\mathsurround\zeropoint a#1{+}b$}
286   \NC \NR}
287
288\starttabulate[|||]
289\SampleLine \mathord
290\SampleLine \mathpunct
291\SampleLine \mathinner
292\SampleLine \mathop
293\SampleLine \mathalpha
294\SampleLine \mathnothing
295\SampleLine \mathbin
296\SampleLine \mathrel
297\stoptabulate
298
299As a user you don't have to worry about these issues because characters are tagged
300according to their usage. \footnote {There are a few more commands, like \type
301{\mathlimop}, \type {\mathnolop} and \type {mathbox} but these are used
302differently.}
303
304\stopmkivmode
305
306With \TEX\ being the oldest and still dominant math renderer it is no surprise
307that \MICROSOFT\ modelled its math renderer after \TEX\ and Cambria quite well
308suits the concept. In retrospect it is somewhat unfortunate that we're still
309stuck with some left overs (or compromises) from the past with respect to spacing
310built into the font. However, as long as this is consistent over fonts it's not
311that relevant. You can always influence the spacing with the commands mentioned.
312
313If you look at the low level definitions in for instance plain \TEX\ but also in
314\CONTEXT\ \MKII\ that relate to prime symbols it probably takes a while before you
315figure out what happens there. For instance, the prime symbol is triggered by a
316quote and multiple in a row results in primes that are spaced tightly. In
317\UNICODE\ we have slots for single, double and tripple primes. Therefore, in
318\MKIV\ we have a mechanism that accepts different kinds of input that eventually
319all end up in one of these three glyphs.
320
321\unexpanded\def\SampleLine#1%
322  {\NC \type{#1} \NC #1 \NC \NR}
323
324\starttabulate[|||]
325\SampleLine{$f^2$}
326\SampleLine{$f\prime^2$}
327\SampleLine{$f\prime\prime^2$}
328\SampleLine{$f\prime\prime\prime^2$}
329\SampleLine{$f{\prime}^2$}
330\SampleLine{$f{\prime\prime}^2$}
331\SampleLine{$f{\prime\prime\prime}^2$}
332\SampleLine{$f'(x)$}
333\SampleLine{$f''(x)$}
334\SampleLine{$f'''(x)$}
335\SampleLine{$f\utfchar{0x2032}(x)$}
336\SampleLine{$f\utfchar{0x2033}(x)$}
337\SampleLine{$f\utfchar{0x2034}(x)$}
338\SampleLine{$f\utfchar{0x2032}\utfchar{0x2032}(x)$}
339\SampleLine{$f\utfchar{0x2032}\utfchar{0x2032}\utfchar{0x2032}(x)$}
340\SampleLine{$f\utfchar{0x2033}\utfchar{0x2032}(x)$}
341\SampleLine{$f\utfchar{0x2032}\utfchar{0x2033}(x)$}
342\stoptabulate
343
344Again, this is not the same as ligature building features in text fonts, but
345handled in a different way.
346
347The \TEX\ engine understands the concept of italic correction. When an italic
348shape is followed by for instance an upright shape, you can insert a \type {\/}
349and the engine will add a correction as defined in the font. In \OPENTYPE\ we
350don't have such corrections available but we can fake it, which is what the \type
351{itlc} feature in \CONTEXT\ does. However, you need to enable this feature
352explicitly. An example of a setup is:
353
354\starttyping
355\definefontfeature
356  [default]
357  [default]
358  [itlc=yes,textitalics=yes]
359
360\setupitaliccorrection
361  [global,always]
362\stoptyping
363
364This will make sure that the right amount of correction is added between
365italic shapes and non italics or boxes. Using \type {text} instead of
366\type {always} would limit the correction to glyphs only and leaving out
367the \type {global} would permit selective (grouped) usage at the cost
368of more runtime. There is no need for the \type {\/} here.
369
370In math we also can have italic correction but there it is built into the engine
371and in traditional \TEX\ no directives are needed. Italic correction properties
372in math fonts are somewhat troublesome as their application depends on what we're
373dealing with: symbols, super- and subscripts, etc. Because early versions of
374\LUATEX\ didn't handle all of it well, if only because the fonts were not yet okay,
375the \MKIV\ math handler provides a bit of control.
376
377\def\SampleLine#1#2%
378  {\NC #1
379   \NC \setupmathematics[italics=#1]\ruledhbox{$m$ t}
380   \NC \setupmathematics[italics=#1]\ruledhbox{$m$ {\it t}}
381   \NC \setupmathematics[italics=#1]\ruledhbox{t $m$ t $m$ {\it t}}
382   \NC #2
383   \NC \NR}
384
385\starttabulate[||||||]
386\SampleLine0{no correction}
387\SampleLine1{only apply italics when the font carries them}
388\SampleLine2{apply italics provided by the font or automatically calculated}
389\SampleLine3{apply italics based on an emwidth and character properties}
390\SampleLine4{use method 1 but fall back on 3 if needed}
391\stoptabulate
392
393Because we cannot rely on fonts too much, we default to method~3 which in practice
394works out well, so the setup is:
395
396\starttyping
397\setupmathematics
398  [italics=3]
399\stoptyping
400
401There are all kind of commands that can be used to build math constructs in such a
402way that super- and subscripts are consistently rendered. It goes beyond this
403chapter to discuss them and most users will never see or use those commands. The
404main message of the examples above is that text and math use different fonts and
405properties and therefore also different methods in rendering text or a formula.
406Even if the names of mechanisms are the same (like italics) you cannot assume
407that both modes do exactly the same.
408
409\stopsection
410
411\startsection[title=Bold math]
412
413If you look at what \UNICODE\ provides you will notice that there are quite some
414bold characters. First of all there are a bunch of alphabets and because bold is
415not present in the text part of \UNICODE\ these alphabets have no holes. Then
416there are some symbols that have special meaning.
417
418\startluacode
419local find = string.find
420local NC, NR = context.NC, context.NR
421
422context.starttabulate { "|Tl||l|" }
423for unicode, entry in table.sortedhash(characters.data) do
424    local description = entry.description
425    if find(description,"^MATHEMATICAL BOLD") then
426        NC() context("U+04X",unicode)
427        NC() context.mathematics(utf.char(unicode))
428        NC() context(description)
429        NC() NR()
430    end
431end
432context.stoptabulate()
433\stopluacode
434
435The biggest mistake one can make when discussing bold math is the assumption that
436these bold alphabets are meant for section titles and other structural elements
437that need some emphasis. This is not true, in that case we would expect the whole
438formula to be bold and the bold symbols or variables would be even more bold.
439Bold math boils down to {\em all} math being bold. The reason why we show the
440list of bold characters on the previous pages is that it gives a good impression of
441fact that we're mostly given alphabets in an otherwise regular font.
442
443As Latin Modern (being derived from Computer Modern) has some bold extras in
444\MKII\ to some extend we do support a complete bold math switch but mixing bold
445formulas with regular ones has some limitations. Math typesetting consists of two
446phases: first the input is translated into a special list where references to
447fonts are not yet resolved. Instead families are used and each family has three
448sizes: text, script and scriptscript. In a second pass the formula is typeset and
449the families get translated into fonts. So, if we change the definition of a
450family, say math italic into bold math italic, then the definition that is actual
451when the second pass takes place is used.
452
453Although \LUATEX\ provides for many more families and as a consequence we could
454have replaced the \MKII\ mechanism with a more complete one, instead we just
455forgot about it and stuck to one family for regular math and another one for bold
456math. Okay, this is not entirely true as later on we added some more in order to
457deal with bidirectional typesetting.
458
459Only a few math fonts come with a bold variant. One of the objectives of the \TEX
460Gyre math font project is to explore the possibilities of bold math companions,
461but such a font will probably have less coverage, simply because no real complex
462math will end up in for instance section titles.
463
464When I wrote this down there were not that many math fonts that come with a real
465(complete) bold variant. The \CONTEXT\ math font subsystem tries to fill this gap
466as good as possible by using pseudo fonts. When a typeface doesn't define a math
467bold variant a pseudo setup is used. When a real bold font is used, it could be
468that not all alphabets are supported in which case a suitable alternative is
469tried.
470
471The Xits font, assembed from Stix and enhanced by Khaled Hosny, comes with a bold
472variant but the coverage is not complete, at least not when I wrote this
473paragraph. This can go unnoticed because \CONTEXT\ tries to work around this. On
474the other hand, it definitely has bold properties, which can be seen from the
475next example. You switch between regular and bold math with the \type {\mr} and
476\type {\mb} commands.
477
478\startbuffer
479\switchtobodyfont[xitsbidi]
480
481$             \sqrt{x } \quad
482 \mb          \sqrt{mb} \quad
483 \mathupright \sqrt{u } \quad
484 \mr          \sqrt{mr} \quad
485 \mathupright \sqrt{u } \quad
486 \mathdefault \sqrt{d }
487$
488\stopbuffer
489
490\typebuffer \blank \start \getbuffer \stop \blank
491
492You can track some of what happens with:
493
494\starttyping
495\enabletrackers[math.remapping,math.families]
496\stoptyping
497
498You will get some information about remapping or when it fails if fallback
499remapping is used. But no matter what happens with glyphs, you will notice in
500this example that the radical symbol is bold indeed.
501
502\stopsection
503
504\startsection[title=Bidirectional math]
505
506There is not that much to tell about bidirectional math typesetting, simply
507because the fonts are still in development. However, Khaled Hosny added
508some support to the Xits font. Of course you need to load this font first:
509
510\starttyping
511\switchtobodyfont[xitsbidi]
512\stoptyping
513
514In the previous chapter we mentioned bold math and as Xits also comes with
515a bold variant which means that this command loads the whole lot (which is
516fast enough anyway).
517
518Easiest is to just show a few examples. When in left to right mode we get what we
519are accustomed to:
520
521\startbuffer
522\setupmathematics[align=l2r]
523
524\startformula
525\sqrt{x^2\over 4x} \eqno(1)
526\stopformula
527
528\startformula
5295 < 6 > 4
530\stopformula
531
532\startformula
5335 \leq 6 \geq 7
534\stopformula
535\stopbuffer
536
537\typebuffer \start \switchtobodyfont[xitsbidi] \getbuffer \stop
538
539However, when we go the other way, we automatically get digits converted to
540arabic.
541
542\startbuffer
543\setupmathematics[align=r2l,bidi=yes]
544
545\startformula
546\sqrt{ف^2\over 4ب} \eqno(1)
547\stopformula
548
549\startformula
5505 < 6 > 4
551\stopformula
552
553\startformula
5545 \leq 6 \geq 7
555\stopformula
556\stopbuffer
557
558\typebuffer \start \switchtobodyfont[xitsbidi] \getbuffer \stop
559
560You don't have to worry about how the font is set up, but not that much is needed
561because \CONTEXT\ does it for you and the Xits typescripts carries the right
562definitions. Just to give you an idea, we show a feature definition: The magic is
563in the \type {rtlm} feature combined with \type {locl}.
564
565\starttyping
566\definefontfeature
567  [mathematics-r2l]
568  [mathematics]
569  [language=ara,
570   rtlm=yes,
571   locl=yes]
572\stoptyping
573
574Some symbols are mirrored too:
575
576\startbuffer
577\setupmathematics[align=r2l,bidi=yes]
578
579\startformula
580\sum^\infty_{س=0} س^2 \eqno(2)
581\stopformula
582\stopbuffer
583
584\typebuffer \start \switchtobodyfont[xitsbidi] \getbuffer \stop
585
586And of course the extensible fences are done properly too:
587
588\startbuffer
589\setupmathematics[align=r2l,bidi=yes]
590
591\startformula
592\left(\root{2} \of{155}\right)
593\stopformula
594
595\startformula
596\left[\int^{55}_{123} 666^3\right]
597\qquad\textstyle\left[\int^{55}_{123} 666^3\right]
598\stopformula
599
600\startformula
601\left\{\sum^{55}_{123} 666^3\right\}
602\stopformula
603\stopbuffer
604
605\typebuffer \start \switchtobodyfont[xitsbidi] \getbuffer \stop
606
607The real torture test is the radical sign. A mirrored shape is used
608and it grows upwards as well as leftwards.
609
610\startbuffer
611\setupmathematics[align=r2l,bidi=yes]
612
613\startformula
614\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{55}}}}}}}}}}
615\stopformula
616\stopbuffer
617
618\typebuffer \start \switchtobodyfont[xitsbidi] \getbuffer \stop
619
620\stopsection
621
622\startsection[title=Styles]
623
624In text mode you use font switches like \type {\sl} that switches the current font
625to a slanted one. In math mode it is an alphabet switch in the same font. In
626fact, there isn't much to choose from fonts there, apart from a massive switch
627to bold, in which case \type {\bf} is just a bolder alphabet in that bolder font.
628
629A lot of things in math mode happen automatically. There are for instance always
630three instances of (the same) font active, each different in size: text, script
631and the smallest, scriptscript and when you ask for instance for a superscript
632the next smaller size is used.
633
634\starttabulate[|l|l|l|]
635\NC normal   \NC \type {\textstyle}         \NC $\textstyle         text^{script^{scriptscript}}$ \NC \NR
636\NC smaller  \NC \type {\scriptstyle}       \NC $\scriptstyle       text^{script^{scriptscript}}$ \NC \NR
637\NC smallest \NC \type {\scriptscriptstyle} \NC $\scriptscriptstyle text^{script^{scriptscript}}$ \NC \NR
638\stoptabulate
639
640In text style, superscripts can go twice smaller, but in script style only one
641smaller size is left, and in scriptscript style you're stuck with one size. The
642commands in the second column can be used to force a style.
643
644The math formula builder has an important property: the formula is typeset after
645it has been scanned completely. In a traditional setup that has some
646consequences. Take this:
647
648\starttyping
649one \sl two \bf three \bi four
650\stoptyping
651
652In a traditional setup four so called families are used and each character gets
653tagged with a family number. So we have (for instance):
654
655\blank \start \tttf
656o\low7n\low7e\low7 t\low6w\low6o\low6 t\low5h\low5r\low5e\low5e\low5 f\low9o\low9u\low9r\low9
657\stop \blank
658
659As the number of families was limited there could be at most 16 families. In
660fact, the first four were traditionally reserved for math roman, math italic,
661symbol and extensibles. Then, due to the limit of 256 characters per font,
662another few were used for additional symbol fonts. So, adding a few more variants
663could exhaust the family pool quite fast. You could argue that we could halfway
664redefine a family but this will not work as there is a one to one relationship
665between family numbers and fonts assigned to them when the formula has been
666read in (the last value counts). And grouping won't help you either.
667
668The actual (plain) situation is even more complex. As we have a limited number of
669characters per font, most symbols are accessed by name, and the name relates to a
670mathematical character definition using for instance \type {\mathchardef}. Such a
671definition refers to a slot in a specific family number and therefore font. It
672also puts a character in a so called math class. One of these, the alphanumeric
673class, with number~7, is special. Characters that are input directly on the
674keyboard (like \type {a}||\type {z} can also be tagged this way using \type
675{\mathcode}.
676
677When we switch a family, this will normally not affect a symbol defined as math
678character, simply because we refer to a specific family|/|slot combination, but
679when a character has class~7, then it will be taken from the current family. This
680permits latin letters, digits and greek letters to be typeset in different
681styles. So, in that traditional approach we have fonts that provide a bunch of
682symbols as well as some alphabets. Think for instance of a font with additional
683symbols where the regular alphabet slots contain blackboard shapes. The symbols
684are accessed directly and the characters are accessed via the regular \type
685{a}||\type {z} characters as these will adapt to the family and therefore font.
686In practice users will not notice this complication as macro packages hide the
687implementation details.
688
689In \MKIV\ the situation is different as there we have one family (or a few more
690if we use a full bold switch and|/|or bidirectional math). Although we no longer
691have the limit of 16 fonts we actually don't need that many families, at least
692not in the way we've set up \MKIV \footnote{A technical note: in principle the
693\MKIV\ approach can have a speed penalty compared to a multi||family approach but
694we don't care too much about it. Also, as we load less fonts the extra overhead gets
695compensated nicely.}
696
697\blank \start \tttf
698o\low1n\low1e\low1 t\low1w\low1o\low1 t\low1h\low1r\low1e\low1e\low1 f\low1o\low1u\low1r\low1
699\stop \blank
700
701So how does this relate to styles? Each family has three fonts and we can use the
702switch commands to choose any of these. In text mode we use the term style for a
703font switch, while in math mode it's more than that: indeed we switch a font, but
704only in size, but the spacing is also adapted. If a proper math font is used, the
705smaller sizes are actually alternates in the font, visually adapted to suit their
706use.
707
708In text mode we do this in order to limit the scope of a switch:
709
710\starttyping
711normal {\bf bold {\it italic} bold} normalbracket
712\stoptyping
713
714This is the same as:
715
716\starttyping
717normal \bgroup \bf bold \bgroup \it italic\egroup
718    \ bold\egroup \ normalbracket
719\stoptyping
720
721and:
722
723\starttyping
724normal \begingroup \bf bold \begingroup \it italic\endgroup
725    \ bold\endgroup \ normalbracket
726\stoptyping
727
728The \CONTEXT\ distribution ships with a plain math definition file that also uses
729one family but reassigns some math codes when we switch to another style. As the
730number of characters that this applies to this is efficient enough for a modern
731computer. A peek into \type {luatex-math.tex} gives an impression of what we deal
732with. However, keep in mind that the implementation in \MKIV\ goes it differently
733and is therefore more powerful. We also have hardly any definitions at the \TEX\
734end and use information from \type {char-def.lua} instead.
735
736In math mode there is a subtle difference in the way grouping works with styles:
737
738\starttyping
739text {\scriptstyle script} normal
740\stoptyping
741
742This is the same as:
743
744\starttyping
745text \bgroup\scriptstyle script\egroup\ normal
746\stoptyping
747
748but different from:
749
750\starttyping
751text \begingroup\scriptstyle script\endgroup\ script
752\stoptyping
753
754This has to do with the fact that a style switch is explicitly registered in the
755math list and grouping like this is not limiting the scope. In math mode the
756braced grouping mode actually does create a math group and there the scope of the
757switch is limited to that group. In practice users will not run into this but
758they can use macros that use \type {\begingroup}. Among other reasons, this is
759why we have a special mathstyle mechanism.
760
761\startbuffer
762\ruledhbox{$x\begingroup\scriptstyle x\endgroup x$} \quad
763\ruledhbox{$x\begingroup\setupmathstyle[script]x\endgroup x$} \quad
764\ruledhbox{$x{\setupmathstyle[script]x}x$} \quad
765\ruledhbox{$x\startmathstyle[script]x\stopmathstyle x$}
766\stopbuffer
767
768\typebuffer
769
770This gives:
771
772\startlinecorrection[blank] \dontleavehmode \getbuffer \stoplinecorrection
773
774Mechanisms that support the \type {mathstyle} parameter know how to apply the
775proper grouping so you don't have to worry there. You can best avoid using the
776verbose grouping command and stick to braces or the \type {start}||\type {stop}
777command. An example is the fence mechanism:
778
779\startbuffer
780\definemathfence
781  [fancybracket] [bracket]
782  [color=darkblue]
783\definemathfence % mkiv: command, mkxl: define
784  [smallbracket] [bracket]
785  [command=yes,define=yes,color=darkgreen,mathstyle=small]
786\definemathfence % mkiv: command, mkxl: define
787  [normalbracket] [bracket]
788  [command=yes,define=yes,color=darkred]
789\stopbuffer
790
791\typebuffer \getbuffer
792
793We apply this to an example:
794
795\startbuffer
796$x \fenced[bar]{\frac{1}{x}} x$ \quad
797$x \fenced[doublebar]{\frac{1}{x}} x$ \quad
798$x \fenced[bracket]{\frac{1}{x}} x$ \quad
799$x \fenced[fancybracket]{\frac{1}{x}} x$ \quad
800$x \frac{1}{n} \normalbracket{\frac{1}{n}} \smallbracket{\frac{1}{s}} x$
801\stopbuffer
802
803\typebuffer
804
805Of course these somewhat weird examples are not real but at least they
806demonstrate the principles.
807
808\startlinecorrection[blank] \dontleavehmode \getbuffer \stoplinecorrection
809
810A math style is a combination of the following keys. Their effect can depend on
811the current state, for instance you can switch cramp or size indepently.
812
813\starttabulate[|T||]
814\NC display          \NC display style, like text style but somewhat more spacy \NC \NR
815\NC text             \NC text style, normally used inline \NC \NR
816\NC script           \NC smaller than text cq. display style \NC \NR
817\NC scriptscript     \NC smaller than script style \NC \NR
818\NC cramped packed   \NC more tightly positioned superscripts \NC \NR
819\NC uncramped normal \NC normal positioned superscripts \NC \NR
820\NC small            \NC switch to the next smaller style but keep cramp state \NC \NR
821\NC big              \NC switch to the next larger style but keep cramp state \NC \NR
822\stoptabulate
823
824Future versions of \MKIV\ will provide more features (like parameter sets driven
825by keywords). As you might prefer a more symbolic approach we provide:
826
827\starttyping
828\definemathstyle[default][text,cramped]
829\stoptyping
830
831After this you can use the keyword \type {default} which has the advantage that
832you only need to change one definition in order to get different rendering.
833
834\stopsection
835
836\startsection[title=Supported fonts]
837
838As in \CONTEXT\ MKIV\ I wanted to go ahead with \UNICODE\ math as soon as the
839first version of \LUATEX\ showed up. Because at that time only Cambria was
840available I decided to provide virtual \UNICODE\ math fonts as a prelude to
841proper replacements for the popular \TYPEONE\ math fonts. In the meantime Xits
842came around and in 2012 we had quite useable math companions for the public Latin
843Modern, Pagella and Termes fonts and the \TEX\ user groups started shipping
844\OPENTYPE\ variants of Lucida. The virtual variants will still around so that we
845can compare them with the new implementations. As the official specification of
846\OPENTYPE\ math is not always clear from the beginning the \OPENTYPE\ fonts get
847improved over time. In fact, this is true not only for math fonts. Just think of
848this:
849
850\startitemize
851
852\startitem As \UNICODE\ gets extended, fonts might get more glyphs and possibly
853alternate shapes. \stopitem
854
855\startitem The more languages are supported, the more glyphs are to be available
856and features have to get language dependent instances. \stopitem
857
858\startitem The larger the font, the bigger the chance that mistakes get unnoticed
859especially when contextual subtitutions and positioning are used. \stopitem
860
861\startitem Math fonts can get more script and scriptscript alternates, more size
862variants, more advanced extensibles, bidirectional support, etc. \stopitem
863
864\stopitemize
865
866So, like regular programs, \LUATEX\ and macro packages, we now have fonts as
867component that needs occasional updating. Of course resources like hyphenation
868patterns are also subjected to this, so it's not a new aspect. But still, best
869keep en eye on font updates.
870
871While there are lots of text fonts, there are not that many math fonts, so you
872can safely assume that \CONTEXT\ ships with the proper setup for those fonts. Of
873course you have to choose a specific instance when you set up your own
874combination of fonts, but a peek into the typescripts shows the way.
875
876In the font manual and on the wiki you can find more about typescript and what is
877possible, so here we just take a look at one definition:
878
879\startnarrowtyping
880\starttypescript [serif] [dejavu] [name]
881  \definefontsynonym [Serif]           [name:dejavuserif]           [features=default]
882  \definefontsynonym [SerifBold]       [name:dejavuserifbold]       [features=default]
883  \definefontsynonym [SerifItalic]     [name:dejavuserifitalic]     [features=default]
884  \definefontsynonym [SerifBoldItalic] [name:dejavuserifbolditalic] [features=default]
885\stoptypescript
886
887\starttypescript [sans] [dejavu] [name]
888  \definefontsynonym [Sans]           [name:dejavusans]            [features=default]
889  \definefontsynonym [SansBold]       [name:dejavusansbold]        [features=default]
890  \definefontsynonym [SansItalic]     [name:dejavusansoblique]     [features=default]
891  \definefontsynonym [SansBoldItalic] [name:dejavusansboldoblique] [features=default]
892\stoptypescript
893
894\starttypescript [mono] [dejavu] [name]
895  \definefontsynonym [Mono]           [name:dejavusansmono]            [features=none]
896  \definefontsynonym [MonoBold]       [name:dejavusansmonobold]        [features=none]
897  \definefontsynonym [MonoItalic]     [name:dejavusansmonooblique]     [features=none]
898  \definefontsynonym [MonoBoldItalic] [name:dejavusansmonoboldoblique] [features=none]
899\stoptypescript
900
901\starttypescript[dejavu]
902  \definetypeface [dejavu] [rm] [serif] [dejavu] [default]
903  \definetypeface [dejavu] [ss] [sans]  [dejavu] [default]
904  \definetypeface [dejavu] [tt] [mono]  [dejavu] [default]
905  \definetypeface [dejavu] [mm] [math]  [xits]   [default] [scale=1.2]
906\stoptypescript
907\stopnarrowtyping
908
909So, in many cases you can just copy this blob and replace the font names by your
910own.
911
912Loading a font, and Dejavu is a predefined one, is done as follows:
913
914\starttyping
915\setupbodyfont[dejavu]
916\stoptyping
917
918In a similar fashion you can enable \type {cambria}, \type {pagella}, \type
919{termes}, \type {lucidaot}, etc.\ and if you don't use this command at all, you
920get Latin Modern. These fonts are part of \TEX\ distributions, including
921\CONTEXT\ stand||alone that can be downloaded from \CONTEXT\ garden.
922
923If you want to use Lucida, all you have to do when you have bought the fonts, is
924to put the \OPENTYPE\ files in a place where they can be found, for instance:
925
926\starttyping
927tex/texmf-fonts/fonts/data/lucida
928\stoptyping
929
930Of course you need to run \type {mtxrun --generate} afterwards so that the files
931can be found.
932
933\startnotabene
934    Tracing and characters coverage will be discussed here as soon as the styles
935    that are used for them are normalized.
936\stopnotabene
937
938\stopsection
939
940\startsection[title={Stylistic alternates}]
941
942Some fonts provide stylistic alternates. These can be described in goodies files
943and the Lucida setup is a good example. Here we demonstrate the effects. We
944disable the default math rendering (which takes the italic variants).
945
946\startbuffer[sa:1]
947\switchtobodyfont[lucidaot,14.4pt]
948\setupmathrendering[lucidaot][it=]
949$x
950   ^{i \leftarrow 0 = }
951   _{i \leftarrow 0 = }
952$
953\stopbuffer
954
955\typebuffer[sa:1]
956
957The next code enabled three alternatives:
958
959\startbuffer[sa:2]
960\switchtobodyfont[lucidaot,14.4pt]
961\setupmathrendering[lucidaot][it=]
962$x
963   ^{i \leftarrow 0 = }
964   _{\setmathfontalternate{arrow}
965     \setmathfontalternate{dotless}
966     \setmathfontalternate{zero}
967     i \leftarrow 0 = }
968$
969\stopbuffer
970
971\typebuffer[sa:2]
972
973Here we set them in one go:
974
975\startbuffer[sa:3]
976\switchtobodyfont[lucidaot,14.4pt]
977\setupmathrendering[lucidaot][it=]
978$x
979   ^{i \leftarrow 0 = }
980   _{\setmathfontalternate{arrow,dotless,zero}
981     i \leftarrow 0 = }
982$
983\stopbuffer
984\
985\typebuffer[sa:3]
986
987The last example shows how to enable these features globally:
988
989\startbuffer[sa:4]
990\switchtobodyfont[lucidaot,14.4pt]
991\setupmathrendering[lucidaot][it=]
992\setupmathematics[stylealternative={arrow,dotless,zero}]
993$x
994   ^{i \leftarrow 0 = }
995   _{i \leftarrow 0 = }
996$
997\stopbuffer
998
999\typebuffer[sa:4]
1000
1001The results are collected here:
1002
1003\startlinecorrection[blank]
1004\startcombination[4*1]
1005    {\vbox{\hsize.2\hsize\midaligned{\nospacing\getbuffer[sa:1]}}} {\bf nothing}
1006    {\vbox{\hsize.2\hsize\midaligned{\nospacing\getbuffer[sa:2]}}} {\bf stepwise}
1007    {\vbox{\hsize.2\hsize\midaligned{\nospacing\getbuffer[sa:3]}}} {\bf combined}
1008    {\vbox{\hsize.2\hsize\midaligned{\nospacing\getbuffer[sa:4]}}} {\bf global}
1009\stopcombination
1010\stoplinecorrection
1011
1012\stopsection
1013
1014\startsection[title=Italics and limits]
1015
1016An \OPENTYPE\ font treats italic correction differently from traditional fonts.
1017Officially the italic correction is used for placement above and below limits
1018where the scripts shift left and right half of the correction from the center of
1019the shape. Advanced kerns are then to be used for anchoring the scripts when they
1020are placed at the right side (so far no fonts seem to do this). Because we cannot
1021foresee if fonts compensate for correction then we can control placement a bit.
1022There is a parameter \type {\mathnolimitsmode} that controls the correction.
1023
1024\definebodyfontenvironment[20pt]
1025
1026\startlinecorrection
1027\startcombination[5*1]
1028    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[20pt]\mathnolimitsmode0$\displaystyle\int\nolimits^0_1$\hss}} {\tttf 0}
1029    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[20pt]\mathnolimitsmode1$\displaystyle\int\nolimits^0_1$\hss}} {\tttf 1}
1030    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[20pt]\mathnolimitsmode2$\displaystyle\int\nolimits^0_1$\hss}} {\tttf 2}
1031    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[20pt]\mathnolimitsmode3$\displaystyle\int\nolimits^0_1$\hss}} {\tttf 3}
1032    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[20pt]\mathnolimitsmode4$\displaystyle\int\nolimits^0_1$\hss}} {\tttf 4}
1033\stopcombination
1034\stoplinecorrection
1035
1036A value larger than 15 is interpreted as a factor (in the usual \TEX\ way 1000
1037means 1.0). We have some values left for future use when correction is to be
1038combined with kerns.
1039
1040In \CONTEXT\ we set the value to 1 which means that the factors for super- and
1041subscript are set via math parameters (or constants in the font). We use a
1042default of \type {{0,800}} so we don't shift the superscript and the subscript we
1043shift less than the italic correction. This is driven by a feature but you can
1044change the values before loading a font, for instance with:
1045
1046\starttyping
1047\adaptfontfeature[*math*][mathnolimitsmode={100,700}]
1048\stoptyping
1049
1050The defaults come out as:
1051
1052\startlinecorrection
1053\startcombination[5*1]
1054    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[modern,  20pt]$\displaystyle\int\nolimits^0_1$\hss}} {\tttf modern}
1055    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[xits,    20pt]$\displaystyle\int\nolimits^0_1$\hss}} {\tttf xits}
1056    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[lucidaot,20pt]$\displaystyle\int\nolimits^0_1$\hss}} {\tttf lucidaot}
1057    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[pagella, 20pt]$\displaystyle\int\nolimits^0_1$\hss}} {\tttf pagella}
1058    {\ruledhbox to .15\hsize{\hss\showglyphs\switchtobodyfont[cambria, 20pt]$\displaystyle\int\nolimits^0_1$\hss}} {\tttf cambria}
1059\stopcombination
1060\stoplinecorrection
1061
1062\stopsection
1063
1064\startsection[title=Kerning]
1065
1066Math kerns in \OPENTYPE\ are quite advanced and use a staircase model left
1067and|/|or right of characters. However, hardly any math font implements them
1068(extensively). Therefore we provide a way to fine tune your fonts to your
1069preferences. You can test this mechanism by patching \type {cambria-math.lfg} by
1070adding this to the mathematics subtable:
1071
1072\starttyping
1073kernpairs = {
1074    [0x1D44E] = {
1075        [0x1D44F] = 1000, -- 𝑎𝑏 demo
1076    }
1077}
1078\stoptyping
1079
1080After that, the next example should work:
1081
1082\starttyping
1083\starttext
1084    \setupbodyfont[cambria]
1085    \setupmathematics[kernpairs=yes]
1086    $abcd$
1087\stoptext
1088\stoptyping
1089
1090There should be a gap between the $𝑎$ and $𝑏$. It is not shown here because I
1091don't want to mess up my goodie file.
1092
1093\stopsection
1094
1095\stopchapter
1096
1097\stopcomponent
1098