summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/unicode-math/unicode-math.dtx
blob: 6ed3ad8bb1bf29cd3e539783fafbc76e3f714509 (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
% \iffalse
% !TEX TS-program = XeLaTeX
% ^^A %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ^^A   SELF-EXTRACTION BEGINS HERE
% ^^A %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%<*internal>
\begingroup
\input l3docstrip.tex
\keepsilent
\let\MetaPrefix\DoubleperCent
\declarepreamble\texpreamble
Copyright 2006-2017   Will Robertson <will.robertson@latex-project.org>
Copyright 2010-2013 Philipp Stephani <st_philipp@yahoo.de>
Copyright 2012-2015     Khaled Hosny <khaledhosny@eglug.org>

This package is free software and may be redistributed and/or modified under
the conditions of the LaTeX Project Public License, version 1.3c or higher
(your choice): <http://www.latex-project.org/lppl/>.

This work is "maintained" by Will Robertson.
\endpreamble
\nopostamble
\askforoverwritefalse
\let\MetaPrefix\DoubleperCent
\usepreamble\texpreamble
\ifx\UMDEBUG\undefined
  \def\UMDEBUG{}%
\else
  \def\UMDEBUG{,debug}%
\fi
\generate{\file{unicode-math.sty}{
  \from{unicode-math-preamble.dtx}{preamble\UMDEBUG}
  \from{unicode-math-msg.dtx}{msg\UMDEBUG}
  \from{unicode-math-usv.dtx}{usv\UMDEBUG}
  \from{unicode-math.dtx}{load\UMDEBUG}
  \from{unicode-math-alphabets.dtx}{alphabets\UMDEBUG}
}}
\generate{\file{unicode-math-xetex.sty}{
  \from{unicode-math.dtx}{package,XE\UMDEBUG}
  \from{unicode-math-epilogue.dtx}{package,XE\UMDEBUG}
  \from{unicode-math-compat.dtx}{compat,XE\UMDEBUG}
}}
\generate{\file{unicode-math-luatex.sty}{
  \from{unicode-math.dtx}{package,LU\UMDEBUG}
  \from{unicode-math-epilogue.dtx}{package,XE\UMDEBUG}
  \from{unicode-math-compat.dtx}{compat,LU\UMDEBUG}
}}
\def\tempa{plain}
\ifx\tempa\fmtname\endgroup\expandafter\bye\fi
\generate{\file{dtx-style.sty}{\from{\jobname.dtx}{dtx-style}}}
\endgroup
\ProvidesFile{unicode-math.dtx}
%</internal>
%<preamble&!XE&!LU>\ProvidesPackage{unicode-math}
%<preamble&XE>\ProvidesPackage{unicode-math-xetex}
%<preamble&LU>\ProvidesPackage{unicode-math-luatex}
%<*preamble>
  [2017/10/02 v0.8g Unicode maths in XeLaTeX and LuaLaTeX]
%</preamble>
%<*internal>
\def\DOCUMENTEND{F}
\input{unicode-math-doc}
\DocInput{unicode-math.dtx}
\DocInput{unicode-math-preamble.dtx}
\DocInput{unicode-math-msg.dtx}
\DocInput{unicode-math-usv.dtx}
\DocInput{unicode-math-alphabets.dtx}
\DocInput{unicode-math-compat.dtx}
\end{document}
%</internal>
% \fi
%
% \clearpage
% \part{Package implementation}
% \parttoc
%
% The prefix for \pkg{unicode-math} is \texttt{um}:
%    \begin{macrocode}
%<@@=um>
%    \end{macrocode}
%
%
% We (later on) bifurcate the package based on the engine being used.
% These separate package files are indicated with the Docstrip flags \textsf{LU} and \textsf{XE}, respectively.
% Shared code executed before loading the engine-specific code is indicated with the flag \textsf{preamble}.
%    \begin{macrocode}
%<*load>
\sys_if_engine_luatex:T { \RequirePackage{unicode-math-luatex} }
\sys_if_engine_xetex:T  { \RequirePackage{unicode-math-xetex}  }
%</load>
%    \end{macrocode}
%
% \section{Bifurcation}
%
% And here the split begins. Most of the code is still shared, but
% code for \LuaTeX\ uses the `\textsf{LU}' flag and code for \XeTeX\ uses `\textsf{XE}'.
%
%    \begin{macrocode}
%<*package&(XE|LU)>
\ExplSyntaxOn
%    \end{macrocode}
%
% \subsection{Engine differences}
%
% \XeTeX\ before version 0.9999 did not support |\U| prefix for extended math
% primitives, and while \LuaTeX\ had it from the start, prior 0.75.0 the
% \LaTeX\ format did not provide them without the |\luatex| prefix.
% We assume that users of \pkg{unicode-math} are using up-to-date engines however.
%
%    \begin{macrocode}
%<*LU>
\RequirePackage{luaotfload}   [2014/05/18]
\RequirePackage{lualatex-math}[2011/08/07]
%</LU>
%    \end{macrocode}
%
%
%
% \section{Fundamentals}
%
% \subsection{Setting math chars, math codes, etc.}
%
% \begin{macro}{\@@_set_mathsymbol:nNNn}
% \darg{A \LaTeX\ symbol font, e.g., \texttt{operators}}
% \darg{Symbol macro, \eg, \cmd\alpha}
% \darg{Type, \eg, \cmd\mathalpha}
% \darg{Slot, \eg, \texttt{"221E}}
% There are a bunch of tests to perform to process the various characters.
% The following assignments should all be fairly straightforward.
%
% The catcode setting is to work around (strange?) behaviour in LuaTeX in which catcode 11 characters don't have italic correction for maths.
% We don't adjust ascii chars, however, because certain punctuation should not have their catcodes changed.
%    \begin{macrocode}
\cs_set:Nn \@@_set_mathsymbol:nNNn
 {
  \bool_lazy_and:nnT
   {
    \int_compare_p:nNn {#4} > {127}
   }
   {
    \int_compare_p:nNn { \char_value_catcode:n {#4} } = {11}
   }
   { \char_set_catcode_other:n {#4} }

  \tl_case:Nn #3
   {
    \mathord   { \@@_set_mathcode:nnn {#4} {#3} {#1} }
    \mathalpha { \@@_set_mathcode:nnn {#4} {#3} {#1} }
    \mathbin   { \@@_set_mathcode:nnn {#4} {#3} {#1} }
    \mathrel   { \@@_set_mathcode:nnn {#4} {#3} {#1} }
    \mathpunct { \@@_set_mathcode:nnn {#4} {#3} {#1} }
    \mathop    { \@@_set_big_operator:nnn {#1} {#2} {#4} }
    \mathopen  { \@@_set_math_open:nnn    {#1} {#2} {#4} }
    \mathclose { \@@_set_math_close:nnn   {#1} {#2} {#4} }
    \mathfence { \@@_set_math_fence:nnnn  {#1} {#2} {#3} {#4} }
    \mathaccent
     { \@@_set_math_accent:Nnnn #2 {fixed} {#1} {#4} }
    \mathbotaccent
     { \@@_set_math_accent:Nnnn #2 {bottom~ fixed} {#1} {#4} }
    \mathaccentwide
     { \@@_set_math_accent:Nnnn #2 {} {#1} {#4} }
    \mathbotaccentwide
     { \@@_set_math_accent:Nnnn #2 {bottom} {#1} {#4} }
    \mathover
     { \@@_set_math_overunder:Nnnn #2 {} {#1} {#4} }
    \mathunder
     { \@@_set_math_overunder:Nnnn #2 {bottom} {#1} {#4} }
   }
 }
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\edef\mathfence{\string\mathfence}
\edef\mathover{\string\mathover}
\edef\mathunder{\string\mathunder}
\edef\mathbotaccent{\string\mathbotaccent}
\edef\mathaccentwide{\string\mathaccentwide}
\edef\mathbotaccentwide{\string\mathbotaccentwide}
%    \end{macrocode}
%
%
% \begin{macro}{\@@_set_big_operator:nnn}
% \darg{Symbol font name}
% \darg{Macro to assign}
% \darg{Glyph slot}
% In the examples following, say we're defining for the symbol \cmd\sum\ ($\sum$).
% In order for literal Unicode characters to be used in the source and still
% have the correct limits behaviour, big operators are made math-active.
% This involves three steps:
% \begin{itemize}
% \item
% The active math char is defined to expand to the macro \cs{sum_sym}.
% (Later, the control sequence \cs{sum} will be assigned the math char.)
% \item
% Declare the plain old mathchardef for the control sequence \cmd\sumop.
% (This follows the convention of \LaTeX/\pkg{amsmath}.)
% \item
% Define \cs{sum_sym} as \cmd\sumop, followed by \cmd\nolimits\ if necessary.
% \end{itemize}
% Whether the \cmd\nolimits\ suffix is inserted is controlled by the
% token list \cs{l_@@_nolimits_tl}, which contains a list of such characters.
% This list is checked dynamically to allow it to be updated mid-document.
%
% Examples of expansion, by default, for two big operators:
% \begin{quote}
% (~\cs{sum} $\to$~) $\sum$ $\to$ \cs{sum_sym} $\to$ \cs{sumop}\cs{nolimits}\par
% (~\cs{int} $\to$~) $\int$ $\to$ \cs{int_sym} $\to$ \cs{intop}
% \end{quote}
%    \begin{macrocode}
\cs_new:Nn \@@_set_big_operator:nnn
 {
  \@@_char_gmake_mathactive:n {#3}
  \cs_set_protected_nopar:Npx \@@_tmpa: { \exp_not:c { \cs_to_str:N #2 _sym } }
  \char_gset_active_eq:nN {#3} \@@_tmpa:

  \@@_set_mathchar:cNnn {\cs_to_str:N #2 op} \mathop {#1} {#3}

  \cs_gset:cpx { \cs_to_str:N #2 _sym }
   {
    \exp_not:c { \cs_to_str:N #2 op   }
    \exp_not:n { \tl_if_in:NnT \l_@@_nolimits_tl {#2} \nolimits }
   }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_math_open:nnn}
% \darg{Symbol font name}
% \darg{Macro to assign}
% \darg{Glyph slot}
%    \begin{macrocode}
\cs_new:Nn \@@_set_math_open:nnn
 {
  \tl_if_in:NnTF \l_@@_radicals_tl {#2}
   {
     \cs_gset_protected_nopar:cpx {\cs_to_str:N #2 sign}
       { \@@_radical:nn {#1} {#3} }
     \tl_set:cn {l_@@_radical_\cs_to_str:N #2_tl} {\use:c{sym #1}~ #3}
   }
   {
     \@@_set_delcode:nnn {#1} {#3} {#3}
     \@@_set_mathcode:nnn {#3} \mathopen {#1}
     \cs_gset_protected_nopar:Npx #2
       { \@@_delimiter:Nnn \mathopen {#1} {#3} }
   }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_math_close:nnn}
% \darg{Symbol font name}
% \darg{Macro to assign}
% \darg{Glyph slot}
%    \begin{macrocode}
\cs_new:Nn \@@_set_math_close:nnn
 {
  \@@_set_delcode:nnn {#1} {#3} {#3}
  \@@_set_mathcode:nnn {#3} \mathclose {#1}
  \cs_gset_protected_nopar:Npx #2
    { \@@_delimiter:Nnn \mathclose {#1} {#3} }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_math_fence:nnnn}
% \darg{Symbol font name}
% \darg{Macro to assign}
% \darg{Type, \eg, \cmd\mathalpha}
% \darg{Glyph slot}
%    \begin{macrocode}
\cs_new:Nn \@@_set_math_fence:nnnn
 {
  \@@_set_mathcode:nnn {#4} {#3} {#1}
  \@@_set_delcode:nnn  {#1} {#4} {#4}
  \cs_gset_protected_nopar:cpx {l \cs_to_str:N #2}
    { \@@_delimiter:Nnn \mathopen  {#1} {#4} }
  \cs_gset_protected_nopar:cpx {r \cs_to_str:N #2}
    { \@@_delimiter:Nnn \mathclose {#1} {#4} }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_math_accent:Nnnn}
% \darg{Accend command}
% \darg{Accent type (string)}
% \darg{Symbol font name}
% \darg{Glyph slot}
%    \begin{macrocode}
\cs_new:Nn \@@_set_math_accent:Nnnn
 {
  \cs_gset_protected_nopar:Npx #1
   { \@@_accent:nnn {#2} {#3} {#4} }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_math_overunder:Nnnn}
% \darg{Accend command}
% \darg{Accent type (string)}
% \darg{Symbol font name}
% \darg{Glyph slot}
%    \begin{macrocode}
\cs_new:Nn \@@_set_math_overunder:Nnnn
 {
  \cs_gset_protected_nopar:Npx #1 ##1
   {
    \mathop
     { \@@_accent:nnn {#2} {#3} {#4} {##1} }
    \limits
   }
 }
%    \end{macrocode}
% \end{macro}
%
% \subsection{\cs{setmathalphabet}}
%
% \begin{macro}{\setmathalphabet}
%    \begin{macrocode}
\keys_define:nn {@@_mathface}
 {
  version .code:n =
   { \tl_set:Nn \l_@@_mversion_tl {#1} }
 }

\DeclareDocumentCommand \setmathfontface { m O{} m O{} }
 {
  \tl_clear:N \l_@@_mversion_tl

  \keys_set_known:nnN {@@_mathface} {#2,#4} \l_@@_keyval_clist
  \exp_args:Nnx \fontspec_set_family:Nxn \l_@@_tmpa_tl
   { ItalicFont={}, BoldFont={}, \exp_not:V \l_@@_keyval_clist } {#3}

  \tl_if_empty:NT \l_@@_mversion_tl
   {
    \tl_set:Nn \l_@@_mversion_tl {normal}
    \DeclareMathAlphabet #1 {\g_fontspec_encoding_tl} {\l_@@_tmpa_tl} {\mddefault} {\updefault}
   }
  \SetMathAlphabet #1 {\l_@@_mversion_tl} {\g_fontspec_encoding_tl} {\l_@@_tmpa_tl} {\mddefault} {\updefault}

  % integrate with fontspec's \setmathrm etc:
  \tl_case:Nn #1
   {
    \mathrm { \cs_set_eq:NN \g__fontspec_mathrm_tl \l_@@_tmpa_tl }
    \mathsf { \cs_set_eq:NN \g__fontspec_mathsf_tl \l_@@_tmpa_tl }
    \mathtt { \cs_set_eq:NN \g__fontspec_mathtt_tl \l_@@_tmpa_tl }
   }
 }

\@onlypreamble \setmathfontface
%    \end{macrocode}
% Note that \LaTeX's SetMathAlphabet simply doesn't work to "reset" a maths alphabet font after \verb"\begin{document}", so unlike most of the other maths commands around we still restrict this one to the preamble.
% \end{macro}
%
% \begin{macro}{\setoperatorfont}
% TODO: add check?
%    \begin{macrocode}
\DeclareDocumentCommand \setoperatorfont {m}
 { \tl_set:Nn \g_@@_operator_mathfont_tl {#1} }
\setoperatorfont{\mathrm}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Hooks into \pkg{fontspec}}
%
% Historically, \cs{mathrm} and so on were completely overwritten by \pkg{unicode-math}, and \pkg{fontspec}'s methods for setting these fonts in the classical manner were bypassed.
%
% While we could now re-activate the way that \pkg{fontspec} does the following, because we can now change maths fonts whenever it's better to define new commands in \pkg{unicode-math} to define the \cs{mathXYZ} fonts.
%
% \subsubsection{Text font}
%    \begin{macrocode}
\cs_generate_variant:Nn \tl_if_eq:nnT {o}
\cs_set:Nn \__fontspec_setmainfont_hook:nn
  {
    \tl_if_eq:onT {\g__fontspec_mathrm_tl} {\rmdefault}
      {
%<XE>   \fontspec_set_family:Nnn \g__fontspec_mathrm_tl {#1} {#2}
%<LU>   \fontspec_set_family:Nnn \g__fontspec_mathrm_tl {Renderer=Basic,#1} {#2}
        \SetMathAlphabet\mathrm{normal}\g_fontspec_encoding_tl\g__fontspec_mathrm_tl\mddefault\updefault
        \SetMathAlphabet\mathit{normal}\g_fontspec_encoding_tl\g__fontspec_mathrm_tl\mddefault\itdefault
        \SetMathAlphabet\mathbf{normal}\g_fontspec_encoding_tl\g__fontspec_mathrm_tl\bfdefault\updefault
      }
  }

\cs_set:Nn \__fontspec_setsansfont_hook:nn
  {
    \tl_if_eq:onT {\g__fontspec_mathsf_tl} {\sfdefault}
      {
%<XE>   \fontspec_set_family:Nnn \g__fontspec_mathsf_tl {#1} {#2}
%<LU>   \fontspec_set_family:Nnn \g__fontspec_mathsf_tl {Renderer=Basic,#1} {#2}
        \SetMathAlphabet\mathsf{normal}\g_fontspec_encoding_tl\g__fontspec_mathsf_tl\mddefault\updefault
        \SetMathAlphabet\mathsf{bold}  \g_fontspec_encoding_tl\g__fontspec_mathsf_tl\bfdefault\updefault
      }
  }

\cs_set:Nn \__fontspec_setmonofont_hook:nn
  {
    \tl_if_eq:onT {\g__fontspec_mathtt_tl} {\ttdefault}
      {
%<XE>   \fontspec_set_family:Nnn \g__fontspec_mathtt_tl {#1} {#2}
%<LU>   \fontspec_set_family:Nnn \g__fontspec_mathtt_tl {Renderer=Basic,#1} {#2}
        \SetMathAlphabet\mathtt{normal}\g_fontspec_encoding_tl\g__fontspec_mathtt_tl\mddefault\updefault
        \SetMathAlphabet\mathtt{bold}  \g_fontspec_encoding_tl\g__fontspec_mathtt_tl\bfdefault\updefault
      }
  }
%    \end{macrocode}
%
% \subsubsection{Maths font}
% If the maths fonts are set explicitly, then the text commands above will not execute their branches to set the maths font alphabets.
%    \begin{macrocode}
\cs_set:Nn \__fontspec_setmathrm_hook:nn
  {
    \SetMathAlphabet\mathrm{normal}\g_fontspec_encoding_tl\g__fontspec_mathrm_tl\mddefault\updefault
    \SetMathAlphabet\mathit{normal}\g_fontspec_encoding_tl\g__fontspec_mathrm_tl\mddefault\itdefault
    \SetMathAlphabet\mathbf{normal}\g_fontspec_encoding_tl\g__fontspec_mathrm_tl\bfdefault\updefault
  }
\cs_set:Nn \__fontspec_setboldmathrm_hook:nn
  {
    \SetMathAlphabet\mathrm{bold}\g_fontspec_encoding_tl\g__fontspec_bfmathrm_tl\mddefault\updefault
    \SetMathAlphabet\mathbf{bold}\g_fontspec_encoding_tl\g__fontspec_bfmathrm_tl\bfdefault\updefault
    \SetMathAlphabet\mathit{bold}\g_fontspec_encoding_tl\g__fontspec_bfmathrm_tl\mddefault\itdefault
  }
\cs_set:Nn \__fontspec_setmathsf_hook:nn
  {
    \SetMathAlphabet\mathsf{normal}\g_fontspec_encoding_tl\g__fontspec_mathsf_tl\mddefault\updefault
    \SetMathAlphabet\mathsf{bold}  \g_fontspec_encoding_tl\g__fontspec_mathsf_tl\bfdefault\updefault
  }
\cs_set:Nn \__fontspec_setmathtt_hook:nn
  {
    \SetMathAlphabet\mathtt{normal}\g_fontspec_encoding_tl\g__fontspec_mathtt_tl\mddefault\updefault
    \SetMathAlphabet\mathtt{bold}  \g_fontspec_encoding_tl\g__fontspec_mathtt_tl\bfdefault\updefault
  }
%    \end{macrocode}
%
%
% \subsection{The main \cs{setmathfont} macro}
%
% Using a |range| including large character sets such as \cmd\mathrel,
% \cmd\mathalpha, \etc, is \emph{very slow}!
% I hope to improve the performance somehow.
%
% \begin{macro}{\setmathfont}
% \doarg{font features (first optional argument retained for backwards compatibility)}
% \darg{font name}
% \doarg{font features}
%    \begin{macrocode}
\DeclareDocumentCommand \setmathfont { O{} m O{} }
  {
    \@@_setmathfont:nn {#1,#3} {#2}
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_set:Nn \@@_setmathfont:nn
 {
  \tl_set:Nn \l_@@_fontname_tl {#2}
  \@@_init:
%    \end{macrocode}
% Grab the current size information:
% (is this robust enough? Maybe it should be preceded by \cmd\normalsize).
% The macro \cmd\S@\meta{size}
% contains the definitions of the sizes used for maths letters, subscripts and subsubscripts in
% \cmd\tf@size, \cmd\sf@size, and \cmd\ssf@size, respectively.
%    \begin{macrocode}
  \cs_if_exist:cF { S@ \f@size } { \calculate@math@sizes }
  \csname S@\f@size\endcsname
%    \end{macrocode}
% Parse options and tell people what's going on:
%    \begin{macrocode}
  \keys_set_known:nnN {unicode-math} {#1} \l_@@_unknown_keys_clist
  \bool_if:NT \l_@@_init_bool { \@@_log:n {default-math-font} }
%    \end{macrocode}
% Use \pkg{fontspec} to select a font to use.
% After loading the font, we detect what sizes it recommends for scriptsize and scriptscriptsize, so after setting those values appropriately, we reload the font to take these into account.
%    \begin{macrocode}

%<debug>  \csname TIC\endcsname
  \@@_fontspec_select_font:
%<debug>  \csname TOC\endcsname
  \bool_if:nT { \l_@@_ot_math_bool && !\g_@@_mainfont_already_set_bool }
   {
    \@@_declare_math_sizes:
    \@@_fontspec_select_font:
   }
%    \end{macrocode}
% Now define |\@@_symfont_tl| as the \LaTeX\ math font to access everything:
%    \begin{macrocode}
  \cs_if_exist:cF { sym \@@_symfont_tl }
    {
      \DeclareSymbolFont{\@@_symfont_tl}
        {\encodingdefault}{\l_@@_family_tl}{\mddefault}{\updefault}
    }
  \SetSymbolFont{\@@_symfont_tl}{\l_@@_mversion_tl}
    {\encodingdefault}{\l_@@_family_tl}{\mddefault}{\updefault}
%    \end{macrocode}
% Set the bold math version.
%    \begin{macrocode}
  \tl_set:Nn \l_@@_tmpa_tl {normal}
  \tl_if_eq:NNT \l_@@_mversion_tl \l_@@_tmpa_tl
    {
     \SetSymbolFont{\@@_symfont_tl}{bold}
      {\encodingdefault}{\l_@@_family_tl}{\bfdefault}{\updefault}
    }
%    \end{macrocode}
% Declare the math sizes (i.e., scaling of superscripts) for the specific
% values for this font,
% and set defaults for math fams two and three for legacy compatibility:
%    \begin{macrocode}
  \bool_if:nT { \l_@@_ot_math_bool && !\g_@@_mainfont_already_set_bool }
   {
    \bool_set_true:N \g_@@_mainfont_already_set_bool
    \@@_setup_legacy_fam_two:
    \@@_setup_legacy_fam_three:
   }
%    \end{macrocode}
% And now we input every single maths char.
%    \begin{macrocode}
%<debug>  \csname TIC\endcsname
  \@@_input_math_symbol_table:
%<debug>  \csname TOC\endcsname
%    \end{macrocode}
% Finally,
% \begin{itemize}
% \item Remap symbols that don't take their natural mathcode
% \item Activate any symbols that need to be math-active
% \item Enable wide/narrow accents
% \item Assign delimiter codes for symbols that need to grow
% \item Setup the maths alphabets (\cs{mathbf} etc.)
% \end{itemize}
%    \begin{macrocode}
  \@@_remap_symbols:
  \@@_setup_mathactives:
  \@@_setup_delcodes:
%<debug>  \csname TIC\endcsname
  \@@_setup_alphabets:
%<debug>  \csname TOC\endcsname
  \@@_setup_negations:
%    \end{macrocode}
% Prevent spaces, and that's it:
%    \begin{macrocode}
  \ignorespaces
 }
%    \end{macrocode}
% \end{macro}
%
% Backward compatibility alias.
%    \begin{macrocode}
\cs_set_eq:NN \resetmathfont \setmathfont
%    \end{macrocode}
%
% \begin{macro}{\@@_init:}
%    \begin{macrocode}
\cs_new:Nn \@@_init:
 {
%    \end{macrocode}
% \begin{itemize}
% \item Initially assume we're using a proper OpenType font with unicode maths.
%    \begin{macrocode}
  \bool_set_true:N  \l_@@_ot_math_bool
%    \end{macrocode}
% \item Erase any conception \LaTeX\ has of previously defined math symbol fonts;
% this allows \cmd\DeclareSymbolFont\ at any point in the document.
%    \begin{macrocode}
  \cs_set_eq:NN \glb@currsize \scan_stop:
%    \end{macrocode}
% \item To start with, assume we're defining the font for every math symbol character.
%    \begin{macrocode}
  \bool_set_true:N \l_@@_init_bool
  \seq_clear:N \l_@@_char_range_seq
  \clist_clear:N \l_@@_char_nrange_clist
  \seq_clear:N \l_@@_mathalph_seq
  \seq_clear:N \l_@@_missing_alph_seq
%    \end{macrocode}
% \item By default use the `normal' math version.
%    \begin{macrocode}
  \tl_set:Nn \l_@@_mversion_tl {normal}
%    \end{macrocode}
% \item Other range initialisations.
%    \begin{macrocode}
  \tl_set:Nn \@@_symfont_tl {operators}
  \cs_set_eq:NN \_@@_sym:nnn \@@_process_symbol_noparse:nnn
  \cs_set_eq:NN \@@_set_mathalphabet_char:nnn \@@_mathmap_noparse:nnn
  \cs_set_eq:NN \@@_remap_symbol:nnn \@@_remap_symbol_noparse:nnn
  \cs_set_eq:NN \@@_maybe_init_alphabet:n \@@_init_alphabet:n
  \cs_set_eq:NN \@@_map_char_single:nn \@@_map_char_noparse:nn
  \cs_set_eq:NN \@@_assign_delcode:nn \@@_assign_delcode_noparse:nn
  \cs_set_eq:NN \@@_make_mathactive:nNN \@@_make_mathactive_noparse:nNN
%    \end{macrocode}
% \item Define default font features for the script and scriptscript font.
%    \begin{macrocode}
  \tl_set:Nn \l_@@_script_features_tl  {Style=MathScript}
  \tl_set:Nn \l_@@_sscript_features_tl {Style=MathScriptScript}
  \tl_set_eq:NN \l_@@_script_font_tl   \l_@@_fontname_tl
  \tl_set_eq:NN \l_@@_sscript_font_tl  \l_@@_fontname_tl
%    \end{macrocode}
% \end{itemize}
%    \begin{macrocode}
 }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_declare_math_sizes:}
% Set the math sizes according to the recommended font parameters:
%    \begin{macrocode}
\cs_new:Nn \@@_declare_math_sizes:
  {
%<*LU>
    \fp_compare:nF { \@@_script_style_size:n {ScriptPercentScaleDown} == 0 }
      {
        \DeclareMathSizes { \f@size } { \f@size }
          { \@@_script_style_size:n {ScriptPercentScaleDown} }
          { \@@_script_style_size:n {ScriptScriptPercentScaleDown} }
      }
%</LU>
%<*XE>
    \dim_compare:nF { \fontdimen 10 \l_@@_font == 0pt }
      {
        \DeclareMathSizes { \f@size } { \f@size }
          { \@@_fontdimen_to_scale:nn {10} {\l_@@_font} }
          { \@@_fontdimen_to_scale:nn {11} {\l_@@_font} }
      }
%</XE>
  }
%    \end{macrocode}
% \end{macro}
%
%<*LU>
% \begin{macro}{\@@_script_style_size:n}
% Determine script- and scriptscriptstyle sizes using luaotfload:
%   \begin{macrocode}
\cs_new:Nn \@@_script_style_size:n
  {
    \fp_eval:n {\directlua{tex.sprint(luaotfload.aux.get_math_dimension("l_@@_font","#1"))} * \f@size / 100 }
  }
%   \end{macrocode}
% \end{macro}
%</LU>
%
%
% \begin{macro}{\@@_setup_legacy_fam_two:}
% \TeX\ won't load the same font twice at the same scale, so we need to magnify this one by an imperceptable amount.
%    \begin{macrocode}
\cs_new:Nn \@@_setup_legacy_fam_two:
  {
    \fontspec_set_family:Nxn \l_@@_family_tl
      {
      \l_@@_font_keyval_tl,
      Scale=1.00001,
      FontAdjustment =
       {
        \fontdimen8\font= \@@_get_fontparam:nn {43} {FractionNumeratorDisplayStyleShiftUp}\relax
        \fontdimen9\font= \@@_get_fontparam:nn {42} {FractionNumeratorShiftUp}\relax
        \fontdimen10\font=\@@_get_fontparam:nn {32} {StackTopShiftUp}\relax
        \fontdimen11\font=\@@_get_fontparam:nn {45} {FractionDenominatorDisplayStyleShiftDown}\relax
        \fontdimen12\font=\@@_get_fontparam:nn {44} {FractionDenominatorShiftDown}\relax
        \fontdimen13\font=\@@_get_fontparam:nn {21} {SuperscriptShiftUp}\relax
        \fontdimen14\font=\@@_get_fontparam:nn {21} {SuperscriptShiftUp}\relax
        \fontdimen15\font=\@@_get_fontparam:nn {22} {SuperscriptShiftUpCramped}\relax
        \fontdimen16\font=\@@_get_fontparam:nn {18} {SubscriptShiftDown}\relax
        \fontdimen17\font=\@@_get_fontparam:nn {18} {SubscriptShiftDownWithSuperscript}\relax
        \fontdimen18\font=\@@_get_fontparam:nn {24} {SuperscriptBaselineDropMax}\relax
        \fontdimen19\font=\@@_get_fontparam:nn {20} {SubscriptBaselineDropMin}\relax
        \fontdimen20\font=0pt\relax % delim1 = FractionDelimiterDisplaySize
        \fontdimen21\font=0pt\relax % delim2 = FractionDelimiterSize
        \fontdimen22\font=\@@_get_fontparam:nn {15} {AxisHeight}\relax
       }
      } {\l_@@_fontname_tl}
    \SetSymbolFont{symbols}{\l_@@_mversion_tl}
      {\encodingdefault}{\l_@@_family_tl}{\mddefault}{\updefault}

    \tl_set:Nn \l_@@_tmpa_tl {normal}
    \tl_if_eq:NNT \l_@@_mversion_tl \l_@@_tmpa_tl
      {
      \SetSymbolFont{symbols}{bold}
        {\encodingdefault}{\l_@@_family_tl}{\bfdefault}{\updefault}
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_setup_legacy_fam_three:}
% Similarly, this font is shrunk by an imperceptable amount for \TeX\ to load it again.
%    \begin{macrocode}
\cs_new:Nn \@@_setup_legacy_fam_three:
  {
    \fontspec_set_family:Nxn \l_@@_family_tl
      {
      \l_@@_font_keyval_tl,
      Scale=0.99999,
      FontAdjustment={
        \fontdimen8\font= \@@_get_fontparam:nn {48} {FractionRuleThickness}\relax
        \fontdimen9\font= \@@_get_fontparam:nn {28} {UpperLimitGapMin}\relax
        \fontdimen10\font=\@@_get_fontparam:nn {30} {LowerLimitGapMin}\relax
        \fontdimen11\font=\@@_get_fontparam:nn {29} {UpperLimitBaselineRiseMin}\relax
        \fontdimen12\font=\@@_get_fontparam:nn {31} {LowerLimitBaselineDropMin}\relax
        \fontdimen13\font=0pt\relax
      }
    } {\l_@@_fontname_tl}
    \SetSymbolFont{largesymbols}{\l_@@_mversion_tl}
      {\encodingdefault}{\l_@@_family_tl}{\mddefault}{\updefault}

    \tl_set:Nn \l_@@_tmpa_tl {normal}
    \tl_if_eq:NNT \l_@@_mversion_tl \l_@@_tmpa_tl
      {
      \SetSymbolFont{largesymbols}{bold}
        {\encodingdefault}{\l_@@_family_tl}{\bfdefault}{\updefault}
      }
  }
%    \end{macrocode}
% \end{macro}
%
%
%    \begin{macrocode}
\cs_new:Nn \@@_get_fontparam:nn
%<XE>  { \the\fontdimen#1\l_@@_font\relax }
%<LU>  { \directlua{fontspec.mathfontdimen("l_@@_font","#2")} }
%    \end{macrocode}
%
%
%
% \begin{macro}{\@@_fontspec_select_font:}
% Select the font with \cs{fontspec} and define \cs{l_@@_font} from it.
%    \begin{macrocode}
\cs_new:Nn \@@_fontspec_select_font:
 {
  \tl_set:Nx \l_@@_font_keyval_tl {
%<LU>     Renderer = Basic,
    BoldItalicFont = {}, ItalicFont = {},
    Script = Math,
    SizeFeatures =
     {
      {
       Size = \tf@size-
      } ,
      {
       Size = \sf@size-\tf@size ,
       Font = \l_@@_script_font_tl ,
       \l_@@_script_features_tl
      } ,
      {
       Size = -\sf@size ,
       Font = \l_@@_sscript_font_tl ,
       \l_@@_sscript_features_tl
      }
     } ,
    \l_@@_unknown_keys_clist
  }
  \fontspec_set_fontface:NNxn \l_@@_font \l_@@_family_tl
    {\l_@@_font_keyval_tl} {\l_@@_fontname_tl}
%    \end{macrocode}
% Check whether we're using a real maths font:
%    \begin{macrocode}
  \group_begin:
    \fontfamily{\l_@@_family_tl}\selectfont
    \fontspec_if_script:nF {math} {\bool_gset_false:N \l_@@_ot_math_bool}
  \group_end:
 }
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Functions for setting up symbols with mathcodes}
% \seclabel{mathsymbol}
%
% \begin{macro}{\@@_process_symbol_noparse:nnn}
% \begin{macro}{\@@_process_symbol_parse:nnn}
% If the \feat{range} font feature has been used, then only
% a subset of the Unicode glyphs are to be defined.
% See \secref{rangeproc} for the code that enables this.
%    \begin{macrocode}
\cs_set:Nn \@@_process_symbol_noparse:nnn
 {
  \@@_set_mathsymbol:nNNn {\@@_symfont_tl} #2 #3 {#1}
 }
%    \end{macrocode}
%    \begin{macrocode}
\cs_set:Nn \@@_process_symbol_parse:nnn
 {
  \@@_if_char_spec:nNNT {#1} {#2} {#3}
   {
    \@@_process_symbol_noparse:nnn {#1} {#2} {#3}
   }
 }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@@_remap_symbols:}
% \begin{macro}{\@@_remap_symbol_noparse:nnn}
% \begin{macro}{\@@_remap_symbol_parse:nnn}
% This function is used to define the mathcodes for those chars which should
% be mapped to a different glyph than themselves.
%    \begin{macrocode}
\cs_new:Npn \@@_remap_symbols:
 {
  \@@_remap_symbol:nnn{`\-}{\mathbin}{"02212}% hyphen to minus
  \@@_remap_symbol:nnn{`\*}{\mathbin}{"02217}% text asterisk to "centred asterisk"
  \bool_if:NF \g_@@_literal_colon_bool
   {
    \@@_remap_symbol:nnn{`\:}{\mathrel}{"02236}% colon to ratio (i.e., punct to rel)
   }
 }
%    \end{macrocode}
% \end{macro}
% Where |\@@_remap_symbol:nnn| is defined to be one of these two, depending
% on the range setup:
%    \begin{macrocode}
\cs_new:Nn \@@_remap_symbol_parse:nnn
 {
  \@@_if_char_spec:nNNT {#3} {\@nil} {#2}
   { \@@_remap_symbol_noparse:nnn {#1} {#2} {#3} }
 }
\cs_new:Nn \@@_remap_symbol_noparse:nnn
 {
  \clist_map_inline:nn {#1}
   { \@@_set_mathcode:nnnn {##1} {#2} {\@@_symfont_tl} {#3} }
 }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \subsubsection{Active math characters}
%
% There are more math active chars later in the subscript/superscript section.
% But they don't need to be able to be typeset directly.
%
% \begin{macro}{\@@_setup_mathactives:}
%    \begin{macrocode}
\cs_new:Npn \@@_setup_mathactives:
 {
  \@@_make_mathactive:nNN {"2032} \@@_prime_single_mchar \mathord
  \@@_make_mathactive:nNN {"2033} \@@_prime_double_mchar \mathord
  \@@_make_mathactive:nNN {"2034} \@@_prime_triple_mchar \mathord
  \@@_make_mathactive:nNN {"2057} \@@_prime_quad_mchar   \mathord
  \@@_make_mathactive:nNN {"2035} \@@_backprime_single_mchar \mathord
  \@@_make_mathactive:nNN {"2036} \@@_backprime_double_mchar \mathord
  \@@_make_mathactive:nNN {"2037} \@@_backprime_triple_mchar \mathord
  \@@_make_mathactive:nNN {`\'} \mathstraightquote \mathord
  \@@_make_mathactive:nNN {`\`} \mathbacktick      \mathord
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_make_mathactive:nNN}
% Makes |#1| a mathactive char, and gives cs |#2| the meaning of mathchar |#1|
% with class |#3|.
% You are responsible for giving active |#1| a particular meaning!
%    \begin{macrocode}
\cs_new:Nn \@@_make_mathactive_parse:nNN
  {
    \@@_if_char_spec:nNNT {#1} #2 #3
      { \@@_make_mathactive_noparse:nNN {#1} #2 #3 }
  }
\cs_new:Nn \@@_make_mathactive_noparse:nNN
  {
    \@@_set_mathchar:NNnn #2 #3 {\@@_symfont_tl} {#1}
    \@@_char_gmake_mathactive:n {#1}
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Delimiter codes}
%
%
% \begin{macro}{\@@_assign_delcode:nn}
%    \begin{macrocode}
\cs_new:Nn \@@_assign_delcode_noparse:nn
 {
  \@@_set_delcode:nnn \@@_symfont_tl {#1} {#2}
 }
\cs_new:Nn \@@_assign_delcode_parse:nn
 {
  \@@_if_char_spec:nNNT {#2} {\@nil} {\@nil}
   {
    \@@_assign_delcode_noparse:nn {#1} {#2}
   }
 }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_assign_delcode:n}
% Shorthand.
%    \begin{macrocode}
\cs_new:Nn \@@_assign_delcode:n { \@@_assign_delcode:nn {#1} {#1} }
%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@@_setup_delcodes:}
% Some symbols that aren't mathopen/mathclose still need to have delimiter codes assigned.
% The list of vertical arrows may be incomplete.
% On the other hand, many fonts won't support them all being stretchy.
% And some of them are probably not meant to stretch, either. But adding them here doesn't hurt.
%    \begin{macrocode}
\cs_new:Npn \@@_setup_delcodes:
 {
  % ensure \left. and \right. work:
  \@@_set_delcode:nnn \@@_symfont_tl {`\.} {\c_zero}
  % this is forcefully done to fix a bug -- indicates a larger problem!

  \@@_assign_delcode:nn {`\/}   {\g_@@_slash_delimiter_usv}
  \@@_assign_delcode:nn {"2044} {\g_@@_slash_delimiter_usv} % fracslash
  \@@_assign_delcode:nn {"2215} {\g_@@_slash_delimiter_usv} % divslash
  \@@_assign_delcode:n {"005C} % backslash
  \@@_assign_delcode:nn {`\<} {"27E8} % angle brackets with ascii notation
  \@@_assign_delcode:nn {`\>} {"27E9} % angle brackets with ascii notation
  \@@_assign_delcode:n {"2191} % up arrow
  \@@_assign_delcode:n {"2193} % down arrow
  \@@_assign_delcode:n {"2195} % updown arrow
  \@@_assign_delcode:n {"219F} % up arrow twohead
  \@@_assign_delcode:n {"21A1} % down arrow twohead
  \@@_assign_delcode:n {"21A5} % up arrow from bar
  \@@_assign_delcode:n {"21A7} % down arrow from bar
  \@@_assign_delcode:n {"21A8} % updown arrow from bar
  \@@_assign_delcode:n {"21BE} % up harpoon right
  \@@_assign_delcode:n {"21BF} % up harpoon left
  \@@_assign_delcode:n {"21C2} % down harpoon right
  \@@_assign_delcode:n {"21C3} % down harpoon left
  \@@_assign_delcode:n {"21C5} % arrows up down
  \@@_assign_delcode:n {"21F5} % arrows down up
  \@@_assign_delcode:n {"21C8} % arrows up up
  \@@_assign_delcode:n {"21CA} % arrows down down
  \@@_assign_delcode:n {"21D1} % double up arrow
  \@@_assign_delcode:n {"21D3} % double down arrow
  \@@_assign_delcode:n {"21D5} % double updown arrow
  \@@_assign_delcode:n {"21DE} % up arrow double stroke
  \@@_assign_delcode:n {"21DF} % down arrow double stroke
  \@@_assign_delcode:n {"21E1} % up arrow dashed
  \@@_assign_delcode:n {"21E3} % down arrow dashed
  \@@_assign_delcode:n {"21E7} % up white arrow
  \@@_assign_delcode:n {"21E9} % down white arrow
  \@@_assign_delcode:n {"21EA} % up white arrow from bar
  \@@_assign_delcode:n {"21F3} % updown white arrow
 }
%    \end{macrocode}
% \end{macro}
%
%
%
%
% \subsection{(Big) operators}
%
% Turns out that \XeTeX\ is clever enough to deal with big operators for us
% automatically with \cmd\Umathchardef. Amazing!
%
% However, the limits aren't set automatically; that is, we want to define,
% a la Plain \TeX\ \etc, |\def\int{\intop\nolimits}|, so there needs to be a
% transformation from \cmd\int\ to \cmd\intop\ during the expansion of
% \cmd\_@@_sym:nnn\ in the appropriate contexts.
%
% \begin{macro}{\l_@@_nolimits_tl}
% This macro is a sequence containing those maths operators that require a
% \cmd\nolimits\ suffix.
% This list is used when processing |unicode-math-table.tex| to define such
% commands automatically (see the macro \cs{@@_set_mathsymbol:nNNn}).
% I've chosen essentially just the operators that look like integrals;
% hopefully a better mathematician can help me out here.
% I've a feeling that it's more useful \emph{not} to include the multiple
% integrals such as $\iiiint$, but that might be a matter of preference.
%    \begin{macrocode}
\tl_new:N \l_@@_nolimits_tl
\tl_set:Nn \l_@@_nolimits_tl
 {
  \int\iint\iiint\iiiint\oint\oiint\oiiint
  \intclockwise\varointclockwise\ointctrclockwise\sumint
  \intbar\intBar\fint\cirfnint\awint\rppolint
  \scpolint\npolint\pointint\sqint\intlarhk\intx
  \intcap\intcup\upint\lowint
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\addnolimits}
% This macro appends material to the macro containing the list of operators
% that don't take limits.
%    \begin{macrocode}
\DeclareDocumentCommand \addnolimits {m}
 {
  \tl_put_right:Nn \l_@@_nolimits_tl {#1}
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\removenolimits}
% Can this macro be given a better name?
% It removes an item from the nolimits list.
%    \begin{macrocode}
\DeclareDocumentCommand \removenolimits {m}
 {
  \tl_remove_all:Nn \l_@@_nolimits_tl {#1}
 }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Radicals}
%
% The radical for square root is organised in \cs{@@_set_mathsymbol:nNNn}.
% I think it's the only radical ever.
% (Actually, there is also \cs{cuberoot} and \cs{fourthroot}, but they don't
%  seem to behave as proper radicals.)
%
% Also, what about right-to-left square roots?
%
% \begin{macro}{\l_@@_radicals_tl}
% We organise radicals in the same way as nolimits-operators.
%    \begin{macrocode}
\tl_new:N \l_@@_radicals_tl
\tl_set:Nn \l_@@_radicals_tl {\sqrt \longdivision}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Maths accents}
%
% Maths accents should just work \emph{if they are available in the font}.
%
% \subsection{Common interface for font parameters}
%
% \XeTeX\ and \LuaTeX\ have different interfaces for math font parameters.
% We use \LuaTeX’s interface because it’s much better, but rename the primitives to be more \LaTeX3-like.
% There are getter and setter commands for each font parameter.
% The names of the parameters is derived from the \LuaTeX\ names, with underscores inserted between words.
% For every parameter \cs{Umath\meta{\LuaTeX\ name}}, we define an expandable getter command \cs{@@_\meta{\LaTeX3 name}:N} and a protected setter command \cs{@@_set_\meta{\LaTeX3 name}:Nn}.
% The getter command takes one of the style primitives (\cs{displaystyle} etc.)\ and expands to the font parameter, which is a \meta{dimension}.
% The setter command takes a style primitive and a dimension expression, which is parsed with \cs{dim_eval:n}.
%
% Often, the mapping between font dimensions and font parameters is bijective, but there are cases which require special attention:
% \begin{itemize}
% \item Some parameters map to different dimensions in display and non-display styles.
% \item Likewise, one parameter maps to different dimensions in non-cramped and cramped styles.
% \item There are a few parameters for which \XeTeX\ doesn’t seem to provide \cs{fontdimen}s; in this case the getter and setter commands are left undefined.
% \end{itemize}
%
% \paragraph{Cramped style tokens}
% \LuaTeX\ has \cs{crampeddisplaystyle} etc.,\ but they are loaded as \cs{luatexcrampeddisplaystyle} etc.\ by the \pkg{luatextra} package.
% \XeTeX, however, doesn’t have these primitives, and their syntax cannot really be emulated.
% Nevertheless, we define these commands as quarks, so they can be used as arguments to the font parameter commands (but nowhere else).
% Making these commands available is necessary because we need to make a distinction between cramped and non-cramped styles for one font parameter.
%
% \begin{macro}{\@@_new_cramped_style:N}
% \darg{command}
% Define \meta{command} as a new cramped style switch.
% For \LuaTeX, simply rename the correspronding primitive if it is not
% already defined.
% For \XeTeX, define \meta{command} as a new quark.
%    \begin{macrocode}
\cs_new_protected_nopar:Nn \@@_new_cramped_style:N
%<XE>  { \quark_new:N #1 }
%<LU>  {
%<LU>    \cs_if_exist:NF #1
%<LU>      { \cs_new_eq:Nc #1 { luatex \cs_to_str:N #1 } }
%<LU>  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\crampeddisplaystyle}
% \begin{macro}{\crampedtextstyle}
% \begin{macro}{\crampedscriptstyle}
% \begin{macro}{\crampedscriptscriptstyle}
% The cramped style commands.
%    \begin{macrocode}
\@@_new_cramped_style:N \crampeddisplaystyle
\@@_new_cramped_style:N \crampedtextstyle
\@@_new_cramped_style:N \crampedscriptstyle
\@@_new_cramped_style:N \crampedscriptscriptstyle
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \paragraph{Font dimension mapping}
% Font parameters may differ between the styles.
% \LuaTeX\ accounts for this by having the parameter primitives take a style token argument.
% To replicate this behavior in \XeTeX, we have to map style tokens to specific combinations of font dimension numbers and math fonts (\cs{textfont} etc.).
%
% \begin{macro}{\@@_font_dimen:Nnnnn}
% \darg{style token}
% \darg{font dimen for display style}
% \darg{font dimen for cramped display style}
% \darg{font dimen for non-display styles}
% \darg{font dimen for cramped non-display styles}
% Map math style to \XeTeX\ math font dimension.
% \meta{style token} must be one of the style switches (\cs{displaystyle}, \cs{crampeddisplaystyle}, \dots).
% The other parameters are integer constants referring to font dimension numbers.
% The macro expands to a dimension which contains the appropriate font dimension.
%    \begin{macrocode}
%<*XE>
  \cs_new_nopar:Npn \@@_font_dimen:Nnnnn #1 #2 #3 #4 #5 {
    \fontdimen
    \cs_if_eq:NNTF #1 \displaystyle {
      #2 \textfont
    } {
      \cs_if_eq:NNTF #1 \crampeddisplaystyle {
        #3 \textfont
      } {
        \cs_if_eq:NNTF #1 \textstyle {
          #4 \textfont
        } {
          \cs_if_eq:NNTF #1 \crampedtextstyle {
            #5 \textfont
          } {
            \cs_if_eq:NNTF #1 \scriptstyle {
              #4 \scriptfont
            } {
              \cs_if_eq:NNTF #1 \crampedscriptstyle {
                #5 \scriptfont
              } {
                \cs_if_eq:NNTF #1 \scriptscriptstyle {
                  #4 \scriptscriptfont
                } {
%    \end{macrocode}
% Should we check here if the style is invalid?
%    \begin{macrocode}
                  #5 \scriptscriptfont
                }
              }
            }
          }
        }
      }
    }
%    \end{macrocode}
% Which family to use?
%    \begin{macrocode}
    \c_two
  }
%</XE>
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Font parameters}
% This paragraph contains macros for defining the font parameter interface, as well as the definition for all font parameters known to \LuaTeX.
%
% \begin{macro}{\@@_font_param:nnnnn}
% \darg{name}
% \darg{font dimension for non-cramped display style}
% \darg{font dimension for cramped display style}
% \darg{font dimension for non-cramped non-display styles}
% \darg{font dimension for cramped non-display styles}
% This macro defines getter and setter functions for the font parameter \meta{name}.
% The \LuaTeX\ font parameter name is produced by removing all underscores and prefixing the result with |Umath|.
% The \XeTeX\ font dimension numbers must be integer constants.
%    \begin{macrocode}
\cs_new_protected_nopar:Nn \@@_font_param:nnnnn
%<*XE>
{
  \@@_font_param_aux:ccnnnn { @@_ #1 :N } { @@_set_ #1 :Nn }
    { #2 } { #3 } { #4 } { #5 }
}
%</XE>
%<*LU>
{
  \tl_set:Nn \l_@@_tmpa_tl { #1 }
  \tl_remove_all:Nn \l_@@_tmpa_tl { _ }
  \@@_font_param_aux:ccc { @@_ #1 :N } { @@_set_ #1 :Nn }
    { Umath \l_@@_tmpa_tl }
}
%</LU>
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_font_param:nnn}
% \darg{name}
% \darg{font dimension for display style}
% \darg{font dimension for non-display styles}
% This macro defines getter and setter functions for the font parameter \meta{name}.
% The \LuaTeX\ font parameter name is produced by removing all underscores and prefixing the result with |Umath|.
% The \XeTeX\ font dimension numbers must be integer constants.
%    \begin{macrocode}
\cs_new_protected_nopar:Nn \@@_font_param:nnn
 {
  \@@_font_param:nnnnn { #1 } { #2 } { #2 } { #3 } { #3 }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_font_param:nn}
% \darg{name}
% \darg{font dimension}
% This macro defines getter and setter functions for the font parameter \meta{name}.
% The \LuaTeX\ font parameter name is produced by removing all underscores and prefixing the result with |Umath|.
% The \XeTeX\ font dimension number must be an integer constant.
%    \begin{macrocode}
\cs_new_protected_nopar:Nn \@@_font_param:nn
 {
  \@@_font_param:nnnnn { #1 } { #2 } { #2 } { #2 } { #2 }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_font_param:n}
% \darg{name}
% This macro defines getter and setter functions for the font parameter \meta{name}, which is considered unavailable in \XeTeX\@.
% The \LuaTeX\ font parameter name is produced by removing all underscores and prefixing the result with |Umath|.
%    \begin{macrocode}
\cs_new_protected_nopar:Nn \@@_font_param:n
%<XE>  { }
%<LU>  { \@@_font_param:nnnnn { #1 } { 0 } { 0 } { 0 } { 0 } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_font_param_aux:NNnnnn}
% \begin{macro}{\@@_font_param_aux:NNN}
% Auxiliary macros for generating font parameter accessor macros.
%    \begin{macrocode}
%<*XE>
\cs_new_protected_nopar:Nn \@@_font_param_aux:NNnnnn
  {
    \cs_new_nopar:Npn #1 ##1
     {
      \@@_font_dimen:Nnnnn ##1 { #3 } { #4 } { #5 } { #6 }
     }
    \cs_new_protected_nopar:Npn #2 ##1 ##2
     {
      #1 ##1 \dim_eval:n { ##2 }
     }
  }
\cs_generate_variant:Nn \@@_font_param_aux:NNnnnn { cc }
%</XE>
%<*LU>
\cs_new_protected_nopar:Nn \@@_font_param_aux:NNN
  {
    \cs_new_nopar:Npn #1 ##1
     {
      #3 ##1
     }
    \cs_new_protected_nopar:Npn #2 ##1 ##2
     {
      #3 ##1 \dim_eval:n { ##2 }
     }
  }
\cs_generate_variant:Nn \@@_font_param_aux:NNN { ccc }
%</LU>
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% Now all font parameters that are listed in the \LuaTeX\ reference follow.
%    \begin{macrocode}
\@@_font_param:nn { axis } { 15 }
\@@_font_param:nn { operator_size } { 13 }
\@@_font_param:n { fraction_del_size }
\@@_font_param:nnn { fraction_denom_down } { 45 } { 44 }
\@@_font_param:nnn { fraction_denom_vgap } { 50 } { 49 }
\@@_font_param:nnn { fraction_num_up } { 43 } { 42 }
\@@_font_param:nnn { fraction_num_vgap } { 47 } { 46 }
\@@_font_param:nn { fraction_rule } { 48 }
\@@_font_param:nn { limit_above_bgap } { 29 }
\@@_font_param:n { limit_above_kern }
\@@_font_param:nn { limit_above_vgap } { 28 }
\@@_font_param:nn { limit_below_bgap } { 31 }
\@@_font_param:n { limit_below_kern }
\@@_font_param:nn { limit_below_vgap } { 30 }
\@@_font_param:nn { over_delimiter_vgap } { 41 }
\@@_font_param:nn { over_delimiter_bgap } { 38 }
\@@_font_param:nn { under_delimiter_vgap } { 40 }
\@@_font_param:nn { under_delimiter_bgap } { 39 }
\@@_font_param:nn { overbar_kern } { 55 }
\@@_font_param:nn { overbar_rule } { 54 }
\@@_font_param:nn { overbar_vgap } { 53 }
\@@_font_param:n { quad }
\@@_font_param:nn { radical_kern } { 62 }
\@@_font_param:nn { radical_rule } { 61 }
\@@_font_param:nnn { radical_vgap } { 60 } { 59 }
\@@_font_param:nn { radical_degree_before } { 63 }
\@@_font_param:nn { radical_degree_after } { 64 }
\@@_font_param:nn { radical_degree_raise } { 65 }
\@@_font_param:nn { space_after_script } { 27 }
\@@_font_param:nnn { stack_denom_down } { 35 } { 34 }
\@@_font_param:nnn { stack_num_up } { 33 } { 32 }
\@@_font_param:nnn { stack_vgap } { 37 } { 36 }
\@@_font_param:nn { sub_shift_down } { 18 }
\@@_font_param:nn { sub_shift_drop } { 20 }
\@@_font_param:n { subsup_shift_down }
\@@_font_param:nn { sub_top_max } { 19 }
\@@_font_param:nn { subsup_vgap } { 25 }
\@@_font_param:nn { sup_bottom_min } { 23 }
\@@_font_param:nn { sup_shift_drop } { 24 }
\@@_font_param:nnnnn { sup_shift_up } { 21 } { 22 } { 21 } { 22 }
\@@_font_param:nn { supsub_bottom_max } { 26 }
\@@_font_param:nn { underbar_kern } { 58 }
\@@_font_param:nn { underbar_rule } { 57 }
\@@_font_param:nn { underbar_vgap } { 56 }
\@@_font_param:n { connector_overlap_min }
%    \end{macrocode}
%
% \section{Font features}
%
% \subsection{Math version}
%    \begin{macrocode}
\keys_define:nn {unicode-math}
  {
    version .code:n =
      {
        \tl_set:Nn \l_@@_mversion_tl {#1}
        \DeclareMathVersion {\l_@@_mversion_tl}
      }
  }
%    \end{macrocode}
%
% \subsection{Script and scriptscript font options}
%    \begin{macrocode}
\keys_define:nn {unicode-math}
 {
  script-features  .tl_set:N =  \l_@@_script_features_tl ,
  sscript-features .tl_set:N = \l_@@_sscript_features_tl ,
       script-font .tl_set:N =      \l_@@_script_font_tl ,
      sscript-font .tl_set:N =     \l_@@_sscript_font_tl ,
 }
%    \end{macrocode}
%
% \subsection{Range processing}
% \seclabel{rangeproc}
%
%    \begin{macrocode}
\keys_define:nn {unicode-math}
 {
  range .code:n =
   {
    \bool_set_false:N \l_@@_init_bool
%    \end{macrocode}
% Set processing functions if we're not defining the full Unicode math repetoire.
% Math symbols are defined with \cmd\_@@_sym:nnn; see \secref{mathsymbol}
% for the individual definitions
%    \begin{macrocode}
    \int_incr:N \g_@@_fam_int
    \tl_set:Nx \@@_symfont_tl {@@_fam\int_use:N\g_@@_fam_int}
    \cs_set_eq:NN \_@@_sym:nnn \@@_process_symbol_parse:nnn
    \cs_set_eq:NN \@@_set_mathalphabet_char:Nnn \@@_mathmap_parse:Nnn
    \cs_set_eq:NN \@@_remap_symbol:nnn \@@_remap_symbol_parse:nnn
    \cs_set_eq:NN \@@_maybe_init_alphabet:n \use_none:n
    \cs_set_eq:NN \@@_map_char_single:nn \@@_map_char_parse:nn
    \cs_set_eq:NN \@@_assign_delcode:nn \@@_assign_delcode_parse:nn
    \cs_set_eq:NN \@@_make_mathactive:nNN \@@_make_mathactive_parse:nNN
%    \end{macrocode}
% Proceed by filling up the various `range' seqs according to the user options.
%    \begin{macrocode}
    \seq_clear:N \l_@@_char_range_seq
    \seq_clear:N \l_@@_mclass_range_seq
    \seq_clear:N \l_@@_cmd_range_seq
    \seq_clear:N \l_@@_mathalph_seq

    \clist_map_inline:nn {#1}
     {
      \@@_if_mathalph_decl:nTF {##1}
       {
        \seq_put_right:Nx \l_@@_mathalph_seq
         {
          { \exp_not:V \l_@@_tmpa_tl }
          { \exp_not:V \l_@@_tmpb_tl }
          { \exp_not:V \l_@@_tmpc_tl }
         }
       }
       {
%    \end{macrocode}
% Four cases:
% math class matching the known list;
% single item that is a control sequence---command name;
% single item that isn't---edge case, must be 0--9;
% none of the above---char range.
%    \begin{macrocode}
        \seq_if_in:NnTF \g_@@_mathclasses_seq {##1}
          { \seq_put_right:Nn \l_@@_mclass_range_seq {##1} }
          {
            \bool_lazy_and:nnTF { \tl_if_single_p:n {##1} } { \token_if_cs_p:N ##1 }
              { \seq_put_right:Nn \l_@@_cmd_range_seq {##1} }
              { \seq_put_right:Nn \l_@@_char_range_seq {##1} }
          }
       }
     }
   }
 }
%    \end{macrocode}
%
%
% \begin{macro}{\@@_if_mathalph_decl:nTF}
% Possible forms of input:\\
% |\mathscr|\\
% |\mathscr->\mathup|\\
% |\mathscr/{Latin}|\\
% |\mathscr/{Latin}->\mathup|\\
% Outputs:\\
% |tmpa|: math style (\eg, |\mathscr|)\\
% |tmpb|: alphabets (\eg, |Latin|)\\
% |tmpc|: remap style (\eg, |\mathup|). Defaults to |tmpa|.
%
% The remap style can also be |\mathcal->stixcal|, which I marginally prefer
% in the general case.
%    \begin{macrocode}
\prg_new_conditional:Nnn \@@_if_mathalph_decl:n {TF}
 {
  \tl_set:Nn  \l_@@_tmpa_tl {#1}
  \tl_clear:N \l_@@_tmpb_tl
  \tl_clear:N \l_@@_tmpc_tl

  \tl_if_in:NnT \l_@@_tmpa_tl {->}
   { \exp_after:wN \@@_split_arrow:w \l_@@_tmpa_tl \q_nil }

  \tl_if_in:NnT \l_@@_tmpa_tl {/}
   { \exp_after:wN \@@_split_slash:w \l_@@_tmpa_tl \q_nil }

  \tl_set:Nx \l_@@_tmpa_tl { \tl_to_str:N \l_@@_tmpa_tl }
  \exp_args:NNx \tl_remove_all:Nn \l_@@_tmpa_tl { \token_to_str:N \math }
  \exp_args:NNx \tl_remove_all:Nn \l_@@_tmpa_tl { \token_to_str:N \sym }
  \tl_trim_spaces:N \l_@@_tmpa_tl

  \tl_if_empty:NT \l_@@_tmpc_tl
   { \tl_set_eq:NN \l_@@_tmpc_tl \l_@@_tmpa_tl }

  \seq_if_in:NVTF \g_@@_named_ranges_seq \l_@@_tmpa_tl
   { \prg_return_true: } { \prg_return_false: }
 }
%    \end{macrocode}
%    \begin{macrocode}
\cs_set:Npn \@@_split_arrow:w #1->#2 \q_nil
 {
  \tl_set:Nx \l_@@_tmpa_tl { \tl_trim_spaces:n {#1} }
  \tl_set:Nx \l_@@_tmpc_tl { \tl_trim_spaces:n {#2} }
 }
%    \end{macrocode}
%    \begin{macrocode}
\cs_set:Npn \@@_split_slash:w #1/#2 \q_nil
 {
  \tl_set:Nx \l_@@_tmpa_tl { \tl_trim_spaces:n {#1} }
  \tl_set:Nx \l_@@_tmpb_tl { \tl_trim_spaces:n {#2} }
 }
%    \end{macrocode}
% \end{macro}
%
% Pretty basic comma separated range processing.
% Donald Arseneau's \pkg{selectp} package has a cleverer technique.
%
% \begin{macro}{\@@_if_char_spec:nNNT}
% \darg{Unicode character slot}
% \darg{control sequence (character macro)}
% \darg{control sequence (math class)}
% \darg{code to execute}
% This macro expands to |#4|
% if any of its arguments are contained in \cmd\l_@@_char_range_seq.
% This list can contain either character ranges (for checking with |#1|) or control sequences.
% These latter can either be the command name of a specific character, \emph{or} the math
% type of one (\eg, \cmd\mathbin).
%
% Character ranges are passed to \cs{@@_if_char_spec:nNNT}, which accepts input in the form shown in \tabref{ranges}.
%
% \begin{table}[htbp]
% \centering
% \topcaption{Ranges accepted by \cs{@@_if_char_spec:nNNT}.}
% \label{tab:ranges}
% \begin{tabular}{>{\ttfamily}cc}
% \textrm{Input} & Range \\
% \hline
% x & $r=x$ \\
% x- & $r\geq x$ \\
% -y & $r\leq y$ \\
% x-y & $x \leq r \leq y$ \\
% \end{tabular}
% \end{table}
%
% We have three tests, performed sequentially in order of execution time.
% Any test finding a match jumps directly to the end.
%    \begin{macrocode}
\cs_new:Nn \@@_if_char_spec:nNNT
  {
    % math class:
    \seq_if_in:NnT \l_@@_mclass_range_seq {#3}
      { \use_none_delimit_by_q_nil:w }

    % command name:
    \seq_if_in:NnT \l_@@_cmd_range_seq {#2}
      { \use_none_delimit_by_q_nil:w }

    % character slot:
    \seq_map_inline:Nn \l_@@_char_range_seq
      {
        \@@_int_if_slot_in_range:nnT {#1} {##1}
          { \seq_map_break:n { \use_none_delimit_by_q_nil:w } }
      }

    % the following expands to nil if no match was found:
    \use_none:nnn
    \q_nil
    \use:n
      {
        \clist_put_right:Nx \l_@@_char_nrange_clist { \int_eval:n {#1} }
        #4
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_int_if_slot_in_range:nnT}
% A `numrange' is like |-2,5-8,12,17-| (can be unsorted).
%
% Four cases, four argument types:
% \begin{Verbatim}
% input    #2     #3      #4
% "1  "   [ 1] - [qn] - [   ] qs
% "1- "   [ 1] - [  ] - [qn-] qs
% " -3"   [  ] - [ 3] - [qn-] qs
% "1-3"   [ 1] - [ 3] - [qn-] qs
% \end{Verbatim}
%
%    \begin{macrocode}
\cs_new:Nn \@@_int_if_slot_in_range:nnT
  { \@@_numrange_parse:nwT {#1} #2 - \q_nil - \q_stop {#3} }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_set:Npn \@@_numrange_parse:nwT #1 #2 - #3 - #4 \q_stop #5
  {
    \tl_if_empty:nTF {#4} { \int_compare:nT {#1=#2} {#5} }
      {
    \tl_if_empty:nTF {#3} { \int_compare:nT {#1>=#2} {#5} }
      {
    \tl_if_empty:nTF {#2} { \int_compare:nT {#1<=#3} {#5} }
      {
    \int_compare:nT {#1>=#2} { \int_compare:nT {#1<=#3} {#5} }
      } } }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Resolving Greek symbol name control sequences}
%
% \begin{macro}{\@@_resolve_greek:}
% This macro defines \cmd\Alpha\dots\cmd\omega\ as their corresponding
% Unicode (mathematical italic) character. Remember that the mapping
% to upright or italic happens with the mathcode definitions, whereas these macros
% just stand for the literal Unicode characters.
%    \begin{macrocode}
\AtBeginDocument{\@@_resolve_greek:}
\cs_new:Npn \@@_resolve_greek:
 {
  \clist_map_inline:nn
   {
    Alpha,Beta,Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,
    alpha,beta,gamma,delta,epsilon,zeta,eta,theta,iota,kappa,lambda,
    Mu,Nu,Xi,Omicron,Pi,Rho,Sigma,Tau,Upsilon,Phi,Chi,Psi,Omega,
    mu,nu,xi,omicron,pi,rho,sigma,tau,upsilon,phi,chi,psi,omega,
    varTheta,varsigma,vartheta,varkappa,varrho,varpi,varepsilon,varphi
   }
   {
    \tl_set:cx {##1} { \exp_not:c { mit ##1 } }
    \tl_set:cx {up ##1} { \exp_not:N \symup \exp_not:c { ##1 } }
    \tl_set:cx {it ##1} { \exp_not:N \symit \exp_not:c { ##1 } }
   }
 }
%    \end{macrocode}
% \end{macro}
%
%
%
%
%
%
%
% \section{Maths alphabets}
% \label{part:mathmap}
%
% Defining commands like \cmd\mathrm\ is not as simple with Unicode fonts.
% In traditional \TeX{} maths font setups, you simply switch between different `families' (\cmd\fam), which is analogous to changing from one font to another---a symbol such as `a' will be upright in one font, bold in another, and so on.
%
% In pkg{unicode-math}, a different mechanism is used to switch between styles. For every letter (start with ascii a-zA-Z and numbers to keep things simple for now), they are assigned a `mathcode' with \cmd\Umathcode\ that maps from input letter to output font glyph slot. This is done with the equivalent of
% \begin{Verbatim}
% \Umathcode`\a = 7 1 "1D44E\relax
% \Umathcode`\b = 7 1 "1D44F\relax
% \Umathcode`\c = 7 1 "1D450\relax
% ...
% \end{Verbatim}
% When switching from regular letters to, say, \cmd\mathrm, we now need to execute a new mapping:
% \begin{Verbatim}
% \Umathcode`\a = 7 1 `\a\relax
% \Umathcode`\b = 7 1 `\b\relax
% \Umathcode`\c = 7 1 `\c\relax
% ...
% \end{Verbatim}
% This is fairly straightforward to perform when we're defining our own commands such as \cmd\symbf\ and so on. However, this means that `classical' \TeX\ font setups will break, because with the original mapping still in place, the engine will be attempting to insert unicode maths glyphs from a standard font.
%
% \subsection{Hooks into \LaTeXe}
%
% To overcome this, we patch \cs{use@mathgroup}.
% (An alternative is to patch \cs{extract@alph@from@version}, which constructs the \cs{mathXYZ} commands, but this method fails if the command has been defined using \cs{DeclareSymbolFontAlphabet}.)
% As far as I can tell, this is only used inside of commands such as \cs{mathXYZ}, so this shouldn't have any major side-effects.
%
%    \begin{macrocode}
\cs_set:Npn \use@mathgroup #1 #2
 {
  \mode_if_math:T % <- not sure if this is really necessary since we've just checked for mmode and raised an error if not!
   {
    \math@bgroup
      \cs_if_eq:cNF {M@\f@encoding} #1 {#1}
      \@@_switchto_literal:
      \mathgroup #2 \relax
    \math@egroup
   }
 }
%    \end{macrocode}
%
%
%
% \subsection{Setting styles}
%
% Algorithm for setting alphabet fonts.
% By default, when |range| is empty, we are in \emph{implicit} mode.
% If |range| contains the name of the math alphabet, we are in \emph{explicit}
% mode and do things slightly differently.
%
% Implicit mode:
% \begin{itemize}
% \item Try and set all of the alphabet shapes.
% \item Check for the first glyph of each alphabet to detect if the font supports each
%       alphabet shape.
% \item For alphabets that do exist, overwrite whatever's already there.
% \item For alphabets that are not supported, \emph{do nothing}.
%       (This includes leaving the old alphabet definition in place.)
% \end{itemize}
%
% Explicit mode:
% \begin{itemize}
% \item Only set the alphabets specified.
% \item Check for the first glyph of the alphabet to detect if the font contains
%       the alphabet shape in the Unicode math plane.
% \item For Unicode math alphabets, overwrite whatever's already there.
% \item Otherwise, use the \ascii\ glyph slots instead.
% \end{itemize}
%
%
%
% \subsection{Defining the math style macros}
%
% We call the different shapes that a math alphabet can be a `math style'.
% Note that different alphabets can exist within the same math style. E.g.,
% we call `bold' the math style |bf| and within it there are upper and lower
% case Greek and Roman alphabets and Arabic numerals.
%
% \begin{macro}{\@@_prepare_mathstyle:n}
% \darg{math style name (e.g., \texttt{it} or \texttt{bb})}
% Define the high level math alphabet macros (\cs{mathit}, etc.) in terms of
% unicode-math definitions. Use \cs{bgroup}/\cs{egroup} so s'scripts scan the
% whole thing.
%
% The flag \cs{l_@@_mathstyle_tl} is for other applications to query the
% current math style.
%    \begin{macrocode}
\cs_new:Nn \@@_prepare_mathstyle:n
 {
  \seq_put_right:Nn \g_@@_mathstyles_seq {#1}
  \@@_init_alphabet:n {#1}
  \cs_set:cpn {_@@_sym_#1_aux:n}
   { \use:c {@@_switchto_#1:} \math@egroup }
  \cs_set_protected:cpx {sym#1}
   {
    \exp_not:n
     {
      \math@bgroup
      \mode_if_math:F
        {
          \egroup\expandafter
          \non@alpherr\expandafter{\csname sym#1\endcsname\space}
        }
      \tl_set:Nn \l_@@_mathstyle_tl {#1}
     }
    \exp_not:c {_@@_sym_#1_aux:n}
   }
 }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_init_alphabet:n}
% \darg{math alphabet name (e.g., \texttt{it} or \texttt{bb})}
% This macro initialises the macros used to set up a math alphabet.
% First used when the math alphabet macro is first defined, but then used
% later when redefining a particular maths alphabet.
%    \begin{macrocode}
\cs_set:Nn \@@_init_alphabet:n
 {
  \@@_log:nx {alph-initialise} {#1}
  \cs_set_eq:cN {@@_switchto_#1:} \prg_do_nothing:
 }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Definition of alphabets and styles}
%
% First of all, we break up unicode into `named ranges', such as |up|, |bb|, |sfup|, and so on, which refer to specific blocks of unicode that contain various symbols (usually alphabetical symbols).
%
%    \begin{macrocode}
\cs_new:Nn \@@_new_named_range:n
 {
  \prop_new:c {g_@@_named_range_#1_prop}
 }
\clist_set:Nn \g_@@_named_ranges_clist
 {
  up, it, tt, bfup, bfit, bb , bbit, scr, bfscr, cal, bfcal,
  frak, bffrak, sfup, sfit, bfsfup, bfsfit, bfsf
 }
\clist_map_inline:Nn \g_@@_named_ranges_clist
 { \@@_new_named_range:n {#1} }
%    \end{macrocode}
%
% Each of these styles usually contains one or more `alphabets', which are currently |latin|, |Latin|, |greek|, |Greek|, |num|, and |misc|, although there's an implicit potential for more.
% |misc| is not included in the official list to avoid checking code.
%    \begin{macrocode}
\clist_new:N  \g_@@_alphabets_seq
\clist_set:Nn \g_@@_alphabets_seq { latin, Latin, greek, Greek, num }
%    \end{macrocode}
%
% Each alphabet style needs to be configured.
% This happens in the |unicode-math-alphabets.dtx| file.
%    \begin{macrocode}
\cs_new:Nn \@@_new_alphabet_config:nnn
 {
  \prop_if_exist:cF {g_@@_named_range_#1_prop}
   { \@@_warning:nnn {no-named-range} {#1} {#2} }

  \prop_gput:cnn {g_@@_named_range_#1_prop} { alpha_tl }
    {
     \prop_item:cn {g_@@_named_range_#1_prop} { alpha_tl }
     {#2}
    }
  % Q: do I need to bother removing duplicates?

  \cs_new:cn { @@_config_#1_#2:n } {#3}
 }
%    \end{macrocode}
%    \begin{macrocode}
\cs_new:Nn \@@_alphabet_config:nnn
 {
  \use:c {@@_config_#1_#2:n} {#3}
 }
%    \end{macrocode}
%    \begin{macrocode}
\prg_new_conditional:Nnn \@@_if_alphabet_exists:nn {T,TF}
 {
  \cs_if_exist:cTF {@@_config_#1_#2:n}
   \prg_return_true: \prg_return_false:
 }
%    \end{macrocode}
%
% The linking between named ranges and symbol style commands happens here.
% It's currently not using all of the machinery we're in the process of setting up above.
% Baby steps.
%    \begin{macrocode}
\cs_new:Nn \@@_default_mathalph:nnn
 {
  \seq_put_right:Nx \g_@@_named_ranges_seq { \tl_to_str:n {#1} }
  \seq_put_right:Nn \g_@@_default_mathalph_seq {{#1}{#2}{#3}}
  \prop_gput:cnn { g_@@_named_range_#1_prop } { default-alpha } {#2}
 }
\@@_default_mathalph:nnn {up    } {latin,Latin,greek,Greek,num,misc} {up    }
\@@_default_mathalph:nnn {it    } {latin,Latin,greek,Greek,misc}     {it    }
\@@_default_mathalph:nnn {bb    } {latin,Latin,num,misc}             {bb    }
\@@_default_mathalph:nnn {bbit  } {misc}                             {bbit  }
\@@_default_mathalph:nnn {scr   } {latin,Latin}                      {scr   }
\@@_default_mathalph:nnn {cal   } {Latin}                            {scr   }
\@@_default_mathalph:nnn {bfcal } {Latin}                            {bfscr }
\@@_default_mathalph:nnn {frak  } {latin,Latin}                      {frak  }
\@@_default_mathalph:nnn {tt    } {latin,Latin,num}                  {tt    }
\@@_default_mathalph:nnn {sfup  } {latin,Latin,num}                  {sfup  }
\@@_default_mathalph:nnn {sfit  } {latin,Latin}                      {sfit  }
\@@_default_mathalph:nnn {bfup  } {latin,Latin,greek,Greek,num,misc} {bfup  }
\@@_default_mathalph:nnn {bfit  } {latin,Latin,greek,Greek,misc}     {bfit  }
\@@_default_mathalph:nnn {bfscr } {latin,Latin}                      {bfscr }
\@@_default_mathalph:nnn {bffrak} {latin,Latin}                      {bffrak}
\@@_default_mathalph:nnn {bfsfup} {latin,Latin,greek,Greek,num,misc} {bfsfup}
\@@_default_mathalph:nnn {bfsfit} {latin,Latin,greek,Greek,misc}     {bfsfit}
%    \end{macrocode}
%
% \subsubsection{Define symbol style commands}
% Finally, all of the `symbol styles' commands are set up, which are the commands to access each of the named alphabet styles. There is not a one-to-one mapping between symbol style commands and named style ranges!
%    \begin{macrocode}
\clist_map_inline:nn
 {
  up, it, bfup, bfit, sfup, sfit, bfsfup, bfsfit, bfsf,
  tt, bb, bbit, scr, bfscr, cal, bfcal, frak, bffrak,
  normal, literal, sf, bf,
 }
 { \@@_prepare_mathstyle:n {#1} }
%    \end{macrocode}
%
%
% \subsubsection{New names for legacy textmath alphabet selection}
% In case a package option overwrites, say, \cs{mathbf} with \cs{symbf}.
%    \begin{macrocode}
\clist_map_inline:nn
 { rm, it, bf, sf, tt }
 { \cs_set_eq:cc { mathtext #1 } { math #1 } }
%    \end{macrocode}
% Perhaps these should actually be defined using a hypothetical unicode-math interface to creating new such styles. To come.
%
%
% \subsubsection{Replacing legacy pure-maths alphabets}
% The following are alphabets which do not have a math/text ambiguity.
%    \begin{macrocode}
\clist_map_inline:nn
 {
   normal, bb , bbit, scr, bfscr, cal, bfcal, frak, bffrak, tt,
   bfup, bfit, sfup, sfit, bfsfup, bfsfit, bfsf
 }
 {
  \cs_set:cpx { math #1 } { \exp_not:c { sym #1 } }
 }
%    \end{macrocode}
%
%
% \subsubsection{New commands for ambiguous alphabets}
%    \begin{macrocode}
\AtBeginDocument{
\clist_map_inline:nn
 { rm, it, bf, sf, tt }
 {
  \cs_set_protected:cpx { math #1 }
   {
    \exp_not:n { \bool_if:NTF  } \exp_not:c { g_@@_ math #1 _text_bool}
     { \exp_not:c { mathtext #1 } }
     { \exp_not:c { sym #1 } }
   }
 }}
%    \end{macrocode}
%
% \paragraph{Alias \cs{mathrm} as legacy name for \cs{mathup}}
%    \begin{macrocode}
\cs_set_protected:Npn \mathup { \mathrm }
\cs_set_protected:Npn \symrm  { \symup  }
%    \end{macrocode}
%
%
% \subsubsection{Fixing up \cs{operator@font}}
%
%In LaTeX maths, the command |\operator@font| is defined that switches to the |operator| mathgroup. The classic example is the |\sin| in |$\sin{x}$|; essentially we're using |\mathrm| to typeset the upright symbols, but the syntax is |{\operator@font sin}|.
%
%It turns out that hooking into |\operator@font| is hard because all other maths font selection in 2e uses |\mathrm{...}| style.
%
%Then reading source2e a little more I stumbled upon: (in the definition of |\select@group|)
%\begin{quote}
% We surround |\select@group| with braces so that functions using it can be used directly after |_| or |^|. However, if we use oldstyle syntax where the math alphabet doesn’t have arguments (ie if |\math@bgroup| is not |\bgroup|) we need to get rid of the extra group.
%\end{quote}
%So there's a trick we can use.
%Because it's late and I'm tired, I went for the first thing that jumped out at me:
%\begin{Verbatim}
%    \documentclass{article}
%    \DeclareMathAlphabet\mathfoo{OT1}{lmdh}{m}{n}
%    \begin{document}
%    \makeatletter
%    ${\operator@font Mod}\, x$
%
%    \def\operator@font{%
%      \let \math@bgroup \relax
%      \def \math@egroup {\let \math@bgroup \@@math@bgroup
%                         \let \math@egroup \@@math@egroup}%
%      \mathfoo}
%    ${\operator@font Mod}\, x$
%    \end{document}
%\end{Verbatim}
% We define a new math alphabet |\mathfoo| to select the Latin Modern Dunhill font, and then locally redefine |\math@bgroup| to allow |\mathfoo| to be used without an argument temporarily.
%
% Now that I've written this whole thing out, another solution pops to mind:
%\begin{Verbatim}
%    \documentclass{article}
%    \DeclareSymbolFont{foo}{OT1}{lmdh}{m}{n}
%    \DeclareSymbolFontAlphabet\mathfoo{foo}
%    \begin{document}
%    \makeatletter
%    ${\operator@font Mod}\, x$
%
%    \def\operator@font{\mathgroup\symfoo}
%    ${\operator@font Mod}\, x$
%    \end{document}
%\end{Verbatim}
%I guess that's the better approach!!
%
% Or perhaps I should just use |\@fontswitch| to do the first solution with a nicer wrapper. I really should read things more carefully:
% \begin{macro}{\operator@font}
%    \begin{macrocode}
\cs_set:Npn \operator@font
 {
  \@@_switchto_literal:
  \@fontswitch {} { \g_@@_operator_mathfont_tl }
 }
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Defining the math alphabets per style}
%
% \begin{macro}{\@@_setup_alphabets:}
% This function is called within \cs{setmathfont} to configure the
% mapping between characters inside math styles.
%    \begin{macrocode}
\cs_new:Npn \@@_setup_alphabets:
 {
%    \end{macrocode}
% If |range=| has been used to configure styles, those choices will be in
% |\l_@@_mathalph_seq|. If not, set up the styles implicitly:
%    \begin{macrocode}
  \seq_if_empty:NTF \l_@@_mathalph_seq
   {
    \@@_log:n {setup-implicit}
    \seq_set_eq:NN \l_@@_mathalph_seq \g_@@_default_mathalph_seq
    \bool_set_true:N \l_@@_implicit_alph_bool
    \@@_maybe_init_alphabet:n  {sf}
    \@@_maybe_init_alphabet:n  {bf}
    \@@_maybe_init_alphabet:n  {bfsf}
   }
%    \end{macrocode}
% If |range=| has been used then we're in explicit mode:
%    \begin{macrocode}
   {
    \@@_log:n {setup-explicit}
    \bool_set_false:N \l_@@_implicit_alph_bool
    \cs_set_eq:NN \@@_set_mathalphabet_char:nnn \@@_mathmap_noparse:nnn
    \cs_set_eq:NN \@@_map_char_single:nn \@@_map_char_noparse:nn
   }

  % Now perform the mapping:
  \seq_map_inline:Nn \l_@@_mathalph_seq
   {
    \tl_set:No    \l_@@_style_tl       { \use_i:nnn   ##1 }
    \clist_set:No \l_@@_alphabet_clist { \use_ii:nnn  ##1 }
    \tl_set:No    \l_@@_remap_style_tl { \use_iii:nnn ##1 }

    % If no set of alphabets is defined:
    \clist_if_empty:NT \l_@@_alphabet_clist
     {
      \cs_set_eq:NN \@@_maybe_init_alphabet:n \@@_init_alphabet:n
      \prop_get:cnN { g_@@_named_range_ \l_@@_style_tl _prop }
       { default-alpha } \l_@@_alphabet_clist
     }

    \@@_setup_math_alphabet:
   }
  \seq_if_empty:NF \l_@@_missing_alph_seq { \@@_log:n { missing-alphabets } }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_setup_math_alphabet:}
%    \begin{macrocode}
\cs_new:Nn \@@_setup_math_alphabet:
 {
%    \end{macrocode}
% First check that at least one of the alphabets for the font shape is defined
% (this process is fast) \dots
%    \begin{macrocode}
  \clist_map_inline:Nn \l_@@_alphabet_clist
   {
    \tl_set:Nn \l_@@_alphabet_tl {##1}
    \@@_if_alphabet_exists:nnTF \l_@@_style_tl \l_@@_alphabet_tl
     {
      \str_if_eq_x:nnTF {\l_@@_alphabet_tl} {misc}
       {
        \@@_maybe_init_alphabet:n \l_@@_style_tl
        \clist_map_break:
       }
       {
        \@@_glyph_if_exist:nT { \@@_to_usv:nn {\l_@@_style_tl} {\l_@@_alphabet_tl} }
         {
          \@@_maybe_init_alphabet:n \l_@@_style_tl
          \clist_map_break:
         }
       }
     }
     { \msg_warning:nnx {unicode-math} {no-alphabet} { \l_@@_style_tl / \l_@@_alphabet_tl } }
   }
%    \end{macrocode}
% \dots and then loop through them defining the individual ranges:
% (currently this process is slow)
%    \begin{macrocode}
%<debug>  \csname TIC\endcsname
  \clist_map_inline:Nn \l_@@_alphabet_clist
   {
    \tl_set:Nx \l_@@_alphabet_tl { \tl_trim_spaces:n {##1} }
    \cs_if_exist:cT {@@_config_ \l_@@_style_tl _ \l_@@_alphabet_tl :n}
     {
      \exp_args:No \tl_if_eq:nnTF \l_@@_alphabet_tl {misc}
       {
        \@@_log:nx {setup-alph} {sym \l_@@_style_tl~(\l_@@_alphabet_tl)}
        \@@_alphabet_config:nnn {\l_@@_style_tl} {\l_@@_alphabet_tl} {\l_@@_remap_style_tl}
       }
       {
        \@@_glyph_if_exist:nTF { \@@_to_usv:nn {\l_@@_remap_style_tl} {\l_@@_alphabet_tl} }
         {
          \@@_log:nx {setup-alph} {sym \l_@@_style_tl~(\l_@@_alphabet_tl)}
          \@@_alphabet_config:nnn {\l_@@_style_tl} {\l_@@_alphabet_tl} {\l_@@_remap_style_tl}
         }
         {
          \bool_if:NTF \l_@@_implicit_alph_bool
           {
            \seq_put_right:Nx \l_@@_missing_alph_seq
             {
              \@backslashchar sym \l_@@_style_tl \space
              (\tl_use:c{c_@@_math_alphabet_name_ \l_@@_alphabet_tl _tl})
             }
           }
           {
            \@@_alphabet_config:nnn {\l_@@_style_tl} {\l_@@_alphabet_tl} {up}
           }
         }
       }
     }
   }
%<debug>  \csname TOC\endcsname
 }
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Mapping `naked' math characters}
%
% Before we show the definitions of the alphabet mappings using the functions
% |\@@_alphabet_config:nnn \l_@@_style_tl {##1} {...}|, we first want to define some functions
% to be used inside them to actually perform the character mapping.
%
% \subsubsection{Functions}
%
% \begin{macro}{\@@_map_char_single:nn}
% Wrapper for |\@@_map_char_noparse:nn| or |\@@_map_char_parse:nn|
% depending on the context.
%
% \begin{macro}{\@@_map_char_noparse:nn}
% \begin{macro}{\@@_map_char_parse:nn}
%    \begin{macrocode}
\cs_new:Nn \@@_map_char_noparse:nn
 { \@@_set_mathcode:nnnn {#1}{\mathalpha}{\@@_symfont_tl}{#2} }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_map_char_parse:nn
 {
  \@@_if_char_spec:nNNT {#1} {\@nil} {\mathalpha}
   { \@@_map_char_noparse:nn {#1}{#2} }
 }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_map_char_single:nnn}
% \darg{char name (`dotlessi')}
% \darg{from alphabet(s)}
% \darg{to alphabet}
% Logical interface to \cs{@@_map_char_single:nn}.
%    \begin{macrocode}
\cs_new:Nn \@@_map_char_single:nnn
 {
  \@@_map_char_single:nn { \@@_to_usv:nn {#1}{#3} }
                         { \@@_to_usv:nn {#2}{#3} }
 }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_map_chars_range:nnnn}
% \darg{Number of chars (26)}
% \darg{From style, one or more (it)}
% \darg{To style (up)}
% \darg{Alphabet name (Latin)}
% First the function with numbers:
%    \begin{macrocode}
\cs_set:Nn \@@_map_chars_range:nnn
 {
  \int_step_inline:nnnn {0}{1}{#1-1}
   { \@@_map_char_single:nn {#2+##1}{#3+##1} }
 }
%    \end{macrocode}
% And the wrapper with names:
%    \begin{macrocode}
\cs_new:Nn \@@_map_chars_range:nnnn
 {
  \@@_map_chars_range:nnn {#1} { \@@_to_usv:nn {#2}{#4} }
                               { \@@_to_usv:nn {#3}{#4} }
 }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Functions for `normal' alphabet symbols}
%
% \begin{macro}{\@@_set_normal_char:nnn}
%    \begin{macrocode}
\cs_set:Nn \@@_set_normal_char:nnn
 {
  \@@_usv_if_exist:nnT {#3} {#1}
  {
    \clist_map_inline:nn {#2}
     {
      \@@_set_mathalphabet_pos:nnnn {normal} {#1} {##1} {#3}
      \@@_map_char_single:nnn {##1} {#3} {#1}
     }
  }
 }
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_normal_Latin:nn
 {
  \clist_map_inline:nn {#1}
   {
    \@@_set_mathalphabet_Latin:nnn {normal} {##1} {#2}
    \@@_map_chars_range:nnnn {26} {##1} {#2} {Latin}
   }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_normal_latin:nn
 {
  \clist_map_inline:nn {#1}
   {
    \@@_set_mathalphabet_latin:nnn {normal} {##1} {#2}
    \@@_map_chars_range:nnnn {26} {##1} {#2} {latin}
   }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_normal_greek:nn
 {
  \clist_map_inline:nn {#1}
   {
    \@@_set_mathalphabet_greek:nnn {normal} {##1} {#2}
    \@@_map_chars_range:nnnn {25} {##1} {#2} {greek}
    \@@_map_char_single:nnn {##1} {#2} {epsilon}
    \@@_map_char_single:nnn {##1} {#2} {vartheta}
    \@@_map_char_single:nnn {##1} {#2} {varkappa}
    \@@_map_char_single:nnn {##1} {#2} {phi}
    \@@_map_char_single:nnn {##1} {#2} {varrho}
    \@@_map_char_single:nnn {##1} {#2} {varpi}
    \@@_set_mathalphabet_pos:nnnn {normal} {epsilon} {##1} {#2}
    \@@_set_mathalphabet_pos:nnnn {normal} {vartheta} {##1} {#2}
    \@@_set_mathalphabet_pos:nnnn {normal} {varkappa} {##1} {#2}
    \@@_set_mathalphabet_pos:nnnn {normal} {phi} {##1} {#2}
    \@@_set_mathalphabet_pos:nnnn {normal} {varrho} {##1} {#2}
    \@@_set_mathalphabet_pos:nnnn {normal} {varpi} {##1} {#2}
   }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_normal_Greek:nn
 {
  \clist_map_inline:nn {#1}
   {
    \@@_set_mathalphabet_Greek:nnn {normal} {##1} {#2}
    \@@_map_chars_range:nnnn {25} {##1} {#2} {Greek}
    \@@_map_char_single:nnn {##1} {#2} {varTheta}
    \@@_set_mathalphabet_pos:nnnn {normal} {varTheta} {##1} {#2}
   }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_normal_numbers:nn
 {
  \@@_set_mathalphabet_numbers:nnn {normal} {#1} {#2}
  \@@_map_chars_range:nnnn {10} {#1} {#2} {num}
 }
%    \end{macrocode}
%
%
% \subsection{Mapping chars inside a math style}
%
% \subsubsection{Functions for setting up the maths alphabets}
%
% \begin{macro}{\@@_set_mathalphabet_char:Nnn}
% This is a wrapper for either |\@@_mathmap_noparse:nnn| or
% |\@@_mathmap_parse:Nnn|, depending on the context.
% \end{macro}
%
% \begin{macro}{\@@_mathmap_noparse:nnn}
% \darg{Maths alphabet, \eg, `bb'}
% \darg{Input slot(s), \eg, the slot for `A' (comma separated)}
% \darg{Output slot, \eg, the slot for `$\mathbb{A}$'}
% Adds \cs{@@_set_mathcode:nnnn} declarations to the specified maths alphabet's definition.
%    \begin{macrocode}
\cs_new:Nn \@@_mathmap_noparse:nnn
 {
  \clist_map_inline:nn {#2}
   {
    \tl_put_right:cx {@@_switchto_#1:}
     {
      \@@_set_mathcode:nnnn {##1} {\mathalpha} {\@@_symfont_tl} {#3}
     }
   }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_mathmap_parse:nnn}
% \darg{Maths alphabet, \eg, `bb'}
% \darg{Input slot(s), \eg, the slot for `A' (comma separated)}
% \darg{Output slot, \eg, the slot for `$\mathbb{A}$'}
% When \cmd\@@_if_char_spec:nNNT\ is executed, it populates the \cmd\l_@@_char_nrange_clist\
% macro with slot numbers corresponding to the specified range. This range is used to
% conditionally add \cs{@@_set_mathcode:nnnn} declaractions to the maths alphabet definition.
%    \begin{macrocode}
\cs_new:Nn \@@_mathmap_parse:nnn
 {
  \clist_if_in:NnT \l_@@_char_nrange_clist {#3}
   {
    \@@_mathmap_noparse:nnn {#1}{#2}{#3}
   }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_mathalphabet_char:nnnn}
% \darg{math style command}
% \darg{input math alphabet name}
% \darg{output math alphabet name}
% \darg{char name to map}
%    \begin{macrocode}
\cs_new:Nn \@@_set_mathalphabet_char:nnnn
 {
  \@@_set_mathalphabet_char:nnn {#1} { \@@_to_usv:nn {#2} {#4} }
                                     { \@@_to_usv:nn {#3} {#4} }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_mathalph_range:nnnn}
% \darg{Number of iterations}
% \darg{Maths alphabet}
% \darg{Starting input char (single)}
% \darg{Starting output char}
% Loops through character ranges setting \cmd\mathcode.
% First the version that uses numbers:
%    \begin{macrocode}
\cs_new:Nn \@@_set_mathalph_range:nnnn
 {
  \int_step_inline:nnnn {0} {1} {#1-1}
    { \@@_set_mathalphabet_char:nnn {#2} { ##1 + #3 } { ##1 + #4 } }
 }
%    \end{macrocode}
% Then the wrapper version that uses names:
%    \begin{macrocode}
\cs_new:Nn \@@_set_mathalph_range:nnnnn
 {
  \@@_set_mathalph_range:nnnn {#1} {#2} { \@@_to_usv:nn {#3} {#5} }
                                        { \@@_to_usv:nn {#4} {#5} }
 }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Individual mapping functions for different alphabets}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_mathalphabet_pos:nnnn
 {
  \@@_usv_if_exist:nnT {#4} {#2}
   {
    \clist_map_inline:nn {#3}
      { \@@_set_mathalphabet_char:nnnn {#1} {##1} {#4} {#2} }
   }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_mathalphabet_numbers:nnn
 {
  \clist_map_inline:nn {#2}
    { \@@_set_mathalph_range:nnnnn {10} {#1} {##1} {#3} {num} }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_mathalphabet_Latin:nnn
 {
  \clist_map_inline:nn {#2}
    { \@@_set_mathalph_range:nnnnn {26} {#1} {##1} {#3} {Latin} }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_mathalphabet_latin:nnn
 {
  \clist_map_inline:nn {#2}
   {
    \@@_set_mathalph_range:nnnnn {26} {#1} {##1} {#3} {latin}
    \@@_set_mathalphabet_char:nnnn    {#1} {##1} {#3} {h}
   }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_mathalphabet_Greek:nnn
 {
  \clist_map_inline:nn {#2}
   {
    \@@_set_mathalph_range:nnnnn {25} {#1} {##1} {#3} {Greek}
    \@@_set_mathalphabet_char:nnnn    {#1} {##1} {#3} {varTheta}
   }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_set_mathalphabet_greek:nnn
 {
  \clist_map_inline:nn {#2}
   {
    \@@_set_mathalph_range:nnnnn {25} {#1} {##1} {#3} {greek}
    \@@_set_mathalphabet_char:nnnn    {#1} {##1} {#3} {epsilon}
    \@@_set_mathalphabet_char:nnnn    {#1} {##1} {#3} {vartheta}
    \@@_set_mathalphabet_char:nnnn    {#1} {##1} {#3} {varkappa}
    \@@_set_mathalphabet_char:nnnn    {#1} {##1} {#3} {phi}
    \@@_set_mathalphabet_char:nnnn    {#1} {##1} {#3} {varrho}
    \@@_set_mathalphabet_char:nnnn    {#1} {##1} {#3} {varpi}
   }
 }
%    \end{macrocode}
%
%
%
% \section{A token list to contain the data of the math table}
%
% Instead of \cmd\input-ing the unicode math table every time we
% want to re-read its data, we save it within a macro. This has two
% advantages: 1.~it should be slightly faster, at the expense of memory;
% 2.~we don't need to worry about catcodes later, since they're frozen
% at this point.
%
% In time, the case statement inside |set_mathsymbol| will be moved in here
% to avoid re-running it every time.
%    \begin{macrocode}
\cs_new:Npn \@@_symbol_setup:
 {
  \cs_set:Npn \UnicodeMathSymbol ##1##2##3##4
   {
    \exp_not:n { \_@@_sym:nnn {##1} {##2} {##3} }
   }
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\tl_set_from_file_x:Nnn \g_@@_mathtable_tl {\@@_symbol_setup:} {unicode-math-table.tex}
%    \end{macrocode}
%
%
% \begin{macro}{\@@_input_math_symbol_table:}
% This function simply expands to the token list containing all the data.
%    \begin{macrocode}
\cs_new:Nn \@@_input_math_symbol_table: {\g_@@_mathtable_tl}
%    \end{macrocode}
% \end{macro}
%
%
% \section{Definitions of the active math characters}
%
% Now give \cmd\_@@_sym:nnn\ a definition in terms of \cmd\@@_cs_set_eq_active_char:Nw\
% and we're good to go.
%
% Ensure catcodes are appropriate;
% make sure |#| is an `other' so that we don't get confused with \cs{mathoctothorpe}.
%    \begin{macrocode}
\AtBeginDocument{\@@_define_math_chars:}
\cs_new:Nn \@@_define_math_chars:
 {
  \group_begin:
    \cs_set:Npn \_@@_sym:nnn ##1##2##3
     {
      \tl_if_in:nnT
       { \mathord \mathalpha \mathbin \mathrel \mathpunct \mathop \mathfence }
       {##3}
      {
        \exp_last_unbraced:NNx \cs_gset_eq:NN ##2 { \Ucharcat ##1 ~ 12 ~ }
      }
     }
    \@@_input_math_symbol_table:
  \group_end:
 }
%    \end{macrocode}
%
%
%    \begin{macrocode}
%</package&(XE|LU)>
%    \end{macrocode}
%
\endinput