summaryrefslogtreecommitdiff
path: root/tex/context/base/math-ini.mkiv
blob: 74f40543de707594b415c747c3b9885cee69fe6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
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
662
663
664
665
666
667
668
669
670
671
672
673
%D \module
%D   [       file=math-ini,
%D        version=2008.01.02,
%D          title=\CONTEXT\ Math Macros,
%D       subtitle=Initializations,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Math Macros / Initializations}

%D This module provides namespaces for math fonts, thereby
%D permitting mixed usage of math fonts. Although not strictly
%D needed, we also provide a family name mapping mechanism as
%D used in the (original) AMS math definition files, but here
%D these names can recursively be remapped and if needed,
%D dynamically be changed. We've tried to minimize the number
%D of definition commands and use plain \TEX\ definitions as
%D fallback. We've tried to follow a couple of conventions
%D from plain and AMS math in order to achieve backward
%D compatinility. We also kept an eye on future usage of these
%D modules in the perspective of MathML and unicode fonts.

\unprotect

\ifx\v!compact\undefined \def\v!compact{compact} \fi

%D New:

\let\startimath\Ustartmath         \let\stopimath \Ustopmath
\let\startdmath\Ustartdisplaymath  \let\stopdmath \Ustopmath

%D We move these definitions into the format:

% test [[\char948 \ctxlua{tex.sprint(utf.char(948))}]]
% test $[[\char948 \ctxlua{tex.sprint(utf.char(948))}]]$

\registerctxluafile{math-ini}{1.001}
\registerctxluafile{math-dim}{1.001}
\registerctxluafile{math-ent}{1.001}
\registerctxluafile{math-ext}{1.001}
\registerctxluafile{math-vfu}{1.001}
\registerctxluafile{math-map}{1.001}
\registerctxluafile{math-noa}{1.001}
\registerctxluafile{math-tag}{1.001}

\definesystemattribute[mathalphabet]        \chardef\mathalphabetattribute    \dogetattributeid{mathalphabet}
\definesystemattribute[mathsize]            \chardef\mathsizeattribute        \dogetattributeid{mathsize}
\definesystemattribute[mathpunctuation]     \chardef\mathpunctuationattribute \dogetattributeid{mathpunctuation}
\definesystemattribute[mathgreek]           \chardef\mathgreekattribute       \dogetattributeid{mathgreek}
\definesystemattribute[mathalternate]       \chardef\mathalternateattribute   \dogetattributeid{mathalternate}

% experiment

% Normally this is applied to only one character.
%
% $ABC$ $\cal ABC$ $\mathaltcal ABC$

\def\mathalternate#1{\ctxlua{mathematics.setalternate(0,"#1")}} % fam 0

\def\mathaltcal{\mathalternate{cal}\cal} % ss01 in xits

% todo: only in mmode

\unexpanded\def\mathgreekupright{\attribute\mathgreekattribute22 }
\unexpanded\def\mathgreekitalic {\attribute\mathgreekattribute33 }
\unexpanded\def\mathgreekdefault{\attribute\mathgreekattribute\attributeunsetvalue}

\let\mathgreeknormal\mathgreekupright
\let\mathgreeknone  \mathgreekdefault

\def\setmathattribute#1#2{\ifmmode\ctxlua{mathematics.syncboth ("#1","#2")}\fi}
\def\setmathalphabet   #1{\ifmmode\ctxlua{mathematics.syncname ("#1")}\fi}
\def\setmathstyle      #1{\ifmmode\ctxlua{mathematics.syncstyle("#1")}\fi}

\unexpanded\def\mr            {\setmathattribute{regular}{tf}}

\unexpanded\def\mathdefault   {\setmathattribute{regular}{it}}
\unexpanded\def\mathscript    {\setmathalphabet{script}}
\unexpanded\def\mathfraktur   {\setmathalphabet{fraktur}}
\unexpanded\def\mathblackboard{\setmathalphabet{blackboard}}

