summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/about/about-mathstyles.tex
blob: 205e985610b9a733432ce735b312d25e8022c523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
% language=uk

\startcomponent about-speed

\environment about-environment

\startchapter[title=Math Styles]

\startsection[title=Introduction]

Because \CONTEXT\ is often considered somewhat less math savvy than for instance
\LATEX\ we have more freedom to experiment with new insights and move forward. Of
course \CONTEXT\ always could deal with math, and even provides rather advanced
support when it comes to combining fonts, which at some point was needed for a
magazine that used two completely different sets of fonts in one issue. Also,
many of the mechanisms had ways to influence the rendering, but often by means of
constants and flags.

Already in an early stage of \LUATEX\ we went \UNICODE\ and after that the low
level code has been cleaned up stepwise. In fact, we probably have less code now
than before because we need less hacks. Well, this might not be that true, if we
consider that we also introduced code at the \LUA\ end which wasn't there before,
but which makes makes support easier.

Because we don't need to support all kind of third party math extensions that
themselves might depend on overloading low level implementations, we can
rigourously replace mechanisms. In the process we also make things easier to
configure, easier to define and we promote some previously low level tuning
options at the user level.

Or course, by introducing new features and more options, there is a price to pay
in terms of speed, but in practice users will seldom use the more complex
constructs thousands of times in one document. Elsewhere arrows and alike were
discussed, here I will spend some words on math styles and will use fences and
fractions as an example as these mechanisms were used to experiment.

\stopsection

\startsection[title=Math styles]

In \TEX\ a formula can used three different sizes of a font: text, script and
scriptscript. In addition a formula can be typeset using rules for display math
or rules for inline math. This means that we have the following so called math
styles:

\starttabulate[||||]
% \FL
\NC \bf keyword         \NC \bf meaning               \NC \bf command                \NC \NR
% \FL
\NC \type{display}      \NC used for display math     \NC \type {\displaystyle}      \NC \NR
\NC \type{text}         \NC used for inline math      \NC \type {\textstyle}         \NC \NR
\NC \type{script}       \NC smaller than text style   \NC \type {\scriptstyle}       \NC \NR
\NC \type{scriptscript} \NC smaller than script style \NC \type {\scriptscriptstyle} \NC \NR
% \LL
\stoptabulate

Each of these commands will force a style but in practice you seldom need to do
that because \TEX\ does it automatically. In addition there is are cramped styles
with corresponding commands.

\starttabulate
  \NC \ruledhbox{$\displaystyle             x^2 + \sqrt{x^2+2x} + \sqrt{\displaystyle             x^2+2x}$} \NC \type{\displaystyle            } \NC \NR
  \NC \ruledhbox{$\crampeddisplaystyle      x^2 + \sqrt{x^2+2x} + \sqrt{\crampeddisplaystyle      x^2+2x}$} \NC \type{\crampeddisplaystyle     } \NC \NR
  \NC \ruledhbox{$\textstyle                x^2 + \sqrt{x^2+2x} + \sqrt{\textstyle                x^2+2x}$} \NC \type{\textstyle               } \NC \NR
  \NC \ruledhbox{$\crampedtextstyle         x^2 + \sqrt{x^2+2x} + \sqrt{\crampedtextstyle         x^2+2x}$} \NC \type{\crampedtextstyle        } \NC \NR
  \NC \ruledhbox{$\scriptstyle              x^2 + \sqrt{x^2+2x} + \sqrt{\scriptstyle              x^2+2x}$} \NC \type{\scriptstyle             } \NC \NR
  \NC \ruledhbox{$\crampedscriptstyle       x^2 + \sqrt{x^2+2x} + \sqrt{\crampedscriptstyle       x^2+2x}$} \NC \type{\crampedscriptstyle      } \NC \NR
  \NC \ruledhbox{$\scriptscriptstyle        x^2 + \sqrt{x^2+2x} + \sqrt{\scriptscriptstyle        x^2+2x}$} \NC \type{\scriptscriptstyle       } \NC \NR
  \NC \ruledhbox{$\crampedscriptscriptstyle x^2 + \sqrt{x^2+2x} + \sqrt{\crampedscriptscriptstyle x^2+2x}$} \NC \type{\crampedscriptscriptstyle} \NC \NR
