math-frc.mkxl /size: 26 Kb    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=math-frc,
3%D        version=2013.04.06, % 2007.07.19,
4%D          title=\CONTEXT\ Math Macros,
5%D       subtitle=Fractions,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Math Macros / Fractions}
15
16\unprotect
17
18%D I need to check it all again as there was a bug in luatex with dimensions that could
19%D resulted in side effects that made me mess with spacing.
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%D This module is reimplemented in \MKIV\ style.
28
29\registerctxluafile{math-frc}{}
30
31%D \macros
32%D   {frac, xfrac, xxfrac}
33%D
34%D This is another one Tobias asked for. It replaces the primitive \type
35%D {\over}. We also take the opportunity to handle math style restoring,
36%D which makes sure units and chemicals come out ok. The \type {\frac}
37%D macro kind of replaces the awkward \type {\over} primitive. Say that
38%D we have the following formulas:
39%D
40%D \startbuffer[sample]
41%D test $\frac  {1}{2}$ test $$1 + \frac  {1}{2} = 1.5$$
42%D test $\xfrac {1}{2}$ test $$1 + \xfrac {1}{2} = 1.5$$
43%D test $\xxfrac{1}{2}$ test $$1 + \xxfrac{1}{2} = 1.5$$
44%D \stopbuffer
45%D
46%D \typebuffer[sample]
47%D
48%D With the most straightforward definitions, we get:
49%D
50%D \startbuffer[code]
51%D \def\dofrac#1#2#3{\relax\mathematics{{{#1{#2}}\over{#1{#3}}}}}
52%D
53%D \def\frac  {\dofrac\mathstyle}
54%D \def\xfrac {\dofrac\scriptstyle}
55%D \def\xxfrac{\dofrac\scriptscriptstyle}
56%D \stopbuffer
57%D
58%D \typebuffer[code] \getbuffer[code,sample]
59%D
60%D Since this does not work well, we can try:
61%D
62%D \startbuffer[code]
63%D \def\xfrac #1#2{\hbox{$\dofrac\scriptstyle      {#1}{#2}$}}
64%D \def\xxfrac#1#2{\hbox{$\dofrac\scriptscriptstyle{#1}{#2}$}}
65%D \stopbuffer
66%D
67%D \typebuffer[code] \getbuffer[code,sample]
68%D
69%D This for sure looks better than:
70%D
71%D \startbuffer[code]
72%D \def\xfrac #1#2{{\scriptstyle      \dofrac\relax{#1}{#2}}}
73%D \def\xxfrac#1#2{{\scriptscriptstyle\dofrac\relax{#1}{#2}}}
74%D \stopbuffer
75%D
76%D \typebuffer[code] \getbuffer[code,sample]
77%D
78%D So we stick to the next definitions (watch the local overloading of
79%D \type {\xfrac}).
80%D
81%D In the meantime, in \LUATEX, we have better control over styles so the
82%D following macros are different from the \MKII\ ones.
83
84% obsolete, is now c!mathstyle
85%
86% 0=auto, 1=displaystyle, 2=textstyle, 3=scriptstyle, 4=scriptscriptstyle, 5=mathstyle
87%
88% $\mathfracmode0 \frac{1}{2}$
89% $\mathfracmode1 \frac{1}{2}$
90% $\mathfracmode2 \frac{1}{2}$
91% $\mathfracmode3 \frac{1}{2}$
92% $\mathfracmode4 \frac{1}{2}$
93% $\mathfracmode5 \frac{1}{2}$
94%
95% we keep the constant for a while
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% color only applies to rule, use regular color for rest
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% Sometimes users want control over the distances:
126
127\let\math_fraction_set_distance\relax
128
129\appendtoks
130    \math_fraction_set_distance
131\to \everymathematics
132
133% why only displaystyle .. a bit weak
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% So far for control.
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  % indeed swapped name
188   \enforced\let\m_fractions_strut_bot\mathtopstrut} % indeed swapped name
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% we use utfchar anyway so we can as well do all at the lua end
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% Having a \withmarginornot{#1}{#2} makes not much sense nor do 4 tests or 4 redundant
252% kerns (longer node lists plus possible interference). A split in normal and margin
253% also makes testing easier. When left and right margins are needed we might merge the
254% variants again. After all, these are not real installers.
255
256% the denominator is in cramped!
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% todo: store first state and reuse second time
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  n_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\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{% forces num style
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{% forces num style
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{% forces num style
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%D Spacing (sensitive for definition of \triggermathstyle:
448
449% \ifcase\contextlmtxmode
450
451\permanent\protected\def\nomathfractiongaps     {\normalexpanded{\math_no_fraction_gaps     \mathstyletrigger\mathstyle}} % maybe collect settings
452\permanent\protected\def\overlaymathfractiongaps{\normalexpanded{\math_overlay_fraction_gaps\mathstyletrigger\mathstyle}} % maybe collect settings
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   %Umathfractionrule     #1\zeropoint
462   \Umathfractiondenomvgap#1\zeropoint
463   \Umathfractiondenomdown#1\zeropoint}
464
465% \else
466
467% \protected\def\math_no_fraction_gaps
468%   {\Umathfractionnumup    \mathstyle\zeropoint
469%    \Umathfractiondenomdown\mathstyle\zeropoint}
470%
471% \protected\def\math_overlay_fraction_gaps
472%   {\Umathfractionnumup    \mathstyle\zeropoint
473%    \Umathfractionnumvgap  \mathstyle\zeropoint
474%    %Umathfractionrule     \mathstyle\zeropoint
475%    \Umathfractiondenomvgap\mathstyle\zeropoint
476%    \Umathfractiondenomdown\mathstyle\zeropoint}
477
478% \fi
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% theshold is new!
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%D \macros
538%D   {dfrac, tfrac, frac, dbinom, tbinom, binom}
539%D
540%D \startbuffer
541%D $\dfrac {1}{2} \tfrac {1}{2} \frac {1}{2}$
542%D $\dbinom{1}{2} \tbinom{1}{2} \binom{1}{2}$
543%D \stopbuffer
544%D
545%D \typebuffer
546%D \getbuffer
547
548% $$
549%     {{a}\over{b}} +
550%     {{a}\overwithdelims(){b}} +
551%     {{a}\atopwithdelims(){b}} +
552%     {{a}\abovewithdelims()\zeropoint{b}} +
553%     \left({{a}\over{b}}\right)
554% $$
555
556% \dorecurse {10} { % weird
557%     $$ {{a}\abovewithdelims()#1pt{b}} $$
558% }
559
560\definemathfraction[i:frac] [\c!alternative=\v!inner,\c!mathstyle=]  % was script and then small but nothing needed
561\definemathfraction[i:tfrac][\c!alternative=\v!inner,\c!mathstyle=\s!text] % was script (before luatex fix)
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] % was cramped,text
566\definemathfraction[d:tfrac][\c!alternative=\v!both ,\c!mathstyle={\s!cramped,\s!text}] % was cramped,script (before luatex fix)
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%D \protected\def\ShowMathFractions#1#2%
571%D   {\dontleavehmode
572%D    \begingroup
573%D    \showmathstruts
574%D    \mathematics{x+\tfrac{#1}{#2}+1+\frac{#1}{#2}+2+\sfrac{#1}{#2}+g}%
575%D    \endgroup}
576%D
577%D The default \type {tfrac}, \type {frac} and \type \sfrac} look like this:
578%D
579%D \blank
580%D \ShowMathFractions{a}{a}\par
581%D \ShowMathFractions{1}{x}\par
582%D \ShowMathFractions{a}{b}\par
583%D \ShowMathFractions{1}{b}\par
584%D \blank
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% \appendtoks
599%     \doifelse{\mathfractionparameter\c!option}{ams}%
600%       {\let\frac\amsfrac}%
601%       {\let\frac\ctxfrac}%
602% \to \everysetupmathfraction
603
604\appendtoks
605    \doifelse{\mathematicsparameter\c!fractions}{ams}%
606      {\enforced\let\frac\amsfrac}%
607      {\enforced\let\frac\ctxfrac}%
608\to \everysetupmathematics
609
610% \definemathfraction[ddfrac][\c!mathstyle=\s!display]
611% \definemathfraction[ttfrac][\c!mathstyle=\s!text]
612% \definemathfraction[ssfrac][\c!mathstyle=\s!script]
613
614% \protected\def\binom #1#2{{\Ustack      {{#1}\normalabovewithdelims()\zeropoint{#2}}}}
615% \protected\def\dbinom#1#2{{\displaystyle{{#1}\normalabovewithdelims()\zeropoint{#2}}}}
616% \protected\def\tbinom#1#2{{\textstyle   {{#1}\normalabovewithdelims()\zeropoint{#2}}}}
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%D \macros
637%D   {cfrac}
638%D
639%D \startbuffer
640%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
641%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
642%D \stopbuffer
643%D
644%D \typebuffer
645%D
646%D \getbuffer
647%D
648%D Now we can align every combination we want:
649%D
650%D \startbuffer
651%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
652%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
653%D $\cfrac[cl]{12}{3} \cfrac[cc]{12}{3} \cfrac[cr]{12}{3}$
654%D $\cfrac[lc]{1}{23} \cfrac[cc]{1}{23} \cfrac[rc]{1}{23}$
655%D \stopbuffer
656%D
657%D \typebuffer
658%D
659%D \getbuffer
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%D \macros
676%D   {splitfrac, splitdfrac}
677%D
678%D Occasionally one needs to typeset multi||line fractions.
679%D These commands use \tex{genfrac} to create such fractions.
680%D
681%D \startbuffer
682%D \startformula
683%D      a=\frac{
684%D          \splitfrac{xy + xy + xy + xy + xy}
685%D                    {+ xy + xy + xy + xy}
686%D        }
687%D        {z}
688%D      =\frac{
689%D          \splitdfrac{xy + xy + xy + xy + xy}
690%D                    {+ xy + xy + xy + xy}
691%D        }
692%D        {z}
693%D \stopformula
694%D \stopbuffer
695%D
696%D \typebuffer \getbuffer
697%D
698%D These macros are based on Michael J.~Downes posting on
699%D comp.text.tex on 2001/12/06 but adapted a bit.
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%D For testing:
705
706% \protected\def\ShowMathFractions#1#2%
707%  {\mathematics{x+\tfrac{#1}{#2}+1+\frac{#1}{#2}+2+\sfrac{#1}{#2}+g}}
708
709%D More fracking (for Alan):
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% I have no clue what \mthfrac and \mthsqrt are supposed to do but
750% I guess that it can be done with tweaking luatex's math parameters.
751% Otherwise I'll write something from scratch.
752
753% \def\math_stylebuilders_frac#1#2#3#4#5#6#7%
754%   {\begingroup
755%    \mathsurround\zeropoint
756%    \setbox0\hbox{$#1 #6$}%
757%    \setbox2\hbox{$#1 #7$}%
758%    \dimen0\wd\ifdim\wd2>\wd0 2\else 0\fi
759%    \setbox4\hbox to \dimen0{\leaders\hbox{#4}\hss#5}%
760%    \mathord{\vcenter{{\offinterlineskip
761%      \hbox to \dimen0{\hss\box0\hss}%
762%      \kern\ht4%
763%      \hbox to \dimen0{\hss\copy4\hss}%
764%      \kern\ht4%
765%      \hbox to \dimen0{\hss\box2\hss}}}}%
766%    \endgroup}
767%
768% \def\math_stylebuilders_sqrt#1#2#3#4#5%
769%   {\begingroup
770%    \mathsurround\zeropoint
771%    \setbox0\hbox{$#1 #5$}%
772%    \ht0\dimexpr1.05\ht0+\onepoint\relax
773%    \dp0\dimexpr1.05\dp0+\onepoint\relax
774%    \setbox4\hbox to \wd0{\mr#2\leaders\hbox{#3}\hfill#4}%
775%    \delimitershortfall\zeropoint
776%    \nulldelimiterspace\zeropoint
777%    \setbox2\hbox{$\left\delimiter"0270370 \vrule \s!height\ht0 \s!depth \dp0 \s!width\zeropoint\right.$}% is this the right code point?
778%    \mathord{\vcenter{\hbox{\copy2\rlap{\raise\dimexpr\ht2-\ht4\relax\copy4}\copy0}}}%
779%    \endgroup}
780%
781% \def\mthfrac#1#2#3#4#5{\mathchoice
782%   {\math_stylebuilders_frac\displaystyle     \textface        {#1}{#2}{#3}{#4}{#5}}%
783%   {\math_stylebuilders_frac\textstyle        \textface        {#1}{#2}{#3}{#4}{#5}}%
784%   {\math_stylebuilders_frac\scriptstyle      \scriptface      {#1}{#2}{#3}{#4}{#5}}%
785%   {\math_stylebuilders_frac\scriptscriptstyle\scriptscriptface{#1}{#2}{#3}{#4}{#5}}}
786%
787% \def\mthsqrt#1#2#3{\mathchoice
788%   {\math_stylebuilders_sqrt\displaystyle     \textface{#1}{#2}{#3}}%
789%   {\math_stylebuilders_sqrt\textstyle        \textface{#1}{#2}{#3}}%
790%   {\math_stylebuilders_sqrt\scriptstyle      \textface{#1}{#2}{#3}}%
791%   {\math_stylebuilders_sqrt\scriptscriptstyle\textface{#1}{#2}{#3}}}
792
793% \protected\def\mthfrac#1#2#3{[mthfrac: #1 #2 #3]}
794% \protected\def\mthsqrt#1#2#3{[mthsqrt: #1 #2 #3]}
795
796% used for prototyping \Uskewed
797%
798% \protected\def\skewedfractiona#1#2{%
799%     \raise
800%         \Umathskewedfractionvgap\textstyle
801%         \hbox\bgroup
802%             $\scriptstyle#1\hskip\dimexpr\Umathskewedfractionhgap\scriptstyle/2\relax$%
803%         \egroup
804%         \hbox to \zeropoint\bgroup
805%             \hss$\textstyle/$\hss
806%         \egroup
807%     \lower
808%         \Umathskewedfractionvgap\textstyle
809%         \hbox\bgroup
810%             $\hskip\dimexpr\Umathskewedfractionhgap\scriptstyle/2\relax\scriptstyle#2$%
811%         \egroup
812% }
813%
814% \protected\def\skewedfractionb#1#2{%
815%     \raise
816%         \Umathskewedfractionvgap\textstyle
817%         \hbox\bgroup
818%             $\scriptstyle#1\hskip\dimexpr\Umathskewedfractionhgap\textstyle/2\relax$%
819%         \egroup
820%         \hbox to \zeropoint\bgroup
821%             \hss$\textstyle/$\hss
822%         \egroup
823%     \lower
824%         \Umathskewedfractionvgap\textstyle
825%         \hbox\bgroup
826%             $\hskip\dimexpr\Umathskewedfractionhgap\textstyle/2\relax\scriptstyle#2$%
827%         \egroup
828% }
829%
830% $\skewedfractiona{1}{2}$
831% $\skewedfractionb{1}{2}$
832