\unexpanded\def\mathrm{\setmathattribute{rm}{tf}}
\unexpanded\def\mathss{\setmathattribute{ss}{tf}}
\unexpanded\def\mathtt{\setmathattribute{tt}{tf}}

\unexpanded\def\mathtf{\setmathstyle{tf}}
\unexpanded\def\mathbf{\setmathstyle{bf}}
\unexpanded\def\mathsl{\setmathstyle{sl}}
\unexpanded\def\mathit{\setmathstyle{it}}
\unexpanded\def\mathbs{\setmathstyle{bs}}
\unexpanded\def\mathbi{\setmathstyle{bi}}

\let\tfmath\mathtf % maybe a grouped command
\let\bfmath\mathbf
\let\slmath\mathsl
\let\itmath\mathit
\let\bsmath\mathbs
\let\bimath\mathbi

\let\Bbb\mathblackboard

\unexpanded\def\frak      {\ifmmode\expandafter\mathfraktur   \fi}
\unexpanded\def\cal       {\ifmmode\expandafter\mathscript    \fi}
\unexpanded\def\bbd       {\ifmmode\expandafter\mathblackboard\fi}
\unexpanded\def\blackboard{\ifmmode\expandafter\mathblackboard\fi}
\unexpanded\def\fraktur   {\ifmmode\expandafter\mathfraktur   \fi}
\unexpanded\def\gothic    {\ifmmode\expandafter\mathfraktur   \fi}

\unexpanded\def\mathcal #1{{\mathscript    #1}}  % for AMS compatibility
\unexpanded\def\mathfrak#1{{\mathfraktur   #1}}  % for AMS compatibility
\unexpanded\def\mathbb  #1{{\mathblackboard#1}}  % for AMS compatibility

\let\normalmr\mr

% \prependtoks
%     \let\mr\normalmr
%     \let\rm\mathrm \let\ss\mathss \let\tt\mathtt
%     \let\tf\mathtf \let\bf\mathbf \let\it\mathit \let\sl\mathsl \let\bi\mathbi \let\bs\mathbs
%     \let\frak\mathfraktur \let\cal\mathscript \let\bbd\mathblackboard
%     \mathdefault
% \to \everymathematics

\let\normaltf\tf \unexpanded\def\tf{\ifmmode\mathtf\else\normaltf\fi}
\let\normalbf\bf \unexpanded\def\bf{\ifmmode\mathbf\else\normalbf\fi}
\let\normalit\it \unexpanded\def\it{\ifmmode\mathit\else\normalit\fi}
\let\normalsl\sl \unexpanded\def\sl{\ifmmode\mathsl\else\normalsl\fi}
\let\normalbi\bi \unexpanded\def\bi{\ifmmode\mathbi\else\normalbi\fi}
\let\normalbs\bs \unexpanded\def\bs{\ifmmode\mathbs\else\normalbs\fi}

\let\normalrm\rm \unexpanded\def\rm{\ifmmode\mathrm\else\normalrm\fi}
\let\normalss\ss \unexpanded\def\ss{\ifmmode\mathss\else\normalss\fi}
\let\normaltt\tt \unexpanded\def\tt{\ifmmode\mathtt\else\normaltt\fi}
                 \unexpanded\def\mr{\ifmmode            \normalmr\fi}

\prependtoks
%     \let\mr\normalmr
%     \let\rm\mathrm \let\ss\mathss \let\tt\mathtt
%     \let\tf\mathtf \let\bf\mathbf \let\it\mathit \let\sl\mathsl \let\bi\mathbi \let\bs\mathbs
%     \let\frak\mathfraktur \let\cal\mathscript \let\bbd\mathblackboard
    \mathdefault
\to \everymathematics

%D \macros
%D   {boldsymbol}
%D
%D To be done.

\let\mathboldsymbol\relax % yet unsupported, will be

