math-frc.mkiv /size: 23 Kb    last modification: 2021-10-28 13:50
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\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%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\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    \setuevalue{\currentmathfraction}{\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\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% So far for control.
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  % indeed swapped name
188   \let\m_fractions_strut_bot\mathtopstrut} % indeed swapped name
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% 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     \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% 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\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% 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\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%D Spacing (sensitive for definition of \triggermathstyle:
365
366% \ifcase\contextlmtxmode
367
368\unexpanded\def\nomathfractiongaps     {\normalexpanded{\math_no_fraction_gaps     \mathstyletrigger\mathstyle}} % maybe collect settings
369\unexpanded\def\overlaymathfractiongaps{\normalexpanded{\math_overlay_fraction_gaps\mathstyletrigger\mathstyle}} % maybe collect settings
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   %Umathfractionrule     #1\zeropoint
379   \Umathfractiondenomvgap#1\zeropoint
380   \Umathfractiondenomdown#1\zeropoint}
381
382% \else
383
384% \unexpanded\def\math_no_fraction_gaps
385%   {\Umathfractionnumup    \mathstyle\zeropoint
386%    \Umathfractiondenomdown\mathstyle\zeropoint}
387%
388% \unexpanded\def\math_overlay_fraction_gaps
389%   {\Umathfractionnumup    \mathstyle\zeropoint
390%    \Umathfractionnumvgap  \mathstyle\zeropoint
391%    %Umathfractionrule     \mathstyle\zeropoint
392%    \Umathfractiondenomvgap\mathstyle\zeropoint
393%    \Umathfractiondenomdown\mathstyle\zeropoint}
394
395% \fi
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% theshold is new!
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%D \macros
457%D   {dfrac, tfrac, frac, dbinom, tbinom, binom}
458%D
459%D \startbuffer
460%D $\dfrac {1}{2} \tfrac {1}{2} \frac {1}{2}$
461%D $\dbinom{1}{2} \tbinom{1}{2} \binom{1}{2}$
462%D \stopbuffer
463%D
464%D \typebuffer
465%D \getbuffer
466
467% $$
468%     {{a}\over{b}} +
469%     {{a}\overwithdelims(){b}} +
470%     {{a}\atopwithdelims(){b}} +
471%     {{a}\abovewithdelims()\zeropoint{b}} +
472%     \left({{a}\over{b}}\right)
473% $$
474
475% \dorecurse {10} { % weird
476%     $$ {{a}\abovewithdelims()#1pt{b}} $$
477% }
478
479\definemathfraction[i:frac] [\c!alternative=\v!inner,\c!mathstyle=]  % was script and then small but nothing needed
480\definemathfraction[i:tfrac][\c!alternative=\v!inner,\c!mathstyle=\s!text] % was script (before luatex fix)
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] % was cramped,text
485\definemathfraction[d:tfrac][\c!alternative=\v!both ,\c!mathstyle={\s!cramped,\s!text}] % was cramped,script (before luatex fix)
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%D \unexpanded\def\ShowMathFractions#1#2%
490%D   {\dontleavehmode
491%D    \begingroup
492%D    \showmathstruts
493%D    \mathematics{x+\tfrac{#1}{#2}+1+\frac{#1}{#2}+2+\sfrac{#1}{#2}+g}%
494%D    \endgroup}
495%D
496%D The default \type {tfrac}, \type {frac} and \type \sfrac} look like this:
497%D
498%D \blank
499%D \ShowMathFractions{a}{a}\par
500%D \ShowMathFractions{1}{x}\par
501%D \ShowMathFractions{a}{b}\par
502%D \ShowMathFractions{1}{b}\par
503%D \blank
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% \appendtoks
518%     \doifelse{\mathfractionparameter\c!option}{ams}%
519%       {\let\frac\amsfrac}%
520%       {\let\frac\ctxfrac}%
521% \to \everysetupmathfraction
522
523\appendtoks
524    \doifelse{\mathematicsparameter\c!fractions}{ams}%
525      {\let\frac\amsfrac}%
526      {\let\frac\ctxfrac}%
527\to \everysetupmathematics
528
529% \definemathfraction[ddfrac][\c!mathstyle=\s!display]
530% \definemathfraction[ttfrac][\c!mathstyle=\s!text]
531% \definemathfraction[ssfrac][\c!mathstyle=\s!script]
532
533% \unexpanded\def\binom #1#2{{\Ustack      {{#1}\normalabovewithdelims()\zeropoint{#2}}}}
534% \unexpanded\def\dbinom#1#2{{\displaystyle{{#1}\normalabovewithdelims()\zeropoint{#2}}}}
535% \unexpanded\def\tbinom#1#2{{\textstyle   {{#1}\normalabovewithdelims()\zeropoint{#2}}}}
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%D \macros
556%D   {cfrac}
557%D
558%D \startbuffer
559%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
560%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
561%D \stopbuffer
562%D
563%D \typebuffer
564%D
565%D \getbuffer
566%D
567%D Now we can align every combination we want:
568%D
569%D \startbuffer
570%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
571%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
572%D $\cfrac[cl]{12}{3} \cfrac[cc]{12}{3} \cfrac[cr]{12}{3}$
573%D $\cfrac[lc]{1}{23} \cfrac[cc]{1}{23} \cfrac[rc]{1}{23}$
574%D \stopbuffer
575%D
576%D \typebuffer
577%D
578%D \getbuffer
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%D \macros
595%D   {splitfrac, splitdfrac}
596%D
597%D Occasionally one needs to typeset multi||line fractions.
598%D These commands use \tex{genfrac} to create such fractions.
599%D
600%D \startbuffer
601%D \startformula
602%D      a=\frac{
603%D          \splitfrac{xy + xy + xy + xy + xy}
604%D                    {+ xy + xy + xy + xy}
605%D        }
606%D        {z}
607%D      =\frac{
608%D          \splitdfrac{xy + xy + xy + xy + xy}
609%D                    {+ xy + xy + xy + xy}
610%D        }
611%D        {z}
612%D \stopformula
613%D \stopbuffer
614%D
615%D \typebuffer \getbuffer
616%D
617%D These macros are based on Michael J.~Downes posting on
618%D comp.text.tex on 2001/12/06 but adapted a bit.
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%D For testing:
627
628% \unexpanded\def\ShowMathFractions#1#2%
629%  {\mathematics{x+\tfrac{#1}{#2}+1+\frac{#1}{#2}+2+\sfrac{#1}{#2}+g}}
630
631%D More fracking (for Alan):
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% I have no clue what \mthfrac and \mthsqrt are supposed to do but
672% I guess that it can be done with tweaking luatex's math parameters.
673% Otherwise I'll write something from scratch.
674
675% \def\math_stylebuilders_frac#1#2#3#4#5#6#7%
676%   {\begingroup
677%    \mathsurround\zeropoint
678%    \setbox0\hbox{$#1 #6$}%
679%    \setbox2\hbox{$#1 #7$}%
680%    \dimen0\wd\ifdim\wd2>\wd0 2\else 0\fi
681%    \setbox4\hbox to \dimen0{\leaders\hbox{#4}\hss#5}%
682%    \mathord{\vcenter{{\offinterlineskip
683%      \hbox to \dimen0{\hss\box0\hss}%
684%      \kern\ht4%
685%      \hbox to \dimen0{\hss\copy4\hss}%
686%      \kern\ht4%
687%      \hbox to \dimen0{\hss\box2\hss}}}}%
688%    \endgroup}
689%
690% \def\math_stylebuilders_sqrt#1#2#3#4#5%
691%   {\begingroup
692%    \mathsurround\zeropoint
693%    \setbox0\hbox{$#1 #5$}%
694%    \ht0\dimexpr1.05\ht0+\onepoint\relax
695%    \dp0\dimexpr1.05\dp0+\onepoint\relax
696%    \setbox4\hbox to \wd0{\mr#2\leaders\hbox{#3}\hfill#4}%
697%    \delimitershortfall\zeropoint
698%    \nulldelimiterspace\zeropoint
699%    \setbox2\hbox{$\left\delimiter"0270370 \vrule \s!height\ht0 \s!depth \dp0 \s!width\zeropoint\right.$}% is this the right code point?
700%    \mathord{\vcenter{\hbox{\copy2\rlap{\raise\dimexpr\ht2-\ht4\relax\copy4}\copy0}}}%
701%    \endgroup}
702%
703% \def\mthfrac#1#2#3#4#5{\mathchoice
704%   {\math_stylebuilders_frac\displaystyle     \textface        {#1}{#2}{#3}{#4}{#5}}%
705%   {\math_stylebuilders_frac\textstyle        \textface        {#1}{#2}{#3}{#4}{#5}}%
706%   {\math_stylebuilders_frac\scriptstyle      \scriptface      {#1}{#2}{#3}{#4}{#5}}%
707%   {\math_stylebuilders_frac\scriptscriptstyle\scriptscriptface{#1}{#2}{#3}{#4}{#5}}}
708%
709% \def\mthsqrt#1#2#3{\mathchoice
710%   {\math_stylebuilders_sqrt\displaystyle     \textface{#1}{#2}{#3}}%
711%   {\math_stylebuilders_sqrt\textstyle        \textface{#1}{#2}{#3}}%
712%   {\math_stylebuilders_sqrt\scriptstyle      \textface{#1}{#2}{#3}}%
713%   {\math_stylebuilders_sqrt\scriptscriptstyle\textface{#1}{#2}{#3}}}
714
715% \unexpanded\def\mthfrac#1#2#3{[mthfrac: #1 #2 #3]}
716% \unexpanded\def\mthsqrt#1#2#3{[mthsqrt: #1 #2 #3]}
717
718% used for prototyping \Uskewed
719%
720% \unexpanded\def\skewedfractiona#1#2{%
721%     \raise
722%         \Umathskewedfractionvgap\textstyle
723%         \hbox\bgroup
724%             $\scriptstyle#1\hskip\dimexpr\Umathskewedfractionhgap\scriptstyle/2\relax$%
725%         \egroup
726%         \hbox to \zeropoint\bgroup
727%             \hss$\textstyle/$\hss
728%         \egroup
729%     \lower
730%         \Umathskewedfractionvgap\textstyle
731%         \hbox\bgroup
732%             $\hskip\dimexpr\Umathskewedfractionhgap\scriptstyle/2\relax\scriptstyle#2$%
733%         \egroup
734% }
735%
736% \unexpanded\def\skewedfractionb#1#2{%
737%     \raise
738%         \Umathskewedfractionvgap\textstyle
739%         \hbox\bgroup
740%             $\scriptstyle#1\hskip\dimexpr\Umathskewedfractionhgap\textstyle/2\relax$%
741%         \egroup
742%         \hbox to \zeropoint\bgroup
743%             \hss$\textstyle/$\hss
744%         \egroup
745%     \lower
746%         \Umathskewedfractionvgap\textstyle
747%         \hbox\bgroup
748%             $\hskip\dimexpr\Umathskewedfractionhgap\textstyle/2\relax\scriptstyle#2$%
749%         \egroup
750% }
751%
752% $\skewedfractiona{1}{2}$
753% $\skewedfractionb{1}{2}$
754