\stoptabulate

Here we applied the styles as follows:

\startbuffer
$\textstyle x^2 + \sqrt{x^2+2x} + \sqrt{\textstyle x^2+2x}$
\stopbuffer

\typebuffer

The differences are subtle: the superscripts in the square root are positioned a
bit lower than normal: the radical forces them to be cramped.

\startlinecorrection
\scale[width=\hsize]{\maincolor \getbuffer}
\stoplinecorrection

Although the average user will not bother about styles, a math power user might
get excited about the possibility to control the size of fonts being used, of
course wit the danger of creating a visually inconsistent document. And, as in
\CONTEXT\ we try to avoid such low level commands \footnote {Although \unknown\
it's pretty hard to convince users to stay away from \type {\vskip} and friends.}
it will be no surprise that we have ways to set them beforehand.

\startbuffer
\definemathstyle[mystyle][scriptscript]

$ 2x + \startmathstyle [mystyle] 4y^2 \stopmathstyle = 10 $
\stopbuffer

\typebuffer

So, if you want it this ugly, you can get it:

\blank \start \getbuffer \stop \blank

A style can be a combination of keywords. Of course we have \type {display},
\type {text}, \type {script} and \type {scriptscript}. Then there are \type
{uncramped} and \type {cramped} along with their synonyms \type {normal} and
\type {packed}. In some cases you can also use \type {small} and \type {big}
which will promote the size up or down, relative to what we have currently.

A style definition can be combination of such keywords:

\starttyping
\definemathstyle[mystyle][scriptscript,cramped]
\stoptyping

Gradually we will introduce the \type {mathstyle} keyword in math related
setups commands.

In most cases a user will limit the scope of some setting by using braces, like
this:

\startbuffer
$x{\setupmathstyle[script]x}x$
\stopbuffer

This gives {\maincolor \ignorespaces \getbuffer \removeunwantedspaces}: a smaller
symbol between two with text size. Equally valid is this:

\startbuffer
$x\startmathstyle[script]x\stopmathstyle x$
\stopbuffer

\typebuffer

Again we get {\maincolor \ignorespaces \getbuffer \removeunwantedspaces}, but at
the cost of more verbose coding.

The use of \type {{}} (either or not hidden in commands) has a few side effects.
In text mode, when we use this at the start of a paragraph, the paragraph will
start inside the group and when we end the group, specific settings that were
done at that time get lost. So, in practice you will force a paragraph outside
the group using \type {\dontleavehmode}, \type {\strut}, or one of the
indentation commands. \stopitem

In math mode a new math group is created which limits local style settings to
this group. But as such groups also can trigger special kinds of spacing you
sometimes don't want that. One pitfall is then to do this:

\startbuffer
$x\begingroup\setupmathstyle[script]x\endgroup x$
\stopbuffer

\typebuffer

Alas, now we get {\maincolor \ignorespaces \getbuffer \removeunwantedspaces}. A
\type {\begingroup} limits the scope of many things but it will not create a math
group! This kind of subtle issues is the reason why we have pre|-|built solutions
that take care of style switching, grouping, spacing and positioning.

\stopsection

\startsection[title=Fences]

Fences are symbols at the left and right of an expression: braces, brackets,
curly braces, and bars are the most well known. Often they are supposed to adapt
their size to the content that they wrap. Here you see some in action:

\starttabulate[||c||]
\NC \type {$|x|$}                            \NC $|x|$                             \NC okay  \NC \NR
\NC \type {$||x||$}                          \NC $||x||$                           \NC okay  \NC \NR
\NC \type {$a\left | \frac{1}{b}\right | c$} \NC $a\left |  \frac{1}{b}\right | c$ \NC okay  \NC \NR
\NC \type {$a\left ||\frac{1}{b}\right ||c$} \NC $a\left || \frac{1}{b}\right ||c$ \NC wrong \NC \NR
\NC \type {$a\left ‖ \frac{1}{b}\right ‖ c$} \NC $a\left ‖  \frac{1}{b}\right ‖ c$ \NC okay  \NC \NR
\stoptabulate

Often authors like to code their math with minimal structure and if you use
\UNICODE\ characters that is actually quite doable. Just look at the double bar
in the example above: if we input \type {||} we don't get what we want, but with
\type {‖} the result is okay. This is because the \type {\left} and \type
{\right} commands expect one character. But, even then, coding a bit more
verbose sometimes makes sense.

In stock \CONTEXT\ we have a couple of predefined fences:

\starttyping
\definemathfence [parenthesis] [left=0x0028,right=0x0029]
\definemathfence [bracket]     [left=0x005B,right=0x005D]
\definemathfence [braces]      [left=0x007B,right=0x007D]
\definemathfence [bar]         [left=0x007C,right=0x007C]
\definemathfence [doublebar]   [left=0x2016,right=0x2016]
\definemathfence [angle]       [left=0x003C,right=0x003E]
\stoptyping

\startbuffer
test $a \fenced[bar]      {\frac{1}{b}} c$ test
test $a \fenced[doublebar]{\frac{1}{b}} c$ test
test $a \fenced[bracket]  {\frac{1}{b}} c$ test
\stopbuffer

You use these by name:

\typebuffer

and get

\startlines \getbuffer \stoplines

\startbuffer
\definemathfence [nooffence] [left=0x005B]
\stopbuffer

You can stick to only one fence:

\typebuffer \getbuffer

\startbuffer
on $a \fenced[nooffence]{\frac{1}{b}} c$ off
\stopbuffer

Here \CONTEXT\ will take care of the dummy fence that \TEX\ expects instead.

\startlines \getbuffer \stoplines

You can define new fences and clone existing ones. You can also assign some
properties:

\startbuffer
\definemathfence
  [fancybracket]
  [bracket]
  [command=yes,
   color=blue]
\stopbuffer

\typebuffer \getbuffer

\startbuffer
test $a\fancybracket{\frac{1}{b}}c$ test
test \color[red]{$a\fancybracket{\frac{1}{b}}c$} test
\stopbuffer

\typebuffer

The color is only applied to the fence. This makes sense as the formula can
follow the main color but influencing the fences is technically somewhat more
complex.

\getbuffer

Here are some more examples:

\startbuffer
\definemathfence
  [normalbracket]
  [bracket]
  [command=yes,
   color=blue]

\definemathfence
  [scriptbracket]
  [normalbracket]
  [mathstyle=script]

\definemathfence
  [smallbracket]
  [normalbracket]
  [mathstyle=small]
\stopbuffer

\typebuffer \getbuffer

\starttabulate
\NC \type{$a                \frac{1}{b} c$}  \NC $a                \frac{1}{b}  c$ \NC \NR
\TB
\NC \type{$a \normalbracket{\frac{1}{b} c$}} \NC $a \normalbracket{\frac{1}{b}} c$ \NC \NR
\TB
\NC \type{$a \scriptbracket{\frac{1}{b} c$}} \NC $a \scriptbracket{\frac{1}{b}} c$ \NC \NR
\TB
\NC \type{$a \smallbracket {\frac{1}{b} c$}} \NC $a \smallbracket {\frac{1}{b}} c$ \NC \NR
\stoptabulate

As with most commands, the fences inherit from the parents so we can say:

\starttyping
\setupmathfences [color=red]
\stoptyping

and get all our fences colored red. The \type {command} option results in a
command being defined, which saves you some keying.

\stopsection

\startsection[title=Fractions]

In \TEX\ the mechanism to put something on top of something else, separated by a
horizontal rule, is driven by the \type {\over} primitive. That one has a
(compared to other commands) somewhat different specification, in the sense that
one of its arguments sits in front:

\starttyping
$ {{2x}\over{x^1}} $
\stoptyping

Although to some extend this is considered to be more readable, macro packages
often provide a \type {\frac} commands that goes like this:

\starttyping
$ \frac{2x}{x^1} $
\stoptyping

There we have less braces and the arguments come after the command. As with the
fences in the previous section, you can define your own fractions:

\startbuffer
\definemathfraction
  [innerfrac]
  [frac]
  [alternative=inner,
   mathstyle=script,
   color=red]

\definemathfraction
  [outerfrac]
  [frac]
  [alternative=outer,
   mathstyle=script,
   color=blue]
\stopbuffer

\typebuffer \getbuffer

The mathstyle and color are already discussed but the \type {alternative} is
specific for these fractions. It determines if the style is applied to the whole
fraction or to its components.

\startbuffer
\startformula
\outerfrac{2a}{3b} = \innerfrac{2a}{3b} = \frac{2a}{3b}
\stopformula
\stopbuffer

\typebuffer

As with fences, the color is only applied to the horizontal bar as there is no
other easy way to color that otherwise.

\getbuffer

As \TEX\ has a couple of low level stackers, we provide an interface to that as
well, but we hide the dirty details. For instance you can define left and right
fences and influence the rule

\startbuffer
\definemathfraction[fraca][rule=no,left=0x005B,right=0x007C]
\definemathfraction[fracb][rule=yes,left=0x007B,right=0x007D]
\definemathfraction[fracc][rule=auto,left=0x007C]
\definemathfraction[fracd][rule=yes,rulethickness=2pt,left=0x007C]
\stopbuffer

\typebuffer \getbuffer

When \type {rule} is set to \type {auto}, we use \TEX's values (derived from font
metrics) for the thickness of rules, while \type {yes} triggers usage of the
specified \type {rulethickness}.

