summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/cleveref/cleveref.dtx
blob: 43db80451bce4ea24f86e07b7d6d56a0c5f1fcad (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
% \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/11/06 v0.6 Automatic cross-reference formatting]
%
%<*driver>
\documentclass[draft]{ltxdoc}
\usepackage{url}
\usepackage{cleveref}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%%\newcommand{\package}[1]{\texttt{#1.sty}}
%\newcommand{\package}[1]{\texttt{#1}}
\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{1914}
%% \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"}
% \changes{v0.5}{2007/31/05}{Added poorman option}
% \changes{v0.5}{2007/11/06}{Poorman no longer writes extra .tmp file}
%
% \GetFileInfo{cleveref.sty}
% 
% \def\fileversion{0.6}
% \def\filedate{11/06/2007}
%
% \title{The \textsf{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.). 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! Or, to take a
% more charitable attitude, that the programmer is pleased with the
% sophisticated processing that he has managed to encapsulate in code,
% relieving the user of some complicated by tedious task. 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 valid with respect 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
% facility by allowing references to be formatted automatically
% according to the type of object they refer to (chapter, section,
% equation, theorem, etc.). It can also automatically format
% references to multiple labels.
%
% In standard \LaTeX, you have almost certainly found yourself
% writing things like |Eq.~(\ref{eq1})| and |Theorem~\ref{thm1}| over
% and over again. This isn't just tedious. 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|.
%
% \package{cleveref} 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 typesetting of equation references, you only
% have to change one preamble definition. If you later decide to
% change 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 saving repetitively typing the same text
% for each and every reference.
%
% A number of other packages provide similar features, most notably
% \package{varioref}, \package{fancyref} and (for theorem-like
% environments) \package{ntheorem} (with the \option{thref}
% option). 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}. Most of
% the time, 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.
%
% 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 telling it the label
% type. \package{cleveref} essentially extends the \package{ntheorem}
% theorem referencing feature to work for all objects. (Although the
% implementation details differ, and \package{cleveref} can be used
% independently of \package{ntheorem}.)  \package{cleveref} provides a
% number extra features too, such as multiple references, automatic
% collapsing of reference ranges, and control over the placement of
% hyper-links.
%
%
%
% \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}).
%
%
%
% \subsection{Typesetting References}
%
% \DescribeMacro\cref
% To automatically typeset a reference according to the type of object
% referred to, simply refer to it using |\cref{|\meta{label}|}|.
% \package{cleveref} imposes just one restriction on the names of
% labels: they are no longer allowed to contain commas ``,''. These
% can instead be used to typeset multiple references (see below).
%
% \DescribeMacro\Cref
% As it is very difficult for \LaTeX to determine whether a
% 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
% customised independently (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 correct
%   behaviour.}, so you can still use it to typeset the formatted
% label counter alone, without any additional text or formatting. If
% you would like to use \package{cleveref}'s enhanced features, but
% want to continue to use |\ref| for referencing, simply redefine the
% |\ref| command in your document's preamble, thus:
% \begin{quote}
%   |\renewcommand{\ref}{\cref}|
% \end{quote}
% Similarly, if you decide you would like all references capitalised,
% you can redefine the |\cref| command:
% \begin{quote}
%   |\renewcommand{\cref}{\Cref}|
% \end{quote}
%
% \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
% into 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}
%
%
% \subsection{Customising the Reference Formats}
% \label{sec:customising}
%
% \subsubsection{Single References}
% \label{sec:ref_format}
% 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 reference types. But if you
% don't like them, or are writing in a different language, or you are
% using an environment for which no default format is defined, you
% will need to define your own.
%
% 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. References to undefined labels still produce
% a ``reference undefined'' warning and are typeset as a double
% question mark, as usual.
%
% \DescribeMacro\crefformat
% \DescribeMacro\Crefformat
% Reference formats for \emph{single} references are defined or
% redefined using the |\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}
%
% 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|. Obviously, this
% will only work properly if the formats have a letter at the
% start. If the first letter is a special construct, such as an
% accented character, you will need to surround everything whose case
% should be changed by braces. If the first thing in the format is not
% a letter (e.g.\ if it is a \LaTeX command, or a hyper-link
% argument, see below), 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
% exception is theorem-like environments when the \package{ntheorem}
% package is loaded, for which \meta{type} should instead be the
% environment name (lemma, corollary, definition, etc.). This is
% because \package{ntheorem} allows different theorem-like
% environments to use a common counter.
%
% 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 will mean that no hyper-link will be created
% when \package{hyperref} is used.
%
% 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-links.
% Note that the hyper-link arguments are \emph{not} letters, so if you
% want to make the ``Eq.'' part of the hyper-link by putting |#2| at
% the start of the formatting code, you will have to define both
% capitalisation variants independently:
% \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|. You do not have to define it if
% you don't plan to use any reference ranges, i.e. you never use the
% |\crefrange| and |\Crefrange| commands, and you never use multiple
% references (see \cref{sec:multiple_ref_format}). However, you will
% get ``reference format undefined'' errors if you later change your
% mind and forget to define the format. 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: the \meta{type} is usually the name of the
% counter, except for theorem-like environments, and if the
% corresponding 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,
% \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|.  If you don't use any multiple references
% for a particular reference type, you are not obliged to define the
% multi-formats. However, you will get ``reference format undefined''
% errors if you later decide to use a multiple reference without
% ensuring the multi-formats are defined. You will also need to define
% \emph{all} the other reference formats (see
% \cref{sec:ref_format,sec:ref_range_format}), including the reference
% range formats, even if you never use the |\crefrange| and
% |\Crefrange| commands.
%
% The commands all take four arguments: the reference type, the format
% for the first reference in a list, the format for the middle
% references in a list, and the format for the last reference in a
% list.
% \begin{quote}
%   |\crefmultiformat{|\meta{type}|}{|\meta{first}|}{|\meta{middle}|}{|\meta{last}|}|\\
%   |\Crefmultiformat{|\meta{type}|}{|\meta{first}|}{|\meta{middle}|}{|\meta{last}|}|\\
%   |\crefrangemultiformat{|\meta{type}|}{|\meta{first}|}{|\meta{middle}|}{|\meta{last}|}|\\
%   |\Crefrangemultiformat{|\meta{type}|}{|\meta{first}|}{|\meta{middle}|}{|\meta{last}|}|
% \end{quote}
%
% The same considerations apply to the \meta{type} and formatting
% arguments \meta{first}, \meta{middle} and \meta{last} as for the
% \meta{format} argument of |\crefformat| and |\Crefformat| or
% |\crefrangeformat| and |\Crefrangeformat|, including the meaning of
% the arguments that should appear in the formatting code. However,
% when the corresponding other-capitalisation variant is automatically
% defined, only the first letter of the \meta{first} argument is
% upper- or lower-cased.
%
% Be careful to get the spaces at the beginning and end of the
% formatting code correct: the \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, with the
% default format
% \begin{quote}
%   |\crefmultiformat{equation}%|\\
%   |{Eqs.~(#2#1#3)}{, (#2#1#3)}{ and~(#2#1#3)}|\\
% \end{quote}
% the \meta{middle} arguments should \emph{not} have a space at the
% beginning, whereas the \meta{last} arguments \emph{should} have a
% space.
%
% \DescribeMacro{\crefmiddleconjunction}
% \DescribeMacro{\creflastconjunction}
% The |\crefmiddleconjunction| and |\creflastconjunction| commands are
% used in between sub-lists of different types of references that
% occur in a multiple reference. If there are two or more reference
% types in the list, |\creflastconjunction| is used between the
% penultimate and final group of references, whereas
% |\crefmiddleconjunction| is used between all others. You can
% redefine them with |\renewcommand| in the usual way:
% \begin{quote}
%   |\renewcommand{\crefmiddleconjunction}{|\meta{conjunction}|}|\\
%   |\renewcommand{\creflastconjunction}{|\meta{conjunction}|}|
% \end{quote}
% 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 (assuming the default |\crefformat|s) as
% \begin{quote}
%   Eqs.~(1)--(3)|\crefmiddleconjunction| Theorems~1\\
%   and~2|\crefmiddleconjunction| Fig.~1|\creflastconjunction|
%   Theorem~3
% \end{quote}
%
%
% \subsection{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 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-reference numbers 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 script to remove the \package{cleveref} dependency.
%
% The script is written to the same directory as the \LaTeX source
% file, and given the same name as the 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} > \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.)
%
%
%
% \section{Interaction with Other Packages}
% \label{sec:other_packages}
% Since \package{cleveref} redefines many internal commands involved
% in \LaTeX's referencing system, it can interact badly with other
% packages that do the same. In general, \package{cleveref} should be
% loaded \emph{last}, and the appropriate options must be supplied if
% the \package{hyperref} and/or \package{ntheorem} packages are also
% used. E.g.
% \begin{quote}
%   |\usepackage{hyperref}|\\
%   |\usepackage[hyperref]{cleveref}|
% \end{quote}
% or
% \begin{quote}
%   |\usepackage{ntheorem}|\\
%   |\usepackage[ntheorem]{cleveref}|
% \end{quote}
% or
% \begin{quote}
%   |\usepackage{hyperref}|\\
%   |\usepackage[hyperref]{ntheorem}|\\
%   |\usepackage[hyperref,ntheorem]{cleveref}|
% \end{quote}
%
% \package{varioref}'s enhanced referencing features (which 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 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}
% Note that you can still use the other features of \package{varioref}
% and \package{ntheorem} whilst using \package{cleveref}.
%
% Other packages which alter the \LaTeX referencing system are
% unlikely to work properly with \package{cleveref}.
%
%
%
% \section{Known Bugs}
% \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 \package{cleveref}'s referencing features currently may or may
%   not work properly with references to list items. (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 \package{cleveref} provides no \package{babel} support.
% \end{itemize}
%
%
%
% \section{Future Improvements}
% \begin{itemize}
% \item Allow selective disabling of reference-range collapsing in
%   multiple references, e.g.\ by the addition of an extra comma
%   between the labels in the label list.
% \item Allow reference-range collapsing to be disabled entirely via a
%   package option
% \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 an \cmd{\cref} command which uses 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 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 required package).
%
%
% \subsection{Redefinitions of \LaTeX kernel macros}
% We store the original \cmd{\refstepcounter} in
% \cmd{\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\old@refstepcounter\refstepcounter
\def\refstepcounter%
  {\@ifnextchar[{\refstepcounter@optarg}{\refstepcounter@noarg}}%]
\def\refstepcounter@noarg#1{%
  \old@refstepcounter{#1}%
  \reset@by{#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{%
  \old@refstepcounter{#2}%
  \reset@by{#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}
%
% 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.
%    \begin{macrocode}
\def\cref@label#1#2{\@result}
\AtBeginDocument{%
  \def\ref#1{%
    \expandafter\ifx\csname r@#1\endcsname\relax%
      \let\@result\relax%
    \else%
      \cref@getlabel{#1}{\@result}%
    \fi%
  \expandafter\@setref\csname r@#1\endcsname{\cref@label}{#1}}%
}
%    \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 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\@tempa\csname r@#1\endcsname%
  \edef\@tempa{\expandafter\@firstoftwo\@tempa}%
  \expandafter\@cref@getlabel\@tempa\@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\@tempa\csname r@#1\endcsname%
  \edef\@tempa{\expandafter\@firstoftwo\@tempa}%
  \expandafter\@cref@gettype\@tempa\@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\@tempa\csname r@#1\endcsname%
  \edef\@tempa{\expandafter\@firstoftwo\@tempa}%
  \expandafter\@cref@getcounter\@tempa\@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\@tempa\csname r@#1\endcsname%
  \edef\@tempa{\expandafter\@firstoftwo\@tempa}%
  \expandafter\@cref@getprefix\@tempa\@nil#2}
\def\@cref@getprefix{\@ifnextchar[%]
  \@@cref@getprefix{\@@cref@getprefix[][][]}}
\def\@@cref@getprefix[#1][#2][#3]#4\@nil#5{\def#5{#3}}
%    \end{macrocode}
%
% We treate 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\stack@init#1{\def#1{\@nil}}
\def\stack@top#1{\expandafter\stack@top@aux#1}
\def\stack@top@aux#1,#2\@nil{#1}
\def\stack@pop#1{\expandafter\stack@pop@aux#1#1}
\def\stack@pop@aux#1,#2\@nil#3{\def#3{#2\@nil}}
\def\stack@push#1#2{\expandafter\stack@push@aux\expandafter{#2}{#1}{#2}}
\def\stack@push@aux#1#2#3{\def#3{#2,#1}}
\def\stack@pull#1#2{\expandafter\stack@pull@aux#2{#1}{#2}}
\def\stack@pull@aux#1\@nil#2#3{\def#3{#1#2,\@nil}}
\newif\ifstackempty
\newif\ifstackfull
\def\isstackempty#1{%
  \expandafter\ifx#1\@nil\stackemptytrue%
  \else\stackemptyfalse\fi}
\def\isstackfull#1{%
  \expandafter\ifx#1\@nil\stackfullfalse%
  \else\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 in.
%    \begin{macrocode}
\newif\ifinresetlist
\def\isinresetlist#1#2{%
  \def\@counter{#1}%
  \begingroup%
    \def\@elt##1{##1,}%
    \expandafter\ifx\csname cl@#2\endcsname\relax%
      \gdef\@resetstack{,\@nil}%
    \else%
      \xdef\@resetstack{\csname cl@#2\endcsname\noexpand\@nil}%
    \fi%
  \endgroup%
  \isstackfull{\@resetstack}%
  \@whilesw\ifstackfull\fi{%
    \edef\@nextcounter{\stack@top{\@resetstack}}%
    \ifx\@nextcounter\@counter%
      \stackfullfalse%
    \else%
      \let\@nextcounter\relax%
      \stack@pop{\@resetstack}%
      \isstackfull{\@resetstack}%
    \fi}%
  \ifx\@nextcounter\relax%
    \inresetlistfalse%
  \else%
    \inresetlisttrue%
  \fi}
%
\def\reset@by#1#2{%
  \isinresetlist{#1}{subsubsubsection}%
  \ifinresetlist%
    \def#2{subsubsubsection}%
  \else%
    \isinresetlist{#1}{subsubsection}%
    \ifinresetlist%
      \def#2{subsubsection}%
    \else%
      \isinresetlist{#1}{subsection}%
      \ifinresetlist%
        \def#2{subsection}%
      \else%
        \isinresetlist{#1}{section}%
        \ifinresetlist%
          \def#2{section}%
        \else%
          \isinresetlist{#1}{chapter}%
          \ifinresetlist%
            \def#2{chapter}%
          \else%
            \isinresetlist{#1}{part}%
            \ifinresetlist%
              \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}
\newcommand{\cref}[1]{\@cref{cref}{#1}}
\newcommand{\Cref}[1]{\@cref{Cref}{#1}}
\newcommand{\crefrange}[2]{\@setcrefrange{#1}{#2}{cref}{}}
\newcommand{\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%
  \newif\if@firstgroup%
  \stack@init{\@refstack}%
  \stack@push{#2}{\@refstack}%
  \@firstgrouptrue%
  \isstackfull{\@refstack}%
%    \end{macrocode}
% Loop until the reference stack is empty.
%    \begin{macrocode}
  \@whilesw\ifstackfull\fi{%
    \stack@init{\@refsubstack}%
    \edef\@nextref{\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\stack@pull\expandafter{\@nextref}{\@refsubstack}%
      \stack@pop{\@refstack}%
      \isstackempty{\@refstack}%
      \ifstackempty%
        \def\@nexttype{\relax}%
      \else%
        \edef\@nextref{\stack@top{\@refstack}}%
        \expandafter\ifx\csname r@\@nextref\endcsname\relax%
          \def\@currenttype{\@undefined}%
        \else%
          \expandafter\cref@gettype\expandafter%
            {\@nextref}{\@currenttype}%
        \fi%
      \fi}%
%    \end{macrocode}
%   Typeset appropriate conjunction between groups of reference types.
%    \begin{macrocode}
    \if@firstgroup%
    \else%
      \isstackfull{\@refstack}%
      \ifstackfull%
        \@setcref@middleconjunction%
      \else%
        \@setcref@lastconjunction%
      \fi%
    \fi%
    \@firstgroupfalse%
%    \end{macrocode}
%   Process first group of consecutive references from substack.
%    \begin{macrocode}
    \edef\@nextref{\stack@top{\@refsubstack}}%
    \stack@pop{\@refsubstack}%
%    \end{macrocode}
%   If the substack only contains one reference, typeset it,
%    \begin{macrocode}
    \isstackempty{\@refsubstack}%
    \ifstackempty%
      \expandafter\@setcref\expandafter{\@nextref}{#1}{}%
%    \end{macrocode}
%   otherwise, find end of consecutive references.
%    \begin{macrocode}
    \else%
      \edef\@beginref{\@nextref}%
      \let\@endref\relax%
      \edef\@nextref{\stack@top{\@refsubstack}}%
      \count@consecutive=1%
      \expandafter\ifx\csname r@\@beginref\endcsname\relax%
        \refconsecutivefalse%
      \else%
        \expandafter\ifx\csname r@\@nextref\endcsname\relax%
          \refconsecutivefalse%
        \else%
          \edef\@tmpa{{\@beginref}{\@nextref}}%
          \expandafter\isrefconsecutive\@tmpa%
        \fi%
      \fi%
      \@whilesw\ifrefconsecutive\fi{%
        \advance\count@consecutive 1%
        \let\@endref\@nextref%
        \stack@pop{\@refsubstack}%
        \isstackempty{\@refsubstack}%
        \ifstackempty%
          \refconsecutivefalse%
        \else%
          \edef\@nextref{\stack@top{\@refsubstack}}%
          \expandafter\ifx\csname r@\@nextref\endcsname\relax%
            \refconsecutivefalse%
          \else%
            \edef\@tmpa{{\@endref}{\@nextref}}%
            \expandafter\isrefconsecutive\@tmpa%
          \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\stack@push\expandafter{\@endref}{\@refsubstack}%
%    \end{macrocode}
%       otherwise, typeset a reference range.
%    \begin{macrocode}
        \else
          \edef\@tmpa{{\@beginref}{\@endref}}%
          \ifstackempty%
            \expandafter\@setcrefrange\@tmpa{#1}{}%
          \else%
            \expandafter\@setcrefrange\@tmpa{#1}{@first}%
          \fi%
        \fi%
      \fi%
%    \end{macrocode}
%     Loop until substack is empty.
%    \begin{macrocode}
      \isstackfull{\@refsubstack}%
      \@whilesw\ifstackfull\fi{%
        \edef\@beginref{\stack@top{\@refsubstack}}%
        \stack@pop{\@refsubstack}%
        \let\@endref\relax%
%    \end{macrocode}
%       If substack only contains only one reference, typeset it,
%    \begin{macrocode}
        \isstackempty{\@refsubstack}%
        \ifstackempty%
          \expandafter\@setcref\expandafter{\@beginref}{#1}{@last}%
%    \end{macrocode}
%       otherwise, find end of consecutive references.
%    \begin{macrocode}
        \else%
          \edef\@nextref{\stack@top{\@refsubstack}}%
          \count@consecutive=1%
          \expandafter\ifx\csname r@\@nextref\endcsname\relax%
            \refconsecutivefalse%
          \else%
            \edef\@tmpa{{\@beginref}{\@nextref}}%
            \expandafter\isrefconsecutive\@tmpa%
          \fi%
          \@whilesw\ifrefconsecutive\fi{%
            \advance\count@consecutive 1%
            \let\@endref\@nextref%
            \stack@pop{\@refsubstack}%
            \isstackempty{\@refsubstack}%
            \ifstackempty%
              \refconsecutivefalse%
            \else%
              \edef\@nextref{\stack@top{\@refsubstack}}%
              \expandafter\ifx\csname r@\@nextref\endcsname\relax%
              \refconsecutivefalse%
              \else%
                \edef\@tmpa{{\@endref}{\@nextref}}%
                \expandafter\isrefconsecutive\@tmpa%
              \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}
          \isstackempty{\@refsubstack}%
          \ifstackempty%
            \def\@pos{@last}%
          \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%
              \edef\@tmpa{{\@beginref}{#1}{\@pos}}%
              \expandafter\@setcref\@tmpa%
%    \end{macrocode}
%           otherwise, typeset a reference range.
%    \begin{macrocode}
            \else
              \edef\@tmpa{{\@beginref}{\@endref}{#1}{\@pos}}%
              \expandafter\@setcrefrange\@tmpa%
            \fi%
          \fi%
        \fi%
        \isstackfull{\@refsubstack}%
      }% end loop over reference substack
    \fi%
    \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\ifrefconsecutive%
\def\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{\refconsecutivefalse}%
    \ifx\refa@prefix\refb@prefix%
      \ifnum\refa@counter=\refb@counter%
        \def\@after{\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{%
%    \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 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}
%    \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@middleconjunction{\crefmiddleconjunction}
\def\@setcref@lastconjunction{\creflastconjunction}
%    \end{macrocode}
%
%
%
% \subsection{Reference Format Customisation Commands}
% \cmd{\crefformat} et al.\ are user-level commands used to define the
% format of different reference types. They simply use the supplied
% argument(s) to define appropriately named formatting macro(s), 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}[4]{%
  \@crefmultiformat{cref}{#1}{#2}{#3}{#4}}
\newcommand{\Crefmultiformat}[4]{%
  \@crefmultiformat{Cref}{#1}{#2}{#3}{#4}}
\newcommand{\crefrangemultiformat}[4]{%
  \@crefrangemultiformat{crefrange}{#1}{#2}{#3}{#4}}
\newcommand{\Crefrangemultiformat}[4]{%
  \@crefrangemultiformat{Crefrange}{#1}{#2}{#3}{#4}}
%    \end{macrocode}
% The utility macros do the real work, by using the first argument
% (``cref'', ``Cref'', ``crefrange'' or ``Crefrange'') to determine
% how to define the corresponding command with the other
% capitalisation.
%    \begin{macrocode}
\def\@crefformat#1#2#3{%
  \expandafter\def\csname #1@#2@format\endcsname##1##2##3{#3}%
%    \end{macrocode}
% Note that these \cmd{@tmpa} macros makes use of the fact that the
% first character of \#1 is ``c'' for lower-case, ``C'' for
% upper-case.
%    \begin{macrocode}
  \def\@tmpa##1##2\@nil{%
    \if##1c \def\@other{C##2}%
    \else\def\@other{c##2}\fi}%
  \@tmpa#1\@nil%
  \@ifundefined{\@other @#2@format}{%
    \def\@tmpa##1##2\@nil{%
      \if##1c\def\@changecase{\MakeUppercase}%
      \else\def\@changecase{\Makelowercase}\fi}%
    \@tmpa#1\@nil%
    \newtoks\@toksa%
    \@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}}%
    \expandafter\the\expandafter\@toksa\expandafter{%
      \expandafter\MakeUppercase\@tmpa{##1}{##2}{##3}}%
    \@toksa={%
      \expandafter\def\csname\@other @#2@format\endcsname##1##2##3}%
    \expandafter\the\expandafter\@toksa\expandafter{%
      \@tmpa{##1}{##2}{##3}}%
  }{}%
}
%
\def\@crefrangeformat#1#2#3{%
  \expandafter\def\csname #1@#2@format\endcsname%
    ##1##2##3##4##5##6{#3}%
%    \end{macrocode}
% Note that these \cmd{@tmpa} macros make use of the fact that the
% first character of \#1 is ``c'' for lower-case, ``C'' for
% upper-case.
%    \begin{macrocode}
  \def\@tmpa##1##2\@nil{%
    \if##1c \def\@other{C##2}%
    \else\def\@other{c##2}\fi}%
  \@tmpa#1\@nil%
  \@ifundefined{\@other @#2@format}{%
    \def\@tmpa##1##2\@nil{%
      \if##1c\def\@changecase{\MakeUppercase}%
      \else\def\@changecase{\Makelowercase}\fi}%
    \@tmpa#1\@nil%
    \newtoks\@toksa%
    \@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}}%
    \expandafter\the\expandafter\@toksa\expandafter{%
      \expandafter\MakeUppercase\@tmpa{##1}{##2}{##3}{##4}{##5}{##6}}
    \@toksa={\expandafter\def%
      \csname\@other @#2@format\endcsname##1##2##3##4##5##6}%
    \expandafter\the\expandafter\@toksa\expandafter{%
      \@tmpa{##1}{##2}{##3}{##4}{##5}{##6}}%
  }{}%
}
%
\def\@crefmultiformat#1#2#3#4#5{%
  \expandafter\def\csname #1@#2@format@first\endcsname##1##2##3{#3}%
  \expandafter\def\csname #1@#2@format@middle\endcsname##1##2##3{#4}%
  \expandafter\def\csname #1@#2@format@last\endcsname##1##2##3{#5}%
%    \end{macrocode}
% Note that these \cmd{@tmpa} macros make use of the fact that the
% first character of \#1 is ``c'' for lower-case, ``C'' for
% upper-case.
%    \begin{macrocode}
  \newtoks\@toksa%
  \def\@tmpa##1##2\@nil{%
    \if##1c \def\@other{C##2}%
    \else\def\@other{c##2}\fi}%
  \@tmpa#1\@nil%
  \@ifundefined{\@other @#2@format@first}{%
    \def\@tmpa##1##2\@nil{%
      \if##1c\def\@changecase{\MakeUppercase}%
      \else\def\@changecase{\Makelowercase}\fi}%
    \@tmpa#1\@nil%
    \@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}}%
    \expandafter\the\expandafter\@toksa\expandafter{%
      \expandafter\MakeUppercase\@tmpa{##1}{##2}{##3}}%
    \@toksa={%
      \expandafter\def\csname\@other @#2@format@first\endcsname%
        ##1##2##3}%
    \expandafter\the\expandafter\@toksa\expandafter{%
      \@tmpa{##1}{##2}{##3}}%
  }{}
  \@ifundefined{\@other @#2@format@middle}{%
    \@toksa={%
      \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\let\csname\@other @#2@format@last\endcsname}%
    \expandafter\the\expandafter\@toksa%
      \csname #1@#2@format@last\endcsname%
  }{}%
}
%
\def\@crefrangemultiformat#1#2#3#4#5{%
  \expandafter\def\csname #1@#2@format@first\endcsname%
    ##1##2##3##4##5##6{#3}%
  \expandafter\def\csname #1@#2@format@middle\endcsname%
    ##1##2##3##4##5##6{#4}%
  \expandafter\def\csname #1@#2@format@last\endcsname%
    ##1##2##3##4##5##6{#5}%
%    \end{macrocode}
% Note that these \cmd{@tmpa} macros make use of the fact that the first
% character of \#1 is ``c'' for lower-case, ``C'' for upper-case.
%    \begin{macrocode}
  \def\@tmpa##1##2\@nil{%
    \if##1c \def\@other{C##2}%
    \else\def\@other{c##2}\fi}%
  \@tmpa#1\@nil%
  \@ifundefined{\@other @#2@format@first}{%
    \def\@tmpa##1##2\@nil{%
      \if##1c\def\@changecase{\MakeUppercase}%
      \else\def\@changecase{\MakeLowercase}\fi}%
    \@tmpa#1\@nil%
    \@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}}%
    \expandafter\the\expandafter\@toksa\expandafter{%
      \expandafter\MakeUppercase\@tmpa{##1}{##2}{##3}{##4}{##5}{##6}}%
    \@toksa={%
      \expandafter\def\csname\@other @#2@format@first\endcsname%
        ##1##2##3##4##5##6}%
    \expandafter\the\expandafter\@toksa\expandafter{%
      \@tmpa{##1}{##2}{##3}{##4}{##5}{##6}}%
  }{}%
  \@ifundefined{\@other @#2@format@middle}{%
    \@toksa={%
      \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\let\csname\@other @#2@format@last\endcsname}%
    \expandafter\the\expandafter\@toksa%
      \csname #1@#2@format@last\endcsname%
  }{}%
}
%    \end{macrocode}
%
%
% \subsection{Default Reference Formats}
% Define the default reference formats, appropriate for \LaTeX
% documents written in English. 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.\\
% To-Do: add babel support
%    \begin{macrocode}
\crefformat{equation}{eq.~#2(#1)#3}
\Crefformat{equation}{Eq.~#2(#1)#3}
\crefformat{chapter}{chapter~#2#1#3}
\Crefformat{chapter}{Chapter~#2#1#3}
\crefformat{section}{section~#2#1#3}
\Crefformat{section}{Section~#2#1#3}
\crefformat{subsection}{section~#2#1#3}
\Crefformat{subsection}{Section~#2#1#3}
\crefformat{subsubsection}{section~#2#1#3}
\Crefformat{subsubsection}{Section~#2#1#3}
\crefformat{subsubsubsection}{section~#2#1#3}
\Crefformat{subsubsubsection}{Section~#2#1#3}
\crefformat{figure}{fig.~#2#1#3}
\Crefformat{figure}{Fig.~#2#1#3}
\crefformat{theorem}{theorem~#2#1#3}
\Crefformat{theorem}{Theorem~#2#1#3}
\crefmultiformat{equation}%
  {eqs.~#2(#1)#3}{, #2(#1)#3}{ and~#2(#1)#3}
\Crefmultiformat{equation}%
  {Eqs.~#2(#1)#3}{, #2(#1)#3}{ and~#2(#1)#3}
\crefmultiformat{chapter}%
  {chapters~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{chapter}%
  {Chapters~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{section}%
  {sections~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{section}%
  {Sections~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{subsection}%
  {sections~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{subsection}%
  {Sections~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{subsubsection}%
  {sections~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{subsubsection}%
  {Sections~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{subsubsubsection}%
  {sections~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{subsubsubsection}%
  {Sections~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{figure}%
  {figs.~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{figure}%
  {Figs.~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{theorem}%
  {theorems~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{theorem}%
  {Theorems~#2#1#3}{, #2#1#3}{ and~#2#1#3}
%
\crefrangeformat{equation}{eqs.~#3(#1)#4--#5(#2)#6}
\Crefrangeformat{equation}{Eqs.~#3(#1)#4--#5(#2)#6}
\crefrangeformat{chapter}{chapters~#3#1#4--#5#2#6}
\Crefrangeformat{chapter}{Chapters~#3#1#4--#5#2#6}
\crefrangeformat{section}{sections~#3#1#4--#5#2#6}
\Crefrangeformat{section}{Sections~#3#1#4--#5#2#6}
\crefrangeformat{subsection}{sections~#3#1#4--#5#2#6}
\Crefrangeformat{subsection}{Sections~#3#1#4--#5#2#6}
\crefrangeformat{subsubsection}{sections~#3#1#4--#5#2#6}
\Crefrangeformat{subsubsection}{Sections~#3#1#4--#5#2#6}
\crefrangeformat{subsubsubsection}{sections~#3#1#4--#5#2#6}
\Crefrangeformat{subsubsubsection}{Sections~#3#1#4--#5#2#6}
\crefrangeformat{figure}{figs.~#3#1#4--#5#2#6}
\Crefrangeformat{figure}{Figs.~#3#1#4--#5#2#6}
\crefrangeformat{theorem}{theorems~#3#1#4--#5#2#6}
\Crefrangeformat{theorem}{Theorems~#3#1#4--#5#2#6}
\crefrangemultiformat{equation}%
  {eqs.~#3(#1)#4--#5(#2)#6}%
  {, #3(#1)#4--#5(#2)#6}%
  { and~#3(#1)#4--#5(#2)#6}
\Crefrangemultiformat{equation}%
  {Eqs.~#3(#1)#4--#5(#2)#6}%
  {, #3(#1)#4--#5(#2)#6}%
  { and~#3(#1)#4--#5(#2)#6}
\crefrangemultiformat{chapter}%
  {chapters~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{chapter}%
  {Chapters~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{section}%
  {sections~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{section}%
  {Sections~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{subsection}%
  {sections~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{subsection}%
  {Sections~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{subsubsection}%
  {sections~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{subsubsection}%
  {Sections~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{subsubsubsection}%
  {sections~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{subsubsubsection}%
  {Sections~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{figure}%
  {figs~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{figure}%
  {Figs~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{theorem}%
  {theorems~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{theorem}%
  {Theorems~#3#1#4--#5#2#6}%
  {, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
%
\def\crefmiddleconjunction{, }
\def\creflastconjunction{, and }
%    \end{macrocode}
%
%
% \subsection{\package{hyperref} Support}
%    \begin{macrocode}
\DeclareOption{hyperref}{%
\PackageInfo{cleveref}{option `hyperref' loaded}
%    \end{macrocode}
% We redefine the utility macros to cope with the extra arguments
% supplied by hyperref (via the aux file).
%    \begin{macrocode}
\def\cref@label#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\@tempa\csname r@#1\endcsname%
  \edef\@tempa{\expandafter\@firstoffive\@tempa}%
  \expandafter\@cref@getlabel\@tempa\@nil#2}
\def\cref@gettype#1#2{%
  \expandafter\let\expandafter\@tempa\csname r@#1\endcsname
  \edef\@tempa{\expandafter\@firstoffive\@tempa}%
  \expandafter\@cref@gettype\@tempa\@nil#2}
\def\cref@getcounter#1#2{%
  \expandafter\let\expandafter\@tempa\csname r@#1\endcsname
  \edef\@tempa{\expandafter\@firstoffive\@tempa}%
  \expandafter\@cref@getcounter\@tempa\@nil#2}
\def\cref@getprefix#1#2{%
  \expandafter\let\expandafter\@tempa\csname r@#1\endcsname
  \edef\@tempa{\expandafter\@firstoffive\@tempa}%
  \expandafter\@cref@getprefix\@tempa\@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}%
  \reset@by{#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{\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{\old@refstepcounter{#1}}
\def\refstepcounter@optarg[#1]#2{%
  \old@refstepcounter{#2}%
  \expandafter\@cref@getlabel\@currentlabel\@nil{\@templabel}%
  \reset@by{#2}{\@tempreset}%
  \ifx\@tempreset\relax\def\@tempreset{}%
  \else\edef\@tempreset{\csname the\@tempreset\endcsname}\fi%
  \protected@edef\@currentlabel{%
    [#1][\arabic{#2}][\@tempreset]\@templabel}}
%    \end{macrocode}
%
% Redefine \@@setcref and \@@setrangeref to create hyperlinks, 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}{\@templabel}%
  \edef\@templink{\cref@hyperref{#2}}%
  #1{\@templabel}{\hyper@linkstart{link}{\@templink}}{\hyper@linkend}}
\def\@@setcrefrange#1#2#3{%
  \cref@getlabel{#2}{\@labela}%
  \cref@getlabel{#3}{\@labelb}%
  \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}}
} % end of hyperref option
%    \end{macrocode}
%
%
% \subsection{\package{ntheorem} Support}
%    \begin{macrocode}
\DeclareOption{ntheorem}{%
\PackageInfo{cleveref}{option `ntheorem' loaded}
%    \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}%  <<<<<
   \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}
\crefformat{lemma}{lemma~#2#1#3}
\Crefformat{lemma}{Lemma~#2#1#3}
\crefformat{corrollary}{corrollary~#2#1#3}
\Crefformat{corrollary}{Corrollary~#2#1#3}
\crefformat{proposition}{proposition~#2#1#3}
\Crefformat{proposition}{Proposition~#2#1#3}
\crefformat{definition}{definition~#2#1#3}
\Crefformat{definition}{Definition~#2#1#3}
\crefformat{result}{result~#2#1#3}
\Crefformat{result}{Result~#2#1#3}
\crefrangeformat{lemma}{lemma~#3#1#4--#5#2#6}
\Crefrangeformat{lemma}{Lemma~#3#1#4--#5#2#6}
\crefrangeformat{corrollary}{corrollary~#3#1#4--#5#2#6}
\Crefrangeformat{corrollary}{Corrollary~#3#1#4--#5#2#6}
\crefrangeformat{proposition}{proposition~#3#1#4--#5#2#6}
\Crefrangeformat{proposition}{Proposition~#3#1#4--#5#2#6}
\crefrangeformat{definition}{definition~#3#1#4--#5#2#6}
\Crefrangeformat{definition}{Definition~#3#1#4--#5#2#6}
\crefrangeformat{result}{result~#3#1#4--#5#2#6}
\Crefrangeformat{result}{Result~#3#1#4--#5#2#6}
\crefmultiformat{lemma}%
  {lemmas~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{lemma}%
  {Lemmas~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{corrollary}%
  {corrollaries~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{corrollary}%
  {Corrollaries~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{proposition}%
  {propositions~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{proposition}%
  {Propositions~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{definition}%
  {definitions~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{definition}%
  {Definitions~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefmultiformat{result}%
  {results~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{result}%
  {Results~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefrangemultiformat{lemma}%
  {lemmas~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}%
\Crefrangemultiformat{lemma}%
  {Lemmas~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{corrollary}%
  {corrollaries~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{corrollary}%
  {Corrollaries~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{proposition}%
  {propositions~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{proposition}%
  {Propositions~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{definition}%
  {definitions~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{definition}%
  {Definitions~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\crefrangemultiformat{result}%
  {results~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
\Crefrangemultiformat{result}%
  {Results~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}%
  { and~#3#1#4--#5#2#6}
} % end of ntheorem option
%    \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 appearning 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%
    \edef\@tmpa{\@tmpa}%
    \ifx\@tmpa\@eof%
      \@not@eoffalse%
    \else%
      \@not@eoftrue%
    \fi%
    \@whilesw\if@not@eof\fi{%
      \expandafter\g@addto@macro\expandafter\cref@text\expandafter{%
        \@tmpa^^J}%
      \show\cref@text%
      \immediate\read\@crefscript to \@tmpa%
      \edef\@tmpa{\@tmpa}%
      \ifx\@tmpa\@eof%
        \@not@eoffalse%
      \else%
        \@not@eoftrue%
      \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/||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}
  \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}
\renewcommand{\cref}[1]{%
  \edef\cref@text{}%
  \@cref{cref}{#1}%
  \cref@writescript{\string\cref\string{#1\string}}}
\renewcommand{\Cref}[1]{%
  \edef\cref@text{}%
  \@cref{Cref}{#1}%
  \cref@writescript{\string\Cref\string{#1\string}}}
\renewcommand{\crefrange}[2]{%
  \edef\cref@text{}%
  \@setcrefrange{#1}{#2}{cref}{}%
  \cref@writescript{%
    \string\crefrange\string{#1\string}\string{#2\string}}}
\renewcommand{\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}, \cmd{\@@setrangeref},
% \cmd{\@setcref@middleconjunction} and
% \cmd{\@setcref@lastconjunction} to append the text they typeset to
% \cmd{\cref@text}, as well as actually typesetting it.
%    \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@middleconjunction{%
  \crefmiddleconjunction%
  \expandafter\g@addto@macro
    \expandafter{\expandafter\cref@text\expandafter}%
    \expandafter{\crefmiddleconjunction}}
\def\@setcref@lastconjunction{%
  \creflastconjunction%
  \expandafter\g@addto@macro
    \expandafter{\expandafter\cref@text\expandafter}%
    \expandafter{\creflastconjunction}}
} % 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: