1
2
3
4
5
6
7
8
9
10
11
12
13
14\writestatus{loading}{ConTeXt Math Macros Fractions}
15
16\unprotect
17
18
19
20
21\unexpanded\def\topstrut{\vrule\s!width\zeropoint\s!height\strutht\s!depth\zeropoint\relax}
22\unexpanded\def\botstrut{\vrule\s!width\zeropoint\s!height\zeropoint\s!depth\strutdp\relax}
23
24\unexpanded\def\mathtopstrut{\setbox\scratchbox\mathstylehbox{(}\vrule\s!width\zeropoint\s!height\ht\scratchbox\s!depth\zeropoint\relax}
25\unexpanded\def\mathbotstrut{\setbox\scratchbox\mathstylehbox{(}\vrule\s!width\zeropoint\s!height\zeropoint\s!depth\dp\scratchbox\relax}
26
27
28
29\registerctxluafile{mathfrc}{}
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\setnewconstant\mathfracmode\zerocount
98
99\installcorenamespace{mathfractions}
100\installcorenamespace{mathfractionstyle}
101\installcorenamespace{mathfractionalternative}
102
103\installcommandhandler \??mathfractions {mathfraction} \??mathfractions
104
105\let\setupmathfractions\setupmathfraction
106
107
108
109\setupmathfractions
110 [\c!mathstyle=,
111 \c!alternative=\v!inner,
112 \c!margin=\zeropoint,
113 \c!rulethickness=.25\exheight,
114 \c!left=0x2E,
115 \c!right=0x2E,
116 \c!strut=\v!yes,
117 \c!topdistance=,
118 \c!bottomdistance=,
119 \c!rule=\v!auto]
120
121\appendtoks
122 \setuevalue{\currentmathfraction}{\math_frac{\currentmathfraction}}
123\to \everydefinemathfraction
124
125
126
127\let\math_fraction_set_distance\relax
128
129\appendtoks
130 \math_fraction_set_distance
131\to \everymathematics
132
133
134
135\unexpanded\def\math_fraction_set_distance_top
136 {\Umathfractionnumup \displaystyle\m_math_fraction_distance_top
137 \relax}
138
139\unexpanded\def\math_fraction_set_distance_bot
140 {\Umathfractiondenomdown\displaystyle\m_math_fraction_distance_bot
141 \relax}
142
143\unexpanded\def\math_fraction_set_distance_all
144 {\Umathfractionnumup \displaystyle\m_math_fraction_distance_top
145 \Umathfractiondenomdown\displaystyle\m_math_fraction_distance_bot
146 \relax}
147
148\appendtoks
149 \ifx\currentmathfraction\empty
150 \edef\m_math_fraction_distance_top{\mathfractionparameter\c!topdistance}
151 \edef\m_math_fraction_distance_bot{\mathfractionparameter\c!bottomdistance}
152 \ifx\m_math_fraction_distance_top\empty
153 \ifx\m_math_fraction_distance_bot\empty
154 \let\math_fraction_set_distance\relax
155 \else
156 \let\math_fraction_set_distance\math_fraction_set_distance_bot
157 \fi
158 \else
159 \ifx\m_math_fraction_distance_bot\empty
160 \let\math_fraction_set_distance\math_fraction_set_distance_top
161 \else
162 \let\math_fraction_set_distance\math_fraction_set_distance_all
163 \fi
164 \fi
165 \fi
166\to \everysetupmathfraction
167
168
169
170\installcorenamespace{mathfractionstrut}
171
172\def\math_frac_no_strut
173 {\let\m_fractions_strut_top\relax
174 \let\m_fractions_strut_bot\relax}
175
176\setvalue{\??mathfractionstrut\v!yes}
177 {\let\m_fractions_strut_top\mathstrut
178 \let\m_fractions_strut_bot\mathstrut}
179
180\setvalue{\??mathfractionstrut\v!math}
181 {\let\m_fractions_strut_top\mathstrut
182 \let\m_fractions_strut_bot\mathstrut}
183
184\letvalue{\??mathfractionstrut\v!no}\math_frac_no_strut
185
186\setvalue{\??mathfractionstrut\v!tight}
187 {\let\m_fractions_strut_top\mathbotstrut
188 \let\m_fractions_strut_bot\mathtopstrut}
189
190\math_frac_no_strut
191
192\newdimen\d_math_fraction_margin
193
194\unexpanded\def\math_frac#1
195 {\begingroup
196 \edef\currentmathfraction{#1}
197
198 \edef\p_math_fraction_fences{\mathfractionparameter\c!fences}
199 \ifx\p_math_fraction_fences\empty \else
200 \math_fenced_fenced_start\p_math_fraction_fences
201 \fi
202
203 \d_math_fraction_margin\mathfractionparameter\c!margin
204
205 \edef\p_math_fractions_color{\mathfractionparameter\c!color}
206
207 \edef\p_math_fractions_strut{\mathfractionparameter\c!strut}
208 \ifcsname\??mathfractionstrut\p_math_fractions_strut\endcsname
209 \lastnamedcs
210 \else
211 \math_frac_no_strut
212 \fi
213
214 \ifx\p_math_fractions_color\empty
215 \expandafter\math_frac_normal
216 \else
217 \expandafter\math_frac_colored
218 \fi}
219
220\unexpanded\def\math_frac_wrapup
221 {\ifx\p_math_fraction_fences\empty \else
222 \math_fenced_fenced_stop\p_math_fraction_fences
223 \fi
224 \endgroup}
225
226\unexpanded\def\math_frac_colored#1#2
227 {\savecolor
228 \colo_helpers_activate\p_math_fractions_color
229 \math_frac_normal{\restorecolor#1}{\restorecolor#2}}
230
231\unexpanded\def\math_frac_normal
232 {\expandnamespaceparameter\??mathfractionalternative\mathfractionparameter\c!alternative\v!inner}
233
234
235
236\def\math_frac_no_delim{0x2E}
237
238\def\math_frac_command
239 {\clf_mathfraction
240 {\mathfractionparameter\c!rule}
241 \ifx\p_math_fraction_fences\empty
242 \mathfractionparameter\c!left \space
243 \mathfractionparameter\c!right\space
244 \else
245 \math_frac_no_delim\space
246 \math_frac_no_delim\space
247 \fi
248 \dimexpr\mathfractionparameter\c!rulethickness\relax
249 \relax}
250
251
252
253
254
255
256
257
258\setvalue{\??mathfractionalternative\v!inner}
259 {\ifcase\d_math_fraction_margin
260 \expandafter\math_fraction_inner_normal
261 \else
262 \expandafter\math_fraction_inner_margin
263 \fi}
264
265\setvalue{\??mathfractionalternative\v!outer}
266 {\ifcase\d_math_fraction_margin
267 \expandafter\math_fraction_outer_normal
268 \else
269 \expandafter\math_fraction_outer_margin
270 \fi}
271
272\setvalue{\??mathfractionalternative\v!both}
273 {\ifcase\d_math_fraction_margin
274 \expandafter\math_fraction_both_normal
275 \else
276 \expandafter\math_fraction_both_margin
277 \fi}
278
279
280
281\def\math_fraction_inner_normal#1#2
282 {\Ustack{
283 {
284 {\usemathstyleparameter\mathfractionparameter{\m_fractions_strut_top#1}}
285 \math_frac_command
286 {\usemathstyleparameter\mathfractionparameter{\m_fractions_strut_bot#2}}
287 }
288 }
289 \math_frac_wrapup}
290
291\def\math_fraction_outer_normal#1#2
292 {\Ustack{
293 \usemathstyleparameter\mathfractionparameter
294 {
295 {\m_fractions_strut_top#1}
296 \math_frac_command
297 {\m_fractions_strut_bot#2}
298 }
299 }
300 \math_frac_wrapup}
301
302\def\math_fraction_both_normal#1#2
303 {\Ustack{
304 \usemathstyleparameter\mathfractionparameter
305 {
306 {\usemathstyleparameter\mathfractionparameter\m_fractions_strut_top#1}
307 \math_frac_command
308 {\usemathstyleparameter\mathfractionparameter\m_fractions_strut_bot#2}
309 }
310 }
311 \math_frac_wrapup}
312
313\def\math_fraction_inner_margin#1#2
314 {\Ustack{
315 {
316 {\kern\d_math_fraction_margin
317 \usemathstyleparameter\mathfractionparameter{\m_fractions_strut_top#1}
318 \kern\d_math_fraction_margin}
319 \math_frac_command
320 {\kern\d_math_fraction_margin
321 \usemathstyleparameter\mathfractionparameter{\m_fractions_strut_bot#2}
322 \kern\d_math_fraction_margin}
323 }
324 }
325 \math_frac_wrapup}
326
327\def\math_fraction_outer_margin#1#2
328 {\Ustack{
329 \usemathstyleparameter\mathfractionparameter
330 {
331 {\kern\d_math_fraction_margin
332 \m_fractions_strut_top#1
333 \kern\d_math_fraction_margin}
334 \math_frac_command
335 {\kern\d_math_fraction_margin
336 \m_fractions_strut_bot#2
337 \kern\d_math_fraction_margin}
338 }
339 }
340 \math_frac_wrapup}
341
342\def\math_fraction_both_margin#1#2
343 {\Ustack{
344 \usemathstyleparameter\mathfractionparameter
345 {
346 {\kern\d_math_fraction_margin
347 \usemathstyleparameter\mathfractionparameter\m_fractions_strut_top#1
348 \kern\d_math_fraction_margin}
349 \math_frac_command
350 {\kern\d_math_fraction_margin
351 \usemathstyleparameter\mathfractionparameter\m_fractions_strut_bot#2
352 \kern\d_math_fraction_margin}
353 }
354 }
355 \math_frac_wrapup}
356
357\definemathfraction[xfrac] [\c!alternative=\v!inner,\c!mathstyle=\s!script]
358\definemathfraction[xxfrac][\c!alternative=\v!inner,\c!mathstyle=\s!scriptscript]
359
360\let\normalxfrac\xfrac
361
362\unexpanded\def\xfrac#1#2{\normalxfrac{\let\xfrac\xxfrac#1}{\let\xfrac\xxfrac#2}}
363
364
365
366
367
368\unexpanded\def\nomathfractiongaps {\normalexpanded{\math_no_fraction_gaps \mathstyletrigger\mathstyle}}
369\unexpanded\def\overlaymathfractiongaps{\normalexpanded{\math_overlay_fraction_gaps\mathstyletrigger\mathstyle}}
370
371\unexpanded\def\math_no_fraction_gaps#1
372 {\Umathfractionnumup #1\zeropoint
373 \Umathfractiondenomdown#1\zeropoint}
374
375\unexpanded\def\math_overlay_fraction_gaps#1
376 {\Umathfractionnumup #1\zeropoint
377 \Umathfractionnumvgap #1\zeropoint
378
379 \Umathfractiondenomvgap#1\zeropoint
380 \Umathfractiondenomdown#1\zeropoint}
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397\installcorenamespace{mathfractiondistance}
398
399\letvalue{\??mathfractiondistance\v!none }\nomathfractiongaps
400\letvalue{\??mathfractiondistance\v!no }\nomathfractiongaps
401\letvalue{\??mathfractiondistance\v!overlay}\overlaymathfractiongaps
402
403\setupmathfractions
404 [\c!distance=\v!none]
405
406\appendtoks
407 \edef\p_distance{\rootmathfractionparameter\c!distance}
408 \ifx\p_distance\empty\else
409 \ifcsname\??mathfractiondistance\p_distance\endcsname
410 \lastnamedcs
411 \fi
412 \fi
413\to \everymathematics
414
415
416
417\let\math_fraction_set_threshold_inline \relax
418\let\math_fraction_set_threshold_display\relax
419
420\appendtoks
421 \math_fraction_set_threshold_inline
422 \math_fraction_set_threshold_display
423\to \everymathematics
424
425\appendtoks
426 \ifx\currentmathfraction\empty
427 \edef\p_threshold{\mathfractionparameter\c!inlinethreshold}
428 \ifx\p_threshold\empty
429 \let\math_fraction_set_threshold_inline\relax
430 \else\ifx\p_threshold\v!auto
431 \let\math_fraction_set_threshold_inline\relax
432 \else
433 \let\math_fraction_set_threshold_inline\math_fraction_set_theshold_inline
434 \fi\fi
435 \edef\p_threshold{\mathfractionparameter\c!displaythreshold}
436 \ifx\p_threshold\empty
437 \let\math_fraction_set_threshold_display\relax
438 \else\ifx\p_threshold\v!auto
439 \let\math_fraction_set_threshold_display\relax
440 \else
441 \let\math_fraction_set_threshold_display\math_fraction_set_theshold_display
442 \fi\fi
443 \fi
444\to \everysetupmathfraction
445
446\def\math_fraction_set_theshold_inline
447 {\edef\p_threshold{\mathfractionparameter\c!inlinethreshold}
448 \Umathfractiondelsize\textstyle \p_threshold\dimexpr\textface\relax
449 \Umathfractiondelsize\scriptstyle \p_threshold\dimexpr\scriptface\relax
450 \Umathfractiondelsize\scriptscriptstyle\p_threshold\dimexpr\scriptscriptface\relax}
451
452\def\math_fraction_set_theshold_display
453 {\edef\p_threshold{\mathfractionparameter\c!displaythreshold}
454 \Umathfractiondelsize\displaystyle \p_threshold\dimexpr\textface\relax}
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479\definemathfraction[i:frac] [\c!alternative=\v!inner,\c!mathstyle=]
480\definemathfraction[i:tfrac][\c!alternative=\v!inner,\c!mathstyle=\s!text]
481\definemathfraction[i:sfrac][\c!alternative=\v!inner,\c!mathstyle=\s!scriptscript]
482\definemathfraction[i:dfrac][\c!alternative=\v!inner,\c!mathstyle=\s!display]
483
484\definemathfraction[d:frac] [\c!alternative=\v!inner,\c!mathstyle=\s!cramped]
485\definemathfraction[d:tfrac][\c!alternative=\v!both ,\c!mathstyle={\s!cramped,\s!text}]
486\definemathfraction[d:sfrac][\c!alternative=\v!both ,\c!mathstyle={\s!cramped,\s!scriptscript}]
487\definemathfraction[d:dfrac][\c!alternative=\v!inner,\c!mathstyle=\s!display]
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505\unexpanded\def\frac {\csname\inlineordisplaymath id:frac\endcsname}
506\unexpanded\def\tfrac{\csname\inlineordisplaymath id:tfrac\endcsname}
507\unexpanded\def\sfrac{\csname\inlineordisplaymath id:sfrac\endcsname}
508\unexpanded\def\dfrac{\csname\inlineordisplaymath id:dfrac\endcsname}
509
510\definemathfraction[ams] [\c!strut=\v!no,\c!alternative=\v!outer]
511\definemathfraction[i:ams:frac][ams][\c!mathstyle={\s!cramped,\s!text}]
512\definemathfraction[d:ams:frac][ams][\c!mathstyle={\s!cramped,\s!display}]
513
514\unexpanded\def\ctxfrac{\csname\inlineordisplaymath id:frac\endcsname}
515\unexpanded\def\amsfrac{\csname\inlineordisplaymath id:ams:frac\endcsname}
516
517
518
519
520
521
522
523\appendtoks
524 \doifelse{\mathematicsparameter\c!fractions}{ams}
525 {\let\frac\amsfrac}
526 {\let\frac\ctxfrac}
527\to \everysetupmathematics
528
529
530
531
532
533
534
535
536
537\definemathfraction
538 [binom]
539 [\c!alternative=\v!outer,
540 \c!rule=\v!no,
541 \c!left=0x28,
542 \c!right=0x29,
543 \c!mathstyle=\s!auto]
544
545\definemathfraction
546 [dbinom]
547 [binom]
548 [\c!mathstyle=\s!display]
549
550\definemathfraction
551 [tbinom]
552 [binom]
553 [\c!mathstyle=\s!text]
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580\unexpanded\def\cfrac
581 {\doifelsenextoptionalcs\math_cfrac_yes\math_cfrac_nop}
582
583\def\math_cfrac_nop {\math_cfrac_indeed[cc]}
584\def\math_cfrac_yes[#1]{\math_cfrac_indeed[#1cc]}
585
586\def\math_cfrac_indeed[#1#2#3]#4#5
587 {{\displaystyle
588 \frac
589 {\strut
590 \ifx r#1\hfill\fi#4\ifx l#1\hfill\fi}
591 {\ifx r#2\hfill\fi#5\ifx l#2\hfill\fi}
592 \kern\nulldelimiterspace}}
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620\unexpanded\def\splitfrac #1#2
621 {{\textstyle {{\textstyle#1\quad\hfill}\normalabove\zeropoint{\textstyle\hfill\quad\mathstrut#2}}}}
622
623\unexpanded\def\splitdfrac#1#2
624 {{\displaystyle{{ #1\quad\hfill}\normalabove\zeropoint{ \hfill\quad\mathstrut#2}}}}
625
626
627
628
629
630
631
632
633\unexpanded\def\math_frac_colored_vulgar#1#2
634 {\savecolor
635 \colo_helpers_activate\p_math_fractions_color
636 {\restorecolor#1}\Uskewed{\restorecolor#2}}
637
638\unexpanded\def\math_frac_normal_vulgar#1#2
639 {{#1}\Uskewed{#2}}
640
641\unexpanded\def\vfrac#1#2
642 {\bgroup
643 \edef\p_math_fractions_color{\namedmathfractionparameter\s!vfrac\c!color}
644 \ifx\p_math_fractions_color\empty
645 \expandafter\math_frac_normal_vulgar
646 \else
647 \expandafter\math_frac_colored_vulgar
648 \fi
649 {#1}
650 {#2}
651 \egroup}
652
653\appendtoks
654 \edef\p_hfactor{\namedmathfractionparameter\s!vfrac\c!hfactor}
655 \edef\p_vfactor{\namedmathfractionparameter\s!vfrac\c!vfactor}
656 \Umathskewedfractionhgap\textstyle \p_hfactor\fontemwidth \mathstylefont\textstyle
657 \Umathskewedfractionhgap\scriptstyle \p_hfactor\fontemwidth \mathstylefont\scriptstyle
658 \Umathskewedfractionhgap\scriptscriptstyle\p_hfactor\fontemwidth \mathstylefont\scriptscriptstyle
659 \Umathskewedfractionvgap\textstyle \p_vfactor\fontexheight\mathstylefont\textstyle
660 \Umathskewedfractionvgap\scriptstyle \p_vfactor\fontexheight\mathstylefont\scriptstyle
661 \Umathskewedfractionvgap\scriptscriptstyle\p_vfactor\fontexheight\mathstylefont\scriptscriptstyle
662\to \everysetupmathfraction
663
664\setupmathfraction
665 [\s!vfrac]
666 [\c!hfactor=.2,
667 \c!vfactor=.1]
668
669\protect \endinput
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754 |