summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/siunitx/siunitx-unit.dtx
blob: be3e6eaf6134ad80f880f5c120a73e0e157f7ced (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
% \iffalse meta-comment
%
% File: siunitx-unit.dtx Copyright (C) 2014-2021 Joseph Wright
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "siunitx bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% The released version of this bundle is available from CTAN.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/josephwright/siunitx
%
% for those people who are interested.
%
% -----------------------------------------------------------------------
%
%<*driver>
\documentclass{l3doc}
\ExplSyntaxOn
\makeatletter
\NewDocumentCommand \acro { m }
  {
    \textsc
      {
        \exp_args:NV \tl_if_head_eq_charcode:nNTF \f@series { m }
          { \tl_lower_case:n }
          { \use:n }
            {#1}
      }
  }
\makeatother
\ExplSyntaxOff
\ProvideDocumentCommand\foreign{m}{\textit{#1}}
% The next line is needed so that \GetFileInfo will be able to pick up
% version data
\usepackage{siunitx}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{siunitx.sty}
%
% \title{^^A
%   \pkg{siunitx-unit} -- Parsing and formatting units^^A
%   \thanks{This file describes \fileversion,
%     last revised \filedate.}^^A
% }
%
% \author{^^A
%  Joseph Wright^^A
%  \thanks{^^A
%    E-mail:
%    \href{mailto:joseph.wright@morningstar2.co.uk}
%      {joseph.wright@morningstar2.co.uk}^^A
%   }^^A
% }
%
% \date{Released \filedate}
%
% \maketitle
%
% \begin{documentation}
%
% This submodule is dedicated to formatting physical units. The main function,
% \cs{siunitx_unit_format:nN}, takes user input specify physical units and
% converts it into a formatted token list suitable for typesetting in math
% mode. While the formatter will deal correctly with \enquote{literal} user
% input, the key strength of the module is providing a method to describe
% physical units in a \enquote{symbolic} manner. The output format of these
% symbolic units can then be controlled by a number of key--value options
% made available by the module.
%
% A small number of \LaTeXe{} math mode commands are assumed to be available
% as part of the formatted output. The \cs{mathchoice} command
% (normally the \TeX{} primitive) is needed when using
% |per-mode = symbol-or-fraction|. The commands \cs{frac}, \cs{mathrm},
% \cs{mbox}, \verb*|\ | and \cs{,} are used by the standard module settings.
% For the display of colored (highlighted) and cancelled units, the commands
% \cs{textcolor} and \cs{cancel} are assumed to be available.
%
% \section{Formatting units}
%
% \begin{function}{\siunitx_unit_format:nN, \siunitx_unit_format:xN}
%   \begin{syntax}
%     \cs{siunitx_unit_format:nN} \Arg{units} \meta{tl~var}
%   \end{syntax}
%   This function converts the input \meta{units} into a processed
%   \meta{tl~var} which can then be inserted in math mode to typeset the
%   material. Where the \meta{units} are given in symbolic form, described
%   elsewhere, this formatting process takes place in two stages: the
%   \meta{units} are parsed into a structured form before the generation
%   of the appropriate output form based on the active settings. When the
%   \meta{units} are given as literals, processing is minimal: the
%   characters |.| and |~| are converted to unit products (boundaries).
%   In both cases, the result is a series of tokens intended to be typeset
%   in math mode with appropriate choice of font for typesetting of the
%   textual parts.
%
%   For example,
%   \begin{verbatim}
%     \siunitx_unit_format:nN { \kilo \metre \per \second } \l_tmpa_tl
%   \end{verbatim}
%   will, with standard settings, result in \cs{l_tmpa_tl} being set to
%   \begin{verbatim}
%     \mathrm{km}\,\mathrm{s}^{-1}
%   \end{verbatim}
% \end{function}
%
% \begin{function}{\siunitx_unit_format_extract_prefixes:nNN}
%   \begin{syntax}
%     \cs{siunitx_unit_format_extract_prefixes:nNN} \Arg{units} \meta{tl~var} \meta{fp~var}
%   \end{syntax}
%   This function formats the \meta{units} in the same way as described for
%   \cs{siunitx_unit_format:nN}. When the input is given in symbolic form,
%   any decimal unit prefixes will be extracted and the overall power of
%   ten that these represent will be stored in the \meta{fp~var}.
%
%   For example,
%   \begin{verbatim}
%     \siunitx_unit_format_extract_prefixes:nNN { \kilo \metre \per \second }
%       \l_tmpa_tl \l_tmpa_fp
%   \end{verbatim}
%   will, with standard settings, result in \cs{l_tmpa_tl} being set to
%   \begin{verbatim}
%     \mathrm{m}\,\mathrm{s}^{-1}
%   \end{verbatim}
%   with \cs{l_tmpa_fp} taking value~$3$. Note that the latter is a floating
%   point variable: it is possible for non-integer values to be obtained here.
% \end{function}
%
% \begin{function}{\siunitx_unit_format_combine_exponent:nnN}
%   \begin{syntax}
%     \cs{siunitx_unit_format_combine_exponent:nnN} \Arg{units} \Arg{exponent} \meta{tl~var}
%   \end{syntax}
%   This function formats the \meta{units} in the same way as described for
%   \cs{siunitx_unit_format:nN}. The \meta{exponent} is combined with any
%   prefix for the \emph{first} unit of the \meta{units}, and an updated
%   prefix is introduced.
%
%   For example,
%   \begin{verbatim}
%     \siunitx_unit_format_combine_exponent:nnN { \metre \per \second }
%       { 3 } \l_tmpa_tl
%   \end{verbatim}
%   will, with standard settings, result in \cs{l_tmpa_tl} being set to
%   \begin{verbatim}
%     \mathrm{km}\,\mathrm{s}^{-1}
%   \end{verbatim}
% \end{function}
%
% \begin{function}
%   {
%     \siunitx_unit_format_multiply:nnN ,
%     \siunitx_unit_format_multiply_extract_prefixes:nnNN ,
%     \siunitx_unit_format_multiply_combine_exponent:nnnN
%   }
%   \begin{syntax}
%     \cs{siunitx_unit_format_multiply:nnN} \Arg{units} \Arg{factor} \meta{tl~var}
%     \cs{siunitx_unit_format_multiply_extract_prefixes:nnNN}
%       \Arg{units} \Arg{factor} \meta{tl~var} \meta{fp~var}
%     \cs{siunitx_unit_format_multiply_combine_exponent:nnnN}
%       \Arg{units} \Arg{factor} \Arg{exponent} \meta{tl~var}
%   \end{syntax}
%   These function formats the \meta{units} in the same way as described for
%   \cs{siunitx_unit_format:nN}. The units are multiplied by the \meta{factor},
%   and further processing takes place as previously described.
%
%   For example,
%   \begin{verbatim}
%     \siunitx_unit_format_multiply:nnN { \metre \per \second }
%       { 3 } \l_tmpa_tl
%   \end{verbatim}
%   will, with standard settings, result in \cs{l_tmpa_tl} being set to
%   \begin{verbatim}
%     \mathrm{km}^{3}\,\mathrm{s}^{-3}
%   \end{verbatim}
% \end{function}
%
% \section{Defining symbolic units}
%
% \begin{function}{\siunitx_declare_prefix:Nnn, \siunitx_declare_prefix:Nnx}
%   \begin{syntax}
%     \cs{siunitx_declare_prefix:Nnn} \meta{prefix} \Arg{power} \Arg{symbol}
%   \end{syntax}
%   Defines a symbolic \meta{prefix} (which should be a control sequence
%   such as |\kilo|) to be converted by the parser to the \meta{symbol}.
%   The latter should consist of literal content (\foreign{e.g.}~|k|).
%   In literal mode the \meta{symbol} will be typeset directly. The prefix
%   should represent an integer \meta{power} of $10$, and this information
%   may be used to convert from one or more \meta{prefix} symbols to an
%   overall power applying to a unit. See also
%   \cs{siunitx_declare_prefix:Nn}.
% \end{function}
%
% \begin{function}{\siunitx_declare_prefix:Nn}
%   \begin{syntax}
%     \cs{siunitx_declare_prefix:Nn} \meta{prefix} \Arg{symbol}
%   \end{syntax}
%   Defines a symbolic \meta{prefix} (which should be a control sequence
%   such as |\kilo|) to be converted by the parser to the \meta{symbol}.
%   The latter should consist of literal content (\foreign{e.g.}~|k|).
%   In literal mode the \meta{symbol} will be typeset directly. In contrast
%   to \cs{siunitx_declare_prefix:Nnn}, there is no assumption about the
%   mathematical nature of the \meta{prefix}, \foreign{i.e.}~the prefix may
%   represent a power of any base. As a result, no conversion of the
%   \meta{prefix} to a numerical power will be possible.
% \end{function}
%
% \begin{function}{\siunitx_declare_power:NNn}
%   \begin{syntax}
%     \cs{siunitx_declare_power:NNn} \meta{pre-power} \meta{post-power} \Arg{value}
%   \end{syntax}
%   Defines \emph{two} symbolic \meta{powers} (which should be control
%   sequences such as |\squared|) to be converted by the parser to the
%   \meta{value}. The latter should be an integer or floating point number in
%   the format defined for \pkg{l3fp}. Powers may precede a unit or be give
%   after it: both forms are declared at once, as indicated by the argument
%   naming. In literal mode, the \meta{value} will be applied as
%   a superscript to either the next token in the input (for the
%   \meta{pre-power}) or appended to the previously-typeset material
%   (for the \meta{post-power}).
% \end{function}
%
% \begin{function}{\siunitx_declare_qualifier:Nn}
%   \begin{syntax}
%     \cs{siunitx_declare_qualifier:Nn} \meta{qualifier} \Arg{meaning}
%   \end{syntax}
%   Defines a symbolic \meta{qualifier} (which should be a control sequence
%   such as |\catalyst|) to be converted by the parser to the \meta{meaning}.
%   The latter should consist of literal content (\foreign{e.g.}~|cat|). In
%   literal mode the \meta{meaning} will be typeset following a space after
%   the unit to which it applies.
% \end{function}
%
% \begin{function}
%   {
%     \siunitx_declare_unit:Nn,
%     \siunitx_declare_unit:Nx,
%     \siunitx_declare_unit:Nnn,
%     \siunitx_declare_unit:Nxn
%   }
%   \begin{syntax}
%     \cs{siunitx_declare_unit:Nn}  \meta{unit} \Arg{meaning}
%     \cs{siunitx_declare_unit:Nnn} \meta{unit} \Arg{meaning} \Arg{options}
%   \end{syntax}
%   Defines a symbolic \meta{unit} (which should be a control sequence
%   such as |\kilogram|) to be converted by the parser to the \meta{meaning}.
%   The latter may consist of literal content (\foreign{e.g.}~|kg|), other
%   symbolic unit commands (\foreign{e.g.}~|\kilo\gram|) or a mixture of the two.
%   In literal mode the \meta{meaning} will be typeset directly. The version
%   taking an \meta{options} argument may be used to support per-unit
%   options: these are applied at the top level or using
%   \cs{siunitx_unit_options_apply:n}.
% \end{function}
%
% \begin{variable}{\l_siunitx_unit_font_tl}
%    The font function which is applied to the text of units when constructing
%    formatted units: set by |font-command|.
% \end{variable}
%
% \begin{variable}{\l_siunitx_unit_fraction_tl}
%    The fraction function which is applied whne constructing fractional units:
%    set by |fraction-command|.
% \end{variable}
%
% \begin{variable}{\l_siunitx_unit_symbolic_seq}
%   This sequence contains all of the symbolic names defined:
%   these will be in the form of control sequences such as |\kilogram|.
%   The order of the sequence is unimportant. This includes prefixes and
%   powers as well as units themselves.
% \end{variable}
%
% \begin{variable}{\l_siunitx_unit_seq}
%   This sequence contains all of the symbolic \emph{unit} names defined:
%   these will be in the form of control sequences such as |\kilogram|.
%   In contrast to \cs{l_siunitx_unit_symbolic_seq}, it \emph{only} holds
%   units themselves
% \end{variable}
%
% \section{Per-unit options}
%
% \begin{function}{\siunitx_unit_options_apply:n}
%   \begin{syntax}
%     \cs{siunitx_unit_options_apply:n} \meta{unit(s)}
%   \end{syntax}
%   Applies any unit-specific options set up using
%   \cs{siunitx_declare_unit:Nnn}. This allows there use outside of unit
%   formatting, for example to influence spacing in quantities. The options
%   are applied only once at a given group level, which allows for user
%   over-ride \foreign{via} |\keys_set:nn { siunitx } { ... }|.
% \end{function}
%
% \section{Units in (PDF) strings}
%
% \begin{function}{\siunitx_unit_pdfstring_context:}
%   \begin{syntax}
%     \cs{group_begin:}
%       \cs{siunitx_unit_pdfstring_context:}
%       \meta{Expansion context} \meta{units}
%     \cs{group_end:}
%   \end{syntax}
%   Sets symbol unit macros to generate text directly. This is needed in
%   expansion contexts where units must be converted to simple text. This
%   function is itself not expandable, so must be using within a
%   surrounding group as show in the example.
% \end{function}
%
% \section{Pre-defined symbolic unit components}
%
% The unit parser is defined to recognise a number of pre-defined units,
% prefixes and powers, and also interpret a small selection of
% \enquote{generic} symbolic parts.
%
% Broadly, the pre-defined units are those defined by the \textsc{bipm} in the
% documentation for the \emph{International System of Units}
% (\acro{SI})~\cite{BIPM}. As far as possible, the names given to the command
% names for units are those used by the \acro{bipm}, omitting spaces and using
% only \acro{ASCII} characters. The standard symbols are also taken from the
% same documentation. In the following documentation, the order of the
% description of units broadly follows the \acro{SI}~Brochure.
%
% \begin{function}
%   {
%     \kilogram ,
%     \metre    ,
%     \meter    ,
%     \mole     ,
%     \kelvin   ,
%     \candela  ,
%     \second   ,
%     \ampere
%   }
%   The base units as defined in the \acro{SI} Brochure~\cite{base-units}.
%   Notice that \cs{meter} is defined as an alias for \cs{metre} as the former
%   spelling is common in the US (although the latter is the official spelling).
% \end{function}
%
% \begin{function}{\gram}
%   The base unit \cs{kilogram} is defined using an \acro{SI} prefix: as such
%   the (derived) unit \cs{gram} is required by the module to correctly produce
%   output for the \cs{kilogram}.
% \end{function}
%
% \begin{function}
%   {
%     \yocto ,
%     \zepto ,
%     \atto  ,
%     \femto ,
%     \pico  ,
%     \nano  ,
%     \micro ,
%     \milli ,
%     \centi ,
%     \deci  ,
%     \deca  ,
%     \deka  ,
%     \hecto ,
%     \kilo  ,
%     \mega  ,
%     \giga  ,
%     \tera  ,
%     \peta  ,
%     \exa   ,
%     \zetta ,
%     \yotta
%   }
%   Prefixes, all of which are integer powers of $10$: the powers are stored
%   internally by the module and can be used for conversion from prefixes to
%   their numerical equivalent. These prefixes are documented in Section~3.1
%   of the \acro{SI}~Brochure.
%
%   Note that the \cs{kilo} prefix is required to
%   define the base \cs{kilogram} unit. Also note the two spellings available
%   for \cs{deca}/\cs{deka}.
% \end{function}
%
% \begin{function}
%   {
%     \becquerel     ,
%     \degreeCelsius ,
%     \coulomb       ,
%     \farad         ,
%     \gray          ,
%     \hertz         ,
%     \henry         ,
%     \joule         ,
%     \katal         ,
%     \lumen         ,
%     \lux           ,
%     \newton        ,
%     \ohm           ,
%     \pascal        ,
%     \radian        ,
%     \siemens       ,
%     \sievert       ,
%     \steradian     ,
%     \tesla         ,
%     \volt          ,
%     \watt          ,
%     \weber
%   }
%   The defined \acro{SI}~units with defined names and symbols, as given in
%   Table~4 of the \acro{SI}~Brochure. Notice that the names
%   of the units are lower case with the exception of \cs{degreeCelsius}, and
%   that this unit name includes \enquote{degree}.
% \end{function}
%
% \begin{function}
%   {
%     \astronomicalunit ,
%     \bel              ,
%     \dalton           ,
%     \day              ,
%     \decibel          ,
%     \electronvolt     ,
%     \hectare          ,
%     \hour             ,
%     \litre            ,
%     \liter            ,
%     \neper            ,
%     \minute           ,
%     \tonne
%   }
%   Units accepted for use with the \acro{SI}: here \cs{minute} is a unit of
%   time not of plane angle. These units are taken from Table~8 of the
%   \acro{SI}~Brochure.
%
%   For the unit \cs{litre}, both |l| and |L| are listed
%   as acceptable symbols: the latter is the standard setting of the module.
%   The alternative spelling \cs{liter} is also given for this unit for US
%   users (as with \cs{metre}, the official spelling is \enquote{re}).
% \end{function}
%
% \begin{function}
%   {
%     \arcminute ,
%     \arcsecond ,
%     \degree
%   }
%   Units for plane angles accepted for use with the \acro{SI}: to avoid a
%   clash with units for time, here \cs{arcminute} and \cs{arcsecond} are
%   used in place of \cs{minute} and \cs{second}. These units are taken from
%   Table~8 of the \acro{SI}~Brochure.
% \end{function}
%
% \begin{function}{\percent}
%   The mathematical concept of percent, usable with the \acro{SI} as detailed
%   in Section~5.4.7 of the \acro{SI}~Brochure.
% \end{function}
%
% \begin{function}{\square, \cubic}
%   \begin{syntax}
%     \cs{square} \meta{prefix} \meta{unit}
%     \cs{cubic} \meta{prefix} \meta{unit}
%   \end{syntax}
%   Pre-defined unit powers which apply to the next \meta{prefix}/\meta{unit}
%   combination.
% \end{function}
%
% \begin{function}{\squared, \cubed}
%   \begin{syntax}
%     \meta{prefix} \meta{unit} \cs{squared}
%     \meta{prefix} \meta{unit} \cs{cubed}
%   \end{syntax}
%   Pre-defined unit powers which apply to the preceding
%   \meta{prefix}/\meta{unit} combination.
% \end{function}
%
% \begin{function}{\per}
%   \begin{syntax}
%     \cs{per} \meta{prefix} \meta{unit} \meta{power}
%   \end{syntax}
%   Indicates that the next \meta{prefix}/\meta{unit}/\meta{power} combination
%   is reciprocal, \foreign{i.e.}~raises it to the power $-1$. This symbolic
%   representation may be applied in addition to a \cs{power}, and will work
%   correctly if the \cs{power} itself is negative. In literal mode \cs{per}
%   will print a slash (\enquote{$/$}).
% \end{function}
%
% \begin{function}{\cancel}
%   \begin{syntax}
%     \cs{cancel} \meta{prefix} \meta{unit} \meta{power}
%   \end{syntax}
%   Indicates that the next \meta{prefix}/\meta{unit}/\meta{power} combination
%   should be \enquote{cancelled out}. In the parsed output, the entire unit
%   combination will be given as the argument to a function \cs{cancel}, which
%   is assumed to be available at a higher level. In literal mode, the same
%   higher-level \cs{cancel} will be applied to the next token. It is the
%   responsibility of the calling code to provide an appropriate definition
%   for \cs{cancel} outside of the scope of the unit parser.
% \end{function}
%
% \begin{function}{\highlight}
%   \begin{syntax}
%     \cs{highlight} \Arg{color} \meta{prefix} \meta{unit} \meta{power}
%   \end{syntax}
%   Indicates that the next \meta{prefix}/\meta{unit}/\meta{power} combination
%   should be highlighted in the specified \meta{color}. In the parsed output,
%   the entire unit combination will be given as the argument to a function
%   \cs{textcolor}, which is assumed to be available at a higher level. In
%   literal mode, the same higher-level \cs{textcolor} will be applied to the
%   next token. It is the responsibility of the calling code to provide an
%   appropriate definition for \cs{textcolor} outside of the scope of the unit
%   parser.
% \end{function}
%
% \begin{function}{\of}
%   \begin{syntax}
%     \meta{prefix} \meta{unit} \meta{power} \cs{of} \Arg{qualifier}
%   \end{syntax}
%   Indicates that the \meta{qualifier} applies to the current
%   \meta{prefix}/\meta{unit}/\meta{power} combination. In parsed mode, the
%   display of the result will depend upon module options. In literal mode,
%   the \meta{qualifier} will be printed in parentheses following the preceding
%   \meta{unit} and a full-width space.
% \end{function}
%
% \begin{function}{\raiseto, \tothe}
%   \begin{syntax}
%     \cs{raiseto} \Arg{power} \meta{prefix} \meta{unit}
%     \meta{prefix} \meta{unit} \cs{tothe} \Arg{power}
%   \end{syntax}
%   Indicates that the \meta{power} applies to the current
%   \meta{prefix}/\meta{unit} combination. As shown, \cs{raiseto} applies to
%   the next \meta{unit} whereas \cs{tothe} applies to the preceding unit. In
%   literal mode the \cs{power} will be printed as a superscript attached to
%   the next token (\cs{raiseto}) or preceding token (\cs{tothe}) as
%   appropriate.
% \end{function}
%
% \subsection{Key--value options}
%
% The options defined by this submodule are available within the \pkg{l3keys}
% |siunitx| tree.
%
% \begin{function}{bracket-unit-denominator}
%   \begin{syntax}
%     |bracket-unit-denominator| = |true|\verb"|"|false|
%   \end{syntax}
%   Switch to determine whether brackets are added to the denominator part of
%   a unit when printed using inline fractional form (with |per-mode| as
%   |repeated-symbol|, |symbol| or |symbol-or-fraction|). The standard setting
%   is |true|.
% \end{function}
%
% \begin{function}{extract-mass-in-kilograms}
%   \begin{syntax}
%     |extract-mass-in-kilograms| = |true|\verb"|"|false|
%   \end{syntax}
%   Determines whether prefix extraction treats kilograms as a base unit; when
%   set |false|, grams are used. The standard setting is |true|.
% \end{function}
%
% \begin{function}{forbid-literal-units}
%   \begin{syntax}
%     |forbid-literal-units| = |true|\verb"|"|false|
%   \end{syntax}
%   Switch which determines if literal units are allowed when parsing is
%   active; does not apply when |parse-units| is |false|.
% \end{function}
%
% \begin{function}{fraction-command}
%   \begin{syntax}
%     |fraction-command| = \meta{command}
%   \end{syntax}
%   Command used to create fractional output when |per-mode| is set to
%   |fraction|. The standard setting is |\frac|.
% \end{function}
%
% \begin{function}{inter-unit-product}
%   \begin{syntax}
%     |inter-unit-product| = \meta{separator}
%   \end{syntax}
%   Inserted between unit combinations in parsed mode, and used to replace
%   |.| and |~| in literal mode. The standard setting is |\,|.
% \end{function}
%
% \begin{function}{parse-units}
%   \begin{syntax}
%     |parse-units| = |true|\verb"|"|false|
%   \end{syntax}
%   Determines whether parsing of unit symbols is attempted or literal
%   mode is used directly. The standard setting is |true|.
% \end{function}
%
% \begin{function}{per-mode}
%   \begin{syntax}
%     |per-mode| = |fraction|\verb"|"|power|\verb"|"|power-positive-first|\verb"|"|repeated-symbol|\verb"|"|symbol|\verb"|"|symbol-or-fraction|
%   \end{syntax}
%   Selects how the negative powers (\cs{per}) are formatted: a choice from
%   the options |fraction|, |power|, |power-positive-first|, |repeated-symbol|,
%   |symbol| and |symbol-or-fraction|. The option |fraction| generates
%   fractional output when appropriate using the command specified by
%   the |fraction-command| option. The setting |power| uses reciprocal powers
%   leaving the units in the order of input, while |power-positive-first| uses
%   the same display format but sorts units such that the positive powers
%   come before negative ones. The |symbol| setting uses a symbol (specified
%   by |per-symbol|) between positive and negative powers, while
%   |repeated-symbol| uses the same symbol but places it before \emph{every}
%   unit with a negative power (this is mathematically \enquote{wrong} but
%   often seen in real work). Finally, |symbol-or-fraction| acts like
%   |symbol| for inline output and like |fraction| when the output is used
%   in a display math environment. The standard setting is |power|.
% \end{function}
%
% \begin{function}{per-symbol}
%   \begin{syntax}
%     |per-symbol| = \meta{symbol}
%   \end{syntax}
%   Specifies the symbol to be used to denote negative powers when the option
%   |per-mode| is set to |repeated-symbol|, |symbol| or |symbol-or-fraction|.
%   The standard setting is |/|.
% \end{function}
%
% \begin{function}{qualifier-mode}
%   \begin{syntax}
%     |qualifier-mode| = |bracket|\verb"|"|combine|\verb"|"|phrase|\verb"|"|subscript|
%   \end{syntax}
%   Selects how qualifiers are formatted: a choice from the options |bracket|,
%   |combine|, |phrase| and |subscript|. The option |bracket| wraps the qualifier
%   in parenthesis, |combine| joins the qualifier with the unit directly, |phrase|
%   joins the material using |qualifier-phrase| as a link, and
%   |subscript| formats the qualifier as a subscript. The standard setting is
%   |subscript|.
% \end{function}
%
% \begin{function}{qualifier-phrase}
%   \begin{syntax}
%     |qualifier-phrase| = \meta{phrase}
%   \end{syntax}
%   Defines the \meta{phrase} used when |qualifier-mode| is set to |phrase|.
% \end{function}
%
% \begin{function}{sticky-per}
%   \begin{syntax}
%     |sticky-per| = |true|\verb"|"|false|
%   \end{syntax}
%   Used to determine whether \cs{per} should be applied one a unit-by-unit
%   basis (when |false|) or should apply to all following units
%   (when |true|). The latter mode is somewhat akin conceptually to the
%   \TeX{} \cs{over} primitive. The standard setting is |false|.
% \end{function}
%
% \begin{function}{unit-font-command}
%   \begin{syntax}
%     |unit-font-command| = \meta{command}
%   \end{syntax}
%   Command applied to text during output of units: should be command usable
%   in math mode for font selection. Notice that in a typical unit this does
%   not (necessarily) apply to all output, for example powers or brackets.
%   The standard setting is |\mathrm|.
% \end{function}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{siunitx-unit} implementation}
%
% Start the \pkg{DocStrip} guards.
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% Identify the internal prefix (\LaTeX3 \pkg{DocStrip} convention): only
% internal material in this \emph{submodule} should be used directly.
%    \begin{macrocode}
%<@@=siunitx_unit>
%    \end{macrocode}
%
% \subsection{Initial set up}
%
% The mechanisms defined here need a few variables to exist and to be
% correctly set: these don't belong to one subsection and so are created
% in a small general block.
%
%   Variants not provided by \pkg{expl3}.
%    \begin{macrocode}
\cs_generate_variant:Nn \tl_replace_all:Nnn { NnV }
%    \end{macrocode}
%
% \begin{variable}{\l_@@_tmp_fp}
% \begin{variable}{\l_@@_tmp_int}
% \begin{variable}{\l_@@_tmp_tl}
%   Scratch space.
%    \begin{macrocode}
\fp_new:N \l_@@_tmp_fp
\int_new:N \l_@@_tmp_int
\tl_new:N  \l_@@_tmp_tl
%    \end{macrocode}
% \end{variable}
% \end{variable}
% \end{variable}
%
% \begin{variable}{\c_@@_math_subscript_tl}
%   Useful tokens with awkward category codes.
%    \begin{macrocode}
\tl_const:Nx \c_@@_math_subscript_tl
  { \char_generate:nn { `\_ } { 8 } }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_parsing_bool}
%   A boolean is used to indicate when the symbolic unit functions should
%   produce symbolic or literal output. This is used when the symbolic names
%   are used along with literal input, and ensures that there is a sensible
%   fall-back for these cases.
%    \begin{macrocode}
\bool_new:N \l_@@_parsing_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_test_bool}
%   A switch used to indicate that the code is testing the input to find
%   if there is any typeset output from individual unit macros. This is needed
%   to allow the \enquote{base} macros to be found, and also to pick up the
%   difference between symbolic and literal unit input.
%    \begin{macrocode}
\bool_new:N \l_@@_test_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_if_symbolic:nTF}
%   The test for symbolic units is needed in two places. First, there is the
%   case of \enquote{pre-parsing} input to check if it can be parsed. Second,
%   when parsing there is a need to check if the current unit is built up
%   from others (symbolic) or is defined in terms of some literals. To do this,
%   the approach used is to set all of the symbolic unit commands expandable
%   and to do nothing, with the few special cases handled manually.
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_if_symbolic:n #1 { TF }
  {
    \group_begin:
      \bool_set_true:N \l_@@_test_bool
      \protected@edef \l_@@_tmp_tl {#1}
    \exp_args:NNV \group_end:
    \tl_if_blank:nTF \l_@@_tmp_tl
      { \prg_return_true: }
      { \prg_return_false: }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Defining symbolic unit}
%
% Unit macros and related support are created here. These exist only within
% the scope of the unit processor code, thus not polluting document-level
% namespace and allowing overlap with other areas in the case of useful short
% names (for example \cs{pm}). Setting up the mechanisms to allow this requires
% a few additional steps on top of simply saving the data given by the user
% in creating the unit.
%
% \begin{variable}{\l_siunitx_unit_symbolic_seq}
%   A list of all of the symbolic units, \foreign{etc.}, set up. This is needed
%   to allow the symbolic names to be defined within the scope of the unit
%   parser but not elsewhere using simple mappings.
%    \begin{macrocode}
\seq_new:N \l_siunitx_unit_symbolic_seq
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_siunitx_unit_seq}
%   A second list featuring only the units themselves.
%    \begin{macrocode}
\seq_new:N \l_siunitx_unit_seq
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_set_symbolic:Nnn}
% \begin{macro}{\@@_set_symbolic:Npnn}
% \begin{macro}{\@@_set_symbolic:Nnnn}
%   The majority of the work for saving each symbolic definition is the same
%   irrespective of the item being defined (unit, prefix, power, qualifier).
%   This is therefore all carried out in a single internal function which
%   does the common tasks. The three arguments here are the symbolic macro
%   name, the literal output and the code to insert when doing full unit
%   parsing. To allow for the \enquote{special cases} (where arguments are
%   required) the entire mechanism is set up in a two-part fashion allowing
%   for flexibility at the slight cost of additional functions.
%
%   Importantly, notice that the unit macros are declared as expandable. This
%   is required so that literals can be correctly converted into a token list
%   of material which does not depend on local redefinitions for the unit
%   macros. That is required so that the unit formatting system can be grouped.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_set_symbolic:Nnn #1
  { \@@_set_symbolic:Nnnn #1 { } }
\cs_new_protected:Npn \@@_set_symbolic:Npnn #1#2#
  { \@@_set_symbolic:Nnnn #1 {#2} }
\cs_new_protected:Npn \@@_set_symbolic:Nnnn #1#2#3#4
  {
    \seq_put_right:Nn \l_siunitx_unit_symbolic_seq {#1}
    \cs_set:cpn { @@_ \token_to_str:N #1 :w } #2
      {
        \bool_if:NF \l_@@_test_bool
          {
            \bool_if:NTF \l_@@_parsing_bool
              {#4}
              {#3}
          }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\siunitx_declare_power:NNn}
%   Powers can come either before or after the unit. As they always come
%   (logically) in matching, we handle this by declaring two commands,
%   and setting each up separately.
%    \begin{macrocode}
\cs_new_protected:Npn \siunitx_declare_power:NNn #1#2#3
  {
    \@@_set_symbolic:Nnn #1
      { \@@_literal_power:nn {#3} }
      { \@@_parse_power:nnN {#1} {#3} \c_true_bool }
    \@@_set_symbolic:Nnn #2
      { ^ {#3} }
      { \@@_parse_power:nnN {#2} {#3} \c_false_bool }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\siunitx_declare_prefix:Nn}
% \begin{macro}{\siunitx_declare_prefix:Nnn, \siunitx_declare_prefix:Nnx}
% \begin{variable}
%   {\l_@@_prefixes_forward_prop, \l_@@_prefixes_reverse_prop}
%   For prefixes there are a couple of options. In all cases, the basic
%   requirement is to set up to parse the prefix using the appropriate
%   internal function. For prefixes which are powers of $10$, there is also
%   the need to be able to do conversion to/from the numerical equivalent.
%   That is handled using two properly lists which can be used to supply
%   the conversion data later.
%    \begin{macrocode}
\cs_new_protected:Npn \siunitx_declare_prefix:Nn #1#2
  {
    \@@_set_symbolic:Nnn #1
      {#2}
      { \@@_parse_prefix:Nn #1 {#2} }
  }
\cs_new_protected:Npn \siunitx_declare_prefix:Nnn #1#2#3
  {
    \siunitx_declare_prefix:Nn #1 {#3}
    \prop_put:Nnn \l_@@_prefixes_forward_prop {#3} {#2}
    \prop_put:Nnn \l_@@_prefixes_reverse_prop {#2} {#3}
  }
\cs_generate_variant:Nn \siunitx_declare_prefix:Nnn { Nnx }
\prop_new:N \l_@@_prefixes_forward_prop
\prop_new:N \l_@@_prefixes_reverse_prop
%    \end{macrocode}
% \end{variable}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\siunitx_declare_qualifier:Nn}
%   Qualifiers are relatively easy to handle: nothing to do other than save
%   the input appropriately.
%    \begin{macrocode}
\cs_new_protected:Npn \siunitx_declare_qualifier:Nn #1#2
  {
    \@@_set_symbolic:Nnn #1
      { ~ ( #2 ) }
      { \@@_parse_qualifier:nn {#1} {#2} }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\siunitx_declare_unit:Nn, \siunitx_declare_unit:Nx}
% \begin{macro}{\siunitx_declare_unit:Nnn, \siunitx_declare_unit:Nxn}
%   For the unit parsing, allowing for variations in definition order requires
%   that a test is made for the output of each unit at point of use.
%    \begin{macrocode}
\cs_new_protected:Npn \siunitx_declare_unit:Nn #1#2
  { \siunitx_declare_unit:Nnn #1 {#2} { } }
\cs_generate_variant:Nn \siunitx_declare_unit:Nn { Nx }
\cs_new_protected:Npn \siunitx_declare_unit:Nnn #1#2#3
  {
    \seq_put_right:Nn \l_siunitx_unit_seq {#1}
    \@@_set_symbolic:Nnn #1
      {#2}
      {
        \@@_if_symbolic:nTF {#2}
          {#2}
          { \@@_parse_unit:Nn #1 {#2} }
      }
    \tl_clear_new:c { l_@@_options_ \token_to_str:N #1 _tl }
    \tl_if_empty:nF {#3}
      { \tl_set:cn { l_@@_options_ \token_to_str:N #1 _tl } {#3} }
  }
\cs_generate_variant:Nn \siunitx_declare_unit:Nnn { Nx }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Applying unit options}
%
% \begin{variable}{\l_@@_options_bool}
%    \begin{macrocode}
\bool_new:N \l_@@_options_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\siunitx_unit_options_apply:n}
%   Options apply only if they have not already been set at this group
%   level.
%    \begin{macrocode}
\cs_new_protected:Npn \siunitx_unit_options_apply:n #1
  {
    \bool_if:NF \l_@@_options_bool
      {
        \tl_if_single_token:nT {#1}
          {
            \tl_if_exist:cT { l_@@_options_ \token_to_str:N #1 _tl }
              {
                \keys_set:nv { siunitx }
                  { l_@@_options_ \token_to_str:N #1 _tl }
              }
          }
      }
    \bool_set_true:N \l_@@_options_bool
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Non-standard symbolic units}
%
% A few of the symbolic units require non-standard definitions: these are
% created here. They all use parts of the more general code but have particular
% requirements which can only be addressed by hand. Some of these could in
% principle be used in place of the dedicated definitions above, but at point
% of use that would then require additional expansions for each unit parsed:
% as the macro names would still be needed, this does not offer any real
% benefits.
%
% \begin{macro}{\per}
%   The \cs{per} symbolic unit is a bit special: it has a mechanism entirely
%   different from everything else, so has to be set up by hand. In literal
%   mode it is represented by a very simple symbol!
%    \begin{macrocode}
\@@_set_symbolic:Nnn \per
  { / }
  { \@@_parse_per: }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cancel}
% \begin{macro}{\highlight}
%   The two special cases, \cs{cancel} and \cs{highlight}, are easy to deal
%   with when parsing. When not parsing, a precaution is taken to ensure that
%   the user level equivalents always get a braced argument.
%    \begin{macrocode}
\@@_set_symbolic:Npnn \cancel
  { }
  { \@@_parse_special:n { \cancel } }
\@@_set_symbolic:Npnn \highlight #1
  { \@@_literal_special:nN { \textcolor {#1} } }
  { \@@_parse_special:n { \textcolor {#1} } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\of}
%   The generic qualifier is simply the same as the dedicated ones except for
%   needing to grab an argument.
%    \begin{macrocode}
\@@_set_symbolic:Npnn \of #1
  { \  ( #1 ) }
  { \@@_parse_qualifier:nn { \of {#1} } {#1} }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\raiseto, \tothe}
%   Generic versions of the pre-defined power macros. These require an
%   argument and so cannot be handled using the general approach. Other than
%   that, the code here is very similar to that in
%   \cs{siunitx_unit_power_set:NnN}.
%    \begin{macrocode}
\@@_set_symbolic:Npnn \raiseto #1
  { \@@_literal_power:nn {#1} }
  { \@@_parse_power:nnN { \raiseto {#1} } {#1} \c_true_bool }
\@@_set_symbolic:Npnn \tothe #1
  { ^ {#1} }
  { \@@_parse_power:nnN { \tothe {#1} } {#1} \c_false_bool }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Main formatting routine}
%
% Unit input can take two forms, \enquote{literal} units (material to be
% typeset directly) or \enquote{symbolic} units (macro-based). Before any
% parsing or typesetting is carried out, a small amount of pre-parsing has to
% be carried out to decide which of these cases applies.
%
% \begin{variable}
%   {\l_siunitx_unit_font_tl, \l_@@_product_tl, \l_@@_mass_kilogram_bool}
%   Options which apply to the main formatting routine, and so are not tied
%   to either symbolic or literal input.
%    \begin{macrocode}
\keys_define:nn { siunitx }
  {
    extract-mass-in-kilograms .bool_set:N =
      \l_@@_mass_kilogram_bool ,
    inter-unit-product .tl_set:N =
      \l_@@_product_tl ,
    unit-font-command .tl_set:N =
      \l_siunitx_unit_font_tl
  }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_formatted_tl}
%   A token list for the final formatted result: may or may not be generated
%   by the parser, depending on the nature of the input.
%    \begin{macrocode}
\tl_new:N \l_@@_formatted_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\siunitx_unit_format:nN}
% \begin{macro}{\siunitx_unit_format_extract_prefixes:nNN}
% \begin{macro}{\siunitx_unit_format_combine_exponent:nnN}
% \begin{macro}{\siunitx_unit_format_multiply:nnN}
% \begin{macro}{\siunitx_unit_format_multiply_extract_prefixes:nnNN}
% \begin{macro}{\siunitx_unit_format_multiply_combine_exponent:nnnN}
% \begin{macro}{\@@_format:nNN}
% \begin{macro}{\@@_format_aux:}
%   Formatting parsed units can take place either with the prefixes printed or
%   separated out into a power of ten. This variation is handled using two
%   separate functions: as this submodule does not really deal with numbers,
%   formatting the numeral part here would be tricky and it is better therefore
%   to have a mechanism to return a simple numerical power. At the same time,
%   most uses will no want this more complex return format and so a version of
%   the code which does not do this is also provided.
%
%   The main unit formatting routine groups all of the parsing/formatting, so
%   that the only value altered will be the return token list. As definitions
%   for the various unit macros are not globally created, the first step is to
%   map over the list of names and active the unit definitions: these do
%   different things depending on the switches set. There is then a decision to
%   be made: is the unit input one that can be parsed (\enquote{symbolic}), or
%   is is one containing one or more literals. In the latter case, there is a
%   still the need to convert the input into an expanded token list as some
%   parts of the input could still be using unit macros.
%
%   Notice that for \cs{siunitx_unit_format:nN} a second return value from the
%   auxiliary has to be allowed for, but is simply discarded.
%    \begin{macrocode}
\cs_new_protected:Npn \siunitx_unit_format:nN #1#2
  {
    \bool_set_false:N \l_@@_prefix_exp_bool
    \fp_zero:N \l_@@_combine_exp_fp
    \fp_set:Nn \l_@@_multiple_fp { \c_one_fp }
    \@@_format:nNN {#1} #2 \l_@@_tmp_fp
  }
\cs_new_protected:Npn \siunitx_unit_format_extract_prefixes:nNN #1#2#3
  {
    \bool_set_true:N \l_@@_prefix_exp_bool
    \fp_zero:N \l_@@_combine_exp_fp
    \fp_set:Nn \l_@@_multiple_fp { \c_one_fp }
    \@@_format:nNN {#1} #2 #3
  }
\cs_new_protected:Npn \siunitx_unit_format_combine_exponent:nnN #1#2#3
  {
    \bool_set_false:N \l_@@_prefix_exp_bool
    \fp_set:Nn \l_@@_combine_exp_fp {#2}
    \fp_set:Nn \l_@@_multiple_fp { \c_one_fp }
    \@@_format:nNN {#1} #3 \l_@@_tmp_fp
  }
\cs_new_protected:Npn \siunitx_unit_format_multiply:nnN #1#2#3
  {
    \bool_set_false:N \l_@@_prefix_exp_bool
    \fp_zero:N \l_@@_combine_exp_fp
    \fp_set:Nn \l_@@_multiple_fp {#2}
    \@@_format:nNN {#1} #3 \l_@@_tmp_fp
  }
\cs_new_protected:Npn \siunitx_unit_format_multiply_extract_prefixes:nnNN
  #1#2#3#4
  {
    \bool_set_true:N \l_@@_prefix_exp_bool
    \fp_zero:N \l_@@_combine_exp_fp
    \fp_set:Nn \l_@@_multiple_fp {#2}
    \@@_format:nNN {#1} #3 #4
  }
\cs_new_protected:Npn \siunitx_unit_format_multiply_combine_exponent:nnnN
  #1#2#3#4
  {
    \bool_set_false:N \l_@@_prefix_exp_bool
    \fp_set:Nn \l_@@_combine_exp_fp {#3}
    \fp_set:Nn \l_@@_multiple_fp {#2}
    \@@_format:nNN {#1} #4 \l_@@_tmp_fp
  }
\cs_new_protected:Npn \@@_format:nNN #1#2#3
  {
    \group_begin:
      \seq_map_inline:Nn \l_siunitx_unit_symbolic_seq
        { \cs_set_eq:Nc ##1 { @@_ \token_to_str:N ##1 :w } }
      \tl_clear:N \l_@@_formatted_tl
      \fp_zero:N \l_@@_prefix_fp
      \bool_if:NTF \l_@@_parse_bool
        {
          \@@_if_symbolic:nTF {#1}
            {
              \@@_parse:n {#1}
              \prop_if_empty:NF \l_@@_parsed_prop
                { \@@_format_parsed: }
            }
            {
              \bool_if:NTF \l_@@_forbid_literal_bool
                { \msg_error:nnn { siunitx } { unit / literal } {#1} }
                { \@@_format_literal:n {#1} }
            }
        }
        { \@@_format_literal:n {#1} }
      \cs_set_protected:Npx \@@_format_aux:
        {
          \tl_set:Nn \exp_not:N #2
            { \exp_not:V \l_@@_formatted_tl }
          \fp_set:Nn \exp_not:N #3
            { \fp_use:N \l_@@_prefix_fp }
        }
    \exp_after:wN \group_end:
    \@@_format_aux:
  }
\cs_new_protected:Npn \@@_format_aux: { }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Formatting literal units}
%
% While in literal mode no parsing occurs, there is a need to provide a few
% auxiliary functions to handle one or two special cases.
%
% \begin{macro}[EXP]{\@@_literal_power:nn}
%   For printing literal units which are given before the unit they apply to,
%   there is a slight rearrangement. This is ex[EXP]pandable to cover the case of
%   creation of a PDF string.
%    \begin{macrocode}
\cs_new:Npn \@@_literal_power:nn #1#2 { #2 ^ {#1} }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_literal_special:nN}
%   When dealing with the special cases, there is an argument to absorb. This
%   should be braced to be passed up to the user level, which is dealt with
%   here.
%    \begin{macrocode}
\cs_new:Npn \@@_literal_special:nN #1#2 { #1 {#2} }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_format_literal:n}
% \begin{macro}
%   {
%     \@@_format_literal_tilde:      ,
%     \@@_format_literal_subscript:  ,
%     \@@_format_literal_superscript:
%   }
% \begin{macro}{\@@_format_literal_auxi:w}
% \begin{macro}{\@@_format_literal_auxii:w}
% \begin{macro}{\@@_format_literal_auxiii:w}
% \begin{macro}{\@@_format_literal_auxiv:n}
% \begin{macro}{\@@_format_literal_auxv:nw}
% \begin{macro}
%   {
%     \@@_format_literal_auxvi:nN   ,
%     \@@_format_literal_auxvii:nN  ,
%     \@@_format_literal_auxviii:nN
%   }
% \begin{macro}{\@@_format_literal_super:nn, \@@_format_literal_sub:nn}
% \begin{macro}{\@@_format_literal_add:n}
% \begin{macro}{\@@_format_literal_auxix:nn}
% \begin{macro}{\@@_format_literal_auxx:nw}
% \begin{variable}{\l_@@_separator_tl}
%   To format literal units, there are two tasks to do. The input is
%   \texttt{x}-type expanded to force any symbolic units to be converted into
%   their literal representation: this requires setting the appropriate
%   switch. In the resulting token list, all |.| and |~| tokens are then
%   replaced by the current unit product token list. To enable this to happen
%   correctly with a normal (active) |~|, a small amount of
%   \enquote{protection} is needed first. To cover active sub- and superscript
%   tokens, appropriate definitions are provided at this stage. Those have
%   to be expandable macros rather than implicit character tokens.
%
%   As with other code dealing with user input, \cs{protected@edef} is used
%   here rather than \cs{tl_set:Nx} as \LaTeXe{} robust commands may be
%   present.
%    \begin{macrocode}
\group_begin:
  \char_set_catcode_active:n { `\~ }
  \cs_new_protected:Npx \@@_format_literal:n #1
    {
      \group_begin:
        \exp_not:n { \bool_set_false:N \l_@@_parsing_bool }
        \tl_set:Nn \exp_not:N \l_@@_tmp_tl {#1}
        \tl_replace_all:Nnn \exp_not:N \l_@@_tmp_tl
          { \token_to_str:N ^ } { ^ }
        \tl_replace_all:Nnn \exp_not:N \l_@@_tmp_tl
          { \token_to_str:N _ } { \c_@@_math_subscript_tl }
        \char_set_active_eq:NN ^
          \exp_not:N \@@_format_literal_superscript:
        \char_set_active_eq:NN _
          \exp_not:N \@@_format_literal_subscript:
        \char_set_active_eq:NN \exp_not:N ~
          \exp_not:N \@@_format_literal_tilde:
        \exp_not:n
          {
            \protected@edef \l_@@_tmp_tl
              { \l_@@_tmp_tl }
            \tl_clear:N \l_@@_formatted_tl
            \tl_if_empty:NF \l_@@_tmp_tl
              {
                \exp_after:wN \@@_format_literal_auxi:w
                  \l_@@_tmp_tl .
                  \q_recursion_tail . \q_recursion_stop
              }
            \exp_args:NNNV \group_end:
            \tl_set:Nn \l_@@_formatted_tl
              \l_@@_formatted_tl
          }
    }
\group_end:
\cs_new:Npx \@@_format_literal_subscript: { \c_@@_math_subscript_tl }
\cs_new:Npn \@@_format_literal_superscript: { ^ }
\cs_new:Npn \@@_format_literal_tilde: { . }
%    \end{macrocode}
%   To introduce the font changing commands while still allowing for line
%   breaks in literal units, a loop is needed to replace one |.| at a time.
%   To also allow for division, a second loop is used within that to handle
%   |/|: as a result, the separator between parts has to be tracked.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_literal_auxi:w #1 .
  {
    \quark_if_recursion_tail_stop:n {#1}
    \@@_format_literal_auxii:n {#1}
    \tl_set_eq:NN \l_@@_separator_tl \l_@@_product_tl
    \@@_format_literal_auxi:w
  }
\cs_set_protected:Npn \@@_format_literal_auxii:n #1
  {
    \@@_format_literal_auxiii:w
      #1 / \q_recursion_tail / \q_recursion_stop
  }
\cs_new_protected:Npn \@@_format_literal_auxiii:w #1 /
  {
    \quark_if_recursion_tail_stop:n {#1}
    \@@_format_literal_auxiv:n {#1}
    \tl_set:Nn \l_@@_separator_tl { / }
    \@@_format_literal_auxiii:w
  }
\cs_new_protected:Npn \@@_format_literal_auxiv:n #1
  {
    \@@_format_literal_auxv:nw { }
      #1 \q_recursion_tail \q_recursion_stop
  }
%    \end{macrocode}
%   To deal properly with literal formatting, we have to worry about super-
%   and subscript markers. That can be complicated as they could come anywhere
%   in the input: we handle that by iterating through the input and picking
%   them out. This avoids any issue with losing braces for mid-input scripts.
%   We also have to deal with fractions, hence needing a series of nested
%   loops and a change of separator.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_literal_auxv:nw
  #1#2 \q_recursion_stop
  {
    \tl_if_head_is_N_type:nTF {#2}
      { \@@_format_literal_auxvi:nN }
      {
        \tl_if_head_is_group:nTF {#2}
          { \@@_format_literal_auxix:nn }
          { \@@_format_literal_auxx:nw }
      }
        {#1} #2 \q_recursion_stop
  }
\cs_new_protected:Npx \@@_format_literal_auxvi:nN #1#2
  {
    \exp_not:N \quark_if_recursion_tail_stop_do:Nn #2
      { \exp_not:N \@@_format_literal_add:n {#1} }
    \exp_not:N \token_if_eq_meaning:NNTF #2 ^
      { \exp_not:N \@@_format_literal_super:nn {#1} }
      {
        \exp_not:N \token_if_eq_meaning:NNTF
          #2 \c_@@_math_subscript_tl
          { \exp_not:N \@@_format_literal_sub:nn {#1} }
          { \exp_not:N \@@_format_literal_auxvii:nN {#1} #2 }
      }
  }
%    \end{macrocode}
%   We need to make sure |\protect| sticks with the next token.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_literal_auxvii:nN #1#2
  {
    \str_if_eq:nnTF {#2} { \protect }
      { \@@_format_literal_auxviii:nN {#1} }
      { \@@_format_literal_auxv:nw {#1#2} }
  }
\cs_new_protected:Npn \@@_format_literal_auxviii:nN #1#2
  { \@@_format_literal_auxv:nw { #1 \protect #2 } }
\cs_new_protected:Npn \@@_format_literal_super:nn #1#2
  {
    \quark_if_recursion_tail_stop:n {#2}
    \@@_format_literal_add:n {#1}
    \tl_put_right:Nn \l_@@_formatted_tl { ^ {#2} }
    \@@_format_literal_auxvi:nN { }
  }
\cs_new_protected:Npx \@@_format_literal_sub:nn #1#2
  {
    \exp_not:N \quark_if_recursion_tail_stop:n {#2}
    \exp_not:N \@@_format_literal_add:n {#1}
    \tl_put_right:Nx \exp_not:N \l_@@_formatted_tl
      {
        \c_@@_math_subscript_tl
          {
            \exp_not:N \exp_not:V
              \exp_not:N \l_siunitx_unit_font_tl
                { \exp_not:N \exp_not:n {#2} }
          }
      }
    \exp_not:N \@@_format_literal_auxvi:nN { }
  }
\cs_new_protected:Npn \@@_format_literal_add:n #1
  {
    \tl_put_right:Nx \l_@@_formatted_tl
      {
        \tl_if_empty:NF \l_@@_formatted_tl
          { \exp_not:V \l_@@_separator_tl }
        \tl_if_empty:nF {#1}
          { \exp_not:V \l_siunitx_unit_font_tl { \exp_not:n {#1} } }
      }
    \tl_clear:N \l_@@_separator_tl
  }
\cs_new_protected:Npn \@@_format_literal_auxix:nn #1#2
  { \@@_format_literal_auxv:nw { #1 {#2} } }
\use:x
  {
    \cs_new_protected:Npn \exp_not:N \@@_format_literal_auxx:nw
      ##1 \c_space_tl
  }
  { \@@_format_literal_auxv:nw {#1} }
\tl_new:N \l_@@_separator_tl
%    \end{macrocode}
% \end{variable}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{(PDF) String creation}
%
% \begin{macro}{\siunitx_unit_pdfstring_context:}
%   A simple function that sets up to make units equal to their text
%   representation.
%    \begin{macrocode}
\cs_new_protected:Npn \siunitx_unit_pdfstring_context:
  {
    \bool_set_false:N \l_@@_parsing_bool
    \seq_map_inline:Nn \l_siunitx_unit_symbolic_seq
      { \cs_set_eq:Nc ##1 { @@_ \token_to_str:N ##1 :w } }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Parsing symbolic units}
%
% Parsing units takes place by storing information about each unit in a
% \texttt{prop}. As well as the unit itself, there are various other optional
% data points, for example a prefix or a power. Some of these can come before
% the unit, others only after. The parser therefore tracks the number of units
% read and uses the current position to allocate data to individual units.
%
% The result of parsing is a property list (\cs{l_@@_parsed_prop}) which
% contains one or more entries for each unit:
% \begin{itemize}
%   \item \texttt{prefix-$n$} The symbol for the prefix which applies to this
%     unit, \foreign{e.g.} for \cs{kilo} with (almost certainly) would be
%     |k|.
%   \item \texttt{unit-$n$} The symbol for the unit itself, \foreign{e.g.}~for
%     \cs{metre} with (almost certainly) would be |m|.
%   \item \texttt{power-$n$} The power which a unit is raised to. During
%     initial parsing this will (almost certainly) be positive, but is combined
%     with \texttt{per-$n$} to give a \enquote{fully qualified} power before
%     any formatting takes place
%   \item \texttt{per-$n$} Indicates that \texttt{per} applies to the current
%     unit: stored during initial parsing then combined with \texttt{power-$n$}
%     (and removed from the list) before further work.
%   \item \texttt{qualifier-$n$} Any qualifier which applies to the current
%     unit.
%   \item \texttt{special-$n$} Any \enquote{special effect} to apply to the
%     current unit.
%   \item \texttt{command-$1$} The command corresponding to \texttt{unit-$n$}:
%     needed to track base units; used for \cs{gram} only.
% \end{itemize}
%
% \begin{variable}{\l_@@_sticky_per_bool}
%   There is one option when \emph{parsing} the input (as opposed to
%   \emph{formatting} for output): how to deal with \cs{per}.
%    \begin{macrocode}
\keys_define:nn { siunitx }
  {
    sticky-per .bool_set:N = \l_@@_sticky_per_bool
  }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_parsed_prop}
% \begin{variable}{\l_@@_per_bool}
% \begin{variable}{\l_@@_position_int}
%   Parsing units requires a small number of variables are available: a
%   \texttt{prop} for the parsed units themselves, a \texttt{bool} to
%   indicate if \cs{per} is active and an \texttt{int} to track how many units
%   have be parsed.
%    \begin{macrocode}
\prop_new:N \l_@@_parsed_prop
\bool_new:N \l_@@_per_bool
\int_new:N \l_@@_position_int
%    \end{macrocode}
% \end{variable}
% \end{variable}
% \end{variable}
%
% \begin{macro}{\@@_parse:n}
%   The main parsing function is quite simple. After initialising the variables,
%   each symbolic unit is set up. The input is then simply inserted into the
%   input stream: the symbolic units themselves then do the real work of
%   placing data into the parsing system. There is then a bit of tidying up to
%   ensure that later stages can rely on the nature of the data here.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse:n #1
  {
    \prop_clear:N \l_@@_parsed_prop
    \bool_set_true:N \l_@@_parsing_bool
    \bool_set_false:N \l_@@_per_bool
    \bool_set_false:N \l_@@_test_bool
    \int_zero:N \l_@@_position_int
    \siunitx_unit_options_apply:n {#1}
    #1
    \int_step_inline:nn \l_@@_position_int
      { \@@_parse_finalise:n {##1} }
    \@@_parse_finalise:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_parse_add:nnnn}
%   In all cases, storing a data item requires setting a temporary \texttt{tl}
%   which will be used as the key, then using this to store the value. The
%   \texttt{tl} is  set using \texttt{x}-type expansion as this will expand the
%   unit index and any additional calculations made for this.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_add:nnnn #1#2#3#4
  {
    \tl_set:Nx \l_@@_tmp_tl { #1 - #2 }
    \prop_if_in:NVTF \l_@@_parsed_prop
      \l_@@_tmp_tl
      {
        \msg_error:nnxx { siunitx } { unit / duplicate-part }
          { \exp_not:n {#1} } { \token_to_str:N #3 }
      }
      {
        \prop_put:NVn \l_@@_parsed_prop
          \l_@@_tmp_tl {#4}
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_parse_prefix:Nn}
% \begin{macro}{\@@_parse_power:nnN}
% \begin{macro}{\@@_parse_qualifier:nn}
% \begin{macro}{\@@_parse_special:n}
%   Storage of the various optional items follows broadly the same pattern
%   in each case. The data to be stored is passed along with an appropriate
%   key name to the underlying storage system. The details for each type of
%   item should be relatively clear. For example, prefixes have to come before
%   their \enquote{parent} unit and so there is some adjustment to do to add
%   them to the correct unit.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_prefix:Nn #1#2
  {
    \int_set:Nn \l_@@_tmp_int { \l_@@_position_int + 1 }
    \@@_parse_add:nnnn { prefix }
      { \int_use:N \l_@@_tmp_int } {#1} {#2}
  }
\cs_new_protected:Npn \@@_parse_power:nnN #1#2#3
  {
    \tl_set:Nx \l_@@_tmp_tl
      { unit- \int_use:N \l_@@_position_int }
    \bool_lazy_or:nnTF
      {#3}
      {
        \prop_if_in_p:NV
          \l_@@_parsed_prop \l_@@_tmp_tl
      }
      {
        \@@_parse_add:nnnn { power }
          {
            \int_eval:n
              { \l_@@_position_int \bool_if:NT #3 { + 1 } }
          }
          {#1} {#2}
      }
      {
        \msg_error:nnxx { siunitx }
          { unit / part-before-unit } { power } { \token_to_str:N #1 }
      }
  }
\cs_new_protected:Npn \@@_parse_qualifier:nn #1#2
  {
    \tl_set:Nx \l_@@_tmp_tl
      { unit- \int_use:N \l_@@_position_int }
    \prop_if_in:NVTF \l_@@_parsed_prop \l_@@_tmp_tl
      {
        \@@_parse_add:nnnn { qualifier }
          { \int_use:N \l_@@_position_int } {#1} {#2}
      }
      {
        \msg_error:nnnn { siunitx }
          { unit / part-before-unit } { qualifier } { \token_to_str:N #1 }
      }
  }
%    \end{macrocode}
%  Special (exceptional) items should always come before the relevant units.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_special:n #1
  {
    \@@_parse_add:nnnn { special }
      { \int_eval:n { \l_@@_position_int + 1 } }
      {#1} {#1}
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_unit:Nn}
%   Parsing units is slightly more involved than the other cases: this is the
%   one place where the tracking value is incremented.  If the switch
%   \cs{l_@@_per_bool} is set true then the current unit is also
%   reciprocal: this can only happen if \cs{l_@@_sticky_per_bool} is also
%   true, so only one test is required.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_unit:Nn #1#2
  {
    \int_incr:N \l_@@_position_int
    \tl_if_eq:nnT {#1} { \gram }
      {
        \@@_parse_add:nnnn { command }
          { \int_use:N \l_@@_position_int }
          {#1} {#1}
      }
    \@@_parse_add:nnnn { unit }
      { \int_use:N \l_@@_position_int }
      {#1} {#2}
    \bool_if:NT \l_@@_per_bool
      {
        \@@_parse_add:nnnn { per }
          { \int_use:N \l_@@_position_int }
          { \per } { true }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_parse_per:}
%   Storing the \cs{per} command requires adding a data item separate from
%   the power which applies: this makes later formatting much more
%   straight-forward. This data could in principle be combined with the
%   \texttt{power}, but depending on the output format required that may make
%   life  more complex. Thus this information is stored separately for later
%   retrieval. If \cs{per} is set to be \enquote{sticky} then after parsing
%   the first occurrence, any further uses are in error.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_per:
  {
    \bool_if:NTF \l_@@_sticky_per_bool
      {
        \bool_set_true:N \l_@@_per_bool
        \cs_set_protected:Npn \per
          { \msg_error:nn { siunitx } { unit / duplicate-sticky-per } }
      }
      {
        \@@_parse_add:nnnn
          { per } { \int_eval:n { \l_@@_position_int + 1 } }
          { \per } { true }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_parse_finalise:n}
%   If \cs{per} applies to the current unit, the power needs to be multiplied
%   by $-1$. That is done using an \texttt{fp} operation so that non-integer
%   powers are supported. The flag for \cs{per} is also removed as this means
%   we don't have to check that the original power was positive. To be on
%   the safe side, there is a check for a trivial power at this stage.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_finalise:n #1
  {
    \tl_set:Nx \l_@@_tmp_tl { per- #1 }
    \prop_if_in:NVT \l_@@_parsed_prop \l_@@_tmp_tl
      {
        \prop_remove:NV \l_@@_parsed_prop
          \l_@@_tmp_tl
        \tl_set:Nx \l_@@_tmp_tl { power- #1 }
        \prop_get:NVNTF
          \l_@@_parsed_prop
          \l_@@_tmp_tl
          \l_@@_part_tl
          {
            \tl_set:Nx \l_@@_part_tl
              { \fp_eval:n { \l_@@_part_tl * -1 } }
            \fp_compare:nNnTF \l_@@_part_tl = 1
              {
                \prop_remove:NV \l_@@_parsed_prop
                  \l_@@_tmp_tl
              }
              {
                \prop_put:NVV \l_@@_parsed_prop
                  \l_@@_tmp_tl \l_@@_part_tl
              }
          }
          {
            \prop_put:NVn \l_@@_parsed_prop
              \l_@@_tmp_tl { -1 }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_parse_finalise:}
%   The final task is to check that there is not a \enquote{dangling} power
%   or prefix: these are added to the \enquote{next} unit so are easy to
%   test for.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_finalise:
  {
    \clist_map_inline:nn { per , power , prefix }
      {
        \tl_set:Nx \l_@@_tmp_tl
          { ##1 - \int_eval:n { \l_@@_position_int + 1 } }
        \prop_if_in:NVT \l_@@_parsed_prop \l_@@_tmp_tl
          { \msg_error:nnn { siunitx } { unit / dangling-part } { ##1 } }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Formatting parsed units}
%
% \begin{variable}
%   {
%     \l_siunitx_unit_fraction_tl    ,
%     \l_@@_denominator_bracket_bool ,
%     \l_@@_forbid_literal_bool      ,
%     \l_@@_parse_bool               ,
%     \l_@@_per_symbol_tl            ,
%     \l_@@_qualifier_mode_tl        ,
%     \l_@@_qualifier_phrase_tl
%   }
%   Set up the options which apply to formatting.
%    \begin{macrocode}
\keys_define:nn { siunitx }
  {
    bracket-unit-denominator .bool_set:N =
      \l_@@_denominator_bracket_bool ,
    forbid-literal-units .bool_set:N =
      \l_@@_forbid_literal_bool ,
    fraction-command .tl_set:N =
      \l_siunitx_unit_fraction_tl ,
    parse-units .bool_set:N =
      \l_@@_parse_bool ,
    per-mode .choice: ,
    per-mode / fraction .code:n =
      {
        \bool_set_false:N \l_@@_autofrac_bool
        \bool_set_false:N \l_@@_per_symbol_bool
        \bool_set_true:N  \l_@@_powers_positive_bool
        \bool_set_true:N  \l_@@_two_part_bool
      } ,
    per-mode / power .code:n =
      {
        \bool_set_false:N \l_@@_autofrac_bool
        \bool_set_false:N \l_@@_per_symbol_bool
        \bool_set_false:N \l_@@_powers_positive_bool
        \bool_set_false:N \l_@@_two_part_bool
      } ,
    per-mode / power-positive-first .code:n =
      {
        \bool_set_false:N \l_@@_autofrac_bool
        \bool_set_false:N \l_@@_per_symbol_bool
        \bool_set_false:N \l_@@_powers_positive_bool
        \bool_set_true:N  \l_@@_two_part_bool
      } ,
    per-mode / repeated-symbol .code:n =
      {
        \bool_set_false:N \l_@@_autofrac_bool
        \bool_set_true:N  \l_@@_per_symbol_bool
        \bool_set_true:N  \l_@@_powers_positive_bool
        \bool_set_false:N \l_@@_two_part_bool
      } ,
    per-mode / symbol .code:n =
      {
        \bool_set_false:N \l_@@_autofrac_bool
        \bool_set_true:N  \l_@@_per_symbol_bool
        \bool_set_true:N  \l_@@_powers_positive_bool
        \bool_set_true:N  \l_@@_two_part_bool
      } ,
    per-mode / symbol-or-fraction .code:n =
      {
        \bool_set_true:N \l_@@_autofrac_bool
        \bool_set_true:N \l_@@_per_symbol_bool
        \bool_set_true:N \l_@@_powers_positive_bool
        \bool_set_true:N \l_@@_two_part_bool
      } ,
    per-symbol .tl_set:N =
      \l_@@_per_symbol_tl ,
    qualifier-mode .choices:nn =
      { bracket , combine , phrase , subscript }
      { \tl_set_eq:NN \l_@@_qualifier_mode_tl \l_keys_choice_tl } ,
    qualifier-phrase .tl_set:N =
      \l_@@_qualifier_phrase_tl
  }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_bracket_bool}
%   A flag to indicate that the unit currently under construction will require
%   brackets if a power is added.
%    \begin{macrocode}
\bool_new:N \l_@@_bracket_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_bracket_open_tl, \l_@@_bracket_close_tl}
%   Abstracted out but currently purely internal.
%    \begin{macrocode}
\tl_new:N \l_@@_bracket_open_tl
\tl_new:N \l_@@_bracket_close_tl
\tl_set:Nn \l_@@_bracket_open_tl { ( }
\tl_set:Nn \l_@@_bracket_close_tl { ) }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_font_bool}
%   A flag to control when font wrapping is applied to the output.
%    \begin{macrocode}
\bool_new:N \l_@@_font_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {
%     \l_@@_autofrac_bool        ,
%     \l_@@_powers_positive_bool ,
%     \l_@@_per_symbol_bool      ,
%     \l_@@_two_part_bool
%   }
%   Dealing with the various ways that reciprocal (\cs{per}) can be handled
%   requires a few different switches.
%    \begin{macrocode}
\bool_new:N \l_@@_autofrac_bool
\bool_new:N \l_@@_per_symbol_bool
\bool_new:N \l_@@_powers_positive_bool
\bool_new:N \l_@@_two_part_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_numerator_bool}
%   Indicates that the current unit should go into the numerator when splitting
%   into two parts (fractions or other \enquote{sorted} styles).
%    \begin{macrocode}
\bool_new:N \l_@@_numerator_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_qualifier_mode_tl}
%   For storing the text of options which are best handled by picking
%   function names.
%    \begin{macrocode}
\tl_new:N \l_@@_qualifier_mode_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_combine_exp_fp}
%   For combining an exponent with the first unit.
%    \begin{macrocode}
\fp_new:N \l_@@_combine_exp_fp
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_prefix_exp_bool}
%   Used to determine if prefixes are converted into powers. Note that
%   while this may be set as an option \enquote{higher up}, at this point it
%   is handled as an internal switch (see the two formatting interfaces for
%   reasons).
%    \begin{macrocode}
\bool_new:N \l_@@_prefix_exp_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_prefix_fp}
%   When converting prefixes to powers, the calculations are done as an
%   \texttt{fp}.
%    \begin{macrocode}
\fp_new:N \l_@@_prefix_fp
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_multiple_fp}
%   For multiplying units.
%    \begin{macrocode}
\fp_new:N \l_@@_multiple_fp
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_current_tl, \l_@@_part_tl}
%   Building up the (partial) formatted unit requires some token list storage.
%   Each part of the unit combination that is recovered also has to be
%   placed in a token list: this is a dedicated one to leave the scratch
%   variables available.
%    \begin{macrocode}
\tl_new:N \l_@@_current_tl
\tl_new:N \l_@@_part_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_denominator_tl}
%   For fraction-like units, space is needed for the denominator as well as
%   the numerator (which is handled using \cs{l_@@_formatted_tl}).
%    \begin{macrocode}
\tl_new:N \l_@@_denominator_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_total_int}
%   The formatting routine needs to know both the total number of units and
%   the current unit. Thus an \texttt{int} is required in addition to
%   \cs{l_@@_position_int}.
%    \begin{macrocode}
\int_new:N \l_@@_total_int
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_format_parsed:}
% \begin{macro}{\@@_format_parsed_aux:n}
%   The main formatting routine is essentially a loop over each position,
%   reading the various parts of the unit to build up complete unit
%   combination.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_parsed:
  {
    \int_set_eq:NN \l_@@_total_int \l_@@_position_int
    \tl_clear:N \l_@@_denominator_tl
    \tl_clear:N \l_@@_formatted_tl
    \fp_zero:N \l_@@_prefix_fp
    \int_zero:N \l_@@_position_int
    \fp_compare:nNnF \l_@@_combine_exp_fp = \c_zero_fp
      { \@@_format_combine_exp: }
    \fp_compare:nNnF \l_@@_multiple_fp = \c_one_fp
      { \@@_format_multiply: }
    \bool_lazy_and:nnT
      { \l_@@_prefix_exp_bool }
      { \l_@@_mass_kilogram_bool }
      { \@@_format_mass_to_kilogram: }
    \int_do_while:nNnn
      \l_@@_position_int < \l_@@_total_int
      {
        \bool_set_false:N \l_@@_bracket_bool
        \tl_clear:N \l_@@_current_tl
        \bool_set_false:N \l_@@_font_bool
        \bool_set_true:N \l_@@_numerator_bool
        \int_incr:N \l_@@_position_int
        \clist_map_inline:nn { prefix , unit , qualifier , power , special }
          { \@@_format_parsed_aux:n {##1} }
        \@@_format_output:
      }
    \@@_format_finalise:
  }
\cs_new_protected:Npn \@@_format_parsed_aux:n #1
  {
    \tl_set:Nx \l_@@_tmp_tl
      { #1 - \int_use:N \l_@@_position_int }
    \prop_get:NVNT \l_@@_parsed_prop
      \l_@@_tmp_tl \l_@@_part_tl
      { \use:c { @@_format_ #1 : } }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_format_combine_exp:}
%   To combine an exponent into the first prefix, we first adjust for any
%   power, then deal with any existing prefix, before looking up the
%   final result.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_combine_exp:
  {
    \prop_get:NnNF \l_@@_parsed_prop { power-1 } \l_@@_tmp_tl
      { \tl_set:Nn \l_@@_tmp_tl { 1 } }
    \fp_set:Nn \l_@@_tmp_fp
      { \l_@@_combine_exp_fp / \l_@@_tmp_tl }
    \prop_get:NnNTF \l_@@_parsed_prop { prefix-1 } \l_@@_tmp_tl
      {
        \prop_get:NVNF \l_@@_prefixes_forward_prop
          \l_@@_tmp_tl \l_@@_tmp_tl
          {
            \prop_get:NnN \l_@@_parsed_prop { prefix-1 } \l_@@_tmp_tl
            \msg_error:nnx { siunitx } { unit / non-numeric-exponent }
              { \l_@@_tmp_tl }
            \tl_set:Nn \l_@@_tmp_tl { 0 }
          }
      }
      { \tl_set:Nn \l_@@_tmp_tl { 0 } }
    \tl_set:Nx \l_@@_tmp_tl
      { \fp_eval:n { \l_@@_tmp_fp + \l_@@_tmp_tl } }
    \fp_compare:nNnTF \l_@@_tmp_tl = \c_zero_fp
      { \prop_remove:Nn \l_@@_parsed_prop { prefix-1 } }
      {
        \prop_get:NVNTF \l_@@_prefixes_reverse_prop
          \l_@@_tmp_tl \l_@@_tmp_tl
          { \prop_put:NnV \l_@@_parsed_prop { prefix-1 } \l_@@_tmp_tl }
          {
            \msg_error:nnx { siunitx } { unit / non-convertible-exponent }
              { \l_@@_tmp_tl }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_format_multiply:}
%   A simple mapping.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_multiply:
  {
    \int_step_inline:nn { \prop_count:N \l_@@_parsed_prop  }
      {
        \prop_get:NnNF \l_@@_parsed_prop { power- ##1 } \l_@@_tmp_tl
          { \tl_set:Nn \l_@@_tmp_tl { 1 } }
        \fp_set:Nn \l_@@_tmp_fp
          { \l_@@_tmp_tl * \l_@@_multiple_fp }
        \fp_compare:nNnTF \l_@@_tmp_fp = \c_one_fp
          { \prop_remove:N \l_@@_parsed_prop { power- ##1 } }
          {
            \prop_put:Nnx \l_@@_parsed_prop { power- ##1 }
              { \fp_use:N \l_@@_tmp_fp }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_format_mass_to_kilogram:}
%   To deal correctly with prefix extraction in combination with kilograms, we
%   need to coerce the prefix for grams. Currently, only this one special case
%   is recorded in the property list, so we do not actually need to check the
%   value. If there is then no prefix we do a bit of gymnastics to create one
%   and then shift the starting point for the prefix extraction.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_mass_to_kilogram:
  {
    \int_step_inline:nn \l_@@_total_int
      {
        \prop_if_in:NnT \l_@@_parsed_prop { command- ##1 }
          {
            \prop_if_in:NnF \l_@@_parsed_prop { prefix- ##1 }
              {
                \group_begin:
                  \bool_set_false:N \l_@@_parsing_bool
                  \tl_set:Nx \l_@@_tmp_tl { \kilo }
                \exp_args:NNNV \group_end:
                \tl_set:Nn \l_@@_tmp_tl \l_@@_tmp_tl
                \prop_put:NnV \l_@@_parsed_prop { prefix- ##1 }
                  \l_@@_tmp_tl
                \prop_get:NnNF \l_@@_parsed_prop { power- ##1 }
                  \l_@@_tmp_tl
                  { \tl_set:Nn \l_@@_tmp_tl { 1 } }
                \fp_set:Nn \l_@@_prefix_fp
                  { \l_@@_prefix_fp - 3 * \l_@@_tmp_tl }
              }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_format_bracket:N}
%   A quick utility function which wraps up a token list variable in brackets
%   if they are required.
%    \begin{macrocode}
\cs_new:Npn \@@_format_bracket:N #1
  {
    \bool_if:NTF \l_@@_bracket_bool
      {
        \exp_not:V \l_@@_bracket_open_tl
        \exp_not:V #1
        \exp_not:V \l_@@_bracket_close_tl
      }
      { \exp_not:V #1 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_format_power:}
% \begin{macro}[EXP]{\@@_format_power_aux:wTF}
% \begin{macro}
%   {
%     \@@_format_power_positive: ,
%     \@@_format_power_negative:
%   }
% \begin{macro}[EXP]{\@@_format_power_negative_aux:w}
% \begin{macro}{\@@_format_power_superscript:}
%   Formatting powers requires a test for negative numbers and depending on
%   output format requests some adjustment to the stored value. This could be
%   done using an \texttt{fp} function, but that would be slow compared to
%   a dedicated if lower-level approach based on delimited arguments.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_power:
  {
    \@@_format_font:
    \exp_after:wN \@@_format_power_aux:wTF
      \l_@@_part_tl - \q_stop
      { \@@_format_power_negative: }
      { \@@_format_power_positive: }
  }
\cs_new:Npn \@@_format_power_aux:wTF #1 - #2 \q_stop
  { \tl_if_empty:nTF {#1} }
%    \end{macrocode}
%   In the case of positive powers, there is little to do: add the power
%   as a subscript (must be required as the parser ensures it's $\neq 1$).
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_power_positive:
  { \@@_format_power_superscript: }
%    \end{macrocode}
%   Dealing with negative powers starts by flipping the switch used to track
%   where in the final output the current part should get added to. For the
%   case where the output is fraction-like, strip off the |~| then ensure that
%   the result is not the trivial power~$1$. Assuming all is well, addition
%   to the current unit combination goes ahead.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_power_negative:
  {
    \bool_set_false:N \l_@@_numerator_bool
    \bool_if:NTF \l_@@_powers_positive_bool
      {
        \tl_set:Nx \l_@@_part_tl
          {
            \exp_after:wN \@@_format_power_negative_aux:w
              \l_@@_part_tl \q_stop
          }
        \str_if_eq:VnF \l_@@_part_tl { 1 }
          { \@@_format_power_superscript: }
      }
      { \@@_format_power_superscript: }
  }
\cs_new:Npn \@@_format_power_negative_aux:w - #1 \q_stop
  { \exp_not:n {#1} }
%    \end{macrocode}
%   Adding the power as a superscript has the slight complication that there
%   is the possibility of needing some brackets. The superscript itself uses
%   \cs{sp} as that avoids any category code issues and also allows redirection
%   at a higher level more readily.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_power_superscript:
  {
    \exp_after:wN \@@_format_power_superscipt:w
      \l_@@_part_tl . . \q_stop
  }
\cs_new_protected:Npn \@@_format_power_superscipt:w #1 . #2 . #3 \q_stop
  {
    \tl_if_blank:nTF {#2}
      {
        \tl_set:Nx \l_@@_current_tl
          {
            \@@_format_bracket:N \l_@@_current_tl
            ^ { \exp_not:n {#1} }
          }
      }
      {
        \tl_set:Nx \l_@@_tmp_tl
          {
            { }
            \tl_if_head_eq_charcode:nNTF {#1} -
              { { - } { \exp_not:o { \use_none:n #1 } } }
              { { } { \exp_not:n {#1} } }
            {#2}
            { }
            { }
            { 0 }
          }
        \tl_set:Nx \l_@@_current_tl
          {
            \@@_format_bracket:N \l_@@_current_tl
            ^ { \siunitx_number_output:N \l_@@_tmp_tl }
          }        
      }
    \bool_set_false:N \l_@@_bracket_bool
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_format_prefix:}
% \begin{macro}
%   {
%     \@@_format_prefix_exp:   ,
%     \@@_format_prefix_gram:  ,
%     \@@_format_prefix_symbol:
%   }
%   Formatting for prefixes depends on whether they are to be expressed as
%   symbols or collected up to be returned as a power of $10$. The latter
%   case requires a bit of processing, which includes checking that the
%   conversion is possible and allowing for any power that applies to the
%   current unit.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_prefix:
  {
    \bool_if:NTF \l_@@_prefix_exp_bool
      { \@@_format_prefix_exp: }
      { \@@_format_prefix_symbol: }
  }
\cs_new_protected:Npn \@@_format_prefix_exp:
  {
    \prop_get:NVNTF \l_@@_prefixes_forward_prop
      \l_@@_part_tl \l_@@_part_tl
      {
        \bool_if:NT \l_@@_mass_kilogram_bool
          {
            \tl_set:Nx \l_@@_tmp_tl
              { command- \int_use:N \l_@@_position_int }
            \prop_if_in:NVT \l_@@_parsed_prop \l_@@_tmp_tl
              { \@@_format_prefix_gram: }
          }
        \tl_set:Nx \l_@@_tmp_tl
          { power- \int_use:N \l_@@_position_int }
        \prop_get:NVNF \l_@@_parsed_prop
          \l_@@_tmp_tl \l_@@_tmp_tl
          { \tl_set:Nn \l_@@_tmp_tl { 1 } }
        \fp_add:Nn \l_@@_prefix_fp
          { \l_@@_tmp_tl * \l_@@_part_tl }
      }
      { \@@_format_prefix_symbol: }
  }
%    \end{macrocode}
%   When the units in use are grams, we may need to deal with conversion to
%   kilograms.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_prefix_gram:
  {
    \tl_set:Nx \l_@@_part_tl
      { \int_eval:n { \l_@@_part_tl - 3 } }
    \group_begin:
      \bool_set_false:N \l_@@_parsing_bool
      \tl_set:Nx \l_@@_current_tl { \kilo }
    \exp_args:NNNV \group_end:
    \tl_set:Nn \l_@@_current_tl \l_@@_current_tl
  }
\cs_new_protected:Npn \@@_format_prefix_symbol:
  { \tl_set_eq:NN \l_@@_current_tl \l_@@_part_tl }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_format_qualifier:}
% \begin{macro}
%   {
%     \@@_format_qualifier_bracket:   ,
%     \@@_format_qualifier_combine:   ,
%     \@@_format_qualifier_phrase:    ,
%     \@@_format_qualifier_subscript:
%   }
%  There are various ways that a qualifier can be added to the output. The
%  idea here is to modify the \enquote{base} text appropriately and then add
%  to the current unit. Notice that when the qualifier is just treated as
%  \enquote{text}, the auxiliary is actually a no-op.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_qualifier:
  {
    \use:c
      {
        @@_format_qualifier_
        \l_@@_qualifier_mode_tl :
      }
    \tl_put_right:NV \l_@@_current_tl \l_@@_part_tl
  }
\cs_new_protected:Npn \@@_format_qualifier_bracket:
  {
    \@@_format_font:
    \tl_set:Nx \l_@@_part_tl
      {
        \exp_not:V \l_@@_bracket_open_tl
        \exp_not:V \l_siunitx_unit_font_tl
          { \exp_not:V \l_@@_part_tl }
        \exp_not:V \l_@@_bracket_close_tl
      }
  }
\cs_new_protected:Npn \@@_format_qualifier_combine: { }
\cs_new_protected:Npn \@@_format_qualifier_phrase:
  {
    \@@_format_font:
    \tl_set:Nx \l_@@_part_tl
      {
        \exp_not:V \l_@@_qualifier_phrase_tl
        \exp_not:V \l_siunitx_unit_font_tl
          { \exp_not:V \l_@@_part_tl }
      }
  }
\cs_new_protected:Npn \@@_format_qualifier_subscript:
  {
    \@@_format_font:
    \tl_set:Nx \l_@@_part_tl
      {
        \c_@@_math_subscript_tl
          {
            \exp_not:V \l_siunitx_unit_font_tl
              { \exp_not:V \l_@@_part_tl }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_format_special:}
%   Any special odds and ends are handled by simply making the current
%   combination into an argument for the recovered code. Font control
%   needs to be \emph{inside} the special formatting here.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_special:
  {
    \tl_set:Nx \l_@@_current_tl
      {
        \exp_not:V \l_@@_part_tl
         {
           \bool_if:NTF \l_@@_font_bool
             { \use:n }
             { \exp_not:V \l_siunitx_unit_font_tl }
              { \exp_not:V \l_@@_current_tl }
         }
      }
    \bool_set_true:N \l_@@_font_bool
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_format_unit:}
%   A very simple task: add the unit to the output currently being
%   constructed.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_unit:
  {
    \tl_put_right:NV
      \l_@@_current_tl \l_@@_part_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_format_output:}
% \begin{macro}
%   {\@@_format_output_aux:, \@@_format_output_denominator:}
% \begin{macro}
%   {
%     \@@_format_output_aux:nn ,
%     \@@_format_output_aux:nV ,
%     \@@_format_output_aux:nv
%   }
%   The first step here is to make a choice based on whether the current
%   part should be stored as part of the numerator or denominator of a
%   fraction. In all cases, if the switch \cs{l_@@_numerator_bool} is
%   true then life is simple: add the current part to the numerator with
%   a standard separator
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_output:
  {
    \@@_format_font:
    \bool_set_false:N \l_@@_bracket_bool
    \use:c
      {
        @@_format_output_
        \bool_if:NTF \l_@@_numerator_bool
          { aux: }
          { denominator: }
      }
  }
\cs_new_protected:Npn \@@_format_output_aux:
  {
    \@@_format_output_aux:nV { formatted }
      \l_@@_product_tl
  }
%    \end{macrocode}
%   There are a few things to worry about at this stage if the current part
%   is in the denominator.  Powers have already been dealt with and some
%   formatting outcomes only need a branch at the final point of building
%   the entire unit. That means that there are three possible outcomes here:
%   if collecting two separate parts, add to the denominator with a product
%   separator, or if only building one token list there may be a need to use
%   a symbol separator. When the |repeated-symbol| option is in use there may
%   be a need to add a leading |1| to the output in the case where the
%   first unit is in the denominator: that can be picked up by looking for
%   empty output in combination with the flag for using a symbol in the output
%   but not a two-part strategy.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_output_denominator:
  {
    \bool_if:NTF \l_@@_two_part_bool
      {
        \bool_lazy_and:nnT
          { \l_@@_denominator_bracket_bool }
          { ! \tl_if_empty_p:N \l_@@_denominator_tl }
          { \bool_set_true:N \l_@@_bracket_bool }
        \@@_format_output_aux:nV { denominator }
          \l_@@_product_tl
      }
      {
        \bool_lazy_and:nnT
          { \l_@@_per_symbol_bool }
          { \tl_if_empty_p:N \l_@@_formatted_tl }
          { \tl_set:Nn \l_@@_formatted_tl { 1 } }
        \@@_format_output_aux:nv { formatted }
          {
            l_@@_
            \bool_if:NTF \l_@@_per_symbol_bool
              { per_symbol }
              { product }
            _tl
          }
      }
  }
\cs_new_protected:Npn \@@_format_output_aux:nn #1#2
  {
    \tl_set:cx { l_@@_ #1 _tl }
      {
         \exp_not:v { l_@@_ #1 _tl }
         \tl_if_empty:cF { l_@@_ #1 _tl }
           { \exp_not:n {#2} }
         \exp_not:V \l_@@_current_tl
      }
  }
\cs_generate_variant:Nn \@@_format_output_aux:nn { nV , nv }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_format_font:}
%   A short auxiliary which checks if the font has been applied to the
%   main part of the output: if not, add it and set the flag.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_font:
  {
    \bool_if:NF \l_@@_font_bool
      {
        \tl_set:Nx \l_@@_current_tl
          {
            \exp_not:V \l_siunitx_unit_font_tl
              { \exp_not:V \l_@@_current_tl }
          }
        \bool_set_true:N \l_@@_font_bool
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_format_finalise:}
% \begin{macro}
%   {
%     \@@_format_finalise_autofrac:   ,
%     \@@_format_finalise_fractional: ,
%     \@@_format_finalise_power:
%   }
%   Finalising the unit format is really about picking up the cases involving
%   fractions: these require assembly of the parts with the need to add
%   additional material in some cases
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_finalise:
  {
    \tl_if_empty:NF \l_@@_denominator_tl
      {
        \bool_if:NTF \l_@@_powers_positive_bool
          { \@@_format_finalise_fractional: }
          { \@@_format_finalise_power: }
      }
  }
%    \end{macrocode}
%   For fraction-like output, there are three possible choices and two
%   actual styles. In all cases, if the numerator is empty then it is set
%   here to |1|.  To deal with the \enquote{auto-format} case, the two
%   styles (fraction and symbol) are handled in auxiliaries: this allows both
%   to be used at the same time! Beyond that, the key here is to use a
%   single \cs{tl_set:Nx} to keep down the number of assignments.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_finalise_fractional:
  {
    \tl_if_empty:NT \l_@@_formatted_tl
      { \tl_set:Nn \l_@@_formatted_tl { 1 } }
    \bool_if:NTF \l_@@_autofrac_bool
      { \@@_format_finalise_autofrac: }
      {
        \bool_if:NTF \l_@@_per_symbol_bool
          { \@@_format_finalise_symbol: }
          { \@@_format_finalise_fraction: }
      }
  }
%    \end{macrocode}
%   For the \enquote{auto-selected} fraction method, the two other auxiliary
%   functions are used to do both forms of formatting. So that everything
%   required is available, this needs one group so that the second auxiliary
%   receives the correct input. After that it is just a case of applying
%   \cs{mathchoice} to the formatted output.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_finalise_autofrac:
  {
    \group_begin:
      \@@_format_finalise_fraction:
    \exp_args:NNNV \group_end:
    \tl_set:Nn \l_@@_tmp_tl \l_@@_formatted_tl
    \@@_format_finalise_symbol:
    \tl_set:Nx \l_@@_formatted_tl
      {
        \mathchoice
          { \exp_not:V \l_@@_tmp_tl }
          { \exp_not:V \l_@@_formatted_tl }
          { \exp_not:V \l_@@_formatted_tl }
          { \exp_not:V \l_@@_formatted_tl }
      }
  }
%    \end{macrocode}
%   When using a fraction function the two parts are now assembled.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_finalise_fraction:
  {
    \tl_set:Nx \l_@@_formatted_tl
      {
        \exp_not:V \l_siunitx_unit_fraction_tl
          { \exp_not:V \l_@@_formatted_tl }
          { \exp_not:V \l_@@_denominator_tl }
      }
  }
\cs_new_protected:Npn \@@_format_finalise_symbol:
  {
    \tl_set:Nx \l_@@_formatted_tl
      {
        \exp_not:V \l_@@_formatted_tl
        \exp_not:V \l_@@_per_symbol_tl
        \@@_format_bracket:N \l_@@_denominator_tl
      }
  }
%    \end{macrocode}
%   In the case of sorted powers, there is a test to make sure there was
%   at least one positive power, and if so a simple join of the two parts
%   with the appropriate product.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_finalise_power:
  {
    \tl_if_empty:NTF \l_@@_formatted_tl
      {
        \tl_set_eq:NN
          \l_@@_formatted_tl
          \l_@@_denominator_tl
      }
      {
        \tl_set:Nx \l_@@_formatted_tl
          {
            \exp_not:V \l_@@_formatted_tl
            \exp_not:V \l_@@_product_tl
            \exp_not:V \l_@@_denominator_tl
          }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Non-Latin character support}
%
% \begin{macro}{\@@_non_latin:n}
% \begin{macro}{\@@_non_latin:nnnn}
%   A small amount of code to make it convenient to include non-Latin
%   characters in units without having to directly include them in the
%   sources directly. We only make the first token active as some packages
%   (\foreign{e.g.}~\pkg{kotex}) do this.
%    \begin{macrocode}
\bool_lazy_or:nnTF
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
    \cs_new:Npn \@@_non_latin:n #1
      { \char_generate:nn {#1} { \char_value_catcode:n {#1} } }
  }
  {
    \cs_new:Npn \@@_non_latin:n #1
      {
        \exp_last_unbraced:Nf \@@_non_latin:nnnn
          { \char_to_utfviii_bytes:n {#1} }
      }
    \cs_new:Npn \@@_non_latin:nnnn #1#2#3#4
      {
        \exp_after:wN \exp_after:wN \exp_after:wN
          \exp_not:N \char_generate:nn {#1} { 13 }
        \char_generate:nn {#2} { 12 }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Pre-defined unit components}
%
% Quite a number of units can be predefined: while this is a code-level module,
% there is little point having a unit parser which does not start off able to
% parse any units!
%
% \begin{macro}
%   {
%     \kilogram ,
%     \metre    ,
%     \meter    ,
%     \mole     ,
%     \kelvin   ,
%     \candela  ,
%     \second   ,
%     \ampere
%   }
%   The basic \acro{SI} units: technically the correct spelling is \cs{metre}
%   but US users tend to use \cs{meter}.
%    \begin{macrocode}
\siunitx_declare_unit:Nn \kilogram { \kilo \gram }
\siunitx_declare_unit:Nn \metre    { m }
\siunitx_declare_unit:Nn \meter    { \metre }
\siunitx_declare_unit:Nn \mole     { mol }
\siunitx_declare_unit:Nn \second   { s }
\siunitx_declare_unit:Nn \ampere   { A }
\siunitx_declare_unit:Nn \kelvin   { K }
\siunitx_declare_unit:Nn \candela  { cd }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\gram}
%   The gram is an odd unit as it is needed for the base unit kilogram.
%    \begin{macrocode}
\siunitx_declare_unit:Nn \gram { g }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \yocto ,
%     \zepto ,
%     \atto  ,
%     \femto ,
%     \pico  ,
%     \nano  ,
%     \micro ,
%     \milli ,
%     \centi ,
%     \deci
%  }
%   The various \acro{SI} multiple prefixes are defined here: first the small
%   ones.
%    \begin{macrocode}
\siunitx_declare_prefix:Nnn \yocto { -24 } { y }
\siunitx_declare_prefix:Nnn \zepto { -21 } { z }
\siunitx_declare_prefix:Nnn \atto  { -18 } { a }
\siunitx_declare_prefix:Nnn \femto { -15 } { f }
\siunitx_declare_prefix:Nnn \pico  { -12 } { p }
\siunitx_declare_prefix:Nnn \nano  {  -9 } { n }
\siunitx_declare_prefix:Nnx \micro { -6 } { \@@_non_latin:n { "03BC } }
\siunitx_declare_prefix:Nnn \milli { -3 } { m }
\siunitx_declare_prefix:Nnn \centi { -2 } { c }
\siunitx_declare_prefix:Nnn \deci  { -1 } { d }
%    \end{macrocode}
% \end{macro}
% \begin{macro}
%   {
%     \deca  ,
%     \deka  ,
%     \hecto ,
%     \kilo  ,
%     \mega  ,
%     \giga  ,
%     \tera  ,
%     \peta  ,
%     \exa   ,
%     \zetta ,
%     \yotta
%   }
%   Now the large ones.
%    \begin{macrocode}
\siunitx_declare_prefix:Nnn \deca  { 1 }  { da }
\siunitx_declare_prefix:Nnn \deka  { 1 }  { da }
\siunitx_declare_prefix:Nnn \hecto { 2 }  { h }
\siunitx_declare_prefix:Nnn \kilo  { 3 }  { k }
\siunitx_declare_prefix:Nnn \mega  { 6 }  { M }
\siunitx_declare_prefix:Nnn \giga  { 9 }  { G }
\siunitx_declare_prefix:Nnn \tera  { 12 } { T }
\siunitx_declare_prefix:Nnn \peta  { 15 } { P }
\siunitx_declare_prefix:Nnn \exa   { 18 } { E }
\siunitx_declare_prefix:Nnn \zetta { 21 } { Z }
\siunitx_declare_prefix:Nnn \yotta { 24 } { Y }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \becquerel     ,
%     \degreeCelsius ,
%     \coulomb       ,
%     \farad         ,
%     \gray          ,
%     \hertz         ,
%     \henry         ,
%     \joule         ,
%     \katal         ,
%     \lumen         ,
%     \lux
%  }
%    Named derived units: first half of alphabet.
%    \begin{macrocode}
\siunitx_declare_unit:Nn \becquerel { Bq }
\siunitx_declare_unit:Nx \degreeCelsius { \@@_non_latin:n { "00B0 } C }
\siunitx_declare_unit:Nn \coulomb   { C }
\siunitx_declare_unit:Nn \farad     { F }
\siunitx_declare_unit:Nn \gray      { Gy }
\siunitx_declare_unit:Nn \hertz     { Hz }
\siunitx_declare_unit:Nn \henry     { H }
\siunitx_declare_unit:Nn \joule     { J }
\siunitx_declare_unit:Nn \katal     { kat }
\siunitx_declare_unit:Nn \lumen     { lm }
\siunitx_declare_unit:Nn \lux       { lx }
%    \end{macrocode}
% \end{macro}
% \begin{macro}
%   {
%     \newton    ,
%     \ohm       ,
%     \pascal    ,
%     \radian    ,
%     \siemens   ,
%     \sievert   ,
%     \steradian ,
%     \tesla     ,
%     \volt      ,
%     \watt      ,
%     \weber
%  }
%    Named derived units: second half of alphabet.
%    \begin{macrocode}
\siunitx_declare_unit:Nn \newton    { N }
\siunitx_declare_unit:Nx \ohm       { \@@_non_latin:n { "03A9 } }
\siunitx_declare_unit:Nn \pascal    { Pa }
\siunitx_declare_unit:Nn \radian    { rad }
\siunitx_declare_unit:Nn \siemens   { S }
\siunitx_declare_unit:Nn \sievert   { Sv }
\siunitx_declare_unit:Nn \steradian { sr }
\siunitx_declare_unit:Nn \tesla     { T }
\siunitx_declare_unit:Nn \volt      { V }
\siunitx_declare_unit:Nn \watt      { W }
\siunitx_declare_unit:Nn \weber     { Wb }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \astronomicalunit ,
%     \bel              ,
%     \dalton           ,
%     \day              ,
%     \decibel          ,
%     \electronvolt     ,
%     \hectare          ,
%     \hour             ,
%     \litre            ,
%     \liter            ,
%     \minute           ,
%     \neper            ,
%     \tonne
%  }
%   Non-\acro{SI}, but accepted for general use. Once again there are two
%   spellings, here for litre and with different output in this case.
%    \begin{macrocode}
\siunitx_declare_unit:Nn \astronomicalunit { au }
\siunitx_declare_unit:Nn \bel              { B }
\siunitx_declare_unit:Nn \decibel          { \deci \bel }
\siunitx_declare_unit:Nn \dalton           { Da }
\siunitx_declare_unit:Nn \day              { d }
\siunitx_declare_unit:Nn \electronvolt     { eV }
\siunitx_declare_unit:Nn \hectare          { ha }
\siunitx_declare_unit:Nn \hour             { h }
\siunitx_declare_unit:Nn \litre            { L }
\siunitx_declare_unit:Nn \liter            { \litre }
\siunitx_declare_unit:Nn \minute           { min }
\siunitx_declare_unit:Nn \neper            { Np }
\siunitx_declare_unit:Nn \tonne            { t }
%    \end{macrocode}
% \end{macro}
% \begin{macro}
%   {
%     \arcminute ,
%     \arcsecond ,
%     \degree
%  }
%   Arc units: again, non-\acro{SI}, but accepted for general use.
%    \begin{macrocode}
\siunitx_declare_unit:Nx \arcminute { \@@_non_latin:n { "02B9 } }
\siunitx_declare_unit:Nx \arcsecond { \@@_non_latin:n { "02BA } }
\siunitx_declare_unit:Nx \degree { \@@_non_latin:n { "00B0 } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\percent}
%   For percent, the raw character is the most flexible way of handling output.
%    \begin{macrocode}
\siunitx_declare_unit:Nx \percent { \cs_to_str:N \% }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\square, \squared, \cubic, \cubed}
%   Basic powers.
%    \begin{macrocode}
\siunitx_declare_power:NNn \square \squared { 2 }
\siunitx_declare_power:NNn \cubic  \cubed   { 3 }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Messages}
%
%    \begin{macrocode}
\msg_new:nnnn { siunitx } { unit / dangling-part }
  { Found~#1~part~with~no~unit. }
  {
    Each~#1~part~must~be~associated~with~a~unit:~a~#1~part~was~found~
    but~no~following~unit~was~given.
  }
\msg_new:nnnn { siunitx } { unit / duplicate-part }
  { Duplicate~#1~part:~#2. }
  {
    Each~unit~may~have~only~one~#1:\\
    the~additional~#1~part~'#2'~will~be~ignored.
  }
\msg_new:nnnn { siunitx } { unit / duplicate-sticky-per }
  { Duplicate~\token_to_str:N \per. }
  {
    When~the~'sticky-per'~option~is~active,~only~one~
    \token_to_str:N \per \  may~appear~in~a~unit.
  }
\msg_new:nnnn { siunitx } { unit / literal }
  { Literal~units~disabled. }
  {
    You~gave~the~literal~input~'#1'~
    but~literal~unit~output~is~disabled.
  }
\msg_new:nnnn { siunitx } { unit / non-convertible-exponent }
  { Exponent~'#1'~cannot~be~converted~into~a~symbolic~prefix. }
  {
    The~exponent~'#1'~does~not~match~with~any~of~the~symbolic~prefixes~
    set~up.
  }
\msg_new:nnnn { siunitx } { unit / non-numeric-exponent }
  { Prefix~'#1'~does~not~have~a~numerical~value. }
  {
    The~prefix~'#1'~needs~to~be~combined~with~a~number,~but~it~has~no
    numerical~value.
  }
\msg_new:nnnn { siunitx } { unit / part-before-unit }
  { Found~#1~part~before~first~unit:~#2. }
  {
    The~#1~part~'#2'~must~follow~after~a~unit:~
    it~cannot~appear~before~any~units~and~will~therefore~be~ignored.
  }
%    \end{macrocode}
%
% \subsection{Standard settings for module options}
%
% Some of these follow naturally from the point of definition
% (\foreign{e.g.}~boolean variables are always |false| to begin with),
% but for clarity everything is set here.
%    \begin{macrocode}
\keys_set:nn { siunitx }
  {
    bracket-unit-denominator  = true      ,
    forbid-literal-units      = false     ,
    fraction-command          = \frac     ,
    inter-unit-product        = \,        ,
    extract-mass-in-kilograms = true      ,
    parse-units               = true      ,
    per-mode                  = power     ,
    per-symbol                = /         ,
    qualifier-mode            = subscript ,
    qualifier-phrase          =           ,
    sticky-per                = false     ,
    unit-font-command         = \mathrm
  }
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \end{implementation}
%
% \begin{thebibliography}{1}
%   \bibitem{BIPM}
%     \emph{The International System of Units (\acro{SI})},
%     \url{https://www.bipm.org/en/measurement-units/}.
%   \bibitem{base-units}
%     \emph{SI base units},
%     \url{https://www.bipm.org/en/measurement-units/si-base-units}.
% \end{thebibliography}
%
% \PrintIndex