\def\boldsymbol
  {\mathortext\mathboldsymbol\bold}

%D Helpers:

\def\utfmathclass  #1{\ctxlua{tex.sprint(mathematics.utfmathclass  ("#1"))}}
\def\utfmathstretch#1{\ctxlua{tex.sprint(mathematics.utfmathstretch("#1"))}}
\def\utfmathcommand#1{\ctxlua{tex.sprint(mathematics.utfmathcommand("#1"))}}
\def\utfmathfiller #1{\ctxlua{tex.sprint(mathematics.utfmathfiller ("#1"))}}

% \def\utfmathclassdefault  #1#2{\ctxlua{
%     tex.sprint(mathematics.utfmathclass("#1","#2"))
% }}
%
% \def\utfmathcommanddefault#1#2#3{\ctxlua{
%     local cmd = mathematics.utfmathcommand("#1","") or ""
%     if cmd == "" then
%         commands.cs("#2","#3")
%     else
%         commands.cs(cmd)
%     end}}

% % %

\def\@@mathlimopcomm #1{\mathop{#1}} %no \limits
\def\@@mathnolopcomm #1{\mathop{#1}\nolimits}
\def\@@mathboxcomm   #1{\dontleavehmode\hbox\Ustartmath\mathsurround\zeropoint#1\Ustopmath}
\def\@@mathchoicecomm#1{[todo #1]}

\chardef\mathordcode    = 0  \let\mathordcomm     \mathord
\chardef\mathopcode     = 1  \let\mathopcomm      \mathop
\chardef\mathbincode    = 2  \let\mathbincomm     \mathbin
\chardef\mathrelcode    = 3  \let\mathrelcomm     \mathrel
\chardef\mathopencode   = 4  \let\mathopencomm    \mathopen
\chardef\mathclosecode  = 5  \let\mathclosecomm   \mathclose
\chardef\mathpunctcode  = 6  \let\mathpunctcomm   \mathpunct
\chardef\mathalphacode  = 7  \let\mathalphacomm   \firstofoneargument
\chardef\mathinnercode  = 0  \let\mathinnercomm   \mathinner
\chardef\mathnothingcode= 0  \let\mathnothingcomm \firstofoneargument
\chardef\mathlimopcode  = 1  \let\mathlimopcomm   \@@mathlimopcomm
\chardef\mathnolopcode  = 1  \let\mathnolopcomm   \@@mathnolopcomm
\chardef\mathchoicecode = 0  \let\mathchoicecomm  \@@mathchoicecomm
\chardef\mathboxcode    = 0  \let\mathboxcomm     \@@mathboxcomm

\chardef\mathaccentcode = 8
\chardef\mathradicalcode= 9

\def\puremathcode#1{\the\csname math#1code\endcsname}
\def\puremathcomm#1{\csname     math#1comm\endcsname}

% \startlines
% $\mathopnolimits{\rm d}x$
% $\mathopnolimits{\kern\zeropoint \rm d}x$
% $\puremathcomm{nolop}{\rm d}x$
% $\puremathcomm{nolop}{\kern\zeropoint\rm d}x$
% \blank
% $\puremathcomm{nolop}{\mr d}x$
% $\puremathcomm{nolop}{\kern\zeropoint\mr d}x$
% $\mathop{\kern\zeropoint\mr d}x$
% $\mathopnolimits{\kern\zeropoint d}x$
% \stoplines

% this will be sorted out:

