summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/cleveref/cleveref.dtx
blob: 6cad24c2f7aff0800b5632bb0e8dbe335c945d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
% \iffalse meta-comment
%
% Package 'cleveref' to use with LaTeX2e
% Copyright 2007 by Toby Cubitt
%
% Written and maintained by Toby Cubitt
%
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.2
%% of this license or (at your option) any later version.
%% The latest version of this license is in:
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% and version 1.2 or later is part of all distributions of LaTeX 
%% version 1999/12/01 or later.
% 
% Please send bugreports and suggestions to toby-cleveref@dr-qubit.org
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{cleveref}
%<package> [2007/21/11 v0.11 Automatic cross-reference formatting]
%
%<*driver>
\documentclass{ltxdoc}
%\documentclass[draft]{ltxdoc}
\usepackage{url}
\usepackage{hyperref}
\usepackage{cleveref}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%%\newcommand{\package}[1]{\texttt{#1.sty}}
\newcommand{\package}[1]{\texttt{#1}}
\newcommand{\file}{\texttt}
\newcommand{\option}{\texttt}
\makeatletter
%% Command for writing code descriptions
\def\packeddescr{%
   \begin{list}{}{\leftmargin0.5cm
                  \labelwidth\z@\itemindent-\leftmargin
                  \topsep=0pt\itemsep=0.05cm\parsep=0.05cm
%                 \let\makelabel\nlabel
                }}
\def\endpackeddescr{\end{list}}
%% Commands for referring to code lines
\def\codelabel#1{\@bsphack  
 \protected@write\@auxout{}{\string\newlabel{#1}{{\number\the
           \c@CodelineNo}{\thepage}}}\@esphack}
\newcounter{tmpcount}
\def\coderef#1#2{\setcounter{tmpcount}{0}\@ifundefined{r@#1}\relax
          {\setcounter{tmpcount}{\ref{#1}}}\relax
          \addtocounter{tmpcount}{#2}\arabic{tmpcount}}
\makeatother
\begin{document}
  \DocInput{cleveref.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{3387}
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%
% \changes{v0.1}{2006/29/06}{Initial version}
% \changes{v0.2}{2007/18/04}{Complete rewrite}
% \changes{v0.3}{2007/26/04}{Added reference ranges}
% \changes{v0.4}{2007/26/05}{Renamed "cleveref" -- first public release}
% \changes{v0.5}{2007/31/05}{Added poorman option}
% \changes{v0.6}{2007/11/06}{Poorman no longer writes extra .tmp file}
% \changes{v0.7}{2007/21/06}{Allow prevention of reference range collapsing}
% \changes{v0.8}{2007/10/07}%
%   {Made referencing commands robust, improved default formats, made
%   starred versions of referencing commands for hyperref, allowed
%   multiple consecutive empty references in list to prevent range
%   collapsing, and fixed bugs. Phew!}
% \changes{v0.8.1}{2007/08/08}{Bug fixes.}
% \changes{v0.9}{2007/29/10}%
%   {Added an extra conjunction and format component, used when a list
%   only contains a single pair of references or reference
%   sub-lists. Formats are now constructed from customisable
%   components, unless overridden using the old format definition
%   commands.}
% \changes{v0.10}{2007/30/10}%
%   {Allow optional argument to \\label to override reference
%   type. Removed hyperref and ntheorem options; support for these and
%   amsmath is enabled automatically if those packages are loaded.}
% \changes{v0.11}{2007/21/11}{Made referencing aware of appendices.}
% \changes{v0.11.1}{2007/29/11}{Bug fixes.}
%
%
% \GetFileInfo{cleveref.sty}
% 
% \def\fileversion{0.11}
% \def\filedate{21/11/2007}
%
% \title{The \package{cleveref} package\thanks{This document
%     corresponds to \textsf{cleveref}~\fileversion, dated
%     \filedate.}}
% \author{Toby Cubitt \\ \texttt{toby-cleveref@dr-qubit.org}}
% \date{\filedate}
%
% \maketitle
%
% \begin{abstract}
%   The \package{cleveref} package enhances \LaTeX{}'s
%   cross-referencing features, allowing the format of references to
%   be determined automatically according to the type of reference
%   (equation, section, etc.) and the context in which the reference
%   is used. The formatting for each reference type can be fully
%   customised in the preamble of your document. In addition,
%   \package{cleveref} can typeset references to lists of multiple
%   labels, automatically formatting them according to their type, and
%   collapsing sequences of numerically consecutive labels to a
%   reference range. Again, the multiple-reference formatting is fully
%   customisable.
% \end{abstract}
%
%
%
% \section{Introduction}
% When ``clever'' is used in the name of a computer program, it
% usually signifies that the programmer is overly smug about his
% achievements! On the other hand, at the heart of the \LaTeX{}
% philosophy is the idea that it is clever to delegate as much of the
% typesetting as possible to the computer, in order to achieve a
% beautiful, but above all consistent, visual appearance.
% 
% Both these points of view are probably valid when it comes to the
% \package{cleveref} package. It's goals are two-fold: to use the
% information that \LaTeX{} inherently has about labels as
% intelligently as possible in order to type-set references to them
% (clever processing); and to enable you to produce an attractive,
% consistent formatting of references throughout your document, with
% the minimum of effort (you'd be clever to use it!).
%
% The \package{cleveref} package enhances \LaTeX{}'s cross-referencing
% facilities by allowing references to be formatted automatically
% according to the type of object they refer to (chapter, section,
% equation, theorem, etc.) and the context in which the reference is
% used. It can also automatically format references to multiple
% labels, automatically collapsing references to consecutive labels
% into a reference range, and all kinds of other clever wizardry.
%
% In standard \LaTeX{}, you have almost certainly found yourself
% writing things like |Eq.~(\ref{eq1})| and
% |Theorems~\ref{thm1} to~\ref{thm2}| over and over again. Tedium
% isn't the only downside to this. What happens if you later decide
% you want equation references to be typeset as |Equation~\ref{eq1}|
% instead?  What happens if you decide to change the theorem labelled
% |thm1| into a lemma? You have to search through the entire \LaTeX{}
% source of your document, modifying all references to equations, or
% changing all references to |thm1|.
%
% The \package{cleveref} package allows you to define the format for
% references once-and-for-all in the preamble of your document. If you
% later decide to change the format of equation references, you only
% have to change one preamble definition. If you xchange a theorem
% into a lemma, you don't need to change any references at all,
% because \package{cleveref} will automatically typeset references to
% it using the appropriate formatting. This makes it far easier to
% typeset references uniformly across your whole document, as well as
% avoiding repetitively typing the same text for each and every
% reference.
%
% There are a number of other packages with similar goals, most
% notably \package{varioref}, \package{fancyref}, \package{hyperref}'s
% |\autoref| command, and (for theorem-like environments)
% \package{ntheorem} (with the \option{thref} option). (There are many
% others, but these come closest to providing similar features to
% \package{cleveref}.) However, all have certain deficiencies which
% \package{cleveref} attempts to overcome.
%
% The \package{fancyref} package doesn't automatically determine the
% type of object being referred to. Instead, it relies on you adhering
% to a naming convention for labels. This is usually a good idea in
% any case, but it can be inconvenient. For example, if you change a
% theorem into a lemma, you have to change the label name, and
% therefore also all references to it. So you are back to searching
% and replacing through the entire document.
%
% The enhanced referencing feature provided by the \package{varioref}
% package decides how to format references when the label is
% \emph{defined}, rather than when it is \emph{referenced}. Often this
% isn't a problem. But it makes it impossible to format references
% differently according the context in which they are referenced,
% which can sometimes be very useful. For example, if you want
% references at the beginning of a sentence formatted any other way
% than by capitalising the first letter of the reference text, it is
% impossible using \package{varioref}. Perhaps even more
% significantly, it makes it impossible to typeset multiple references
% automatically; you are back typesetting
% |Eqs.~(\ref{eq1}) and~(\ref{eq2})| or
% |Eqs.~(\ref{eq1})--(\ref{eq3})| by hand. Not to mention missing out
% on automatic collapsing of consecutive references,
% \package{ntheorem} support, etc.
%
% The \package{hyperref} package's |\autoref| command typesets a name
% before a reference, determined by the reference type. This is less
% flexible than \package{cleveref}'s fully customisable reference
% formatting but, when combined with \package{varioref}, the two
% packages working together come close. However, even with
% \package{hyperref}, it is impossible to customise precisely which
% part of the reference is made into a hyper-link in PDF documents;
% this is very easy with \package{cleveref}. And it still remains
% impossible to typeset multiple references, have consecutive
% references collapsed automatically, etc.
%
% The \package{ntheorem} package (with the \option{thref} option) does
% things right\ldots except that it only works for theorem-like
% environments. It is possible to use it for other environments, but
% only in a bastardized form, by manually supplying an optional
% argument to |\label| commands that specifies the label
% type. \package{cleveref} works equally well when referencing any
% type of object, as well as fully supporting \package{ntheorem}. And
% again, \package{cleveref} provides a number additional features,
% such as multiple references, automatic collapsing of reference
% ranges, control over the placement of hyper-links, etc.
%
%
%
% \section{Usage}
% The \package{cleveref} package is loaded in the usual way, by
% putting the line
% \begin{quote}
%   |\usepackage{cleveref}|
% \end{quote}
% in your document's preamble. However, care must be taken when using
% \package{cleveref} in conjunction with other packages that modify
% \LaTeX{}'s referencing system (see
% \cref{sec:other_packages}). Basically, \package{cleveref} must be
% loaded \emph{last}.
%
% If you just want to get going quickly with \package{cleveref}, and
% come back later to read up on all the features it provides in more
% detail, here's what you need to do. Wherever you would previously
% have used |\ref|, use |\cref| instead. (Except at the beginning of a
% sentence, where you should use |\Cref|.) You no longer need to put
% the name of the thing you're referencing in front of the |\cref|
% command, because \package{cleveref} will sort that out for you:
% i.e.\ use |\cref{eq1}| instead of |eq.~(\ref{eq1})|. If you want to
% refer to a range of labels, use |\crefrange|: |\crefrange{eq1}{eq5}|
% gives |eqs.~(1)--(5)|. Finally, if you want to refer to multiple
% things at once, you can now combine them all into one reference and
% leave \package{cleveref} to sort it out: e.g.\
% |\cref{eq1,eq2,eq3,eq5,thm2,def1}| produces
% |eqs.~(1)--(3) and~(5), theorem~5, and definition~1|.
%
%
% \section{Typesetting References}
% \label{sec:typesetting}
% \DescribeMacro\cref
% To automatically typeset a cross-reference according to the type of
% object referred to, simply refer to it using
% |\cref{|\meta{label}|}|.  \package{cleveref} imposes just one extra
% restriction on the names of labels: they are no longer allowed to
% contain commas ``,''. These are instead used to typeset multiple
% references (see below).
%
% \DescribeMacro\Cref
% As it is very difficult\footnote{Actually, very likely impossible!}
% for \LaTeX{} to determine whether a cross-reference appears at the
% beginning of a sentence or not, a capitalised version exists:
% |\Cref{|\meta{label}|}|. By default, this typesets the reference
% with the first letter capitalised. (Though the formatting of the
% |\cref| and |\Cref| forms can be fully and independently customised,
% see \cref{sec:customising}.)
%
% \DescribeMacro\ref
% \package{cleveref} does \emph{not} modify the standard |\ref|
% command\footnote{This is not quite true. The original \cmd{\ref}
%   command no longer works when \package{cleveref} is loaded, so
%   \package{cleveref} redefines it to recover the original
%   behaviour.}, so you can still use it to typeset the formatted
% label counter alone, without any additional text or formatting.
%
% \DescribeMacro\crefrange
% \DescribeMacro\Crefrange
% To typeset a reference range, e.g. |Eqs.~(1.1)--(1.5)|, use
% |\crefrange| or |\Crefrange| (depending on the capitalisation you
% require), which take the beginning and end of the range as
% arguments:
% \begin{quote}
%   |\crefrange{|\meta{label1}|}{|\meta{label2}|}|
% \end{quote}
%
% \DescribeMacro\cref
% \DescribeMacro\Cref
% To typeset multiple references, simply list the lables inside the
% |\cref| or |\Cref| command, separated by commas (you are not allowed
% to use commas in label names when using \package{cleveref}):
% \begin{quote}
%   |\cref{|\meta{label1}|,|\meta{label2}|,|\meta{label3}|,...}|
% \end{quote}
% The references will be typeset in the order in which they appear in
% the list, and sequences of consecutive references will be collapsed
% to a reference range. It is up to you to put the labels in the order
% you require. This is especially significant if the labels refer to
% different types of object, or to consecutive sequences of
% labels. For example, if |eq*| are equation labels, and |thm*| are
% theorem labels,
% \begin{quote}
%   |\cref{eq1,eq2,eq3,thm1,thm2}|
% \end{quote}
% will be typeset as
% \begin{quote}
%   eqs.~(1)--(3), and theorems~1 and~2
% \end{quote}
% whereas
% \begin{quote}
%   |\cref{eq1,eq2,thm1,thm2,eq3}|
% \end{quote}
% will be typeset as
% \begin{quote}
%   eqs.~(1) and~(2), theorems~1 and~2, and eq.~(3)
% \end{quote}
% (assuming you haven't customised the reference formats).
%
% To prevent a sequence of consecutive references from being collapsed
% to a reference range, you can separate the references in the list by
% one or more empty references, at the point at which you want to
% prevent collapsing. For example,
% \begin{quote}
%   |\cref{eq1,eq2,eq3,,eq4}|
% \end{quote}
% will be typeset as
% \begin{quote}
%   eqs.~(1)--(3) and (4)
% \end{quote}
% or
% \begin{quote}
%   |\cref{eq1,eq2,,eq3,eq4,eq5,,eq6,eq7,eq8}|
% \end{quote}
% will be typeset as
% \begin{quote}
%   eqs.~(1), (2), (3)--(5) and (6)--(7)
% \end{quote}
% You can safely put an empty reference between references that would
% never be collapsed anyway; it will simply be ignored.
%
% \DescribeMacro{\cref*}
% \DescribeMacro{\Cref*}
% \DescribeMacro{\crefrange*}
% \DescribeMacro{\Crefrange*}
% When \package{cleveref} is used along with the \package{hyperref}
% package (see \cref{sec:customising,sec:other_packages}), additional
% starred variants of all the referencing commands are available. The
% standard referencing commands will make references into hyper-links;
% the starred variants prevent this, producing the same typeset text
% but without creating hyper-links.
%
%
% \section{Customising the Reference Format}
% \label{sec:customising}
% The \package{cleveref} package allows you to take full control of
% the typesetting of references. Defaults appropriate for English
% documents are provided for the standard label types\footnote{For any
%   pedantic classics scholars out there: ``lemmas'' is recognised as
%   a valid plural form of ``lemma'' in all current versions of the
%   Oxford English Dictionary. ``Lemmata'' was last heard in a
%   mathematical debate that took place in a pub just around the
%   corner from Hadrian's wall\dots a few years before the Romans
%   pulled out of Britain. \package{cleveref} might have ``clever'' in
%   its name, but even that doesn't make it pretentious enough to use
%   ``lemmata''.}. But if you don't like them, or are writing in a
% different language, or you need to cross-reference something for
% which no default format is defined, then you can take charge and
% define your own formats.
%
% If \package{cleveref} encounters a reference to a type it does not
% know, it will produce a ``reference type undefined'' warning, and
% typeset the reference as
% \begin{quote}
%   {\bf ??}~|\ref{|\meta{label}|}|
% \end{quote}
% i.e.\ it will typeset the reference as the label counter preceded by
% a double question mark. The error message indicates the name of the
% unknown reference type, which you will then probably want to
% define. (References to undefined labels still produce a ``reference
% undefined'' warning and are typeset as a double question mark, as
% usual.)
%
% The reference formats are usually constructed out of components: the
% reference name (different for each type of reference), the label
% formats, and the conjunctions used in reference ranges and lists of
% multiple references. There are three levels of customisation: you
% can customise the components globally, individually for each
% reference type, or you can take full control and ignore the
% components entirely.
%
%
% \subsection{Global Customisation}
% \label{sec:global_customisation}
% \DescribeMacro\crefdefaultlabelformat
% The format for the label counter itself can be customised globally
% using
% \begin{quote}
%   |\crefdefaultlabelformat{|\meta{format}|}|
% \end{quote}
% The \meta{format} argument can be any valid \LaTeX{} code, though
% you will need to |\protect| fragile commands. It can (and almost
% certainly should!) contain three arguments, |#1|, |#2| and |#3|. The
% first argument is the formatted version of the label counter (e.g.\
% |\theequation|). The other two are used to mark the beginning and
% end of the part of the reference that should form the hyper-link
% when the \package{hyperref} package is used (see
% \cref{sec:other_packages}). The hyper-link arguments |#2| and |#3|
% \emph{must} appear in that order. (Leaving them out completely will
% not cause an error, but in that case no hyper-link will be created
% when \package{hyperref} is used, and there are better ways to
% achieve this. See \cref{sec:typesetting,sec:other_packages}.)
%
% \DescribeMacro\crefrangeconjunction
% The conjunction used in a reference range can be customised by
% redefining |\crefrangeconjunction|:
% \begin{quote}
%   |\renewcommand{\crefrangeconjunction}{|\meta{conjunction}|}|
% \end{quote}
% It does not have to be an actual conjunction in the linguistic
% sense, e.g.\ it is perfectly reasonable to define it to be an emdash
% ``|--|''. |\crefrangeconjunction| is used directly between the start
% and end references in a reference range, without any additional
% space surrounding it, e.g.\ |\crefrange{eq1}{eq2}| is typeset as
% \begin{quote}
%   |eqs.~(\ref{eq1})\crefrangeconjunction(\ref{eq2})|
% \end{quote}
% (assuming the default equation format hasn't been customised). So
% you may or may not want to include surrounding space, depending on
% the formatting you desire. For example,
% \begin{quote}
%   |\renewcommand{\crefrangeconjunction}{ and~}|
% \end{quote}
% does require surrounding space, whereas
% \begin{quote}
%   |\renewcommand{\crefrangeconjunction}{--}|
% \end{quote}
% does not.
%
% \DescribeMacro\crefpairconjunction
% \DescribeMacro\crefmiddleconjunction
% \DescribeMacro\creflastconjunction
% The conjunctions used in lists of references can be customised by
% redefining |\crefpairconjunction|, |\crefmiddleconjunction| and
% |\creflastconjunction|:
% \begin{quote}
%   |\renewcommand{\crefpairconjunction}{|\meta{conjunction}|}|\\
%   |\renewcommand{\crefmiddleconjunction}{|\meta{conjunction}|}|\\
%   |\renewcommand{\creflastconjunction}{|\meta{conjunction}|}|
% \end{quote}
% |\crefpairconjunction| is used when there are only two references in
% the list, |\creflastconjunction| is used between the penultimate and
% final reference in a list of more than two, and
% |\crefmiddleconjunction| is used between all the others. Again, they
% do not have to be conjunctions in the linguistic sense, and the same
% comments about surrounding space apply as in the case of
% |\crefrangeconjunction|. For example, the default definition of
% |\crefmiddleconjunction| is:
% \begin{quote}
%   |\renewcommand{\crefmiddleconjunction}{, }|
% \end{quote}
%
% \DescribeMacro\crefpairgroupconjunction
% \DescribeMacro\crefmiddlegroupconjunction
% \DescribeMacro\creflastgroupconjunction
% By default, the conjunctions used to separate sub-lists of different
% reference types in a multi-reference are identical to those used to
% separate references of the same type\footnote{More accurately,
%   redefining $\backslash$crefpairconjunction etc.\ automatically
%   redefines $\backslash$crefpairgroupconjunction etc.\ so that they
%   match. If you \emph{don't} redefine anything, the default
%   definition of $\backslash$creflastgroupconjunction has an
%   additional comma lacking in $\backslash$creflastconjunction.}. You
% can override this by redefining |\crefpairgroupconjunction|,
% |\crefmiddlegroupconjunction| and |\creflastgroupconjunction|.
%
% For example, if |eq*|, |thm*| and |fig*| are respectively equation,
% theorem and figure labels,
% \begin{quote}
%   |\cref{eq1,eq2,eq3,thm1,thm2,fig1,thm3}|
% \end{quote}
% is typeset as
% \begin{quote}
%   eqs.~(1)|\crefrangeconjunction|(3)|\crefmiddlegroupconjunction|%\\
%   theorems~1|\crefpairconjunction|2|\crefmiddlegroupconjunction|%\\
%   fig.~1|\creflastgroupconjunction|theorem~3
% \end{quote}
%
%
% \subsection{Customising Individual Reference Types}
% \DescribeMacro\crefname
% \DescribeMacro\Crefname
% The reference name for a given reference type is customised using
% the |\crefname| and |\Crefname| commands:
% \begin{quote}
%   |\crefname{|\meta{type}|}{|\meta{singular}|}{|\meta{plural}|}|\\
%   |\Crefname{|\meta{type}|}{|\meta{singular}|}{|\meta{plural}|}|
% \end{quote}
% used by the |\cref| and |\Cref| commands, respectively. You must
% supply both \meta{singular} and \meta{plural} forms of the name. If
% the corresponding |\Crefname| is undefined when |\crefname| is
% called, it will define |\Crefname| to be a capitalised version of
% |\crefname|, using |\MakeUppercase|. Conversely, if the
% corresponding |\crefname| is undefined when |\Crefname| is called,
% it will define |\Crefname| to be a lower-case version of
% |\Crefname|, using |\MakeLowercase|. Obviously, this will only work
% properly if the names begin with a letter. If the first letter is a
% special character construct, such as an accented character, you will
% need to surround it by braces. If the first thing in the name is
% \emph{not} a letter at all (e.g.\ if it is a \LaTeX{} command), you
% \emph{must} define both capitalisation variants
% explicitly. Otherwise you will get strange and fatal errors when
% processing the document.
%
% The reference \meta{type} is usually the name of the counter for the
% environment (equation, chapter, section, etc.). Currently, the only
% exceptions are appendices, for which \meta{type} should be
% ``appendix'' (instead of chapter or section), and theorem-like
% environments when the \package{ntheorem} package is loaded, for
% which \meta{type} should instead be the environment name (lemma,
% corollary, definition, etc.). \package{ntheorem} provides extra
% information about the environment when different theorem-like
% environments use a common counter, which \package{cleveref} makes
% use of to distinguish between them automatically.
%
% \DescribeMacro\creflabelformat
% You may want the label format for a particular reference type to
% differ from the global format set by |\crefdefaultlabelformat| (see
% \ref{sec:global_customisation}. You can do this using
% \begin{quote}
%   |\creflabelformat{|\meta{type}|}{|\meta{format}|}|
% \end{quote}
% The \meta{type} argument is the reference type to customise, and the
% \meta{format} argument defines the label format for references of
% that type. As in the case of\linebreak[4] |\crefdefaultlabelformat|,
% the latter should contain the three arguments |#1|, |#2| and |#3|,
% the first being the formatted version of the label counter, the
% others determining the beginning and end of the portion that becomes
% a hyper-link when the \package{hyperref} package is loaded (see
% \cref{sec:other_packages}). |#2| and |#3| must appear in that order.
%
% \DescribeMacro\crefrangelabelformat 
% Normally, the start and end references in a reference range are
% typeset using using the usual label format (as defined by
% |\crefdefaultlabelformat| or |\creflabelformat|) separated by
% |\crefrangeconjunction| (\cref{sec:global_customisation}). You can
% override this for a given reference type using
% \begin{quote}
%   |\crefrangelabelformat{|\meta{type}|}{|\meta{format}|}|
% \end{quote}
% The \meta{format} argument should contain six arguments: |#1|, |#2|,
% |#3|, |#4|, |#5|, |#6|. The first two (|#1| and |#2|) are the
% formatted versions of the two label counters defining the reference
% range. The next two (|#3| and |#4|) denote the beginning and end of
% the hyper-link for the first reference, the final two (|#5| and
% |#6|) the hyper-link for the second reference. The hyper-link
% arguments must appear in order. For example, the default format for
% equations is defined as:
% \begin{quote}
%   |\crefrangelabelformat{equation}{(#3#1#4)--(#5#2#6)}|
% \end{quote}
% 
%
%
% \subsection{Taking Full Control}
% If you need more precise control over the reference format than is
% possible by customising the individual components, then you can take
% full control of the format for any given type, overriding the
% component-derived format entirely. The formats for single
% references, reference ranges and multi-references are customised
% separately. If you only customise some of these, the other formats
% will still be constructed from components, as usual.
%
% \subsubsection{Single References}
% \label{sec:ref_format}
% \DescribeMacro\crefformat
% \DescribeMacro\Crefformat
% Reference formats for \emph{single} references are defined or
% redefined using the\linebreak[4] |\crefformat| and |\Crefformat|
% commands, which are used by the |\cref| and |\Cref| commands
% respectively. These take two arguments: the reference type, and the
% formatting code:
% \begin{quote}
%   |\crefformat{|\meta{type}|}{|\meta{format}|}|\\
%   |\Crefformat{|\meta{type}|}{|\meta{format}|}|
% \end{quote}
% The \meta{type} is usually the name of the counter, except for
% theorem-like environments when \package{ntheorem} is loaded, in
% which case it is the environment name,and appendices, when it is
% ``appendix''.
%
% As in the case of the |\crefname| and |\Crefname| commands, if the
% corresponding |\Crefformat| is undefined when |\crefformat| is
% called, it will define the |\Crefformat| to produce a capitalised
% version of |\crefformat|, using |\MakeUppercase|. Conversely, if the
% corresponding |\crefformat| is undefined whe |\Crefformat| is
% called, if will define the |\Crefformat| to produce a lower-case
% version of |\Crefformat|, using |\MakeLowercase|. Obviosly, this
% will only work properly if the format starts with a letter, and
% letter constructs (such as accented letter constructs) must be
% surrounded by braces (see \cref{sec:global_customisation}).
%
% The \meta{format} argument can be any valid \LaTeX{} code, though
% you will need to |\protect| fragile commands. It should contain
% three arguments, |#1|, |#2| and |#3|. The first argument is the
% formatted version of the label counter (e.g.\ |\theequation|). The
% other two are used to mark the beginning and end of the part of the
% reference that forms the hyper-link when the \package{hyperref}
% package is used, and must appear in that order (see
% \cref{sec:other_packages}).
%
% As an example,
% \begin{quote}
%   |\crefformat{equation}{Eq.~(#2#1#3)}|
% \end{quote}
% will typeset equation references as
% \begin{quote}
%   Eq.~(\meta{counter})
% \end{quote}
% with the counter (excluding the brackets) forming the hyper-link.
%
% Note that the hyper-link arguments are \emph{not} letters, so if
% |#2| appears at the beginning of \meta{format}, \package{cleveref}
% will not be able to automatically define the other capitalisation
% variant automatically using |\MakeUppercase| or |\MakeLowercase|. In
% this case, you will have to define both variants separately. For
% example, if you wanted to the ``Eq.'' to be part of the hyper-link,
% you would have to define:
% \begin{quote}
%   |\crefformat{equation}{#2eq.~(#1)#3}|\\
%   |\Crefformat{equation}{#2Eq.~(#1)#3}|
% \end{quote}
%
%
% \subsubsection{Reference Ranges}
% \label{sec:ref_range_format}
% \DescribeMacro\crefrangeformat
% \DescribeMacro\Crefrangeformat
% The format for reference ranges is defined by |\crefrangeformat|
% and\linebreak[4] |\Crefrangeformat|. Like |\creformat| and
% |\Crefformat|, the commands take two arguments: the reference type,
% and the formatting code.
% \begin{quote}
%   |\crefrangeformat{|\meta{type}|}{|\meta{format}|}|\\
%   |\Crefrangeformat{|\meta{type}|}{|\meta{format}|}|
% \end{quote}
% The same comments apply as in the case of single references: the
% \meta{type} is usually the name of the counter, except for
% theorem-like environments when \package{ntheorem} is loaded, and if
% the other-capitalisation variant is not already defined, it will be
% defined automatically.
%
% The \meta{format} argument can again be any valid \LaTeX{} code,
% with fragile commands |\protect|ed. However, this time it should
% contain \emph{six} arguments, |#1|--|#6|. The first two (|#1| and
% |#2|) are the formatted versions of the label counters, the next two
% (|#3| and |#4|) are used to mark the beginning and end of the
% hyper-link for the first reference (|#1|), and the final two (|#5|
% and |#6|) mark the beginning and end of the second reference's
% hyper-link.
%
% As an example, the default
% \begin{quote}
%   |\crefrangeformat{equation}{eqs.~(#3#1#4)--(#5#2#6)}|
% \end{quote}
% will typeset equation reference ranges as
% \begin{quote}
%   eqs.~(\meta{counter1})--(\meta{counter2})
% \end{quote}
% with the counters (excluding the brackets) forming the hyper-links.
%
%
% \subsubsection{Multiple References}
% \label{sec:multiple_ref_format}
% \DescribeMacro\crefmultiformat
% \DescribeMacro\Crefmultiformat
% \DescribeMacro\crefrangemultiformat
% \DescribeMacro\Crefrangemultiformat
% The format for multiple references is defined by |\crefmultiformat|
% and\linebreak[4] |\Crefmultiformat|, and that of reference ranges
% within multiple references by |\crefrangemultiformat| and
% |\Crefrangemultiformat|. Multi-references also require \emph{all}
% the other reference formats to be defined (see
% \cref{sec:ref_format,sec:ref_range_format}), including the single
% reference range formats, even if you never use the |\crefrange| and
% |\Crefrange| commands.
%
% The commands all take five arguments: the reference type, the format
% for the first reference in a list, the format for the second
% reference in a list of two, the format for the middle references in
% a list of more than two, and the format for the last reference in a
% list of more than two.
% \begin{quote}
%   |\crefmultiformat{|\meta{type}|}{|\meta{first}|}{|\meta{second}|}{|\meta{middle}|}{|\meta{last}|}|\\
%   |\Crefmultiformat{|\meta{type}|}{|\meta{first}|}{|\meta{second}|}{|\meta{middle}|}{|\meta{last}|}|\\
%   |\crefrangemultiformat{|\meta{type}|}{|\meta{first}|}{|\meta{second}|}{|\meta{middle}|}{|\meta{last}|}|\\
%   |\Crefrangemultiformat{|\meta{type}|}{|\meta{first}|}{|\meta{second}|}{|\meta{middle}|}{|\meta{last}|}|
% \end{quote}
%
% The \meta{type} is, as ever, the counter name, or environment name
% in the case of \package{ntheorem} theorem-like environments. The
% same considerations apply to the formatting arguments \meta{first},
% \meta{second}, \meta{middle} and \meta{last} as for the
% \meta{format} argument of |\crefformat| or |\crefrangeformat|,
% including the meaning of the arguments that should appear in the
% formatting code (|#1|, |#2| and |#3| for |\crefmultiformat| and
% |\Crefmultiformat|, |#1|--|#6| for |\crefmultiformat| and
% |\Crefmultiformat|). However, when the corresponding
% other-capitalisation variant is automatically defined, only the
% first letter of the \meta{first} argument is upper- or lower-cased;
% the other arguments are defined to be identical for both variants.
%
% Be careful to get the spaces at the beginning and end of the
% formatting code correct: the \meta{first} and {|\meta{second}|}, or
% \meta{first}, \meta{middle} and \meta{last}, \LaTeX{} code is
% typeset one after another in a multi-reference, with no space
% separating them. You may or may not want spaces at the beginning and
% end of the formatting code, depending on the formatting you
% desire. For example, in the default equation format
% \begin{quote}
%   |\crefmultiformat{equation}%|\\
%   |{eqs.~(#2#1#3)}{ and~(#2#1#3)}{, (#2#1#3)}{ and~(#2#1#3)}|
% \end{quote}
% the \meta{middle} argument should \emph{not} have a space at the
% beginning, whereas the \meta{second} and \meta{last} arguments
% \emph{should} have a space.
%
%
%
% \section{Overriding the Reference Type}
% As described previously, a label's reference type is usually
% determined by its counter, or in the case of \package{ntheorem}
% theorem-like environments by the environment name. Occasionally, you
% may want to override the reference type for a particular label. You
% can do this by supplying the desired type as an optional argument to
% the |\label| command:
% \begin{quote}
%   |\label[|\meta{type}|]{|\meta{label}|}|
% \end{quote}
%
% One circumstance in which is useful is when you want to define a
% special reference format for certain labels of a given type. By
% specifying a type that doesn't already exist in the |\label|'s
% optional argument, you can then define the reference format for that
% new type in whatever way you like, without affecting other
% references of the same type. For example, if a particular equation
% contains multiple expressions and you want it to always be referred
% to in the plural, you could use:
% \begin{quote}
%   |\crefname{pluralequation}{eqs.}{eqs.}|\\
%   \dots\\
%   |\label[pluralequation]{eq1}|
% \end{quote}
% You can of course reuse this format for other plural equations, too.
%
% If you need to do this frequently, it can become tedious specifying
% the label explicitly each time. An alternative is to use the
% \package{aliascnt} package. This lets you define one counter to be
% an alias for another, so that effectively the same counter has two
% names. Since \package{cleveref} determines the label type from the
% counter name, the two counter aliases can have different reference
% formats whilst really being the same counter. You have to somehow
% arrange for the correct counter alias to be used depending on which
% reference format you want (probably by defining two variants of the
% environment in question). But the effort involved might be worth the
% convenience of not having to remember to pass an explicit optional
% argument to a large number of labels.
%
% You can use this trick to get different reference formats for
% different theorem-like environments, \emph{without} using the
% \package{ntheorem} package\footnote{This trick seems to belong to
%   \LaTeX{} mythology, and certainly isn't my own idea! I haven't
%   been able to definitively track down who originally came up with
%   it.}. For example,
% \begin{quote}
%   |\usepackage{aliascnt}|\\
%   |\usepackage{cleveref}|\\
%   |\newaliascnt{lemma}{theorem}|\\
%   |\newtheorem{lemma}[lemma]{Lemma}|\\
%   |\aliascntresetthe{lemma}|\\
%   |\crefname{lemma}{lemma}{lemmas}|
% \end{quote}
%
%
%
% \section{Poor Man's \package{cleveref}}
% Sometimes you may need to send your \LaTeX{} source to someone who
% can't install the \package{cleveref} package themselves. For
% example, many academic journals accept papers in \LaTeX{} format,
% but only support a small subset of the packages available on
% CTAN. The \option{poorman} option was designed specifically to help
% in this situation.
%
% When the \option{poorman} option is supplied, your document will be
% processed as normal. But in addition, a {\tt sed} script will
% automatically be written, containing rules for replacing all the
% \cmd{\cref} commands with the \LaTeX{} code that they would produce,
% and using the standard \cmd{\ref} command to produce the
% cross-references themselves. I.e.\ the script rewrites your document
% as you would have done if you had had to do it manually!
%
% The advantage, of course, is that you \emph{don't} have to do it
% manually. Instead, you can use all the features of
% \package{cleveref}, and once you've created a version of your
% document that you want to send elsewere, you can process it through
% the {\tt sed} script to completely remove the \package{cleveref}
% dependency. The recipient won't even realise you used
% \package{cleveref}!
%
% The {\tt sed} script is written to the same directory as the (main)
% \LaTeX{} source file, and given the same name as that source file
% but with the extension \file{.sed}. To process your document through
% the script, all you need to do is run the following from your shell:
% \begin{quote}
%   sed -f \meta{name}\file{.sed} \
%   \meta{name}\file{.tex} \textgreater \meta{newname}\file{.tex}
% \end{quote}
% where \meta{name} is the name of the file containing your \LaTeX{}
% source file minus the \file{.tex} extension, and \meta{newname} is
% whatever you want to call the new version. \emph{Do not} make
% \meta{newname} the same as \meta{name}. (It's in any case wise to
% keep the original \LaTeX{} source file containing the
% \package{cleveref} commands, in case you need to produce an updated
% version of your document in the future. Think of the
% \meta{newname}\file{.tex} file as being rather like a \file{DVI}
% file: something you can always reproduce from the original source.)
% If your document is composed of a number of separate \LaTeX{} source
% files, combined with |\include| commands, only one {\tt sed} script
% will be generated, but you will need to run \emph {each} source file
% through that \emph{same} script (and probably modify the |\include|
% commands to match the new file names).
%
%
%
% \section{Interaction with Other Packages}
% \label{sec:other_packages}
% The \package{cleveref} package \emph{must} be loaded \emph{after}
% all other packages that don't specifically support it\footnote{At
% the time of writing, I'm not aware of any that do.}, i.e.\ the
% \begin{quote}
%   |\usepackage{cleveref}|
% \end{quote}
% line should always be the last |\usepackage| command in your
% document's preamble.
%
% Since \package{cleveref} redefines many internal commands involved
% in \LaTeX{}'s referencing system, it can interact badly with other
% packages that do the same. \package{varioref}'s enhanced referencing
% features (the ones you make use of by via the |\labelformat|
% command), the \package{fancyref} package, and \package{ntheorem}'s
% \option{thref} option are incompatible with
% \package{cleveref}. However, since \package{cleveref} implements an
% enhanced version of these packages' features, this is not really a
% problem. For example, if you have a pre-existing document that uses
% \package{ntheorem}'s |\thref| command, you can simply redefine it to
% call |\cref| instead:
% \begin{quote}
%   |\renewcommand{\thref}{\cref}|
% \end{quote}
% In fact, \package{cleveref} does this for you automatically if
% \package{ntheorem} was loaded with the \option{thref} option. Note
% that you can still use the other features of \package{varioref} and
% \package{ntheorem} whilst using \package{cleveref}, as long as
% \package{cleveref} is loaded \emph{last}.
%
% Other packages which alter the \LaTeX{} referencing system are
% unlikely to work properly with \package{cleveref}.
%
%
%
% \section{Known Bugs}
% In no particular order.
% \begin{itemize}
% \item \package{cleveref} will not work properly with the standard
%   \LaTeX{} |eqnarray| environment. The |eqnarray| environment is
%   poorly implemented, making it somewhat difficult to get it to work
%   properly with \package{cleveref}. You're better off using the
%   \package{amsmath} replacements in any case, such as |gather|,
%   |align|, |multine| and |split|, which \emph{do} work properly with
%   \package{cleveref}. (See
%   \url{http://www.tug.org/pracjourn/2006-4/madsen/}).
% \item The \option{poorman} sed script always uses |\ref| rather than
%   |\ref*|, even if the command it's replacing is a starred variant
%   |\cref*|, |\crefrange*| etc. (This should be fixed in a future
%   version.)
% \item \package{cleveref} provides no \package{babel} support. (This
%   should be fixed in a future version.)
% \item \package{cleveref} breaks \package{hyperref}'s
%   \option{backref} option, and probably also the \package{backref}
%   package when used by itself. (This should be fixed in a future
%   version.)
% \item \package{cleveref} assumes that counters are only ever reset
%   by the standard sectioning commands (chapter, section, etc.). If
%   this is not the case, the automatic collapsing of consecutive
%   references into a reference range may be incorrect.
% \item The \option{poorman} sed script uses |\ref| when replacing
%   \package{cleveref} cross-references, but this loses any custom
%   \package{cleveref} hyper-link formatting you might have
%   defined. The philosophy behind the \option{poorman} option is to
%   replace \package{cleveref}'s enhanced cross-referencing with
%   standard \LaTeX{} cross-reference commands that are guaranteed to
%   work with any standard \LaTeX{} installation. Although it would be
%   simple to fix this ``bug'', it's not obvious how to do it without
%   compromising this underlying philosophy, i.e.\ without using
%   \LaTeX{} code that is unlikely to be supported by journals.
% \end{itemize}
%
%
%
% \section{Possible Future Improvements}
% In no particular order.
% \begin{itemize}
% \item Allow reference-range collapsing to be disabled entirely via a
%   package option
% \item Add an option to automatically sort references in a
%   multi-reference before typesetting them.
% \item Enhance the \option{poorman} option to allow a choice of
%   script language rather than just sed (e.g.\ awk, perl, \dots?).
% \end{itemize}
%
%
%
% \StopEventually{\PrintIndex}
%
%
%
% \section{Implementation}
% Essentially, the core of the implementation consists of causing an
% extra piece of information -- the label ``type'' -- to be written to
% the aux file, and defining \cmd{\cref} commands which use this extra
% information to typeset the reference.
%
% The least invasive implementation seems to be that used by the
% \package{varioref} package. Namely, to redefine the
% \cmd{\refstepcounter} command so that the \cmd{\@currentlabel}
% macro, which usually just cotains the typeset version of the
% counter, now contains the additional type information. (In fact, we
% write three extra pieces of information: the type, the counter value
% itself, and the formatted version of the counter that causes the
% label's counter to be reset, which we call the ``prefix'' from now
% on.) \cmd{\@currentlabel} eventually gets written to the aux file as
% an argument to \cmd{\newlabel} by the usual \LaTeX{}
% mechanisms. This involves less hacking to get everything else
% working again, since very few macros other than \cmd{\ref} use this
% particular \cmd{\newlabel} argument (nor are other packages likely
% to, given that \package{varioref} is a \texttt{required} \LaTeX{}
% package).
%
%
% \subsection{Redefinitions of \LaTeX{} kernel macros}
% We store the original \cmd{\refstepcounter} in
% \cmd{\cref@old@refstepcounter}, then redefine \cmd{\refstepcounter} so
% that it first calls the old version and then adds the extra
% information to \cmd{\@currentlabel}. The new \cmd{\@refstepcounter}
% can take an optional argument, which overrides using the counter
% name as the ``type'' and instead uses whatever is supplied.
%    \begin{macrocode}
\let\cref@old@refstepcounter\refstepcounter
\def\refstepcounter{%
  \@ifnextchar[{\refstepcounter@optarg}{\refstepcounter@noarg}%]
}
\def\refstepcounter@noarg#1{%
  \cref@old@refstepcounter{#1}%
  \cref@resetby{#1}{\@result}%
  \ifx\@result\relax\def\@result{}%
  \else\edef\@result{\csname the\@result\endcsname}\fi%
  \protected@edef\@currentlabel{%
    [#1][\arabic{#1}][\@result]\@currentlabel}}
\def\refstepcounter@optarg[#1]#2{%
  \cref@old@refstepcounter{#2}%
  \cref@resetby{#2}{\@result}%
  \ifx\@result\relax\def\@result{}%
  \else\edef\@result{\csname the\@result\endcsname}\fi%
  \protected@edef\@currentlabel{%
    [#1][\arabic{#2}][\@result]\@currentlabel}}
%    \end{macrocode}
% 
% We redefine the \cmd{\label} command to allow it to take an optional
% argument that overrides the default reference type in
% \cmd{\@currentlabel}. We have to postpone this redefinition of
% \cmd{\label} until the beginning of the document because some other
% packages do too, and we need to override their redefinitions.
%    \begin{macrocode}
\def\cref@override@label@type[#1][#2][#3]#4\@nil#5{[#5][#2][#3]#4}
\AtBeginDocument{%
  \let\cref@old@label\label
  \def\label{\@ifnextchar[\label@optarg\cref@old@label}%]
  \def\label@optarg[#1]{%
    \protected@edef\@currentlabel{%
      \expandafter\cref@override@label@type\@currentlabel\@nil{#1}}%
    \cref@old@label}%
%    \end{macrocode}
% 
% The \package{amsmath} package redefines the \cmd{\label} command
% within equation environments, so if it is loaded we have to extend
% the behaviour to support the optional argument. With
% \package{amsmath}, the original \cmd{\label} command in stored in
% \cmd{ltx@label}, and \cmd{\label@in@display} replaces \cmd{label}
% inside equations. \cmd{\label@in@display} just saves the label for
% later, and defining it is left until the end of the equation, when
% \cmd{\ltx@label} is finally called.
%
% To allow \cmd{label} within equations to support an optional
% argument, we first store the original \cmd{\label@in@display} and
% the new \cmd{\label} macro we defined above (since \cmd{\label} will
% be clobbered inside equations). Then we redefine
% \cmd{label@in@display} so that it wraps all its arguments, including
% any optional argument, in an extra set of braces \{\}. These are
% stripped away again by \cmd{\ltx@label} before calling the
% \cmd{\label} macro we defined above (saved in \cmd{\cref@label}).
%    \begin{macrocode}
  \@ifpackageloaded{amsmath}{%
    \let\cref@label\label%
    \let\cref@old@label@in@display\label@in@display%
    \def\label@in@display{%
      \@ifnextchar[\label@in@display@optarg\label@in@display@noarg}%]
    \def\label@in@display@noarg#1{\cref@old@label@in@display{{#1}}}
    \def\label@in@display@optarg[#1]#2{%
      \cref@old@label@in@display{[#1]{#2}}}
    \def\ltx@label#1{\cref@label#1}%
  }{}%
}
%    \end{macrocode}
%
% The standard \cmd{\ref} macro spits out whatever was in
% \cmd{\@currentlabel} when the label was written to the aux file, but
% this now contains the additional type information which we don't
% want. Therefore, we redefine \cmd{\cref} to recover the original
% behaviour. We have to defer redefinition of \cmd{\ref} till the
% beginning of the document, in case other packages (such as
% \package{ntheorem}) modify it after \package{cleveref} is
% loaded. For some reason, \cmd{\DeclareRobustCommand} doesn't work
% here, so we make it robust manually.
%    \begin{macrocode}
\def\cref@reflabel#1#2{\@result}
\AtBeginDocument{%
 \expandafter\def\csname ref \endcsname#1{%
   \expandafter\ifx\csname r@#1\endcsname\relax%
     \let\@result\relax%
   \else%
     \cref@getlabel{#1}{\@result}%
   \fi%
   \expandafter\@setref\csname r@#1\endcsname{\cref@reflabel}{#1}}%
 \def\ref{\expandafter\protect\csname ref \endcsname}%
}
%    \end{macrocode}
%
% The \cmd{\appendix} command causes the top-level sectioning commands
% (\cmd{\chapter} or \cmd{\section}, depending on the document class)
% to produce appendices instead. Since we want to be able to format
% references to appendices separately from references to normal
% top-level sections, we add to the tasks that \cmd{\appendix} does:
% it redefines \cmd{\refstepcounter@noarg} to exceptionally override
% the label type for chapters or sections, as appropriate, setting it
% to ``appendix'' instead.
%    \begin{macrocode}
\let\cref@old@appendix\appendix
\renewcommand\appendix{%
  \cref@old@appendix%
  \@ifundefined{chapter}{%
    \def\refstepcounter@noarg##1{%
      \cref@old@refstepcounter{##1}%
      \cref@resetby{##1}{\@result}%
      \ifx\@result\relax\def\@result{}%
      \else\edef\@result{\csname the\@result\endcsname}\fi%
      \def\@tmpa{##1}
      \def\@tmpb{section}%
      \ifx\@tmpa\@tmpb%
        \protected@edef\@currentlabel{%
          [appendix][\arabic{##1}][\@result]\@currentlabel}%
      \else        
        \protected@edef\@currentlabel{%
          [##1][\arabic{##1}][\@result]\@currentlabel}
      \fi}
  }{%
    \def\refstepcounter@noarg##1{%
      \cref@old@refstepcounter{##1}%
      \cref@resetby{##1}{\@result}%
      \ifx\@result\relax\def\@result{}%
      \else\edef\@result{\csname the\@result\endcsname}\fi%
      \def\@tmpa{##1}
      \def\@tmpb{chapter}%
      \ifx\@tmpa\@tmpb%
        \protected@edef\@currentlabel{%
          [appendix][\arabic{##1}][\@result]\@currentlabel}%
      \else        
        \protected@edef\@currentlabel{%
          [##1][\arabic{##1}][\@result]\@currentlabel}
      \fi}%
  }%
}
%    \end{macrocode}
%
%
% \subsection{Utility Macros}
% Define some utility macros for extracting label, type, and counter
% information from the contents of \cmd{\@currentlabel}, as written to
% the aux file and stored in \cmd{\r@\meta{label}} when this is
% re-read on the next pass. Some other packages commandeer the
% referencing system to write label information to the aux file for
% other purposes, and probably use \cmd{\ref} to recover it later. We
% still want them to work, so our utility macros must cope with the
% type information being absent. However, since we need them to be
% fully expandable in various places, and \cmd{\@ifnextchar} is
% definitely \emph{not} fully expandable, we use the work-around of
% having the macros store their result in another macro, whose name is
% passed as the second argument. This other macro \emph{will} then be
% fully expandable, and can be used e.g.\ inside an \cmd{\edef} or
% \cmd{\csname}\ldots\cmd{\endcsname}.
%    \begin{macrocode}
\def\cref@getlabel#1#2{%
  \expandafter\let\expandafter\@tmpa\csname r@#1\endcsname%
  \edef\@tmpa{\expandafter\@firstoftwo\@tmpa}%
  \expandafter\@cref@getlabel\@tmpa\@nil#2}
\def\@cref@getlabel{\@ifnextchar[%]
  \@@cref@getlabel{\@@cref@getlabel[][][]}}
\def\@@cref@getlabel[#1][#2][#3]#4\@nil#5{\def#5{#4}}
\def\cref@gettype#1#2{%
  \expandafter\let\expandafter\@tmpa\csname r@#1\endcsname%
  \edef\@tmpa{\expandafter\@firstoftwo\@tmpa}%
  \expandafter\@cref@gettype\@tmpa\@nil#2}
\def\@cref@gettype{\@ifnextchar[%]
  \@@cref@gettype{\@@cref@gettype[][][]}}
\def\@@cref@gettype[#1][#2][#3]#4\@nil#5{\def#5{#1}}
\def\cref@getcounter#1#2{%
  \expandafter\let\expandafter\@tmpa\csname r@#1\endcsname%
  \edef\@tmpa{\expandafter\@firstoftwo\@tmpa}%
  \expandafter\@cref@getcounter\@tmpa\@nil#2}
\def\@cref@getcounter{\@ifnextchar[%]
  \@@cref@getcounter{\@@cref@getcounter[][][]}}
\def\@@cref@getcounter[#1][#2][#3]#4\@nil#5{\def#5{#2}}
\def\cref@getprefix#1#2{%
  \expandafter\let\expandafter\@tmpa\csname r@#1\endcsname%
  \edef\@tmpa{\expandafter\@firstoftwo\@tmpa}%
  \expandafter\@cref@getprefix\@tmpa\@nil#2}
\def\@cref@getprefix{\@ifnextchar[%]
  \@@cref@getprefix{\@@cref@getprefix[][][]}}
\def\@@cref@getprefix[#1][#2][#3]#4\@nil#5{\def#5{#3}}
%    \end{macrocode}
%
% A basic utility macro for appending tokens to a token register.
%    \begin{macrocode}
\def\cref@append@toks#1#2{\toks0={#2}%
  \edef\act{\noexpand#1={\the#1\the\toks0}}%
  \act}%
%    \end{macrocode}
%
% We treat multiple references, supplied as a comma-separated list to
% \cmd{\cref} or \cmd{\Cref}, as a stack structure. So we define some
% utility macros for manipulating stacks (\cmd{\@nil} is used as an
% end-of-stack delimeter).
%    \begin{macrocode}
\def\cref@stack@init#1{\def#1{\@nil}}
\def\cref@stack@top#1{\expandafter\cref@stack@top@aux#1}
\def\cref@stack@top@aux#1,#2\@nil{#1}
\def\cref@stack@pop#1{\expandafter\cref@stack@pop@aux#1#1}
\def\cref@stack@pop@aux#1,#2\@nil#3{\def#3{#2\@nil}}
\def\cref@stack@push#1#2{%
  \expandafter\cref@stack@push@aux\expandafter{#2}{#1}{#2}}
\def\cref@stack@push@aux#1#2#3{\def#3{#2,#1}}
\def\cref@stack@pull#1#2{\expandafter\cref@stack@pull@aux#2{#1}{#2}}
\def\cref@stack@pull@aux#1\@nil#2#3{\def#3{#1#2,\@nil}}
\newif\if@cref@stackempty
\newif\if@cref@stackfull
\def\cref@isstackempty#1{%
  \def\@tmpa{\@nil}%
  \ifx#1\@tmpa\@cref@stackemptytrue%
  \else\@cref@stackemptyfalse\fi}
\def\cref@isstackfull#1{%
  \def\@tmpa{\@nil}%
  \ifx#1\@tmpa\@cref@stackfullfalse%
  \else\@cref@stackfulltrue\fi}
%    \end{macrocode}
% 
% We need to be able to determine which counter is used to reset a
% given counter. Usually, resets are done by sectioning counters, and
% we assume that to be the case here. \cmd{\isinresetlist} searches
% through one counter's reset list, stored in
% \cmd{\cl@\meta{counter}}, to determine whether another counter
% appears there, and sets the new conditional
% appropriately. \cmd{\reset@by} searches through all the sectioning
% counters' reset lists, from lowest-level (subsubsection) to highest
% (part), checking whether the given counter is in the list, and
% returns the first sectioning counter whose list it appears (value is
% returned by defining its second argument, which should be a macro
% name).
%    \begin{macrocode}
\newif\if@cref@inresetlist
\def\cref@isinresetlist#1#2{%
  \def\@counter{#1}%
  \begingroup%
    \def\@elt##1{##1,}%
    \expandafter\ifx\csname cl@#2\endcsname\relax%
      \gdef\cref@resetstack{,\@nil}%
    \else%
      \xdef\cref@resetstack{\csname cl@#2\endcsname\noexpand\@nil}%
    \fi%
  \endgroup%
  \cref@isstackfull{\cref@resetstack}%
  \@whilesw\if@cref@stackfull\fi{%
    \edef\@nextcounter{\cref@stack@top{\cref@resetstack}}%
    \ifx\@nextcounter\@counter%
      \@cref@stackfullfalse%
    \else%
      \let\@nextcounter\relax%
      \cref@stack@pop{\cref@resetstack}%
      \cref@isstackfull{\cref@resetstack}%
    \fi}%
  \ifx\@nextcounter\relax%
    \@cref@inresetlistfalse%
  \else%
    \@cref@inresetlisttrue%
  \fi}
%
\def\cref@resetby#1#2{%
  \cref@isinresetlist{#1}{subsubsubsection}%
  \if@cref@inresetlist%
    \def#2{subsubsubsection}%
  \else%
    \cref@isinresetlist{#1}{subsubsection}%
    \if@cref@inresetlist%
      \def#2{subsubsection}%
    \else%
      \cref@isinresetlist{#1}{subsection}%
      \if@cref@inresetlist%
        \def#2{subsection}%
      \else%
        \cref@isinresetlist{#1}{section}%
        \if@cref@inresetlist%
          \def#2{section}%
        \else%
          \cref@isinresetlist{#1}{chapter}%
          \if@cref@inresetlist%
            \def#2{chapter}%
          \else%
            \cref@isinresetlist{#1}{part}%
            \if@cref@inresetlist%
              \def#2{part}%
            \else%
              \let#2\relax%
            \fi%
          \fi%
        \fi%
      \fi%
    \fi%
  \fi}
%    \end{macrocode}
%
%
% \subsection{Referencing Commands}
% Define the main referencing macros \cmd{\cref} and the
% start-of-sentence variant \cmd{\Cref}.
%    \begin{macrocode}
\DeclareRobustCommand{\cref}[1]{\@cref{cref}{#1}}
\DeclareRobustCommand{\Cref}[1]{\@cref{Cref}{#1}}
\DeclareRobustCommand{\crefrange}[2]{\@setcrefrange{#1}{#2}{cref}{}}
\DeclareRobustCommand{\Crefrange}[2]{\@setcrefrange{#1}{#2}{Cref}{}}
%    \end{macrocode}
% To save duplicating code, the referencing macros pass an argument
% determining the variant to an auxilliary macro \cmd{\@cref}, which
% does the real work. The \cmd{\@cref} macro is the behemoth at the
% heart of all the smart referencing features. It deals with grouping
% references by type, typesetting the conjunctions between groups,
% choosing the right formatting macro to use for each reference,
% and collapsing consecutive references into ranges.
%    \begin{macrocode}
\def\@cref#1#2{%
  \begingroup%
%    \end{macrocode}
% Initialise some things, and put all the references into a stack
% called \cmd{\@refstack}.
%    \begin{macrocode}
  \countdef\count@consecutive=0%
  \def\@empty{}%
  \newif\if@firstgroup%
  \newif\if@secondgroup%
  \newif\if@secondref%
  \cref@stack@init{\@refstack}%
  \cref@stack@push{#2}{\@refstack}%
  \@firstgrouptrue%
  \@secondgroupfalse%
  \cref@isstackfull{\@refstack}%
%    \end{macrocode}
% Loop until the reference stack is empty.
%    \begin{macrocode}
  \@whilesw\if@cref@stackfull\fi{%
    \cref@stack@init{\@refsubstack}%
    \edef\@nextref{\cref@stack@top{\@refstack}}%
    \expandafter\ifx\csname r@\@nextref\endcsname\relax%
      \def\@currenttype{\@undefined}%
    \else%
      \expandafter\cref@gettype\expandafter{\@nextref}{\@currenttype}%
    \fi%
    \let\@nexttype\@currenttype%
%    \end{macrocode}
%    Move references from \cmd{\@refstack} into a different stack
%    called \cmd{\@refsubstack}, until we encounter a reference that
%    has a different type to those that came before.
%    \begin{macrocode}
    \@whilesw\ifx\@nexttype\@currenttype\fi{%
      \expandafter\cref@stack@pull\expandafter%
        {\@nextref}{\@refsubstack}%
      \cref@stack@pop{\@refstack}%
      \cref@isstackempty{\@refstack}%
      \if@cref@stackempty%
        \def\@nexttype{\relax}%
      \else%
        \edef\@nextref{\cref@stack@top{\@refstack}}%
        \ifx\@nextref\@empty%
          \let\@currenttype\@nexttype%
        \else%
          \expandafter\ifx\csname r@\@nextref\endcsname\relax%
            \def\@currenttype{\@undefined}%
          \else%
            \expandafter\cref@gettype\expandafter%
              {\@nextref}{\@currenttype}%
          \fi%
        \fi%
      \fi}%
%    \end{macrocode}
%
%   Typeset appropriate conjunction between groups of reference types.
%    \begin{macrocode}
    \if@firstgroup%
      \@firstgroupfalse%
      \@secondgrouptrue%
    \else%
      \cref@isstackfull{\@refstack}%
      \if@cref@stackfull%
        \@setcref@middlegroupconjunction%
      \else%
        \if@secondgroup%
          \@setcref@pairgroupconjunction%
        \else%
          \@setcref@lastgroupconjunction%
        \fi%
      \fi%
      \@secondgroupfalse%
    \fi%
%    \end{macrocode}
%
%   Process first group of consecutive references from substack.
%    \begin{macrocode}
    \edef\@nextref{\cref@stack@top{\@refsubstack}}%
    \cref@stack@pop{\@refsubstack}%
%    \end{macrocode}
%   If the substack only contains one reference, typeset it,
%    \begin{macrocode}
    \cref@isstackempty{\@refsubstack}%
    \if@cref@stackempty%
      \expandafter\@setcref\expandafter{\@nextref}{#1}{}%
%    \end{macrocode}
%   otherwise, find end of consecutive references.
%    \begin{macrocode}
    \else%
      \edef\@beginref{\@nextref}%
      \let\@endref\relax%
      \edef\@nextref{\cref@stack@top{\@refsubstack}}%
      \count@consecutive=1%
      \expandafter\ifx\csname r@\@beginref\endcsname\relax%
        \@cref@refconsecutivefalse%
      \else%
%    \end{macrocode}
%       If next reference in substack is empty, it indicates that no
%       further collapsing should take place. Having served its
%       purpose, the empty reference and any consecutive empty
%       references are removed from the substack.
%    \begin{macrocode}
        \ifx\@nextref\@empty%
          \@cref@refconsecutivefalse%
          \@whilesw\ifx\@nextref\@empty\fi{%
            \cref@stack@pop{\@refsubstack}%
            \cref@isstackempty{\@refsubstack}%
            \if@cref@stackempty%
              \let\@nextref\relax%
            \else%
              \edef\@nextref{\cref@stack@top{\@refsubstack}}%
            \fi%
          }%
          \ifnum\count@consecutive=2%
            \edef\@endref{\@endref,}%
          \fi%
%    \end{macrocode}
%       Otherwise, test whether next reference is consecutive or not.
%    \begin{macrocode}
        \else%
          \expandafter\ifx\csname r@\@nextref\endcsname\relax%
            \@cref@refconsecutivefalse%
          \else%
            \edef\@tmpa{{\@beginref}{\@nextref}}%
            \expandafter\cref@isrefconsecutive\@tmpa%
          \fi%
        \fi%
      \fi%
      \@whilesw\if@cref@refconsecutive\fi{%
        \advance\count@consecutive 1%
        \let\@endref\@nextref%
        \cref@stack@pop{\@refsubstack}%
        \cref@isstackempty{\@refsubstack}%
        \if@cref@stackempty%
          \@cref@refconsecutivefalse%
        \else%
          \edef\@nextref{\cref@stack@top{\@refsubstack}}%
%    \end{macrocode}
%         Test whether next reference is empty;
%    \begin{macrocode}
          \ifx\@nextref\@empty%
            \@cref@refconsecutivefalse%
            \@whilesw\ifx\@nextref\@empty\fi{%
              \cref@stack@pop{\@refsubstack}%
              \cref@isstackempty{\@refsubstack}%
              \if@cref@stackempty%
                \let\@nextref\relax%
              \else%
                \edef\@nextref{\cref@stack@top{\@refsubstack}}%
              \fi%
            }%
            \ifnum\count@consecutive=2%
              \edef\@endref{\@endref,}%
            \fi%
%    \end{macrocode}
%         otherwise, test whether next reference is consecutive or not.
%    \begin{macrocode}
          \else%
            \expandafter\ifx\csname r@\@nextref\endcsname\relax%
              \@cref@refconsecutivefalse%
            \else%
              \edef\@tmpa{{\@endref}{\@nextref}}%
              \expandafter\cref@isrefconsecutive\@tmpa%
            \fi%
          \fi%
        \fi}%
%    \end{macrocode}
%     If there were no consecutive references, typeset the first
%     reference;
%    \begin{macrocode}
      \ifx\@endref\relax%
        \expandafter\@setcref\expandafter{\@beginref}{#1}{@first}%
%    \end{macrocode}
%       if there were only two consecutive references, typeset the first
%       one and return the second to the substack;
%    \begin{macrocode}
      \else%
        \ifnum\count@consecutive=2%
          \expandafter\@setcref\expandafter{\@beginref}{#1}{@first}%
          \expandafter\cref@stack@push\expandafter%
            {\@endref}{\@refsubstack}%
%    \end{macrocode}
%       otherwise, typeset a reference range.
%    \begin{macrocode}
        \else%
          \edef\@tmpa{{\@beginref}{\@endref}}%
          \if@cref@stackempty%
            \expandafter\@setcrefrange\@tmpa{#1}{}%
          \else%
            \expandafter\@setcrefrange\@tmpa{#1}{@first}%
          \fi%
        \fi%
      \fi%
%    \end{macrocode}
%
%     Process further groups of consecutive references, until substack
%     is empty.
%    \begin{macrocode}
      \@secondreftrue%
      \cref@isstackfull{\@refsubstack}%
      \@whilesw\if@cref@stackfull\fi{%
        \edef\@beginref{\cref@stack@top{\@refsubstack}}%
        \cref@stack@pop{\@refsubstack}%
        \let\@endref\relax%
%    \end{macrocode}
%       If substack only contains only one reference, typeset it,
%    \begin{macrocode}
        \cref@isstackempty{\@refsubstack}%
        \if@cref@stackempty%
          \if@secondref%
            \expandafter\@setcref\expandafter{\@beginref}{#1}{@second}%
          \else%
            \expandafter\@setcref\expandafter{\@beginref}{#1}{@last}%
          \fi%
%    \end{macrocode}
%       otherwise, find end of consecutive references.
%    \begin{macrocode}
        \else%
          \edef\@nextref{\cref@stack@top{\@refsubstack}}%
          \count@consecutive=1%
%    \end{macrocode}
%         Test whether next reference is empty;
%    \begin{macrocode}
          \ifx\@nextref\@empty%
            \@cref@refconsecutivefalse%
            \@whilesw\ifx\@nextref\@empty\fi{%
              \cref@stack@pop{\@refsubstack}%
              \cref@isstackempty{\@refsubstack}%
              \if@cref@stackempty%
                \let\@nextref\relax%
              \else%
                \edef\@nextref{\cref@stack@top{\@refsubstack}}%
              \fi%
            }%
            \ifnum\count@consecutive=2%
              \edef\@endref{\@endref,}%
            \fi%
%    \end{macrocode}
%         otherwise, test whether next reference is consecutive or not.
%    \begin{macrocode}
          \else%
            \expandafter\ifx\csname r@\@nextref\endcsname\relax%
              \@cref@refconsecutivefalse%
            \else%
              \edef\@tmpa{{\@beginref}{\@nextref}}%
              \expandafter\cref@isrefconsecutive\@tmpa%
            \fi%
          \fi%
          \@whilesw\if@cref@refconsecutive\fi{%
            \advance\count@consecutive 1%
            \let\@endref\@nextref%
            \cref@stack@pop{\@refsubstack}%
            \cref@isstackempty{\@refsubstack}%
            \if@cref@stackempty%
              \@cref@refconsecutivefalse%
            \else%
              \edef\@nextref{\cref@stack@top{\@refsubstack}}%
%    \end{macrocode}
%             Test whether next reference is empty;
%    \begin{macrocode}
              \ifx\@nextref\@empty%
              \@cref@refconsecutivefalse%
              \@whilesw\ifx\@nextref\@empty\fi{%
                \cref@stack@pop{\@refsubstack}%
                \cref@isstackempty{\@refsubstack}%
                \if@cref@stackempty%
                  \let\@nextref\relax%
                \else%
                  \edef\@nextref{\cref@stack@top{\@refsubstack}}%
                \fi%
              }%
              \ifnum\count@consecutive=2%
                \edef\@endref{\@endref,}%
              \fi%
%    \end{macrocode}
%             otherwise, test whether next reference is consecutive or not.
%    \begin{macrocode}
              \else%
                \expandafter\ifx\csname r@\@nextref\endcsname\relax%
                  \@cref@refconsecutivefalse%
                \else%
                  \edef\@tmpa{{\@endref}{\@nextref}}%
                  \expandafter\cref@isrefconsecutive\@tmpa%
                \fi%
              \fi%
            \fi}%
%    \end{macrocode}
%         If the substack is now empty, we will need to typeset an
%         ``end'' reference, otherwise we will need to typeset a
%         ``middle'' reference.
%    \begin{macrocode}
          \cref@isstackempty{\@refsubstack}%
          \if@cref@stackempty%
            \if@secondref%
              \def\@pos{@second}%
            \else%
              \def\@pos{@last}%
            \fi%
          \else%
            \def\@pos{@middle}%
          \fi%
%    \end{macrocode}
%         If there were no consecutive references, just typeset the
%         next reference;
%    \begin{macrocode}
          \ifx\@endref\relax%
            \edef\@tmpa{{\@beginref}{#1}{\@pos}}%
            \expandafter\@setcref\@tmpa%
          \else%
%    \end{macrocode}
%           if there were only two consecutive references, typeset the
%           first one, and return the second one to the substack,
%    \begin{macrocode}
            \ifnum\count@consecutive=2%
              \expandafter\@setcref\expandafter%
                {\@beginref}{#1}{@middle}%
              \expandafter\cref@stack@push\expandafter%
                {\@endref}{\@refsubstack}%
%    \end{macrocode}
%           otherwise, typeset a reference range.
%    \begin{macrocode}
            \else%
              \edef\@tmpa{{\@beginref}{\@endref}{#1}{\@pos}}%
              \expandafter\@setcrefrange\@tmpa%
            \fi%
          \fi%
        \fi%
        \@secondreffalse%
        \cref@isstackfull{\@refsubstack}%
      }% end loop over reference substack
    \fi%
    \cref@isstackfull{\@refstack}%
  }% end loop over main reference stack
  \endgroup}
%    \end{macrocode}
% 
%
% The internal \cmd{\@setcref} macro deals with actually typesetting
% the reference, by calling the appropriate type-dependent formatting
% macro defined by \cmd{\crefformat} etc.
%    \begin{macrocode}
\def\@setcref#1#2#3{%
  \expandafter\ifx\csname r@#1\endcsname\relax%
    \protect\G@refundefinedtrue%
    \nfss@text{\reset@font\bfseries ??}%
    \@latex@warning{Reference `#1' on page \thepage \space undefined}%
  \else%
    \cref@gettype{#1}{\@temptype}%  puts label type in \@temptype
    \cref@getlabel{#1}{\@templabel}%  puts label in \@templabel
    \expandafter\ifx\csname #2@\@temptype @format#3\endcsname\relax%
      \protect\G@refundefinedtrue%
      \nfss@text{\reset@font\bfseries ??}~\@templabel%
      \@latex@warning{\string\Cref \space reference format for label
        type `\@temptype' undefined}%
    \else%
      \expandafter\@@setcref\expandafter%
        {\csname #2@\@temptype @format#3\endcsname}{#1}%
    \fi%
  \fi}
%    \end{macrocode}
%
% We separate out the very final typesetting step into a separate
% macro, in order to make it easier to redefine things later to make
% them work with the \package{hyperref} package.
%    \begin{macrocode}
\def\@@setcref#1#2{\cref@getlabel{#2}{\@templabel}#1{\@templabel}{}{}}
%    \end{macrocode}
%
% Define a new conditional to test whether two references are
% consecutive (needed when typesetting reference ranges). This uses
% the counter and prefix (i.e. formatted version of the counter that
% resets the label's counter) information provided by
% \cmd{\r@\meta{label}} (via the aux file) to check if the prefices are
% identical (i.e. the references come from the same chapter, section
% or whatever), and that the label counters differ by 1.
%    \begin{macrocode}
\newif\if@cref@refconsecutive%
\def\cref@isrefconsecutive#1#2{%
  \begingroup%
    \countdef\refa@counter=1%
    \countdef\refb@counter=2%
    \cref@getcounter{#1}{\@result}%
    \refa@counter=\@result%
    \advance\refa@counter 1%
    \cref@getcounter{#2}{\@result}%
    \refb@counter=\@result%
    \cref@getprefix{#1}{\refa@prefix}%
    \cref@getprefix{#2}{\refb@prefix}%
    \def\@after{\@cref@refconsecutivefalse}%
    \ifx\refa@prefix\refb@prefix%
      \ifnum\refa@counter=\refb@counter%
        \def\@after{\@cref@refconsecutivetrue}%
      \fi%
    \fi%
  \expandafter\endgroup\@after}
%    \end{macrocode}
%
% The internal \cmd{\@setcrefrange} macro deals with typesetting
% reference ranges, just as \cmd{\@setcref} does for normal
% references. The actual typesetting is no more complicated in the
% range case; it's the error checking that makes the code so much
% longer. We now have to check whether \emph{two} references are
% undefined, whether \emph{two} reference formats are undefined,
% whether the reference types are consistent, and also combinations of
% these various errors.
%    \begin{macrocode}
\def\@setcrefrange#1#2#3#4{%
  \begingroup%
%    \end{macrocode}
% Check if both references are defined.
%    \begin{macrocode}
    \expandafter\ifx\csname r@#1\endcsname\relax%
      \protect\G@refundefinedtrue%
      \@latex@warning{Reference `#1' on page \thepage \space%
        undefined}%
      \expandafter\ifx\csname r@#2\endcsname\relax%
        \nfss@text{\reset@font\bfseries ??}--%
        \nfss@text{\reset@font\bfseries ??}%
        \@latex@warning{Reference `#2' on page \thepage \space%
          undefined}%
      \else%
        \cref@getlabel{#2}{\@labelb}%
        \nfss@text{\reset@font\bfseries ??}--\@labelb%
      \fi%
    \else%
      \expandafter\ifx\csname r@#2\endcsname\relax%
        \protect\G@refundefinedtrue%
        \cref@getlabel{#1}{\@labela}%
        \@labela--\nfss@text{\reset@font\bfseries ??}%
        \@latex@warning{Reference `#2' on page \thepage \space undefined}%
%    \end{macrocode}
%   If both references are defined, check that the reference format is
%   defined.
%    \begin{macrocode}
      \else%
        \cref@gettype{#1}{\@typea}%
        \cref@gettype{#2}{\@typeb}%
        \cref@getlabel{#1}{\@labela}%
        \cref@getlabel{#2}{\@labelb}%
        \edef\@formata{\expandafter\noexpand%
          \csname #3range@\@typea @format#4\endcsname}%
        \edef\@formatb{\expandafter\noexpand%
          \csname #3range@\@typeb @format#4\endcsname}%
        \expandafter\ifx\@formata\relax%
          \protect\G@refundefinedtrue%
          \nfss@text{\reset@font\bfseries ??}~\@labela--\@labelb%
          \@latex@warning{#3\space reference range format for label
            type `\@typea' undefined}%
        \else%
%    \end{macrocode}
%     If reference types are identical, typeset reference range,
%     otherwise display warning.\\
%     (Note: there's no need to check if reference format for second
%     type is defined, since if it isn't it will be caught here as a
%     non-identical type.)
%    \begin{macrocode}
          \ifx\formata\formatb%
            \expandafter\@@setcrefrange\expandafter{\@formata}{#1}{#2}%
          \else%
            \protect\G@refundefinedtrue%
            \nfss@text{\reset@font\bfseries ??}~\@labela--\@labelb%
            \@latex@warning{Types inconsistent in reference range for
              references `#1' and `#2' on page \thepage}%
          \fi%
        \fi%
      \fi%
    \fi%
  \endgroup}
%    \end{macrocode}
%
% We again separate out the very final typesetting step into a
% separate macro, in order to make it easier to redefine things later
% to make them work with the \package{hyperref} package.
%    \begin{macrocode}
\def\@@setcrefrange#1#2#3{%
  \cref@getlabel{#2}{\@labela}%
  \cref@getlabel{#3}{\@labelb}%
  #1{\@labela}{\@labelb}{}{}{}{}}
%    \end{macrocode}
%
%
% The typesetting of conjunctions is also separated out into separate
% macros, for the same reason.
%    \begin{macrocode}
\def\@setcref@pairgroupconjunction{\crefpairgroupconjunction}
\def\@setcref@middlegroupconjunction{\crefmiddlegroupconjunction}
\def\@setcref@lastgroupconjunction{\creflastgroupconjunction}
%    \end{macrocode}
%
%
%
% \subsection{Reference Format Customisation Commands}
%
% \subsubsection{Format Component Commands}
% The reference formats are usually constructed out of components
% defined by the user-level \cmd{\crefname}, \cmd{\Crefname},
% \cmd{\creflabel} and \cmd{\crefrangelabel} commands. They simply use
% the supplied arguments to define appropriately named macros
% containing the formatting components. If the corresponding
% \cmd{\Crefname} or \cmd{\crefname} variant is not already defined,
% \cmd{\crefname} and \cmd{\Crefname} define it to be a version with
% the first letter capitalised or lower-cased, respectively.
% \cmd{\@crefdefineallformats} is then called to define all the
% formats from the new components.
%    \begin{macrocode}
\newcommand{\crefdefaultlabelformat}[1]{%
  \def\cref@default@label##1##2##3{#1}}
\newcommand{\crefname}[3]{%
  \@crefname{cref}{#1}{#2}{#3}%
  \@crefdefineallformats{#1}}
\newcommand{\Crefname}[3]{%
  \@crefname{Cref}{#1}{#2}{#3}%
  \@crefdefineallformats{#1}}
\newcommand{\creflabelformat}[2]{%
  \@creflabelformat{#1}{#2}%
  \@crefdefineallformats{#1}}
\newcommand{\crefrangelabelformat}[2]{%
  \@crefrangelabelformat{#1}{#2}%
  \@crefdefineallformats{#1}}
\def\@creflabelformat#1#2{%
  \expandafter\def\csname cref@#1@label\endcsname##1##2##3{#2}}
\def\@crefrangelabelformat#1#2{%
  \expandafter\def\csname cref@#1@rangelabel\endcsname%
  ##1##2##3##4##5##6{#2}}
%    \end{macrocode}
%
% The \cmd{\@crefname} utility macro does the real work of defining
% format names, by defining an appropriately named command to contain
% the format component, and using the first argument (``cref'' of
% ``Cref'') to determine how to define the corresponding command with
% the other capitalisation.
%    \begin{macrocode}
\def\@crefname#1#2#3#4{%
  \begingroup%
    \expandafter\gdef\csname #1@#2@name\endcsname{#3}%
    \expandafter\gdef\csname #1@#2@name@plural\endcsname{#4}%
%    \end{macrocode}
% The following \cmd{\@tmpa} macro makes use of the fact that the
% first character of \#1 is ``c'' for lower-case and ``C'' for
% upper-case, in order to wrap the capitalisation-dependent parts in
% macros so that the rest of the code can be capitalisation-variant
% agnostic.
%    \begin{macrocode}
    \def\@tmpa##1##2\@nil{%
      \if##1c%
        \def\@other{C##2}%
        \def\@changecase{\MakeUppercase}%
      \else%
        \def\@other{c##2}%
        \def\@changecase{\MakeLowercase}%
      \fi}%
    \@tmpa#1\@nil%
%    \end{macrocode}
% If the other capitalisation variant is not already defined...
%    \begin{macrocode}
    \@ifundefined{\@other @#2@name}{%
%    \end{macrocode}
% Define \cmd{\@tmpa} and \cmd{@tmpb} to be partial expansions
% (expanded just once) of the macros for the capitalisation variant
% we've just defined above. The \cmd{\@toska} token register just
% makes the code less verbose.
%    \begin{macrocode}
      \expandafter\expandafter\expandafter\def%
      \expandafter\expandafter\expandafter\@tmpa%
      \expandafter\expandafter\expandafter{%
        \csname#1@#2@name\endcsname}%
      \expandafter\expandafter\expandafter\def%
      \expandafter\expandafter\expandafter\@tmpb%
      \expandafter\expandafter\expandafter{%
        \csname#1@#2@name@plural\endcsname}%
%    \end{macrocode}
% Add the \cmd{\@changecase} command to the front of the definitions of
% \cmd{\@tmpa} and \cmd{\@tmpb}.
%    \begin{macrocode}
      \expandafter\expandafter\expandafter\def%
      \expandafter\expandafter\expandafter\@tmpa%
      \expandafter\expandafter\expandafter{%
        \expandafter\@changecase\@tmpa}%
      \expandafter\expandafter\expandafter\def%
      \expandafter\expandafter\expandafter\@tmpb%
      \expandafter\expandafter\expandafter{%
        \expandafter\@changecase\@tmpb}%
%    \end{macrocode}
% Define the other capitalisation variants to be the partial expansions
% (expanded just once) of \cmd{\@tmpa} and \cmd{\@tmpb}.
%    \begin{macrocode}
      \toksdef\@toksa=0%
      \@toksa={%
        \expandafter\gdef\csname\@other @#2@name\endcsname}%
      \expandafter\the\expandafter\@toksa\expandafter{\@tmpa}%
      \@toksa={%
        \expandafter\gdef\csname\@other @#2@name@plural\endcsname}%
      \expandafter\the\expandafter\@toksa\expandafter{\@tmpb}%
    }{}%
  \endgroup}
%    \end{macrocode}
%
%
% \cmd{\@crefconstructcomponents} utility macro puts the reference
% format components for the specified reference type into temporary
% macros, for use by later macros. The ridiculous number of ``\#''
% characters ensure that enough are left when they come to be used
% later (pairs ``\#\#'' are collapsed to a single ``\#'' each time the
% code is expanded).
%    \begin{macrocode}
\def\@crefconstructcomponents#1{%
  \@ifundefined{cref@#1@label}{%
    \let\@tmplabel\cref@default@label%
  }{%
    \expandafter\let\expandafter\@tmplabel%
    \csname cref@#1@label\endcsname%
  }%
  \@ifundefined{cref@#1@rangelabel}{%
    \expandafter\def\expandafter\@tmpa\expandafter{%
      \@tmplabel{####1}{####3}{####4}}%
    \expandafter\def\expandafter\@tmpb\expandafter{%
      \@tmplabel{####2}{####5}{####6}}%
    \toksdef\@toksa=0%
    \@toksa={\def\@tmprangelabel##1##2##3##4##5##6}%
    \expandafter\expandafter\expandafter\expandafter%
    \expandafter\expandafter\expandafter\the%
    \expandafter\expandafter\expandafter\expandafter%
    \expandafter\expandafter\expandafter\@toksa%
    \expandafter\expandafter\expandafter\expandafter%
    \expandafter\expandafter\expandafter{%
      \expandafter\expandafter\expandafter\@tmpa%
      \expandafter\crefrangeconjunction\@tmpb}%
  }{%
    \expandafter\let\expandafter\@tmprangelabel%
    \csname cref@#1@rangelabel\endcsname%
  }%
  \expandafter\expandafter\expandafter\def%
  \expandafter\expandafter\expandafter\@tmpname%
  \expandafter\expandafter\expandafter{%
    \csname cref@#1@name\endcsname}%
  \expandafter\expandafter\expandafter\def%
  \expandafter\expandafter\expandafter\@tmpName%
  \expandafter\expandafter\expandafter{%
    \csname Cref@#1@name\endcsname}%
  \expandafter\expandafter\expandafter\def%
  \expandafter\expandafter\expandafter\@tmpnameplural%
  \expandafter\expandafter\expandafter{%
    \csname cref@#1@name@plural\endcsname}%
  \expandafter\expandafter\expandafter\def%
  \expandafter\expandafter\expandafter\@tmpNameplural%
  \expandafter\expandafter\expandafter{%
    \csname Cref@#1@name@plural\endcsname}%
  \expandafter\def\expandafter\@tmplabel\expandafter{%
    \@tmplabel{########1}{########2}{########3}}%
  \expandafter\def\expandafter\@tmprangelabel\expandafter{%
    \@tmprangelabel{########1}{########2}{########3}%
    {########4}{########5}{########6}}%
}
%    \end{macrocode}
%
% The \cmd{\@crefdefineformat} et al.\ macros construct calls to
% \cmd{\crefformat} et al.\ for the supplied reference type that
% define the corresponding formats in terms of the format
% components. This is mostly just an arduous exercise in controlling
% macro expansion order.
%    \begin{macrocode}
\def\@crefdefineformat#1{%
  \begingroup%
%    \end{macrocode}
% put format components into tmp macros
%    \begin{macrocode}
    \@crefconstructcomponents{#1}%
%    \end{macrocode}
% Assemble the arguments for \cmd{\crefformat} and \cmd{\Crefformat}
% from the components.
%    \begin{macrocode}
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpfirst%
    \expandafter\expandafter\expandafter{%
      \expandafter\@tmpname\expandafter~\@tmplabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpFirst%
    \expandafter\expandafter\expandafter{%
      \expandafter\@tmpName\expandafter~\@tmplabel}%
%    \end{macrocode}
% Define \cmd{\crefformat} and \cmd{\Crefformat}.
%    \begin{macrocode}
    \toksdef\@toksa=0%
    \@toksa={\crefformat{#1}}%
    \expandafter\the\expandafter\@toksa\expandafter{\@tmpfirst}%
    \@toksa={\Crefformat{#1}}%
    \expandafter\the\expandafter\@toksa\expandafter{\@tmpFirst}%
  \endgroup}
%
\def\@crefrangedefineformat#1{%
  \begingroup%
%    \end{macrocode}
% put format components into tmp macros
%    \begin{macrocode}
    \@crefconstructcomponents{#1}%
%    \end{macrocode}
% Assemble the arguments for \cmd{\crefrangeformat} and
% \cmd{\Crefrangeformat} from the components.
%    \begin{macrocode}
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpfirst%
    \expandafter\expandafter\expandafter{%
      \expandafter\@tmpnameplural\expandafter~\@tmprangelabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpFirst%
    \expandafter\expandafter\expandafter{%
      \expandafter\@tmpNameplural\expandafter~\@tmprangelabel}%
%    \end{macrocode}
% Define \cmd{\crefrangeformat} and \cmd{\Crefrangeformat}.
%    \begin{macrocode}
    \toksdef\@toksa=0%
    \@toksa={\crefrangeformat{#1}}%
    \expandafter\the\expandafter\@toksa\expandafter{\@tmpfirst}%
    \@toksa={\Crefrangeformat{#1}}%
    \expandafter\the\expandafter\@toksa\expandafter{\@tmpFirst}%
  \endgroup}
%
\def\@crefdefinemultiformat#1{%
  \begingroup%
%    \end{macrocode}
% put format components into tmp macros
%    \begin{macrocode}
    \@crefconstructcomponents{#1}%
%    \end{macrocode}
% Assemble the arguments for \cmd{\crefmultiformat} and
% \cmd{\Crefmultiformat} from the components.
%    \begin{macrocode}
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpfirst%
    \expandafter\expandafter\expandafter{%
      \expandafter\@tmpnameplural\expandafter~\@tmplabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpFirst%
    \expandafter\expandafter\expandafter{%
      \expandafter\@tmpNameplural\expandafter~\@tmplabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpsecond%
    \expandafter\expandafter\expandafter{%
      \expandafter\crefpairconjunction\@tmplabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpmiddle%
    \expandafter\expandafter\expandafter{%
      \expandafter\crefmiddleconjunction\@tmplabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmplast%
    \expandafter\expandafter\expandafter{%
      \expandafter\creflastconjunction\@tmplabel}%
%    \end{macrocode}
% Bundle all four arguments for \cmd{\crefmultiformat} in token
% register \cmd{\@toksb}, then call it.
%    \begin{macrocode}
    \toksdef\@toksa=0%
    \toksdef\@toksb=1%
    \@toksb={}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpfirst}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpsecond}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpmiddle}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmplast}}%
    \@toksa={\crefmultiformat{#1}}%
    \expandafter\the\expandafter\@toksa\the\@toksb%
%    \end{macrocode}
% Bundle all four arguments for \cmd{\Crefmultiformat} in token
% register \cmd{\@toksb}, then call it.
%    \begin{macrocode}
    \@toksb={}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpFirst}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpsecond}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpmiddle}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmplast}}%
    \@toksa={\Crefmultiformat{#1}}%
    \expandafter\the\expandafter\@toksa\the\@toksb%
  \endgroup}
%
\def\@crefrangedefinemultiformat#1{%
  \begingroup%
%    \end{macrocode}
% put format components into tmp macros
%    \begin{macrocode}
    \@crefconstructcomponents{#1}%
%    \end{macrocode}
% Assemble the arguments that need to be passed to
% \cmd{\crefrangemultiformat} and \cmd{\Crefrangemultiformat} from the
% reference components.
%    \begin{macrocode}
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpfirst%
    \expandafter\expandafter\expandafter{%
      \expandafter\@tmpnameplural\expandafter~\@tmprangelabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpFirst%
    \expandafter\expandafter\expandafter{%
      \expandafter\@tmpNameplural\expandafter~\@tmprangelabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpsecond%
    \expandafter\expandafter\expandafter{%
      \expandafter\crefpairconjunction\@tmprangelabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmpmiddle%
    \expandafter\expandafter\expandafter{%
      \expandafter\crefmiddleconjunction\@tmprangelabel}%
    \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter\@tmplast%
    \expandafter\expandafter\expandafter{%
      \expandafter\creflastconjunction\@tmprangelabel}%
%    \end{macrocode}
% Bundle all four arguments for \cmd{\crefrangemultiformat} in token
% register \cmd{\@toksb}, then call it.
%    \begin{macrocode}
    \toksdef\@toksa=0%
    \toksdef\@toksb=1%
    \@toksb={}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpfirst}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpsecond}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpmiddle}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmplast}}%
    \@toksa={\crefrangemultiformat{#1}}%
    \expandafter\the\expandafter\@toksa\the\@toksb%
%    \end{macrocode}
% Bundle all four arguments for \cmd{\Crefrangemultiformat} in token
% register \cmd{\@toksb}, then call it.
%    \begin{macrocode}
    \@toksb={}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpFirst}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpsecond}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmpmiddle}}%
    \expandafter\cref@append@toks\expandafter\@toksb\expandafter{%
      \expandafter{\@tmplast}}%
    \@toksa={\Crefrangemultiformat{#1}}%
    \expandafter\the\expandafter\@toksa\the\@toksb%
  \endgroup}
%    \end{macrocode}
%
% \cmd{\@crefdefineallformats} calls each of the above, to define all
% formats for the given type from the corresponding components.
%    \begin{macrocode}
\def\@crefdefineallformats#1{%
  \@crefdefineformat{#1}%
  \@crefrangedefineformat{#1}%
  \@crefdefinemultiformat{#1}%
  \@crefrangedefinemultiformat{#1}}
%    \end{macrocode}
%
%
% \subsubsection{Format Definition Commands}\label{sec:crefformat}
% \cmd{\crefformat} et al.\ are lower-level user commands that give
% complete control over the format of different reference types. They
% override the component-based formats, simply using the supplied
% arguments to define appropriately named formatting macros, which are
% called by \cmd{\@setcref}. The only moderately interesting part is
% that if the corresponding \cmd{\Crefformat} or \cmd{\crefformat}
% variant is not already defined, they define it to be a version with
% the first letter capitalised or lower-cased.
%    \begin{macrocode}
\newcommand{\crefformat}[2]{\@crefformat{cref}{#1}{#2}}
\newcommand{\Crefformat}[2]{\@crefformat{Cref}{#1}{#2}}
\newcommand{\crefrangeformat}[2]{\@crefrangeformat{crefrange}{#1}{#2}}
\newcommand{\Crefrangeformat}[2]{\@crefrangeformat{Crefrange}{#1}{#2}}
\newcommand{\crefmultiformat}[5]{%
  \@crefmultiformat{cref}{#1}{#2}{#3}{#4}{#5}}
\newcommand{\Crefmultiformat}[5]{%
  \@crefmultiformat{Cref}{#1}{#2}{#3}{#4}{#5}}
\newcommand{\crefrangemultiformat}[5]{%
  \@crefrangemultiformat{crefrange}{#1}{#2}{#3}{#4}{#5}}
\newcommand{\Crefrangemultiformat}[5]{%
  \@crefrangemultiformat{Crefrange}{#1}{#2}{#3}{#4}{#5}}
%    \end{macrocode}
%
% The utility macros do the real work, by using the first argument
% (``cref'' or ``Cref'', and ``crefrange'' or ``Crefrange'') to
% determine how to define the corresponding command with the other
% capitalisation.
%    \begin{macrocode}
\def\@crefformat#1#2#3{%
  \begingroup%
    \expandafter\gdef\csname #1@#2@format\endcsname##1##2##3{#3}%
%    \end{macrocode}
% The following \cmd{\@tmpa} macro makes use of the fact that the
% first character of \#1 is ``c'' for lower-case and ``C'' for
% upper-case, in order to wrap the capitalisation-dependent parts in
% macros so that the rest of the code can be capitalisation-variant
% agnostic.
%    \begin{macrocode}
    \def\@tmpa##1##2\@nil{%
      \if##1c%
        \def\@other{C##2}%
        \def\@changecase{\MakeUppercase}%
      \else%
        \def\@other{c##2}%
        \def\@changecase{\MakeLowercase}%
      \fi}%
    \@tmpa#1\@nil%
%    \end{macrocode}
% If the other capitalisation variant is not already defined...
%    \begin{macrocode}
    \@ifundefined{\@other @#2@format}{%
%    \end{macrocode}
% Define \cmd{\@tmpa} to be a partial expansion (expanded just once)
% of the capitalisation variant we've just defined above. The
% \cmd{\@toska} token register just makes the code less verbose.
%    \begin{macrocode}
      \toksdef\@toksa=0%
      \@toksa={\def\@tmpa##1##2##3}%
      \expandafter\expandafter\expandafter\the%
      \expandafter\expandafter\expandafter\@toksa%
      \expandafter\expandafter\expandafter{%
        \csname#1@#2@format\endcsname{##1}{##2}{##3}}%
%    \end{macrocode}
% Add the \cmd{\@changecase} command to the front of the definition of
% \cmd{\@tmpa}.
%    \begin{macrocode}
      \expandafter\expandafter\expandafter\the%
      \expandafter\expandafter\expandafter\@toksa%
      \expandafter\expandafter\expandafter{%
        \expandafter\@changecase\@tmpa{##1}{##2}{##3}}%
%    \end{macrocode}
% Define the other capitalisation variant to be the partial expansion
% (expanded just once) of \cmd{\@tmpa}.
%    \begin{macrocode}
      \@toksa={%
        \expandafter\gdef\csname\@other @#2@format\endcsname##1##2##3}%
      \expandafter\the\expandafter\@toksa\expandafter{%
        \@tmpa{##1}{##2}{##3}}%
    }{}%
  \endgroup}
%
\def\@crefrangeformat#1#2#3{%
  \begingroup%
    \expandafter\gdef\csname #1@#2@format\endcsname%
      ##1##2##3##4##5##6{#3}%
%    \end{macrocode}
% The following \cmd{\@tmpa} macro makes use of the fact that the
% first character of \#1 is ``c'' for lower-case and ``C'' for
% upper-case, in order to wrap the capitalisation-dependent parts in
% macros so that the rest of the code can be capitalisation-variant
% agnostic.
%    \begin{macrocode}
    \def\@tmpa##1##2\@nil{%
      \if##1c%
        \def\@other{C##2}%
        \def\@changecase{\MakeUppercase}%
      \else%
        \def\@other{c##2}%
        \def\@changecase{\MakeLowercase}%
      \fi}%
    \@tmpa#1\@nil%
%    \end{macrocode}
% If the other capitalisation variant is not already defined...
%    \begin{macrocode}
    \@ifundefined{\@other @#2@format}{%
%    \end{macrocode}
% Define \cmd{\@tmpa} to be a partial expansion (expanded just once)
% of the capitalisation variant we've just defined above. The
% \cmd{\@toska} token register just makes the code less verbose.
%    \begin{macrocode}
      \toksdef\@toksa=0%
      \@toksa={\def\@tmpa##1##2##3##4##5##6}%
      \expandafter\expandafter\expandafter\the%
      \expandafter\expandafter\expandafter\@toksa%
      \expandafter\expandafter\expandafter{%
        \csname#1@#2@format\endcsname{##1}{##2}{##3}{##4}{##5}{##6}}%
%    \end{macrocode}
% Add the \cmd{\@changecase} command to the front of the definition of
% \cmd{\@tmpa}.
%    \begin{macrocode}
      \expandafter\expandafter\expandafter\the%
      \expandafter\expandafter\expandafter\@toksa%
      \expandafter\expandafter\expandafter{%
        \expandafter\@changecase\@tmpa{##1}{##2}{##3}{##4}{##5}{##6}}%
%    \end{macrocode}
% Define the other capitalisation variant to be the partial expansion
% (expanded just once) of \cmd{\@tmpa}.
%    \begin{macrocode}
      \@toksa={\expandafter\gdef%
        \csname\@other @#2@format\endcsname##1##2##3##4##5##6}%
      \expandafter\the\expandafter\@toksa\expandafter{%
        \@tmpa{##1}{##2}{##3}{##4}{##5}{##6}}%
    }{}%
  \endgroup}
%
\def\@crefmultiformat#1#2#3#4#5#6{%
  \begingroup%
    \expandafter\gdef\csname #1@#2@format@first\endcsname##1##2##3{#3}%
    \expandafter\gdef\csname #1@#2@format@second\endcsname##1##2##3{#4}%
    \expandafter\gdef\csname #1@#2@format@middle\endcsname##1##2##3{#5}%
    \expandafter\gdef\csname #1@#2@format@last\endcsname##1##2##3{#6}%
%    \end{macrocode}
% The following \cmd{\@tmpa} macro makes use of the fact that the
% first character of \#1 is ``c'' for lower-case and ``C'' for
% upper-case, in order to wrap the capitalisation-dependent parts in
% macros so that the rest of the code can be capitalisation-variant
% agnostic.
%    \begin{macrocode}
    \def\@tmpa##1##2\@nil{%
      \if##1c%
        \def\@other{C##2}%
        \def\@changecase{\MakeUppercase}%
      \else%
        \def\@other{c##2}%
        \def\@changecase{\MakeLowercase}%
      \fi}%
    \@tmpa#1\@nil%
%    \end{macrocode}
% If the other capitalisation variant of the first part of the
% multi-format definition is not already defined...
%    \begin{macrocode}
    \@ifundefined{\@other @#2@format@first}{%
%    \end{macrocode}
% Define \cmd{\@tmpa} to be a partial expansion (expanded just once)
% of the capitalisation variant we've just defined above. The
% \cmd{\@toska} token register just makes the code less verbose.
%    \begin{macrocode}
      \toksdef\@toksa=0%
      \@toksa={\def\@tmpa##1##2##3}%
      \expandafter\expandafter\expandafter\the%
      \expandafter\expandafter\expandafter\@toksa%
      \expandafter\expandafter\expandafter{%
        \csname#1@#2@format@first\endcsname{##1}{##2}{##3}}%
%    \end{macrocode}
% Add the \cmd{\@changecase} command to the front of the definition of
% \cmd{\@tmpa}.
%    \begin{macrocode}
      \expandafter\expandafter\expandafter\the%
      \expandafter\expandafter\expandafter\@toksa%
      \expandafter\expandafter\expandafter{%
        \expandafter\@changecase\@tmpa{##1}{##2}{##3}}%
%    \end{macrocode}
% Define the other capitalisation variant to be the partial expansion
% (expanded just once) of \cmd{\@tmpa}.
%    \begin{macrocode}
      \@toksa={%
        \expandafter\gdef\csname\@other @#2@format@first\endcsname%
          ##1##2##3}%
      \expandafter\the\expandafter\@toksa\expandafter{%
        \@tmpa{##1}{##2}{##3}}%
    }{}%
%    \end{macrocode}
% The other parts of the multi-format definition are defined to be
% identical for both capitalisation variants.
%    \begin{macrocode}
    \@ifundefined{\@other @#2@format@second}{%
      \@toksa={%
        \expandafter\global\expandafter\let%
        \csname\@other @#2@format@second\endcsname}%
      \expandafter\the\expandafter\@toksa%
        \csname #1@#2@format@second\endcsname%
    }{}%
    \@ifundefined{\@other @#2@format@middle}{%
      \@toksa={%
        \expandafter\global\expandafter\let%
        \csname\@other @#2@format@middle\endcsname}%
      \expandafter\the\expandafter\@toksa%
        \csname #1@#2@format@middle\endcsname%
    }{}%
    \@ifundefined{\@other @#2@format@last}{%
      \@toksa={%
        \expandafter\global\expandafter\let%
        \csname\@other @#2@format@last\endcsname}%
      \expandafter\the\expandafter\@toksa%
        \csname #1@#2@format@last\endcsname%
    }{}%
  \endgroup}
%
\def\@crefrangemultiformat#1#2#3#4#5#6{%
  \begingroup%
    \expandafter\gdef\csname #1@#2@format@first\endcsname%
      ##1##2##3##4##5##6{#3}%
    \expandafter\gdef\csname #1@#2@format@second\endcsname%
      ##1##2##3##4##5##6{#4}%
    \expandafter\gdef\csname #1@#2@format@middle\endcsname%
      ##1##2##3##4##5##6{#5}%
    \expandafter\gdef\csname #1@#2@format@last\endcsname%
      ##1##2##3##4##5##6{#6}%
%    \end{macrocode}
% The following \cmd{\@tmpa} macro makes use of the fact that the
% first character of \#1 is ``c'' for lower-case and ``C'' for
% upper-case, in order to wrap the capitalisation-dependent parts in
% macros so that the rest of the code can be capitalisation-variant
% agnostic.
%    \begin{macrocode}
    \def\@tmpa##1##2\@nil{%
      \if##1c%
        \def\@other{C##2}%
        \def\@changecase{\MakeUppercase}%
      \else%
        \def\@other{c##2}%
        \def\@changecase{\MakeLowercase}%
      \fi}%
    \@tmpa#1\@nil%
%    \end{macrocode}
% If the other capitalisation variant of the first part of the
% multi-format definition is not already defined...
%    \begin{macrocode}
    \@ifundefined{\@other @#2@format@first}{%
%    \end{macrocode}
% Define \cmd{\@tmpa} to be a partial expansion (expanded just once)
% of the capitalisation variant we've just defined above. The
% \cmd{\@toska} token register just makes the code less verbose.
%    \begin{macrocode}
      \toksdef\@toksa=0%
      \@toksa={\def\@tmpa##1##2##3##4##5##6}%
      \expandafter\expandafter\expandafter\the%
      \expandafter\expandafter\expandafter\@toksa%
      \expandafter\expandafter\expandafter{%
        \csname#1@#2@format@first\endcsname%
          {##1}{##2}{##3}{##4}{##5}{##6}}%
%    \end{macrocode}
% Add the \cmd{\@changecase} command to the front of the definition of
% \cmd{\@tmpa}.
%    \begin{macrocode}
      \expandafter\expandafter\expandafter\the%
      \expandafter\expandafter\expandafter\@toksa%
      \expandafter\expandafter\expandafter{%
        \expandafter\@changecase\@tmpa{##1}{##2}{##3}{##4}{##5}{##6}}%
%    \end{macrocode}
% Define the other capitalisation variant to be the partial expansion
% (expanded just once) of \cmd{\@tmpa}.
%    \begin{macrocode}
      \@toksa={%
        \expandafter\gdef\csname\@other @#2@format@first\endcsname%
          ##1##2##3##4##5##6}%
      \expandafter\the\expandafter\@toksa\expandafter{%
        \@tmpa{##1}{##2}{##3}{##4}{##5}{##6}}%
    }{}%
%    \end{macrocode}
% The other parts of the multi-format definition are defined to be
% identical for both capitalisation variants.
%    \begin{macrocode}
    \@ifundefined{\@other @#2@format@second}{%
      \@toksa={%
        \expandafter\global\expandafter\let%
        \csname\@other @#2@format@second\endcsname}%
      \expandafter\the\expandafter\@toksa%
        \csname #1@#2@format@second\endcsname%
    }{}%
    \@ifundefined{\@other @#2@format@middle}{%
      \@toksa={%
        \expandafter\global\expandafter\let%
        \csname\@other @#2@format@middle\endcsname}%
      \expandafter\the\expandafter\@toksa%
        \csname #1@#2@format@middle\endcsname%
    }{}%
    \@ifundefined{\@other @#2@format@last}{%
      \@toksa={%
        \expandafter\global\expandafter\let%
        \csname\@other @#2@format@last\endcsname}%
      \expandafter\the\expandafter\@toksa%
        \csname #1@#2@format@last\endcsname%
    }{}%
  \endgroup}
%    \end{macrocode}
%
%
% \subsection{Default Reference Formats}
% We define default reference format components appropriate for
% \LaTeX{} documents written in English.\\
% FIXME: add babel support
%
% The following must be defined before the user has the chance to
% customise anything, otherwise the component commands will not work.
%    \begin{macrocode}
\newcommand{\crefrangeconjunction}{ to~}
\newcommand{\crefpairconjunction}{ and~}
\newcommand{\crefmiddleconjunction}{, }
\newcommand{\creflastconjunction}{ and~}
\crefdefaultlabelformat{#2#1#3}
%    \end{macrocode}
%
% Define default format components. We want don't want these to be
% defined during the document preamble in case the user defines them
% themselves (automatic definition of the other capitalisation variant
% would fail). Therefore, we postpone actual definition of the formats
% until the beginning of the document, and only define those
% that haven't already been defined.\\
% FIXME: add babel support
%    \begin{macrocode}
\AtBeginDocument{%
%    \end{macrocode}
% If the group conjunctions haven't been defined, define them to be
% identical to the reference conjunctions.
%    \begin{macrocode}
  \@ifundefined{crefpairgroupconjunction}{%
    \let\crefpairgroupconjunction\crefpairconjunction}{}%
  \@ifundefined{crefmiddlegroupconjunction}{%
    \let\crefmiddlegroupconjunction\crefmiddleconjunction}{}%
%    \end{macrocode}
% If the last reference conjunction hasn't been modified from its
% default, define the last group conjunction to include an extra
% comma. However, if the user has modified the last reference
% conjunction but hasn't defined the last group conjunction, they will
% expect the last group conjunction to be identical to their last
% reference conjunction definition, so do that.
%    \begin{macrocode}
  \@ifundefined{creflastgroupconjunction}{%
    \def\@tmpa{ and~}%
    \ifx\creflastconjunction\@tmpa%
      \def\creflastgroupconjunction{, and~}%
    \else%
      \let\creflastgroupconjunction\creflastconjunction%
    \fi}{}%
%    \end{macrocode}
% Define default names (and in the case of equations, also the label
% format). We define the lowercase and capitalised versions
% separately, rather than relying on the automatic definitions,
% because the code produced by the poor man's sed script is then
% slightly tidier.
%    \begin{macrocode}
  \@ifundefined{cref@equation@name}{%
    \@crefname{cref}{equation}{eq.}{eqs.}%
    \@crefname{Cref}{equation}{Equation}{Equations}}{}%
  \@ifundefined{cref@equation@label}{%
    \@creflabelformat{equation}{\textup{(#2#1#3)}}%
    \@ifundefined{cref@equation@rangelabel}{%
      \@crefrangelabelformat{equation}{%
        \textup{(#3#1#4)}--\textup{(#5#2#6)}}}{}}{}%
   \@ifundefined{cref@chapter@name}{%
     \@crefname{cref}{chapter}{chapter}{chapters}%
     \@crefname{Cref}{chapter}{Chapter}{Chapters}}{}%
  \@ifundefined{cref@section@name}{%
    \@crefname{cref}{section}{section}{sections}%
    \@crefname{Cref}{section}{Section}{Sections}}{}%
  \@ifundefined{cref@subsection@name}{%
    \@crefname{cref}{subsection}{section}{sections}%
    \@crefname{Cref}{subsection}{Section}{Sections}}{}%
  \@ifundefined{cref@subsubsection@name}{%
    \@crefname{cref}{subsubsection}{section}{sections}%
    \@crefname{Cref}{subsubsection}{Section}{Sections}}{}%
  \@ifundefined{cref@subsubsubsection@name}{%
    \@crefname{cref}{subsubsubsection}{section}{sections}%
    \@crefname{Cref}{subsubsubsection}{Section}{Sections}}{}%
  \@ifundefined{cref@appendix@name}{%
    \@crefname{cref}{appendix}{appendix}{appendices}%
    \@crefname{Cref}{appendix}{Appendix}{Appendices}}{}%
  \@ifundefined{cref@figure@name}{%
    \@crefname{cref}{figure}{fig.}{figs.}%
    \@crefname{Cref}{figure}{Figure}{Figures}}{}%
  \@ifundefined{cref@figure@rangelabel}{%
    \@crefrangelabelformat{figure}{#3#1#4--#5#2#6}}{}%
  \@ifundefined{cref@table@name}{%
    \@crefname{cref}{table}{table}{tables}%
    \@crefname{Cref}{table}{Table}{Tables}}{}%
  \@ifundefined{cref@theorem@name}{%
    \@crefname{cref}{theorem}{theorem}{theorems}%
    \@crefname{Cref}{theorem}{Theorem}{Theorems}}{}%
  \@ifundefined{cref@enumi@name}{%
    \@crefname{cref}{enumi}{item}{items}%
    \@crefname{Cref}{enumi}{Item}{Items}}{}%
  \@ifundefined{cref@enumii@name}{%
    \@crefname{cref}{enumii}{item}{items}%
    \@crefname{Cref}{enumii}{Item}{Items}}{}%
  \@ifundefined{cref@enumiii@name}{%
    \@crefname{cref}{enumiii}{item}{items}%
    \@crefname{Cref}{enumiii}{Item}{Items}}{}%
  \@ifundefined{cref@enumiv@name}{%
    \@crefname{cref}{enumiv}{item}{items}%
    \@crefname{Cref}{enumiv}{Item}{Items}}{}%
  \@ifundefined{cref@enumv@name}{%
    \@crefname{cref}{enumv}{item}{items}%
    \@crefname{Cref}{enumv}{Item}{Items}}{}%
%    \end{macrocode}
% Define any undefined formats using the components.
%    \begin{macrocode}
  \@ifundefined{cref@equation@format}{%
    \@crefdefineformat{equation}}{}%
  \@ifundefined{crefrange@equation@format}{%
    \@crefrangedefineformat{equation}}{}%
  \@ifundefined{cref@equation@format@first}{%
    \@crefdefinemultiformat{equation}}{}%
  \@ifundefined{crefrange@equation@format@first}{%
    \@crefrangedefinemultiformat{equation}}{}%
  \@ifundefined{cref@chapter@format}{%
    \@crefdefineformat{chapter}}{}%
  \@ifundefined{crefrange@chapter@format}{%
    \@crefrangedefineformat{chapter}}{}%
  \@ifundefined{cref@chapter@format@first}{%
    \@crefdefinemultiformat{chapter}}{}%
  \@ifundefined{crefrange@chapter@format@first}{%
    \@crefrangedefinemultiformat{chapter}}{}%
  \@ifundefined{cref@section@format}{%
    \@crefdefineformat{section}}{}%
  \@ifundefined{crefrange@section@format}{%
    \@crefrangedefineformat{section}}{}%
  \@ifundefined{cref@section@format@first}{%
    \@crefdefinemultiformat{section}}{}%
  \@ifundefined{crefrange@section@format@first}{%
    \@crefrangedefinemultiformat{section}}{}%
  \@ifundefined{cref@subsection@format}{%
    \@crefdefineformat{subsection}}{}%
  \@ifundefined{crefrange@subsection@format}{%
    \@crefrangedefineformat{subsection}}{}%
  \@ifundefined{cref@subsection@format@first}{%
    \@crefdefinemultiformat{subsection}}{}%
  \@ifundefined{crefrange@subsection@format@first}{%
    \@crefrangedefinemultiformat{subsection}}{}%
  \@ifundefined{cref@subsubsection@format}{%
    \@crefdefineformat{subsubsection}}{}%
  \@ifundefined{crefrange@subsubsection@format}{%
    \@crefrangedefineformat{subsubsection}}{}%
  \@ifundefined{cref@subsubsection@format@first}{%
    \@crefdefinemultiformat{subsubsection}}{}%
  \@ifundefined{crefrange@subsubsection@format@first}{%
    \@crefrangedefinemultiformat{subsubsection}}{}%
  \@ifundefined{cref@subsubsubsection@format}{%
    \@crefdefineformat{subsubsubsection}}{}%
  \@ifundefined{crefrange@subsubsubsection@format}{%
    \@crefrangedefineformat{subsubsubsection}}{}%
  \@ifundefined{cref@subsubsubsection@format@first}{%
    \@crefdefinemultiformat{subsubsubsection}}{}%
  \@ifundefined{crefrange@subsubsubsection@format@first}{%
    \@crefrangedefinemultiformat{subsubsubsection}}{}%
  \@ifundefined{cref@appendix@format}{%
    \@crefdefineformat{appendix}}{}%
  \@ifundefined{crefrange@appendix@format}{%
    \@crefrangedefineformat{appendix}}{}%
  \@ifundefined{cref@appendix@format@first}{%
    \@crefdefinemultiformat{appendix}}{}%
  \@ifundefined{crefrange@appendix@format@first}{%
    \@crefrangedefinemultiformat{appendix}}{}%
  \@ifundefined{cref@figure@format}{%
    \@crefdefineformat{figure}}{}%
  \@ifundefined{crefrange@figure@format}{%
    \@crefrangedefineformat{figure}}{}%
  \@ifundefined{cref@figure@format@first}{%
    \@crefdefinemultiformat{figure}}{}%
  \@ifundefined{crefrange@figure@format@first}{%
    \@crefrangedefinemultiformat{figure}}{}%
  \@ifundefined{cref@table@format}{%
    \@crefdefineformat{table}}{}%
  \@ifundefined{crefrange@table@format}{%
    \@crefrangedefineformat{table}}{}%
  \@ifundefined{cref@table@format@first}{%
    \@crefdefinemultiformat{table}}{}%
  \@ifundefined{crefrange@table@format@first}{%
    \@crefrangedefinemultiformat{table}}{}%
  \@ifundefined{cref@theorem@format}{%
    \@crefdefineformat{theorem}}{}%
  \@ifundefined{crefrange@theorem@format}{%
    \@crefrangedefineformat{theorem}}{}%
  \@ifundefined{cref@theorem@format@first}{%
    \@crefdefinemultiformat{theorem}}{}%
  \@ifundefined{crefrange@theorem@format@first}{%
    \@crefrangedefinemultiformat{theorem}}{}%
  \@ifundefined{cref@enumi@format}{%
    \@crefdefineformat{enumi}}{}%
  \@ifundefined{crefrange@enumi@format}{%
    \@crefrangedefineformat{enumi}}{}%
  \@ifundefined{cref@enumi@format@first}{%
    \@crefdefinemultiformat{enumi}}{}%
  \@ifundefined{crefrange@enumi@format@first}{%
    \@crefrangedefinemultiformat{enumi}}{}%
  \@ifundefined{cref@enumii@format}{%
    \@crefdefineformat{enumii}}{}%
  \@ifundefined{crefrange@enumii@format}{%
    \@crefrangedefineformat{enumii}}{}%
  \@ifundefined{cref@enumii@format@first}{%
    \@crefdefinemultiformat{enumii}}{}%
  \@ifundefined{crefrange@enumii@format@first}{%
    \@crefrangedefinemultiformat{enumii}}{}%
  \@ifundefined{cref@enumiii@format}{%
    \@crefdefineformat{enumiii}}{}%
  \@ifundefined{crefrange@enumiii@format}{%
    \@crefrangedefineformat{enumiii}}{}%
  \@ifundefined{cref@enumiii@format@first}{%
    \@crefdefinemultiformat{enumiii}}{}%
  \@ifundefined{crefrange@enumiii@format@first}{%
    \@crefrangedefinemultiformat{enumiii}}{}%
  \@ifundefined{cref@enumiv@format}{%
    \@crefdefineformat{enumiv}}{}%
  \@ifundefined{crefrange@enumiv@format}{%
    \@crefrangedefineformat{enumiv}}{}%
  \@ifundefined{cref@enumiv@format@first}{%
    \@crefdefinemultiformat{enumiv}}{}%
  \@ifundefined{crefrange@enumiv@format@first}{%
    \@crefrangedefinemultiformat{enumiv}}{}%
  \@ifundefined{cref@enumiv@format}{%
    \@crefdefineformat{enumiv}}{}%
  \@ifundefined{crefrange@enumiv@format}{%
    \@crefrangedefineformat{enumiv}}{}%
  \@ifundefined{cref@enumiv@format@first}{%
    \@crefdefinemultiformat{enumiv}}{}%
  \@ifundefined{crefrange@enumiv@format@first}{%
    \@crefrangedefinemultiformat{enumiv}}{}%
}
%    \end{macrocode}
%
%
% \subsection{\package{hyperref} Support}
% If the \package{hyperref} package is loaded, we add hyper-link
% support to \package{cleveref}. Since \package{hyperref} messes
% around with some of the same \LaTeX{} internals as
% \package{cleveref}, we also have to override some of its
% redefinitions so that they work with \package{cleveref}.
%    \begin{macrocode}
\@ifpackageloaded{hyperref}{%
\PackageInfo{cleveref}{`hyperref' support loaded}
\@ifpackagewith{hyperref}{backref}{%
  \PackageError{cleveref}{`cleveref' is currently incompatible with
    `hyperref's `backref' option}{Remove the `backref' option from
    `hyperref' if you want to use `cleveref'}}{}
%    \end{macrocode}
% We redefine the utility macros to cope with the extra arguments
% supplied by hyperref (via the aux file).
%    \begin{macrocode}
\def\cref@reflabel#1#2#3#4#5{\@result}
\def\cref@hyperref#1{\expandafter\expandafter\expandafter%
  \@fourthoffive\csname r@#1\endcsname}
\def\cref@getlabel#1#2{%
  \expandafter\let\expandafter\@tmpa\csname r@#1\endcsname%
  \edef\@tmpa{\expandafter\@firstoffive\@tmpa}%
  \expandafter\@cref@getlabel\@tmpa\@nil#2}
\def\cref@gettype#1#2{%
  \expandafter\let\expandafter\@tmpa\csname r@#1\endcsname%
  \edef\@tmpa{\expandafter\@firstoffive\@tmpa}%
  \expandafter\@cref@gettype\@tmpa\@nil#2}
\def\cref@getcounter#1#2{%
  \expandafter\let\expandafter\@tmpa\csname r@#1\endcsname%
  \edef\@tmpa{\expandafter\@firstoffive\@tmpa}%
  \expandafter\@cref@getcounter\@tmpa\@nil#2}
\def\cref@getprefix#1#2{%
  \expandafter\let\expandafter\@tmpa\csname r@#1\endcsname%
  \edef\@tmpa{\expandafter\@firstoffive\@tmpa}%
  \expandafter\@cref@getprefix\@tmpa\@nil#2}
%    \end{macrocode}
%
% The \package{hyperref} package stores the original
% \cmd{\refstepcounter} definition as \cmd{\H@refstepcounter}, which
% we therefore need to modify so that it adds the extra information to
% \cmd{\@currentlabel}.
%    \begin{macrocode}
\def\H@refstepcounter#1{%
  \stepcounter{#1}%
  \cref@resetby{#1}{\@result}%
  \ifx\@result\relax\def\@result{}%
  \else\edef\@result{\csname the\@result\endcsname}\fi%
  \protected@edef\@currentlabel{%
    [#1][\arabic{#1}][\@result]%
    \csname p@#1\endcsname\csname the#1\endcsname}}
%    \end{macrocode}
%
% The original \cmd{\refstepcounter}, as stored earlier in
% \cmd{\cref@old@refstepcounter}, already calls \H@refstepcounter if
% \package{hyperref} is loaded, and we just redefined it to store the
% type information. So we only need to change \cmd{\@currentlabel} in
% our \cmd{\refstepcounter} if an optional argument was supplied.
%    \begin{macrocode}
\def\refstepcounter@noarg#1{\cref@old@refstepcounter{#1}%
}
\def\refstepcounter@optarg[#1]#2{%
  \cref@old@refstepcounter{#2}%
  \expandafter\@cref@getlabel\@currentlabel\@nil{\@tmplabel}%
  \cref@resetby{#2}{\@tmpreset}%
  \ifx\@tmpreset\relax\def\@tmpreset{}%
  \else\edef\@tmpreset{\csname the\@tmpreset\endcsname}\fi%
  \protected@edef\@currentlabel{%
    [#1][\arabic{#2}][\@tmpreset]\@tmplabel}}
%    \end{macrocode}
%
% We again make \cmd{\appendix} redefine things so that the label type
% for chapters or sections is exceptionally overridden and set to
% ``appendix'' instead. But this time, it is \cmd{\H@refstepcounter}
% that needs to be redefined.
%    \begin{macrocode}
\renewcommand\appendix{%
  \cref@old@appendix%
  \@ifundefined{chapter}{%
    \def\H@refstepcounter##1{%
      \stepcounter{##1}%
      \cref@resetby{##1}{\@result}%
      \ifx\@result\relax\def\@result{}%
      \else\edef\@result{\csname the\@result\endcsname}\fi%
      \def\@tmpa{##1}%
      \def\@tmpb{section}%
      \ifx\@tmpa\@tmpb%
        \protected@edef\@currentlabel{%
          [appendix][\arabic{##1}][\@result]%
          \csname p@##1\endcsname\csname the##1\endcsname}%
      \else%
        \protected@edef\@currentlabel{%
          [##1][\arabic{##1}][\@result]%
          \csname p@##1\endcsname\csname the##1\endcsname}%
      \fi}%
  }{%
    \def\H@refstepcounter##1{%
      \stepcounter{##1}%
      \cref@resetby{##1}{\@result}%
      \ifx\@result\relax\def\@result{}%
      \else\edef\@result{\csname the\@result\endcsname}\fi%
      \def\@tmpa{##1}%
      \def\@tmpb{chapter}%
      \ifx\@tmpa\@tmpb%
        \protected@edef\@currentlabel{%
          [appendix][\arabic{##1}][\@result]%
          \csname p@##1\endcsname\csname the##1\endcsname}%
      \else%
        \protected@edef\@currentlabel{%
          [##1][\arabic{##1}][\@result]%
          \csname p@##1\endcsname\csname the##1\endcsname}%
      \fi}%
  }%
}
%    \end{macrocode}
%
% Redefine \cmd{\cref} and all the others to allow starred variants,
% which don't create hyper-links. The starred variants simply set a
% flag, which is tested in \cmd{\@@setcref} and \cmd{\@@setrangeref}
% (below).
%    \begin{macrocode}
\newif\if@crefstarred
\DeclareRobustCommand{\cref}{%
  \@ifstar{\@crefstar{cref}}{\@crefnostar{cref}}}
\DeclareRobustCommand{\Cref}{%
  \@ifstar{\@crefstar{Cref}}{\@crefnostar{Cref}}}
\DeclareRobustCommand{\crefrange}{%
  \@ifstar{\@crefrangestar{cref}}{\@crefrangenostar{cref}}}
\DeclareRobustCommand{\Crefrange}{%
  \@ifstar{\@crefrangestar{Cref}}{\@crefrangenostar{Cref}}}
\def\@crefnostar#1#2{\@cref{#1}{#2}}
\def\@crefstar#1#2{%
  \@crefstarredtrue\@crefnostar{#1}{#2}\@crefstarredfalse}
\def\@crefrangenostar#1#2#3{\@setcrefrange{#2}{#3}{#1}{}}
\def\@crefrangestar#1#2#3{%
  \@crefstarredtrue\@crefrangenostar{#1}{#2}{#3}\@crefstarredfalse}
%    \end{macrocode}
%
% Redefine \cmd{\@@setcref} and \cmd{\@@setrangeref} to create
% hyper-links (unless the starred flag is set), using the extra
% arguments supplied in \cmd{\r@\meta{label}} (via the aux file) by
% \package{hyperref}.
%    \begin{macrocode}
\def\@@setcref#1#2{%
  \cref@getlabel{#2}{\@tmplabel}%
  \if@crefstarred%
    #1{\@tmplabel}{}{}%
  \else%
    \edef\@tmplink{\cref@hyperref{#2}}%
    #1{\@tmplabel}{\hyper@linkstart{link}{\@tmplink}}{\hyper@linkend}%
  \fi}
\def\@@setcrefrange#1#2#3{%
  \cref@getlabel{#2}{\@labela}%
  \cref@getlabel{#3}{\@labelb}%
  \if@crefstarred%
    #1{\@labela}{\@labelb}{}{}{}{}%
  \else%
    \edef\@linka{\cref@hyperref{#2}}%
    \edef\@linkb{\cref@hyperref{#3}}%
    #1{\@labela}{\@labelb}%
      {\hyper@linkstart{link}{\@linka}}{\hyper@linkend}%
      {\hyper@linkstart{link}{\@linkb}}{\hyper@linkend}%
  \fi}
}{}
%    \end{macrocode}
%
%
% \subsection{\package{ntheorem} Support}
% If \package{ntheorem} is loaded, we need to modify its theorem
% referencing features so that they work with \package{cleveref}.
%    \begin{macrocode}
\@ifpackageloaded{ntheorem}{%
\PackageInfo{cleveref}{`ntheorem' support loaded}
\@ifpackagewith{ntheorem}{thref}{%
  \PackageWarning{cleveref}{`cleveref' supersedes `ntheorem's `thref'
    option}%
  \renewcommand{\thref}{\cref}}{}
%    \end{macrocode}
%
% Newer versions of \package{ntheorem} require a call to
% \cmd{\theorem@prework} when typesetting theorems. If an older
% version of \package{ntheorem} is being used, we just \cmd{\let} it
% to \cmd{\relax} to make sure it's defined.
%    \begin{macrocode}
\@ifundefined{theorem@prework}{\let\theorem@prework\relax}{}
%    \end{macrocode}
%
% We modify \package{ntheorem}'s version of the \cmd{\@thm} macro very
% slightly, to have it call \cmd{\refstepcounter} with an optional
% argument containing the theorem type.
%    \begin{macrocode}
\gdef\@thm#1#2#3{%
   \if@thmmarks%
     \stepcounter{end\InTheoType ctr}%
   \fi%
   \renewcommand{\InTheoType}{#1}%
   \if@thmmarks%
     \stepcounter{curr#1ctr}%
     \setcounter{end#1ctr}{0}%
   \fi%
   \refstepcounter[#1]{#2}%  <<<<<
   \theorem@prework%
   \thm@topsepadd \theorempostskipamount%
   \ifvmode \advance\thm@topsepadd\partopsep\fi%
   \trivlist%
   \@topsep \theorempreskipamount%
   \@topsepadd \thm@topsepadd%
   \advance\linewidth -\theorem@indent%
   \advance\@totalleftmargin \theorem@indent%
   \parshape \@ne \@totalleftmargin \linewidth%
   \@ifnextchar[{\@ythm{#1}{#2}{#3}}{\@xthm{#1}{#2}{#3}}%]
 }
%    \end{macrocode}
%
% Default formats for new theorem-like environments defined by ntheorem.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifundefined{cref@lemma@name}{%
    \@crefname{cref}{lemma}{lemma}{lemmas}%
    \@crefname{Cref}{lemma}{Lemma}{Lemmas}}{}%
  \@ifundefined{cref@corollary@name}{%
    \@crefname{cref}{corollary}{corollary}{corollaries}%
    \@crefname{Cref}{corollary}{Corollary}{Corollaries}}{}%
  \@ifundefined{cref@proposition@name}{%
    \@crefname{cref}{proposition}{proposition}{propositions}%
    \@crefname{Cref}{proposition}{Proposition}{Proposition}}{}%
  \@ifundefined{cref@definition@name}{%
    \@crefname{cref}{definition}{definition}{definitions}%
    \@crefname{Cref}{definition}{Definition}{Definitions}}{}%
  \@ifundefined{cref@result@name}{%
    \@crefname{cref}{result}{result}{results}%
    \@crefname{cref}{result}{Result}{Results}}{}%
%    \end{macrocode}
% Define any undefined formats using the components.
%    \begin{macrocode}
  \@ifundefined{cref@lemma@format}{%
    \@crefdefineformat{lemma}}{}%
  \@ifundefined{crefrange@lemma@format}{%
    \@crefrangedefineformat{lemma}}{}%
  \@ifundefined{cref@lemma@format@first}{%
    \@crefdefinemultiformat{lemma}}{}%
  \@ifundefined{crefrange@lemma@format@first}{%
    \@crefrangedefinemultiformat{lemma}}{}%
  \@ifundefined{cref@corollary@format}{%
    \@crefdefineformat{corollary}}{}%
  \@ifundefined{crefrange@corollary@format}{%
    \@crefrangedefineformat{corollary}}{}%
  \@ifundefined{cref@corollary@format@first}{%
    \@crefdefinemultiformat{corollary}}{}%
  \@ifundefined{crefrange@corollary@format@first}{%
    \@crefrangedefinemultiformat{corollary}}{}%
  \@ifundefined{cref@definition@format}{%
    \@crefdefineformat{definition}}{}%
  \@ifundefined{crefrange@definition@format}{%
    \@crefrangedefineformat{definition}}{}%
  \@ifundefined{cref@definition@format@first}{%
    \@crefdefinemultiformat{definition}}{}%
  \@ifundefined{crefrange@definition@format@first}{%
    \@crefrangedefinemultiformat{definition}}{}%
  \@ifundefined{cref@result@format}{%
    \@crefdefineformat{result}}{}%
  \@ifundefined{crefrange@result@format}{%
    \@crefrangedefineformat{result}}{}%
  \@ifundefined{cref@result@format@first}{%
    \@crefdefinemultiformat{result}}{}%
  \@ifundefined{crefrange@result@format@first}{%
    \@crefrangedefinemultiformat{result}}{}%
}%
}{}
%    \end{macrocode}
%
%
% \subsection{Poor Man's \package{cleveref}}
%    \begin{macrocode}
\DeclareOption{poorman}{%
\PackageInfo{cleveref}{option `poorman' loaded}
%    \end{macrocode}
%
%
% Define global macro \cmd{\cref@text} to store the text produced by
% the \cmd{\cref} commands, and open an output stream for writing the
% script before starting to process to document body.
%    \begin{macrocode}
\edef\cref@text{}
\AtBeginDocument{%
  \newwrite\@crefscript%
  \immediate\openout\@crefscript=\jobname.sed%
}
%    \end{macrocode}
% 
%
% After processing the document body, we re-read in the temporary
% script file, and write it out again to the final sed script file,
% escaping regexp special characters in the process. The escaping is
% carried out by turning the regexp special characters into active
% characters, and defining them to expand to their escaped form. This
% involves a lot of juggling of catcodes and lccodes!
%
% Both \cmd{\DeclareOption} and \cmd{\AtEndDocument} store their
% arguments in token lists, so all the following \TeX code is already
% tokenised long before it is expanded and evaluated. Thus there is no
% (easy) way to change the catcodes of the characters appearing here
% before they are tokenised. In one way this is convenient: the
% catcode changes we make don't ``take'' until evaluated, so we can
% continue to use the standard \TeX characters ($\backslash$, \{, \}
% etc.) even after the lines containing the catcode commands. But in
% another, more significant, way, it is very inconvenient: it makes it
% difficult to define the regexp special characters as active
% characters, since it's impossible to directly create tokens with the
% correct char- and catcodes.
%
% We get around this by creating the unusual charcode/catcode
% combinations using the \cmd{\lowercase} trick (\cmd{\lowercase}
% changes the charcodes of all characters in its argument to their
% lccodes, but \emph{leaves} their catcodes alone). That way, the
% argument of \cmd{\AtEndDocument} is tokenised correctly, and when it
% comes to be expanded and evaluated, the \cmd{\lowercase} commands
% create tokens with the correct char- and catcodes.
%    \begin{macrocode}
\AtEndDocument{%
  \immediate\closeout\@crefscript%
  \newread\@crefscript%
  \immediate\openin\@crefscript=\jobname.sed%
  \begingroup%
    \newif\if@not@eof%
    \def\@eof{\par }%
%    \end{macrocode}
% Change catcodes of regexp special characters to make them active
% characters and define them to expand to their escaped forms. Change
% those of \TeX special characters to make them normal letters.
%    \begin{macrocode}
    \catcode`.=13 \catcode`[=13 \catcode`]=13
    \catcode`^=13 \catcode`$=13 %$
    \catcode`\=0 \catcode`<=1 \catcode`>=2
    \catcode`\\=13 \catcode`\{=12 \catcode`\}=12 \catcode`_=12
    \lccode`/=92
    \lccode`~=92\lowercase{\def~{\string/\string/}}%
    \lccode`~=46\lowercase{\def~{\string/\string.}}%
    \lccode`~=91\lowercase{\def~{\string/\string[}}%
    \lccode`~=93\lowercase{\def~{\string/\string]}}%
    \lccode`~=94\lowercase{\def~{\string/\string^}}%
    \lccode`~=36\lowercase{\def~{\string/\string$}}% $
    \lccode`~=0 \lccode`/=0 \catcode`~=12
%    \end{macrocode}
% Read lines from the temporary script file, expand them to escape
% regexp special characters, and store them in \cmd{\cref@text}.
%    \begin{macrocode}
    \def\cref@text{}%
    \immediate\read\@crefscript to \@tmpa%
    \ifx\@tmpa\@eof%
      \@not@eoffalse%
    \else%
      \@not@eoftrue%
      \edef\@tmpa{\@tmpa}%
    \fi%
    \@whilesw\if@not@eof\fi{%
      \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
        \@tmpa^^J}%
      \immediate\read\@crefscript to \@tmpa%
      \ifx\@tmpa\@eof%
        \@not@eoffalse%
      \else%
        \@not@eoftrue%
        \edef\@tmpa{\@tmpa}%
      \fi}%
  \endgroup%
  \immediate\closein\@crefscript%
%    \end{macrocode}
% Add some rules to remove likely \package{cleveref} preamble
% commands. We use the \cmd{\lowercase} trick again for writing the
% $\backslash$, \{ and \} characters. (This could be done in other
% ways, but since we're in \cmd{\lowercase} mood, why not go with it!)
%    \begin{macrocode}
  \begingroup%
    \lccode`|=92 \lccode`<=123 \lccode`>=125 \lccode`C=67
    \lowercase{\edef\@tmpa{s/||usepackage|(|[.*|]|)|?<cleveref>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{s/||[cC]refformat<.*><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{s/||[cC]refrangeformat<.*><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{s/||[cC]refmultiformat<.*><.*><.*><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{%
        s/||[cC]refrangemultiformat<.*><.*><.*><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{s/||[cC]refname<.*><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{s/||[cC]reflabelformat<.*><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{s/||[cC]refrangelabelformat<.*><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{s/||[cC]refdefaultlabelformat<.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{%
        s/||renewcommand<||crefpairconjunction><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{%
        s/||renewcommand<||crefpairgroupconjunction><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{%
        s/||renewcommand<||crefmiddleconjunction><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{%
        s/||renewcommand<||crefmiddlegroupconjunction><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{%
        s/||renewcommand<||creflastconjunction><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{%
        s/||renewcommand<||creflastgroupconjunction><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{s/||renewcommand<||[cC]ref><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
    \lowercase{\edef\@tmpa{s/||renewcommand<||[cC]refrange><.*>//g}}%
    \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
      \@tmpa^^J}%
  \endgroup%
%    \end{macrocode}
% Overwrite the script file with the new, escaped regexp rules.
%    \begin{macrocode}
  \newwrite\@crefscript%
  \immediate\openout\@crefscript=\jobname.sed%
  \immediate\write\@crefscript{\cref@text}%
  \immediate\closeout\@crefscript%
}
%    \end{macrocode}
%
%
% Redefine the user-level referencing commands so that they write a
% substitution rule for the reference to the script, as well as
% typesetting the reference itself.
%    \begin{macrocode}
\@ifpackageloaded{hyperref}{%
  \def\@crefnostar#1#2{%
    \edef\cref@text{}%
    \@cref{#1}{#2}%
    \cref@writescript{%
      \expandafter\string\csname#1\endcsname\string{#2\string}}}
  \def\@crefrangenostar#1#2#3{%
    \edef\cref@text{}%
    \@setcrefrange{#2}{#3}{#1}{}%
    \cref@writescript{%
      \expandafter\string\csname#1range\endcsname%
      \string{#2\string}\string{#3\string}}}%
}{%
  \DeclareRobustCommand{\cref}[1]{%
    \edef\cref@text{}%
    \@cref{cref}{#1}%
    \cref@writescript{\string\cref\string{#1\string}}}
  \DeclareRobustCommand{\Cref}[1]{%
    \edef\cref@text{}%
    \@cref{Cref}{#1}%
    \cref@writescript{\string\Cref\string{#1\string}}}
  \DeclareRobustCommand{\crefrange}[2]{%
    \edef\cref@text{}%
    \@setcrefrange{#1}{#2}{cref}{}%
    \cref@writescript{%
      \string\crefrange\string{#1\string}\string{#2\string}}}
  \DeclareRobustCommand{\Crefrange}[2]{%
    \edef\cref@text{}%
    \@setcrefrange{#1}{#2}{Cref}{}%
    \cref@writescript{%
      \string\Crefrange\string{#1\string}\string{#2\string}}}%
}
%    \end{macrocode}
%
% 
% The \cmd{\cref@writescript} utility macro does the actual writing of
% the substitution rule to the script.
%    \begin{macrocode}
\def\cref@writescript#1{%
  \edef\@tmpa{\cref@getmeaning{\cref@text}}%
  \immediate\write\@crefscript{s/#1/\@tmpa/g}}
%    \end{macrocode}
%
%
% Redefine \cmd{\@@setcref} and \cmd{\@@setrangeref}, as well as the
% conjunction macros \cmd{\@setcref@middlegroupconjunction},
% \cmd{\@setcref@lastgroupconjunction} and
% \cmd{\@setcref@pairgroupconjunction}, to append text they typeset to
% \cmd{\cref@text}, as well as actually doing the typesetting.
%    \begin{macrocode}
\let\old@@setcref\@@setcref
\let\old@@setcrefrange\@@setcrefrange
\def\cref@getmeaning#1{\expandafter\@cref@getmeaning\meaning#1\@nil}
\def\@cref@getmeaning#1->#2\@nil{#2}
\def\@@setcref#1#2{%
  \old@@setcref{#1}{#2}%
  \expandafter\g@addto@macro\expandafter{%
    \expandafter\cref@text\expandafter}\expandafter{%
    #1{\ref{#2}}{}{}}}
\def\@@setcrefrange#1#2#3{%
  \old@@setcrefrange{#1}{#2}{#3}%
  \expandafter\g@addto@macro%
    \expandafter{\expandafter\cref@text\expandafter}%
    \expandafter{#1{\ref{#2}}{\ref{#3}}{}{}{}{}}}
\def\@setcref@pairgroupconjunction{%
  \crefpairgroupconjunction%
  \expandafter\g@addto@macro%
    \expandafter{\expandafter\cref@text\expandafter}%
    \expandafter{\crefpairgroupconjunction}}
\def\@setcref@middlegroupconjunction{%
  \crefmiddlegroupconjunction%
  \expandafter\g@addto@macro%
    \expandafter{\expandafter\cref@text\expandafter}%
    \expandafter{\crefmiddlegroupconjunction}}
\def\@setcref@lastgroupconjunction{%
  \creflastgroupconjunction%
  \expandafter\g@addto@macro%
    \expandafter{\expandafter\cref@text\expandafter}%
    \expandafter{\creflastgroupconjunction}}
} % end of poorman option
%    \end{macrocode}
%
%
% Process options.
%    \begin{macrocode}
\ProcessOptions\relax
%    \end{macrocode}
%
% \Finale
\endinput
%%
%% End of file `cleveref.dtx'.

%%% Local Variables: 
%%% mode: doctex
%%% TeX-master: t
%%% End: