summaryrefslogtreecommitdiff
path: root/source/luametatex/source/tex/texmath.c
blob: e9371400026fcb394ce4c472d7e710beb2640145 (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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
/*
    See license.txt in the root of this project.
*/

# include "luametatex.h"

/*tex 

    The code can be simplified a lot when we decide that traditional 8 bit fonts are virtualized in
    a way that avoids the split delimiter definitions (small and large) and that the traditional 
    way to define characters is dropped in favor of the unicode variants. So, this might happen at
    some point. After all it makes no sense to use this engine with traditional fonts because there
    \PDFTEX\ is a better choice. 

    We might also benefit more from the fact that we have prev pointers. Occasionally I visit this 
    file and make some variables more verbose. I'm in no hurry with that. 

*/

/*tex

    When \TEX\ reads a formula that is enclosed between |$|'s, it constructs an \quote {mlist},
    which is essentially a tree structure representing that formula. An mlist is a linear sequence
    of items, but we can regard it as a tree structure because mlists can appear within mlists. For
    example, many of the entries can be subscripted or superscripted, and such \quote {scripts} are
    mlists in their own right.

    An entire formula is parsed into such a tree before any of the actual typesetting is done,
    because the current style of type is usually not known until the formula has been fully scanned.
    For example, when the formula |$a+b \over c+d$| is being read, there is no way to tell that |a+b|
    will be in script size until |\over| has appeared.

    During the scanning process, each element of the mlist being built is classified as a relation,
    a binary operator, an open parenthesis, etc., or as a construct like |\sqrt| that must be built
    up. This classification appears in the mlist data structure.

    After a formula has been fully scanned, the mlist is converted to an hlist so that it can be
    incorporated into the surrounding text. This conversion is controlled by a recursive procedure
    that decides all of the appropriate styles by a \quote {top-down} process starting at the
    outermost level and working in towards the subformulas. The formula is ultimately pasted together
    using combinations of horizontal and vertical boxes, with glue and penalty nodes inserted as
    necessary.

    An mlist is represented internally as a linked list consisting chiefly of \quote {noads}
    (pronounced \quotation {no-adds}), to distinguish them from the somewhat similar \quote {nodes}
    in hlists and vlists. Certain kinds of ordinary nodes are allowed to appear in mlists together
    with the noads; \TEX\ tells the difference by means of the |type| field, since a noad's |type|
    is always greater than that of a node. An mlist does not contain character nodes, hlist nodes,
    vlist nodes, math nodes or unset nodes; in particular, each mlist item appears in the
    variable-size part of |mem|, so the |type| field is always present.

    Each noad is five or more words long. The first word contains the |type| and |subtype| and |link|
    fields that are already so familiar to us; the second contains the attribute list pointer, and
    the third, fourth an fifth words are called the noad's |nucleus|, |subscr|, and |supscr| fields.
    (This use of a combined attribute list is temporary. Eventually, each of fields need their own
    list)

    Consider, for example, the simple formula |$x^2$|, which would be parsed into an mlist containing
    a single element called an |ord_noad|. The |nucleus| of this noad is a representation of |x|, the
    |subscr| is empty, and the |supscr| is a representation of |2|.

    The |nucleus|, |subscr|, and |supscr| fields are further broken into subfields. If |p| points to
    a noad, and if |q| is one of its principal fields (e.g., |q=subscr(p)|), |q=null| indicates a
    field with no value (the corresponding attribute of noad |p| is not present). Otherwise, there are
    several possibilities for the subfields, depending on the |type| of |q|.

    \startitemize

        \startitem
            |type(q)=math_char_node| means that |math_fam(q)| refers to one of the sixteen font
            families, and |character(q)| is the number of a character within a font of that family, as
            in a character node.
        \stopitem

        \startitem
            |type(q) = math_text_char_node| is similar, but the character is unsubscripted and
            unsuperscripted and it is followed immediately by another character from the same font.
            (This |type| setting appears only briefly during the processing; it is used to suppress
            unwanted italic corrections.)
        \stopitem

        \startitem
            |type(q) = sub_box_node| means that |math_list(q)| points to a box node (either an
            |hlist_node| or a |vlist_node|) that should be used as the value of the field. The
            |shift_amount| in the subsidiary box node is the amount by which that box will be 
            shifted downward.
        \stopitem

        \startitem
            |type(q) = sub_mlist_node| means that |math_list(q)| points to an mlist; the mlist must
            be converted to an hlist in order to obtain the value of this field.
        \stopitem

        \startitem
            In the latter case, we might have |math_list(q) = null|. This is not the same as |q =
            null|; for example, |$P_{\}$| and |$P$| produce different results (the former will not
            have the \quote {italic correction} added to the width of |P|, but the \quote {script
            skip} will be added).
        \stopitem

    \startitemize

    Concerning display skips, \TEX\ normally always inserts before and only after when larger than
    zero. This can now be controlled with |\mathdisplayskipmode|:

    \starttabulate
    \NC 0 \NC normal \TEX \NC \NR
    \NC 1 \NC always      \NC \NR
    \NC 2 \NC non-zero    \NC \NR
    \NC 3 \NC ignore      \NC \NR
    \stoptabulate

*/

math_state_info lmt_math_state = {
    .size       = 0,
    .level      = 0,
    .par_head   = NULL,
    .fam_head   = NULL,
    .last_left  = 0,
    .last_right = 0,
    .last_atom  = 0,
    .scale      = scaling_factor,
};

static int      tex_aux_scan_math           (halfword p, halfword style, int usetextfont, halfword toks, halfword toks_text, int nocomponent, halfword cls, halfword all);
static halfword tex_aux_finish_math_list    (halfword p);
static void     tex_aux_math_math_component (halfword n, int append);

# define cramped 1

# define cramped_style(A) (2 * ((A) / 2) + cramped)                     /*tex cramp the style */
# define sub_style(A)     (2 * ((A) / 4) + script_style + cramped)      /*tex smaller and cramped */
# define sup_style(A)     (2 * ((A) / 4) + script_style + ((A) % 2))    /*tex smaller */
# define num_style(A)     ((A) + 2 - 2 * ((A) / 6))                     /*tex smaller unless already scriptscript */
# define denom_style(A)   (2 * ((A) / 2) + cramped + 2 - 2 * ((A) / 6)) /*tex smaller, cramped */
# define sup_sup_style(A) sup_style(sup_style((A)))                     /*tex smaller */

inline static mathdictval tex_fake_math_dict(halfword chr) 
{
    mathdictval d = tex_no_dict_code();
    if (math_dict_properties_par || math_dict_group_par) {
        d.properties = (unsigned short) math_dict_properties_par;
        d.group = (unsigned short) math_dict_group_par;
        d.index = (unsigned int) chr;
    }
    return d;
}

void tex_math_copy_char_data(halfword target, halfword source, int wipelist)
{
    if (node_type(source) == math_char_node) {
        kernel_math_family(target) = kernel_math_family(source);
        kernel_math_character(target) = kernel_math_character(source);
        kernel_math_options(target) = kernel_math_options(source);
        kernel_math_properties(target) = kernel_math_properties(source);
        kernel_math_group(target) = kernel_math_group(source);
        kernel_math_index(target) = kernel_math_index(source);
    } else {
        kernel_math_list(target) = kernel_math_list(source);
        if (wipelist) { 
           kernel_math_list(source) = null;
        }
    }
}

static inline void tex_math_set_scripts_options(halfword n)
{
    switch (math_scripts_mode_par) { 
        case 1: noad_options(n) |= noad_option_fixed_super_or_sub_script; break;
        case 2: noad_options(n) |= noad_option_fixed_super_and_sub_script; break;
    }
}

// static const math_styles map_cramped_style[] = { /*tex cramp the style */
//     cramped_display_style,
//     cramped_display_style,
//     cramped_text_style,
//     cramped_text_style,
//     cramped_script_style,
//     cramped_script_style,
//     cramped_script_script_style,
//     cramped_script_script_style,
// };
//
// static const math_styles map_subscript_style[] = { /*tex smaller and cramped */
//     cramped_script_style,
//     cramped_script_style,
//     cramped_script_style,
//     cramped_script_style,
//     cramped_script_script_style,
//     cramped_script_script_style,
//     cramped_script_script_style,
//     cramped_script_script_style,
// };
//
// static const math_styles map_superscript_style[] = { /*tex smaller */
//     script_style,
//     script_style,
//     script_style,
//     script_style,
//     script_script_style,
//     script_script_style,
//     script_script_style,
//     script_script_style,
// };
//
// static const math_styles map_numerator_style[] = {/*tex smaller unless already scriptscript */
//     script_style,
//     cramped_script_style,
//     script_style,
//     cramped_script_style,
//     script_script_style,
//     cramped_script_script_style,
//     script_script_style,
//     cramped_script_script_style,
// };
//
// static const math_styles map_denominator_style[] = { /*tex smaller, all cramped */
//     cramped_script_style,
//     cramped_script_style,
//     cramped_script_style,
//     cramped_script_style,
//     cramped_script_script_style,
//     cramped_script_script_style,
//     cramped_script_script_style,
//     cramped_script_script_style,
// };
//
// static const math_styles map_double_superscript_style[] = { /*tex smaller, keep cramped */
//     script_style,
//     cramped_script_style,
//     script_style,
//     cramped_script_style,
//     script_script_style,
//     cramped_script_script_style,
//     script_script_style,
//     cramped_script_script_style,
// };

/*tex
    This is very \TEX: a variable class influences the family being used.
*/

halfword tex_size_of_style(halfword style)
{
    switch (style) {
        case script_style:
        case cramped_script_style:
            return script_size;
        case script_script_style:
        case cramped_script_script_style:
            return script_script_size;
        default:
            return text_size;
    }
}

halfword tex_math_style_variant(halfword style, halfword param)
{
    switch (tex_get_math_parameter(style, param, NULL)) {
        case math_normal_style_variant:
            return style;
        case math_cramped_style_variant:
         // return map_cramped_style[s];
            return cramped_style(style);
        case math_subscript_style_variant:
         // return map_subscript_style[s];
            return sub_style(style);
        case math_superscript_style_variant:
        case math_small_style_variant:
         // return map_superscript_style[s];
            return sup_style(style);
        case math_smaller_style_variant:
        case math_numerator_style_variant:
         // return map_numerator_style[s];
            return num_style(style);
        case math_denominator_style_variant:
         // return map_denominator_style[s];
            return denom_style(style);
        case math_double_superscript_variant:
         // return map_double_superscript_style[s];
            return sup_sup_style(style);
        default:
            return style;
    }
}

int tex_math_has_class_option(halfword cls, int option)
{
    halfword value = count_parameter(first_math_options_code + cls);
    if (value == no_class_options) {
        unsigned parent = (unsigned) count_parameter(first_math_parent_code + cls);
        cls = (parent >> 16) & 0xFF;
        if (! valid_math_class_code(cls)) {
            return 0;
        }
        value = count_parameter(first_math_options_code + cls);
    }
    return (value & option) == option;
}

int tex_math_has_class_parent(halfword cls)
{
    halfword value = count_parameter(first_math_options_code + cls);
    if (value == no_class_options) {
        unsigned parent = (unsigned) count_parameter(first_math_parent_code + cls);
        return (parent >> 16) & 0xFF;
    }
    return 0;
}

static void tex_aux_unsave_math(void)
{
    tex_unsave();
    lmt_save_state.save_stack_data.ptr -= saved_math_n_of_items;
    tex_flush_node_list(lmt_dir_state.text_dir_ptr);
    if (saved_type(saved_math_item_direction) == text_direction_save_type) {
        lmt_dir_state.text_dir_ptr = saved_value(saved_math_item_direction);
    } else {
        tex_confusion("unsave math");
    }
}

/*tex

    Sometimes it is necessary to destroy an mlist. The following subroutine empties the current
    list, assuming that |abs(mode) = mmode|  aka |is_m_mode(mode)|.

*/

void tex_flush_math(void)
{
    halfword head = cur_list.head;
    tex_flush_node_list(node_next(head));
    tex_flush_node_list(cur_list.incomplete_noad);
    node_next(head) = null;
    cur_list.tail = head;
    cur_list.incomplete_noad = null;
}

/*tex A printing helper. */

static void tex_aux_print_parameter(const char *what, halfword style, halfword param, halfword indirect, halfword value)
{
    tex_begin_diagnostic();
    tex_print_format("{%s ", what);
    if (indirect >= 0 && indirect <= last_math_indirect) {
        tex_print_str(lmt_interface.math_indirect_values[indirect].name);
        tex_print_char(' ');
    }
    if (param < math_parameter_last) {
        tex_print_cmd_chr(set_math_parameter_cmd, param);
    } else {
        tex_print_format("%x %x ", math_parameter_spacing_left(param), math_parameter_spacing_right(param));
    }
    tex_print_cmd_chr(math_style_cmd, style);
    tex_print_char('=');
    switch (math_parameter_value_type(param)) {
        case math_int_parameter:
        case math_style_parameter:
            tex_print_int(value);
            break;
        case math_dimen_parameter:
            tex_print_dimension(value, pt_unit);
            break;
        case math_muglue_parameter:
            tex_print_spec(value, mu_unit);
            break;
        default:
            tex_print_int(value);
            break;
    }
    tex_print_char('}');
    tex_end_diagnostic();
}

static void tex_aux_print_fam(const char *what, halfword size, halfword fam)
{
    tex_begin_diagnostic();
    tex_print_format("{%s %C family %i: %F}", what, define_family_cmd, size, fam, tex_fam_fnt(fam, size));
    tex_end_diagnostic();
}

/*tex
    Before we can do anything in math mode, we need fonts. We can use |max_n_of_math_families|
    instead of 256 but we need to pack in bytes anyway so there is no gain.
*/

int tex_fam_fnt(int fam, int size)
{
    sa_tree_item item;
    sa_get_item_4(lmt_math_state.fam_head, fam + (256 * size), &item);
    return (int) item.int_value;
}

void tex_def_fam_fnt(int fam, int size, int fnt, int level)
{
    sa_tree_item item;
    item.int_value = fnt;
    sa_set_item_4(lmt_math_state.fam_head, fam + (256 * size), item, level);
    if (tracing_assigns_par > 1) {
        tex_aux_print_fam("assigning", size, fam);
    }
    tex_fixup_math_parameters(fam, size, fnt, level);
}

static void tex_aux_unsave_math_fam_data(int gl)
{
    if (lmt_math_state.fam_head->stack) {
        while (lmt_math_state.fam_head->sa_stack_ptr > 0 && abs(lmt_math_state.fam_head->stack[lmt_math_state.fam_head->sa_stack_ptr].level) >= (int) gl) {
            sa_stack_item item = lmt_math_state.fam_head->stack[lmt_math_state.fam_head->sa_stack_ptr];
            if (item.level > 0) {
                sa_rawset_item_4(lmt_math_state.fam_head, item.code, item.value_1);
                /*tex Now do a trace message, if requested. */
                if (tracing_restores_par > 1) {
                    int size = item.code / 256;
                    int fam = item.code % 256;
                    tex_aux_print_fam("restoring", size, fam);
                }
            }
            (lmt_math_state.fam_head->sa_stack_ptr)--;
        }
    }
}

/*tex Math parameters, we have a lot of them! Todo: move the style into 2 */

void tex_def_math_parameter(int style, int param, scaled value, int level, int indirect)
{
    sa_tree_item item1, item2;
    int different = 1;
    if (level <= 1) {
        if (math_parameter_value_type(param) == math_muglue_parameter) {
            sa_get_item_8(lmt_math_state.par_head, (param + (math_parameter_max_range * style)), &item1, &item2);
            if (item2.int_value == indirect_math_regular && item1.int_value > thick_mu_skip_code) {
                if (lmt_node_memory_state.nodesizes[item1.int_value]) {
                    tex_free_node(item1.int_value, glue_spec_size);
                }
            }
        }
    } else { 
        /*tex Less tracing at the cost of a lookup. */
        sa_get_item_8(lmt_math_state.par_head, (param + (math_parameter_max_range * style)), &item1, &item2);
        different = item1.int_value != value || item2.int_value != indirect;
    }
 // if (different) { // maybe
        item1.int_value = value;
        item2.int_value = indirect;
        sa_set_item_8(lmt_math_state.par_head, (param + (math_parameter_max_range * style)), item1, item2, level);
        if (different && tracing_assigns_par > 1) {
            tex_aux_print_parameter("assigning", style, param, indirect, value);
        }
 // }
}

// mukern .. there is no mudimen

scaled tex_get_math_parameter(int style, int param, halfword *type)
{
    halfword indirect, value;
    sa_tree_item v1, v2;
    sa_get_item_8(lmt_math_state.par_head, (param + (math_parameter_max_range * style)), &v1, &v2);
    indirect = v2.int_value == lmt_math_state.par_head->dflt.int_value ? indirect_math_unset : v2.uint_value;
    value = v1.int_value;
    switch (indirect) {
        case indirect_math_unset:
            if (type) {
                *type = no_val_level;
            }
            return MATHPARAMDEFAULT;
        /* we stored nothing */
        case indirect_math_regular:
            switch (math_parameter_value_type(param)) {
                case math_dimen_parameter:
                    if (type) {
                        *type = dimen_val_level;
                    }
                    return value;
                case math_muglue_parameter:
                    if (type) {
                        *type = mu_val_level;
                    }
                    return value <= thick_mu_skip_code ? mu_glue_parameter(value) : value;
             // case math_int_parameter:
             // case math_style_parameter:
                default:
                    if (type) {
                        *type = int_val_level;
                    }
                    return value;
            }
        /* we stored cs */
        case indirect_math_integer:
            if (! value) {
                if (type) {
                    *type = int_val_level;
                }
                return value;
            } else if (eq_type(value) == integer_cmd) {
                if (type) {
                    *type = int_val_level;
                }
                return eq_value(value);
            } else {
                goto MISMATCH;
            }
        case indirect_math_dimension:
            if (! value) {
                if (type) {
                    *type = dimen_val_level;
                }
                return value;
            } else if (eq_type(value) == dimension_cmd) {
                if (type) {
                    *type = dimen_val_level;
                }
                return eq_value(value);
            } else if (eq_type(value) == posit_cmd) {
                if (type) {
                    *type = dimen_val_level;
                }
                return tex_posit_to_dimension(eq_value(value));
            } else {
                goto MISMATCH;
            }
        case indirect_math_mugluespec:
            if (! value) {
                if (type) {
                    *type = mu_val_level;
                }
                return value;
            } else {
                switch (eq_type(value)) {
                    case mugluespec_cmd:
                        if (type) {
                            *type = mu_val_level;
                        }
                        return eq_value(value);
                    default:
                        goto MISMATCH;
                }

            }
        case indirect_math_gluespec:
            if (! value) {
                if (type) {
                    *type = glue_val_level;
                }
                return value;
            } else {
                switch (eq_type(value)) {
                    case gluespec_cmd:
                        if (type) {
                            *type = glue_val_level;
                        }
                        return eq_value(value);
                    default:
                        goto MISMATCH;
                }
            }
        /* we stored chr */
        case indirect_math_register_integer:
            if (! value) {
                if (type) {
                    *type = int_val_level;
                }
                return value;
            } else if (eq_type(value) == register_int_reference_cmd) {
                if (type) {
                    *type = int_val_level;
                }
                return eq_value(value);
            } else {
                goto MISMATCH;
            }
        case indirect_math_register_dimension:
            if (! value) {
                if (type) {
                    *type = dimen_val_level;
                }
                return value;
            } else if (eq_type(value) == register_dimen_reference_cmd) {
                if (type) {
                    *type = dimen_val_level;
                }
                return eq_value(value);
            } else {
                goto MISMATCH;
            }
        case indirect_math_register_gluespec:
            if (! value) {
                if (type) {
                    *type = glue_val_level;
                }
                return value;
            } else if (eq_type(value) == register_glue_reference_cmd) {
                if (type) {
                    *type = glue_val_level;
                }
                return eq_value(value);
            } else {
                goto MISMATCH;
            }
        case indirect_math_register_mugluespec:
            if (! value) {
                if (type) {
                    *type = mu_val_level;
                }
                return value;
            } else if (eq_type(value) == register_mu_glue_reference_cmd) {
                if (type) {
                    *type = mu_val_level;
                }
                return eq_value(value);
            } else {
                goto MISMATCH;
            }
        case indirect_math_internal_integer:
            if (! value) {
                if (type) {
                    *type = int_val_level;
                }
                return value;
            } else if (eq_type(value) == internal_int_reference_cmd) {
                if (type) {
                    *type = int_val_level;
                }
                return eq_value(value);
            } else {
                goto MISMATCH;
            }
        case indirect_math_internal_dimension:
            if (! value) {
                if (type) {
                    *type = dimen_val_level;
                }
                return value;
            } else if (eq_type(value) == internal_dimen_reference_cmd) {
                if (type) {
                    *type = dimen_val_level;
                }
                return eq_value(value);
            } else {
                goto MISMATCH;
            }
        case indirect_math_internal_gluespec:
            if (! value) {
                if (type) {
                    *type = glue_val_level;
                }
                return value;
            } else if (eq_type(value) == internal_glue_reference_cmd) {
                if (type) {
                    *type = glue_val_level;
                }
                return eq_value(value);
            } else {
                goto MISMATCH;
            }
        case indirect_math_internal_mugluespec:
            if (! value) {
                if (type) {
                    *type = mu_val_level;
                }
                return value;
            } else  if (eq_type(value) == internal_mu_glue_reference_cmd) {
                if (type) {
                    *type = mu_val_level;
                }
                return eq_value(value);
            } else {
                goto MISMATCH;
            }
        default:
          MISMATCH:
            tex_handle_error(
                normal_error_type,
                "Invalid inherited math parameter",
                "You probably changed the type of the inherited math parameter, so I will "
                "use zero instead."
            );
            return 0;
    }
}

int tex_has_math_parameter(int style, int param)
{
    sa_tree_item v1, v2;
    sa_get_item_8(lmt_math_state.par_head, (param + (math_parameter_max_range * style)), &v1, &v2);
    return v2.int_value == lmt_math_state.par_head->dflt.int_value ? indirect_math_unset : v2.uint_value;
}

static void tex_aux_unsave_math_parameter_data(int gl)
{
    if (lmt_math_state.par_head->stack) {
        while (lmt_math_state.par_head->sa_stack_ptr > 0 && abs(lmt_math_state.par_head->stack[lmt_math_state.par_head->sa_stack_ptr].level) >= (int) gl) {
            sa_stack_item item = lmt_math_state.par_head->stack[lmt_math_state.par_head->sa_stack_ptr];
            if (item.level > 0) {
                int param = item.code % math_parameter_max_range;
                int style = item.code / math_parameter_max_range;
                if (math_parameter_value_type(param) == math_muglue_parameter) {
                    sa_tree_item item1, item2;
                    sa_get_item_8(lmt_math_state.par_head, item.code, &item1, &item2);
                    if (item2.int_value == indirect_math_regular && item1.int_value > thick_mu_skip_code) {
                     /* if (tex_valid_node(item1.int_value)) { */
                        if (lmt_node_memory_state.nodesizes[item1.int_value]) {
                            // printf("HERE 2.1: %i %i / %i %i / %i\n",item2.int_value,item1.int_value, item.value_1.int_value, item.value_2.int_value, node_type(item1.int_value));
                            tex_free_node(item1.int_value, glue_spec_size);
                        } else {
                            // printf("HERE 2.2: %i %i / %i %i / %i\n",item2.int_value,item1.int_value, item.value_1.int_value, item.value_2.int_value, node_type(item1.int_value));
                        }
                    }
                }
                sa_rawset_item_8(lmt_math_state.par_head, item.code, item.value_1, item.value_2);
                /*tex Do a trace message, if requested. */
                if (tracing_restores_par > 1) {
                    int indirect = item.value_2.int_value;
                    tex_aux_print_parameter("restoring", style, param, indirect, tex_get_math_parameter(style, param, NULL));
                }
            }
            lmt_math_state.par_head->sa_stack_ptr--;
        }
    }
}

/*tex Saving and unsaving of both: */

void tex_unsave_math_data(int level)
{
    tex_aux_unsave_math_fam_data(level);
    tex_aux_unsave_math_parameter_data(level);
}

/*tex Dumping and undumping: */

void tex_dump_math_data(dumpstream f)
{
    if (! lmt_math_state.fam_head) {
        lmt_math_state.fam_head = sa_new_tree(MATHFONTSTACK, 4, (sa_tree_item) { .int_value = MATHFONTDEFAULT });
    }
    sa_dump_tree(f, lmt_math_state.fam_head);
    if (! lmt_math_state.par_head) {
        lmt_math_state.par_head = sa_new_tree(MATHPARAMSTACK, 8, (sa_tree_item) { .int_value = MATHPARAMDEFAULT });
    }
    sa_dump_tree(f, lmt_math_state.par_head);
}

void tex_undump_math_data(dumpstream f)
{
    lmt_math_state.fam_head = sa_undump_tree(f);
    lmt_math_state.par_head = sa_undump_tree(f);
}

void tex_initialize_math(void)
{
    if (! lmt_math_state.fam_head) {
        lmt_math_state.fam_head = sa_new_tree(MATHFONTSTACK, 4, (sa_tree_item) { .int_value = MATHFONTDEFAULT });
    }
    if (! lmt_math_state.par_head) {
        lmt_math_state.par_head = sa_new_tree(MATHPARAMSTACK, 8, (sa_tree_item) { .int_value = MATHPARAMDEFAULT });
        tex_initialize_math_spacing();
    }
    return;
}

/*tex

    Each portion of a formula is classified as Ord, Op, Bin, Rel, Ope, Clo, Pun, or Inn, for purposes
    of spacing and line breaking. An |ord_noad|, |op_noad|, |bin_noad|, |rel_noad|, |open_noad|,
    |close_noad|, |punct_noad|, or |inner_noad| is used to represent portions of the various types.
    For example, an |=| sign in a formula leads to the creation of a |rel_noad| whose |nucleus| field
    is a representation of an equals sign (usually |fam = 0|, |character = 075|). A formula preceded
    by |\mathrel| also results in a |rel_noad|. When a |rel_noad| is followed by an |op_noad|, say,
    and possibly separated by one or more ordinary nodes (not noads), \TEX\ will insert a penalty
    node (with the current |rel_penalty|) just after the formula that corresponds to the |rel_noad|,
    unless there already was a penalty immediately following; and a \quote {thick space} will be
    inserted just before the formula that corresponds to the |op_noad|.

    A noad of type |ord_noad|, |op_noad|, \dots, |inner_noad| usually has a |subtype = normal|. The
    only exception is that an |op_noad| might have |subtype = limits| or |no_limits|, if the normal
    positioning of limits has been overridden for this operator.

    A |radical_noad| also has a |left_delimiter| field, which usually represents a square root sign.

    A |fraction_noad| has a |right_delimiter| field as well as a |left_delimiter|.

    Delimiter fields have four subfields called |small_fam|, |small_char|, |large_fam|, |large_char|.
    These subfields represent variable-size delimiters by giving the \quote {small} and \quote
    {large} starting characters, as explained in Chapter~17 of {\em The \TEX book}.

    A |fraction_noad| is actually quite different from all other noads. It has |thickness|,
    |denominator|, and |numerator| fields instead of |nucleus|, |subscr|, and |supscr|. The
    |thickness| is a scaled value that tells how thick to make a fraction rule; however, the special
    value |preset_rule_thickness| is used to stand for the |preset_rule_thickness| of the current
    size. The |numerator| and |denominator| point to mlists that define a fraction; we always have
    |type(numerator) = type(denominator) = sub_mlist|. The |left_delimiter| and |right_delimiter|
    fields specify delimiters that will be placed at the left and right of the fraction. In this way,
    a |fraction_noad| is able to represent all of \TEX's operators |\over|, |\atop|, |\above|,
    |\overwithdelims|, |\atopwithdelims|, and |\abovewithdelims|.

    The |new_noad| function creates an |ord_noad| that is completely |null|.

*/

halfword tex_new_sub_box(halfword curbox)
{
    halfword noad = tex_new_node(simple_noad, ordinary_noad_subtype);
    halfword sbox = tex_new_node(sub_box_node, 0);
    noad_nucleus(noad) = sbox;
    kernel_math_list(sbox) = curbox;
    return noad;
}

static quarterword tex_aux_set_math_char(halfword target, mathcodeval *mval, mathdictval *dval)
{
    halfword hmcode = tex_get_hm_code(mval->character_value);
    kernel_math_character(target) = mval->character_value;
    if (mval->class_value == math_use_current_family_code) {
        kernel_math_family(target) = cur_fam_par_in_range ? cur_fam_par : mval->family_value;
        node_subtype(target) = ordinary_noad_subtype;
    } else if (mval->family_value == variable_family_par) {
        /*tex For CMS chairman MS, so that he can answer a ltx question someplace. */
        kernel_math_family(target) = cur_fam_par_in_range ? cur_fam_par : mval->family_value;
        node_subtype(target) = mval->class_value;
    } else {
        kernel_math_family(target) = mval->family_value;
        node_subtype(target) = mval->class_value;
    }
    if (dval) { 
        kernel_math_properties(target) = dval->properties;
        kernel_math_group(target) = dval->group;
        kernel_math_index(target) = dval->index;
    }
    if ((hmcode & auto_discretionary_normal) == auto_discretionary_normal) { // has_discretionary_normal
        math_kernel_node_set_option(target, math_kernel_auto_discretionary);
    } 
    if ((hmcode & auto_discretionary_italic) == auto_discretionary_italic) {  // has_discretionary_italic
        math_kernel_node_set_option(target, math_kernel_full_discretionary);
    }
    return node_subtype(target);
}

/*tex

    A few more kinds of noads will complete the set: An |under_noad| has its nucleus underlined; an
    |over_noad| has it overlined. An |accent_noad| places an accent over its nucleus; the accent
    character appears as |math_fam (accent_chr (p))| and |math_character (accent_chr (p))|. A
    |vcenter_noad| centers its nucleus vertically with respect to the axis of the formula; in such
    noads we always have |type (nucleus (p)) = sub_box|.

    And finally, we have the |fence_noad| type, to implement \TEX's |\left| and |\right| as well as
    \ETEX's |\middle|. The |nucleus| of such noads is replaced by a |delimiter| field; thus, for
    example, |\left(| produces a |fence_noad| such that |delimiter(p)| holds the family and
    character codes for all left parentheses. A |fence_noad| of subtype |left_noad_side| never
    appears in an mlist except as the first element, and a |fence_noad| with subtype
    |right_noad_side| never appears in an mlist except as the last element; furthermore, we either
    have both a |left_noad_side| and a |right_noad_side|, or neither one is present.

    Math formulas can also contain instructions like |\textstyle| that override \TeX's normal style
    rules. A |style_node| is inserted into the data structure to record such instructions; it is
    three words long, so it is considered a node instead of a noad. The |subtype| is either
    |display_style| or |text_style| or |script_style| or |script_script_style|. The second and
    third words of a |style_node| are not used, but they are present because a |choice_node| is
    converted to a |style_node|.

    \TEX\ uses even numbers 0, 2, 4, 6 to encode the basic styles |display_style|, \dots,
    |script_script_style|, and adds~1 to get the \quote {cramped} versions of these styles. This
    gives a numerical order that is backwards from the convention of Appendix~G in {\em The \TEX
    book}; i.e., a smaller style has a larger numerical value.

*/

void tex_run_math_style(void) {
    switch (cur_chr) { 
        case yet_unset_math_style:
            { 
                halfword style = tex_scan_math_style_identifier(1, 0);
                if (is_valid_math_style(style)) {
                    halfword noad = tex_new_node(style_node, (quarterword) style);
                    cur_list.math_style = style;
                    tex_tail_append(noad);
                }
            }
            break;
        case scaled_math_style:
            { 
                halfword noad = tex_new_node(style_node, scaled_math_style);
                style_scale(noad) = tex_scan_int(0, NULL);
             // style_scale(noad) = tex_scan_positive_scale(0);
                tex_tail_append(noad);
            }
            break;
        default: 
            if (is_valid_math_style(cur_chr)) {
                halfword noad = tex_new_node(style_node, (quarterword) cur_chr);
                cur_list.math_style = cur_chr;
                tex_tail_append(noad);
            } else {
                /*tex For now silently ignored. */
            }
    }
}

/*tex

    Let's consider now the previously unwritten part of |show_node_list| that displays the things
    that can only be present in mlists; this program illustrates how to access the data structures
    just defined.

    In the context of the following program, |p| points to a node or noad that should be displayed,
    and the current string contains the \quote {recursion history} that leads to this point. The
    recursion history consists of a dot for each outer level in which |p| is subsidiary to some
    node, or in which |p| is subsidiary to the |nucleus| field of some noad; the dot is replaced by
    |_| or |^| or |/| or |\\| if |p| is descended from the |subscr| or |supscr| or |denominator| or
    |numerator| fields of noads. For example, the current string would be |{\^_/}| if |p| points to
    the |ord_noad| for |x| in the (ridiculous) formula {$\sqrt {a ^ {\mathinner {b _
    {c \over x+y}}}}$|.

*/

static void tex_aux_display_choice_noad    (halfword n, int threshold, int max);
static void tex_aux_display_parameter_node (halfword n);
static void tex_aux_display_simple_noad    (halfword n, int threshold, int max);
static void tex_aux_display_radical_noad   (halfword n, int threshold, int max);
static void tex_aux_display_accent_noad    (halfword n, int threshold, int max);
static void tex_aux_display_fence_noad     (halfword n, int threshold, int max);
static void tex_aux_display_fraction_noad  (halfword n, int threshold, int max);

static void tex_aux_print_fam_and_char(halfword n)
{
    tex_print_format(", family %x, character %x, original %x", kernel_math_family(n), kernel_math_character(n));
    tex_aux_show_dictionary(n, kernel_math_properties(n), kernel_math_group(n), kernel_math_index(n), tex_fam_fnt(kernel_math_family(n), 0), kernel_math_character(n));
}

int tex_show_math_node(halfword n, int threshold, int max)
{
    switch (node_type(n)) {
        case style_node:
            /* why not shown? */
            break;
        case choice_node:
            tex_aux_display_choice_noad(n, threshold, max);
            break;
        case parameter_node:
            tex_aux_display_parameter_node(n);
            break;
        case simple_noad:
            tex_aux_display_simple_noad(n, threshold, max);
            break;
        case radical_noad:
            tex_aux_display_radical_noad(n, threshold, max);
            break;
        case accent_noad:
            tex_aux_display_accent_noad(n, threshold, max);
            break;
        case fence_noad:
            tex_aux_display_fence_noad(n, threshold, max);
            break;
        case fraction_noad:
            tex_aux_display_fraction_noad(n, threshold, max);
            break;
        case math_text_char_node:
        case math_char_node:
            tex_aux_print_fam_and_char(n);
            break;
        case sub_box_node:
            tex_print_node_list(kernel_math_list(n), NULL, threshold, max);
            break;
        case sub_mlist_node:
            if (kernel_math_list(n)) {
                tex_print_node_list(kernel_math_list(n), NULL, threshold, max);
            } else {
                tex_print_str(", empty");
            }
            break;
        default:
            return 0;
    }
    return 1;
}

inline static halfword tex_aux_valid_delimiter(halfword d)
{
    return (d && (delimiter_small_family(d) || delimiter_small_character(d) || delimiter_large_family(d) || delimiter_large_character(d))) ? d : null;
}

static void tex_aux_print_delimiter(halfword d)
{
    if (delimiter_small_family(d) < 0) {
        /*tex This should never happen. */
        tex_print_int(-1);
    } else if (delimiter_small_family(d) < 16 && delimiter_large_family(d) < 16 && delimiter_small_character(d) < 256 && delimiter_large_character(d) < 256) {
        /*tex Traditional tex style. */
        int a = delimiter_small_family(d) * 256 + delimiter_small_character(d);
        a = a * 0x1000 + delimiter_large_family(d) * 256 + delimiter_large_character(d);
        tex_print_format(", code %x", a);
    } else if ((delimiter_large_family(d) == 0 && delimiter_large_character(d) == 0) || delimiter_small_character(d) > 65535 || delimiter_large_character(d) > 65535) {
        /*tex \LUATEX\ style. */
        tex_print_format(", family %x, character %x", delimiter_small_family(d), delimiter_small_character(d));
    }
}

/*tex

    The next subroutine will descend to another level of recursion when a subsidiary mlist needs to
    be displayed. The parameter |c| indicates what character is to become part of the recursion
    history. An empty mlist is distinguished from a missing field, because these are not equivalent
    (as explained above).

*/

static void tex_aux_display_common_noad(halfword n, int threshold, int max)
{
    tex_print_node_list(noad_nucleus(n), "nucleus", threshold, max);
    tex_print_node_list(noad_supscr(n), "superscript", threshold, max);
    tex_print_node_list(noad_subscr(n), "subscript", threshold, max);
    tex_print_node_list(noad_supprescr(n), "superprescript", threshold, max);
    tex_print_node_list(noad_subprescr(n), "subprescript", threshold, max);
    tex_print_node_list(noad_prime(n), "primescript", threshold, max);
    tex_print_node_list(noad_new_hlist(n), "newhlist", threshold, max);
}

static void tex_aux_display_parameter_node(halfword n)
{
    tex_print_format(", id %i, style %i", parameter_name(n), parameter_style(n));
}

static void tex_aux_display_choice_noad(halfword n, int threshold, int max)
{
    switch (node_subtype(n)) { 
        case normal_choice_subtype: 
            tex_print_node_list(choice_display_mlist(n), "display", threshold, max);
            tex_print_node_list(choice_text_mlist(n), "text", threshold, max);
            tex_print_node_list(choice_script_mlist(n), "script", threshold, max);
            tex_print_node_list(choice_script_script_mlist(n), "scriptscript", threshold, max);
            break;
        case discretionary_choice_subtype: 
            tex_print_format(", class %i", choice_class(n));
            tex_print_node_list(choice_pre_break(n), "pre", threshold, max);
            tex_print_node_list(choice_post_break(n), "post", threshold, max);
            tex_print_node_list(choice_no_break(n), "replace", threshold, max);
            break;
    }
}

static void tex_aux_display_simple_noad(halfword n, int threshold, int max)
{
    if (noad_source(n)) {
        tex_print_format(", source %i", noad_source(n));
    }
    tex_aux_display_common_noad(n, threshold, max);
}

static void tex_aux_display_radical_noad(halfword n, int threshold, int max) /* todo: more fields */
{
    if (noad_width(n)) {
        tex_print_format(", width %D", noad_width(n), pt_unit);
    }
    if (radical_height(n)) {
        tex_print_format(", height %D", radical_height(n), pt_unit);
    }
    if (radical_depth(n)) {
        tex_print_format(", depth %D", radical_depth(n), pt_unit);
    }
    if (radical_size(n)) {
        tex_print_format(", size %i", radical_size(n));
    }
    if (noad_source(n) != 0) {
        tex_print_format(", source %i", noad_source(n));
    }
    if (noad_options(n)) {
        tex_print_format(", options %x", noad_options(n));
    }
    if (radical_left_delimiter(n)) { 
        tex_print_str(", left");
        tex_aux_print_delimiter(radical_left_delimiter(n));
    }
    if (radical_right_delimiter(n)) { 
        tex_print_str(", right");
        tex_aux_print_delimiter(radical_right_delimiter(n));
    }
    if (radical_degree(n)) {
        tex_print_node_list(radical_degree(n), "degree", threshold, max);
    }
    tex_aux_display_common_noad(n, threshold, max);
}

static void tex_aux_display_accent_noad(halfword n, int threshold, int max) /* todo: more fields */
{
    halfword top_char = accent_top_character(n);
    halfword bottom_char = accent_bottom_character(n);
    halfword fraction = accent_fraction(n);
    if (fraction) {
        tex_print_str(", fraction ");
        tex_print_int(fraction);
    }
    switch (node_subtype(n)) {
        case bothflexible_accent_subtype:
            if (top_char) {
                tex_print_str(", top ");
                tex_aux_print_fam_and_char(top_char);
            }
            if (bottom_char) {
                tex_print_str(", bottom ");
                tex_aux_print_fam_and_char(bottom_char);
            }
            if (! (top_char || bottom_char)) {
                tex_print_str(", overlay ");
                tex_aux_print_fam_and_char(accent_middle_character(n));
            }
            break;
        case fixedtop_accent_subtype:
            if (top_char) {
                tex_print_str(", fixed top ");
                tex_aux_print_fam_and_char(top_char);
            }
            if (bottom_char) {
                tex_print_str(", bottom ");
                tex_aux_print_fam_and_char(bottom_char);
            }
            break;
        case fixedbottom_accent_subtype:
            if (top_char) {
                tex_print_str(", top ");
                tex_aux_print_fam_and_char(top_char);
            }
            if (bottom_char) {
                tex_print_str(", fixed bottom ");
                tex_aux_print_fam_and_char(bottom_char);
            }
            break;
        case fixedboth_accent_subtype:
            if (top_char) {
                tex_print_str(", fixed top ");
                tex_aux_print_fam_and_char(top_char);
            }
            if (bottom_char) {
                tex_print_str(", fixed bottom ");
                tex_aux_print_fam_and_char(bottom_char);
            }
            break;
    }
    tex_aux_display_common_noad(n, threshold, max);
}

static void tex_aux_display_fence_noad(halfword n, int threshold, int max) /* todo: more fields */
{
    if (noad_height(n)) {
        tex_print_format(", height %D", noad_height(n), pt_unit);
    }
    if (noad_depth(n)) {
        tex_print_format(", depth %D", noad_depth(n), pt_unit);
    }
    if (fence_top_overshoot(n)) {
        tex_print_format(", top %D", fence_top_overshoot(n), pt_unit);
    }
    if (fence_bottom_overshoot(n)) {
        tex_print_format(", top %D", fence_bottom_overshoot(n), pt_unit);
    }
    if (get_noad_main_class(n) != unset_noad_class) {
        tex_print_format(", class %i", get_noad_main_class(n));
    }
    if (get_noad_left_class(n) != unset_noad_class) {
        tex_print_format(", leftclass %i", get_noad_left_class(n));
    }
    if (get_noad_right_class(n) != unset_noad_class) {
        tex_print_format(", rightclass %i", get_noad_right_class(n));
    }
    if (noad_source(n) != 0) {
        tex_print_format(", source %i", noad_source(n));
    }
    if (noad_options(n)) {
        tex_print_format(", options %x", noad_options(n));
    }
    tex_aux_print_delimiter(fence_delimiter_list(n));
    tex_print_node_list(fence_delimiter_top(n), "top", threshold, max);
    tex_print_node_list(fence_delimiter_bottom(n), "bottom", threshold, max);
}

static void tex_aux_display_fraction_noad(halfword n, int threshold, int max) /* todo: more fields */
{
    halfword leftdelimiter = tex_aux_valid_delimiter(fraction_left_delimiter(n));
    halfword rightdelimiter = tex_aux_valid_delimiter(fraction_right_delimiter(n));
    tex_print_str(", thickness ");
    if (fraction_rule_thickness(n) == preset_rule_thickness) {
        tex_print_str("default");
    } else {
        tex_print_dimension(fraction_rule_thickness(n), pt_unit);
    }
    if (leftdelimiter) {
        tex_print_str(", leftdelimiter ");
        tex_aux_print_delimiter(leftdelimiter);
    }
    if (rightdelimiter) {
        tex_print_str(", rightdelimiter ");
        tex_aux_print_delimiter(rightdelimiter);
    }
    if (noad_source(n) != 0) {
        tex_print_str(", source ");
        tex_print_int(noad_source(n));
    }
    if (noad_options(n)) {
        tex_print_str(", options ");
        tex_print_qhex(noad_options(n));
    }
    tex_print_node_list(fraction_numerator(n), "numerator", threshold, max);
    tex_print_node_list(fraction_denominator(n), "denominator", threshold, max);
}

/*tex

    The routines that \TEX\ uses to create mlists are similar to those we have just seen for the
    generation of hlists and vlists. But it is necessary to make \quote {noads} as well as nodes,
    so the reader should review the discussion of math mode data structures before trying to make
    sense out of the following program.

    Here is a little routine that needs to be done whenever a subformula is about to be processed.
    The parameter is a code like |math_group|.

*/

static void tex_aux_new_save_level_math(quarterword group)
{
    halfword direction = math_direction_par;
    tex_set_saved_record(saved_math_item_direction, text_direction_save_type, 0, lmt_dir_state.text_dir_ptr);
    lmt_save_state.save_stack_data.ptr += saved_math_n_of_items;
    lmt_dir_state.text_dir_ptr = tex_new_dir(normal_dir_subtype, direction);
    tex_new_save_level(group);
    update_tex_par_direction(direction);
    update_tex_text_direction(direction);
}

static void tex_aux_push_math(quarterword group, int style)
{
    if (math_direction_par != text_direction_par) {
        cur_list.math_dir = 1;
    }
    cur_list.math_begin = math_begin_class_par;
    cur_list.math_end = math_end_class_par;
    cur_list.math_main_style = style;
    tex_push_nest();
    cur_list.mode = inline_mmode;
    cur_list.incomplete_noad = null;
    cur_list.math_style = style;
    tex_aux_new_save_level_math(group);
    update_tex_math_left_class(unset_noad_class);
    update_tex_math_right_class(unset_noad_class);
}

static void tex_aux_enter_inline_math(int style)
{
    tex_aux_push_math(math_inline_group, style);
    update_tex_family(0, unused_math_family);
    if (every_math_par) {
        tex_begin_token_list(every_math_par, every_math_text);
    }
}

static void tex_aux_enter_display_math(halfword cmd);

/*tex

    We get into math mode from horizontal mode when a |$| (i.e., a |math_shift| character) is
    scanned. We must check to see whether this |$| is immediately followed by another, in case
    display math mode is called for.

*/

void tex_run_math_initialize(void)
{
    switch(cur_cmd) {
        case math_shift_cmd:
            /*tex |get_x_token| would fail on |\ifmmode|! */
            lmt_nest_state.math_mode = 1;
            tex_get_token();
            lmt_nest_state.math_mode = 0;
            if (cur_cmd == math_shift_cmd && cur_list.mode > nomode) {
                tex_aux_enter_display_math(math_shift_cmd);
            } else {
                tex_back_input(cur_tok);
                tex_aux_enter_inline_math(text_style);
            }
            break;
        case math_shift_cs_cmd:
            if (cur_chr == begin_math_mode_code) {
                tex_aux_enter_inline_math(tex_scan_math_style_identifier(0, 0));
            } else if (cur_chr == begin_display_math_code && cur_list.mode > nomode) {
                tex_aux_enter_display_math(begin_display_math_code);
            } else if (cur_chr == begin_inline_math_code) {
                tex_aux_enter_inline_math(text_style);
            } else {
                tex_you_cant_error("math shift 1");
            }
            break;
        default:
            tex_you_cant_error("math shift 2");
            break;
    }
}

/*tex

    We get into ordinary math mode from display math mode when |\eqno| or |\leqno| appears. In such
    cases |cur_chr| will be 0 or~1, respectively; the value of |cur_chr| is placed onto |save_stack|
    for safe keeping. When \TEX\ is in display math mode, |cur_group = math_shift_group|, so it is
    not necessary for the |start_eq_no| procedure to test for this condition.

*/

void tex_run_math_equation_number(void) {
    if (cur_group == math_display_group) {
        tex_set_saved_record(saved_equation_number_item_location, equation_number_location_save_type, 0, cur_chr);
        lmt_save_state.save_stack_data.ptr += saved_equation_number_n_of_items;
        tex_aux_enter_inline_math(text_style);
    } else {
        tex_off_save();
    }
}

/*tex

    Subformulas of math formulas cause a new level of math mode to be entered, on the semantic nest
    as well as the save stack. These subformulas arise in several ways: (1)~A left brace by itself
    indicates the beginning of a subformula that will be put into a box, thereby freezing its glue
    and preventing line breaks. (2)~A subscript or superscript is treated as a subformula if it is
    not a single character; the same applies to the nucleus of things like |\underline|. (3)~The
    |\left| primitive initiates a subformula that will be terminated by a matching |\right|. The
    group codes placed on |save_stack| in these three cases are |math_group|, |math_group|, and
    |math_left_group|, respectively.

    Here is the code that handles case (1); the other cases are not quite as trivial, so we shall
    consider them later.

*/

void tex_run_math_left_brace(void)
{
    if (math_grouping_mode_par) {
        /*tex This is an experiment. Some tracing has to be adapted probably. */
        tex_new_save_level(math_simple_group);
        update_tex_internal_math_style(cur_mode == mmode ? cur_list.math_style : -1);
        update_tex_internal_math_scale(cur_mode == mmode ? cur_list.math_scale : -1);
    } else {
        halfword q = tex_new_node(math_char_node, 0);
        halfword n = tex_new_node(simple_noad, ordinary_noad_subtype);
        tex_tail_append(n);
        noad_nucleus(n) = q;
        tex_back_input(cur_tok);
        tex_aux_scan_math(q, cur_list.math_style, 0, 0, 0, 0, unset_noad_class, unset_noad_class);
    }
}

/*tex

    If the inline directions of |\pardir| and |\mathdir| are opposite, then this function will
    return true. Discovering that fact is somewhat odd because it needs traversal of the
    |save_stack|. The occurance of displayed equations is weird enough that this is probably still
    better than having yet another field in the |input_stack| structures.

    None of this makes much sense if the inline direction of either one of |\pardir| or |\mathdir|
    is vertical, but in that case the current math machinery is ill suited anyway so I do not
    bother to test that. We now just return the direction.

*/

static int tex_aux_pre_math_par_direction(void)
{
    return tex_located_save_value(internal_int_location(par_direction_code));
}

/*tex

    When we enter display math mode, we need to call |line_break| to process the partial paragraph
    that has just been interrupted by the display. Then we can set the proper values of
    |display_width| and |display_indent| and |pre_display_size|.

*/

static void tex_aux_enter_display_math(halfword cmd)
{
    if (math_display_mode_par) {
        tex_aux_push_math(math_inline_group, display_style);
        cur_list.math_mode = cmd; 
        cur_list.mode = inline_mmode; /* new */
        update_tex_family(0, unused_math_family);
        if (every_display_par) {
            tex_begin_token_list(every_display_par, every_display_text);
        }
    } else { 
        /*tex new or partial |pre_display_size| */
        scaled size;
        /*tex new |display_width| */
        scaled width;
        /*tex new |display_indent| */
        scaled indent;
        /*tex
            Deal with |\noindent$$| or |$${ }$$| or the 2nd of |$${ }$$| |$${ }$$|.
        */
        if (cur_list.head == cur_list.tail || (node_next(cur_list.head) == cur_list.tail && node_type(cur_list.tail) == par_node && ! node_next(cur_list.tail))) {
            if (node_next(cur_list.head) == cur_list.tail) {
                /*tex
                    |resume_after_display| inserts a |par_node|, but if there is another display
                    immediately following, we have to get rid of that node.
                */
                tex_flush_node(cur_list.tail);
             /* cur_list.tail = cur_list.head; */ /* probably needed */
            }
            tex_pop_nest();
            size = - max_dimen;
        } else {
            tex_line_break(1, math_display_group);
         // size = tex_actual_box_width(lmt_linebreak_state.just_box, tex_x_over_n(tex_get_font_em_width(cur_font_par), scaling_factor) * math_pre_display_gap_factor_par);
            size = tex_actual_box_width(lmt_linebreak_state.just_box, scaledround((tex_get_font_em_width(cur_font_par) / scaling_factor_double) * math_pre_display_gap_factor_par));
        }
        /*tex
            Now we are in vertical mode, working on the list that will contain the display. A displayed
            equation is considered to be three lines long, so we calculate the length and offset of line
            number |prev_graf + 2|.
        */
        if (par_shape_par) {
            /*tex scope of paragraph shape specification */
            int n = tex_get_specification_count(par_shape_par);
            if (n > 0) {
                if (cur_list.prev_graf + 2 < n) {
                    n = cur_list.prev_graf + 2;
                }
                indent = tex_get_specification_indent(par_shape_par, n) ;
                width = tex_get_specification_width(par_shape_par, n);
                indent = swap_parshape_indent(pre_display_direction_par, indent, width);
            } else {
                width = hsize_par;
                indent = 0;
            }
        } else if ((hang_indent_par != 0) && (((hang_after_par >= 0) && (cur_list.prev_graf + 2 > hang_after_par)) || (cur_list.prev_graf + 1 < -hang_after_par))) {
            halfword hangindent = swap_hang_indent(pre_display_direction_par, hang_indent_par);
            width = hsize_par - abs(hangindent);
            indent = hangindent > 0 ? hangindent : 0;
        } else {
            width = hsize_par;
            indent = 0;
        }
        tex_aux_push_math(math_display_group, display_style);
        cur_list.mode = mmode;
        update_tex_family(0, unused_math_family);
        update_tex_pre_display_size(size);
        update_tex_display_width(width);
        update_tex_display_indent(indent);
        update_tex_pre_display_direction(tex_aux_pre_math_par_direction());
        if (every_display_par) {
            tex_begin_token_list(every_display_par, every_display_text);
        }
        if (lmt_nest_state.nest_data.ptr == 1) {
            if (! lmt_page_builder_state.output_active) {
                lmt_page_filter_callback(before_display_page_context, 0);
            }
            tex_build_page();
        }
    }
}

/*tex

    The next routine parses all variations of a delimiter code. The |extcode| tells what syntax form
    to use (\TEX\ or \LUATEX) , the |doclass| tells whether or not read a math class also (for
    |\delimiter| c.s.). The class is passed on for conversion to |\mathchar|.

*/

static delcodeval tex_aux_scan_extdef_del_code(int extcode, int doclass)
{
    delcodeval d = tex_no_del_code();
    switch (extcode) {
        case tex_mathcode:
            /*tex This is the easiest: |\delcode|,*/
            {
                halfword v = tex_scan_int(0, NULL);
                /*tex |MFCCFCC| or |FCCFCC| */
                if (doclass) {
                    d.small.class_value = (short) (v / 0x1000000);
                    v = (v & 0xFFFFFF);
                }
                if (v > 0xFFFFFF) {
                    tex_handle_error(
                        normal_error_type,
                        "Invalid delimiter code",
                        "I'm going to use 0 instead of that illegal code value."
                    );
                    v = 0;
                }
                d.small.family_value = (short) (v / 0x100000);
                d.small.character_value = (v % 0x100000) / 0x1000;
                d.large.family_value = (short) ((v & 0xFFF) / 0x100);
                d.large.character_value = (v % 0x100);
                /* */
                d.small.character_value = math_character_part(d.small.character_value);
                d.large.character_value = math_character_part(d.large.character_value);
            }
            break;
        case umath_mathcode:
            /*tex |\Udelcode|: |<0-7><0-0xFF><0-0x10FFFF>| or |<0-0xFF><0-0x10FFFF>| */
            {
                if (doclass) {
                    d.small.class_value = (short) tex_scan_math_class_number(0);
                }
                d.small.family_value = (short) tex_scan_math_family_number();
                d.small.character_value = tex_scan_math_char_number();
                if (d.small.family_value < 0 || d.small.family_value > max_math_family_index) {
                    tex_handle_error(
                        normal_error_type,
                        "Invalid delimiter family",
                        "I'm going to use family 0 instead."
                    );
                    d.small.family_value = 0;
                    d.small.character_value = 0;
                }
            }
            break;
        default:
            /*tex Something's gone wrong! */
            tex_confusion("unknown extcode, case 1");
            break;
    }
    d.large.class_value = d.small.class_value;
    return d;
}

void tex_scan_extdef_del_code(int level, int extcode)
{
    delcodeval d;
    int chr = tex_scan_char_number(0);
    tex_scan_optional_equals();
    d = tex_aux_scan_extdef_del_code(extcode, 0);
    tex_set_del_code(chr, d, (quarterword) level);
}

mathdictval tex_scan_mathdict(void)
{
    mathdictval d = tex_no_dict_code(); /* use this one directly */
    d.properties = (unsigned short) tex_scan_math_properties_number();
    d.group = (unsigned short) tex_scan_math_group_number();
    d.index = (unsigned int) tex_scan_math_index_number();
    return d;
}

mathcodeval tex_scan_mathchar(int extcode)
{
    mathcodeval d = tex_no_math_code(); /* use this one directly */
    switch (extcode) {
        case tex_mathcode:
            /*tex |"<4bits><4bits><8bits>| */
            {
                halfword v = tex_scan_int(0, NULL);
                if (v >= 0) {
                    if (v > 0xFFFF) {
                        v = 0xFFFF;
                    }
                    d.class_value = (short) math_old_class_part(v);
                    d.family_value = (short) math_old_family_part(v);
                    d.character_value = math_old_character_part(v);
                }
            }
            break;
        case umath_mathcode:
            /*tex |"<6bits>"<6bits>"<20bits>| */
            {
                d.class_value = (short) tex_scan_math_class_number(0);
                d.family_value = (short) tex_scan_math_family_number();
                d.character_value = tex_scan_math_char_number();
            }
            break;
        /*
        case umathnum_mathcode:
            // |"<6bits><6bits><20bits>|: the largest numeric value is $2^32-1$, but the top of bit 21 can't
            // be used as it contains invalid USV's. Note: |scan_int| won't accept families 128-255
            // because these use bit 32.
            {
                halfword v = tex_scan_int(0, NULL);
                d.class_value = (short) math_class_part(v);
                d.family_value = (short) math_family_part(v);
                d.character_value = math_character_part(v);
            }
            break;
        */
        default:
            /*tex Something's gone wrong. */
            tex_confusion("unknown extcode, case 2");
            break;
    }
    if (d.class_value < 0 || d.character_value > max_math_character_code || d.class_value > max_math_class_code || d.family_value > max_math_family_index) {
        tex_handle_error(
            normal_error_type,
            "Invalid math code",
            "I'm going to use 0 instead of that illegal code value."
        );
        d.class_value = 0;
        d.family_value = 0;
        d.character_value = 0;
    }
    return d;
}

halfword tex_new_math_spec(mathcodeval m, quarterword code)
{
    halfword s = tex_new_node(math_spec_node, code);
    math_spec_class(s) = (singleword) m.class_value;
    math_spec_family(s) = (singleword) m.family_value;
    math_spec_character(s) = m.character_value;
    return s;
}

halfword tex_new_math_dict_spec(mathdictval d, mathcodeval m, quarterword code)
{
    halfword s = tex_new_node(math_spec_node, code);
    math_spec_class(s) = (singleword) m.class_value;
    math_spec_family(s) = (singleword) m.family_value;
    math_spec_character(s) = m.character_value;
    math_spec_properties(s) = (quarterword) d.properties;
    math_spec_group(s) = (quarterword) d.group;
    math_spec_index(s) = d.index;
    return s;
}

mathcodeval tex_get_math_spec(halfword s)
{
    mathcodeval m = tex_no_math_code();
    if (s) {
        m.class_value = math_spec_class(s);
        m.family_value = math_spec_family(s);
        m.character_value = math_spec_character(s);
    }
    return m;
}

mathdictval tex_get_math_dict(halfword s)
{
    mathdictval d = tex_no_dict_code();
    if (s) {
        d.properties = math_spec_properties(s);
        d.group = math_spec_group(s);
        d.index = math_spec_index(s);
    }
    return d;
}

halfword tex_scan_math_spec(int optional_equal)
{
    mathcodeval m;
    if (optional_equal) {
        tex_scan_optional_equals();
    }
    m = tex_scan_mathchar(umath_mathcode);
    return tex_new_math_spec(m, mathspec_mathcode);
}

void tex_scan_extdef_math_code(int level, int extcode)
{
    mathcodeval d;
    int chr = tex_scan_char_number(0);
    tex_scan_optional_equals();
    d = tex_scan_mathchar(extcode);
    tex_set_math_code(chr, d, (quarterword) level);
}

/*tex This reads in a delcode when actually a mathcode is needed. */

mathcodeval tex_scan_delimiter_as_mathchar(int extcode)
{
    delcodeval dval = tex_aux_scan_extdef_del_code(extcode, 1);
    return dval.small;
}

/*tex

    Recall that the |nucleus|, |subscr|, and |supscr| fields in a noad are broken down into subfields
    called |type| and either |math_list| or |(math_fam, math_character)|. The job of |scan_math| is
    to figure out what to place in one of these principal fields; it looks at the subformula that
    comes next in the input, and places an encoding of that subformula into a given word of |mem|.

    already prepared: every [component, degree, radical, over, under, accent, prime, subscript,
    superscript]

    toks      : every_subscript_par
    toks_text : every_subscipt_text or every_math_text (for tracing)

*/

/*tex 
    For some reason |$\char44$| gives an undefined |$| when we made that character active in math. 
*/

static void tex_aux_report_active(int where, const char *what, int code, int character) 
{
    tex_begin_diagnostic();
    tex_print_format("[active: location %i, %s, code %i, char %i]",where, what, code, character);
    tex_end_diagnostic();
}

static void tex_aux_append_math_char(mathcodeval mval, mathdictval dval, int automatic);

int tex_check_active_math_char(int character)
{
    halfword code = tex_get_am_code(character);
    if (code) {
        switch (code) {
            case alignment_tab_cmd:              
            case superscript_cmd:                
            case subscript_cmd:                  
            case letter_cmd:                     
            case other_char_cmd:
            case active_char_cmd:
                cur_cmd = code;
                cur_chr = character;
                cur_tok = token_val(cur_cmd, cur_chr);
                if (tracing_commands_par >= 4) {
                    switch (code) {
                        case alignment_tab_cmd:              
                        case superscript_cmd:                
                        case subscript_cmd:                  
                            tex_aux_report_active(4, "control", code, character);
                            break;
                        case letter_cmd:                     
                        case other_char_cmd:
                            tex_aux_report_active(4, "inject", code, character);
                            break;
                        case active_char_cmd:
                            tex_aux_report_active(4, "active", code, character);
                            break;
                    }
                }
                return 1;
            default: 
                if (tracing_commands_par >= 4) {
                    tex_aux_report_active(4, "ignore", code, character);
                }
                return 1;
        }
    } else { 
        return 0;
    }
}

int tex_pass_active_math_char(int character)
{
    halfword code = tex_get_am_code(character);
    if (code) {
        return 1;
    } else { 
        return 0;
    }
}

static int tex_aux_scan_active_math_char(mathcodeval *mval, int where)
{
    halfword character = mval->character_value;
    halfword code = tex_get_am_code(character);
    if (code) {
        switch (code) {
            case alignment_tab_cmd:              
            case superscript_cmd:                
            case subscript_cmd:                  
                cur_cmd = code;
                cur_chr = character;
                cur_tok = token_val(cur_cmd, cur_chr);
                tex_back_input(cur_tok);
                if (tracing_commands_par >= 4) {
                    tex_aux_report_active(where, "control", code, character);
                }
                return 1;
            case letter_cmd:                     
            case other_char_cmd:
                cur_cmd = code;
                cur_chr = character;
                cur_tok = token_val(cur_cmd, cur_chr);
                if (tracing_commands_par >= 4) {
                    tex_aux_report_active(where, "inject", code, character);
                }
                return 0;
            case active_char_cmd:
                /*tex 
                    We reset the code so that we don't get a loop, which means that the macro that 
                    gets invoked has to set the amcode again if needed. 
                */
                tex_set_am_code(character, other_char_cmd, cur_level);
                cur_cs = tex_active_to_cs(cur_chr, 1);
                cur_cmd = eq_type(cur_cs);
                cur_chr = eq_value(cur_cs);
                tex_x_token();
                tex_back_input(cur_tok);
                if (tracing_commands_par >= 4) {
                    tex_aux_report_active(where, "active", code, character);
                }
                return 1;
            default: 
                if (tracing_commands_par >= 4) {
                    tex_aux_report_active(where, "ignore", code, character);
                }
                return 1;
        }
    } else if (mval->class_value == active_math_class_value) {
        /*tex We might eventually drop this feature in favor of the amcode. */
        cur_cs = tex_active_to_cs(cur_chr, 1);
        cur_cmd = eq_type(cur_cs);
        cur_chr = eq_value(cur_cs);
        tex_x_token();
        tex_back_input(cur_tok);
        if (tracing_commands_par >= 4) {
            tex_aux_report_active(where, "active", code, character);
        }
        return 1;
    } else { 
     // if (tracing_commands_par >= 4) {
     //     tex_aux_report_active(where, "keep", code, mval->character_value);
     // }
        return 0;
    }
}

static int tex_aux_scan_math(halfword target, halfword style, int usetextfont, halfword toks, halfword toks_text, int nocomponent, halfword cls, halfword all)
{
    mathcodeval mval = tex_no_math_code();
    mathdictval dval = tex_no_dict_code();
    lmt_math_state.last_atom = cls;
  RESTART:
    do {
        tex_get_x_token();
    } while (cur_cmd == spacer_cmd || cur_cmd == relax_cmd);
//  RESWITCH:
    switch (cur_cmd) {
        case char_number_cmd:
            /* The |\glyph| variant is accepted but no keywords here. */
            cur_chr = tex_scan_char_number(0);
            // fall through 
        case letter_cmd:
        case other_char_cmd:
        case char_given_cmd:
            mval = tex_get_math_code(cur_chr);
            if (tex_aux_scan_active_math_char(&mval, 1)) { 
                goto RESTART; /* rescan pushed back token */
            } else {
                dval = tex_fake_math_dict(mval.character_value);
                break;
            }
    //  case char_number_cmd:
    //      /* The |\glyph| variant is accepted but no keywords here. */
    //      cur_chr = tex_scan_char_number();
    //      cur_cmd = char_given_cmd;
    //      goto RESWITCH;
        case math_char_number_cmd:
            switch (cur_chr) {
                case math_char_number_code:
                    mval = tex_scan_mathchar(tex_mathcode);
                    break;
                case math_xchar_number_code:
                    mval = tex_scan_mathchar(umath_mathcode);
                    break;
                default:
                    tex_confusion("scan math char, case 1");
                    break;
            }
            dval = tex_fake_math_dict(mval.character_value);
            break;
        case mathspec_cmd:
            mval = tex_get_math_spec(cur_chr);
            dval = tex_get_math_dict(cur_chr);
            break;
        case delimiter_number_cmd:
            switch (cur_chr) {
                case math_delimiter_code:
                    mval = tex_scan_delimiter_as_mathchar(tex_mathcode);
                    break;
                case math_udelimiter_code:
                    mval = tex_scan_delimiter_as_mathchar(umath_mathcode);
                    break;
                default:
                    tex_confusion("scan math char, case 2");
                    break;
            }
            break;
		case math_component_cmd:
			if (nocomponent) {
                goto DEFAULT;
            } else {
			    tex_set_saved_record(saved_math_group_item_pointer, math_pointer_save_type, 0, target);
                tex_set_saved_record(saved_math_group_all_class, math_class_save_type, 0, unset_noad_class);
			    lmt_save_state.save_stack_data.ptr += saved_math_group_n_of_items;
			    tex_aux_push_math(math_component_group, style);
                if (usetextfont) {
                    tex_set_math_text_font(style, usetextfont);
                }
    		    tex_aux_math_math_component(cur_list.tail, 0);
                tex_finish_math_group();
    			return 1;
		    }
        case left_brace_cmd:
            goto SCAN_SUBFORMULA;
        default:
            /*tex
                The pointer |p| is placed on |save_stack| while a complex subformula is being
                scanned.
            */
          DEFAULT:
            tex_back_input(cur_tok);
            tex_scan_left_brace();
          SCAN_SUBFORMULA:
            tex_set_saved_record(saved_math_group_item_pointer, math_pointer_save_type, 0, target);
            tex_set_saved_record(saved_math_group_all_class, math_class_save_type, 0, all);
            lmt_save_state.save_stack_data.ptr += saved_math_group_n_of_items;
            tex_aux_push_math(math_group, style);
            toks = every_math_atom_par;
            toks_text = every_math_atom_text; 
            if (toks) {
                tex_begin_token_list(toks, (quarterword) toks_text);
            }
            if (usetextfont) {
                tex_set_math_text_font(style, usetextfont);
            }
            return 1;
    }
    node_type(target) = math_char_node;
    if (glyph_options_par & glyph_option_no_italic_correction) {
        math_kernel_node_set_option(target, math_kernel_no_italic_correction);
    }
    if (glyph_options_par & glyph_option_no_left_kern) {
        math_kernel_node_set_option(target, math_kernel_no_left_pair_kern);
    }
    if (glyph_options_par & glyph_option_no_right_kern) {
        math_kernel_node_set_option(target, math_kernel_no_right_pair_kern);
    }
    tex_aux_set_math_char(target, &mval, &dval);
    return 0;
}

/*tex

    The |append_math_char| procedure creates a new noad appropriate to a given math code, and
    appends it to the current mlist. However, if the math code is sufficiently large, the |cur_chr|
    is treated as an active character and nothing is appended.

*/

static void tex_aux_append_math_accent(mathcodeval mval, mathdictval dval)
{
    halfword accent = tex_new_node(accent_noad, bothflexible_accent_subtype);
    quarterword subtype = ordinary_noad_subtype;
    tex_tail_append(accent);
    if (! (mval.character_value == 0 && mval.family_value == 0)) {
        halfword q = tex_new_node(math_char_node, 0);
        subtype = tex_aux_set_math_char(q, &mval, &dval);
        accent_top_character(accent) = q;
    }
    {
        halfword q = tex_new_node(math_char_node, subtype);
        noad_nucleus(accent) = q;
        tex_aux_scan_math(q, tex_math_style_variant(cur_list.math_style, math_parameter_accent_variant), 0, 0, 0, 0, unset_noad_class, unset_noad_class);
    }
}

/*tex 
    Fences are actually constructs and middle sort of interferes here: we keep a sort of flat fence
    sequence so middle ends a group and opens a new one. 

*/

static void tex_aux_append_math_fence(halfword fence, quarterword mathclass)
{
    switch (mathclass) {
        case open_noad_subtype:
            {
                tex_aux_push_math(math_fence_group, cur_list.math_style);
                node_subtype(fence) = left_fence_side;
                node_next(cur_list.head) = fence;
                cur_list.tail = fence;
                cur_list.delimiter = fence;
            }
            break;
        case close_noad_subtype:
            {
                halfword q = tex_aux_finish_math_list(fence);
                halfword n = tex_new_node(simple_noad, fenced_noad_subtype);
                halfword l = tex_new_node(sub_mlist_node, 0);
                tex_aux_unsave_math();
                tex_tail_append(n);
                node_subtype(fence) = right_fence_side;
                noad_nucleus(n) = l;
                noad_options(n) |= noad_option_unpack_list;
                kernel_math_list(noad_nucleus(n)) = q;
            }
            break;
        case middle_noad_subtype:
            { 
                halfword q = tex_aux_finish_math_list(fence);
                tex_aux_unsave_math();
                tex_aux_push_math(math_fence_group, cur_list.math_style);
                node_subtype(fence) = middle_fence_side;
                node_next(cur_list.head) = q;
                cur_list.tail = fence;
                cur_list.delimiter = fence;
            }
            break;
    }
}

static void tex_aux_append_math_fence_val(mathcodeval mval, mathdictval dval, quarterword mathclass)
{
    halfword fence = tex_new_node(fence_noad, middle_fence_side);
    halfword delimiter = tex_new_node(delimiter_node, mval.class_value);
    (void) dval; /* maybe todo */
    fence_delimiter_list(fence) = delimiter;
    delimiter_small_family(delimiter) = mval.family_value;
    delimiter_small_character(delimiter) = mval.character_value;
    delimiter_large_family(delimiter) = mval.family_value;
    delimiter_large_character(delimiter) = mval.character_value;
    set_noad_classes(fence, mval.class_value);
    /* todo : share the next three with the regular fences */
    noad_options(fence) |= noad_option_no_check;
    if (mathclass == middle_noad_subtype && cur_group != math_fence_group) { 
        tex_aux_append_math_fence_val(tex_no_math_code(), tex_no_dict_code(), open_noad_subtype);
    }
    tex_aux_append_math_fence(fence, mathclass);
}

static void tex_aux_append_math_char(mathcodeval mval, mathdictval dval, int automatic)
{
    if (tex_aux_scan_active_math_char(&mval, 2)) { 
        return; /* rescan pushed back token */
    } else { 
        if (automatic && tex_math_has_class_option(mval.class_value, auto_inject_class_option)) {
            switch (mval.class_value) { 
                case accent_noad_subtype:
                    tex_aux_append_math_accent(mval, dval);
                    return;
                case open_noad_subtype:
                case close_noad_subtype:
                case middle_noad_subtype:
                    tex_aux_append_math_fence_val(mval, dval, mval.class_value);
                    return;
            }
        } 
        {
            halfword p = tex_new_node(simple_noad, ordinary_noad_subtype);
            halfword q = tex_new_node(math_char_node, 0);
            noad_nucleus(p) = q;
            if (glyph_options_par & glyph_option_no_italic_correction) {
                math_kernel_node_set_option(q, math_kernel_no_italic_correction);
            }
            node_subtype(p) = tex_aux_set_math_char(q, &mval, &dval);
            tex_math_set_scripts_options(p);
            tex_tail_append(p);
        }
    }
}

/*tex

    The |append_math_char_in_text| procedure creates a new node representing a math char in text
    code, and appends it to the current list. However, if the math code is sufficiently large, the
    |cur_chr| is treated as an active character and nothing is appended.

*/

static void tex_aux_append_math_char_in_text(mathcodeval mval, mathdictval dval)
{
    (void) dval;
    if (tex_aux_scan_active_math_char(&mval, 3)) {
        return; /* rescan pushed back token */
    } else { 
        halfword p = tex_new_char_node(glyph_character_subtype, tex_fam_fnt(mval.family_value, text_size), mval.character_value, 1); /* todo: data */
        tex_tail_append(p);
    }
}

void tex_run_math_letter(void) 
{
    tex_aux_append_math_char(tex_get_math_code(cur_chr), tex_fake_math_dict(cur_chr), 1);
}

void tex_run_math_char_number(void) {
    /*tex 
        Both |\char| and |\glyph| get the same treatment. Scanning can change |cur_chr| so we do 
        that first. We no longer check for active here! 
    */
    mathcodeval mval = tex_no_math_code();
    mathdictval dval = tex_no_dict_code();
    cur_chr = tex_scan_char_number(0); 
    mval.character_value = cur_chr;
    mval.family_value = (short) cur_fam_par;
 // tex_aux_append_math_char(tex_get_math_code(cur_chr), tex_fake_math_dict(cur_chr));
    tex_aux_append_math_char(mval, dval, 1);
}

void tex_run_math_math_spec(void)
{
    tex_aux_append_math_char(tex_get_math_spec(cur_chr), tex_get_math_dict(cur_chr), 1);
}

void tex_run_text_math_spec(void)
{
    tex_aux_append_math_char_in_text(tex_get_math_spec(cur_chr), tex_get_math_dict(cur_chr));
}

int tex_scan_math_cmd_val(mathcodeval *mval, mathdictval *dval)
{
    do {
        tex_get_x_token();
    } while (cur_cmd == spacer_cmd);
    switch (cur_cmd) {
        case mathspec_cmd:
            *mval = tex_get_math_spec(cur_chr);
            break;
        case math_char_number_cmd:
            switch (cur_chr) {
                case math_char_number_code:
                    *mval = tex_scan_mathchar(tex_mathcode);
                    break;
                case math_xchar_number_code:
                    *mval = tex_scan_mathchar(umath_mathcode);
                    break;
                case math_dchar_number_code:
                    *dval = tex_scan_mathdict();
                    *mval = tex_scan_mathchar(umath_mathcode);
                    break;
                default:
                    /* no message yet */
                    return 0;
            }
            break;
        case delimiter_number_cmd:
            switch (cur_chr) {
                case math_delimiter_code:
                    *mval = tex_scan_delimiter_as_mathchar(tex_mathcode);
                    break;
                case math_udelimiter_code:
                    *mval = tex_scan_delimiter_as_mathchar(umath_mathcode);
                    break;
                default:
                    /* no message yet */
                    return 0;
            }
            break;
        /*tex 
            This is/was an experiment but could work out ambigiuous in some cases so when I bring it 
            back it will be under more strict control. So, for instance a register would make us 
            enter the default branch but a direct number the other case. In the meantiem we no longer 
            use the direct char approach (for delimiters mostly) so we can comment it. 
        */
      // case letter_cmd: 
      // case other_char_cmd: 
      //     mval->character_value = cur_chr; 
      //     break; 
        default:
            /*tex
                We could do a fast |tex_scan_something_internal| here but this branch is not that 
                critical. 
            */     
            {
                halfword n = 0;
                tex_back_input(cur_tok);
                n = tex_scan_int(0, NULL);
                *mval = tex_mathchar_from_integer(n, umath_mathcode);
            }
            break;
    }
    return 1;
}

int tex_scan_math_code_val(halfword code, mathcodeval *mval, mathdictval *dval)
{
    switch (code) {
        case math_char_number_code:
            *mval = tex_scan_mathchar(tex_mathcode);
            break;
        case math_xchar_number_code:
            *mval = tex_scan_mathchar(umath_mathcode);
            break;
        case math_dchar_number_code:
            *dval = tex_scan_mathdict();
            *mval = tex_scan_mathchar(umath_mathcode);
            break;
        case math_class_number_code:
            {
                halfword family = cur_fam_par;
                halfword mathclass  = tex_scan_int(0, NULL);
                tex_scan_math_cmd_val(mval, dval);
                mval->class_value = (short) mathclass;
                mval->family_value = (short) family;
            }
            break;
        default:
            /* no message yet */
            tex_back_input(cur_tok);
            return 0;
    }
    return 1;
}

void tex_run_text_math_char_number(void) {
    mathcodeval mval = tex_no_math_code();
    mathdictval dval = tex_no_dict_code();
    if (tex_scan_math_code_val(cur_chr, &mval, &dval)) {
        tex_aux_append_math_char_in_text(mval, dval);
    }
}

void tex_run_math_math_char_number(void) {
    mathcodeval mval = tex_no_math_code();
    mathdictval dval = tex_no_dict_code();
    if (tex_scan_math_code_val(cur_chr, &mval, &dval)) {
        tex_aux_append_math_char(mval, dval, 1);
    }
}

void tex_run_math_delimiter_number(void) {
    switch (cur_chr) {
        case math_delimiter_code:
            tex_aux_append_math_char(tex_scan_delimiter_as_mathchar(tex_mathcode), tex_no_dict_code(), 0);
            break;
        case math_udelimiter_code:
            tex_aux_append_math_char(tex_scan_delimiter_as_mathchar(umath_mathcode), tex_no_dict_code(), 0);
            break;
    }
}

/*tex 
    In original \TEX\ the subtype overlaps the class. Here we are more strict: a subtype is the
    main class as in original \TEX\ but we also have overloads: main, left and right. The subtype 
    drives the rendering, the others the spacing etc. 
*/

static void tex_aux_math_math_component(halfword target, int append)
{
    quarterword subtype = unset_noad_class;
    quarterword allclass = unset_noad_class;
    halfword style = cur_list.math_style;
    int usetextfont = math_atom_no_font_option;
    reset_noad_classes(target);
    switch (cur_chr) {
        case math_component_ordinary_code:
            subtype = ordinary_noad_subtype;
            break;
        case math_component_operator_code:
            subtype = operator_noad_subtype;
            break;
        case math_component_binary_code:
            subtype = binary_noad_subtype;
            break;
        case math_component_relation_code:
            subtype = relation_noad_subtype;
            break;
        case math_component_open_code:
            subtype = open_noad_subtype;
            break;
        case math_component_close_code:
            subtype = close_noad_subtype;
            break;
        case math_component_punctuation_code:
            subtype = punctuation_noad_subtype;
            break;
        case math_component_variable_code:
            subtype = variable_noad_subtype;
            break;
        case math_component_inner_code:
            subtype = inner_noad_subtype;
            break;
        case math_component_under_code:
            subtype = under_noad_subtype;
            style = tex_math_style_variant(style, math_parameter_under_line_variant);
            break;
        case math_component_over_code:
            subtype = over_noad_subtype;
            style = tex_math_style_variant(style, math_parameter_over_line_variant);
            break;
        case math_component_fraction_code:
            subtype = fraction_noad_subtype;
            break;
        case math_component_radical_code:
            subtype = radical_noad_subtype;
            break;
        case math_component_middle_code:
            subtype = middle_noad_subtype;
            break;
        case math_component_accent_code:
            subtype = accent_noad_subtype;
            break;
        case math_component_fenced_code:
            subtype = fenced_noad_subtype;
            break;
        case math_component_ghost_code:
            subtype = ghost_noad_subtype;
            break;
        case math_component_atom_code:
            {
                halfword attrlist = null;
                while (1) {
                    switch (tex_scan_character("custnmaolprvCUSTNMAOLPRV", 0, 1, 0)) {
                        case 'a': case 'A':
                            switch (tex_scan_character("ltLT", 0, 0, 0)) {
                                case 't': case 'T':
                                    if (tex_scan_mandate_keyword("attr", 2)) {
                                        attrlist = tex_scan_attribute(attrlist);
                                    }
                                    break;
                                case 'l': case 'L':
                                    if (tex_scan_mandate_keyword("all", 2)) {
                                        allclass = (quarterword) tex_scan_math_class_number(0);
                                        if (! valid_math_class_code(allclass)) {
                                            allclass = unset_noad_class;
                                        }
                                    }
                                    break;
                                default:
                                    tex_aux_show_keyword_error("attr|all");
                                    goto DONE;
                            }
                            break;
                        case 'l': case 'L':
                            switch (tex_scan_character("ieIE", 0, 0, 0)) {
                                case 'e': case 'E':
                                    if (tex_scan_mandate_keyword("leftclass", 2)) {
                                        halfword c = tex_scan_math_class_number(0);
                                        if (! valid_math_class_code(c)) {
                                            c = ordinary_noad_subtype;
                                        }
                                        set_noad_left_class(target, c);
                                    }
                                    break;
                                case 'i': case 'I':
                                    if (tex_scan_mandate_keyword("limits", 2)) {
                                        noad_options(target) |= noad_option_limits;
                                    }
                                    break;
                                default:
                                    tex_aux_show_keyword_error("leftclass|limits");
                                    goto DONE;
                            }
                            break;
                        case 'r': case 'R':
                            if (tex_scan_mandate_keyword("rightclass", 1)) {
                                halfword c = tex_scan_math_class_number(0);
                                if (! valid_math_class_code(c)) {
                                    c = ordinary_noad_subtype;
                                }
                                set_noad_right_class(target, c);
                            }
                            break;
                        case 'c': case 'C':
                            if (tex_scan_mandate_keyword("class", 1)) {
                                subtype = (quarterword) tex_scan_math_class_number(0);
                                if (! valid_math_class_code(subtype)) {
                                    subtype = ordinary_noad_subtype;
                                }
                                set_noad_main_class(target, subtype);
                            }
                            break;
                        case 'u': case 'U':
                            /*tex A bit over the top, three steps but a push back is still worse. We can scan for 'un'. */
                            if (tex_scan_character("nN", 0, 0, 0)) {
                                switch (tex_scan_character("prPR", 0, 0, 0)) {
                                    case 'p': case 'P':
                                        if (tex_scan_mandate_keyword("unpack", 3)) {
                                            noad_options(target) |= noad_option_unpack_list;
                                        }
                                        break;
                                    case 'r': case 'R':
                                        if (tex_scan_mandate_keyword("unroll", 3)) {
                                            noad_options(target) |= noad_option_unroll_list;
                                        }
                                        break;
                                    default:
                                        tex_aux_show_keyword_error("unpack|unroll");
                                        goto DONE;
                                }
                            }
                            break;
                        case 's': case 'S':
                            switch (tex_scan_character("ioIO", 0, 0, 0)) {
                                case 'i': case 'I':
                                    if (tex_scan_mandate_keyword("single", 2)) {
                                        noad_options(target) |= noad_option_single;
                                    }
                                    break;
                                case 'o': case 'O':
                                    if (tex_scan_mandate_keyword("source", 2)) {
                                        noad_source(target) = tex_scan_int(0, NULL);
                                    }
                                    break;
                                default:
                                    tex_aux_show_keyword_error("single|source");
                                    goto DONE;
                            }
                            break;
                       case 't': case 'T':
                            if (tex_scan_mandate_keyword("textfont", 1)) {
                                usetextfont = math_atom_text_font_option;
                            }
                            break;
                        case 'm': case 'M':
                            if (tex_scan_mandate_keyword("mathfont", 1)) {
                                usetextfont = math_atom_math_font_option;
                            }
                            break;
                        case 'n': case 'N':
                            /*tex A bit over the top, three steps but a push back is still worse. We can scan for 'no'. */
                            if (tex_scan_character("oO", 0, 0, 0)) {
                                switch (tex_scan_character("loLO", 0, 0, 0)) {
                                    case 'l': case 'L':
                                        if (tex_scan_mandate_keyword("nolimits", 3)) {
                                            noad_options(target) |= noad_option_no_limits;
                                        }
                                        break;
                                    case 'o': case 'O':
                                        if (tex_scan_mandate_keyword("nooverflow", 3)) {
                                            noad_options(target) |= noad_option_no_overflow;
                                        }
                                        break;
                                    default:
                                        tex_aux_show_keyword_error("nolimits|nooverflow");
                                        goto DONE;
                                }
                            }
                            break;
                        case 'o': case 'O':
                            /* no names, just numbers, we might also do that with other noads */
                            if (tex_scan_mandate_keyword("options", 1)) {
                                noad_options(target) = tex_scan_int(0, NULL);
                            }
                            break;
                        case 'v': case 'V':
                            if (tex_scan_mandate_keyword("void", 1)) {
                                noad_options(target) |= noad_option_void;
                            }
                            break;
                        case 'p': case 'P':
                            if (tex_scan_mandate_keyword("phantom", 1)) {
                                noad_options(target) |= noad_option_phantom;
                            }
                            break;
                        default:
                            goto DONE;
                    }
                }
              DONE:
                if (attrlist) {
                    tex_attach_attribute_list_attribute(target, attrlist);
                }
                if (subtype == unset_noad_class) {
                    if (get_noad_left_class(target) != unset_noad_class && get_noad_right_class(target) != unset_noad_class) {
                        subtype = ordinary_noad_subtype;
                    } else {
                        /* mandate, maybe we will just force a keyword */
                        subtype = (quarterword) tex_scan_math_class_number(0);
                    }
                }
            }
            break;
    }
    if (! valid_math_class_code(subtype)) {
        subtype = ordinary_noad_subtype;
    }
    /*tex 
        Now we can scan for the content: 
    */
    {
        halfword content = tex_new_node(math_char_node, 0);
        noad_nucleus(target) = content;
        node_subtype(target) = subtype;
        if (append) {
            tex_tail_append(target);
        }
        tex_aux_scan_math(content, style, usetextfont, 0, 0, 0, subtype, allclass);
    }
}

void tex_run_math_math_component(void)
{
    halfword n = tex_new_node(simple_noad, ordinary_noad_subtype);
    tex_math_set_scripts_options(n);
    tex_aux_math_math_component(n, 1);
}

int tex_is_math_disc(halfword n)
{
    return
        n && node_type(n) == hlist_node && box_list(n) && node_type(box_list(n)) == disc_node &&
        disc_class(box_list(n)) != unset_disc_class && ! node_next(box_list(n));
}

halfword tex_math_make_disc(halfword d)
{
    halfword q = tex_new_node(sub_mlist_node, 0);
    halfword n = tex_new_node(simple_noad, (quarterword) disc_class(d));
    kernel_math_list(q) = d;
    noad_nucleus(n) = q;
    noad_options(n) = noad_option_unpack_list;
    return n;
}

/*tex
    Easiest is to permit all modifiers and just ignore those that make no sense. We then can
    stepwise support whatever modifier we like later on.
*/

void tex_run_math_modifier(void)
{
    halfword tail = cur_list.tail;
    if (cur_list.head != tail) {
        switch (node_type(tail)) {
            case simple_noad:
                switch (cur_chr) {
                    case adapt_to_left_modifier_code:
                        noad_options(tail) = unset_option(noad_options(tail), noad_option_adapt_to_right_size);
                        noad_options(tail) |= noad_option_adapt_to_left_size;
                        break;
                    case adapt_to_right_modifier_code:
                        noad_options(tail) = unset_option(noad_options(tail), noad_option_adapt_to_left_size);
                        noad_options(tail) |= noad_option_adapt_to_right_size;
                        break;
                    /* todo: actually this one can also be used for other types */
                    case axis_modifier_code:
                        noad_options(tail) |= noad_option_axis;
                        break;
                    case no_axis_modifier_code:
                        noad_options(tail) |= noad_option_no_axis;
                        break;
                    case phantom_modifier_code:
                        noad_options(tail) |= noad_option_phantom;
                        break;
                    case void_modifier_code:
                        noad_options(tail) |= noad_option_void;
                        break;
                    case source_modifier_code:
                        if (tex_scan_keyword("nucleus")) {
                            noad_options(tail) |= noad_option_source_on_nucleus;    
                        }
                        noad_source(tail) = tex_scan_int(0, NULL);
                        break;
                    case openup_height_modifier_code:
                        noad_options(tail) |= noad_option_openup_height;
                        noad_height(tail) = tex_scan_dimen(0, 0, 0, 0, NULL);
                        break;
                    case openup_depth_modifier_code:
                        noad_options(tail) |= noad_option_openup_depth;
                        noad_depth(tail) = tex_scan_dimen(0, 0, 0, 0, NULL);
                        break;
                    case display_limits_modifier_code:
                        noad_options(tail) = unset_option(noad_options(tail), noad_option_limits | noad_option_no_limits);
                        break;
                    case limits_modifier_code:
                        noad_options(tail) = unset_option(noad_options(tail), noad_option_no_limits);
                        noad_options(tail) |= noad_option_limits;
                        break;
                    case no_limits_modifier_code:
                        noad_options(tail) = unset_option(noad_options(tail), noad_option_limits);
                        noad_options(tail) |= noad_option_no_limits;
                        break;
                }
            default:
                switch (node_type(tail)) {
                    case accent_noad:
                        switch (cur_chr) {
                            case source_modifier_code:
                                if (tex_scan_keyword("nucleus")) {
                                    noad_options(tail) |= noad_option_source_on_nucleus;    
                                }
                                noad_source(tail) = tex_scan_int(0, NULL);
                                break;
                        }

                }
                break;
        }
    }
}

/*tex

     Delimiter fields of noads are filled in by the |scan_delimiter| routine. The first parameter
     of this procedure is the |mem| address where the delimiter is to be placed; the second tells
     if this delimiter follows |\radical| or not.

*/

static void tex_aux_scan_delimiter(halfword target, int code, int mathclass)
{
    delcodeval dval = tex_no_del_code();
    mathcodeval mval = tex_no_math_code();
    switch (code) {
        case no_mathcode:
            /* can be integrated */
            do {
                tex_get_x_token();
            } while (cur_cmd == spacer_cmd || cur_cmd == relax_cmd);
            switch (cur_cmd) {
                case letter_cmd:
                case other_char_cmd:
                    dval = tex_get_del_code(cur_chr);
                    if (tex_has_del_code(dval)) { 
                        goto REALDELIMITER;
                    } else { 
                        mval = tex_get_math_code(cur_chr);
                        goto FAKEDELIMITER;
                    }
                case delimiter_number_cmd:
                    switch (cur_chr) {
                        case math_delimiter_code:
                            /*tex |\delimiter| */
                            dval = tex_aux_scan_extdef_del_code(tex_mathcode, 1);
                            break;
                        case math_udelimiter_code:
                            /*tex |\Udelimiter| */
                            dval = tex_aux_scan_extdef_del_code(umath_mathcode, 1);
                            break;
                        default:
                            tex_confusion("scan delimiter, case 1");
                            break;
                    }
                    goto REALDELIMITER;
                case mathspec_cmd:
                    mval = tex_get_math_spec(cur_chr);
                    goto FAKEDELIMITER;
                case math_char_number_cmd:
                    switch (cur_chr) {
                        case math_char_number_code:
                            mval = tex_scan_mathchar(tex_mathcode);
                            break;
                        case math_xchar_number_code:
                            mval = tex_scan_mathchar(umath_mathcode);
                            break;
                        default:
                            tex_confusion("scan math char, case 1");
                            break;
                    }
                    goto FAKEDELIMITER;
            }
            break;
        case tex_mathcode:
            /*tex |\radical| */
            dval = tex_aux_scan_extdef_del_code(tex_mathcode, 1);
            goto REALDELIMITER;
        case umath_mathcode:
            /*tex |\Uradical| */
            dval = tex_aux_scan_extdef_del_code(umath_mathcode, 0);
            goto REALDELIMITER;
        default:
            tex_confusion("scan delimiter, case 2");
            goto REALDELIMITER;
    }
  FAKEDELIMITER:
    if (mathclass != unset_noad_class) {
        mval.class_value = (short) mathclass; 
    }
    dval.small = mval;
    dval.large = mval;
  REALDELIMITER:
    if (! target) {
        return;
    } else if (tex_has_del_code(dval)) {
        node_subtype(target) = dval.small.class_value;
        delimiter_small_family(target) = dval.small.family_value;
        delimiter_small_character(target) = dval.small.character_value;
        delimiter_large_family(target) = dval.large.family_value;
        delimiter_large_character(target) = dval.large.character_value;
    } else {
        tex_back_input(cur_tok);
        tex_handle_error(
            normal_error_type,
            "Missing delimiter (. inserted)",
            "I was expecting to see something like '(' or '\\{' or '\\}' here. Acceptable\n"
            "delimiters are characters whose \\delcode is nonnegative, or you can use\n"
            "'\\delimiter <delimiter code>'."
        );
        node_subtype(target) = unset_noad_class;
        delimiter_small_family(target) = 0;
        delimiter_small_character(target) = 0;
        delimiter_large_family(target) = 0;
        delimiter_large_character(target) = 0;
    }
    return;
}

void tex_run_math_radical(void)
{
    halfword code = cur_chr;
    fullword options = 0;
    halfword radical = tex_new_node(radical_noad, (quarterword) code);
    halfword style = yet_unset_math_style;
    halfword variant = 0; /* quad, harmless */
    halfword attrlist = null;
    tex_tail_append(radical);
    halfword top = null;
    halfword bottom = null;
    /* only kewords to UI ones? */
    while (1) {
        switch (tex_scan_character("abeswlmrhndtABESWLMRHDNT", 0, 1, 0)) {
            case 0:
                goto DONE;
            case 'a': case 'A':
                if (tex_scan_mandate_keyword("attr", 1)) {
                    attrlist = tex_scan_attribute(attrlist);
                }
                break;
            case 'b': case 'B':
                if (tex_scan_mandate_keyword("bottom", 1)) {
                    bottom = 1;
                }
                break;
            case 'e': case 'E':
                if (tex_scan_mandate_keyword("exact", 1)) {
                    options = options | noad_option_exact;
                }
                break;
            case 't': case 'T':
                if (tex_scan_mandate_keyword("top", 1)) {
                    top = 1;
                }
                break;
            case 's': case 'S':
                switch (tex_scan_character("hitoHITO", 0, 0, 0)) {
                    case 't': case 'T':
                        switch (tex_scan_character("ryRY", 0, 0, 0)) {
                            case 'y': case 'Y':
                                if (tex_scan_mandate_keyword("style", 3)) {
                                    switch (code) {
                                        case normal_radical_subtype:
                                        case radical_radical_subtype:
                                        case root_radical_subtype:
                                        case rooted_radical_subtype:
                                        case delimited_radical_subtype:
                                            style = tex_scan_math_style_identifier(1, 0);
                                            break;
                                        default:
                                            /* ignore */
                                            break;
                                    }
                                }
                                break;
                            case 'r': case 'R':
                                if (tex_scan_mandate_keyword("stretch", 3)) {
                                     options = options | noad_option_stretch;
                                }
                                break;
                            default: 
                                tex_aux_show_keyword_error("style|stretch");
                                goto DONE;
                        }
                        break;
                    case 'o': case 'O':
                        if (tex_scan_mandate_keyword("source", 2)) {
                            noad_source(radical) = tex_scan_int(0, NULL);
                        }
                        break;
                    case 'i': case 'I':
                        if (tex_scan_mandate_keyword("size", 2)) {
                            radical_size(radical) = tex_scan_int(0, NULL);
                        }
                        break;
                    case 'h': case 'H':
                        if (tex_scan_mandate_keyword("shrink", 2)) {
                             options = options | noad_option_shrink;
                        }
                        break;
                    default:
                        tex_aux_show_keyword_error("style|source|stretch|shrink");
                        goto DONE;
                }
                break;
            case 'w': case 'W':
                if (tex_scan_mandate_keyword("width", 1)) {
                    noad_width(radical) = tex_scan_dimen(0, 0, 0, 0, NULL);
                }
                break;
            case 'd': case 'D':
                if (tex_scan_mandate_keyword("depth", 1)) {
                    radical_depth(radical) = tex_scan_dimen(0, 0, 0, 0, NULL);
                }
                break;
            case 'h': case 'H':
                if (tex_scan_mandate_keyword("height", 1)) {
                    radical_height(radical) = tex_scan_dimen(0, 0, 0, 0, NULL);
                }
                break;
            case 'l': case 'L':
                if (tex_scan_mandate_keyword("left", 1)) {
                     options = options | noad_option_left;
                }
                break;
            case 'm': case 'M':
                if (tex_scan_mandate_keyword("middle", 1)) {
                    options = options | noad_option_middle;
                }
                break;
            case 'r': case 'R':
                if (tex_scan_mandate_keyword("right", 1)) {
                    options = options | noad_option_right;
                }
                break;
            case 'n': case 'N':
                if (tex_scan_mandate_keyword("nooverflow", 1)) {
                    options |= noad_option_no_overflow;
                }
                break;
            default:
                goto DONE;
        }
    }
  DONE:
    if (style == yet_unset_math_style) {
        switch (code) {
            case normal_radical_subtype:
            case radical_radical_subtype:
            case root_radical_subtype:
                variant = math_parameter_radical_variant;
                break;
            case under_delimiter_radical_subtype:
                variant = math_parameter_under_delimiter_variant;
                break;
            case over_delimiter_radical_subtype:
                variant = math_parameter_over_delimiter_variant;
                break;
            case delimiter_under_radical_subtype:
                variant = math_parameter_delimiter_under_variant;
                break;
            case delimiter_over_radical_subtype:
                variant = math_parameter_delimiter_over_variant;
                break;
            case delimited_radical_subtype:
                variant = math_parameter_radical_variant; /* math_parameter_delimited_variant */
                break;
            case h_extensible_radical_subtype:
                variant = math_parameter_h_extensible_variant;
                break;
        }
        style = variant ? tex_math_style_variant(cur_list.math_style, variant) : cur_list.math_style;
    }
    if (attrlist) {
        tex_attach_attribute_list_attribute(radical, attrlist);
    }
    noad_options(radical) = options;
    set_noad_style(radical, style);
    {
        switch (code) {
            case normal_radical_subtype:
                {
                    halfword left = tex_new_node(delimiter_node, 0);
                    radical_left_delimiter(radical) = left;
                    tex_aux_scan_delimiter(left, tex_mathcode, unset_noad_class);
                }
                break;
            case radical_radical_subtype:
            case root_radical_subtype:
            case rooted_radical_subtype:
            case delimited_radical_subtype:
                {
                    halfword left = tex_new_node(delimiter_node, 0);
                    radical_left_delimiter(radical) = left;
                    tex_aux_scan_delimiter(left, umath_mathcode, unset_noad_class);
                }
                switch (code) {
                    case rooted_radical_subtype:
                    case delimited_radical_subtype:
                        {
                            halfword right = tex_new_node(delimiter_node, 0);
                            radical_right_delimiter(radical) = right;
                            tex_aux_scan_delimiter(right, umath_mathcode, unset_noad_class);
                        }
                }
                break;
            case under_delimiter_radical_subtype:
            case over_delimiter_radical_subtype:
            case delimiter_under_radical_subtype:
            case delimiter_over_radical_subtype:
            case h_extensible_radical_subtype:
                {
                    halfword left = tex_new_node(delimiter_node, 0);
                    radical_left_delimiter(radical) = left;
                    tex_aux_scan_delimiter(left, umath_mathcode, unset_noad_class);
                }
                break;
            default:
                tex_confusion("scan math radical");
                break;
        }
        if (top) { 
            top = tex_new_node(delimiter_node, 0);
            radical_top_delimiter(radical) = top;
            tex_aux_scan_delimiter(top, umath_mathcode, unset_noad_class);
        }
        if (bottom) { 
            bottom = tex_new_node(delimiter_node, 0);
            radical_bottom_delimiter(radical) = bottom;
            tex_aux_scan_delimiter(bottom, umath_mathcode, unset_noad_class);
        }
    }
    switch (code) {
        case h_extensible_radical_subtype:
            /*tex type will change */
            {
                halfword q = tex_new_node(sub_box_node, 0);
                noad_nucleus(radical) = q;
                break;
            }
        case root_radical_subtype:
        case rooted_radical_subtype:
            {
                tex_set_saved_record(saved_radical_degree_done, radical_degree_done_save_type, 0, 0); 
                tex_set_saved_record(saved_radical_style, radical_style_save_type, 0, 0); 
                lmt_save_state.save_stack_data.ptr += saved_radical_n_of_items;
                tex_aux_push_math(math_radical_group, tex_math_style_variant(style, math_parameter_degree_variant));
                tex_scan_left_brace();
                break;
            }
        default :
            {
                halfword q = tex_new_node(math_char_node, 0);
                noad_nucleus(radical) = q;
                tex_aux_scan_math(q, tex_math_style_variant(style, variant ? variant : math_parameter_radical_variant), 0, 0, 0, 0, unset_noad_class, unset_noad_class);
                break;
            }
    }
}

void tex_finish_math_radical(void)
{
    halfword whatever = tex_new_node(sub_mlist_node, 0);
    tex_aux_unsave_math();
    if (saved_type(saved_radical_degree_done - saved_radical_n_of_items) == radical_degree_done_save_type) {
        halfword content = tex_aux_finish_math_list(null);
        halfword radical = cur_list.tail;
        kernel_math_list(whatever) = content;
        if (saved_value(saved_radical_degree_done - saved_radical_n_of_items)) {
            noad_nucleus(radical) = whatever;
            lmt_save_state.save_stack_data.ptr -= saved_radical_n_of_items;
        } else {
            halfword style = saved_value(saved_radical_style - saved_radical_n_of_items);
            radical_degree(radical) = whatever;
            tex_set_saved_record(saved_radical_degree_done - saved_radical_n_of_items, radical_degree_done_save_type, 0, 1); 
            tex_aux_push_math(math_radical_group, tex_math_style_variant(style, math_parameter_radical_variant));
            tex_scan_left_brace();     
        }
    } else {
        tex_confusion("scan radical");
    }
}

void tex_run_math_accent(void)
{
    mathcodeval t = tex_no_math_code();
    mathcodeval b = tex_no_math_code();
    mathcodeval o = tex_no_math_code();
    halfword code = cur_chr;
    halfword accent = tex_new_node(accent_noad, bothflexible_accent_subtype);
    quarterword subtype = ordinary_noad_subtype;
    halfword mathclass = accent_noad_subtype;
    halfword attrlist = null;
    if (cur_cmd == accent_cmd) {
        tex_handle_error(
            normal_error_type,
            "Please use \\mathaccent for accents in math mode",
            "I'm changing \\accent to \\mathaccent here; wish me luck. (Accents are not the\n"
            "same in formulas as they are in text.)" );
    }
    tex_tail_append(accent);
    switch (code) {
        case math_accent_code:
            /*tex |\mathaccent| */
            t = tex_scan_mathchar(tex_mathcode);
            break;
        case math_uaccent_code:
            /*tex |\Umathaccent| */
            while (1) {
                switch (tex_scan_character("abcensftokABCENSFTOK", 0, 1, 0)) {
                    case 'a': case 'A':
                        switch (tex_scan_character("txTX", 0, 0, 0)) {
                            case 't': case 'T':
                                if (tex_scan_mandate_keyword("attr", 2)) {
                                    attrlist = tex_scan_attribute(attrlist);
                                }
                                break;
                         // case 'x': case 'X':
                         //     if (tex_scan_mandate_keyword("axis", 2)) {
                         //         noad_options(accent) |= noad_option_axis;
                         //     }
                         //     break;
                            default:
                         //     tex_aux_show_keyword_error("attr|axis");
                                tex_aux_show_keyword_error("attr");
                                goto DONE;
                        }
                        break;
                    case 'c': case 'C':
                        switch (tex_scan_character("elEL", 0, 0, 0)) {
                            case 'e': case 'E':
                                if (tex_scan_mandate_keyword("center", 2)) {
                                    noad_options(accent) |= noad_option_center;
                                }
                                break;
                            case 'l': case 'L':
                                if (tex_scan_mandate_keyword("class", 2)) {
                                    halfword c = (quarterword) tex_scan_math_class_number(0);
                                    if (valid_math_class_code(c)) {
                                        mathclass = c;
                                    }
                                }
                                break;
                            default:
                                tex_aux_show_keyword_error("center|class");
                                goto DONE;
                        }
                        break;
                    case 'e': case 'E':
                        if (tex_scan_mandate_keyword("exact", 1)) {
                            noad_options(accent) |= noad_option_exact;
                        }
                        break;
                    case 's': case 'S':
                        switch (tex_scan_character("othOTH", 0, 0, 0)) {
                            case 'o': case 'O':
                                if (tex_scan_mandate_keyword("source", 2)) {
                                    noad_source(accent) = tex_scan_int(0, NULL);
                                }
                                break;
                            case 't': case 'T':
                                if (tex_scan_mandate_keyword("stretch", 2)) {
                                    noad_options(accent) |= noad_option_stretch;
                                }
                                break;
                            case 'h': case 'H':
                                if (tex_scan_mandate_keyword("shrink", 2)) {
                                    noad_options(accent) |= noad_option_shrink;
                                }
                                break;
                            default:
                                tex_aux_show_keyword_error("source|stretch|shrink");
                                goto DONE;
                        }
                        break;
                    case 'f': case 'F':
                        switch (tex_scan_character("frFR", 0, 0, 0)) {
                            case 'r': case 'R':
                                if (tex_scan_mandate_keyword("fraction", 2)) {
                                    accent_fraction(accent) = tex_scan_int(0, NULL);
                                }
                                break;
                            case 'f': case 'F':
                                /*tex fixed <char> */
                                if (tex_scan_mandate_keyword("fixed", 2)) {
                                    node_subtype(accent) = fixedtop_accent_subtype;
                                    t = tex_scan_mathchar(umath_mathcode);
                                }
                                goto DONE;
                            default:
                                tex_aux_show_keyword_error("fraction|fixed");
                                goto DONE;
                        }
                    case 'k': case 'K':
                        if (tex_scan_mandate_keyword("keepbase", 1)) {
                            noad_options(accent) |= noad_option_keep_base;
                        }
                        break;
                    case 'n': case 'N':
                        if (tex_scan_mandate_keyword("nooverflow", 1)) {
                            /*tex 
                                Actually there never is an overflow but for consistency we do 
                                accept this key. Mayebe in the future it will be used. 
                            */
                            noad_options(accent) |= noad_option_no_overflow;
                        }
                        break;
                    case 'b': case 'B':
                        switch (tex_scan_character("aoAo", 0, 0, 0)) {
                            case 'a': case 'A':
                                if (tex_scan_mandate_keyword("base", 2)) {
                                    noad_options(accent) |= noad_option_auto_base;
                                }
                                break;
                            case 'o': case 'O':
                                /*tex bottom [fixed] <char> */
                                /*tex both [fixed] <char> [fixed] <char> */
                                if (tex_scan_character("t", 0, 0, 0)) {
                                     switch (tex_scan_character("thTH", 0, 0, 0)) {
                                         case 'h': case 'H':
                                            /*tex top bottom */
                                            if (tex_scan_keyword("fixed")) {
                                                node_subtype(accent) = fixedtop_accent_subtype;
                                            }
                                            t = tex_scan_mathchar(umath_mathcode);
                                            if (tex_scan_keyword("fixed")) {
                                                node_subtype(accent) = fixedboth_accent_subtype;
                                            }
                                            b = tex_scan_mathchar(umath_mathcode);
                                            goto DONE;
                                         case 't': case 'T':
                                             if (tex_scan_mandate_keyword("bottom", 4)) {
                                                /*tex bottom */
                                                if (tex_scan_keyword("fixed")) {
                                                    node_subtype(accent) = fixedbottom_accent_subtype;
                                                }
                                                b = tex_scan_mathchar(umath_mathcode);
                                             }
                                            goto DONE;
                                        default:
                                            tex_aux_show_keyword_error("both|bottom");
                                            goto DONE;
                                     }
                                }
                                goto DONE;
                            default:
                                tex_aux_show_keyword_error("base|both|bottom");
                                goto DONE;
                        }
                        break;
                    case 't': case 'T':
                        /*tex top [fixed] <char> */
                        if (tex_scan_mandate_keyword("top", 1)) {
                            if (tex_scan_keyword("fixed")) {
                                node_subtype(accent) = fixedtop_accent_subtype;
                            }
                            t = tex_scan_mathchar(umath_mathcode);
                        }
                        goto DONE;
                    case 'o': case 'O':
                        /*tex overlay [fixed] <char> */
                        if (tex_scan_mandate_keyword("overlay", 1)) {
                            if (tex_scan_keyword("fixed")) {
                                node_subtype(accent) = fixedtop_accent_subtype;
                            }
                            o = tex_scan_mathchar(umath_mathcode);
                        }
                        goto DONE;
                    default:
                        /*tex top <char> */
                        t = tex_scan_mathchar(umath_mathcode);
                        goto DONE;
                }
            }
        default:
            tex_confusion("scan math accent");
    }
  DONE:
    if (attrlist) {
        tex_attach_attribute_list_attribute(accent, attrlist);
    }
    if (! (t.character_value == 0 && t.family_value == 0)) {
        halfword n = tex_new_node(math_char_node, 0);
        subtype = tex_aux_set_math_char(n, &t, NULL);
        accent_top_character(accent) = n;
    }
    if (! (b.character_value == 0 && b.family_value == 0)) {
        halfword n = tex_new_node(math_char_node, 0);
        subtype = tex_aux_set_math_char(n, &b, NULL);
        accent_bottom_character(accent) = n;
    }
    if (! (o.character_value == 0 && o.family_value == 0)) {
        halfword n = tex_new_node(math_char_node, 0);
        subtype = tex_aux_set_math_char(n, &o, NULL);
        accent_middle_character(accent) = n;
    }
    {
        halfword n = tex_new_node(math_char_node, subtype);
        noad_nucleus(accent) = n;
        tex_aux_scan_math(n, tex_math_style_variant(cur_list.math_style, math_parameter_accent_variant), 0, 0, 0, 0, unset_noad_class, unset_noad_class);
    }
    set_noad_main_class(accent, mathclass);
}

/*tex

    The routine that scans the four mlists of a |\mathchoice| is very much like the routine that
    builds discretionary nodes. Finally, the |\mathchoice| primitive creates a |choice_node|,
    which has special subfields |display_mlist|, |text_mlist|, |script_mlist|, and
    |script_script_mlist| pointing to the mlists for each style.

*/

void tex_run_math_choice(void) {
    switch (cur_chr) {
        case math_discretionary_code:
            {
                halfword n = tex_new_node(choice_node, discretionary_choice_subtype);
                choice_class(n) = unset_noad_class;
                while (1) {
                    switch (tex_scan_character("cC", 0, 1, 0)) {
                        case 0:
                            goto DONE;
                        case 'c': case 'C':
                            if (tex_scan_mandate_keyword("class", 1)) {
                                choice_class(n) = tex_scan_math_class_number(0);
                            }
                            break;
                        default:
                            goto DONE;
                    }
                }
              DONE:
                tex_tail_append(n);
                tex_set_saved_record(saved_choice_item_count, choices_count_save_type, 0, math_pre_break_choice);
                lmt_save_state.save_stack_data.ptr += saved_choice_n_of_items;
                tex_aux_push_math(math_choice_group, cur_list.math_style);
                tex_scan_left_brace();
                break;
            }
        case math_choice_code:
            /*tex |\mathchoice| */
            {
                halfword n = tex_new_node(choice_node, normal_choice_subtype);
                tex_tail_append(n);
                tex_set_saved_record(saved_choice_item_count, choices_count_save_type, 0, math_display_choice);
                lmt_save_state.save_stack_data.ptr += saved_choice_n_of_items;
                tex_aux_push_math(math_choice_group, display_style);
                tex_scan_left_brace();
                break;
            }
        case math_ustack_code:
            /*tex |\Ustack| */
            {
             // halfword m = tex_new_node(sub_mlist_node, 0); /* was for some reason a math_char_node */
                halfword m = tex_new_node(math_char_node, 0);
                halfword n = tex_new_node(simple_noad, ordinary_noad_subtype);
                halfword s = tex_math_style_variant(cur_list.math_style, math_parameter_stack_variant);
                tex_tail_append(n);
                noad_nucleus(n) = m;
                tex_scan_left_brace();
                tex_set_saved_record(saved_math_group_item_pointer, math_pointer_save_type, 0, m);
                tex_set_saved_record(saved_math_group_all_class, math_class_save_type, 0, unset_noad_class);
                lmt_save_state.save_stack_data.ptr += saved_math_group_n_of_items;
                tex_aux_push_math(math_stack_group, s);
                break;
            }
    }
}

int tex_current_math_style(void)
{
    return is_m_mode(cur_list.mode) ? cur_list.math_style : -1;
}

int tex_current_math_main_style(void)
{
    return is_m_mode(cur_list.mode) ? cur_list.math_main_style : -1;
}

void tex_finish_math_choice(void)
{
    halfword content;
    tex_aux_unsave_math();
    content = tex_aux_finish_math_list(null);
    /* We should just count and not rely on the next hackery test: */
    if (saved_type(saved_choice_item_count - saved_choice_n_of_items) == choices_count_save_type) {
        int choice = saved_value(saved_choice_item_count - saved_choice_n_of_items);
        int style = cur_list.math_style;
        switch (node_subtype(cur_list.tail)) { 
            case normal_choice_subtype:
                switch (choice) {
                    case math_display_choice:
                        choice_display_mlist(cur_list.tail) = content;
                        style = text_style;
                        break;
                    case math_text_choice:
                        choice_text_mlist(cur_list.tail) = content;
                        style = script_style;
                        break;
                    case math_script_choice:
                        choice_script_mlist(cur_list.tail) = content;
                        style = script_script_style;
                        break;
                    case math_script_script_choice:
                        choice_script_script_mlist(cur_list.tail) = content;
                        lmt_save_state.save_stack_data.ptr -= saved_choice_n_of_items;
                        return;
                }
                break;
            case discretionary_choice_subtype:
                switch (choice) {
                    case math_pre_break_choice:
                        choice_pre_break(cur_list.tail) = content;
                        style = display_style;
                        break;
                    case math_post_break_choice:
                        choice_post_break(cur_list.tail) = content;
                        style = text_style;
                        break;
                    case math_no_break_choice:
                        choice_no_break(cur_list.tail) = content;
                        style = script_style;
                        lmt_save_state.save_stack_data.ptr -= saved_choice_n_of_items;
                        return;
                }
                break;
        }
        tex_set_saved_record(saved_choice_item_count - saved_choice_n_of_items, choices_count_save_type, 0, choice + 1);
        tex_aux_push_math(math_choice_group, style);
        tex_scan_left_brace();
    } else {
        tex_confusion("scan build choices");
    }
}

void tex_finish_math_fraction(void)
{
    halfword content;
    tex_aux_unsave_math();
    content = tex_aux_finish_math_list(null);
    if (saved_type(saved_fraction_item_variant - saved_fraction_n_of_items) == fraction_variant_save_type) {
        halfword over = saved_value(saved_fraction_item_variant - saved_fraction_n_of_items);
        halfword autostyle = saved_value(saved_fraction_item_autostyle - saved_fraction_n_of_items);
        halfword userstyle = saved_value(saved_fraction_item_userstyle - saved_fraction_n_of_items);
        halfword fraction = cur_list.tail;
        set_noad_style(fraction, userstyle);
        switch (over) {
            case math_numerator_above:
                kernel_math_list(fraction_numerator(fraction)) = content;
                break;
            case math_denominator_above:
                kernel_math_list(fraction_denominator(fraction)) = content;
                lmt_save_state.save_stack_data.ptr -= saved_fraction_n_of_items;
                return;
        }
        tex_set_saved_record(saved_fraction_item_variant - saved_fraction_n_of_items, fraction_variant_save_type, 0, over + 1);
        tex_aux_push_math(math_fraction_group, autostyle);
        tex_scan_left_brace();
    } else {
        tex_confusion("scan build fraction");
    }
}

void tex_finish_math_operator(void)
{
    halfword content;
    tex_aux_unsave_math();
    content = tex_aux_finish_math_list(null);
    if (saved_type(saved_operator_item_variant - saved_operator_n_of_items) == operator_variant_save_type) {
        halfword over = saved_value(saved_operator_item_variant - saved_operator_n_of_items);
        halfword fenced = cur_list.tail;
        switch (over) {
            case math_limits_top:
                kernel_math_list(fence_delimiter_top(fenced)) = content;
                break;
            case math_limits_bottom:
                kernel_math_list(fence_delimiter_bottom(fenced)) = content;
                lmt_save_state.save_stack_data.ptr -= saved_operator_n_of_items;
                return;
        }
        tex_set_saved_record(saved_operator_item_variant - saved_operator_n_of_items, operator_variant_save_type, 0, over + 1);
        tex_aux_push_math(math_operator_group, tex_math_style_variant(cur_list.math_style, math_parameter_subscript_variant));
        tex_scan_left_brace();
    } else {
        tex_confusion("scan build operator");
    }
}

/*tex

    Subscripts and superscripts are attached to the previous nucleus by the action procedure called
    |sub_sup|.

*/

# define scripts_allowed(A) ((node_type((A)) >= simple_noad) && (node_type((A)) < fence_noad))

static halfword tex_math_double_atom(void)
{
    halfword tail = tex_new_node(simple_noad, ordinary_noad_subtype);
    halfword list = tex_new_node(sub_mlist_node, 0);
    tex_tail_append(tail);
    if (math_double_script_mode_par >= 0) { 
        node_subtype(tail) = (math_double_script_mode_par >> 16) & 0xFF;
        noad_class_left(tail) = (math_double_script_mode_par >> 8) & 0xFF;
        noad_class_right(tail) = (math_double_script_mode_par >> 0) & 0xFF;
    }
    noad_nucleus(tail) = list;
    return tail; 
}

void tex_run_math_script(void)
{
    int code = cur_chr;
    halfword tail = cur_list.tail;
    switch (cur_cmd) {
        case subscript_cmd:
            code = math_sub_script_code;
            break;
        case superscript_cmd:
            code = math_super_script_code;
            break;
    }
    switch (code) {
        case math_no_script_code:
            {
                halfword glue = tex_new_glue_node(zero_glue, conditional_math_glue);
                tex_tail_append(glue);
                tex_add_glue_option(glue, glue_option_no_auto_break);
            }
            return;
        case math_no_ruling_code:
            {
                halfword glue = tex_new_glue_node(zero_glue, rulebased_math_glue);
                tex_tail_append(glue);
                tex_add_glue_option(glue, glue_option_no_auto_break);
            }
            return;
        case math_sub_script_code:
            tex_get_token();
            if (cur_tok == underscore_token || cur_cmd == subscript_cmd) {
                tex_get_token();
                if (cur_tok == underscore_token || cur_cmd == subscript_cmd) {
                    tex_get_token();
                    if (cur_tok == underscore_token || cur_cmd == subscript_cmd) {
                        code = math_shifted_sub_pre_script_code;
                    } else {
                        tex_back_input(cur_tok);
                        code = math_shifted_sub_script_code;
                    }
                } else {
                    tex_back_input(cur_tok);
                    code = math_sub_pre_script_code;
                }
            } else {
                tex_back_input(cur_tok);
            }
            break;
        case math_super_script_code:
            tex_get_token();
            if (cur_tok == circumflex_token || cur_cmd == superscript_cmd) {
                tex_get_token();
                if (cur_tok == circumflex_token || cur_cmd == superscript_cmd) {
                    tex_get_token();
                    if (cur_tok == circumflex_token || cur_cmd == superscript_cmd) {
                        code = math_shifted_super_pre_script_code;
                    } else {
                        tex_back_input(cur_tok);
                        code = math_shifted_super_script_code;
                    }
                } else {
                    tex_back_input(cur_tok);
                    code = math_super_pre_script_code;
                }
            } else {
                tex_back_input(cur_tok);
            }
            break;
    }
    if (tail == cur_list.head || (! scripts_allowed(tail))) {
        halfword n = tex_new_node(sub_mlist_node, 0);
        tail = tex_new_node(simple_noad, ordinary_noad_subtype);
        tex_tail_append(tail);
        noad_nucleus(tail) = n;
    }
    switch (code) {
        case math_sub_script_code:
        case math_no_sub_script_code:
        case math_shifted_sub_script_code:
            {
                if (noad_subscr(tail)) {
                    tail = tex_math_double_atom();
                    if (math_double_script_mode_par < 0) { 
                        tex_handle_error(
                            normal_error_type,
                            "Double subscript",
                            "I treat 'x_1_2' essentially like 'x_1{}_2'."
                        );
                    }
                }
                switch (code) {
                    case math_no_sub_script_code:
                        noad_options(tail) |= noad_option_no_sub_script;
                        break;
                    case math_shifted_sub_script_code:
                        noad_options(tail) |= noad_option_shifted_sub_script;
                        break;
                }
                {
                    halfword n = tex_new_node(math_char_node, 0);
                    noad_subscr(tail) = n;
                    tex_aux_scan_math(n, tex_math_style_variant(cur_list.math_style, math_parameter_subscript_variant), 0, 0, 0, 1, unset_noad_class, unset_noad_class);
                    if (! noad_script_order(tail)) {
                        noad_script_order(tail) = script_subscript_first;
                    }
                }
                break;
            }
        case math_sub_pre_script_code:
        case math_no_sub_pre_script_code:
        case math_shifted_sub_pre_script_code:
            {
                if (noad_subprescr(tail)) {
                    int limitation = node_type(tail) == fraction_noad; /*tex See remark at node definition. */
                    tail = tex_math_double_atom();
                    if (math_double_script_mode_par < 0) { 
                        tex_handle_error(
                            normal_error_type,
                            limitation ? "Fractions take no pre subscript directly" : "Double pre subscript",
                            "I just ignore it; consider wrapping this element."
                        );
                    }
                }
                switch (code) {
                    case math_no_sub_pre_script_code:
                        noad_options(tail) |= noad_option_no_sub_pre_script;
                        break;
                    case math_shifted_sub_pre_script_code:
                        noad_options(tail) |= noad_option_shifted_sub_pre_script;
                        break;
                }
                {
                    halfword n = tex_new_node(math_char_node, 0);
                    noad_subprescr(tail) = n;
                    tex_aux_scan_math(n, tex_math_style_variant(cur_list.math_style, math_parameter_subscript_variant), 0, 0, 0, 1, unset_noad_class, unset_noad_class);
                }
                break;
            }
        case math_super_script_code:
        case math_no_super_script_code:
        case math_shifted_super_script_code:
            {
                if (noad_supscr(tail)) {
                    tail = tex_math_double_atom();
                    if (math_double_script_mode_par < 0) { 
                        tex_handle_error(
                            normal_error_type,
                            "Double superscript",
                            "I treat 'x^1^2' essentially like 'x^1{}^2'."
                        );
                    }
                }
                switch (code) {
                    case math_no_super_script_code:
                        noad_options(tail) |= noad_option_no_super_script;
                        break;
                    case math_shifted_super_script_code:
                        noad_options(tail) |= noad_option_shifted_super_script;
                        break;
                }
                {
                    halfword n = tex_new_node(math_char_node, 0);
                    noad_supscr(tail) = n;
                    if (! noad_script_order(tail)) {
                        noad_script_order(tail) = script_superscript_first;
                    }
                    tex_aux_scan_math(n, tex_math_style_variant(cur_list.math_style, math_parameter_superscript_variant), 0, 0, 0, 1, unset_noad_class, unset_noad_class);
                }
                break;
            }
        case math_super_pre_script_code:
        case math_no_super_pre_script_code:
        case math_shifted_super_pre_script_code:
            {
                if (noad_supprescr(tail)) {
                    int limitation = node_type(tail) == fraction_noad; /*tex See remark at node definition. */
                    tail = tex_math_double_atom();
                    if (math_double_script_mode_par < 0) { 
                        tex_handle_error(
                            normal_error_type,
                            limitation ? "Fractions take no pre superscript directly" : "Double pre superscript",
                            "I just ignore it; consider wrapping this element."
                        );
                    }
                }
                switch (code) {
                    case math_no_super_script_code:
                        noad_options(tail) |= noad_option_no_super_pre_script;
                        break;
                    case math_shifted_super_pre_script_code:
                        noad_options(tail) |= noad_option_shifted_super_pre_script;
                        break;
                }
                {
                    halfword n = tex_new_node(math_char_node, 0);
                    noad_supprescr(tail) = n;
                    tex_aux_scan_math(n, tex_math_style_variant(cur_list.math_style, math_parameter_superscript_variant), 0, 0, 0, 1, unset_noad_class, unset_noad_class);
                }
                break;
            }
        case math_prime_script_code:
            {
                if (noad_prime(tail)) {
                    tail = tex_math_double_atom();
                    if (math_double_script_mode_par < 0) { 
                        tex_handle_error(
                            normal_error_type,
                            "Double prime script",
                            "I'll add a dummy nucleus."
                        );
                    }
                }
                {
                    halfword n = tex_new_node(math_char_node, 0);
                    noad_prime(tail) = n;
                    if (! noad_script_order(tail)) {
                        noad_script_order(tail) = script_primescript_first;
                    }
                    /* maybe it's own variant */
                    tex_aux_scan_math(n, tex_math_style_variant(cur_list.math_style, math_parameter_superscript_variant), 0, 0, 0, 1, unset_noad_class, unset_noad_class);
                }
                break;
            }
    }
}

/*tex

    An operation like |\over| causes the current mlist to go into a state of suspended animation:
    |incomplete_noad| points to a |fraction_noad| that contains the mlist-so-far as its numerator,
    while the denominator is yet to come. Finally when the mlist is finished, the denominator will
    go into the incomplete fraction noad, and that noad will become the whole formula, unless it is
    surrounded by |\left| and |\right| delimiters.

    We can probably replace the |incomplete_noad_par| trickery because we can now look back in the
    list using the |alink| field. But not now.

*/

void tex_run_math_fraction(void)
{
    /*tex The type of generalized fraction we are scanning: */
    halfword code = cur_chr;
    if (cur_list.incomplete_noad) {
        /*tex Recovery code. */
        switch (code) {
            case math_above_delimited_code:
            case math_over_delimited_code:
            case math_atop_delimited_code:
            case math_u_above_delimited_code:
            case math_u_over_delimited_code:
            case math_u_atop_delimited_code:
            case math_u_skewed_delimited_code:
            case math_u_stretched_delimited_code:
                tex_aux_scan_delimiter(null, no_mathcode, unset_noad_class);
                tex_aux_scan_delimiter(null, no_mathcode, unset_noad_class);
                break;
        }
        switch (code) {
            case math_above_code:
            case math_above_delimited_code:
            case math_u_above_code:
            case math_u_above_delimited_code:
                tex_scan_dimen(0, 0, 0, 0, NULL);
                break;
        }
        /*tex This is somewhat weird, this error here. */
        tex_handle_error(
            normal_error_type,
            "Ambiguous; you need another { and }",
            "I'm ignoring this fraction specification, since I don't know whether a\n"
            "construction like 'x \\over y \\over z' means '{x \\over y} \\over z' or\n"
            "'x \\over {y \\over z}'."
        );
    } else {
        halfword fraction = tex_new_node(fraction_noad, 0);
        halfword numerator = tex_new_node(sub_mlist_node, 0);
        halfword denominator = null;
        halfword autostyle = tex_math_style_variant(cur_list.math_style, math_parameter_fraction_variant);
        halfword userstyle = -1;
        halfword attrlist = null;
        fullword options = 0;
        halfword mathclass = fraction_noad_subtype;
        halfword rulethickness = preset_rule_thickness;
        int ruledone = 0;
        fraction_h_factor(fraction) = scaling_factor;
        fraction_v_factor(fraction) = scaling_factor;
        switch (code) {
            case math_above_code:
            case math_above_delimited_code:
                node_subtype(fraction) = above_fraction_subtype;
                goto NEXTSTEP1;
            case math_over_code:
            case math_over_delimited_code:
                node_subtype(fraction) = over_fraction_subtype;
                goto NEXTSTEP1;
            case math_atop_code:
            case math_atop_delimited_code:
                node_subtype(fraction) = atop_fraction_subtype;
              NEXTSTEP1:
                {
                    cur_list.incomplete_noad = fraction;
                    fraction_numerator(fraction) = numerator;
                    kernel_math_list(numerator) = node_next(cur_list.head);
                    node_next(cur_list.head) = null;
                    cur_list.tail = cur_list.head;
                    cur_list.math_style = autostyle;
                    break;
                }
            case math_u_above_code:
            case math_u_above_delimited_code:
                node_subtype(fraction) = above_fraction_subtype;
                goto NEXTSTEP2;
            case math_u_over_code:
            case math_u_over_delimited_code:
                node_subtype(fraction) = over_fraction_subtype;
                goto NEXTSTEP2;
            case math_u_atop_code:
            case math_u_atop_delimited_code:
                node_subtype(fraction) = atop_fraction_subtype;
                goto NEXTSTEP2;
            case math_u_skewed_code:
            case math_u_skewed_delimited_code:
                node_subtype(fraction) = skewed_fraction_subtype;
                goto NEXTSTEP2;
            case math_u_stretched_code:
            case math_u_stretched_delimited_code:
                node_subtype(fraction) = stretched_fraction_subtype;
              NEXTSTEP2:
                {
                    cur_list.incomplete_noad = null;
                    denominator = tex_new_node(sub_mlist_node, 0);
                    tex_tail_append(fraction);
                    fraction_numerator(fraction) = numerator;
                    fraction_denominator(fraction) = denominator;
                    break;
                }
        }
        switch (code) {
            case math_u_skewed_code:
            case math_u_skewed_delimited_code:
            case math_u_stretched_code:
            case math_u_stretched_delimited_code:
                {
                    halfword q = tex_new_node(delimiter_node, 0);
                    fraction_middle_delimiter(fraction) = q;
                    tex_aux_scan_delimiter(q, no_mathcode, unset_noad_class);
                    break;
                }
        }
        switch (code) {
            case math_above_delimited_code:
            case math_over_delimited_code:
            case math_atop_delimited_code:
            case math_u_above_delimited_code:
            case math_u_over_delimited_code:
            case math_u_atop_delimited_code:
            case math_u_skewed_delimited_code:
            case math_u_stretched_delimited_code:
                {
                    halfword left = tex_new_node(delimiter_node, 0);
                    halfword right = tex_new_node(delimiter_node, 0);
                    fraction_left_delimiter(fraction) = left;
                    fraction_right_delimiter(fraction) = right;
                    tex_aux_scan_delimiter(left, no_mathcode, open_noad_subtype);
                    tex_aux_scan_delimiter(right, no_mathcode, close_noad_subtype);
                    break;
                }
        }
        switch (code) {
            /*tex We can't have keyword here because of compatibility reasons. */
            case math_above_code:
            case math_above_delimited_code:
                rulethickness = tex_scan_dimen(0, 0, 0, 0, NULL);
                break;
            case math_over_code:
            case math_over_delimited_code:
                rulethickness = preset_rule_thickness;
                break;
            case math_atop_code:
            case math_atop_delimited_code:
                break;
            /*tex
                But here we can! For practical reasons we accept the rule related options
                and in principle we cold do with one command.
            */
            case math_u_above_code:
            case math_u_above_delimited_code:
                goto OPTIONS;
            case math_u_atop_code:
            case math_u_atop_delimited_code:
            case math_u_over_code:
            case math_u_over_delimited_code:
                ruledone = 1;
                goto OPTIONS;
            case math_u_stretched_code:
            case math_u_stretched_delimited_code:
            case math_u_skewed_code:
            case math_u_skewed_delimited_code:
                ruledone = 1;
              OPTIONS:
                while (1) {
                    switch (tex_scan_character("acefhnpstvACEFHNPSTV", 0, 1, 0)) {
                        case 'a': case 'A':
                            if (tex_scan_mandate_keyword("attr", 1)) {
                                attrlist = tex_scan_attribute(attrlist);
                            }
                            break;
                        case 'c': case 'C':
                            if (tex_scan_mandate_keyword("class", 1)) {
                                halfword c = (quarterword) tex_scan_math_class_number(0);
                                if (valid_math_class_code(c)) {
                                    mathclass = c;
                                }
                            }
                            break;
                        case 'e': case 'E':
                            /* not used */
                            if (tex_scan_mandate_keyword("exact", 1)) {
                                options |= noad_option_exact;
                            }
                            break;
                        case 'p': case 'P':
                            /* not used */
                            if (tex_scan_mandate_keyword("proportional", 1)) {
                                options |= noad_option_proportional;
                            }
                            break;
                        case 'n': case 'N':
                            /*tex A bit over the top, three steps but a push back is still worse. */
                            if (tex_scan_character("oO", 0, 0, 0)) {
                                switch (tex_scan_character("aoAO", 0, 0, 0)) {
                                    case 'a': case 'A':
                                        if (tex_scan_mandate_keyword("noaxis", 3)) {
                                            options |= noad_option_no_axis;
                                        }
                                        break;
                                    case 'o': case 'O':
                                        if (tex_scan_mandate_keyword("nooverflow", 3)) {
                                            options |= noad_option_no_overflow;
                                        }
                                        break;
                                    default:
                                        tex_aux_show_keyword_error("noaxis|nooverflow");
                                        goto DONE;
                                }
                            }
                            break;
                        case 't': case 'T':
                            if (tex_scan_mandate_keyword("thickness", 1)) {
                                ruledone = 1;
                                rulethickness = tex_scan_dimen(0, 0, 0, 0, NULL);
                            }
                            break;
                        case 'f': case 'F':
                            if (tex_scan_mandate_keyword("font", 1)) {
                                ruledone = 1;
                                options |= noad_option_prefer_font_thickness;
                            }
                            break;
                        case 's': case 'S':
                            switch (tex_scan_character("toTO", 0, 0, 0)) {
                                case 't': case 'T':
                                    if (tex_scan_mandate_keyword("style", 2)) {
                                        halfword style = tex_scan_math_style_identifier(1, 0);
                                        if (denominator) {
                                            userstyle = style;
                                        } else {
                                            /* just ignore */
                                        }
                                    }
                                    break;
                                case 'o': case 'O':
                                    if (tex_scan_mandate_keyword("source", 2)) {
                                        noad_source(fraction) = tex_scan_int(0, NULL);
                                    }
                                    break;
                                default:
                                    tex_aux_show_keyword_error("style|source");
                                    goto DONE;
                            }
                            break;
                        case 'h': case 'H':
                            if (tex_scan_mandate_keyword("hfactor", 1)) {
                                fraction_h_factor(fraction) = tex_scan_int(0, NULL);
                            }
                            break;
                        case 'v': case 'V':
                            if (tex_scan_mandate_keyword("vfactor", 1)) {
                                fraction_v_factor(fraction) = tex_scan_int(0, NULL);
                            }
                            break;
                        default:
                            goto DONE;
                    }
                }
              DONE:
                if (! ruledone) {
                    rulethickness = tex_scan_dimen(0, 0, 0, 0, NULL);
                }
                break;
        }
        fraction_rule_thickness(fraction) = rulethickness;
        noad_options(fraction) = options;
        set_noad_main_class(fraction, mathclass);
        if (attrlist) {
            tex_attach_attribute_list_attribute(fraction, attrlist);
        }
        if (denominator) {
            /*tex
                In this case we need to pick up two math groups, and after some playing around using
                a variant of choices made most sense.
            */
            tex_set_saved_record(saved_fraction_item_variant, fraction_variant_save_type, 0, math_numerator_above);
            tex_set_saved_record(saved_fraction_item_autostyle, fraction_auto_style_save_type, 0, autostyle);
            tex_set_saved_record(saved_fraction_item_userstyle, fraction_user_style_save_type, 0, userstyle);
            lmt_save_state.save_stack_data.ptr += saved_fraction_n_of_items;
            cur_list.math_flatten = 0;
            tex_aux_push_math(math_fraction_group, autostyle);
            tex_scan_left_brace();
        } else {
            /*tex
                This is the pre/post variant. Actually, this variant is the reason why math scanning
                code is somewhat complex, this |incomplete_noad| stuff.
            */
        }
    }
}

/*tex

    At the end of a math formula or subformula, the |finish_math_list| routine is called upon to
    return a halfword to the newly completed mlist, and to pop the nest back to the enclosing
    semantic level. The parameter to |finish_math_list|, if not null, points to a |fence_noad| that
    ends the current mlist; this |fence_noad| has not yet been appended.

*/

static halfword tex_aux_finish_math_list(halfword p)
{
    halfword q;
    if (cur_list.incomplete_noad) {
        halfword denominator = fraction_denominator(cur_list.incomplete_noad);
        if (denominator) {
            node_type(denominator) = sub_mlist_node;
        } else {
            denominator = tex_new_node(sub_mlist_node, 0);
            fraction_denominator(cur_list.incomplete_noad) = denominator;
            q = denominator;
        }
        kernel_math_list(denominator) = node_next(cur_list.head);
        if (p) {
            halfword numerator = fraction_numerator(cur_list.incomplete_noad);
            q = kernel_math_list(numerator);
            if ((node_type(q) != fence_noad) || (node_subtype(q) != left_fence_side) || (! cur_list.delimiter)) {
                tex_confusion("right fence");
            }
            kernel_math_list(numerator) = node_next(cur_list.delimiter);
            node_next(cur_list.delimiter) = cur_list.incomplete_noad;
            node_next(cur_list.incomplete_noad) = p;
        } else {
            q = cur_list.incomplete_noad;
        }
    } else {
        node_next(cur_list.tail) = p;
        q = node_next(cur_list.head);
    }
    tex_pop_nest();
    return q;
}

/*tex
    Here traditional \TEX\ does some flattening but it can interfrere. It is for instance needed
    in order to find the skew of an accented character which happens at the outer level but that
    bit of code now does that recursively. I need to check why the accent was flattened so we 
    keep the original code here for testing.

    A \CONTEXT\ test case: |$\tilde{x}'$| i.e.\ primes!
*/

static void tex_aux_flatten_math_list(halfword parent)
{
    halfword p = kernel_math_list(parent);
    if (p && ! node_next(p)) {
        switch (node_type(p)) {
            case simple_noad:
                {
                    // how about the options and class
                    if (! noad_has_following_scripts(p) && tex_math_has_class_option(node_subtype(p), flatten_class_option) && ! noad_source(p)) {
                        halfword n = noad_nucleus(p);
                        halfword s = parent;
                        node_type(s) = node_type(n);
                        tex_math_copy_char_data(s, n, 1);
                        tex_attach_attribute_list_copy(s, n);
                        tex_flush_node(p);
                    }
                    break;
                }
            case accent_noad:
                {
                    halfword tail = cur_list.tail;
                    if (saved_value(saved_math_group_item_pointer) == noad_nucleus(tail) && node_type(tail) == simple_noad) {
                        switch (node_subtype(tail)) {
                            case ordinary_noad_subtype:
                                tex_couple_nodes(node_prev(tail), p);
                                noad_nucleus(tail) = null;
                                noad_subscr(tail) = null;
                                noad_supscr(tail) = null;
                                noad_prime(tail) = null;
                                tex_attach_attribute_list_copy(p, tail);
                                tex_flush_node(tail);
                                cur_list.tail = p;
                                break;
                        }
                    }
                    break;
                }
        }
    }
}

/*tex

    Now at last we're ready to see what happens when a right brace occurs in a math formula. Two
    special cases are simplified here: braces are effectively removed when they surround a single
    Ord without sub- and/or superscripts, or when they surround an accent that is the nucleus of
    an Ord atom.

*/

void tex_finish_math_group(void)
{
    int old_style = cur_list.math_style;
    halfword p, parent;
    quarterword allclass; 
    tex_aux_unsave_math();
    lmt_save_state.save_stack_data.ptr -= saved_math_group_n_of_items;
    parent = saved_value(saved_math_group_item_pointer);
    allclass = (quarterword) saved_value(saved_math_group_all_class);
    node_type(parent) = sub_mlist_node; /* can be math_char_node */
    p = tex_aux_finish_math_list(null); /* this incomplete trickery */
    kernel_math_list(parent) = p;
    if (cur_list.math_flatten) {
        tex_aux_flatten_math_list(parent);
    }
    /*tex
        If needed, here we pickup a next \quote {argument}, so we sort of finish a group and reopen
        a new one. It is somewhat curious that we use a character node here.
    */
    if (allclass != unset_noad_class) {
        while (p) {
            if (node_type(p) == simple_noad) { 
             // node_subtype(p) = allclass; 
                if (get_noad_main_class(p) == unset_noad_class) {
                    set_noad_main_class(p, allclass); 
                }
                if (get_noad_left_class(p) == unset_noad_class) {
                    set_noad_left_class(p, allclass); 
                }
                if (get_noad_right_class(p) == unset_noad_class) {
                    set_noad_right_class(p, allclass); 
                }
            }
            p = node_next(p);
        }
        /* */
    }
    if (node_next(saved_value(saved_math_group_item_pointer)) > 0) {
        halfword q = tex_new_node(math_char_node, 0); /* hm */
        noad_nucleus(node_next(saved_value(saved_math_group_item_pointer))) = q;
        node_next(saved_value(saved_math_group_item_pointer)) = null;
        saved_value(saved_math_group_item_pointer) = q;
        tex_aux_scan_math(q, old_style, 0, 0, 0, 0, unset_noad_class, unset_noad_class);
        /*tex restart */
    }
}

/*tex

    We have dealt with all constructions of math mode except |\left| and |\right|, so the picture is
    completed by the following sections of the program. The |middle| feature of \ETEX\ allows one or
    several |\middle| delimiters to appear between |\left| and |\right|.

*/

void tex_run_math_fence(void)
{
    scaled ht = 0;
    scaled dp = 0;
    scaled top = 0;
    scaled bottom = 0;
    fullword options = 0;
    halfword mainclass = unset_noad_class;
    halfword leftclass = unset_noad_class;
    halfword rightclass = unset_noad_class;
    halfword source = 0;
    halfword attrlist = null;
    quarterword st = (quarterword) cur_chr;
    halfword style = cur_list.math_style;
    if (math_check_fences_par) { 
        options |= noad_option_no_check;
    }
    switch (st) {
        case left_operator_side:
        case no_fence_side:
            break;
        case extended_left_fence_side:   /*tex |\Uleft| */
            st = left_fence_side;
            break;
        case extended_middle_fence_side: /*tex |\Umiddle| */
            st = middle_fence_side;
            break;
        case extended_right_fence_side:  /*tex |\Uright| */
            st = right_fence_side;
            break;
        default :
            goto CHECK_PAIRING;
    }
    while (1) {
           /* todo: break down  */
        switch (tex_scan_character("hdanlevpcrsutbHDANLEVPCRSUTB", 0, 1, 0)) {
            case 0:
                goto CHECK_PAIRING;
            case 'a': case 'A':
                switch (tex_scan_character("uxtUXT", 0, 0, 0)) {
                    case 'u': case 'U':
                        if (tex_scan_mandate_keyword("auto", 2)) {
                            options |= noad_option_auto;
                        }
                        break;
                    case 't': case 'T':
                        if (tex_scan_mandate_keyword("attr", 2)) {
                            attrlist = tex_scan_attribute(attrlist);
                        }
                        break;
                    case 'x': case 'X':
                        if (tex_scan_mandate_keyword("axis", 2)) {
                            options |= noad_option_axis;
                        }
                        break;
                    default:
                        tex_aux_show_keyword_error("auto|attr|axis");
                        goto CHECK_PAIRING;
                }
                break;
            case 'b': case 'B':
                if (tex_scan_mandate_keyword("bottom", 1)) {
                    bottom = tex_scan_dimen(0, 0, 0, 0, NULL);
                }
                break;
            case 'd': case 'D':
                if (tex_scan_mandate_keyword("depth", 1)) {
                    dp = tex_scan_dimen(0, 0, 0, 0, NULL);
                }
                break;
            case 'h': case 'H':
                if (tex_scan_mandate_keyword("height", 1)) {
                    ht = tex_scan_dimen(0, 0, 0, 0, NULL);
                }
                break;
            case 'n': case 'N':
                switch (tex_scan_character("oO", 0, 0, 0)) {
                    case 'o': case 'O':
                        switch (tex_scan_character("alcoALCO", 0, 0, 0)) {
                            case 'a': case 'A':
                                if (tex_scan_mandate_keyword("noaxis", 3)) {
                                    options |= noad_option_no_axis;
                                }
                                break;
                            case 'l': case 'L':
                                if (tex_scan_mandate_keyword("nolimits", 3)) {
                                    options = unset_option(options, noad_option_limits);
                                    options |= noad_option_no_limits;
                                }
                                break;
                            case 'c': case 'C':
                                if (tex_scan_mandate_keyword("nocheck", 3)) {
                                    options |= noad_option_no_check;
                                }
                                break;
                            case 'o': case 'O':
                                if (tex_scan_mandate_keyword("nooverflow", 3)) {
                                    options |= noad_option_no_overflow;
                                }
                                break;
                            default:
                                tex_aux_show_keyword_error("noaxis|nolimits|nocheck|nooverflow");
                                goto CHECK_PAIRING;
                        }
                        break;
                    default:
                        goto CHECK_PAIRING;
                }
                break;
            case 'l': case 'L':
                switch (tex_scan_character("ieIE", 0, 0, 0)) {
                    case 'e': case 'E':
                        if (tex_scan_mandate_keyword("leftclass", 2)) {
                            halfword c = tex_scan_math_class_number(0);
                         // if (! valid_math_class_code(c)) {
                            if (valid_math_class_code(c)) {
                                leftclass = c;
                            }
                        }
                        break;
                    case 'i': case 'I':
                        if (tex_scan_mandate_keyword("limits", 2)) {
                            options = unset_option(options, noad_option_no_limits);
                            options |= noad_option_limits;
                        }
                        break;
                    default:
                        tex_aux_show_keyword_error("leftclass|limits");
                        goto CHECK_PAIRING;
                }
                break;
            case 'e': case 'E':
                if (tex_scan_mandate_keyword("exact", 1)) {
                    options |= noad_option_exact;
                }
                break;
            case 'v': case 'V':
                if (tex_scan_mandate_keyword("void", 1)) {
                    options |= noad_option_void;
                }
                break;
            case 'p': case 'P':
                if (tex_scan_mandate_keyword("phantom", 1)) {
                    options |= noad_option_phantom;
                }
                break;
            case 'c': case 'C':
                if (tex_scan_mandate_keyword("class", 1)) {
                    mainclass = tex_scan_math_class_number(0);
                }
                break;
            case 'r': case 'R':
                if (tex_scan_mandate_keyword("rightclass", 1)) {
                    halfword c = tex_scan_math_class_number(0);
                 // if (valid_math_class_code(c)) {
                    if (valid_math_class_code(c)) {
                        rightclass = c;
                    }
                }
                break;
            case 's': case 'S':
                switch (tex_scan_character("coCO", 0, 0, 0)) {
                    case 'c': case 'C':
                        if (tex_scan_mandate_keyword("scale", 2)) {
                            options |= noad_option_scale;
                        }
                        break;
                    case 'o': case 'O':
                        if (tex_scan_mandate_keyword("source", 2)) {
                            source = tex_scan_int(0, NULL);
                        }
                        break;
                    default:
                        tex_aux_show_keyword_error("scale|source");
                        goto CHECK_PAIRING;
                }
                break;
            case 't': case 'T':
                if (tex_scan_mandate_keyword("top", 1)) {
                    top = tex_scan_dimen(0, 0, 0, 0, NULL);
                }
                break;
            default:
                goto CHECK_PAIRING;
        }
    }
  CHECK_PAIRING:
    switch (st) {
        case no_fence_side:
        case left_fence_side:
            break;
        case left_operator_side:
            {
                /* becomes a class option */
                int indisplay = style == display_style || style == cramped_display_style;
                /* options |= noad_option_no_check; */ /*tex Best just expect a dummy right. */
                if (! (has_option(options, noad_option_limits) || has_option(options, noad_option_no_limits))) {
                    /* otherwise we don't enter the placement function */
                    options |= indisplay ? noad_option_limits : noad_option_no_limits;
                }
            }
            break;
        default:
            if (cur_group != math_fence_group) {
                tex_aux_append_math_fence_val(tex_no_math_code(), tex_no_dict_code(), open_noad_subtype);
            }
            switch (cur_group) {
                case math_fence_group:
                    break;
                case math_inline_group:
                case math_display_group:
                case math_number_group:
                    tex_aux_scan_delimiter(null, no_mathcode, unset_noad_class);
                    if (st == middle_fence_side) {
                        tex_handle_error(
                            normal_error_type,
                            "Extra \\middle",
                            "I'm ignoring a \\middle that had no matching \\left."
                        );
                    } else {
                        tex_handle_error(
                            normal_error_type,
                            "Extra \\right",
                            "I'm ignoring a \\right that had no matching \\left."
                        );
                    }
                    break;
                default:
                    tex_off_save();
            }
    }
    /*tex
        Now we only have a no, left, middle or right case left.
    */
    {
        halfword fence = tex_new_node(fence_noad, st);
        halfword delimiter = tex_new_node(delimiter_node, 0);
        halfword autoclass = unset_noad_class;
        fence_delimiter_list(fence) = delimiter;
        noad_height(fence) = ht;
        noad_depth(fence) = dp;
        noad_options(fence) = options;
        set_noad_classes(fence, mainclass);
        if (leftclass != unset_noad_class) {
            set_noad_left_class(fence, leftclass);
        }
        if (rightclass != unset_noad_class) {
            set_noad_right_class(fence, rightclass);
        }
        noad_italic(fence) = 0;
        noad_source(fence) = source;
        /* */
        fence_top_overshoot(fence) = top;
        fence_bottom_overshoot(fence) = bottom;
        /*tex
            By setting this here, we can get rid of the hard coded values in |mlist_to_hlist| which
            sort of interfere (or at least confuse) things there. When set, the |leftclass| and
            |rightclass| settings win anyway.
        */
        if (mainclass == unset_noad_class) {
            mainclass = node_subtype(delimiter);
            if (mainclass == unset_noad_class || mainclass == ordinary_noad_subtype) {
                switch (st) {
                    case left_fence_side:
                        mainclass = open_noad_subtype;
                        break;
                    case middle_fence_side:
                        mainclass = middle_noad_subtype;
                        break;
                    case right_fence_side:
                        mainclass = close_noad_subtype;
                        break;
                }
            }
            set_noad_main_class(fence, mainclass);
        }
        /* */
        switch (st) {
            case left_fence_side:
                autoclass = open_noad_subtype;
                break;
            case middle_fence_side:
                autoclass = middle_noad_subtype; /* we need a way to overload this */
                break;
            case right_fence_side:
                autoclass = close_noad_subtype;
                break;
        }
        /* */
        tex_aux_scan_delimiter(delimiter, no_mathcode, autoclass);
        /* */
        if (attrlist) {
            tex_attach_attribute_list_attribute(fence, attrlist);
            tex_attach_attribute_list_attribute(delimiter, attrlist);
        }
        switch (st) {
            case left_fence_side:
                tex_aux_append_math_fence(fence, open_noad_subtype);
                break;
            case middle_fence_side:
                tex_aux_append_math_fence(fence, middle_noad_subtype);
                break;
            case right_fence_side:
                tex_aux_append_math_fence(fence, close_noad_subtype);
                break;
            case left_operator_side:
                {
                    halfword top = tex_new_node(sub_mlist_node, 0);
                    halfword bottom = tex_new_node(sub_mlist_node, 0);
                    fence_delimiter_top(fence) = top;
                    fence_delimiter_bottom(fence) = bottom;
                    tex_aux_push_math(math_fence_group, style);
                    node_next(cur_list.head) = fence;
                    cur_list.tail = fence;
                    cur_list.delimiter = fence;
                    tex_set_saved_record(saved_operator_item_variant, operator_variant_save_type, 0, math_limits_top);
                    lmt_save_state.save_stack_data.ptr += saved_operator_n_of_items;
                    tex_aux_push_math(math_operator_group, tex_math_style_variant(style, math_parameter_superscript_variant));
                    tex_scan_left_brace();
                }
                break;
            case no_fence_side:
                {
                    halfword n = tex_new_node(simple_noad, fenced_noad_subtype);
                    halfword l = tex_new_node(sub_mlist_node, 0);
                    tex_tail_append(n);
                    set_noad_main_class(n, mainclass); /*tex Really needed here! */
                    noad_nucleus(n) = l;
                    kernel_math_list(noad_nucleus(n)) = fence;
                }
                break;
            default:
                tex_confusion("left right fence");
                break;
        }
    }
}

/*tex

    \TEX\ gets to the following part of the program when the first |$| ending a display has been
    scanned.

*/

static void tex_aux_check_second_math_shift(void)
{
    tex_get_x_token();
    if (cur_cmd != math_shift_cmd) {
        tex_back_input(cur_tok);
        tex_handle_error(
            normal_error_type,
            "Display math should end with $$",
            "The '$' that I just saw supposedly matches a previous '$$'. So I shall assume\n"
            "that you typed '$$' both times."
        );
    }
}

static void tex_aux_check_display_math_end(void)
{
    switch (cur_chr) { 
        case end_display_math_code:
        case end_math_mode_code:
            return;
    }
    tex_handle_error(
        normal_error_type,
        "Display math should end with \\Ustopdisplaymath or \\Ustopmathmode",
        "I shall assume that you typed that."
    );
}

static void tex_aux_check_inline_math_end(void)
{
    switch (cur_chr) { 
        case end_inline_math_code:
        case end_math_mode_code:
            return;
    }
    tex_handle_error(
        normal_error_type,
        "Inline math should end with \\Ustopmath or \\Ustopmathmode",
        "I shall assume that you typed that."
    );
}

static void tex_aux_resume_after_display(void)
{
    switch (cur_group) {
        case math_display_group:
            tex_aux_unsave_math();
            cur_list.prev_graf += 3;
            tex_push_nest();
            cur_list.mode = hmode;
            cur_list.space_factor = default_space_factor;
            /*tex This needs to be intercepted in the display math start! Todo! */
            tex_tail_append(tex_new_par_node(penalty_par_subtype));
            tex_get_x_token();
            if (cur_cmd != spacer_cmd) {
                tex_back_input(cur_tok);
            }
            if (lmt_nest_state.nest_data.ptr == 1) {
                lmt_page_filter_callback(after_display_page_context, 0);
                tex_build_page();
            }
            break;
        default:    
            tex_confusion("finishing display math");
            break;
    }
}

/*tex

    The fuziest part of math mode processing occurs when a displayed formula is being centered and
    placed with an optional equation number. At this time we are in vertical mode (or internal
    vertical mode).

    \starttabulate
    \NC \type {p} \NC points to the mlist for the formula \NC \NR
    \NC \type {a} \NC is either |null| or it points to a box containing the equation number \NC \NR
    \NC \type {l} \NC is true if there was an |\leqno| (so |a| is a horizontal box) \NC \NR
    \stoptabulate

    Per 2022 we ditched display mode in \CONTEXT\ LMTX\ so the code related to display math is now
    completely frozen, if only because testing has become unreasonable. There is anyway not much more
    to do here.

*/

static void tex_aux_inject_display_skip(quarterword param, quarterword subtype)
{
    if (param > 0) {
        switch (display_skip_mode_par) {
            case display_skip_default :
            case display_skip_always :
                break;
            case display_skip_non_zero:
                if (tex_glue_is_zero(glue_parameter(param))) {
                    return;
                } else {
                    break;
                }
            case display_skip_ignore:
                return;
            default:
                /*tex > 3 reserved for future use */
                break;
        }
        tex_tail_append(tex_new_param_glue_node(param, subtype));
    }
}

static void tex_aux_finish_displayed_math(int atleft, halfword eqnumber, halfword equation)
{
    /*tex box containing the equation */
    halfword equation_box;
    /*tex width of the equation */
    scaled equation_width;
    /*tex width of the line */
    scaled line_width;
    /*tex width of equation number */
    scaled number_width;
    /*tex width of equation number plus space to separate from equation */
    scaled number_plus_gap_width;
    /*tex move the line right this much */
    scaled indent;
    /*tex displacement of equation in the line */
    scaled displacement;
    /*tex glue parameter codes for before and after */
    quarterword glue_above, glue_below;
    /*tex glue parameter subtypes for before and after */
    quarterword subtype_above, subtype_below;
    /*tex for equation numbers */
    scaled eqno_width;
    /*tex true if the math and surrounding (par) dirs are different */
    int swap_dir = math_direction_par != pre_display_direction_par;
    if (eqnumber && swap_dir) {
        atleft = ! atleft;
    }
    /* */
    lmt_packaging_state.post_adjust_tail = post_adjust_head;
    lmt_packaging_state.pre_adjust_tail = pre_adjust_head;
    lmt_packaging_state.post_migrate_tail = post_migrate_head;
    lmt_packaging_state.pre_migrate_tail = pre_migrate_head;
    /* */
    equation_box = tex_hpack(equation, 0, packing_additional, direction_unknown, holding_none_option);
    node_subtype(equation_box) = equation_list;
    attach_current_attribute_list(equation_box);
    equation = box_list(equation_box);
    /* */
    equation_width = box_width(equation_box);
    line_width = display_width_par;
    indent = display_indent_par;
    if (eqnumber) {
        number_width = box_width(eqnumber);
        eqno_width = number_width;
        number_plus_gap_width = number_width + tex_round_xn_over_d(math_eqno_gap_step_par, tex_get_math_quad_style(text_style), scaling_factor);
        node_subtype(eqnumber) = equation_number_list;
        /*tex attach_current_attribute_list(eqno_box); */
    } else {
        number_width = 0;
        eqno_width = 0;
        number_plus_gap_width = 0;
    }
    if (equation_width + number_plus_gap_width > line_width) {
        /*tex

            The user can force the equation number to go on a separate line by causing its width to
            be zero.

        */
        if ((number_width != 0) && ((equation_width - lmt_packaging_state.total_shrink[normal_glue_order] + number_plus_gap_width <= line_width)
                || (lmt_packaging_state.total_shrink[fi_glue_order] != 0)
                || (lmt_packaging_state.total_shrink[fil_glue_order] != 0)
                || (lmt_packaging_state.total_shrink[fill_glue_order] != 0)
                || (lmt_packaging_state.total_shrink[filll_glue_order] != 0))) {
            box_list(equation_box) = null;
            tex_flush_node(equation_box);
            equation_box = tex_hpack(equation, line_width - number_plus_gap_width, packing_exactly, direction_unknown, holding_none_option);
            node_subtype(equation_box) = equation_list;
            attach_current_attribute_list(equation_box);
        } else {
            number_width = 0;
            if (equation_width > line_width) {
                box_list(equation_box) = null;
                tex_flush_node(equation_box);
                equation_box = tex_hpack(equation, line_width, packing_exactly, direction_unknown, holding_none_option);
                node_subtype(equation_box) = equation_list;
                attach_current_attribute_list(equation_box);
            }
        }
        equation_width = box_width(equation_box);
    }
    /*tex

        We try first to center the display without regard to the existence of the equation number.
        If that would make it too close (where \quote {too close} means that the space between
        display and equation number is less than the width of the equation number), we either
        center it in the remaining space or move it as far from the equation number as possible.
        The latter alternative is taken only if the display begins with glue, since we assume that
        the user put glue there to control the spacing precisely.

    */
    displacement = tex_half_scaled(line_width - equation_width);
    if ((number_width > 0) && (displacement < 2 * number_width)) {
        /*tex too close */
        displacement = tex_half_scaled(line_width - equation_width - number_width);
        /*
        if (p && !is_char_node(p) && node_type(p) == glue_node)
            d = 0;
        */ /* kind of weird this, so why not just */
        if (equation && node_type(equation) == glue_node) {
            displacement = 0;
        }
    }
    tex_tail_append(tex_new_penalty_node(pre_display_penalty_par, before_display_penalty_subtype));
    if ((displacement + indent <= pre_display_size_par) || ((cur_list.math_dir == dir_lefttoright) &&   atleft)
                                                        || ((cur_list.math_dir == dir_righttoleft) && ! atleft)) {
        /*tex not enough clearance */
        glue_above = above_display_skip_code;
        subtype_above = above_display_skip_glue;
        glue_below = below_display_skip_code;
        subtype_below = below_display_skip_glue;
    } else {
        glue_above = above_display_short_skip_code;
        subtype_above = above_display_short_skip_glue;
        glue_below = below_display_short_skip_code;
        subtype_below = below_display_short_skip_glue;
    }
    /*tex

        If the equation number is set on a line by itself, either before or after the formula, we
        append an infinite penalty so that no page break will separate the display from its number;
        and we use the same size and displacement for all three potential lines of the display,
        even though |\parshape| may specify them differently; |\leqno| on a forced single line due
        to |width=0|; it follows that |type(a) = hlist_node|.

    */
    if (eqnumber && atleft && (number_width == 0)) {
     /* if (math_direction_par == dir_lefttoright) { */
            box_shift_amount(eqnumber) = 0;
     /* } else { */
     /* } */
        tex_append_to_vlist(eqnumber, lua_key_index(equation_number), NULL);
        tex_tail_append(tex_new_penalty_node(infinite_penalty, equation_number_penalty_subtype));
    } else {
        tex_aux_inject_display_skip(glue_above, subtype_above);
    }
    if (number_width != 0) {
        scaled shift = line_width - equation_width - number_width - displacement;
        halfword move = tex_new_kern_node(shift, explicit_kern_subtype);
        if (atleft) {
            if (swap_dir) {
                if (math_direction_par == dir_lefttoright) {
                    /*tex TRT + TLT + \eqno: (swap_dir=true,  math_direction_par=TLT, l=true) */
                    halfword kern = tex_new_kern_node(shift + number_width, explicit_kern_subtype);
                    tex_try_couple_nodes(eqnumber, move);
                    tex_try_couple_nodes(move, equation_box);
                    tex_try_couple_nodes(equation_box, kern);
                } else {
                    /*tex TLT + TRT + \eqno: (swap_dir=true,  math_direction_par=TRT, l=true) */
                    tex_try_couple_nodes(eqnumber, move);
                    tex_try_couple_nodes(move, equation_box);
                }
            } else {
                halfword kern;
                if (math_direction_par == dir_lefttoright) {
                    /*tex TLT + TLT + \leqno: (swap_dir=false, math_direction_par=TLT, l=true) */
                    kern = tex_new_kern_node(shift + number_width, explicit_kern_subtype);
                } else {
                    /*tex TRT + TRT + \leqno: (swap_dir=false, math_direction_par=TRT, l=true) */
                    kern = tex_new_kern_node(shift, explicit_kern_subtype);
                }
                tex_try_couple_nodes(eqnumber, move);
                tex_try_couple_nodes(move, equation_box);
                tex_try_couple_nodes(equation_box, kern);
            }
            equation_box = eqnumber;
        } else {
            if (swap_dir) {
                if (math_direction_par == dir_lefttoright) {
                    /*tex TRT + TLT + \leqno: (swap_dir=true,  math_direction_par=TLT, l=false) */
                } else {
                    /*tex TLT + TRT + \leqno: (swap_dir=true,  math_direction_par=TRT, l=false) */
                }
                tex_try_couple_nodes(equation_box, move);
                tex_try_couple_nodes(move, eqnumber);
            } else {
                halfword kern;
                if (math_direction_par == dir_lefttoright) {
                    /*tex TLT + TLT + \eqno: (swap_dir=false, math_direction_par=TLT, l=false) */
                    kern = tex_new_kern_node(displacement, explicit_kern_subtype);
                } else {
                    /*tex TRT + TRT + \eqno: (swap_dir=false, math_direction_par=TRT, l=false) */
                    kern = tex_new_kern_node(shift + number_width, explicit_kern_subtype);
                }
                tex_try_couple_nodes(kern, equation_box);
                tex_try_couple_nodes(equation_box, move);
                tex_try_couple_nodes(move, eqnumber);
                equation_box = kern;
            }
        }
        equation_box = tex_hpack(equation_box, 0, packing_additional, direction_unknown, holding_none_option);
        node_subtype(equation_box) = equation_list; /* new */
        attach_current_attribute_list(equation_box);
        box_shift_amount(equation_box) = indent;
    } else {
        box_shift_amount(equation_box) = indent + displacement;
    }
    /* */
    if (pre_adjust_head != lmt_packaging_state.pre_adjust_tail) {
        tex_inject_adjust_list(pre_adjust_head, 1, lmt_linebreak_state.just_box, NULL);
    }
    lmt_packaging_state.pre_adjust_tail = null;
    /* Pre-migrate content (callback). */
    if (pre_migrate_head != lmt_packaging_state.pre_migrate_tail) {
        tex_append_list(pre_migrate_head, lmt_packaging_state.pre_migrate_tail);
     // if (! lmt_page_builder_state.output_active) {
     //     lmt_append_line_filter_callback(pre_migrate_append_line_context, 0);
     // }
    }
    /* */
    tex_append_to_vlist(equation_box, lua_key_index(equation), NULL); /* eqbox has the formula */
    if (eqnumber && number_width == 0 && ! atleft) {
        tex_tail_append(tex_new_penalty_node(infinite_penalty, equation_number_penalty_subtype));
     /* if (math_direction_par == dir_lefttoright) { */
            box_shift_amount(eqnumber) = indent + line_width - eqno_width ;
     /* } else { */
     /* } */
        tex_append_to_vlist(eqnumber, lua_key_index(equation_number), NULL);
        glue_below = 0; /* shouldn't this be an option */
    }
    /* */
    if (post_migrate_head != lmt_packaging_state.post_migrate_tail) {
        tex_append_list(post_migrate_head, lmt_packaging_state.post_migrate_tail);
        // if (! lmt_page_builder_state.output_active) {
        //     lmt_append_line_filter_callback(post_migrate_append_line_context, 0);
        // }
    }
    lmt_packaging_state.post_migrate_tail = null;
    if (lmt_packaging_state.post_adjust_tail) {
        if (post_adjust_head != lmt_packaging_state.post_adjust_tail) {
            tex_inject_adjust_list(post_adjust_head, 1, null, NULL);
        }
        lmt_packaging_state.post_adjust_tail = null;
    }
    /* */
    tex_tail_append(tex_new_penalty_node(post_display_penalty_par, after_display_penalty_subtype));
    tex_aux_inject_display_skip(glue_below, subtype_below);
    tex_aux_resume_after_display();
}

/*tex

    A |math_node|, which occurs only in horizontal lists, appears before and after mathematical
    formulas. The |subtype| field is |before| before the formula and |after| after it. There is a
    |surround| field, which represents the amount of surrounding space inserted by |\mathsurround|.

    As an outcome of the math upgrading sub project that Mikael Sundqvist and I undertook end 2021
    and beginning 2022 Mikael suggested penalties surrounding inline formulas so there you have it:
    |\preinlinepanelty| and |\postinlinepanelty|.

*/

/* make propper mappers (see 5967 in texmlist) */

static inline int tex_aux_class_from_glyph(halfword n) {
    return node_subtype(n) - (node_subtype(n) > glyph_math_extra_subtype  ? glyph_math_extra_subtype : glyph_math_ordinary_subtype);
}

static inline int tex_aux_class_from_list(halfword n) {
    switch (node_subtype(n)) { 
        case math_fraction_list: 
            return fraction_noad_subtype;
        case math_accent_list: 
            return accent_noad_subtype;
        case math_radical_list: 
            return radical_noad_subtype;
        default: 
            return 0;
    }
}

static int tex_aux_short_math(halfword m)
{
 // tex_show_node_list(m,10000,10000);
    if (m) { 
        /*tex 
            These are the cases we catch, the class option drives succes. 

            \starttyping
            kern[] glyph[subtype -> class] vlist[scripts] kern[] 
                   hlist[subtype -> class] 
                   vlist[subtype -> class] 
            \stoptyping
            
        */
        switch (node_type(m)) { 
            case kern_node:
                /*tex Do we need to test for some size here? */
                m = node_next(m); 
                break;
            case hlist_node:
            case vlist_node:
                /*tex 
                    These are actually more extensive constructs that we don't want to analyze any 
                    further (for being single characters). 
                */
                if (! node_next(m) && tex_math_has_class_option(tex_aux_class_from_list(m), short_inline_class_option)) {
                    scaled threshold = short_inline_math_threshold_par;
                    if (threshold > 0 && box_width(m) <= threshold) {
                        return 1;
                    }
                }
                return 0;
        } 
        /*tex We don't have a list so we check for a list now. */
        if (m) { 
            switch (node_type(m)) { 
                case glyph_node: 
                    if (tex_math_has_class_option(tex_aux_class_from_glyph(m), short_inline_class_option)) {
                        m = node_next(m);
                        break;
                    } else { 
                        return 0;
                    }
                default: 
                    return 0;
                }
        } else { 
            return 0;
        }
        /*tex We accept optional sub, super or combined scripts. */
        if (m) { 
            switch (node_type(m)) { 
                case vlist_node:
                case hlist_node:
                    switch (node_subtype(m)) { 
                        case math_sup_list:        /* in hlist */
                        case math_sub_list:        /* in hlist */ 
                        case math_pre_post_list:   /* in vlist */
                        case math_scripts_list:    /* in vlist */
                           m = node_next(m);
                           break;
                    }
                /* default */
            }
        } 
        /*tex We ignore trailing kerns (for now). We could test for size. */
        if (m && node_type(m) == kern_node) {
            m = node_next(m);
        } 
        return ! m;
    }
    return 0;
}

void tex_run_math_shift(void) 
{
    switch (cur_group) {
        case math_inline_group:
        case math_display_group:
        case math_number_group:
            {
                /*tex box containing equation number */
                halfword eqnumber = null;
                /*tex Use |\leqno| instead of |\eqno|, we default to right. */
                int atleft = 0;
                /*tex |mmode| or |-mmode| */
                int mode = cur_list.mode;
                int mathmode = cur_list.math_mode; 
                /*tex this pops the nest, the formula */
                halfword mathlist = tex_aux_finish_math_list(null);
                int mathleft = cur_list.math_begin;
                int mathright = cur_list.math_end;
                if (cur_cmd == math_shift_cs_cmd) { 
                    switch (cur_chr) { 
                        case begin_inline_math_code:
                        case begin_display_math_code:
                        case begin_math_mode_code:
                            tex_you_cant_error(NULL);
                            break;
                    }
                }
                if (cur_list.mode == -mode) { // todo: symbolic 
                    /*tex end of equation number */
                  AGAIN:
                    switch (cur_cmd) {
                        case math_shift_cmd:
                            tex_aux_check_second_math_shift();
                            break;
                        case end_paragraph_cmd:
                            tex_get_x_token();
                            goto AGAIN;
                        default:
                            tex_aux_check_display_math_end();
                            break;
                    }
                    tex_run_mlist_to_hlist(mathlist, 0, text_style, unset_noad_class, unset_noad_class);
                    eqnumber = tex_hpack(node_next(temp_head), 0, packing_additional, direction_unknown, holding_none_option);
                    attach_current_attribute_list(eqnumber);
                    tex_aux_unsave_math();
                    /*tex now |cur_group = math_shift_group| */
                    lmt_save_state.save_stack_data.ptr -= saved_equation_number_n_of_items;
                    if (saved_type(saved_equation_number_item_location) == equation_number_location_save_type) {
                        atleft = saved_value(saved_equation_number_item_location) == left_location_code;
                        mode = cur_list.mode;
                        mathlist = tex_aux_finish_math_list(null);
                    } else {
                        tex_confusion("after math");
                    }
                }
                if (mode == inline_mmode) { 
             // if (mode < 0) {
                    /*tex

                        The |unsave| is done after everything else here; hence an appearance of |\mathsurround|
                        inside of |$...$| affects the spacing at these particular |$|'s. This is consistent
                        with the conventions of |$$ ... $$|, since |\abovedisplayskip| inside a display affects
                        the space above that display.

                    */
                    halfword beginmath = tex_new_node(math_node, begin_inline_math);
                    halfword endmath = tex_new_node(math_node, end_inline_math);
                    halfword shortmath = 0;
                    if (mathmode) { 
                        switch (cur_cmd) { 
                            case math_shift_cs_cmd: 
                                if (cur_chr != end_display_math_code && cur_chr != end_math_mode_code) {
                                    tex_aux_check_second_math_shift();
                                }
                                break;
                            case math_shift_cmd: 
                                tex_aux_check_second_math_shift();
                                break;
                        }
                    } else if (cur_cmd == math_shift_cs_cmd) {
                        tex_aux_check_inline_math_end();
                    }
                    tex_tail_append(beginmath);
                    if (pre_inline_penalty_par != max_integer) {
                        math_penalty(beginmath) = pre_inline_penalty_par;
                    }
                    /*tex begin mathskip code */
                    switch (math_skip_mode_par) {
                        case math_skip_surround_when_zero:
                            if (! tex_glue_is_zero(math_skip_par)) {
                                tex_copy_glue_values(beginmath, math_skip_par);
                            } else {
                                math_surround(beginmath) = math_surround_par;
                            }
                            break ;
                        case math_skip_always_left:
                        case math_skip_always_both:
                        case math_skip_only_when_skip:
                            tex_copy_glue_values(beginmath, math_skip_par);
                            break ;
                        case math_skip_always_right:
                        case math_skip_ignore:
                            break ;
                        case math_skip_always_surround:
                        default:
                            math_surround(beginmath) = math_surround_par;
                            break;
                    }
                    /*tex end mathskip code */
                    if (cur_list.math_dir) {
                        tex_tail_append(tex_new_dir(normal_dir_subtype, math_direction_par)); 
                    }
                    tex_run_mlist_to_hlist(mathlist, cur_list.mode > nomode, is_valid_math_style(cur_list.math_main_style) ?  cur_list.math_main_style : text_style, cur_list.math_begin, cur_list.math_end);
                    shortmath = tex_aux_short_math(node_next(temp_head));
                    tex_try_couple_nodes(cur_list.tail, node_next(temp_head));
                    cur_list.tail = tex_tail_of_node_list(cur_list.tail);
                    if (cur_list.math_dir) {
                        tex_tail_append(tex_new_dir(cancel_dir_subtype, math_direction_par));
                    }
                    cur_list.math_dir = 0;
                    tex_tail_append(endmath);
                    /* */
                    if (post_inline_penalty_par != max_integer) {
                        math_penalty(endmath) = post_inline_penalty_par;
                    }
                    /*tex begin mathskip code */
                    switch (math_skip_mode_par) {
                        case math_skip_surround_when_zero :
                            if (! tex_glue_is_zero(math_skip_par)) {
                                tex_copy_glue_values(endmath, math_skip_par);
                                math_surround(endmath) = 0;
                            } else {
                                math_surround(endmath) = math_surround_par;
                            }
                            break;
                        case math_skip_always_right:
                        case math_skip_always_both:
                        case math_skip_only_when_skip:
                            tex_copy_glue_values(endmath, math_skip_par);
                            break;
                        case math_skip_always_left:
                        case math_skip_ignore:
                            break;
                        case math_skip_always_surround:
                        default:
                            math_surround(endmath) = math_surround_par;
                            break;
                    }
                    /*tex end mathskip code */
                    if (shortmath) {
                        if (pre_short_inline_penalty_par != max_integer) {
                            math_penalty(beginmath) = pre_short_inline_penalty_par;
                        }
                        if (post_short_inline_penalty_par != max_integer) {
                            math_penalty(endmath) = post_short_inline_penalty_par;
                        }
                        tex_add_math_option(beginmath, glue_option_short_math);
                        tex_add_math_option(endmath, glue_option_short_math);
                    }
                    cur_list.space_factor = default_space_factor;
                    mathleft = cur_list.math_begin;
                    mathright = cur_list.math_end;
                    tex_aux_unsave_math();
                } else {
                    if (! eqnumber) {
                        if (cur_cmd == math_shift_cmd) {
                            tex_aux_check_second_math_shift();
                        } else {
                            tex_aux_check_display_math_end();
                        }
                    }
                    tex_run_mlist_to_hlist(mathlist, 0, display_style, cur_list.math_begin, cur_list.math_end);
                    mathleft = cur_list.math_begin;
                    mathright = cur_list.math_end;
                    tex_aux_finish_displayed_math(atleft, eqnumber, node_next(temp_head));
                }
                /* local */
                update_tex_math_left_class(mathleft);
                update_tex_math_right_class(mathright);
                /* global */
                lmt_math_state.last_left = mathleft;
                lmt_math_state.last_right = mathright;
            }
            break;
        default:
            tex_off_save();
            break;
    }
}

/*tex

    When |\halign| appears in a display, the alignment routines operate essentially as they do in
    vertical mode. Then the following program is activated, with |p| and |q| pointing to the
    beginning and end of the resulting list, and with |aux_save| holding the |prev_depth| value.

*/

void tex_finish_display_alignment(halfword head, halfword tail, halfword prevdepth)
{
    tex_handle_assignments();
  AGAIN:
    switch (cur_cmd) {
        case math_shift_cmd:
            tex_aux_check_second_math_shift();
            break;
        case end_paragraph_cmd:
            tex_get_x_token();
            goto AGAIN;
        default:
            tex_aux_check_display_math_end();
            break;
    }
    tex_pop_nest();
    tex_tail_append(tex_new_penalty_node(pre_display_penalty_par, before_display_penalty_subtype));
    tex_aux_inject_display_skip(above_display_skip_code, above_display_skip_glue);
    node_next(cur_list.tail) = head;
    if (head && tail) {
        cur_list.tail = tail;
    }
    tex_tail_append(tex_new_penalty_node(post_display_penalty_par, after_display_penalty_subtype));
    tex_aux_inject_display_skip(below_display_skip_code, below_display_skip_glue);
    cur_list.prev_depth = prevdepth;
    tex_aux_resume_after_display();
}

/*

    Turning macros into functions brought the mingw64 bin down from 2548224 to 2511360 bytes but
    not the linux one, so I guess mingw doesn't inline (yet, in 2020).

*/

static void tex_aux_define_inl_math_parameters(int size, int param, scaled value, int level)
{
    switch (size) {
        case script_size:
            tex_def_math_parameter(script_style, param, value, level, indirect_math_regular);
            tex_def_math_parameter(cramped_script_style, param, value, level, indirect_math_regular);
            break;
        case script_script_size:
            tex_def_math_parameter(script_script_style, param, value, level, indirect_math_regular);
            tex_def_math_parameter(cramped_script_script_style, param, value, level, indirect_math_regular);
            break;
        default:
            tex_def_math_parameter(text_style, param, value, level, indirect_math_regular);
            tex_def_math_parameter(cramped_text_style, param, value, level, indirect_math_regular);
            break;
    }
}

static void tex_aux_define_dis_math_parameters(int size, int param, scaled value, int level)
{
    if (size == text_size) {
        tex_def_math_parameter(display_style, param, value, level, indirect_math_regular);
        tex_def_math_parameter(cramped_display_style, param, value, level, indirect_math_regular);
    }
}

/*tex
    In principle we could save some storage in the format file with:

    \starttyping
    static void tex_aux_define_all_math_parameters(int size, int param, scaled value, int level)
    {
        tex_def_math_parameter(all_math_styles, param, value, level, indirect_math_regular);
    } 
    \stoptyping

    and then do this (we also need to move |all_math_styles| up in the enum to keep ranges compact):

    \starttyping
    if (! sa_get_item_8(lmt_math_state.par_head, (param + (math_parameter_max_range * style)), &item1, &item2)) {
        sa_get_item_8(lmt_math_state.par_head, (param + (math_parameter_max_range * all_math_styles)), &item1, &item2);
    }
    \stoptyping

    but in practice we actually get a bit larger file. 
*/

static void tex_aux_define_all_math_parameters(int size, int param, scaled value, int level)
{
    switch (size) {
        case script_size:
            tex_def_math_parameter(script_style, param, value, level, indirect_math_regular);
            tex_def_math_parameter(cramped_script_style, param, value, level, indirect_math_regular);
            break;
        case script_script_size:
            tex_def_math_parameter(script_script_style, param, value, level, indirect_math_regular);
            tex_def_math_parameter(cramped_script_script_style, param, value, level, indirect_math_regular);
            break;
        default:
            tex_def_math_parameter(text_style, param, value, level, indirect_math_regular);
            tex_def_math_parameter(cramped_text_style, param, value, level, indirect_math_regular);
            tex_def_math_parameter(display_style, param, value, level, indirect_math_regular);
            tex_def_math_parameter(cramped_display_style, param, value, level, indirect_math_regular);
            break;
    }
}

/*tex

    Here are the math parameters that are font-dependant. Before an mlist is converted to an hlist,
    \TEX\ makes sure that the fonts in family~2 have enough parameters to be math symbol fonts, and
    that the fonts in family~3 have enough parameters to be math extension fonts. The math-symbol
    parameters are referred to by using the following macros, which take a size code as their
    parameter; for example, |num1 (cur_size)| gives the value of the |num1| parameter for the
    current size.

    The math extension parameters have similar macros, but the size code is omitted (since it is
    always |cur_size| when we refer to such parameters).

*/

# define total_mathsy_parameters 22
# define total_mathex_parameters 13

# define mathsy(A,B) font_parameter(tex_fam_fnt(2,A),B)
# define mathex(A,B) font_parameter(tex_fam_fnt(3,A),B)

# define math_x_height(A)          mathsy(A,5)  /*tex height of |x| */
# define math_quad(A)              mathsy(A,6)  /*tex |18mu| */
# define num1(A)                   mathsy(A,8)  /*tex numerator shift-up in display styles */
# define num2(A)                   mathsy(A,9)  /*tex numerator shift-up in non-display, non-|\atop| */
# define num3(A)                   mathsy(A,10) /*tex numerator shift-up in non-display |\atop| */
# define denom1(A)                 mathsy(A,11) /*tex denominator shift-down in display styles */
# define denom2(A)                 mathsy(A,12) /*tex denominator shift-down in non-display styles */
# define sup1(A)                   mathsy(A,13) /*tex superscript shift-up in uncramped display style */
# define sup2(A)                   mathsy(A,14) /*tex superscript shift-up in uncramped non-display */
# define sup3(A)                   mathsy(A,15) /*tex superscript shift-up in cramped styles */
# define sub1(A)                   mathsy(A,16) /*tex subscript shift-down if superscript is absent */
# define sub2(A)                   mathsy(A,17) /*tex subscript shift-down if superscript is present */
# define sup_drop(A)               mathsy(A,18) /*tex superscript baseline below top of large box */
# define sub_drop(A)               mathsy(A,19) /*tex subscript baseline below bottom of large box */
# define delim1(A)                 mathsy(A,20) /*tex size of |\atopwithdelims| delimiters in display styles */
# define delim2(A)                 mathsy(A,21) /*tex size of |\atopwithdelims| delimiters in non-displays */
# define axis_height(A)            mathsy(A,22) /*tex height of fraction lines above the baseline */

# define default_rule_thickness(A) mathex(A,8)  /*tex thickness of |\over| bars */
# define big_operator_spacing1(A)  mathex(A,9)  /*tex minimum clearance above a displayed op */
# define big_operator_spacing2(A)  mathex(A,10) /*tex minimum clearance below a displayed op */
# define big_operator_spacing3(A)  mathex(A,11) /*tex minimum baselineskip above displayed op */
# define big_operator_spacing4(A)  mathex(A,12) /*tex minimum baselineskip below displayed op */
# define big_operator_spacing5(A)  mathex(A,13) /*tex padding above and below displayed limits */

/*tex
    Somehow a scale > scaling_factor results in extreme values.
*/

/*
inline static int tex_aux_get_font_math_parameter(scaled scale, halfword f, int id)
{
    scaled v = get_font_math_par(f, id);
//  return scale == scaling_factor ? v : round_xn_over_d(v, scale, scaling_factor);
    if (v) {
        double d = 0.001 * scale * v;
        return (d < 0.0) ? (int) (d - 0.5) : (int) (d + 0.5);
    } else {
        return 0;
    }
}

inline static int tex_aux_get_font_math_quantity(scaled scale, halfword v)
{
//   return scale == scaling_factor ? v : round_xn_over_d(v, scale, scaling_factor);
    if (v) {
        double d = 0.001 * scale * v;
        return (d < 0.0) ? (int) (d - 0.5) : (int) (d + 0.5);
    } else {
        return 0;
    }
}
*/

# define math_parameter(a,b) ((font_math_parameter_count(a) >= b) ? font_math_parameter(a,b) : undefined_math_parameter)

inline static scaled tex_aux_get_font_math_parameter(scaled scale, halfword f, int id)
{
    scaled v = math_parameter(f, id);
    if (v == undefined_math_parameter) {
        return v;
    } else {
        return v ? scaledround(0.001 * scale * v) : 0;
    }
}

inline static scaled tex_aux_get_font_math_quantity(scaled scale, halfword v)
{
    return v ? scaledround(0.001 * scale * v) : 0;
}

/*tex
    The next function is called when we define a family, but first we define a few helpers
    for identifying traditional math fonts. Watch the hard codes family check (gone now)!
*/

void tex_fixup_math_parameters(int fam, int size, int f, int level)
{
    scaled scale = tex_get_math_font_scale(f, size);

    if (tracing_math_par > 1) {
        tex_begin_diagnostic();
        tex_print_format("[math: fixing up font, family %i, size %i, font %i, level %i]", fam, size, f, level);
        tex_end_diagnostic();
    }

    /*tex These apply to all: */

    tex_aux_define_all_math_parameters(size, math_parameter_quad, tex_aux_get_font_math_quantity (scale, font_size(f)),  level);
    tex_aux_define_all_math_parameters(size, math_parameter_axis, tex_aux_get_font_math_parameter(scale, f, AxisHeight), level);

    tex_aux_define_all_math_parameters(size, math_parameter_accent_base_height,               tex_aux_get_font_math_parameter(scale, f, AccentBaseHeight),                  level);
    tex_aux_define_all_math_parameters(size, math_parameter_accent_base_depth,                tex_aux_get_font_math_parameter(scale, f, AccentBaseDepth),                   level); /* engine, reserved */
    tex_aux_define_all_math_parameters(size, math_parameter_flattened_accent_base_height,     tex_aux_get_font_math_parameter(scale, f, FlattenedAccentBaseHeight),         level);
    tex_aux_define_all_math_parameters(size, math_parameter_flattened_accent_base_depth,      tex_aux_get_font_math_parameter(scale, f, FlattenedAccentBaseDepth),          level); /* engine, reserved */
    tex_aux_define_all_math_parameters(size, math_parameter_overbar_kern,                     tex_aux_get_font_math_parameter(scale, f, OverbarExtraAscender),              level);
    tex_aux_define_all_math_parameters(size, math_parameter_overbar_rule,                     tex_aux_get_font_math_parameter(scale, f, OverbarRuleThickness),              level);
    tex_aux_define_all_math_parameters(size, math_parameter_overbar_vgap,                     tex_aux_get_font_math_parameter(scale, f, OverbarVerticalGap),                level);
    tex_aux_define_all_math_parameters(size, math_parameter_underbar_kern,                    tex_aux_get_font_math_parameter(scale, f, UnderbarExtraDescender),            level);
    tex_aux_define_all_math_parameters(size, math_parameter_underbar_rule,                    tex_aux_get_font_math_parameter(scale, f, UnderbarRuleThickness ),            level);
    tex_aux_define_all_math_parameters(size, math_parameter_underbar_vgap,                    tex_aux_get_font_math_parameter(scale, f, UnderbarVerticalGap),               level);
    tex_aux_define_all_math_parameters(size, math_parameter_under_delimiter_vgap,             tex_aux_get_font_math_parameter(scale, f, StretchStackGapAboveMin),           level);
    tex_aux_define_all_math_parameters(size, math_parameter_under_delimiter_bgap,             tex_aux_get_font_math_parameter(scale, f, StretchStackBottomShiftDown),       level);
    tex_aux_define_all_math_parameters(size, math_parameter_over_delimiter_vgap,              tex_aux_get_font_math_parameter(scale, f, StretchStackGapBelowMin),           level);
    tex_aux_define_all_math_parameters(size, math_parameter_over_delimiter_bgap,              tex_aux_get_font_math_parameter(scale, f, StretchStackTopShiftUp),            level);
    tex_aux_define_all_math_parameters(size, math_parameter_radical_kern,                     tex_aux_get_font_math_parameter(scale, f, RadicalExtraAscender),              level);
    tex_aux_define_all_math_parameters(size, math_parameter_radical_rule,                     tex_aux_get_font_math_parameter(scale, f, RadicalRuleThickness),              level);
    tex_aux_define_all_math_parameters(size, math_parameter_radical_degree_before,            tex_aux_get_font_math_parameter(scale, f, RadicalKernBeforeDegree),           level);
    tex_aux_define_all_math_parameters(size, math_parameter_radical_degree_after,             tex_aux_get_font_math_parameter(scale, f, RadicalKernAfterDegree),            level);
    tex_aux_define_all_math_parameters(size, math_parameter_subscript_shift_drop,             tex_aux_get_font_math_parameter(scale, f, SubscriptBaselineDropMin),          level);
    tex_aux_define_all_math_parameters(size, math_parameter_superscript_shift_drop,           tex_aux_get_font_math_parameter(scale, f, SuperscriptBaselineDropMax),        level);
    tex_aux_define_all_math_parameters(size, math_parameter_subscript_shift_down,             tex_aux_get_font_math_parameter(scale, f, SubscriptShiftDown),                level);
    tex_aux_define_all_math_parameters(size, math_parameter_prime_shift_drop,                 tex_aux_get_font_math_parameter(scale, f, PrimeBaselineDropMax),              level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_subscript_top_max,                tex_aux_get_font_math_parameter(scale, f, SubscriptTopMax),                   level);
    tex_aux_define_all_math_parameters(size, math_parameter_superscript_bottom_min,           tex_aux_get_font_math_parameter(scale, f, SuperscriptBottomMin),              level);
    tex_aux_define_all_math_parameters(size, math_parameter_superscript_subscript_bottom_max, tex_aux_get_font_math_parameter(scale, f, SuperscriptBottomMaxWithSubscript), level);
    tex_aux_define_all_math_parameters(size, math_parameter_subscript_superscript_vgap,       tex_aux_get_font_math_parameter(scale, f, SubSuperscriptGapMin),              level);
    tex_aux_define_all_math_parameters(size, math_parameter_limit_above_vgap,                 tex_aux_get_font_math_parameter(scale, f, UpperLimitGapMin),                  level);
    tex_aux_define_all_math_parameters(size, math_parameter_limit_above_bgap,                 tex_aux_get_font_math_parameter(scale, f, UpperLimitBaselineRiseMin),         level);
    tex_aux_define_all_math_parameters(size, math_parameter_limit_below_vgap,                 tex_aux_get_font_math_parameter(scale, f, LowerLimitGapMin),                  level);
    tex_aux_define_all_math_parameters(size, math_parameter_limit_below_bgap,                 tex_aux_get_font_math_parameter(scale, f, LowerLimitBaselineDropMin),         level);
    tex_aux_define_all_math_parameters(size, math_parameter_nolimit_sub_factor,               tex_aux_get_font_math_parameter(scale, f, NoLimitSubFactor),                  level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_nolimit_sup_factor,               tex_aux_get_font_math_parameter(scale, f, NoLimitSupFactor),                  level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_skewed_fraction_hgap,             tex_aux_get_font_math_parameter(scale, f, SkewedFractionHorizontalGap),       level);
    tex_aux_define_all_math_parameters(size, math_parameter_skewed_fraction_vgap,             tex_aux_get_font_math_parameter(scale, f, SkewedFractionVerticalGap),         level);
    tex_aux_define_all_math_parameters(size, math_parameter_space_before_script,              tex_aux_get_font_math_parameter(scale, f, SpaceBeforeScript),                 level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_space_after_script,               tex_aux_get_font_math_parameter(scale, f, SpaceAfterScript),                  level);
    tex_aux_define_all_math_parameters(size, math_parameter_connector_overlap_min,            tex_aux_get_font_math_parameter(scale, f, MinConnectorOverlap),               level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_fraction_rule,                    tex_aux_get_font_math_parameter(scale, f, FractionRuleThickness),             level);

    tex_aux_define_all_math_parameters(size, math_parameter_radical_degree_raise,               math_parameter(f, RadicalDegreeBottomRaisePercent), level);
    tex_aux_define_all_math_parameters(size, math_parameter_prime_raise,                        math_parameter(f, PrimeRaisePercent),               level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_prime_raise_composed,               math_parameter(f, PrimeRaiseComposedPercent),       level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_prime_space_after,                  math_parameter(f, PrimeSpaceAfter),                 level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_prime_width,                        math_parameter(f, PrimeWidthPercent),               level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_skewed_delimiter_tolerance,         math_parameter(f, SkewedDelimiterTolerance),        level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_accent_top_shift_up,                math_parameter(f, AccentTopShiftUp),                level); /* engine, undefined */
    tex_aux_define_all_math_parameters(size, math_parameter_accent_bottom_shift_down,           math_parameter(f, AccentBottomShiftDown),           level); /* engine, undefined */
    tex_aux_define_all_math_parameters(size, math_parameter_accent_top_overshoot,               math_parameter(f, AccentTopOvershoot),              level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_accent_bottom_overshoot,            math_parameter(f, AccentBottomOvershoot),           level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_accent_superscript_drop,            math_parameter(f, AccentSuperscriptDrop),           level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_accent_superscript_percent,         math_parameter(f, AccentSuperscriptPercent),        level); /* engine, default 0 */
    tex_aux_define_all_math_parameters(size, math_parameter_accent_extend_margin,               math_parameter(f, AccentExtendMargin),              level); /* engine, undefined */
    tex_aux_define_all_math_parameters(size, math_parameter_flattened_accent_top_shift_up,      math_parameter(f, FlattenedAccentTopShiftUp),       level); /* engine, undefined */
    tex_aux_define_all_math_parameters(size, math_parameter_flattened_accent_bottom_shift_down, math_parameter(f, FlattenedAccentBottomShiftDown),  level); /* engine, undefined */
    tex_aux_define_all_math_parameters(size, math_parameter_delimiter_percent,                  math_parameter(f, DelimiterPercent),                level); /* engine, undefined */
    tex_aux_define_all_math_parameters(size, math_parameter_delimiter_shortfall,                math_parameter(f, DelimiterShortfall),              level); /* engine, undefined */
    tex_aux_define_all_math_parameters(size, math_parameter_delimiter_extend_margin,            math_parameter(f, DelimiterExtendMargin),           level); /* engine, undefined */

    tex_aux_define_all_math_parameters(size, math_parameter_radical_extensible_after,           math_parameter(f, RadicalKernAfterExtensible),      level); /* engine, undefined */
    tex_aux_define_all_math_parameters(size, math_parameter_radical_extensible_before,          math_parameter(f, RadicalKernBeforeExtensible),     level); /* engine, undefined */

    /*tex Not all are official \OPENTYPE: */

    tex_aux_define_all_math_parameters(size, math_parameter_x_scale, scaling_factor, level);
    tex_aux_define_all_math_parameters(size, math_parameter_y_scale, scaling_factor, level);

    /*tex Most are zero and have to be set at by the macro package (if at all):. */

    tex_aux_define_all_math_parameters(size, math_parameter_limit_above_kern,              0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_limit_below_kern,              0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_extra_superscript_shift,       0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_extra_subscript_shift,         0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_extra_superprescript_shift,    0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_extra_subprescript_shift,      0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_rule_height,                   0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_rule_depth,                    0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_superscript_shift_distance,    0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_subscript_shift_distance,      0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_superprescript_shift_distance, 0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_subprescript_shift_distance,   0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_extra_superscript_space,       0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_extra_subscript_space,         0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_extra_superprescript_space,    0, level);
    tex_aux_define_all_math_parameters(size, math_parameter_extra_subprescript_space,      0, level);

    /*tex A special one: */

    if (math_parameter(f, SubscriptShiftDownWithSuperscript) != undefined_math_parameter) { /* engine */
        tex_aux_define_all_math_parameters(size, math_parameter_subscript_superscript_shift_down, tex_aux_get_font_math_parameter(scale, f, SubscriptShiftDownWithSuperscript), level);
    } else {
        tex_aux_define_all_math_parameters(size, math_parameter_subscript_superscript_shift_down, tex_aux_get_font_math_parameter(scale, f, SubscriptShiftDown),                level);
    }

    /*tex These differentiate between display and inline: */

    tex_aux_define_dis_math_parameters(size, math_parameter_operator_size,       tex_aux_get_font_math_parameter(scale, f, DisplayOperatorMinHeight),                 level);
    tex_aux_define_inl_math_parameters(size, math_parameter_radical_vgap,        tex_aux_get_font_math_parameter(scale, f, RadicalVerticalGap),                       level);
    tex_aux_define_dis_math_parameters(size, math_parameter_radical_vgap,        tex_aux_get_font_math_parameter(scale, f, RadicalDisplayStyleVerticalGap),           level);
    tex_aux_define_inl_math_parameters(size, math_parameter_stack_num_up,        tex_aux_get_font_math_parameter(scale, f, StackTopShiftUp),                          level);
    tex_aux_define_dis_math_parameters(size, math_parameter_stack_num_up,        tex_aux_get_font_math_parameter(scale, f, StackTopDisplayStyleShiftUp),              level);
    tex_aux_define_inl_math_parameters(size, math_parameter_stack_denom_down,    tex_aux_get_font_math_parameter(scale, f, StackBottomShiftDown),                     level);
    tex_aux_define_dis_math_parameters(size, math_parameter_stack_denom_down,    tex_aux_get_font_math_parameter(scale, f, StackBottomDisplayStyleShiftDown),         level);
    tex_aux_define_inl_math_parameters(size, math_parameter_stack_vgap,          tex_aux_get_font_math_parameter(scale, f, StackGapMin),                              level);
    tex_aux_define_dis_math_parameters(size, math_parameter_stack_vgap,          tex_aux_get_font_math_parameter(scale, f, StackDisplayStyleGapMin),                  level);
    tex_aux_define_inl_math_parameters(size, math_parameter_fraction_num_vgap,   tex_aux_get_font_math_parameter(scale, f, FractionNumeratorGapMin),                  level);
    tex_aux_define_dis_math_parameters(size, math_parameter_fraction_num_vgap,   tex_aux_get_font_math_parameter(scale, f, FractionNumeratorDisplayStyleGapMin),      level);
    tex_aux_define_inl_math_parameters(size, math_parameter_fraction_num_up,     tex_aux_get_font_math_parameter(scale, f, FractionNumeratorShiftUp),                 level);
    tex_aux_define_dis_math_parameters(size, math_parameter_fraction_num_up,     tex_aux_get_font_math_parameter(scale, f, FractionNumeratorDisplayStyleShiftUp),     level);
    tex_aux_define_inl_math_parameters(size, math_parameter_fraction_denom_vgap, tex_aux_get_font_math_parameter(scale, f, FractionDenominatorGapMin),                level);
    tex_aux_define_dis_math_parameters(size, math_parameter_fraction_denom_vgap, tex_aux_get_font_math_parameter(scale, f, FractionDenominatorDisplayStyleGapMin),    level);
    tex_aux_define_inl_math_parameters(size, math_parameter_fraction_denom_down, tex_aux_get_font_math_parameter(scale, f, FractionDenominatorShiftDown),             level);
    tex_aux_define_dis_math_parameters(size, math_parameter_fraction_denom_down, tex_aux_get_font_math_parameter(scale, f, FractionDenominatorDisplayStyleShiftDown), level);
    tex_aux_define_inl_math_parameters(size, math_parameter_fraction_del_size,   tex_aux_get_font_math_parameter(scale, f, FractionDelimiterSize),                    level); /* engine, undefined */
    tex_aux_define_dis_math_parameters(size, math_parameter_fraction_del_size,   tex_aux_get_font_math_parameter(scale, f, FractionDelimiterDisplayStyleSize),        level); /* engine, undefined */

    /*tex A few more specials: */

    switch (size) {
        case script_size:
            tex_def_math_parameter(script_style,         math_parameter_superscript_shift_up, tex_aux_get_font_math_parameter(scale, f, SuperscriptShiftUp),        level, indirect_math_regular);
            tex_def_math_parameter(cramped_script_style, math_parameter_superscript_shift_up, tex_aux_get_font_math_parameter(scale, f, SuperscriptShiftUpCramped), level, indirect_math_regular);
            tex_def_math_parameter(script_style,         math_parameter_prime_shift_up,       tex_aux_get_font_math_parameter(scale, f, PrimeShiftUp),              level, indirect_math_regular); /* engine, default 0 */
            tex_def_math_parameter(cramped_script_style, math_parameter_prime_shift_up,       tex_aux_get_font_math_parameter(scale, f, PrimeShiftUpCramped),       level, indirect_math_regular); /* engine, default 0 */
            break;
        case script_script_size:
            tex_def_math_parameter(script_script_style,         math_parameter_superscript_shift_up, tex_aux_get_font_math_parameter(scale, f, SuperscriptShiftUp),        level, indirect_math_regular);
            tex_def_math_parameter(cramped_script_script_style, math_parameter_superscript_shift_up, tex_aux_get_font_math_parameter(scale, f, SuperscriptShiftUpCramped), level, indirect_math_regular);
            tex_def_math_parameter(script_script_style,         math_parameter_prime_shift_up,       tex_aux_get_font_math_parameter(scale, f, PrimeShiftUp),              level, indirect_math_regular); /* engine, default 0 */
            tex_def_math_parameter(cramped_script_script_style, math_parameter_prime_shift_up,       tex_aux_get_font_math_parameter(scale, f, PrimeShiftUpCramped),       level, indirect_math_regular); /* engine, default 0 */
            break;
        default:
            tex_def_math_parameter(display_style,         math_parameter_superscript_shift_up, tex_aux_get_font_math_parameter(scale, f, SuperscriptShiftUp),        level, indirect_math_regular);
            tex_def_math_parameter(cramped_display_style, math_parameter_superscript_shift_up, tex_aux_get_font_math_parameter(scale, f, SuperscriptShiftUpCramped), level, indirect_math_regular);
            tex_def_math_parameter(text_style,            math_parameter_superscript_shift_up, tex_aux_get_font_math_parameter(scale, f, SuperscriptShiftUp),        level, indirect_math_regular);
            tex_def_math_parameter(cramped_text_style,    math_parameter_superscript_shift_up, tex_aux_get_font_math_parameter(scale, f, SuperscriptShiftUpCramped), level, indirect_math_regular);
            tex_def_math_parameter(display_style,         math_parameter_prime_shift_up,       tex_aux_get_font_math_parameter(scale, f, PrimeShiftUp),              level, indirect_math_regular); /* engine, default 0 */
            tex_def_math_parameter(cramped_display_style, math_parameter_prime_shift_up,       tex_aux_get_font_math_parameter(scale, f, PrimeShiftUpCramped),       level, indirect_math_regular); /* engine, default 0 */
            tex_def_math_parameter(text_style,            math_parameter_prime_shift_up,       tex_aux_get_font_math_parameter(scale, f, PrimeShiftUp),              level, indirect_math_regular); /* engine, default 0 */
            tex_def_math_parameter(cramped_text_style,    math_parameter_prime_shift_up,       tex_aux_get_font_math_parameter(scale, f, PrimeShiftUpCramped),       level, indirect_math_regular); /* engine, default 0 */
            break;
    }

}

/*tex

    There is some trickery here. The values are actually pointers and in \LUATEX\ the predefined
    muglue ones are small numbers that are way below the normal node values. So, they are kind
    of save signals. However, in \LUAMETATEX\ we use zero based internal codes (because that is
    nicer for the interface.

*/

void tex_set_display_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    tex_def_math_parameter(display_style,         code, value, level, indirect);
    tex_def_math_parameter(cramped_display_style, code, value, level, indirect);
}

