about-mathstyles.tex /size: 14 Kb    last modification: 2023-12-21 09:43
1% language=us
2
3\startcomponent about-speed
4
5\environment about-environment
6
7\startchapter[title=Math Styles]
8
9\startsection[title=Introduction]
10
11Because \CONTEXT\ is often considered somewhat less math savvy than for instance
12\LATEX\ we have more freedom to experiment with new insights and move forward. Of
13course \CONTEXT\ always could deal with math, and even provides rather advanced
14support when it comes to combining fonts, which at some point was needed for a
15magazine that used two completely different sets of fonts in one issue. Also,
16many of the mechanisms had ways to influence the rendering, but often by means of
17constants and flags.
18
19Already in an early stage of \LUATEX\ we went \UNICODE\ and after that the low
20level code has been cleaned up stepwise. In fact, we probably have less code now
21than before because we need less hacks. Well, this might not be that true, if we
22consider that we also introduced code at the \LUA\ end which wasn't there before,
23but which makes makes support easier.
24
25Because we don't need to support all kind of third party math extensions that
26themselves might depend on overloading low level implementations, we can
27rigourously replace mechanisms. In the process we also make things easier to
28configure, easier to define and we promote some previously low level tuning
29options at the user level.
30
31Or course, by introducing new features and more options, there is a price to pay
32in terms of speed, but in practice users will seldom use the more complex
33constructs thousands of times in one document. Elsewhere arrows and alike were
34discussed, here I will spend some words on math styles and will use fences and
35fractions as an example as these mechanisms were used to experiment.
36
37\stopsection
38
39\startsection[title=Math styles]
40
41In \TEX\ a formula can used three different sizes of a font: text, script and
42scriptscript. In addition a formula can be typeset using rules for display math
43or rules for inline math. This means that we have the following so called math
44styles:
45
46\starttabulate[||||]
47% \FL
48\NC \bf keyword         \NC \bf meaning               \NC \bf command                \NC \NR
49% \FL
50\NC \type{display}      \NC used for display math     \NC \type {\displaystyle}      \NC \NR
51\NC \type{text}         \NC used for inline math      \NC \type {\textstyle}         \NC \NR
52\NC \type{script}       \NC smaller than text style   \NC \type {\scriptstyle}       \NC \NR
53\NC \type{scriptscript} \NC smaller than script style \NC \type {\scriptscriptstyle} \NC \NR
54% \LL
55\stoptabulate
56
57Each of these commands will force a style but in practice you seldom need to do
58that because \TEX\ does it automatically. In addition there is are cramped styles
59with corresponding commands.
60
61\starttabulate
62  \NC \ruledhbox{$\displaystyle             x^2 + \sqrt{x^2+2x} + \sqrt{\displaystyle             x^2+2x}$} \NC \type{\displaystyle            } \NC \NR
63  \NC \ruledhbox{$\crampeddisplaystyle      x^2 + \sqrt{x^2+2x} + \sqrt{\crampeddisplaystyle      x^2+2x}$} \NC \type{\crampeddisplaystyle     } \NC \NR
64  \NC \ruledhbox{$\textstyle                x^2 + \sqrt{x^2+2x} + \sqrt{\textstyle                x^2+2x}$} \NC \type{\textstyle               } \NC \NR
65  \NC \ruledhbox{$\crampedtextstyle         x^2 + \sqrt{x^2+2x} + \sqrt{\crampedtextstyle         x^2+2x}$} \NC \type{\crampedtextstyle        } \NC \NR
66  \NC \ruledhbox{$\scriptstyle              x^2 + \sqrt{x^2+2x} + \sqrt{\scriptstyle              x^2+2x}$} \NC \type{\scriptstyle             } \NC \NR
67  \NC \ruledhbox{$\crampedscriptstyle       x^2 + \sqrt{x^2+2x} + \sqrt{\crampedscriptstyle       x^2+2x}$} \NC \type{\crampedscriptstyle      } \NC \NR
68  \NC \ruledhbox{$\scriptscriptstyle        x^2 + \sqrt{x^2+2x} + \sqrt{\scriptscriptstyle        x^2+2x}$} \NC \type{\scriptscriptstyle       } \NC \NR
69  \NC \ruledhbox{$\crampedscriptscriptstyle x^2 + \sqrt{x^2+2x} + \sqrt{\crampedscriptscriptstyle x^2+2x}$} \NC \type{\crampedscriptscriptstyle} \NC \NR
70\stoptabulate
71
72Here we applied the styles as follows:
73
74\startbuffer
75$\textstyle x^2 + \sqrt{x^2+2x} + \sqrt{\textstyle x^2+2x}$
76\stopbuffer
77
78\typebuffer
79
80The differences are subtle: the superscripts in the square root are positioned a
81bit lower than normal: the radical forces them to be cramped.
82
83\startlinecorrection
84\scale[width=\hsize]{\maincolor \getbuffer}
85\stoplinecorrection
86
87Although the average user will not bother about styles, a math power user might
88get excited about the possibility to control the size of fonts being used, of
89course wit the danger of creating a visually inconsistent document. And, as in
90\CONTEXT\ we try to avoid such low level commands \footnote {Although \unknown\
91it's pretty hard to convince users to stay away from \type {\vskip} and friends.}
92it will be no surprise that we have ways to set them beforehand.
93
94\startbuffer
95\definemathstyle[mystyle][scriptscript]
96
97$ 2x + \startmathstyle [mystyle] 4y^2 \stopmathstyle = 10 $
98\stopbuffer
99
100\typebuffer
101
102So, if you want it this ugly, you can get it:
103
104\blank \start \getbuffer \stop \blank
105
106A style can be a combination of keywords. Of course we have \type {display},
107\type {text}, \type {script} and \type {scriptscript}. Then there are \type
108{uncramped} and \type {cramped} along with their synonyms \type {normal} and
109\type {packed}. In some cases you can also use \type {small} and \type {big}
110which will promote the size up or down, relative to what we have currently.
111
112A style definition can be combination of such keywords:
113
114\starttyping
115\definemathstyle[mystyle][scriptscript,cramped]
116\stoptyping
117
118Gradually we will introduce the \type {mathstyle} keyword in math related
119setups commands.
120
121In most cases a user will limit the scope of some setting by using braces, like
122this:
123
124\startbuffer
125$x{\setupmathstyle[script]x}x$
126\stopbuffer
127
128This gives {\maincolor \ignorespaces \getbuffer \removeunwantedspaces}: a smaller
129symbol between two with text size. Equally valid is this:
130
131\startbuffer
132$x\startmathstyle[script]x\stopmathstyle x$
133\stopbuffer
134
135\typebuffer
136
137Again we get {\maincolor \ignorespaces \getbuffer \removeunwantedspaces}, but at
138the cost of more verbose coding.
139
140The use of \type {{}} (either or not hidden in commands) has a few side effects.
141In text mode, when we use this at the start of a paragraph, the paragraph will
142start inside the group and when we end the group, specific settings that were
143done at that time get lost. So, in practice you will force a paragraph outside
144the group using \type {\dontleavehmode}, \type {\strut}, or one of the
145indentation commands. \stopitem
146
147In math mode a new math group is created which limits local style settings to
148this group. But as such groups also can trigger special kinds of spacing you
149sometimes don't want that. One pitfall is then to do this:
150
151\startbuffer
152$x\begingroup\setupmathstyle[script]x\endgroup x$
153\stopbuffer
154
155\typebuffer
156
157Alas, now we get {\maincolor \ignorespaces \getbuffer \removeunwantedspaces}. A
158\type {\begingroup} limits the scope of many things but it will not create a math
159group! This kind of subtle issues is the reason why we have pre|-|built solutions
160that take care of style switching, grouping, spacing and positioning.
161
162\stopsection
163
164\startsection[title=Fences]
165
166Fences are symbols at the left and right of an expression: braces, brackets,
167curly braces, and bars are the most well known. Often they are supposed to adapt
168their size to the content that they wrap. Here you see some in action:
169
170\starttabulate[||c||]
171\NC \type {$|x|$}                            \NC $|x|$                             \NC okay  \NC \NR
172\NC \type {$||x||$}                          \NC $||x||$                           \NC okay  \NC \NR
173\NC \type {$a\left | \frac{1}{b}\right | c$} \NC $a\left |  \frac{1}{b}\right | c$ \NC okay  \NC \NR
174\NC \type {$a\left ||\frac{1}{b}\right ||c$} \NC $a\left || \frac{1}{b}\right ||c$ \NC wrong \NC \NR
175\NC \type {$a\left  \frac{1}{b}\right  c$} \NC $a\left   \frac{1}{b}\right  c$ \NC okay  \NC \NR
176\stoptabulate
177
178Often authors like to code their math with minimal structure and if you use
179\UNICODE\ characters that is actually quite doable. Just look at the double bar
180in the example above: if we input \type {||} we don't get what we want, but with
181\type {} the result is okay. This is because the \type {\left} and \type
182{\right} commands expect one character. But, even then, coding a bit more
183verbose sometimes makes sense.
184
185In stock \CONTEXT\ we have a couple of predefined fences:
186
187\starttyping
188\definemathfence [parenthesis] [left=0x0028,right=0x0029]
189\definemathfence [bracket]     [left=0x005B,right=0x005D]
190\definemathfence [braces]      [left=0x007B,right=0x007D]
191\definemathfence [bar]         [left=0x007C,right=0x007C]
192\definemathfence [doublebar]   [left=0x2016,right=0x2016]
193\definemathfence [angle]       [left=0x003C,right=0x003E]
194\stoptyping
195
196\startbuffer
197test $a \fenced[bar]      {\frac{1}{b}} c$ test
198test $a \fenced[doublebar]{\frac{1}{b}} c$ test
199test $a \fenced[bracket]  {\frac{1}{b}} c$ test
200\stopbuffer
201
202You use these by name:
203
204\typebuffer
205
206and get
207
208\startlines \getbuffer \stoplines
209
210\startbuffer
211\definemathfence [nooffence] [left=0x005B]
212\stopbuffer
213
214You can stick to only one fence:
215
216\typebuffer \getbuffer
217
218\startbuffer
219on $a \fenced[nooffence]{\frac{1}{b}} c$ off
220\stopbuffer
221
222Here \CONTEXT\ will take care of the dummy fence that \TEX\ expects instead.
223
224\startlines \getbuffer \stoplines
225
226You can define new fences and clone existing ones. You can also assign some
227properties:
228
229\startbuffer
230\definemathfence
231  [fancybracket]
232  [bracket]
233  [command=yes,
234   color=blue]
235\stopbuffer
236
237\typebuffer \getbuffer
238
239\startbuffer
240test $a\fancybracket{\frac{1}{b}}c$ test
241test \color[red]{$a\fancybracket{\frac{1}{b}}c$} test
242\stopbuffer
243
244\typebuffer
245
246The color is only applied to the fence. This makes sense as the formula can
247follow the main color but influencing the fences is technically somewhat more
248complex.
249
250\getbuffer
251
252Here are some more examples:
253
254\startbuffer
255\definemathfence
256  [normalbracket]
257  [bracket]
258  [command=yes,
259   color=blue]
260
261\definemathfence
262  [scriptbracket]
263  [normalbracket]
264  [mathstyle=script]
265
266\definemathfence
267  [smallbracket]
268  [normalbracket]
269  [mathstyle=small]
270\stopbuffer
271
272\typebuffer \getbuffer
273
274\starttabulate
275\NC \type{$a                \frac{1}{b} c$}  \NC $a                \frac{1}{b}  c$ \NC \NR
276\TB
277\NC \type{$a \normalbracket{\frac{1}{b} c$}} \NC $a \normalbracket{\frac{1}{b}} c$ \NC \NR
278\TB
279\NC \type{$a \scriptbracket{\frac{1}{b} c$}} \NC $a \scriptbracket{\frac{1}{b}} c$ \NC \NR
280\TB
281\NC \type{$a \smallbracket {\frac{1}{b} c$}} \NC $a \smallbracket {\frac{1}{b}} c$ \NC \NR
282\stoptabulate
283
284As with most commands, the fences inherit from the parents so we can say:
285
286\starttyping
287\setupmathfences [color=red]
288\stoptyping
289
290and get all our fences colored red. The \type {command} option results in a
291command being defined, which saves you some keying.
292
293\stopsection
294
295\startsection[title=Fractions]
296
297In \TEX\ the mechanism to put something on top of something else, separated by a
298horizontal rule, is driven by the \type {\over} primitive. That one has a
299(compared to other commands) somewhat different specification, in the sense that
300one of its arguments sits in front:
301
302\starttyping
303$ {{2x}\over{x^1}} $
304\stoptyping
305
306Although to some extend this is considered to be more readable, macro packages
307often provide a \type {\frac} commands that goes like this:
308
309\starttyping
310$ \frac{2x}{x^1} $
311\stoptyping
312
313There we have less braces and the arguments come after the command. As with the
314fences in the previous section, you can define your own fractions:
315
316\startbuffer
317\definemathfraction
318  [innerfrac]
319  [frac]
320  [alternative=inner,
321   mathstyle=script,
322   color=red]
323
324\definemathfraction
325  [outerfrac]
326  [frac]
327  [alternative=outer,
328   mathstyle=script,
329   color=blue]
330\stopbuffer
331
332\typebuffer \getbuffer
333
334The mathstyle and color are already discussed but the \type {alternative} is
335specific for these fractions. It determines if the style is applied to the whole
336fraction or to its components.
337
338\startbuffer
339\startformula
340\outerfrac{2a}{3b} = \innerfrac{2a}{3b} = \frac{2a}{3b}
341\stopformula
342\stopbuffer
343
344\typebuffer
345
346As with fences, the color is only applied to the horizontal bar as there is no
347other easy way to color that otherwise.
348
349\getbuffer
350
351As \TEX\ has a couple of low level stackers, we provide an interface to that as
352well, but we hide the dirty details. For instance you can define left and right
353fences and influence the rule
354
355\startbuffer
356\definemathfraction[fraca][rule=no,left=0x005B,right=0x007C]
357\definemathfraction[fracb][rule=yes,left=0x007B,right=0x007D]
358\definemathfraction[fracc][rule=auto,left=0x007C]
359\definemathfraction[fracd][rule=yes,rulethickness=2pt,left=0x007C]
360\stopbuffer
361
362\typebuffer \getbuffer
363
364When \type {rule} is set to \type {auto}, we use \TEX's values (derived from font
365metrics) for the thickness of rules, while \type {yes} triggers usage of the
366specified \type {rulethickness}.
367
368\startbuffer
369\startformula
370\fraca{a}{b} + \fracb{a}{b} + \fracc{a}{b} + \fracd{a}{b}
371\stopformula
372\stopbuffer
373
374\typebuffer
375
376Gives:
377
378\getbuffer
379
380\startbuffer
381\definemathfraction
382  [frace]
383  [rule=yes,
384   color=blue,
385   rulethickness=1pt,
386   left=0x005B,
387   right=0x007C]
388\stopbuffer
389
390\typebuffer \getbuffer
391
392This fraction looks as follows (scaled up):
393
394\startlinecorrection
395\midaligned{\scale[height=5ex]{$\displaystyle\frace{a}{b}$}}
396\stoplinecorrection
397
398So, the color is applied to the (optional) fences as well as to the (optional)
399rule. And when you color the whole formula as part of the context, you get
400
401\startlinecorrection
402\midaligned{\scale[height=5ex]{\color[maincolor]{$\displaystyle\frace{a}{b}$}}}
403\stoplinecorrection
404
405There is a (maybe not so) subtle difference between fences that come with
406fractions and regular fences, Take these definitions:
407
408\startbuffer
409\definemathfence    [parenta] [left=0x28,right=0x29,command=yes]
410\definemathfraction [parentb] [left=0x28,right=0x29,rule=auto]
411\stopbuffer
412
413\typebuffer \getbuffer
414
415Of course the \type {b} variant takes less code:
416
417\startbuffer
418\startformula
419\parenta{\frac{a}{b}} + \parentb{a}{b}
420\stopformula
421\stopbuffer
422
423\typebuffer
424
425But watch how the parentheses are also larger. At some point \CONTEXT\ will
426provide a bit more control over this,
427
428\getbuffer
429
430You can also influence the width of the rule, but that is not related to the
431style.
432
433\startbuffer
434\definemathfraction
435  [wfrac]
436  [margin=.25em]
437
438\definemathfraction
439  [wwfrac]
440  [margin=.50em]
441
442\startformula
443  \frac   {  a } { \frac {  b } {  c } } +
444  \wfrac  {  a } { \frac {  b } {  c } } =
445  \wwfrac { 2a } { \frac { 2b } { 2c } }
446\stopformula
447\stopbuffer
448
449\typebuffer
450
451Both the nominator and denominator are widened by the margin:
452
453\getbuffer
454
455\stopsection
456
457\stopcomponent
458