\let\mathcharacter           \getvalue
\let\textcharacter           \getvalue
\unexpanded\def\definefamilysynonym     {\dotripleempty\dodefinefamilysynonym}
\def\dodefinefamilysynonym   [#1][#2][#3]{}
\unexpanded\def\definemathsymbol        {\dosixtupleempty\dodefinemathsymbol}
\def\dodefinemathsymbol      [#1][#2][#3][#4][#5][#6]{}
\unexpanded\def\definemathcharacter     {\dosixtupleempty\dodefinemathcharacter}
\def\dodefinemathcharacter   [#1][#2][#3][#4][#5][#6]{}

\unexpanded\def\definemathcommand
  {\dotripleempty\dodefinemathcommand}

\def\dodefinemathcommand[#1][#2][#3]#4% command class args meaning
  {\ifthirdargument
     \processaction
       [#3]
       [one=>\setuvalue{#1}##1{\puremathcomm{#2}{#4{##1}}},
        two=>\setuvalue{#1}##1##2{\puremathcomm{#2}{#4{##1}{##2}}}]%
   \else\ifsecondargument
     \setuvalue{#1}{\puremathcomm{#2}{#4}}%
   \else
     \setuvalue{#1}{\puremathcomm{nothing}{#4}}%
   \fi\fi}

%D Moved from font-ini.mkiv:
%D
%D \macros
%D   {mf,mbox,enablembox,mathop}
%D
%D Todo:

\unexpanded\def\mf
  {\csname\fontalternative\endcsname}

\let\normalmathop\mathop

\unexpanded\def\mathop
  {\normalmathop
   \bgroup
   \let\rm\mf
   \let\next=}

\def\normalmbox
  {\normalhbox\bgroup\mf
   \dowithnextbox{\flushnextbox\egroup}\normalhbox}

\def\mbox % we cannot add \dontleavehmode ... else no \setbox0\mbox possible
  {\ifmmode\normalmbox\else\normalhbox\fi}

\unexpanded\def\enablembox
  {\appendtoks
     \ifx\normalhbox\undefined\let\normalhbox\hbox\fi
     \let\hbox\mbox
   \to\everymathematics}

%D needed for sin, cos etc

\let\mathfunction\firstofoneargument

\def\mfunction         #1{{\mr#1}}
\def\mfunctionlabeltext#1{{\mr\mathlabeltext{#1}}}

% \def\mlimitsfunction  #1{\mathlimopcomm{{\mr#1}}
% \def\mnolimitsfunction#1{\mathnolopcomm{{\mr#1}}

%D Taco posted this solution as response to a mail by Olivier, so
%D let's integrate it here.

\def\currentmscaledstyle{rm} % will be plugged into the typeface text=ss option

\unexpanded\def\do@mathopnolimits    #1{\mathop{\mscaledtext{#1}}\nolimits}
\unexpanded\def\do@mfunction         #1{\mscaledtext{#1}}
\unexpanded\def\do@mfunctionlabeltext#1{\mscaledtext{\mathlabeltext{#1}}}

\def\setmathfunctionstyle#1% rm ss tt (can be made faster if needed)
  {\doifsomething{#1}
     {\def\currentmscaledstyle{#1}%
      \let\mathopnolimits    \do@mathopnolimits
      \let\mfunction         \do@mfunction
      \let\mfunctionlabeltext\do@mfunctionlabeltext}}

\def\mscaledtext#1%
  {\mathchoice
     {\hbox{\csname\currentmscaledstyle\endcsname\tf  #1}}
     {\hbox{\csname\currentmscaledstyle\endcsname\tf  #1}}
     {\hbox{\csname\currentmscaledstyle\endcsname\tfx #1}}
     {\hbox{\csname\currentmscaledstyle\endcsname\tfxx#1}}}

%D We can force the way functions are typeset by manipulating the text
%D option:
%D
%D \starttyping
%D \definetypeface[iwona][ss][sans][iwona][default][encoding=texnansi]
%D \definetypeface[iwona][mm][math][iwona][default][encoding=texnansi,text=ss]
%D \stoptyping
%D
%D This hooks into the math handler with:

% no longer supported this way, has to be done with \setupmathematics
%
% \appendtoks
%     \setmathfunctionstyle\currentmathtextstyle
% \to \everybodyfont

%D Usage:
%D
%D \starttyping
%D \setmathfunctionstyle\fontstyle % or {rm} or {ss} or ..
%D \rm test $\sin{(x^{\sin(x^{\sin(x)})})}$ test
%D \ss test $\sin{(x^{\sin(x^{\sin(x)})})}$ test
%D \tt test $\sin{(x^{\sin(x^{\sin(x)})})}$ test
%D \stoptyping

%D Some goodies:

\def\Angstrom{\nomathematics{\Aring}}

%D \macros
%D   {nonknuthmode, donknuthmode}
%D
%D The underscore is frequently used in manuals but unfortunately \TEX\ prefers
%D it to be a math specific character. And since computer modern fonts didn't
%D have an underscore, one had to use commands to fake one. Nowadays we do
%D have underscores in latin modern, and since all other fonts have them, we
%D decided to get away from the restriction to use the underscore character in
%D text mode.
%D
%D \starttyping
%D \def\test#1{#1}
%D
%D \nonknuthmode $x_2$ x_2 \test{$x_2$} \test{x_2}
%D
%D \donknuthmode $x_2$ x_2 \test{$x_2$} \test{x_2}
%D \stoptyping
%D
%D The result is as expected: the first line typesets ok, while the second
%D one triggers an error message.

\newtoks\everydonknuthmode
\newtoks\everynonknuthmode

\def\nonknuthmode{\the\everynonknuthmode\let\nonknuthmode\relax}
\def\donknuthmode{\the\everydonknuthmode}

\ifdefined\normalsuber\else \def\normalsuber{_} \fi
\ifdefined\normalsuper\else \def\normalsuper{^} \fi
\ifdefined\normalaltab\else \def\normalaltab{&} \fi

\bgroup

    \catcode`_=\active
    \catcode`^=\active
    \catcode`&=\active

    \global \everynonknuthmode {\appendtoks
        \let_\normalsuber
        \let^\normalsuper
        \let&\normalaltab
    \to \everymathematics}

\egroup

\appendtoks
    \mathcode`_="8000
    \mathcode`^="8000
    \mathcode`&="8000
    \catcode`_=\@@other
    \catcode`^=\@@other
    \catcode`&=\@@other
\to \everynonknuthmode

\appendtoks
    \catcode`_=\@@subscript
    \catcode`^=\@@superscript
    \catcode`&=\@@alignment
\to \everydonknuthmode

\appendtoks
    \startextendcatcodetable\ctxcatcodes
        \catcode`_=\@@other
        \catcode`^=\@@other
        \catcode`&=\@@other
    \stopextendcatcodetable
\to \everynonknuthmode

\appendtoks
    \startextendcatcodetable\ctxcatcodes
        \catcode`_=\@@subscript
        \catcode`^=\@@superscript
        \catcode`&=\@@alignment
    \stopextendcatcodetable
\to \everydonknuthmode

%D Even more drastic:

\def\asciimode{\catcodetable\txtcatcodes\nonknuthmode}

%D Needed for unicode:

\def\nulloperator{\mathortext{\mathop{\null}}{\null}}

%D To be dealt with ...

\mathcode`\ ="8000 % \space
\mathcode`\'="8000 % ^\prime
\mathcode`\_="8000 % \_

%D \macros
%D   {setupmathematics}
%D
%D Configuration for integrals. (If needed we can speed this up and make it
%D installable; no processaction is needed then).

\newtoks\everysetupmathematics

\unexpanded\def\setupmathematics
  {\dosingleargument\dosetupmathematics}

\def\dosetupmathematics[#1]%
  {\getparameters[\??mo][#1]%
   \the\everysetupmathematics}

\def\mathematicsparameter#1{\ifcsname\??mo#1\endcsname\csname\??mo#1\endcsname\fi}

%D Memory saver:

\appendtoks
   \doifelse{\mathematicsparameter\v!compact}\v!yes
     {\ctxlua{fonts.vf.math.optional=true}}
     {\ctxlua{fonts.vf.math.optional=false}}%
\to \everysetupmathematics

\setupmathematics
  [\v!compact=no]

%D Delayed: greek.
%D
%D \starttyping
%D \usetypescript[cambria]\setupbodyfont[cambria]
%D \startTEXpage
%D     $\alpha \mathgreekupright \alpha \mathgreekitalic \alpha$
%D \stopTEXpage
%D \stoptyping

% [lc uc] normal (upright) = 2, italic = 3, none = 0/1

\setevalue{\??mo:greek:\v!normal :\v!normal}{\attribute\mathgreekattribute22 }
\setevalue{\??mo:greek:\v!normal :\v!italic}{\attribute\mathgreekattribute23 }
\setevalue{\??mo:greek:\v!normal :\v!none  }{\attribute\mathgreekattribute21 }

\setevalue{\??mo:greek:\v!italic :\v!normal}{\attribute\mathgreekattribute32 }
\setevalue{\??mo:greek:\v!italic :\v!italic}{\attribute\mathgreekattribute33 }
\setevalue{\??mo:greek:\v!italic :\v!none  }{\attribute\mathgreekattribute31 }

\setevalue{\??mo:greek:\v!none   :\v!normal}{\attribute\mathgreekattribute12 }
\setevalue{\??mo:greek:\v!none   :\v!italic}{\attribute\mathgreekattribute13 }
\setevalue{\??mo:greek:\v!none   :\v!none  }{\attribute\mathgreekattribute\attributeunsetvalue}

\appendtoks
    \csname\??mo:greek:\@@molcgreek:\@@moucgreek\endcsname
\to \everymathematics

\setupmathematics
  [lcgreek=\v!italic,
   ucgreek=\v!normal] % was: none

%D \macros
%D   {enablemathpunctuation,disablemathpunctuation}
%D
%D \startbuffer
%D \enablemathpunctuation$(1,2) (1, 2) (1{,}2) \hbox{foo, not bar}$
%D \stopbuffer
%D
%D \typebuffer
%D
%D \blank{\getbuffer}\blank

\setfalse \automathpunctuation

\def\enablemathpunctuation {\settrue \automathpunctuation}
\def\disablemathpunctuation{\setfalse\automathpunctuation}

\ifx\v!autopunctuation\undefined \def\v!autopunctuation{autopunctuation} \fi

\appendtoks
    \doifelse{\mathematicsparameter\v!autopunctuation}\v!yes\enablemathpunctuation\disablemathpunctuation
\to \everysetupmathematics

\appendtoks
    \ifconditional\automathpunctuation\attribute\mathpunctuationattribute\plusone\fi
\to \everymathematics

\setupmathematics
  [\v!autopunctuation=\v!no]

%D \macros
%D   {mathstyle}
%D
%D If one want to be sure that something is typeset in the
%D appropriate style, \type {\mathstyle} can be used:
%D
%D \starttyping
%D \mathstyle{something}
%D \stoptyping

% \def\mathstyle#1%
%   {\mathchoice
%      {\displaystyle     #1}%
%      {\textstyle        #1}%
%      {\scriptstyle      #1}%
%      {\scriptscriptstyle#1}}
%
% We now have a primitive operation for this. As the
% macro overloads a new primitive introduced in \LUATEX,
% we need to use \type {\normalmathstyle} when we consult
% the current math style.
%
% \let \mathstyle \Ustack % spoils cramped
%
% \let \mathstyle \firstofoneargument
%
% 0 = display
% 1 = crampeddisplay
% 2 = text
% 3 = crampedtext
% 4 = script
% 5 = crampedscript
% 6 = scriptscript
% 7 = crampedscriptscript

\def\uncramped#1%
  {{\ifcase\normalmathstyle
    \or \displaystyle      \or
    \or \textstyle         \or
    \or \scriptstyle       \or
    \or \scriptscriptstyle \fi
    #1}}

\def\cramped#1%
  {{\ifcase\normalmathstyle
    \crampeddisplaystyle      \or \or % 0 -> 1
    \crampedtextstyle         \or \or % 2 -> 3
    \crampedscriptstyle       \or \or % 4 -> 5
    \crampedscriptscriptstyle \fi     % 6 -> 7
    #1}}

\def\triggermathstyle#1% #1 is number
  {\ifcase#1\relax
     \displaystyle             \or
     \crampeddisplaystyle      \or
     \textstyle                \or
     \crampedtextstyle         \or
     \scriptstyle              \or
     \crampedscriptstyle       \or
     \scriptscriptstyle        \or
     \crampedscriptscriptstyle \else
     % error
   \fi}

\def\cramped#1%
  {{\ifcase\normalmathstyle
    \crampeddisplaystyle      \or \or % 0 -> 1
    \crampedtextstyle         \or \or % 2 -> 3
    \crampedscriptstyle       \or \or % 4 -> 5
    \crampedscriptscriptstyle \fi     % 6 -> 7
    #1}}

\def\mathstylefont#1% #1 is number (\normalmathstyle)
  {\ifcase#1\relax
     \textfont         \or
     \textfont         \or
     \textfont         \or
     \textfont         \or
     \scriptfont       \or
     \scriptfont       \or
     \scriptscriptfont \or
     \scriptscriptfont \else
     \textfont
   \fi\zerocount}

%D Something similar can be used in the (re|)|definition
%D of \type {\text}. This version is a variation on the one
%D in the math module (see \type{m-math} and|/|or \type
%D {m-newmat}).

\unexpanded\def\mathtext
  {\mathortext\domathtext\hbox}

\def\domathtext#1%
  {\mathchoice
     {\dodomathtext\displaystyle\textface        {#1}}%
     {\dodomathtext\textstyle   \textface        {#1}}%
     {\dodomathtext\textstyle   \scriptface      {#1}}%
     {\dodomathtext\textstyle   \scriptscriptface{#1}}}

\def\dodomathtext#1#2#3% no \everymath !
 %{\hbox{\everymath{#1}\switchtobodyfont  [#2]#3}} % 15 sec
  {\hbox{\everymath{#1}\setcurrentfontbody{#2}#3}} %  3 sec (no math)

%D Safeguard against redefinitions:

\appendtoks
    \let\_\normalunderscore % is textunderscore or fakeunderscore
\to \everymathematics

%D Because we may overload \type {\text} in other (structuring)
%D macros, we say:

\appendtoks \let\text\mathtext \to \everymathematics

%D The next code is derived from plain \TEX.

\newcount\interdisplaylinepenalty \interdisplaylinepenalty=100

\newif\ifdt@p

\def\displ@y
  {\global\dt@ptrue
   \openup\displayopenupvalue % was \openup\jot
   \everycr
     {\noalign
        {\ifdt@p
           \global\dt@pfalse
           \ifdim\prevdepth>-\thousandpoint
             \vskip-\lineskiplimit
             \vskip\normallineskiplimit
           \fi
         \else
           \penalty\interdisplaylinepenalty
         \fi}}}

\let\normaldispl@y\displ@y

\def\displ@y{\resetdisplaymatheq\normaldispl@y}

\def\m@th{\mathsurround\zeropoint} % obsolete

%D Text in math:

\def\mathortext
  {\ifmmode
     \expandafter\firstoftwoarguments
   \else
     \expandafter\secondoftwoarguments
   \fi}

% \defineactivecharacter _ {\mathortext{_}{\_}} text_text  $a^2$

% force text mode, will be overloaded later

\ifx\text\undefined \let\text\hbox \fi

\def\mathoptext#1{\mathop{\text{#1}}}

\protect \endinput