void tex_set_text_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    tex_def_math_parameter(text_style,         code, value, level, indirect);
    tex_def_math_parameter(cramped_text_style, code, value, level, indirect);
}

void tex_set_main_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    for (int style = display_style; style <= cramped_text_style; style++) {
        tex_def_math_parameter(style, code, value, level, indirect);
    }
}

void tex_set_script_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    tex_def_math_parameter(script_style,         code, value, level, indirect);
    tex_def_math_parameter(cramped_script_style, code, value, level, indirect);
}

void tex_set_script_script_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    tex_def_math_parameter(script_script_style,         code, value, level, indirect);
    tex_def_math_parameter(cramped_script_script_style, code, value, level, indirect);
}

void tex_set_all_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    for (int style = display_style; style <= cramped_script_script_style; style++) {
        tex_def_math_parameter(style, code, value, level, indirect);
    }
}

void tex_set_uncramped_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    for (int style = display_style; style <= script_script_style; style += 2) {
        tex_def_math_parameter(style, code, value, level, indirect);
    }
}

void tex_set_cramped_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    for (int style = cramped_display_style; style <= cramped_script_script_style; style += 2) {
        tex_def_math_parameter(style, code, value, level, indirect);
    }
}

void tex_set_split_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    tex_set_display_styles      (code, value, level, indirect);
    tex_set_text_styles         (code, value, level, indirect);
    tex_set_script_styles       (code, 0,     level, indirect);
    tex_set_script_script_styles(code, 0,     level, indirect);
}

