math-frc.mkxl /size: 29 Kb    last modification: 2024-01-16 09:02
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% todo : mathclass=\mathnothingcode, % todo also accept string
15
16\writestatus{loading}{ConTeXt Math Macros / Fractions}
17
18\unprotect
19
20%D This module is reimplemented in \MKIV\ style.
21
22\registerctxluafile{math-frc}{autosuffix}
23
24%D \macros
25%D   {frac, xfrac, xxfrac}
26%D
27%D This is another one Tobias asked for. It replaces the primitive \type {\over}. We
28%D also take the opportunity to handle math style restoring, which makes sure units
29%D and chemicals come out ok. The \type {\frac} macro kind of replaces the awkward
30%D \type {\over} primitive. Say that we have the following formulas:
31%D
32%D \startbuffer[sample]
33%D test $\frac  {1}{2}$ test $$1 + \frac  {1}{2} = 1.5$$
34%D test $\xfrac {1}{2}$ test $$1 + \xfrac {1}{2} = 1.5$$
35%D test $\xxfrac{1}{2}$ test $$1 + \xxfrac{1}{2} = 1.5$$
36%D \stopbuffer
37%D
38%D \typebuffer[sample]
39%D
40%D With the most straightforward definitions, we get:
41%D
42%D \startbuffer[code]
43%D \def\dofrac#1#2#3{\relax\mathematics{{{#1{#2}}\over{#1{#3}}}}}
44%D
45%D \def\frac  {\dofrac\mathstyle}
46%D \def\xfrac {\dofrac\scriptstyle}
47%D \def\xxfrac{\dofrac\scriptscriptstyle}
48%D \stopbuffer
49%D
50%D \typebuffer[code] \getbuffer[code,sample]
51%D
52%D Since this does not work well, we can try:
53%D
54%D \startbuffer[code]
55%D \def\xfrac #1#2{\hbox{$\dofrac\scriptstyle      {#1}{#2}$}}
56%D \def\xxfrac#1#2{\hbox{$\dofrac\scriptscriptstyle{#1}{#2}$}}
57%D \stopbuffer
58%D
59%D \typebuffer[code] \getbuffer[code,sample]
60%D
61%D This for sure looks better than:
62%D
63%D \startbuffer[code]
64%D \def\xfrac #1#2{{\scriptstyle      \dofrac\relax{#1}{#2}}}
65%D \def\xxfrac#1#2{{\scriptscriptstyle\dofrac\relax{#1}{#2}}}
66%D \stopbuffer
67%D
68%D \typebuffer[code] \getbuffer[code,sample]
69%D
70%D So we stick to the next definitions (watch the local overloading of
71%D \type {\xfrac}).
72%D
73%D In the meantime, in \LUATEX, we have better control over styles so the following
74%D macros are different from the \MKII\ ones.
75
76\installcorenamespace{mathfractions}
77\installcorenamespace{mathfractionstyle}
78\installcorenamespace{mathfractionalternative}
79
80\installcommandhandler \??mathfractions {mathfraction} \??mathfractions
81
82\aliased\let\setupmathfractions\setupmathfraction
83
84% color only applies to rule, use regular color for rest
85
86\setupmathfractions
87  [\c!mathstyle=,
88   \c!method=\v!vertical,
89   \c!mathnumeratorstyle=\mathfractionparameter\c!mathstyle,
90   \c!mathdenominatorstyle=\mathfractionparameter\c!mathstyle,
91   \c!mathclass=\mathfractioncode,
92   \c!alternative=\v!inner,
93   \c!margin=\zeropoint,
94 % \c!rulethickness=.1\exheight, % was .25 for above / atop
95   \c!rulethickness=\v!font,
96   \c!left="2E,
97   \c!right="2E,
98   \c!middle="2F,
99   \c!strut=\v!yes,
100   \c!topdistance=,
101   \c!bottomdistance=,
102   \c!source=\zerocount,
103   \c!hfactor=\plusthousand,
104   \c!vfactor=\plusthousand,
105   \c!rule=\v!auto]
106
107%D We now default to nice bars:
108
109\integerdef\fractionbarextenderuc \privatecharactercode{fraction bar extender}
110
111\setupmathfractions
112  [\c!rule=\v!symbol,
113   \c!symbol=\fractionbarextenderuc]
114
115\appendtoks
116    \instance\frozen\protected\edefcsname\currentmathfraction\endcsname{\math_frac{\currentmathfraction}}%
117\to \everydefinemathfraction
118
119% Sometimes users want control over the distances:
120
121\let\math_fraction_set_distance\relax
122
123\appendtoks
124    \math_fraction_set_distance
125\to \everymathematics
126
127% why only displaystyle .. a bit weak .. will become a set
128
129\protected\def\math_fraction_set_distance_top
130  {\Umathfractionnumup    \displaystyle\m_math_fraction_distance_top
131   \relax}
132
133\protected\def\math_fraction_set_distance_bot
134  {\Umathfractiondenomdown\displaystyle\m_math_fraction_distance_bot
135   \relax}
136
137\protected\def\math_fraction_set_distance_all
138  {\Umathfractionnumup    \displaystyle\m_math_fraction_distance_top
139   \Umathfractiondenomdown\displaystyle\m_math_fraction_distance_bot
140   \relax}
141
142\appendtoks
143    \ifempty\currentmathfraction
144      \edef\m_math_fraction_distance_top{\mathfractionparameter\c!topdistance}%
145      \edef\m_math_fraction_distance_bot{\mathfractionparameter\c!bottomdistance}%
146      \ifempty\m_math_fraction_distance_top
147        \ifempty\m_math_fraction_distance_bot
148          \let\math_fraction_set_distance\relax
149        \else
150          \let\math_fraction_set_distance\math_fraction_set_distance_bot
151        \fi
152      \else
153        \ifempty\m_math_fraction_distance_bot
154          \let\math_fraction_set_distance\math_fraction_set_distance_top
155        \else
156          \let\math_fraction_set_distance\math_fraction_set_distance_all
157        \fi
158      \fi
159    \fi
160\to \everysetupmathfraction
161
162% So far for control.
163
164% no    : \ruledhbox{\m{\frac[strut=no]   {28}{8\times29}}} \par
165% tight : \ruledhbox{\m{\frac[strut=tight]{28}{8\times29}}} \par
166% yes   : \ruledhbox{\m{\frac[strut=yes]  {28}{8\times29}}} \par
167% math  : \ruledhbox{\m{\frac[strut=math] {28}{8\times29}}} \par
168% text  : \ruledhbox{\m{\frac[strut=text] {28}{8\times29}}} \par
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\mathdepthstrut
188   \enforced\let\m_fractions_strut_bot\mathheightstrut}
189
190\defcsname\??mathfractionstrut\v!text\endcsname
191  {\enforced\let\m_fractions_strut_top\strut
192   \enforced\let\m_fractions_strut_bot\strut}
193
194\math_frac_no_strut
195
196\newdimension\d_math_fraction_margin
197
198\def\math_fraction_margin_kern_indeed{\kern\d_math_fraction_margin}
199
200\let\math_fraction_margin_kern\relax
201
202%D \starttyping
203%D $\Uover{2}{3} ^4$ % double script error but no longer because we use the prescripts
204%D ${\over{2}{3}}^4$ % no issue anyway because we wrap in {} (goes unnoticed)
205%D $\frac {2}{3} ^4$ % we wrap in \mathfrac so we can also use prescripts
206%D \stoptyping
207
208\installlocalcurrenthandler\??mathfractions {mathfraction}
209
210\tolerant\protected\def\math_frac#1#*[#S#2]%%
211  {\beginmathgroup
212   %
213   \setlocalmathfractioncurrent{#1}% \edef\currentmathfraction{#1}%
214   %
215   \ifparameter#2\or\setupcurrentmathfraction[#2]\fi
216   %
217   \edef\p_math_fraction_fences{\mathfractionparameter\c!fences}%
218   %
219   \math_atom_by_parameter\mathfractionparameter\bgroup
220   \ifempty\p_math_fraction_fences \else
221     \math_fenced_fenced_start\p_math_fraction_fences
222   \fi
223   %
224   \d_math_fraction_margin\mathfractionparameter\c!margin\relax
225   \ifcase\d_math_fraction_margin
226      \let\math_fraction_margin_kern\relax
227   \else
228      \let\math_fraction_margin_kern\math_fraction_margin_kern_indeed
229   \fi
230   %
231   \edef\p_math_fractions_strut{\mathfractionparameter\c!strut}%
232   \ifcsname\??mathfractionstrut\p_math_fractions_strut\endcsname
233     \lastnamedcs
234   \else
235     \math_frac_no_strut
236   \fi
237   %
238   \expandnamespaceparameter\??mathfractionalternative\mathfractionparameter\c!alternative\v!inner}
239
240\protected\def\math_frac_wrapup
241  {\ifempty\p_math_fraction_fences \else
242     \math_fenced_fenced_stop\p_math_fraction_fences
243   \fi
244   \egroup
245   \endmathgroup}
246
247\integerdef\math_frac_no_delim "2E % or just \zerocount
248
249% Having a \withmarginornot{#1}{#2} makes not much sense nor do 4 tests or 4 redundant
250% kerns (longer node lists plus possible interference). A split in normal and margin
251% also makes testing easier. When left and right margins are needed we might merge the
252% variants again. After all, these are not real installers.
253%
254% The denominator is normally in cramped mode!
255%
256% See \MKIV\ for the old (more messy) implementation where we have to fight the over
257% related syntax (we now use a lmtx primitive).
258
259% timestamp: 01/2022 : (re)watching eNerdā“ - Time Capsule 2020 a.k.a. Farewell Party
260% for Normalcy (UHD) with the soekris/neumanns combination
261
262\installcorenamespace{fractionmethods}
263
264\defcsname\??fractionmethods\v!vertical  \v!no     \v!none\endcsname{\Uatop}
265\defcsname\??fractionmethods\v!vertical  \v!no            \endcsname{\Uatopwithdelims
266                                                                     \Udelimiter\zerocount\fam\scratchcounterone
267                                                                     \Udelimiter\zerocount\fam\scratchcountertwo}
268\defcsname\??fractionmethods\v!vertical  \v!yes    \v!none\endcsname{\Uabove}
269\defcsname\??fractionmethods\v!vertical  \v!yes           \endcsname{\Uabovewithdelims
270                                                                     \Udelimiter\zerocount\fam\scratchcounterone
271                                                                     \Udelimiter\zerocount\fam\scratchcountertwo}
272\defcsname\??fractionmethods\v!vertical  \v!hidden \v!none\endcsname{\Uabove}
273\defcsname\??fractionmethods\v!vertical  \v!hidden        \endcsname{\Uabovewithdelims
274                                                                     \Udelimiter\zerocount\fam\scratchcounterone
275                                                                     \Udelimiter\zerocount\fam\scratchcountertwo}
276\defcsname\??fractionmethods\v!vertical  \v!auto   \v!none\endcsname{\Uover}
277\defcsname\??fractionmethods\v!vertical  \v!auto          \endcsname{\Uoverwithdelims
278                                                                     \Udelimiter\zerocount\fam\scratchcounterone
279                                                                     \Udelimiter\zerocount\fam\scratchcountertwo}
280\defcsname\??fractionmethods\v!vertical  \v!symbol \v!none\endcsname{\Ustretched
281                                                                     \Udelimiter\zerocount\fam\scratchcounterthree}
282\defcsname\??fractionmethods\v!vertical  \v!symbol        \endcsname{\Ustretchedwithdelims
283                                                                     \Udelimiter\zerocount\fam\scratchcounterone
284                                                                     \Udelimiter\zerocount\fam\scratchcountertwo
285                                                                     \Udelimiter\zerocount\fam\scratchcounterthree}
286\defcsname\??fractionmethods\v!horizontal          \v!none\endcsname{\Uskewed
287                                                                     \Udelimiter\zerocount\fam\scratchcounterthree
288                                                                     \s!hfactor \numexpr\mathfractionparameter\c!hfactor\relax}
289\defcsname\??fractionmethods\v!horizontal                 \endcsname{\Uskewedwithdelims
290                                                                     \Udelimiter\zerocount\fam\scratchcounterone
291                                                                     \Udelimiter\zerocount\fam\scratchcountertwo
292                                                                     \Udelimiter\zerocount\fam\scratchcounterthree
293                                                                     \s!hfactor \numexpr\mathfractionparameter\c!hfactor\relax}
294\defcsname\??fractionmethods\v!line                \v!none\endcsname{\Uskewed
295                                                                     \Udelimiter\zerocount\fam\scratchcounterthree
296                                                                     \s!noaxis \space
297                                                                     \s!hfactor \numexpr\mathfractionparameter\c!hfactor\relax}
298\defcsname\??fractionmethods\v!line                       \endcsname{\Uskewedwithdelims
299                                                                     \Udelimiter\zerocount\fam\scratchcounterone
300                                                                     \Udelimiter\zerocount\fam\scratchcountertwo
301                                                                     \Udelimiter\zerocount\fam\scratchcounterthree
302                                                                     \s!noaxis \space
303                                                                     \s!hfactor \numexpr\mathfractionparameter\c!hfactor\relax}
304\defcsname\??fractionmethods\v!center              \v!none\endcsname{\Uskewed
305                                                                     \Udelimiter\zerocount\fam\scratchcounterthree
306                                                                     \s!noaxis \space \s!center
307                                                                     \s!hfactor \numexpr\mathfractionparameter\c!hfactor\relax}
308\defcsname\??fractionmethods\v!center                     \endcsname{\Uskewedwithdelims
309                                                                     \Udelimiter\zerocount\fam\scratchcounterone
310                                                                     \Udelimiter\zerocount\fam\scratchcountertwo
311                                                                     \Udelimiter\zerocount\fam\scratchcounterthree
312                                                                     \s!noaxis \space \s!center
313                                                                     \s!hfactor \numexpr\mathfractionparameter\c!hfactor\relax}
314
315\setupmathfractions % we now inherit !
316  [\c!topcolor=\mathfractionparameter\c!color,
317   \c!bottomcolor=\mathfractionparameter\c!color,
318   \c!rulecolor=\mathfractionparameter\c!color]
319
320\def\math_frac_command_u
321  {\edef\p_method   {\mathfractionparameter\c!method}%
322   \edef\p_rule     {\mathfractionparameter\c!rule}%
323   \edef\p_thickness{\mathfractionparameter\c!rulethickness}%
324   \scratchcounterone  \mathfractionparameter\c!left  \relax
325   \scratchcountertwo  \mathfractionparameter\c!right \relax
326   \scratchcounterthree
327      \ifx\p_method\v!horizontal
328        \mathfractionparameter\c!middle
329      \orelse\ifx\p_method\v!line
330        \mathfractionparameter\c!middle
331      \orelse\ifx\p_method\v!center
332        \mathfractionparameter\c!middle
333      \else
334        \mathfractionparameter\c!symbol
335      \fi
336   \relax
337   \ifcsname
338      \??fractionmethods
339      \p_method
340      \ifx\p_method\v!horizontal
341      \orelse\ifx\p_method\v!line
342      \orelse\ifx\p_method\v!center
343      \orelse\ifcsname\??fractionmethods\p_method\p_rule\endcsname % \v!vertical
344        \p_rule
345      \else
346        \v!auto
347      \fi
348      \ifnum\scratchcounterone=\math_frac_no_delim
349        \ifnum\scratchcountertwo=\math_frac_no_delim
350          \v!none
351        \fi
352      \fi
353   \endcsname \lastnamedcs \else \csname\??fractionmethods\v!vertical\v!auto\endcsname \fi
354   \usedcolorparameterattributes{\mathfractionparameter\c!rulecolor}% was \c!color so we break compatibility !
355   thickness \ifx\p_thickness\v!font
356     \zeropoint
357     \s!font \space
358   \else
359     \dimexpr\p_thickness\relax
360   \fi
361   \s!class   \math_class_by_parameter\mathfractionparameter\c!mathclass
362   \s!vfactor \numexpr\mathfractionparameter\c!vfactor\relax
363   \s!source  \number\namedboxanchor{\mathfractionparameter\c!source}}
364
365\installcorenamespace{mathalignsimple} % this should move to math-ali
366
367\defcsname\??mathalignsimple\v!right     \endcsname#1{#1\hfill}
368\defcsname\??mathalignsimple\v!flushleft \endcsname#1{#1\hfill}
369\defcsname\??mathalignsimple\v!left      \endcsname#1{\hfill#1}
370\defcsname\??mathalignsimple\v!flushright\endcsname#1{\hfill#1}
371\defcsname\??mathalignsimple\v!middle    \endcsname#1{\hfill#1\hfill}
372
373\protected\def\math_frac_aligned_top{\begincsname\??mathalignsimple\mathfractionparameter\c!topalign   \endcsname}
374\protected\def\math_frac_aligned_bot{\begincsname\??mathalignsimple\mathfractionparameter\c!bottomalign\endcsname}
375
376% \protected\def\math_frac_aligned_top{\begincsname\??mathalignsimple\mathfractionparameter\c!align\endcsname}
377% \protected\def\math_frac_aligned_bot{\begincsname\??mathalignsimple\mathfractionparameter\c!align\endcsname}
378
379\protected\defcsname\??mathfractionalternative\v!inner\endcsname#1#2%
380  {%\beginmathgroup
381   \math_frac_command_u
382     {\dousecolorparameter{\mathfractionparameter\c!topcolor}%
383      \math_fraction_margin_kern
384      \usemathstyleparameter\mathfractionparameter\c!mathnumeratorstyle
385      \m_fractions_strut_top\math_frac_aligned_top{#1}%
386      \math_fraction_margin_kern}%
387     {\dousecolorparameter{\mathfractionparameter\c!bottomcolor}%
388      \math_fraction_margin_kern
389      \usemathstyleparameter\mathfractionparameter\c!mathdenominatorstyle
390      \m_fractions_strut_bot\math_frac_aligned_bot{#2}%
391      \math_fraction_margin_kern}%
392   %\endmathgroup
393   \math_frac_wrapup}
394
395\protected\defcsname\??mathfractionalternative\v!outer\endcsname#1#2%
396  {\beginmathgroup
397   \usemathstyleparameter\mathfractionparameter\c!mathstyle
398   \math_frac_command_u
399     {\dousecolorparameter{\mathfractionparameter\c!topcolor}%
400      \math_fraction_margin_kern
401      \m_fractions_strut_top\math_frac_aligned_top{#1}%
402      \math_fraction_margin_kern}%
403     {\dousecolorparameter{\mathfractionparameter\c!bottomcolor}%
404      \math_fraction_margin_kern
405      \m_fractions_strut_bot\math_frac_aligned_bot{#2}%
406      \math_fraction_margin_kern}%
407   \endmathgroup
408   \math_frac_wrapup}
409
410\protected\defcsname\??mathfractionalternative\v!both\endcsname#1#2%
411  {\beginmathgroup
412   \usemathstyleparameter\mathfractionparameter\c!mathstyle
413   \math_frac_command_u
414     {\dousecolorparameter{\mathfractionparameter\c!topcolor}%
415      \math_fraction_margin_kern
416      \usemathstyleparameter\mathfractionparameter\c!mathnumeratorstyle
417      \m_fractions_strut_top\math_frac_aligned_top{#1}%
418      \math_fraction_margin_kern}%
419     {\dousecolorparameter{\mathfractionparameter\c!bottomcolor}%
420      \math_fraction_margin_kern
421      \usemathstyleparameter\mathfractionparameter\c!mathdenominatorstyle
422      \m_fractions_strut_bot\math_frac_aligned_bot{#2}%
423      \math_fraction_margin_kern}%
424   \endmathgroup
425   \math_frac_wrapup}
426
427\nulldelimiterspace\zeropoint % was 1.2pt
428
429\definemathfraction[xfrac] [\c!alternative=\v!inner,\c!mathstyle=\s!script]
430\definemathfraction[xxfrac][\c!alternative=\v!inner,\c!mathstyle=\s!scriptscript]
431
432\aliased\let\normalxfrac\xfrac
433
434\permanent\protected\def\xfrac#1#2{\normalxfrac{\enforced\let\xfrac\xxfrac#1}{\enforced\let\xfrac\xxfrac#2}}
435
436%D Spacing (sensitive for definition of \triggermathstyle:
437
438\permanent\protected\def\nomathfractiongaps     {\normalexpanded{\math_no_fraction_gaps     \allmathstyles}}
439\permanent\protected\def\overlaymathfractiongaps{\normalexpanded{\math_overlay_fraction_gaps\allmathstyles}}
440
441\protected\def\math_no_fraction_gaps#1%
442  {\Umathfractionnumup    #1\zeropoint
443   \Umathfractiondenomdown#1\zeropoint}
444
445\protected\def\math_overlay_fraction_gaps#1%
446  {\Umathfractionnumup    #1\zeropoint
447   \Umathfractionnumvgap  #1\zeropoint
448   \Umathfractiondenomvgap#1\zeropoint
449   \Umathfractiondenomdown#1\zeropoint}
450
451\installcorenamespace{mathfractiondistance}
452
453\letcsname\??mathfractiondistance\v!none   \endcsname\nomathfractiongaps
454\letcsname\??mathfractiondistance\v!no     \endcsname\nomathfractiongaps
455\letcsname\??mathfractiondistance\v!overlay\endcsname\overlaymathfractiongaps
456
457\setupmathfractions
458  [\c!distance=\v!none]
459
460% we need to keep it local, so not everybodyfont
461
462\appendtoks
463    \edef\p_distance{\rootmathfractionparameter\c!distance}%
464    \ifempty\p_distance\else
465        \begincsname\??mathfractiondistance\p_distance\endcsname
466    \fi
467\to \everymathematics
468
469% theshold is new!
470
471\let\math_fraction_set_threshold_inline \relax
472\let\math_fraction_set_threshold_display\relax
473
474\appendtoks
475    \math_fraction_set_threshold_inline
476    \math_fraction_set_threshold_display
477\to \everymathematics
478
479\appendtoks
480    \ifempty\currentmathfraction
481      \edef\p_threshold{\mathfractionparameter\c!inlinethreshold}%
482      \ifempty\p_threshold
483        \let\math_fraction_set_threshold_inline\math_fraction_set_threshold_inline_auto
484      \orelse\ifx\p_threshold\v!auto
485        \let\math_fraction_set_threshold_inline\math_fraction_set_threshold_inline_auto
486      \else
487        \let\math_fraction_set_threshold_inline\math_fraction_set_threshold_inline_ratio
488      \fi
489      \edef\p_threshold{\mathfractionparameter\c!displaythreshold}%
490      \ifempty\p_threshold
491        \let\math_fraction_set_threshold_display\math_fraction_set_threshold_display_auto
492      \orelse\ifx\p_threshold\v!auto
493        \let\math_fraction_set_threshold_display\math_fraction_set_threshold_display_auto
494      \else
495        \let\math_fraction_set_threshold_display\math_fraction_set_threshold_display_ratio
496      \fi
497    \fi
498\to \everysetupmathfraction
499
500\def\math_fraction_set_threshold_inline_auto
501   {\Umathfractiondelsize\allmathstyles\maxdimen}
502
503\def\math_fraction_set_threshold_display_auto
504   {\Umathfractiondelsize\alldisplaystyles\maxdimen}
505
506\def\math_fraction_set_threshold_inline_ratio % watch the reverse compensation
507  {\edef\p_threshold{\mathfractionparameter\c!inlinethreshold}%
508   \Umathfractiondelsize\alltextstyles        \p_threshold\glyphscaled\textface
509   \Umathfractiondelsize\allscriptstyles      \p_threshold\glyphscaled\scriptface
510   \Umathfractiondelsize\allscriptscriptstyles\p_threshold\glyphscaled\scriptscriptface}
511
512\def\math_fraction_set_threshold_display_ratio % watch the reverse compensation
513  {\edef\p_threshold{\mathfractionparameter\c!displaythreshold}%
514   \Umathfractiondelsize\alldisplaystyles\p_threshold\glyphscaled\textface}
515
516\setupmathfractions
517  [\c!inlinethreshold=1.2,      % no unit but fraction
518   \c!displaythreshold=\v!auto, % no unit but fraction
519   \c!threshold=.25\exheight]
520
521\appendtoks
522    \Umathskeweddelimitertolerance\allmathstyles\mathfractionparameter\c!threshold
523\to \everymathematics % else original lost: \to \everybodyfont
524
525%D \macros
526%D   {dfrac, tfrac, frac, dbinom, tbinom, binom}
527%D
528%D \startbuffer
529%D $\dfrac {1}{2} \tfrac {1}{2} \frac {1}{2}$
530%D $\dbinom{1}{2} \tbinom{1}{2} \binom{1}{2}$
531%D \stopbuffer
532%D
533%D \typebuffer
534%D \getbuffer
535
536% $$
537%     {{a}\over{b}} +
538%     {{a}\overwithdelims(){b}} +
539%     {{a}\atopwithdelims(){b}} +
540%     {{a}\abovewithdelims()\zeropoint{b}} +
541%     \left({{a}\over{b}}\right)
542% $$
543
544% \dorecurse {10} { % weird
545%     $$ {{a}\abovewithdelims()#1pt{b}} $$
546% }
547
548\definemathfraction[i:frac] [\c!alternative=\v!inner,\c!mathstyle=\v!small]
549\definemathfraction[i:tfrac][\c!alternative=\v!inner,\c!mathstyle=\s!script]
550\definemathfraction[i:sfrac][\c!alternative=\v!inner,\c!mathstyle=\s!scriptscript]
551\definemathfraction[i:dfrac][\c!alternative=\v!inner,\c!mathstyle=\s!display]
552
553\definemathfraction[d:frac] [\c!alternative=\v!inner,\c!mathstyle=\s!cramped]
554\definemathfraction[d:tfrac][\c!alternative=\v!inner,\c!mathstyle={\s!cramped,\s!script}]
555\definemathfraction[d:sfrac][\c!alternative=\v!inner,\c!mathstyle={\s!cramped,\s!scriptscript}]
556\definemathfraction[d:dfrac][\c!alternative=\v!both,\c!mathstyle=\s!display]
557
558%D \protected\def\ShowMathFractions#1#2%
559%D   {\dontleavehmode
560%D    \begingroup
561%D    \showmathstruts
562%D    \mathematics{x+\tfrac{#1}{#2}+1+\frac{#1}{#2}+2+\sfrac{#1}{#2}+g}%
563%D    \endgroup}
564%D
565%D The default \type {tfrac}, \type {frac} and \type \sfrac} look like this:
566%D
567%D \blank
568%D \ShowMathFractions{a}{a}\par
569%D \ShowMathFractions{1}{x}\par
570%D \ShowMathFractions{a}{b}\par
571%D \ShowMathFractions{1}{b}\par
572%D \blank
573
574%D There is no real need to support ams styling any longer so we removed the
575%D related code.
576
577\permanent\protected\def\frac {\csname\inlineordisplaymath id:frac\endcsname}
578\permanent\protected\def\tfrac{\csname\inlineordisplaymath id:tfrac\endcsname}
579\permanent\protected\def\sfrac{\csname\inlineordisplaymath id:sfrac\endcsname}
580\permanent\protected\def\dfrac{\csname\inlineordisplaymath id:dfrac\endcsname}
581
582\definemathfraction
583  [d:binom]
584  [d:frac]
585  [\c!rule=\v!no,
586 % \c!mathstyle=\s!display,
587   \c!left="28,
588   \c!right="29]
589
590\definemathfraction
591  [i:binom]
592  [i:frac]
593  [\c!rule=\v!no,
594  %\c!mathstyle=\s!text,
595   \c!left="28,
596   \c!right="29]
597
598\permanent\protected\def\binom{\csname\inlineordisplaymath id:binom\endcsname}
599
600\definemathfraction
601  [dbinom]
602  [d:binom]
603  [\c!mathstyle=\s!display]
604
605\definemathfraction
606  [tbinom]
607  [i:binom]
608  [\c!mathstyle=\s!text]
609
610%D \macros
611%D   {cfrac}
612%D
613%D \startbuffer
614%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
615%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
616%D \stopbuffer
617%D
618%D \typebuffer
619%D
620%D \getbuffer
621%D
622%D Now we can align every combination we want:
623%D
624%D \startbuffer
625%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
626%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
627%D $\cfrac[cl]{12}{3} \cfrac[cc]{12}{3} \cfrac[cr]{12}{3}$
628%D $\cfrac[lc]{1}{23} \cfrac[cc]{1}{23} \cfrac[rc]{1}{23}$
629%D \stopbuffer
630%D
631%D \typebuffer
632%D
633%D \getbuffer
634
635% % we forget about this ams one ... no one uses it anyway
636%
637% \permanent\protected\def\cfrac
638%   {\doifelsenextoptionalcs\math_cfrac_yes\math_cfrac_nop}
639%
640% \def\math_cfrac_nop    {\math_cfrac_indeed[cc]}
641% \def\math_cfrac_yes[#1]{\math_cfrac_indeed[#1cc]}
642%
643% \def\math_cfrac_indeed[#1#2#3]#4#5%
644%   {{\displaystyle
645%     \frac
646%       {\strut
647%        \ifx r#1\hfill\fi#4\ifx l#1\hfill\fi}%
648%       {\ifx r#2\hfill\fi#5\ifx l#2\hfill\fi}%
649%     \ifzeropt\nulldelimiterspace\else
650%       \kern-\nulldelimiterspace
651%     \fi}}
652%
653% instead we predefine one:
654
655\definemathfraction
656  [cfrac]
657  [\c!mathstyle=\v!text]
658
659% so users can do this:
660%
661% \setupmathfraction
662%   [cfrac]
663%   [topalign=flushright]
664%
665% or define an child instance that presets it.
666
667%D \macros
668%D   {splitfrac, splitdfrac}
669%D
670%D Occasionally one needs to typeset multi||line fractions. These commands use \type
671%D {\genfrac} to create such fractions.
672%D
673%D \startbuffer
674%D \startformula
675%D      a=\frac{
676%D          \splitfrac{xy + xy + xy + xy + xy}
677%D                    {+ xy + xy + xy + xy}
678%D        }
679%D        {z}
680%D      =\frac{
681%D          \splitdfrac{xy + xy + xy + xy + xy}
682%D                    {+ xy + xy + xy + xy}
683%D        }
684%D        {z}
685%D \stopformula
686%D \stopbuffer
687%D
688%D \typebuffer \getbuffer
689
690\defcsname\??mathalignsimple\v!split:\v!flushleft\endcsname#1%
691  {#1%
692   \ifchkdimension\mathfractionparameter\c!distance\or\hkern\lastchkdimension\fi
693   \hfill}
694
695\defcsname\??mathalignsimple\v!split:\v!flushright\endcsname#1%
696  {\hfill
697   \ifchkdimension\mathfractionparameter\c!distance\or\hkern\lastchkdimension\fi
698   \mathatom \s!class \mathordcode \s!unroll {}% prevents +/- seen as sign, unroll prevents empty box
699   #1}
700
701\definemathfraction
702  [splitfrac]
703  [\c!mathnumeratorstyle=\v!text,
704   \c!mathdenominatorstyle=\v!text,
705   \c!topalign=\v!split:\v!flushleft,
706   \c!bottomalign=\v!split:\v!flushright,
707   \c!distance=\emwidth,
708   \c!rule=\v!no]
709
710\definemathfraction
711  [splitdfrac]
712  [splitfrac]
713  [\c!mathnumeratorstyle=\v!display,
714   \c!mathdenominatorstyle=\v!display]
715
716%D More fracking (for Alan):
717
718% \appendtoks
719%     \Umathskewedfractionhgap\allmathstyles\namedmathfractionparameter\s!vfrac\c!hfactor\fontemwidth \mathstylefont\textstyle
720%     \Umathskewedfractionvgap\allmathstyles\namedmathfractionparameter\s!vfrac\c!vfactor\fontexheight\mathstylefont\textstyle
721% \to \everymathematics
722
723\definemathfraction
724  [\v!horizontal]
725  [\c!method=\v!horizontal]
726
727\definemathfraction
728  [\s!vfrac]
729  [\v!horizontal]
730  [\c!hfactor=20, % quite small, as we have 1000 as default
731   \c!vfactor=10] % quite small, as we have 1000 as default
732
733\definemathfraction
734  [hfrac]
735  [\c!method=\v!center,
736   \c!hfactor=\zerocount,
737   \c!vfactor=\zerocount]
738
739%D And a cheat (for Mikael):
740
741% \definemathfraction
742%   [afrac]
743%   [\c!mathclass=\mathrelationcode,
744%    \c!rule=\v!symbol,
745%    \c!middle="2192]
746
747%D New stretched fraction with middle symnbol:
748
749% \enableexperiments[math.arrows] % enforce construction of these in math-act
750%
751% {
752%     tweak = "addarrows",
753%     list  = { [0x3D] = { squeeze = .85, yoffset = .0975 } } -- bonum
754% },
755
756\definemathfraction
757  [relfraction]
758  [\c!mathclass=\mathrelationcode,
759   \c!margin=.25\emwidth,
760   \c!rule=\v!symbol]
761
762\definemathfraction [bartext]                [relfraction] [\c!middle="203E]
763\definemathfraction [leftarrowtext]          [relfraction] [\c!middle="2190]
764\definemathfraction [rightarrowtext]         [relfraction] [\c!middle="2192]
765\definemathfraction [twoheadleftarrowtext]   [relfraction] [\c!middle="219E]
766\definemathfraction [twoheadrightarrowtext]  [relfraction] [\c!middle="21A0]
767\definemathfraction [reltext]                [relfraction] [\c!middle="002D]
768\definemathfraction [equaltext]              [relfraction] [\c!middle="003D]
769\definemathfraction [mapstotext]             [relfraction] [\c!middle="21A6]
770\definemathfraction [hookleftarrowtext]      [relfraction] [\c!middle="21A9]
771\definemathfraction [hookrightarrowtext]     [relfraction] [\c!middle="21AA]
772\definemathfraction [leftharpoondowntext]    [relfraction] [\c!middle="21BD]
773\definemathfraction [leftharpoonuptext]      [relfraction] [\c!middle="21BC]
774\definemathfraction [rightharpoondowntext]   [relfraction] [\c!middle="21C1]
775\definemathfraction [rightharpoonuptext]     [relfraction] [\c!middle="21C0]
776\definemathfraction [rightoverleftarrowtext] [relfraction] [\c!middle="21C4]
777\definemathfraction [leftrightharpoonstext]  [relfraction] [\c!middle="21CB]
778\definemathfraction [rightleftharpoonstext]  [relfraction] [\c!middle="21CC]
779\definemathfraction [triplereltext]          [relfraction] [\c!middle="2261]
780\definemathfraction [leftrightarrowtext]     [relfraction] [\c!middle="27F7]
781\definemathfraction [rightleftarrowtext]     [relfraction] [\c!middle="27F7]
782\definemathfraction [Leftarrowtext]          [relfraction] [\c!middle="27F8]
783\definemathfraction [Rightarrowtext]         [relfraction] [\c!middle="27F9]
784\definemathfraction [Leftrightarrowtext]     [relfraction] [\c!middle="27FA]
785\definemathfraction [Rightleftarrowtext]     [relfraction] [\c!middle="27FA]
786
787\protect \endinput
788