\startbuffer
\startformula
\fraca{a}{b} + \fracb{a}{b} + \fracc{a}{b} + \fracd{a}{b}
\stopformula
\stopbuffer

\typebuffer

Gives:

\getbuffer

\startbuffer
\definemathfraction
  [frace]
  [rule=yes,
   color=blue,
   rulethickness=1pt,
   left=0x005B,
   right=0x007C]
\stopbuffer

\typebuffer \getbuffer

This fraction looks as follows (scaled up):

\startlinecorrection
\midaligned{\scale[height=5ex]{$\displaystyle\frace{a}{b}$}}
\stoplinecorrection

So, the color is applied to the (optional) fences as well as to the (optional)
rule. And when you color the whole formula as part of the context, you get

\startlinecorrection
\midaligned{\scale[height=5ex]{\color[maincolor]{$\displaystyle\frace{a}{b}$}}}
\stoplinecorrection

There is a (maybe not so) subtle difference between fences that come with
fractions and regular fences, Take these definitions:

\startbuffer
\definemathfence    [parenta] [left=0x28,right=0x29,command=yes]
\definemathfraction [parentb] [left=0x28,right=0x29,rule=auto]
\stopbuffer

\typebuffer \getbuffer

Of course the \type {b} variant takes less code:

\startbuffer
\startformula
\parenta{\frac{a}{b}} + \parentb{a}{b}
\stopformula
\stopbuffer

\typebuffer

But watch how the parentheses are also larger. At some point \CONTEXT\ will
provide a bit more control over this,

\getbuffer

You can also influence the width of the rule, but that is not related to the
style.

\startbuffer
\definemathfraction
  [wfrac]
  [margin=.25em]

\definemathfraction
  [wwfrac]
  [margin=.50em]

\startformula
  \frac   {  a } { \frac {  b } {  c } } +
  \wfrac  {  a } { \frac {  b } {  c } } =
  \wwfrac { 2a } { \frac { 2b } { 2c } }
\stopformula
\stopbuffer

\typebuffer

Both the nominator and denominator are widened by the margin:

\getbuffer

\stopsection

\stopcomponent