void tex_set_unsplit_styles(halfword code, halfword value, halfword level, halfword indirect)
{
    tex_set_script_styles       (code, value, level, indirect);
    tex_set_script_script_styles(code, value, level, indirect);
}

void tex_reset_all_styles(halfword level)
{
    for (int code = math_parameter_atom_pairs_first; code <= math_parameter_atom_pairs_last; code++) {
        tex_set_all_styles(code, zero_mu_skip_code, level, indirect_math_unset);
    }
}

inline static halfword tex_aux_math_class_default(halfword mathclass) {
    return (mathclass << 24) + (mathclass << 16) + (mathclass << 8) + mathclass;
}

inline static void tex_set_math_class_default(halfword mathclass, halfword parent, halfword options)
{
    tex_word_define(0, internal_int_location(first_math_class_code   + mathclass), tex_aux_math_class_default(parent));
    tex_word_define(0, internal_int_location(first_math_atom_code    + mathclass), tex_aux_math_class_default(mathclass));
    tex_word_define(0, internal_int_location(first_math_options_code + mathclass), options);
    tex_word_define(0, internal_int_location(first_math_parent_code  + mathclass), tex_aux_math_class_default(mathclass));
}

static void tex_aux_set_math_atom_rule(halfword left, halfword right, halfword newleft, halfword newright)
{
    tex_set_all_styles(math_parameter_rules_pair(left, right), (newleft << 16) + newright, level_one, indirect_math_regular);
}

