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\permanent\protected\def\topstrut{\vrule\s!width\zeropoint\s!height\strutht\s!depth\zeropoint\relax}
22\permanent\protected\def\botstrut{\vrule\s!width\zeropoint\s!height\zeropoint\s!depth\strutdp\relax}
23
24\permanent\protected\def\mathtopstrut{\setbox\scratchbox\mathstylehbox{(}\vrule\s!width\zeropoint\s!height\ht\scratchbox\s!depth\zeropoint\relax}
25\permanent\protected\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\aliased\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 \instance\frozen\protected\edefcsname\currentmathfraction\endcsname{\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\protected\def\math_fraction_set_distance_top
136 {\Umathfractionnumup \displaystyle\m_math_fraction_distance_top
137 \relax}
138
139\protected\def\math_fraction_set_distance_bot
140 {\Umathfractiondenomdown\displaystyle\m_math_fraction_distance_bot
141 \relax}
142
143\protected\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 \ifempty\currentmathfraction
150 \edef\m_math_fraction_distance_top{\mathfractionparameter\c!topdistance}
151 \edef\m_math_fraction_distance_bot{\mathfractionparameter\c!bottomdistance}
152 \ifempty\m_math_fraction_distance_top
153 \ifempty\m_math_fraction_distance_bot
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 \ifempty\m_math_fraction_distance_bot
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 {\enforced\let\m_fractions_strut_top\relax
174 \enforced\let\m_fractions_strut_bot\relax}
175
176\defcsname\??mathfractionstrut\v!yes\endcsname
177 {\enforced\let\m_fractions_strut_top\mathstrut
178 \enforced\let\m_fractions_strut_bot\mathstrut}
179
180\defcsname\??mathfractionstrut\v!math\endcsname
181 {\enforced\let\m_fractions_strut_top\mathstrut
182 \enforced\let\m_fractions_strut_bot\mathstrut}
183
184\letcsname\??mathfractionstrut\v!no\endcsname\math_frac_no_strut
185
186\defcsname\??mathfractionstrut\v!tight\endcsname
187 {\enforced\let\m_fractions_strut_top\mathbotstrut
188 \enforced\let\m_fractions_strut_bot\mathtopstrut}
189
190\math_frac_no_strut
191
192\newdimen\d_math_fraction_margin
193
194\protected\def\math_frac#1
195 {\begingroup
196 \edef\currentmathfraction{#1}
197
198 \edef\p_math_fraction_fences{\mathfractionparameter\c!fences}
199 \ifempty\p_math_fraction_fences \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 \ifempty\p_math_fractions_color
215 \expandafter\math_frac_normal
216 \else
217 \expandafter\math_frac_colored
218 \fi}
219
220\protected\def\math_frac_wrapup
221 {\ifempty\p_math_fraction_fences \else
222 \math_fenced_fenced_stop\p_math_fraction_fences
223 \fi
224 \endgroup}
225
226\protected\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\protected\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 \ifempty\p_math_fraction_fences
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\defcsname\??mathfractionalternative\v!inner\endcsname
259 {\ifcase\d_math_fraction_margin
260 \expandafter\math_fraction_inner_normal
261 \else
262 \expandafter\math_fraction_inner_margin
263 \fi}
264
265\defcsname\??mathfractionalternative\v!outer\endcsname
266 {\ifcase\d_math_fraction_margin
267 \expandafter\math_fraction_outer_normal
268 \else
269 \expandafter\math_fraction_outer_margin
270 \fi}
271
272\defcsname\??mathfractionalternative\v!both\endcsname
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
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\def\math_frac_command_u
358 {\clf_umathfraction
359 {\mathfractionparameter\c!rule}
360 \ifempty\p_math_fraction_fences
361 \mathfractionparameter\c!left \space
362 \mathfractionparameter\c!right\space
363 \else
364 \math_frac_no_delim\space
365 \math_frac_no_delim\space
366 \fi
367 \dimexpr\mathfractionparameter\c!rulethickness\relax
368 \relax}
369
370\def\math_fraction_inner_normal#1#2
371 {\Ustack{
372 \math_frac_command_u
373 {\usemathstyleparameter\mathfractionparameter{\m_fractions_strut_top#1}}
374 {\usemathstyleparameter\mathfractionparameter{\m_fractions_strut_bot#2}}
375 }
376 \math_frac_wrapup}
377
378\def\math_fraction_outer_normal#1#2
379 {\Ustack{
380 \usemathstyleparameter\mathfractionparameter
381 \math_frac_command_u
382 {\m_fractions_strut_top#1}
383 {\m_fractions_strut_bot#2}
384 }
385 \math_frac_wrapup}
386
387\def\math_fraction_both_normal#1#2
388 {\Ustack{
389 \usemathstyleparameter\mathfractionparameter
390 \math_frac_command_u
391 {\usemathstyleparameter\mathfractionparameter\m_fractions_strut_top#1}
392 {\usemathstyleparameter\mathfractionparameter\m_fractions_strut_bot#2}
393 }
394 \math_frac_wrapup}
395
396\def\math_fraction_inner_margin#1#2
397 {\Ustack{
398 {
399 \math_frac_command_u
400 {\kern\d_math_fraction_margin
401 \usemathstyleparameter\mathfractionparameter{\m_fractions_strut_top#1}
402 \kern\d_math_fraction_margin}
403 {\kern\d_math_fraction_margin
404 \usemathstyleparameter\mathfractionparameter{\m_fractions_strut_bot#2}
405 \kern\d_math_fraction_margin}
406 }
407 }
408 \math_frac_wrapup}
409
410\def\math_fraction_outer_margin#1#2
411 {\Ustack{
412 \usemathstyleparameter\mathfractionparameter
413 {
414 \math_frac_command_u
415 {\kern\d_math_fraction_margin
416 \m_fractions_strut_top#1
417 \kern\d_math_fraction_margin}
418 {\kern\d_math_fraction_margin
419 \m_fractions_strut_bot#2
420 \kern\d_math_fraction_margin}
421 }
422 }
423 \math_frac_wrapup}
424
425\def\math_fraction_both_margin#1#2
426 {\Ustack{
427 \usemathstyleparameter\mathfractionparameter
428 {
429 \math_frac_command_u
430 {\kern\d_math_fraction_margin
431 \usemathstyleparameter\mathfractionparameter\m_fractions_strut_top#1
432 \kern\d_math_fraction_margin}
433 {\kern\d_math_fraction_margin
434 \usemathstyleparameter\mathfractionparameter\m_fractions_strut_bot#2
435 \kern\d_math_fraction_margin}
436 }
437 }
438 \math_frac_wrapup}
439
440\definemathfraction[xfrac] [\c!alternative=\v!inner,\c!mathstyle=\s!script]
441\definemathfraction[xxfrac][\c!alternative=\v!inner,\c!mathstyle=\s!scriptscript]
442
443\aliased\let\normalxfrac\xfrac
444
445\permanent\protected\def\xfrac#1#2{\normalxfrac{\enforced\let\xfrac\xxfrac#1}{\enforced\let\xfrac\xxfrac#2}}
446
447
448
449
450
451\permanent\protected\def\nomathfractiongaps {\normalexpanded{\math_no_fraction_gaps \mathstyletrigger\mathstyle}}
452\permanent\protected\def\overlaymathfractiongaps{\normalexpanded{\math_overlay_fraction_gaps\mathstyletrigger\mathstyle}}
453
454\protected\def\math_no_fraction_gaps#1
455 {\Umathfractionnumup #1\zeropoint
456 \Umathfractiondenomdown#1\zeropoint}
457
458\protected\def\math_overlay_fraction_gaps#1
459 {\Umathfractionnumup #1\zeropoint
460 \Umathfractionnumvgap #1\zeropoint
461
462 \Umathfractiondenomvgap#1\zeropoint
463 \Umathfractiondenomdown#1\zeropoint}
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480\installcorenamespace{mathfractiondistance}
481
482\letcsname\??mathfractiondistance\v!none \endcsname\nomathfractiongaps
483\letcsname\??mathfractiondistance\v!no \endcsname\nomathfractiongaps
484\letcsname\??mathfractiondistance\v!overlay\endcsname\overlaymathfractiongaps
485
486\setupmathfractions
487 [\c!distance=\v!none]
488
489\appendtoks
490 \edef\p_distance{\rootmathfractionparameter\c!distance}
491 \ifempty\p_distance\else
492 \begincsname\??mathfractiondistance\p_distance\endcsname
493 \fi
494\to \everymathematics
495
496
497
498\let\math_fraction_set_threshold_inline \relax
499\let\math_fraction_set_threshold_display\relax
500
501\appendtoks
502 \math_fraction_set_threshold_inline
503 \math_fraction_set_threshold_display
504\to \everymathematics
505
506\appendtoks
507 \ifempty\currentmathfraction
508 \edef\p_threshold{\mathfractionparameter\c!inlinethreshold}
509 \ifempty\p_threshold
510 \let\math_fraction_set_threshold_inline\relax
511 \orelse\ifx\p_threshold\v!auto
512 \let\math_fraction_set_threshold_inline\relax
513 \else
514 \let\math_fraction_set_threshold_inline\math_fraction_set_theshold_inline
515 \fi
516 \edef\p_threshold{\mathfractionparameter\c!displaythreshold}
517 \ifempty\p_threshold
518 \let\math_fraction_set_threshold_display\relax
519 \orelse\ifx\p_threshold\v!auto
520 \let\math_fraction_set_threshold_display\relax
521 \else
522 \let\math_fraction_set_threshold_display\math_fraction_set_theshold_display
523 \fi
524 \fi
525\to \everysetupmathfraction
526
527\def\math_fraction_set_theshold_inline
528 {\edef\p_threshold{\mathfractionparameter\c!inlinethreshold}
529 \Umathfractiondelsize\textstyle \p_threshold\dimexpr\textface\relax
530 \Umathfractiondelsize\scriptstyle \p_threshold\dimexpr\scriptface\relax
531 \Umathfractiondelsize\scriptscriptstyle\p_threshold\dimexpr\scriptscriptface\relax}
532
533\def\math_fraction_set_theshold_display
534 {\edef\p_threshold{\mathfractionparameter\c!displaythreshold}
535 \Umathfractiondelsize\displaystyle \p_threshold\dimexpr\textface\relax}
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560\definemathfraction[i:frac] [\c!alternative=\v!inner,\c!mathstyle=]
561\definemathfraction[i:tfrac][\c!alternative=\v!inner,\c!mathstyle=\s!text]
562\definemathfraction[i:sfrac][\c!alternative=\v!inner,\c!mathstyle=\s!scriptscript]
563\definemathfraction[i:dfrac][\c!alternative=\v!inner,\c!mathstyle=\s!display]
564
565\definemathfraction[d:frac] [\c!alternative=\v!inner,\c!mathstyle=\s!cramped]
566\definemathfraction[d:tfrac][\c!alternative=\v!both ,\c!mathstyle={\s!cramped,\s!text}]
567\definemathfraction[d:sfrac][\c!alternative=\v!both ,\c!mathstyle={\s!cramped,\s!scriptscript}]
568\definemathfraction[d:dfrac][\c!alternative=\v!inner,\c!mathstyle=\s!display]
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586\permanent\protected\def\frac {\csname\inlineordisplaymath id:frac\endcsname}
587\permanent\protected\def\tfrac{\csname\inlineordisplaymath id:tfrac\endcsname}
588\permanent\protected\def\sfrac{\csname\inlineordisplaymath id:sfrac\endcsname}
589\permanent\protected\def\dfrac{\csname\inlineordisplaymath id:dfrac\endcsname}
590
591\definemathfraction[ams] [\c!strut=\v!no,\c!alternative=\v!outer]
592\definemathfraction[i:ams:frac][ams][\c!mathstyle={\s!cramped,\s!text}]
593\definemathfraction[d:ams:frac][ams][\c!mathstyle={\s!cramped,\s!display}]
594
595\permanent\protected\def\ctxfrac{\csname\inlineordisplaymath id:frac\endcsname}
596\permanent\protected\def\amsfrac{\csname\inlineordisplaymath id:ams:frac\endcsname}
597
598
599
600
601
602
603
604\appendtoks
605 \doifelse{\mathematicsparameter\c!fractions}{ams}
606 {\enforced\let\frac\amsfrac}
607 {\enforced\let\frac\ctxfrac}
608\to \everysetupmathematics
609
610
611
612
613
614
615
616
617
618\definemathfraction
619 [binom]
620 [\c!alternative=\v!outer,
621 \c!rule=\v!no,
622 \c!left=0x28,
623 \c!right=0x29,
624 \c!mathstyle=\s!auto]
625
626\definemathfraction
627 [dbinom]
628 [binom]
629 [\c!mathstyle=\s!display]
630
631\definemathfraction
632 [tbinom]
633 [binom]
634 [\c!mathstyle=\s!text]
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661\permanent\protected\def\cfrac
662 {\doifelsenextoptionalcs\math_cfrac_yes\math_cfrac_nop}
663
664\def\math_cfrac_nop {\math_cfrac_indeed[cc]}
665\def\math_cfrac_yes[#1]{\math_cfrac_indeed[#1cc]}
666
667\def\math_cfrac_indeed[#1#2#3]#4#5
668 {{\displaystyle
669 \frac
670 {\strut
671 \ifx r#1\hfill\fi#4\ifx l#1\hfill\fi}
672 {\ifx r#2\hfill\fi#5\ifx l#2\hfill\fi}
673 \kern\nulldelimiterspace}}
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\permanent\protected\def\splitfrac #1#2{{\textstyle {{\textstyle#1\quad\hfill}\normalabove\zeropoint{\textstyle\hfill\quad\mathstrut#2}}}}
702\permanent\protected\def\splitdfrac#1#2{{\displaystyle{{ #1\quad\hfill}\normalabove\zeropoint{ \hfill\quad\mathstrut#2}}}}
703
704
705
706
707
708
709
710
711\protected\def\math_frac_colored_vulgar#1#2
712 {\savecolor
713 \colo_helpers_activate\p_math_fractions_color
714 {\restorecolor#1}\Uskewed{\restorecolor#2}}
715
716\protected\def\math_frac_normal_vulgar#1#2
717 {{#1}\Uskewed{#2}}
718
719\permanent\protected\def\vfrac#1#2
720 {\bgroup
721 \edef\p_math_fractions_color{\namedmathfractionparameter\s!vfrac\c!color}
722 \ifempty\p_math_fractions_color
723 \expandafter\math_frac_normal_vulgar
724 \else
725 \expandafter\math_frac_colored_vulgar
726 \fi
727 {#1}
728 {#2}
729 \egroup}
730
731\appendtoks
732 \edef\p_hfactor{\namedmathfractionparameter\s!vfrac\c!hfactor}
733 \edef\p_vfactor{\namedmathfractionparameter\s!vfrac\c!vfactor}
734 \Umathskewedfractionhgap\textstyle \p_hfactor\fontemwidth \mathstylefont\textstyle
735 \Umathskewedfractionhgap\scriptstyle \p_hfactor\fontemwidth \mathstylefont\scriptstyle
736 \Umathskewedfractionhgap\scriptscriptstyle\p_hfactor\fontemwidth \mathstylefont\scriptscriptstyle
737 \Umathskewedfractionvgap\textstyle \p_vfactor\fontexheight\mathstylefont\textstyle
738 \Umathskewedfractionvgap\scriptstyle \p_vfactor\fontexheight\mathstylefont\scriptstyle
739 \Umathskewedfractionvgap\scriptscriptstyle\p_vfactor\fontexheight\mathstylefont\scriptscriptstyle
740\to \everysetupmathfraction
741
742\setupmathfraction
743 [\s!vfrac]
744 [\c!hfactor=.2,
745 \c!vfactor=.1]
746
747\protect \endinput
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832 |