/*tex

    Originally a penalty of 10000 signaled that no penalty has to be included but because we want 
    to control penalties in nested sequences (like open and close bound sequences) we need to be 
    able to go up (multiply by a factor) or down (divide by a factor). Therefore we need to be able
    to set a penalty to 10000 as a start. so that it will be ignored unless we apply a factor. For 
    that reason we now use 10001 instead. 

*/

void tex_initialize_math_spacing(void)
{

    for (int mathclass = 0; mathclass <= max_math_class_code; mathclass++) {
        tex_set_math_class_default(mathclass, mathclass, no_class_options);
        /*tex We do this here as there is no real need for yet another initializer. */
        tex_word_define(0, internal_int_location(first_math_pre_penalty_code  + mathclass), math_default_penalty);
        tex_word_define(0, internal_int_location(first_math_post_penalty_code + mathclass), math_default_penalty);
        tex_word_define(0, internal_int_location(first_math_display_pre_penalty_code  + mathclass), math_default_penalty);
        tex_word_define(0, internal_int_location(first_math_display_post_penalty_code + mathclass), math_default_penalty);
    }

    tex_reset_all_styles(level_one);
    
    tex_set_math_class_default(ordinary_noad_subtype,    ordinary_noad_subtype,    no_italic_correction_class_option | 
                                                                                   check_ligature_class_option | 
                                                                                   check_kern_pair_class_option | 
                                                                                   flatten_class_option);
    tex_set_math_class_default(operator_noad_subtype,    operator_noad_subtype,    check_ligature_class_option |
                                                                                   check_kern_pair_class_option);
    tex_set_math_class_default(binary_noad_subtype,      binary_noad_subtype,      no_italic_correction_class_option | 
                                                                                   check_ligature_class_option | 
                                                                                   check_kern_pair_class_option | 
                                                                                   flatten_class_option);
    tex_set_math_class_default(relation_noad_subtype,    relation_noad_subtype,    no_italic_correction_class_option | 
                                                                                   check_ligature_class_option | 
                                                                                   check_kern_pair_class_option | 
                                                                                   flatten_class_option | 
                                                                                   omit_penalty_class_option);
    tex_set_math_class_default(open_noad_subtype,        open_noad_subtype,        no_italic_correction_class_option | 
                                                                                /* open_fence_class_option | */
                                                                                   check_ligature_class_option |
                                                                                   check_kern_pair_class_option); 
    tex_set_math_class_default(close_noad_subtype,       close_noad_subtype,       no_italic_correction_class_option | 
                                                                                /* close_fence_class_option | */
                                                                                   check_ligature_class_option |
                                                                                   check_kern_pair_class_option); 
    tex_set_math_class_default(punctuation_noad_subtype, punctuation_noad_subtype, no_italic_correction_class_option | 
                                                                                   check_ligature_class_option | 
                                                                                   check_kern_pair_class_option | 
                                                                                   flatten_class_option);
    tex_set_math_class_default(variable_noad_subtype,    ordinary_noad_subtype,    no_italic_correction_class_option);
    tex_set_math_class_default(active_noad_subtype,      ordinary_noad_subtype,    no_italic_correction_class_option);
    tex_set_math_class_default(inner_noad_subtype,       inner_noad_subtype,       flatten_class_option);
    tex_set_math_class_default(under_noad_subtype,       ordinary_noad_subtype,    no_class_options);
    tex_set_math_class_default(over_noad_subtype,        ordinary_noad_subtype,    no_class_options);
    tex_set_math_class_default(fraction_noad_subtype,    ordinary_noad_subtype,    no_class_options);
    tex_set_math_class_default(radical_noad_subtype,     ordinary_noad_subtype,    no_class_options);
    tex_set_math_class_default(middle_noad_subtype,      open_noad_subtype,        no_italic_correction_class_option); /* | middle_fence_class_option= */
    tex_set_math_class_default(accent_noad_subtype,      ordinary_noad_subtype,    no_class_options);
    tex_set_math_class_default(fenced_noad_subtype,      inner_noad_subtype   ,    no_class_options);
    tex_set_math_class_default(ghost_noad_subtype,       ordinary_noad_subtype,    no_class_options);
    tex_set_math_class_default(vcenter_noad_subtype,     ordinary_noad_subtype,    no_class_options);

    tex_aux_set_math_atom_rule(math_begin_class,         binary_noad_subtype,      ordinary_noad_subtype,    ordinary_noad_subtype);
    tex_aux_set_math_atom_rule(binary_noad_subtype,      math_end_class,           ordinary_noad_subtype,    ordinary_noad_subtype);

    tex_aux_set_math_atom_rule(binary_noad_subtype,      binary_noad_subtype,      binary_noad_subtype,      ordinary_noad_subtype);
    tex_aux_set_math_atom_rule(operator_noad_subtype,    binary_noad_subtype,      operator_noad_subtype,    ordinary_noad_subtype);
    tex_aux_set_math_atom_rule(open_noad_subtype,        binary_noad_subtype,      open_noad_subtype,        ordinary_noad_subtype);
    tex_aux_set_math_atom_rule(punctuation_noad_subtype, binary_noad_subtype,      punctuation_noad_subtype, ordinary_noad_subtype);
    tex_aux_set_math_atom_rule(relation_noad_subtype,    binary_noad_subtype,      relation_noad_subtype,    ordinary_noad_subtype);

    tex_aux_set_math_atom_rule(binary_noad_subtype,      close_noad_subtype,       ordinary_noad_subtype,    close_noad_subtype);
    tex_aux_set_math_atom_rule(binary_noad_subtype,      punctuation_noad_subtype, ordinary_noad_subtype,    punctuation_noad_subtype);
    tex_aux_set_math_atom_rule(binary_noad_subtype,      relation_noad_subtype,    ordinary_noad_subtype,    relation_noad_subtype);

    tex_aux_set_math_atom_rule(relation_noad_subtype,    close_noad_subtype,       ordinary_noad_subtype,    close_noad_subtype);
    tex_aux_set_math_atom_rule(relation_noad_subtype,    punctuation_noad_subtype, ordinary_noad_subtype,    punctuation_noad_subtype);

    /* */

//    math_parameter_spacing_pair(ordinary_noad_subtype,ordinary_noad_subtype)

    tex_set_all_styles   (math_parameter_spacing_pair(ordinary_noad_subtype,    operator_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(ordinary_noad_subtype,    binary_noad_subtype),      med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(ordinary_noad_subtype,    relation_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(ordinary_noad_subtype,    inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);

    tex_set_all_styles   (math_parameter_spacing_pair(operator_noad_subtype,    ordinary_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_spacing_pair(operator_noad_subtype,    operator_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(operator_noad_subtype,    relation_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(operator_noad_subtype,    inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);

    tex_set_all_styles   (math_parameter_spacing_pair(operator_noad_subtype,    fraction_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_spacing_pair(operator_noad_subtype,    radical_noad_subtype),     thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_spacing_pair(fraction_noad_subtype,    operator_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_spacing_pair(radical_noad_subtype,     operator_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);

    tex_set_split_styles (math_parameter_spacing_pair(binary_noad_subtype,      ordinary_noad_subtype),    med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(binary_noad_subtype,      operator_noad_subtype),    med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(binary_noad_subtype,      open_noad_subtype),        med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(binary_noad_subtype,      inner_noad_subtype),       med_mu_skip_code,   level_one, indirect_math_regular);

    tex_set_split_styles (math_parameter_spacing_pair(binary_noad_subtype,      middle_noad_subtype),      med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(binary_noad_subtype,      fraction_noad_subtype),    med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(binary_noad_subtype,      radical_noad_subtype),     med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(middle_noad_subtype,      binary_noad_subtype),      med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(fraction_noad_subtype,    binary_noad_subtype),      med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(radical_noad_subtype,     binary_noad_subtype),      med_mu_skip_code,   level_one, indirect_math_regular);

    tex_set_split_styles (math_parameter_spacing_pair(relation_noad_subtype,    ordinary_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(relation_noad_subtype,    operator_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(relation_noad_subtype,    open_noad_subtype),        thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(relation_noad_subtype,    inner_noad_subtype),       thick_mu_skip_code, level_one, indirect_math_regular);

    tex_set_split_styles (math_parameter_spacing_pair(relation_noad_subtype,    middle_noad_subtype),      thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(relation_noad_subtype,    fraction_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(relation_noad_subtype,    radical_noad_subtype),     thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(middle_noad_subtype,      relation_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(fraction_noad_subtype,    relation_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(radical_noad_subtype,     relation_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);

    tex_set_all_styles   (math_parameter_spacing_pair(close_noad_subtype,       operator_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(close_noad_subtype,       binary_noad_subtype),      med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(close_noad_subtype,       relation_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(close_noad_subtype,       inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);

    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, ordinary_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, operator_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, relation_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, open_noad_subtype),        thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, close_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, punctuation_noad_subtype), thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);

    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, fraction_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, middle_noad_subtype),      thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(punctuation_noad_subtype, radical_noad_subtype),     thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(fraction_noad_subtype,    punctuation_noad_subtype), thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(middle_noad_subtype,      punctuation_noad_subtype), thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(radical_noad_subtype,     punctuation_noad_subtype), thin_mu_skip_code,  level_one, indirect_math_regular);

    tex_set_split_styles (math_parameter_spacing_pair(inner_noad_subtype,       ordinary_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_spacing_pair(inner_noad_subtype,       operator_noad_subtype),    thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(inner_noad_subtype,       binary_noad_subtype),      med_mu_skip_code,   level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(inner_noad_subtype,       relation_noad_subtype),    thick_mu_skip_code, level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(inner_noad_subtype,       open_noad_subtype),        thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(inner_noad_subtype,       punctuation_noad_subtype), thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(inner_noad_subtype,       inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);

    tex_set_split_styles (math_parameter_spacing_pair(inner_noad_subtype,       middle_noad_subtype),      thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(fraction_noad_subtype,    inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(radical_noad_subtype,     inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(middle_noad_subtype,      inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(fraction_noad_subtype,    inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);
    tex_set_split_styles (math_parameter_spacing_pair(radical_noad_subtype,     inner_noad_subtype),       thin_mu_skip_code,  level_one, indirect_math_regular);

    /* */

    tex_set_all_styles   (math_parameter_x_scale, scaling_factor, level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_y_scale, scaling_factor, level_one, indirect_math_regular);

    /* could be initialize_math_defaults */

    tex_set_all_styles   (math_parameter_over_line_variant,       math_cramped_style_variant,      level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_under_line_variant,      math_normal_style_variant,       level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_over_delimiter_variant,  math_small_style_variant,        level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_under_delimiter_variant, math_small_style_variant,        level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_delimiter_over_variant,  math_normal_style_variant,       level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_delimiter_under_variant, math_normal_style_variant,       level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_h_extensible_variant,    math_normal_style_variant,       level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_v_extensible_variant,    math_normal_style_variant,       level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_fraction_variant,        math_cramped_style_variant,      level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_radical_variant,         math_cramped_style_variant,      level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_degree_variant,          math_double_superscript_variant, level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_accent_variant,          math_cramped_style_variant,      level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_top_accent_variant,      math_cramped_style_variant,      level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_bottom_accent_variant,   math_cramped_style_variant,      level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_overlay_accent_variant,  math_cramped_style_variant,      level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_numerator_variant,       math_numerator_style_variant,    level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_denominator_variant,     math_denominator_style_variant,  level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_superscript_variant,     math_superscript_style_variant,  level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_subscript_variant,       math_subscript_style_variant,    level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_prime_variant,           math_superscript_style_variant,  level_one, indirect_math_regular);
    tex_set_all_styles   (math_parameter_stack_variant,           math_numerator_style_variant,    level_one, indirect_math_regular);
}

/*tex

    This needs to be called just at the start of |mlist_to_hlist|, for backward compatibility with
    |\scriptspace|.

*/

void tex_finalize_math_parameters(void)
{
    int saved_trace = tracing_assigns_par;
    tracing_assigns_par = 0;
    if (tex_get_math_parameter(display_style,               math_parameter_space_after_script, NULL) == undefined_math_parameter) {
        tex_def_math_parameter(display_style,               math_parameter_space_after_script, script_space_par, level_one, indirect_math_regular);
        tex_def_math_parameter(text_style,                  math_parameter_space_after_script, script_space_par, level_one, indirect_math_regular);
        tex_def_math_parameter(script_style,                math_parameter_space_after_script, script_space_par, level_one, indirect_math_regular);
        tex_def_math_parameter(script_script_style,         math_parameter_space_after_script, script_space_par, level_one, indirect_math_regular);
        tex_def_math_parameter(cramped_display_style,       math_parameter_space_after_script, script_space_par, level_one, indirect_math_regular);
        tex_def_math_parameter(cramped_text_style,          math_parameter_space_after_script, script_space_par, level_one, indirect_math_regular);
        tex_def_math_parameter(cramped_script_style,        math_parameter_space_after_script, script_space_par, level_one, indirect_math_regular);
        tex_def_math_parameter(cramped_script_script_style, math_parameter_space_after_script, script_space_par, level_one, indirect_math_regular);
    }
    tracing_assigns_par = saved_trace;
}

static void tex_aux_math_parameter_error(int style, int param, const char *name)
{
    if (param >= 0) {
        tex_handle_error(
            normal_error_type,
            "Math error: parameter '%s' with id %i in style %i is not set", 
            name, param, style,
            "Sorry, but I can't typeset math unless various parameters have been set. This is\n"
            "normally done by loading special math fonts into the math family slots. Your font\n"
            "set is lacking at least the parameter mentioned earlier."
        );
    } else {
        tex_formatted_error("math", "invalid parameter '%s' in style %i", name, style);
    }
    return;
}

/*tex
    For the moment this is experimental.
*/

inline static scaled tex_aux_max_scale(int style, int param)
{
    scaled scale = tex_get_math_parameter(style, param, NULL);
    if (scale > max_math_scaling_factor) {
        return max_math_scaling_factor;
    } else if (scale < 0) {
        return 0;
    } else {
        return scale;
    }
}

/*tex

    The non-staticness of this function is for the benefit of |texmath.w|. Watch out, this one
    uses the style! The style and size numbers don't match because we have cramped styles.

*/

scaled tex_get_math_quad_style(int style)
{
    scaled scale = tex_aux_max_scale(style, math_parameter_x_scale);
    scaled value = tex_get_math_parameter(style, math_parameter_quad, NULL);
    if (value == undefined_math_parameter) {
        tex_aux_math_parameter_error(style, -1, "quad");
        return 0;
    } else {
        return scaledround(0.001 * value * scale);
    }
}

/*tex

    For this reason the next one is different because it is called with a size specifier instead
    of a style specifier.

*/

scaled tex_math_axis_size(int size)
{
    scaled value;
    switch (size) {
        case script_size       : size = script_style;        break;
        case script_script_size: size = script_script_style; break;
        default                : size = text_style;          break;
    }
    value = tex_get_math_parameter(size, math_parameter_axis, NULL);
    if (value == undefined_math_parameter) {
        tex_aux_math_parameter_error(size, -1, "axis");
        return 0;
    } else {
        return value;
    }
}

scaled tex_get_math_quad_size(int size) /* used in degree before and after */
{
    switch (size) {
        case script_size       : size = script_style;        break;
        case script_script_size: size = script_script_style; break;
        default                : size = text_style;          break;
    }
    return tex_get_math_parameter(size, math_parameter_quad, NULL);
}

scaled tex_get_math_quad_size_scaled(int size) /* used in cur_mu */
{
    scaled value, scale;
    switch (size) {
        case script_size       : size = script_style;        break;
        case script_script_size: size = script_script_style; break;
        default                : size = text_style;          break;
    }
    value = tex_get_math_parameter(size, math_parameter_quad, NULL);
    scale = tex_aux_max_scale(size, math_parameter_x_scale);
 /* return tex_x_over_n(scaledround(0.001 * value * scale), 18); */
    return scaledround(0.001 * value * scale / 18.0);
}

static int tex_aux_math_parameter_okay(int param) 
{
    if (ignore_math_parameter(param)) {
        if (tracing_math_par > 1) {
            tex_begin_diagnostic();
            tex_print_format("[math: parameter, name %s, ignored]", lmt_name_of_math_parameter(param));
            tex_end_diagnostic();
        }
        return 0;
    } else { 
        return 1; 
    }
}

scaled tex_get_math_parameter_checked(int style, int param)
{
    if (tex_aux_math_parameter_okay(param)) {
        scaled value = tex_get_math_parameter(style, param, NULL);
        if (value == undefined_math_parameter) {
            tex_aux_math_parameter_error(style, param, lmt_name_of_math_parameter(param));
            return 0;
        } else {
            return value;
        }
    } else {
        return 0;
    }
}

scaled tex_get_math_parameter_default(int style, int param, scaled dflt)
{
    if (tex_aux_math_parameter_okay(param)) {
        scaled value = tex_get_math_parameter(style, param, NULL);
        if (value == undefined_math_parameter) {
            return dflt;
        } else {
            return value;
        }
    } else {
        return dflt;
    }
}

void tex_run_math_italic_correction(void) {
    tex_tail_append(tex_new_kern_node(0, explicit_kern_subtype)); /* maybe math_shape_kern */
}

/* */

scaled tex_get_math_x_parameter(int style, int param)
{
    if (tex_aux_math_parameter_okay(param)) {
        scaled scale = tex_aux_max_scale(style, math_parameter_x_scale);
        scaled value = tex_get_math_parameter(style, param, NULL);
        if (value == undefined_math_parameter) {
            return value;  // ?? scaledround(value * scale * 0.001);
        } else {
            return value ? scaledround(0.000000001 * glyph_scale_par * glyph_x_scale_par * value * scale) : 0;
        }
    } else {
        return 0;
    }
}

scaled tex_get_math_x_parameter_checked(int style, int param)
{
    if (tex_aux_math_parameter_okay(param)) {
        scaled scale = tex_aux_max_scale(style, math_parameter_x_scale);
        scaled value = tex_get_math_parameter(style, param, NULL);
        if (value == undefined_math_parameter) {
            tex_aux_math_parameter_error(style, param, lmt_name_of_math_parameter(param));
            return 0;
        } else {
            return value ? scaledround(0.000000001 * glyph_scale_par * glyph_x_scale_par * value * scale) : 0;
        }
    } else {
        return 0;
    }
}

scaled tex_get_math_x_parameter_default(int style, int param, scaled dflt)
{
    if (tex_aux_math_parameter_okay(param)) {
        scaled scale = tex_aux_max_scale(style, math_parameter_x_scale);
        scaled value = tex_get_math_parameter(style, param, NULL);
        if (value == undefined_math_parameter) {
            return dflt;
        } else{
            return value ? scaledround(0.000000001 * glyph_scale_par * glyph_x_scale_par * value * scale) : 0;
        }
    } else {
        return dflt;
    }
}

scaled tex_get_math_y_parameter(int style, int param)
{
    if (tex_aux_math_parameter_okay(param)) {
        scaled scale = tex_aux_max_scale(style, math_parameter_y_scale);
        scaled value = tex_get_math_parameter(style, param, NULL);
        if (value == undefined_math_parameter) {
            return value;
        } else{
            return value ? scaledround(0.000000001 * glyph_scale_par * glyph_y_scale_par * value * scale) : 0;
        }
    } else {
        return 0;
    }
}

scaled tex_get_math_y_parameter_checked(int style, int param)
{
    if (tex_aux_math_parameter_okay(param)) {
        scaled scale = tex_aux_max_scale(style, math_parameter_y_scale);
        scaled value = tex_get_math_parameter(style, param, NULL);
        if (value == undefined_math_parameter) {
            tex_aux_math_parameter_error(style, param, lmt_name_of_math_parameter(param));
            return 0;
        } else {
            return value ? scaledround(0.000000001 * glyph_scale_par * glyph_y_scale_par * value * scale) : 0;
        }
    } else {
        return 0;
    }
}

scaled tex_get_math_y_parameter_default(int style, int param, scaled dflt)
{
    if (tex_aux_math_parameter_okay(param)) {
        scaled scale = tex_aux_max_scale(style, math_parameter_y_scale);
        scaled value = tex_get_math_parameter(style, param, NULL);
        if (value == undefined_math_parameter) {
            return dflt;
        } else{
            return value ? scaledround(0.000000001 * glyph_scale_par * glyph_y_scale_par * value * scale) : 0;
        }
    } else {
        return dflt;
    }
}

scaled tex_get_font_math_parameter(int font, int size, int param)
{
    scaled scale = tex_get_math_font_scale(font, size);
    scaled value = tex_aux_get_font_math_parameter(scale, font, param);
    if (value == undefined_math_parameter) {
        return undefined_math_parameter;
    } else {
        return value ? scaledround(0.001 * glyph_scale_par * value) : 0;
    }
}

/* maybe more precission, so multiply all and divide by 0.000000001 */

scaled tex_get_font_math_y_parameter(int font, int size, int param)
{
    scaled scale = tex_get_math_font_scale(font, size);
    scaled value = tex_aux_get_font_math_parameter(scale, font, param);
    if (value == undefined_math_parameter) {
        return undefined_math_parameter;
    } else {
        return value ? scaledround(0.000001 * glyph_scale_par * glyph_y_scale_par * value) : 0;
    }
}

scaled tex_get_font_math_x_parameter(int font, int size, int param)
{
    scaled scale = tex_get_math_font_scale(font, size);
    scaled value = tex_aux_get_font_math_parameter(scale, font, param);
    if (value == undefined_math_parameter) {
        return undefined_math_parameter;
    } else {
        return value ? scaledround(0.000001 * glyph_scale_par * glyph_x_scale_par * value) : 0;
    }
}

halfword tex_to_math_spacing_parameter(halfword left, halfword right)
{
    halfword param = math_parameter_spacing_pair(left,right);
    return (param >= math_parameter_atom_pairs_first && param <= math_parameter_atom_pairs_last) ? param : -1;
}

halfword tex_to_math_rules_parameter(halfword left, halfword right)
{
    halfword param = math_parameter_rules_pair(left,right);
    return (param >= math_parameter_atom_rules_first && param <= math_parameter_atom_rules_last) ? param : -1;
}

void tex_set_default_math_codes(void)
{
    mathcodeval mval = tex_no_math_code();
    /*tex This will remap old font families at runtime. */
    mval.class_value = math_use_current_family_code;
    /*tex Upright math digts come from family 0. */
    for (int d = '0'; d <= '9'; d++) {
        mval.character_value = d;
        tex_set_math_code(d, mval, level_one);
    }
    /* In traditional fonts math italic has family 1. */
    mval.family_value = 1;
    for (int u = 'A'; u <= 'Z'; u++) {
        mval.character_value = u;
        tex_set_math_code(u, mval, level_one);
    }
    for (int l = 'a'; l <= 'z'; l++) {
        mval.character_value = l;
        tex_set_math_code(l, mval, level_one);
    }
    /*tex This is kind of standard. */
    tex_set_del_code('.', (delcodeval) { { 0, 0, 0, }, { 0, 0, 0 } }, level_one);
}

int tex_in_main_math_style(halfword style)
{
    switch (style) {
        case display_style:
        case text_style:
            return 1;
        /*
         case cramped_display_style:
         case cramped_text_style:
            return 0; // could be parameter driven
        */
        default: 
            return 0;
    }
}