summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/phfthm/phfthm.dtx
blob: 6e9656689bd56642e972c92c46591bb6685ef3f0 (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
% \iffalse meta-comment
%
% Copyright (C) 2016 by Philippe Faist <philippe.faist@bluewin.ch>
% -------------------------------------------------------
% 
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% 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.3 or later is part of all distributions of LaTeX 
% version 2005/12/01 or later.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{phfthm.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[2005/12/01]
%<package>\ProvidesPackage{phfthm}
%<*package>
    [2016/08/15 v1.0 phfthm package]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{xcolor}
\usepackage{phfthm}
\usepackage[preset=xpkgdoc]{phfnote}
\usepackage{needspace}
\EnableCrossrefs         
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{phfthm.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \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{v1.0}{2016/04/20}{Initial version}
%
% \GetFileInfo{phfthm.dtx}
%
% \DoNotIndex{\newcommand,\newenvironment,\def,\gdef,\edef,\xdef,\if,\else,\fi,\ifx}
% \DoNotIndex{\expandafter,\csname,\endcsname,\let}
% 
% \title{\phfqitltxPkgTitle{phfthm}}
% \author{Philippe Faist\quad\email{philippe.faist@bluewin.ch}}
% \date{\pkgfmtdate\filedate}
% \maketitle
%
% \begin{abstract}
%   \pkgname{phfthm}---Goodies for theorems and proofs.
% \end{abstract}
%
% \inlinetoc
%
% \section{Introduction}
%
% The \pkgname{phfthm} package provides enhanced theorem and proof environments,
% based on the \pkgname{amsthm} original versions.  It allows for hooks to be
% placed, adds some default goodies and is highly customizable.
%
% There are three generic types of environments provided: theorem environments,
% proof environments and ``thmheading'' environments.
%
% \subsection{Theorem environments}
%
% Theorem environments look like this:
% \begingroup\setlength{\fboxsep}{1ex}
% \par\noindent\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
%   \begin{theorem}[Gauss]\noproofref
%     \label{thm:example-Gauss}
%     For a closed surface $S$ enclosing a volume $V$, we have
%     \begin{equation}
%       \oint_S\vec u\cdot d\vec S = \int_V(\vec\nabla\cdot\vec u)\,dV\ .
%     \end{equation}
%   \end{theorem}
% \end{minipage}}\endgroup
%
% \subsection{Proof environments}
%
% A proof environment might look like this:
% \begingroup\setlength{\fboxsep}{1ex}
% \par\noindent\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
%   \begin{proof}[*thm:example-Gauss]
%     The proof of the theorem goes here. \par\ldots
%   \end{proof}
% \end{minipage}}\endgroup
%
% The enhanced theorem and proof environments provided by this package allow to
% pair theorems with proofs, automatically generating references from one to the
% other (see \autoref{sec:proof-ref-mechanism}).
%
%
% \subsection{Theorem-heading environments}
%
% Finally, theorem-heading environments are formatted like theorems, but the
% heading title is set as an argument to the environment.  These environments
% are a nice alternative for definitions, and look like this:
% \begingroup\setlength{\fboxsep}{1ex}
% \par\noindent\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
%   \begin{thmheading}{Trace Distance}
%     The `trace distance' between $\rho$ and $\sigma$ is defined as
%     \begin{equation}
%       \delta(\rho,\sigma)=\frac12\,\left\Vert \rho - \sigma \right\Vert_1\ ,
%     \end{equation}
%     where $\lVert\cdot\rVert_1$ is the Schatten-1 norm.
%   \end{thmheading}
% \end{minipage}}\endgroup
% 
%
% 
% \section{Quick start and package options}
% \label{sec:quick-start}
% \label{sec:global-pkg-options}
%
% Example: Load the rich theorem set, with separate counters, and with proof-ref
% mechanism on and always displaying the proof reference in the margin:
% \begin{verbatim}
% \usepackage[thmset=rich,sepcounters=true,proofref={always,margin}]{phfthm}
% \end{verbatim}
%
% By default, some styles are tweaked a bit so that they appear nicely as
% documented below (for example, by using a filled square instead of a simple
% square for end-of-proof QED markers).  Use the package option
% \pkgoptionfmt{resetstyle} to instruct \pkgname{phfthm} not to proceed to these
% style adjustments; this allows you to enable features individually and
% selectively:
% \begin{verbatim}
% \usepackage[resetstyle,prooftitleitbf=true]{../phfthm}
% \end{verbatim}
% 
%
% \subsection{Predefined theorem environments}
% \label{sec:theorem-sets}
%
% You may load predefined theorem sets via the package option
% \pkgoptionfmt{thmset}.  Theorem sets group common environments used in
% mathematical works such as Theorem, Proposition, Definition, etc.
%
% Some package options control the way these environments are defined.  If you
% would like more refined control over the appearance of these environments, or
% over which environments are defined, you may consider calling |\phfLoadThmSet|
% manually or defining individual environments with |\phfMakeTheorem|.
%
% Possible theorem sets are:
% \begin{pkgoptions}
% \item[thmset=]\pkgoptionscombineitem
% \item[thmset=empty] Do not define any environment at package loading.  You may
%   of course invoke |\phfLoadThmSet| or |\phfMakeTheorem| manually at any
%   later point.
% \item[thmset=simple]
%   \DescribeEnv{theorem} \DescribeEnv{proposition}
%   \DescribeEnv{lemma} \DescribeEnv{corollary} \DescribeEnv{definition}
%   Define the environments |theorem|, |proposition|, |lemma|
%   and |corollary| as theorem-like environments, and |definition| as a
%   definition-like environment.
% \item[thmset=default] \DescribeEnv{conjecture} \DescribeEnv{remark} Define the
%   environments |theorem|, |proposition|, |lemma|, |corollary|, |conjecture|,
%   |remark| as theorem-like environments, and |definition| as a definition-like
%   environment.
% \item[thmset=shortnames] For if you like typing less: the same environments
%   are defined as the default set, but with shorter names.  Define the
%   environments |thm|, |prop|, |lem|, |cor|, |conj|, |rem| as theorem-like
%   environments, and |defn| as a definition-like environment.
% \item[thmset=rich] \DescribeEnv{idea} \DescribeEnv{question}
%   \DescribeEnv{problem} Provides the same environments as the |default|
%   theorem set, as well as in addition: |idea|, |question| and |problem| as
%   theorem-like environments.
% \end{pkgoptions}
%
% You may also load a theorem set at a later
% point after loading the \pkgname{phfthm} package by invoking the
% |\phfLoadThmSet| macro, see \autoref{sec:load-thm-set-manually}.
%
% Further package options modify the style of the theorem-like and
% definition-like environments defined via the \pkgoptionfmt{thmset} package
% option:
% 
% \begin{pkgoptions}
% \item[theoremstyle=\meta{theorem style name}] Use this package option to
%   specify which style to use for theorem-like environments when loading the
%   theorem set specified via the \pkgoptionfmt{thmset} package option.  The
%   theorem style name should be one of |plain|, |definition|, |remark|, or any
%   other |\newtheoremstyle|-defined theorem style (see documentation of
%   \pkgname{amsthm}).
% \item[definitionstyle=\meta{theorem style name}] Use this package option to
%   specify which style to use for definition-like environments when loading the
%   theorem set specified via the \pkgoptionfmt{thmset} package option.  The
%   theorem style name should be one of |plain|, |definition|, |remark|, or any
%   other |\newtheoremstyle|-defined theorem style (see documentation of
%   \pkgname{amsthm}).
% \end{pkgoptions}
%
% Further options control various aspects of the environments defined by
% \pkgoptionfmt{thmset}.
% \begin{pkgoptions}
% \item[sepcounters=\metatruefalsearg] Each theorem environment defined with the
%   \pkgoptionfmt{thmset} package option will use a separate counter if this
%   option is set; otherwise (the default), there is a single counter which is
%   shared by all those theorem environments.
% \end{pkgoptions}
%
% The \pkgoptionfmt{proofref} package option allows to specify a comma-separated
% list of attributes to apply to the proof reference (``proof on page XYZ'')
% displayed along with the theorem.  The following attributes may be specified:
% \begin{pkgoptions}
% \item[proofref=\pkgoptattribempty{}]\pkgoptionscombineitem
% \item[proofref=\pkgoptattribnodots{default}] Do not change the default
%   proof reference appearance.
%
% \item[proofref=false] Deactivate the proof-ref mechanism.
%
% \item[proofref=\pkgoptattrib{margin}] The proof reference is displayed in
%   the margin, instead of after the theorem.
%
% \item[proofref=\pkgoptattrib{longref}] The proof reference is displayed as
%   a full sentence (``The proof of this \meta{Theorem Name} can be found on
%   page \meta{XYZ}.'').
%
% \item[proofref=\pkgoptattrib{off}] Turn off the proof reference mechanism
%   completely for theorems defined with the \pkgoptionfmt{thmset} option.
%
% \item[proofref=\pkgoptattrib{always}] Always display the proof reference,
%   even if the proof is on the same page or on a nearby page.
%
%   Note: this option has a global effect.
%
% \item[proofref=\pkgoptattrib{onyifveryfar}] The proof reference is only
%   displayed if the proof is at least two pages back, or four pages ahead.
%
%   Note: this option has a global effect.
%
% \end{pkgoptions}
%
% \begin{pkgnote}
%   The two package options \pkgoptionfmt{proofref=\pkgoptattribnodots{always}}
%   and \pkgoptionfmt{proofref=\pkgoptattribnodots{onlyifveryfar}} apply to
%   \emph{all} theorem environments which use the proof-ref mechanism, whether
%   they have already been defined or not (see |\phfProofrefPageBackTolerance|
%   and |\phfProofrefPageAheadTolerance|).
%
%   All the other above options apply only to the theorem environments defined
%   via the \pkgoptionfmt{thmset} package option.
% \end{pkgnote}
% 
% \subsection{The proof environment}
% \label{sec:proof-env}
%
% \DescribeEnv{proof}
% By default, the \pkgname{phfthm} package overrides the |proof| environment
% with a the package's own enhanced version.  If you want to preserve the
% original \emph{AMS} environment, you should use the
% \pkgoptionfmt{proofenv=false} package option.
%
% \begin{pkgoptions}
% \item[proofenv=\metatruefalsearg] If set to |true|, then define an enhanced
%   |proof| environment when loading this package.  This will override any
%   previously existing |proof| environment such as \emph{AMS}'.
%
%   If set to |false|, no action is taken at package loading time.  You should
%   then directly use the |\phfMakeProofEnv| macro to define proof environments.
% \end{pkgoptions}
%
% If you want finer control over how the proof environment is defined, or if you
% want to customize its appearance, you should use the |\phfMakeProofEnv| macro
% directly (\autoref{sec:mk-proof-env}).
%
% If you set \pkgoptionfmt{proofenv=true}, there are a couple package options
% which alter the way the proof displays:
% \begin{pkgoptions}
% \item[smallproofs=\metatruefalsearg] If set to |true|, then proofs display in
%   a smaller font.
%
% \item[qedsymbolblacksquare=\metatruefalsearg] If set to |true|, the QED
%   end-of-proof symbol (usually ``$\square$'' with \pkgname{amsthm}) is
%   replaced by a filled square (``$\blacksquare$'').
%
% \item[prooftitleitbf=\metatruefalsearg] If set to |true|, then the proof title
%   (``Proof'' or ``Proof of Theorem 1'') is typeset in bold italic font.
% \end{pkgoptions}
%
%
% \subsection{The theorem-heading environment}
% \label{sec:thmheading-default}
% \DescribeEnv{thmheading} By default, the |thmheading| environment is provided
% by the \pkgname{phfthm} package:
% \begin{verbatim}
% \begin{thmheading}{Trace Distance}
%   The `trace distance' between $\rho$ and $\sigma$ is defined as
%   \begin{equation}
%     \delta(\rho,\sigma)=\frac12\,\left\Vert\rho-\sigma\right\Vert_1\ ,
%   \end{equation}
%   where $\lVert\cdot\rVert_1$ is the Schatten-1 norm.
% \end{thmheading}
% \end{verbatim}
% \begingroup\setlength{\fboxsep}{1ex}
% \par\noindent\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
%   \begin{thmheading}{Trace Distance}
%     The `trace distance' between $\rho$ and $\sigma$ is defined as
%     \begin{equation}
%       \delta(\rho,\sigma)=\frac12\,\left\Vert \rho - \sigma \right\Vert_1\ ,
%     \end{equation}
%     where $\lVert\cdot\rVert_1$ is the Schatten-1 norm.
%   \end{thmheading}
% \end{minipage}}\endgroup
%
% You may also use |\label| and |\ref| as usual (|\ref| simply displays the
% given title).
%
% Some package options control the way this environment is defined.
%
% \begin{pkgoptions}
% \item[thmheading=\metatruefalsearg] Define the environment
%   |\begin{thmheading}...\end{thmheading}| when loading the \pkgname{phfthm}
%   package, with reasonable default settings.
% \item[thmheadingstyle=\meta{theorem style}] If \pkgoptionfmt{thmheading=true}
%   was specified, you may use this option to specify the theorem style to use
%   for the |thmheading| environment.  Possible values are \emph{AMS} theorem
%   style names (e.g.\@ the base styles |plain|, |definition| or |remark|), or
%   any other style defined with |\newtheoremstyle|.
% \end{pkgoptions}
%
% If you want to define theorem-heading environments manually, see
% \autoref{sec:thmheading-manually}.
%
%
%
% 
% \section{Theorem environments}
%
% A theorem environment is based on the environment furnished by
% \pkgname{amsthm}'s |\newtheorem| command, but with added goodies.
%
%
% \subsection{Define theorem environments manually}
%
% \DescribeMacro{\phfMakeTheorem} If you don't want to load a full theorem set
% (\autoref{sec:theorem-sets}), you can define theorem environments individually
% with |\phfMakeTheorem|:
%
% \noindent |\phfMakeTheorem|\hspace{0pt}\oarg{key-value
% options}\hspace{0pt}\marg{theorem environment name}\hspace{0pt}\marg{theorem
% name}
%
% This command defines a new environment (given as the first mandatory argument)
% which behaves as a theorem and is displayed as given by the second mandatory
% argument.  For example, we might call |\phfMakeTheorem{theorem}{Theorem}| to
% define the environment |\begin{theorem}...\end{theorem}| which displays
% ``\textit{Theorem N.} \ldots''
%
% The possible key-value options for the optional argument are:
% \begin{cmdoptions}
% \item[counter=\meta{\LaTeX{} counter $\mid$ (empty)}] The name of the \LaTeX{}
%   counter to use for the theorem environment.  If this is empty, then a new
%   counter will be created which is specific to this theorem environment (the
%   default).  If not empty, then the theorem environment uses the given counter
%   (or an alias thereof, see \cmdoptionfmt{aliascounter}).
%
%   If a counter is specified, the counter should already be defined with
%   \LaTeX's |\newcounter|.
%
% \item[aliascounter=\metatruefalsearg] In some cases
%   (e.g.\@ if you're using \pkgname{hyperref}'s |\autoref|), it is important to
%   have counters specific to each theorem environment (so you get ``Theorem 5''
%   or ``Proposition 5'' right).  However, you may want different theorem
%   environments to share a same logical counter (Say ``Definition 1'',
%   ``Definition 2'', ``Theorem 3'', ``Proposition 4'').  In this case, you
%   should specify \cmdoptionfmt{aliascounter=true}.
%
%   When this option is on, then first we define an alias counter of the one
%   given to the \cmdoptionfmt{counter} option, and then use the alias for the
%   theorem environment.  The alias is declared using the \pkgname{aliascnt}
%   package.  The alias counter is automatically set up correctly for using
%   |\autoref|.
%
%   Note that the \cmdoptionfmt{aliascounter} option only has an effect if the
%   \cmdoptionfmt{counter} option is set to some non-empty value.  If
%   \cmdoptionfmt{counter} is set to a non-empty value, then
%   \cmdoptionfmt{aliascounter} defaults to |true|.
%
% \item[thmstyle=\meta{theorem style $\mid$ (empty)}] The theorem style to use
%   to define this theorem environment.  The value of this option should be a
%   valid argument to \textit{AMS}'s |\theoremstyle|.  If you leave this empty
%   (the default), then the theorem style is not set explicitly and whatever
%   default style is used.
%
% \item[defnostar=\metatruefalsearg] Set this to
%   |true| if you want the corresponding non-starred theorem environment to be
%   defined, e.g.\@ |\begin{theorem}...\end{theorem}|.
%
%   Normal (non-starred) versions of the environments have an associated theorem
%   number, as you expect by default.
%
% \item[defstar=\metatruefalsearg] Set this to
%   |true| if you want the corresponding starred theorem environment to be
%   defined, e.g.\@ |\begin{theorem*}...\end{theorem*}|.
%
%   Starred versions of the environments do not have an associated theorem
%   number.
%
% \item[proofref=\metatruefalsearg] Enable or disable
%   the proof-ref mechanism for this theorem environment (enabled by default).
%
% \item[proofrefstyle=\meta{proof-ref style}] The style to use for the proof
%   references.  Here you may specify how the proof ref appears, for example (in
%   the margin, long sentence, ...).  Possible styles are
%   \cmdoptionfmt{proofrefstyle=default} (the default),
%   \cmdoptionfmt{proofrefstyle=margin} (display the proof ref in the margin of
%   the page) and \cmdoptionfmt{proofrefstyle=longref} (as by default but with a
%   full sentence).  See \autoref{sec:proof-ref-customize-appearance} for how to
%   further customize the appearance of the proof reference.
%
% \end{cmdoptions}
%
% For example, you may use the following command invocation to define a theorem
% environment named ``Remark'' implemented as |\begin{remark}...\end{remark}|,
% also with a starred verison |\begin{remark*}...\end{remark*}|, using the
% |plain| \emph{AMS} theorem style, and without the proof-ref mechanism:
% \begin{verbatim}
% \phfMakeTheorem[defstar=true,defnostar=true,thmstyle=plain,counter=,%
%     proofref=false]{remark}{Remark}
% \end{verbatim}
%
%
%
% \subsection{Loading theorem sets manually}
% \label{sec:load-thm-set-manually}
%
% \DescribeMacro{\phfLoadThmSet} You may load theorem sets at any time via the
% macro |\phfLoadThmSet|.  This may be useful, for example, to load theorem sets
% only after you have defined a custom theorem style.  The syntax of
% |\phfLoadThmSet| is:
%
% \noindent |\phfLoadThmSet|\hspace{0pt}\marg{options to
% \phfverb{\phfLoadThmSet} for theorem-like
% environments}\hspace{0pt}\marg{options to \phfverb{\phfLoadThmSet} for
% definition-like environments}\hspace{0pt}\marg{name of theorem set to load}
%
% The first and second argument to this macro are tokens to expand in front of
% |\phfMakeTheorem| for theorem-like or definition-like environments.  For
% example:
% \begin{verbatim}
% \newcounter{mythmcounter}
% \newtheoremstyle{mythmstyle}{...}
% \newtheoremstyle{mydefnstyle}{...}
% \phfLoadThmSet{[thmstyle=mythmstyle,counter=mythmcounter]}
%     {[thmstyle=mydefnstyle,counter=mythmcounter]}{rich}
% \end{verbatim}
% 
% \begin{pkgwarning}
%   The first and second arguments to |\phfLoadThmSet| must either be empty,
%   or be enclosed in square braces.
% \end{pkgwarning}
%
% \needspace{10\baselineskip}
% \DescribeMacro{\theoremname}
% \DescribeMacro{\propositionname}
% \DescribeMacro{\lemmaname}
% \DescribeMacro{\corollaryname}
% \DescribeMacro{\conjecturename}
% The title of the theorem environments defined in theorem sets use the same
% scheme as figures, tables, etc.\@ with regard to translations and
% \pkgname{babel}: they use |\theoremname|, |\propositionname|, etc.
% 
% \DescribeMacro{\remarkname}
% \DescribeMacro{\definitionname}
% \DescribeMacro{\ideaname}
% \DescribeMacro{\questionname}
% \DescribeMacro{\problemname}
%
% This package is language agnostic (with titles defined by default in English),
% and does not provide the titles for other languages.  In order to support
% language switching with \pkgname{babel} and |\selectlanguage|, you should add
% the relevant names to the corresponding |\captions|\meta{language name} macro,
% for example:
% \begin{verbatim}
% \usepackage[francais,...]{babel}
% ...
% \addto\captionsfrancais{%
%   \def\theoremname{Th\'eor\`eme}%
%   \def\propositionname{Proposition}%
%   \def\lemmaname{Lemme}%
%   \def\corollaryname{Corollaire}%
%   \def\conjecturename{Conjecture}%
%   \def\remarkname{Remarque}%
%   \def\definitionname{D\'efinition}%
%   \def\ideaname{Id\'ee}%
%   \def\questionname{Question}%
%   \def\problemname{Probl\`eme}%
% }
% ... \selectlanguage{francais} ...
% \end{verbatim}
%
% 
% \subsection{Theorem hooks}
% \label{sec:theorem-hooks}
%
% Any theorem environment automatically calls some hooks.  There are hooks
% available per theorem environment as well as generic for all theorem
% environments.
%
% \DescribeMacro{\phfthm@hook@start@thmname} The hook
% |\phfthm@hook@start@|\meta{theorem environment name}\marg{theorem title} is
% called at the start of the environment.  More precisely, it is called inside
% the original \pkgname{amsthm} base environment; that is, after the heading was
% generated.  It takes one mandatory argument, the optional title provided to
% the theorem environment which may be empty.  By default, the hook defers to
% the global hook |\phfthm@hook@startcommonnostar|.
%
% \DescribeMacro{\phfthm@hook@start@thmname*} The hook
% |\phfthm@hook@start@|\meta{starred theorem environment name} is completely
% analogous, and is called for the starred environment.  The only difference is
% that by default, it defers its call to |\phfthm@hook@startcommonstar|.
%
% \DescribeMacro{\phfthm@hook@startcommonnostar} The hook
% |\phfthm@hook@startcommonnostar|\marg{theorem environment name}\marg{theorem
% optional given title} collects the default definitions for non-starred
% environments (none by default) and continues to defer to
% \DescribeMacro{\phfthm@hook@startcommon}
% |\phfthm@hook@startcommon|\marg{theorem environment name}\marg{theorem
% optional given title}.  \DescribeMacro{\phfthm@hook@startcommonstar}
% Analogously, the macro |\phfthm@hook@startcommonstar|\marg{theorem environment
% name}\marg{theorem optional given title} groups commands for starred
% environments (typically doesn't take care of |\label| stuff) and also defers
% to |\phfthm@hook@startcommon|.
%
% The end hooks work very much
% analogously. \DescribeMacro{\phfthm@hook@end@thmname}
% |\phfthm@hook@end@|\meta{theorem environment name} and
% \DescribeMacro{\phfthm@hook@end@thmname*} |\phfthm@hook@end@|\meta{starred
% theorem environment name} are called respectively for the non-starred and
% starred version of that theorem environment, and by default they defer to the
% common \DescribeMacro{\phfthm@hook@endcommonnostar}
% |\phfthm@hook@endcommonnostar|\marg{theorem environment name} or
% \DescribeMacro{\phfthm@hook@endcommonstar}
% |\phfthm@hook@endcommonstar|\marg{theorem environment name}.  Both these hooks
% defer their calls to |\phfthm@hook@endcommon|\marg{theorem environment name}.
%
% For theorems using the proof-reference mechanism, i.e.\@ for which
% \cmdoptionfmt{proofref=true} was specified to |\phfMakeTheorem| and which uses
% the |\label| hack (\autoref{sec:proof-ref-mechanism}), there is an additional
% hook.  \DescribeMacro{\phfthm@hook@afterlabel@thmname} The hook
% |\phfthm@hook@afterlabel@|\meta{theorem environment name} is called just after
% the |\label| command corresponding to the theorem is encountered (this should
% always be at the \emph{beginning} of the theorem, see
% \autoref{sec:proof-ref-mechanism}).  Depending on the proof-ref style, this
% hook may be used to generate the proof reference text (for example, with the
% |margin| proof-ref style).  The hook is called after the theorem label is set.
% The label itself can be recovered from the value of the macro
% |\phfthm@val@thmlabel|.  By default, that hook calls the common hook
% \DescribeMacro{\phfthm@hook@afterlabelcommon}
% |\phfthm@hook@afterlabelcommon|\marg{theorem environment name}. (After the
% first occurrence of the command |\label|, the latter's definition is
% restored.)
% 
%
%
% \section{Proof environments}
%
% Proof environments typeset mathematical proofs.  The proof environment(s)
% provided by \pkgname{phfthm} give some added functionality with respect to the
% \emph{AMS}-default |proof| environment, such as supporting the proof-reference
% mechanism described in \autoref{sec:proof-ref-mechanism}.
%
% A proof environment might look like the following:
% \begin{proof}[Theorem 5]
%   Let $\mathcal{T}_{X\to X'}$ be any trace nonincreasing completely positive
%   map such that $\mathcal{T}_{X\to X'}\left(\Gamma_X\right)$ lies within the
%   support of $\Gamma_{X'}$. Define the normalized state
%   $\gamma_X = \Gamma_X / \operatorname{tr}\Gamma_X$.
%
%   Now consider this and that \ldots
% \end{proof}
%
%
% The proof environments defined by this package wrap a given proof display
% environment (such as \emph{AMS}' (\pkgname{amsthm}'s) or \pkgname{IEEEtran}'s
% original |proof| environment) by adding functionality in the form of hooks.
% In the following, we refer to the ``underlying proof display environment'' as
% the original environment which is wrapped.  It may be any \LaTeX{} environment
% whose task is to format the proof nicely.
%
%
% \subsection{Manually define a proof environment}
% \label{sec:mk-proof-env}
%
% \DescribeMacro{\phfMakeProofEnv} You may use the macro |\phfMakeProofEnv| to
% declare a new proof environment. The syntax is:
%
% \noindent |\phfMakeProofEnv|\oarg{key-value options}\marg{proof environment name}
%
% This defines a new environment with the given name, which may be used to
% display proofs to theorems.  The options may be:
% \begin{cmdoptions}
% \item[displayenv=\meta{name of \LaTeX{} environment}] Set a
%   \LaTeX{} environment to use to actually format and display the proof.  (The
%   |\phfMakeProofEnv| command itself doesn't care about how the proof is
%   displayed or formatted; rather it adds a goodies infrastructure in which
%   stuff can be plugged in and provides options for such goodies.)
%
%   You may specify here the name of a \LaTeX{} environment, or give the special
%   value \cmdoptionfmt{displayenv=*} to indicate the default appearance
%   provided by \pkgname{phfthm}, or leave the value empty
%   \cmdoptionfmt{displayenv=} to signify that no underlying display environment
%   should be invoked.  (The latter may be useful if you are plugging a
%   |\phfMakeProofEnv|-generated environment into a larger environment which
%   already takes care of the display.)
%
% \item[defaultproofname=\meta{default proof title}] Specify here the title to
%   use (e.g.\@ ``Proof'') if no argument was given to the proof environment.
%   If you do not specify any |defaultproofname|, or pass an empty value, then
%   the value of |\proofname| is used.
%
% \item[parselabel=\metatruefalsearg] Specify whether
%   the environment should parse its argument for some special information.  If
%   set to |true|, then the proof argument is passed on to a command (specified
%   by the \cmdoptionfmt{parselabelcmd} option).
%
% \item[parselabelcmd=\meta{{\LaTeX} macro}] If \cmdoptionfmt{parselabel} is set
%   to |true|, then specify here a \LaTeX{} command which parses whatever it
%   wants from the proof environment's argument.  The macro should set the
%   |\phfthm@val@displayargs| macro to tokens which will be expanded just after
%   the invocation of the proof environment's display environment
%   (\cmdoptionfmt{displayenv}). It should also set |\phfthm@val@proofoflabel|
%   (if appropriate) to the label corresponding to the theorem for which this is
%   the proof of.
%
%   By default, the command |\phfthm@proof@parselabel| is used, which parses the
%   proof environment's argument for a reference to a theorem in the context of
%   a proof-ref mechanism (see \autoref{sec:proof-ref-mechanism}).  The label is
%   parsed to see if it is of the form |[*thm:reference]|, where |thm:reference|
%   is the label pinned to a theorem.
%
% \item[override=\metatruefalsearg] Whether to
%   override any existing environment with the same name as the new proof
%   environment. If |true| is specified here, then |\renewenvironment| is used
%   to define the proof environment, otherwise a simple |\newenvironment| is
%   used.
%
% \item[internalcounter=\meta{name of \LaTeX{} counter}] The name of the
%   internal counter the proof environment should use.  The count number is not
%   displayed (by default at least), but it is only used to pin down anchors for
%   PDF hyperlinks.
%
%   The counter should already be defined with |\newcounter|.
%
% \item[proofofname=\meta{\LaTeX{} macro}] Specify here a macro
%   which will be called with a single argument. The macro produces the text to
%   display when the proof environment is parsed as the proof of a specific
%   theorem or proposition (or other theorem environment).  The argument which
%   will be given to it is the title of what the proof is of (e.g.\@ ``Theorem
%   3''). Typically, the macro should produce something like ``Proof of Theorem
%   XYZ.''
%
%   By default, the global macro |\proofofname| is used.
% \end{cmdoptions}
%
% \DescribeMacro{\proofname} Text to use to display ``Proof.''  This should be
% already defined by the \LaTeX{} system, and \pkgname{babel} should already
% provide translations in different languages.
%
% \DescribeMacro{\proofofname} The globally defined macro |\proofofname|
% specifies the default way of displaying ``Proof of Theorem~5.''  It is
% originally defined as something like
% \begin{verbatim}
% \newcommand\proofofname[1]{\proofname{} of #1}
% \end{verbatim}
% You may override this to obtain something fancier, of you wish to display the
% document in a different language:
% \begin{verbatim}
% \def\proofofnamefrancais#1{\proofname{} (#1)}
% \addto\captionsfrancais{\let\proofofname\proofofnamefrancais}
% ...
% \selectlanguage{francais} ...
% \end{verbatim}
%
%
% 
% \subsection{Proof hooks}
% \label{sec:proof-hooks}
%
% The proof hooks are relatively straightforward.  All hooks presented here take
% no argument.
%
% Information about the argument of the proof, both the raw argument and the
% possibly parsed reference, are available as macros to some of the hooks (but
% don't change these values unless you know what you're doing).  The macro
% |\phfthm@val@proofarg| contains the raw argument to the proof environment, and
% is available to all hooks.  If you use the default proof environment argument
% parsing (which you must have enabled when calling |\phfMakeProofEnv|), then
% additionally the macros |\phfthm@val@prooftitle| and |\phfthm@val@proofofname|
% are available containing, respectively, the label of the theorem which is
% referenced, and the displayable reference to it (e.g. ``Theorem~5'').  The
% last two macros are available to all hooks except the first one (|..@start|).
%
% The hooks named |..@start...| are called within the call to
% |\beg||in{<proof environment>}|.
%
% \expandafter\DescribeMacro\expandafter{\csname phfthm@hookproof@...@start\endcsname}
% The hook named |\phfthm@hookproof@|\meta{environment name}|@start| is called
% at the very beginning of the proof environment.
%
% \expandafter\DescribeMacro\expandafter{\csname phfthm@hookproof@...@startafterdisplay\endcsname}
% 
% The hook named |\phfthm@hookproof@|\meta{environment name}|@startafterdisplay|
% is invoked immediately after the beginning of the underlying ``display''
% environment (the environment used to display the proof contents).
%
% \expandafter\DescribeMacro\expandafter{\csname phfthm@hookproof@...@startlast\endcsname}
% 
% The hook named |\phfthm@hookproof@|\meta{environment name}|@startlast| is
% called after we are sure that an anchor has been pinned down for the proof.
% This hook is called last within the commands in |\beg||in{<proof environment>}|.
%
% \DescribeMacro{\phfPinProofAnchor} By the way, the macro |\phfPinProofAnchor|
% may be used within the hooks to pin down an anchor for referring to the proof
% (especially via the proof-ref mechanism).  Just call it anywhere appropriate
% (a good idea is calling it after leaving v-mode before displaying the title,
% in order to avoid placing it just before a page break).  If you do not call
% this macro, it is automatically called for you just before the |...@startlast|
% hook.
%
%
% The two following hooks are called within the call to%
% |\en||d{<proof environment>}|.
%
% \expandafter\DescribeMacro\expandafter{\csname phfthm@hookproof@...@end\endcsname}
% The hook |\phfthm@hookproof@|\meta{environment name}|@end| is called before
% the proof display environment is closed.
%
% \expandafter\DescribeMacro\expandafter{\csname phfthm@hookproof@...@final\endcsname}
% The hook |\phfthm@hookproof@|\meta{environment name}|@final| is called after
% the proof environment display is finished, as the very last.
%
% All proof hooks call are defined by default to defer their call to a common
% hook.  The common hooks each take one argument (the proof environment name).
% They are named |\phfthm@hookproof@startcommon|\marg{environment name},
% |\phfthm@hookproof@startafterdisplaycommon|\marg{environment name},
% |\phfthm@hookproof@startlastcommon|\marg{environment name},
% |\phfthm@hookproof@endcommon|\marg{environment name}, and
% |\phfthm@hookproof@finalcommon|\marg{environment name}.
% They are all defined to be empty by default.
%
%
% \section{Pairing theorems to proofs and proof-reference mechanism}
% \label{sec:proof-ref-mechanism}
%
% One of the goodies provided by the \pkgname{phfthm} package is the proof-ref
% mechanism, where in a theorem environment, the text ``see proof on page
% \ldots'' is displayed to direct the reader to the location of the
% corresponding proof.  The mechanism is deactivated by default, but can be
% enabled with a simple package option.
%
% This only works if the proof is given the label of the corresponding theorem
% or proposition.  For example:
% \begin{verbatim}
% \begin{theorem}[Gauss]
%   \label{thm:Gauss}
%   For a closed surface $S$ enclosing a volume $V$, we have
%   \begin{equation}
%     \oint_S\vec u\cdot d\vec S = \int_V(\vec\nabla\cdot\vec u)\,dV\ .
%   \end{equation}
% \end{theorem}
%
% ...
%
% \begin{proof}[*thm:Gauss]
%   ...
% \end{proof}
% \end{verbatim}
%
% The above example might produce the following output:
% \begingroup\setlength{\fboxsep}{1em}
% \par\noindent\fbox{%
%   \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
%     \textbf{Theorem 17} (Gauss). For a closed surface $S$ enclosing a
%     volume $V$, we have
%     \setcounter{equation}{41}
%     \begin{equation}
%       \oint_S\vec u\cdot d\vec S = \int_V(\vec\nabla\cdot\vec u)\,dV\ .
%     \end{equation}
%     \hfill{\small\itshape (Proof on page XXX.)}\hfilneg
%     {\par\vspace{1ex}\relax
%     \ldots\par\vspace{1ex}}\relax
%     \par\textit{Proof of Theorem 17.}\hspace{2em}\ldots
%   \end{minipage}
% }\endgroup
%
% \subsection{On the theorem side}
%
% On the theorem side, the proof-ref mechanism works by hacking into the
% definition of \LaTeX's |\label|.  The |\label| command should be placed first
% within the theorem (see example above).  It is important, in theorems which
% use the proof-ref mechanism (on by default), to always have a corresponding
% label: Indeed, you may experience weird results if you don't have a theorem
% label, but then have labels for other objects in the theorem such as equations
% or itemize items.
%
% Once the corresponding proof is detected (a proof environment with an optional
% argument of the form |[*thm:the-label]| for the same label |thm:the-label| as
% specified to the theorem, see \autoref{sec:proof-ref-mechanism-proof-side}),
% then a text is generated (by default ``Proof on page \ldots'') and placed
% after the theorem. The appearance of this text is customizable
% (\autoref{sec:proof-ref-customize-appearance}).
%
% More precisely, the hack with the |\label| command works as follows: At the
% beginning of the theorem, the |\label| command is redefined so that at its
% fist occurrence, it stores its argument as the theorem's label to use for the
% proof reference, it then pins down a \LaTeX{} label as the original |\label|
% command would do, and finally it calls the |...@afterlabel| theorem hook (see
% \ref{sec:theorem-hooks}).  After the first occurrence of |\label|, the
% |\label| command is restored to its original \LaTeX{} meaning in case there
% are other objects within the theorem which are to be referred to.
%
% \begin{pkgtip}
%   The |\label| hack is only active within theorem environments where the
%   proof-ref mechanism has been enabled.  Outside these environments, the
%   |\label| macro retains its original \LaTeX{} definition.
% \end{pkgtip}
%
% \DescribeMacro{\noproofref} If, for any reason, you do not want to make sure
% you don't have any text ``Proof on page \ldots'' appearing (for example there
% is no corresponding proof because the theorem is obvious), then you should
% call |\noproofref| immediately inside the theorem:
% \begin{verbatim}
% \begin{theorem}
%   \noproofref
%   Theorem text ...
% \end{theorem}
% \end{verbatim}
%
% The command |\noproofref| temporarily disables the proof-ref mechanism (and
% restores |\label| to \LaTeX's original meaning) for the current theorem.
%
%
% \subsection{On the proof side}
% \label{sec:proof-ref-mechanism-proof-side}
%
% On the proof side, you just need to specify for which theorem this is the
% proof of.  For that (unless you override the defaults and plug in your own
% magic parsing; see \autoref{sec:mk-proof-env}), you should specify an optional
% argument to the proof which is of the following form:
% |\begin{proof}[*|\meta{label}|]|,
%   \iffalse meta-comment \end{proof} [-- emacs is confused] \fi
% where \meta{label} is the label name you have associated with the theorem in
% question (see example above).
%
% This has two effects: it sets the proof to display ``\textit{Proof of
% \ldots},'' and also does some background dark magic to display, at the
% location of the corresponding theorem, some text like ``\textit{Proof on page
% \ldots},'' where the page number corresponds to the page on which this proof
% is located.
%
%
% \subsection{Customizing appearance of the proof reference text}
% \label{sec:proof-ref-customize-appearance}
%
% Here we explain the workings of the |\phfthm@proofrefstyle@...| macros and how
% they are called.  It allows you to define new proof-ref styles, for example.
%
% When the option \cmdoptionfmt{proofref=true} is given to |\phfMakeTheorem| to
% define an environment (say |mytheoremenv|), then the hook
% |phfthm@hook@start@mytheoremenv| will automatically include the following
% calls:
% \begin{itemize}
% \item The macro |\phfthm@proofrefstyle@|\meta{proof-ref style}|@setup| is called
%   (for the proof-ref style given via the
%   \cmdoptionfmt{proofenvstyle=\meta{style name}} key-value option to
%   |\phfMakeTheorem|);
% \item The macro |\phfthm@def@label@thmlabel| is invoked, implementing the hack
%   on the |\label| macro;
% \item The macro |\phfthm@proofref@impl@start| is called.  This macro is
%   expected to be defined after calling |\phfthm@proofrefstyle@|\meta{proof-ref
%   style}|@setup|.
% \end{itemize}
% 
% Furthermore, the |\phfthm@hook@afterlabel@mytheoremenv| hook will include a
% call to |\phfthm@proofref@impl@afterlabel|\marg{label of the theorem}.  Again,
% the latter macro is expected to be defined after calling
% |\phfthm@proofrefstyle@|\meta{proof-ref style}|@setup|.
%
% Finally, the hook |\phfthm@hook@end@mytheoremenv| includes a call to
% |\phfthm@proofref@impl@end|\marg{label of the theorem}.  Again,
% the latter macro is expected to be defined after calling
% |\phfthm@proofrefstyle@|\meta{proof-ref style}|@setup|.
%
%
% Hence, to define a new proof-ref style, you simply need to define a macro
% called |\phfthm@proofrefstyle@<PROOF-REF-STYLE-NAME>@setup|.  This macro
% should include commands to locally define the macros
% |\phfthm@proofref@impl@start|, |\phfthm@proofref@impl@afterlabel|, and
% |\phfthm@proofref@impl@end|.
%
%
% Different proof-ref styles may work similarly and want to share most of the
% code.  A good idea is to build up on the |default| proof-ref style, which is
% highly modular and can be instantiated in different flavors.  For an example,
% check the |margin| proof-ref style which does precisely that.  For more
% documentation, check out the implementation of the |default| proof-ref style
% in \autoref{sec:impl-default-proof-ref-style}.
%
%
%
%
% \section{Theorem-heading definition-like environments}
% \label{sec:thmheading}
%
% A theorem-heading environment is an environment which displays in the same way
% as a theorem environment, but where the title may be any text (say, ``Trace
% Distance'' instead of, e.g., ``Theorem~5'').
%
% By default, the \pkgname{phfthm} package provides the |thmheading| environment
% (see \autoref{sec:thmheading-default}).
%
%
% \subsection{Define theorem-heading environments manually}
% \label{sec:thmheading-manually}
% 
% \DescribeMacro{\phfMakeThmheadingEnvironment} A new theorem-heading
% environment can be defined by calling |\phfMakeThmheadingEnvironment|.
% The syntax is:
%
% \noindent |\phfMakeThmheadingEnvironment|\oarg{key-value options}\marg{environment name}
%
% The key-value options may be any combination of the following:
% \begin{cmdoptions}
% \item[thmstyle=\meta{theorem style name}] The theorem style to use to display
%   the environment.  You may specify here any default \emph{AMS} style
%   (|plain|, |remark| or |definition|), or any other |\newtheoremstyle|-defined
%   style.
% \item[internalcounter=\meta{name of counter}] The name of a counter which will
%   internally track environment instances.  By default, a common internal
%   counter is used for all theorem-heading environments (named
%   |phfthmheadingcounter|).  The counter must be already defined (see \LaTeX's
%   |\newcounter|).
% \end{cmdoptions}
%
%
% You can also use |\label| and |\ref| (the latter simply displays the given
% title).
%
% \subsection{Available hooks for theorem-heading environments}
% 
% The hook |\phfthm@hook@thmheading@...@start|\marg{title} is invoked at start,
% within an internal environment created with |\newtheorem|.  This hook accepts
% one argument, the title of the theorem-heading.
%
% The hook |\phfthm@hook@thmheading@...@end| is called at the end, but still
% within the internal theorem environment.
%
% Replace the dots with the name of the theorem-heading environment (such as
% |thmheading|).
%
% By default, these hooks simply call the common hooks
% |\phfthm@hook@thmheading@start| and |\phfthm@hook@thmheading@end|.  These
% common hooks are empty by default.
%
%
%
%
%
% \StopEventually{\PrintChangesAndIndex}
%
%
%
% \section{Implementation}
% \label{sec:implementation}
%
% First, load some packages.  General toolboxes:
%    \begin{macrocode}
\RequirePackage{xkeyval}
\RequirePackage{etoolbox}
%    \end{macrocode}
% 
% To define alias counters for theorems, load \pkgname{aliascnt}:
%    \begin{macrocode}
\RequirePackage{aliascnt}
%    \end{macrocode}
% 
% And finally, load the \textit{AMS} math and theorem (\pkgname{amsmath},
% \pkgname{amsthm}) packages:
%    \begin{macrocode}
\RequirePackage{amsmath}
\RequirePackage{amsthm}
%    \end{macrocode}
% 
%
% \subsection{Generic Internal Stuff}
%
% \begin{macro}{\phfthm@internal@execattribs}
%
%   Internal command: execute all definitions given in list of attributes.  This was
%   copy-pasted from a similar definition in the \pkgname{phfnote} package.
%
%   |#1| = prefix to look for attributes
%   
%   |#2| = name of what |#1| represents, to use in message in case attribute is not found
%   
%   |#3| = list of attributes
%
%    \begin{macrocode}
\def\phfthm@internal@execattribs#1#2#3{%
  \@for\next:=#3\do{%
    \ifcsname #1\next\endcsname%
      \csname #1\next\endcsname%
    \else%
      \PackageWarning{phfthm}{Unknown #2: '\next'. Ignoring.}
    \fi
  }
}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsection{Definitions for theorem environments}
%
% \subsubsection{\phfverb{\phfMakeTheorem}: definition of a new theorem environment}
%
% First, define some key-value syntax accepted by the |\phfMakeTheorem| command.
%    \begin{macrocode}
\define@cmdkey{phfmkthm}{counter}{}
\define@boolkey{phfmkthm}{aliascounter}[true]{}
\define@cmdkey{phfmkthm}{thmstyle}{}
\define@boolkey{phfmkthm}{defnostar}[true]{}
\define@boolkey{phfmkthm}{defstar}[true]{}
\define@boolkey{phfmkthm}{proofref}[true]{}
\define@cmdkey{phfmkthm}{proofrefstyle}{}
%    \end{macrocode}
% 
% \begin{macro}{\phfMakeTheorem}
%   Define a new theorem environment.  The syntax is
%   |\phfMakeTheorem|\hspace{0pt}\oarg{options}\hspace{0pt}\marg{theorem
%   environment name}\hspace{0pt}\marg{Theorem Display Name}.  For example:
%   |\phfMakeTheorem[counter=thmcounter]{prop}{Proposition}|
%    \begin{macrocode}
\newcommand\phfMakeTheorem[3][]{% }
%    \end{macrocode}
% Handle the [options].  First, ensure that the defaults are set, and then, parse the input.
%    \begin{macrocode}
  \KV@phfmkthm@aliascountertrue%
  \def\cmdKV@phfmkthm@counter{}%
  \def\cmdKV@phfmkthm@thmstyle{}%
  \KV@phfmkthm@defnostartrue%
  \KV@phfmkthm@defstartrue%
  \KV@phfmkthm@proofreftrue%
  \def\cmdKV@phfmkthm@proofrefstyle{default}%
  \setkeys{phfmkthm}{#1}%
%    \end{macrocode}
% 
% Now, react to whatever was given in the options.
%
% \verbdef\tmptheifconstruct|\if\relax\detokenize{...}\relax|
% Set the theorem style, if requested.\footnote{The construct
% \tmptheifconstruct\space tests whether \phfverb{...} is empty: see
% \url{http://tex.stackexchange.com/a/53091/32188}}
%    \begin{macrocode}
  \if\relax\detokenize\expandafter{\cmdKV@phfmkthm@thmstyle}\relax%
  \else%
    \theoremstyle{\cmdKV@phfmkthm@thmstyle}%
  \fi%
%    \end{macrocode}
% 
% If requested, define the default, unstarred version of the theorem.  Use
% |\newtheorem| for that, which we make sure to call appropriately depending on
% whether a separate counter is requested or not.  Make sure also to define
% |\...autorefname| for |\autoref|.  If an alias counter is requested, create it
% and pass that one to |\newtheorem|.
%
% At this point, we create a theorem named |phfthm@...| using |\newtheorem|
% (because we still want to add calls to hooks).
%    \begin{macrocode}
  \ifKV@phfmkthm@defnostar%
    \if\relax\detokenize\expandafter{\cmdKV@phfmkthm@counter}\relax%
%    \end{macrocode}
% ---in case we use a separate counter (if \cmdoptionfmt{counter=}):
%    \begin{macrocode}
      \newtheorem{phfthm@#2}{#3}%
      \csdef{phfthm@#2autorefname}{#3}%
    \else%
      \ifKV@phfmkthm@aliascounter%
%    \end{macrocode}
% ---in case we make a distinct alias counter, eg. for use with |\autoref|:
%    \begin{macrocode}
        \newaliascnt{#2}{\cmdKV@phfmkthm@counter}%
        \newtheorem{phfthm@#2}[#2]{#3}%
        \aliascntresetthe{#2}%
        \csdef{#2autorefname}{#3}%
      \else%
%    \end{macrocode}
% ---in case we directly instruct |\newtheorem| to use the other counter (does not work with |\autoref|):
%    \begin{macrocode}
        \newtheorem{phfthm@#2}[\cmdKV@phfmkthm@counter]{#3}%
      \fi%
    \fi%
%    \end{macrocode}
%
% And also define the actual theorem environment, adding calls to hooks.
%    \begin{macrocode}
    \newenvironment{#2}[1][]{%
      \begin{phfthm@#2}[##1]%
        \begingroup%
          \csname phfthm@hook@start@#2\endcsname{##1}%
      }{%
          \csname phfthm@hook@end@#2\endcsname%
        \endgroup%
      \end{phfthm@#2}%
    }%
%    \end{macrocode}
% Define hooks specific to this theorem with sensible defaults.  If proof-ref is
% on, call the appropriate callbacks. Then, call the common hooks (see
% |\phfthm@hook@startcommonnostar|, |\phfthm@hook@afterlabelcommon| and
% |\phfthm@hook@endcommonnostar|, detailed in \autoref{sec:theorem-hooks}).
%    \begin{macrocode}
    \csedef{phfthm@hook@start@#2}##1{%
      \ifKV@phfmkthm@proofref%
        \expandafter\noexpand%
          \csname phfthm@proofrefstyle@\cmdKV@phfmkthm@proofrefstyle @setup\endcsname%
        \noexpand\phfthm@def@label@thmlabel{#2}%
        \noexpand\phfthm@proofref@impl@start%
      \fi%
      \noexpand\phfthm@hook@startcommonnostar{#2}{##1}%
    }%
    \csedef{phfthm@hook@afterlabel@#2}{%
      \ifKV@phfmkthm@proofref%
        \noexpand\phfthm@proofref@expandthmlabeltoarg%
          \noexpand\phfthm@proofref@impl@afterlabel%
      \fi%
      \noexpand\phfthm@hook@afterlabelcommon{#2}%
    }%
    \csedef{phfthm@hook@end@#2}{%
      \ifKV@phfmkthm@proofref%
        \noexpand\phfthm@proofref@expandthmlabeltoarg%
          \noexpand\phfthm@proofref@impl@end%
      \fi%
      \noexpand\phfthm@hook@endcommonnostar{#2}%
    }%
  \fi%
%    \end{macrocode}
% 
%
% If requested, define the starred version of the theorem.  We call
% |\newtheorem*| to define the base theorem environment (which we call
% |phfthm@...|), after which as above we define the actual environment which
% also calls the relevant hooks.
%    \begin{macrocode}
  \ifKV@phfmkthm@defstar%
    \newtheorem*{phfthm@#2*}{#3}%
    \newenvironment{#2*}[1][]{%
      \begin{phfthm@#2*}[##1]%
        \begingroup%
          \csname phfthm@hook@start@#2*\endcsname{##1}%
      }{%
          \csname phfthm@hook@end@#2*\endcsname%
        \endgroup%
      \end{phfthm@#2*}%
    }%
  \fi%
%    \end{macrocode}
% 
% Finally, define the default hooks specific to the starred version of the
% theorem (see \autoref{sec:theorem-hooks}).
%    \begin{macrocode}
  \csdef{phfthm@hook@start@#2*}##1{\phfthm@hook@startcommonstar{#2}{##1}}%
  \csdef{phfthm@hook@end@#2*}{\phfthm@hook@endcommonstar{#2}}%
}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsubsection{Default hooks for theorems}
%
% \needspace{3\baselineskip}
% \begin{macro}{\phfthm@hook@startcommonnostar}
% \begin{macro}{\phfthm@hook@startcommonstar}
% \begin{macro}{\phfthm@hook@startcommon}
% Common default hooks definitions for start of the theorems.
%
% For all three of these hooks, we have
% |#1| = theorem name, e.g. |proposition| and 
% |#2| = full (optional) title of proposition, if given, or empty.
%
% Make sure to invoke the |\label| re-definition hack only for non-starred
% theorems/propositions; indeed, if no theorem label is set we don't want to
% interfere with labels set to inner equations, itemizes etc.  Hence, call
% |\phfhtm@def@label@thmlabel| only in the ``nostar'' hook.
%    \begin{macrocode}
\def\phfthm@hook@startcommonnostar#1#2{%
  \phfthm@hook@startcommon{#1}{#2}%
}
\def\phfthm@hook@startcommonstar#1#2{%
  \phfthm@hook@startcommon{#1}{#2}%
}
\def\phfthm@hook@startcommon#1#2{%
%    \end{macrocode}
% Furthermore, in any case, set the |\postdisplaypenalty| to avoid an orphan
% line on a new page after display equation.
%    \begin{macrocode}
  \postdisplaypenalty=10000\relax%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
% \begin{macro}{\phfthm@hook@afterlabelcommon}
% \begin{macro}{\phfthm@hook@endcommonnostar}
% \begin{macro}{\phfthm@hook@endcommonstar}
% \begin{macro}{\phfthm@hook@endcommon}
%   Further hooks, for after the theorem main |\label| command
%   (|\phfthm@hook@afterlabelcommon|) and for the end of the theorem.
%    \begin{macrocode}
\def\phfthm@hook@afterlabelcommon#1{}
\def\phfthm@hook@endcommonnostar#1{\phfthm@hook@endcommon{#1}}
\def\phfthm@hook@endcommonstar#1{\phfthm@hook@endcommon{#1}}
\def\phfthm@hook@endcommon#1{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Proof-ref mechanism (on the theorem side)}
%
% These macros enable the proof-ref mechanism
% (\autoref{sec:proof-ref-mechanism}).  The theorem's label is stored upon
% calling |\label|, because we (locally) hack into the definition of |\label|.
% (After the first usage of |\label| its meaning is restored.)
% \begin{macro}{\phfthm@def@label@thmlabel}
%   Main macro to invoke at the beginning of the theorem environment, so that
%   the theorem label is stored in a local macro once |\label| is invoked.  This
%   hacks the |\label| macro locally.  Here, |#1| = the theorem environment
%   name, e.g.\@ |proposition|.
%    \begin{macrocode}
\def\phfthm@def@label@thmlabel#1{%
  \ifdefined\phfthm@old@label
    \PackageWarning{phfthm}{Internal inconsistency: \string\phfthm@def@label@thmlabel
      called twice for the same theorem environment!}
  \else
    \let\phfthm@old@label\label%
    \edef\label{\noexpand\phfthm@thmlabel{#1}}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\phfthm@thmlabel}
%   The first call to |\label| within the theorem redirects to the macro
%   |\phfthm@thmlabel|.  (Applies to theorem environments for which
%   |\phfthm@def@label@thmlabel| was called, which is the default).
%
%   Here |#1| = theorem environment name, e.g.\@ |proposition|; and |#2| = the
%   label value (argument to the |\label| macro).
%    \begin{macrocode}
\def\phfthm@thmlabel#1#2{%
%    \end{macrocode}
% First, store the label value into a macro called |\phfthm@val@thmlabel|.
%    \begin{macrocode}
  \def\phfthm@val@thmlabel{#2}%
%    \end{macrocode}
% Then, call the original |\label| macro to do what \LaTeX\space would normally
% do for a |\label{|\ldots|}| call.
%    \begin{macrocode}
  \phfthm@old@label{#2}%
%    \end{macrocode}
% Restore the old |\label| definition, in case there are other items in the
% theorem environment such as equations, itemizes etc.\@ which may themselves
% have |\label|'s.
%    \begin{macrocode}
  \let\label\phfthm@old@label%
%    \end{macrocode}
% Invoke the |\phfthm@hook@afterlabel@thmname| hook for this theorem
% environment.
%    \begin{macrocode}
  \csname phfthm@hook@afterlabel@#1\endcsname%
%    \end{macrocode}
% Finally, ignore any spaces following the |\label| command. (Maybe we should
% have done something with |\@bsphack| and |\@esphack| but oh well\ldots
%    \begin{macrocode}
  \ignorespaces%
}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsection{Definitions for proof environments}
%
% Improved, smarter |proof| environments.
%
% \begin{macro}{\phfthm@old@proof}
% \begin{macro}{\endphfthm@old@proof}
%   Save old |proof| environment provided by \pkgname{amsthm}.
%    \begin{macrocode}
\let\phfthm@old@proof\proof
\let\endphfthm@old@proof\endproof
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\proofname}
% And provide a default name for proofs (this should normally already be
% provided by \pkgname{amsthm}).
%    \begin{macrocode}
\providecommand\proofname{Proof}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\proofofname}
%   Default text to display when we want to say e.g.\@ ``Proof of Theorem 3.''
%    \begin{macrocode}
\def\proofofname#1{\proofname\space of #1}
%    \end{macrocode}
% \end{macro}
%
% The default counter for proofs.  The value of this counter is typically not
% displayed, we just use it to pin down anchors for labels for cross references.
%    \begin{macrocode}
\newcounter{phfthmproofcnt}
%    \end{macrocode}
% 
% Utility: to see if an argument was specified (possibly empty) to the proof
% environment.
%    \begin{macrocode}
\def\phfthm@NOPROOFARG{}
\def\phfthm@test@NOPROOFARG{\phfthm@NOPROOFARG}
%    \end{macrocode}
% 
% \subsubsection{Define a proof environment: \phfverb{\phfMakeProofEnv}}
%
% Declare some key-value options accepted by |\phfMakeProofEnv|.  See
% \autoref{sec:mk-proof-env} for the documentation of these options.
%    \begin{macrocode}
\define@cmdkey{phfmkprf}{displayenv}{}
\define@cmdkey{phfmkprf}{defaultproofname}{}
\define@boolkey{phfmkprf}{override}[true]{}
\define@cmdkey{phfmkprf}{internalcounter}{}
\define@cmdkey{phfmkprf}{proofofname}{}
\define@boolkey{phfmkprf}{parselabel}[true]{}
\define@cmdkey{phfmkprf}{parselabelcmd}{}
%    \end{macrocode}
%
% \begin{macro}{\phfMakeProofEnv}
%   Make a proof environment. Syntax:
%   |\phfMakeProofEnv|\hspace{0pt}\oarg{options}\hspace{0pt}\marg{proof
%   environment name}.
%    \begin{macrocode}
\newcommand\phfMakeProofEnv[2][]{%}
%    \end{macrocode}
%
% Parse the key-value options.  First, make sure that all the defaults are set,
% then parse the options.
%    \begin{macrocode}
  \KV@phfmkprf@overridefalse%
  \def\cmdKV@phfmkprf@displayenv{*}%
  \def\cmdKV@phfmkprf@defaultproofname{\proofname}%
  \def\cmdKV@phfmkprf@internalcounter{phfthmproofcnt}%
  \def\cmdKV@phfmkprf@proofofname{\proofofname}%
  \KV@phfmkprf@parselabeltrue
  \def\cmdKV@phfmkprf@parselabelcmd{\phfthm@proof@parselabel}%
  \setkeys{phfmkprf}{#1}%
%    \end{macrocode}
% 
% The meaning of the options are detailed in \autoref{sec:mk-proof-env}.
% 
% The general idea here is first to pre-process all the options, and save all
% the useful information in macros named
% |\phfthm@prfenv@<proof-environment-name>@val@<something>|.  Then, we can define
% the begin/end environment macros which will recall the saved information.
%
% Take care of the display environment to use.  Recall that if |displayenv=*|,
% we use our own default; if |displayenv=| (empty), there is no display
% environment.  Here, we set
% |\phfthm@prfenv@<proof-environment-name>@val@displayenv| to the name of the
% environment to use (possibly empty), for later reference.
%    \begin{macrocode}
  \def\phfmkprf@tmp@star{*}%
  \ifx\cmdKV@phfmkprf@displayenv\phfmkprf@tmp@star\relax%
    \def\cmdKV@phfmkprf@displayenv{phfthm@proof@defaultdisplayenv}%
  \fi
  \cslet{phfthm@prfenv@#2@val@displayenv}\cmdKV@phfmkprf@displayenv%
%    \end{macrocode}
% 
% Process the default proof name.  If none is given, use |\proofname| and pass
% no option to the underlying display environment whenever the proof environment
% is called with no option.  Here, we set
% |\phfthm@prfenv@<proof-environment-name>@val@defaultproofnameargs| and
% |\phfthm@prfenv@<proof-environment-name>@val@setdefaultprooftitle|; the former
% is the tokens to put in front of the proof environment invocation in case no
% explicit proof title is given to the proof environment while the latter
% contains the command to set |\phfthm@val@prooftitle| to the default proof
% name.
%    \begin{macrocode}
  \if\relax\detokenize\expandafter{\cmdKV@phfmkprf@defaultproofname}\relax%
    \csdef{phfthm@prfenv@#2@val@defaultproofnameargs}{}%
    \csdef{phfthm@prfenv@#2@val@setdefaultprooftitle}{%
      \def\phfthm@val@prooftitle{\proofname}}%
  \else
    \csedef{phfthm@prfenv@#2@val@defaultproofnameargs}{%
      [\expandonce{\cmdKV@phfmkprf@defaultproofname}]}%
    \csedef{phfthm@prfenv@#2@val@setdefaultprooftitle}{%
      \noexpand\def\noexpand\phfthm@val@prooftitle{%
        \expandonce{\cmdKV@phfmkprf@defaultproofname}}}%
  \fi
  \csedef{phfthm@prfenv@#2@val@parselabelandmkdisplayargs}##1{%
    \ifKV@phfmkprf@parselabel
      \expandonce\cmdKV@phfmkprf@parselabelcmd{##1}%
    \else
      \noexpand\phfthm@proof@noparselabel{##1}%
    \fi
    \noexpand\def\noexpand\phfthm@val@displayargs{[{%
        \expandafter\noexpand\csname phfthm@prfenv@#2@val@proofofname\endcsname
        {\noexpand\phfthm@val@prooftitle}%
      }]}%
  }
%    \end{macrocode}
% 
% Store the macro which creates the ``Proof of \ldots'' text (|proofofname|
% option).
%    \begin{macrocode}
  \cslet{phfthm@prfenv@#2@val@proofofname}\cmdKV@phfmkprf@proofofname%
%    \end{macrocode}
% 
% Create the macro which will take care of pinning down the label for the
% proof-ref (see \autoref{sec:proof-ref-mechanism}). This macro first ref-steps
% the internal counter and then pins down a label, if appropriate.
%    \begin{macrocode}
  \csdef{phfthm@prfenv@#2@val@pinproofanchor}{%
    \csname phfthm@prfenv@#2@val@refstepinternalcounter\endcsname%
    \if\relax\detokenize\expandafter{\phfthm@val@proofoflabel}\relax\else%
      \edef\phfthm@tmp@larg{{proof:\phfthm@val@proofoflabel}}%
      \expandafter\label\phfthm@tmp@larg%
    \fi
  }%
  %
%    \end{macrocode}
% 
% The command to ref-step the internal proof counter.  Use the value of the
% \cmdoptionfmt{internalcounter} command option.
%    \begin{macrocode}
  \csedef{phfthm@prfenv@#2@val@refstepinternalcounter}{%
    \noexpand\refstepcounter{\cmdKV@phfmkprf@internalcounter}}%
%    \end{macrocode}
% 
% Make macros |\phfthm@prfenv@<proof-environment-name>@val@displayenvbegincmd|
% and |\phfthm@prfenv@<proof-environment-name>@val@displayenvendcmd|, which
% essentially expand to |\begin{<the-display-env>}| and
%   |\end{<the-display-env>}| for the display environment given in the option
% \cmdoptionfmt{displayenv}.
%    \begin{macrocode}
  \if\relax\detokenize\expandafter{\cmdKV@phfmkprf@displayenv}\relax%
    \csdef{phfthm@prfenv@#2@val@displayenvbegincmd}##1{}%
    \csdef{phfthm@prfenv@#2@val@displayenvendcmd}##1{}%
  \else
    \csedef{phfthm@prfenv@#2@val@displayenvbegincmd}##1{%
      \noexpand\begin{\csname phfthm@prfenv@#2@val@displayenv\endcsname}##1}%
    \csedef{phfthm@prfenv@#2@val@displayenvendcmd}##1{%
      \noexpand\end{\csname phfthm@prfenv@#2@val@displayenv\endcsname}##1}%
  \fi
%    \end{macrocode}
% 
% See if we need to call |\newenvironment| or |\renewenvironment|, depending on
% the value of the \cmdoptionfmt{override} option.
%    \begin{macrocode}
  \def\phfthm@tmp@defcmd{\newenvironment}%
  \ifKV@phfmkprf@override\def\phfthm@tmp@defcmd{\renewenvironment}\fi%
%    \end{macrocode}
% 
% Finally, (re-)define the environment.  The default value of the optional
% argument is the token |\phfthm@NOPROOFARG|, which indicates that no argument
% was provided.
%
% Start by storing the value of the argument into a macro, and then call the
% ``start'' hook (see proof hooks in \autoref{sec:proof-hooks}).
%    \begin{macrocode}
  \phfthm@tmp@defcmd{#2}[1][\phfthm@NOPROOFARG]{%
    \def\phfthm@val@proofarg{##1}%
    \csname phfthm@hookproof@#2@start\endcsname%
%    \end{macrocode}
% 
% First, parse the optional argument into proof label (maybe) and proof title.
% If no optional argument was given, don't give any argument to the underlying
% display environment.  If an empty argument was given, set some defaults;
% otherwise, use the necessary command to potentially parse the label and create
% the proper arguments for the underlying display environment.
%    \begin{macrocode}
    \ifx\phfthm@val@proofarg\phfthm@test@NOPROOFARG\relax%
      \def\phfthm@val@proofoflabel{}%
      \csname phfthm@prfenv@#2@val@setdefaultprooftitle\endcsname%
      \letcs\phfthm@val@displayargs{phfthm@prfenv@#2@val@defaultproofnameargs}%
    \else%
      \if\relax\detokenize{##1}\relax%
        \def\phfthm@val@proofoflabel{}%
        \csname phfthm@prfenv@#2@val@setdefaultprooftitle\endcsname%
        \def\phfthm@val@displayargs{[{%
            \csname phfthm@prfenv@#2@val@proofofname\endcsname
            {\phfthm@val@prooftitle}%
          }]}%
      \else
        \csname phfthm@prfenv@#2@val@parselabelandmkdisplayargs\endcsname{##1}%
      \fi
    \fi%
%    \end{macrocode}
% 
% Define the |\phfPinProofAnchor| command (locally) in case the
% display formatting environment takes care of where to place the anchor already.
%    \begin{macrocode}
    \def\phfPinProofAnchor{%
      \csname phfthm@prfenv@#2@val@pinproofanchor\endcsname%
      \global\let\phfPinProofAnchor\relax}%
%    \end{macrocode}
% [Also provide the obsolete |\phfthmPinProofAnchor| which I previously had in
% older versions of this package:]
%    \begin{macrocode}
    \def\phfthmPinProofAnchor{\phfPinProofAnchor}%
%    \end{macrocode}
% 
% Start the proof's display environment.  Don't be fooled here by the curly
% braces after |\x|, it only protects the argument to the
% |\phfthm@prfenv@#2@val@displayenvbegincmd| command itself: the
% |\phfthm@val@displayargs| are still just tokens which will be expanded in
% front of the |\beg||in{<proof-display-env>}| command.
%    \begin{macrocode}
    \def\x{\csname phfthm@prfenv@#2@val@displayenvbegincmd\endcsname}%
    \expandafter\x\expandafter{\phfthm@val@displayargs}%
%    \end{macrocode}
% And call the corresponding hook:
%    \begin{macrocode}
    \csname phfthm@hookproof@#2@startafterdisplay\endcsname%
%    \end{macrocode}
% If required, pin anchor after the proof-display-environment.
% (|\phfPinProofAnchor| auto-destructs after first use, so it's safe to
% potentially call it a second time here). Then, call the corresponding hook.
%    \begin{macrocode}
    \phfPinProofAnchor%
    \expandafter\noexpand\csname phfthm@hookproof@#2@startlast\endcsname%
  }%
%    \end{macrocode}
%
% Now, the definitions for the ``end'' part of the environment.  Just call the
% relevant hooks and close the display environment.
%    \begin{macrocode}
  {%
    \expandafter\noexpand\csname phfthm@hookproof@#2@end\endcsname%
    \csname phfthm@prfenv@#2@val@displayenvendcmd\endcsname
    \expandafter\noexpand\csname phfthm@hookproof@#2@final\endcsname%
  }%
%    \end{macrocode}
% 
% Finally, define the default values of the proof-environment-specific hooks.
% These just call the corresponding global hooks (see \autoref{sec:proof-hooks}).
%    \begin{macrocode}
  \csdef{phfthm@hookproof@#2@start}{\phfthm@hookproof@startcommon{#2}}%
  \csdef{phfthm@hookproof@#2@startafterdisplay}{%
    \phfthm@hookproof@startafterdisplaycommon{#2}}%
  \csdef{phfthm@hookproof@#2@startlast}{\phfthm@hookproof@startlastcommon{#2}}%
  \csdef{phfthm@hookproof@#2@end}{\phfthm@hookproof@endcommon{#2}}%
  \csdef{phfthm@hookproof@#2@final}{\phfthm@hookproof@finalcommon{#2}}%
}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsubsection{Common hooks for proofs}
%
% The hooks are documented in \autoref{sec:proof-hooks}.
%    \begin{macrocode}
\def\phfthm@hookproof@startcommon#1{}
\def\phfthm@hookproof@startafterdisplaycommon#1{}
\def\phfthm@hookproof@startlastcommon#1{}
\def\phfthm@hookproof@endcommon#1{}
\def\phfthm@hookproof@finalcommon#1{}
%    \end{macrocode}
% 
%
% \subsubsection{Default display environment for proofs}
%
% \begin{environment}{phfthm@proof@defaultdisplayenv}
%   Provide an environment which displays a proof in a similar fashion as
%   \emph{AMS}', but with some small additional features.
%    \begin{macrocode}
\newenvironment{phfthm@proof@defaultdisplayenv}[1][\proofname]{%
  \par
  \pushQED{\qed}%
  \normalfont \topsep6\p@\@plus6\p@\relax
  \trivlist\item\relax
  \phfPinProofAnchor
  \phfthm@ProofTitleFmt{#1}%
  \phfthm@ProofTitleHspace
  \ignorespaces
}{%
  \popQED\endtrivlist\@endpefalse
}
%    \end{macrocode}
% \end{environment}
% 
% \begin{macro}{\phfthm@ProofTitleFmt}
% \begin{macro}{\phfthm@ProofTitleHspace}
%   These macros may be overridden to change the proof title appearance.
%    \begin{macrocode}
\def\phfthm@ProofTitleFmt#1{%
  {\itshape #1.}%
}
\def\phfthm@ProofTitleHspace{%
  \hspace{1.5ex plus 0.5ex minus 0.2ex}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Parsing the proof argument}
%
% These macros parse the argument of the proof environment to see if it is of
% the form |*<some-label>| (see \autoref{sec:proof-ref-mechanism}).
%
% \begin{macro}{\phfthm@proof@parselabel}
%   Call |\phfthm@proof@parselabel|\marg{proof environment argument} to parse
%   the argument string.  This macro will set |\phfthm@val@proofoflabel| and
%   |\phfthm@val@prooftitle| to appropriate values (respectively, the label name
%   of the corresponding theorem and a representative title such as ``Theorem
%   6'').
%    \begin{macrocode}
\def\phfthm@proof@parselabel#1{%
  \phfthm@proof@parselabel@maybelabel#1\phfthm@proof@parselabel@END%
}
\def\phfthm@proof@parselabel@maybelabel{%
  \@ifnextchar*\phfthm@proof@parselabel@label\phfthm@proof@parselabel@title%
}
\def\phfthm@proof@parselabel@label*#1\phfthm@proof@parselabel@END{%
%    \end{macrocode}
% The use of |\detokenize| here is a trick to make sure that all chars in the
% label text have a non-active category (e.g.\@ we would have problems, e.g., if
% in the label ``|thm:gauss|'' the ``|:|'' is an active char---such as in French):
%    \begin{macrocode}
  \edef\phfthm@val@proofoflabel{\detokenize{#1}}%
  \def\phfthm@val@prooftitle{\phfthm@autoref{#1}}%
}
\def\phfthm@proof@parselabel@title#1\phfthm@proof@parselabel@END{%
  \def\phfthm@val@proofoflabel{}%
  \def\phfthm@val@prooftitle{#1}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\phfthm@proof@noparselabel}
%   Enjoys the same syntax as |\phfthm@proof@parselabel|, i.e., it is a drop-in
%   replacement for the latter, except that it invariably sets
%   |\phfhtm@val@proofoflabel| to an empty value and |\phfthm@val@prooftitle| to
%   the argument itself.  You could use this as a \cmdoptionfmt{parselabelcmd}
%   macro if you didn't want to parse the label.
%    \begin{macrocode}
\def\phfthm@proof@noparselabel#1{%
  \def\phfthm@val@proofoflabel{}%
  \def\phfthm@val@prooftitle{#1}%
}
%    \end{macrocode}
% \end{macro}
% 
%
% In order to look up what we are a proof of, we use |\autoref| provided by the
% \pkgname{hyperref} package.  If it is not available, fall back to the regular
% |\ref| command.
%    \begin{macrocode}
\def\phfthm@autoref{\ref}
\AtBeginDocument{%
  \@ifpackageloaded{hyperref}{\def\phfthm@autoref{\autoref}}{}
}
%    \end{macrocode}
% 
%
% \subsection{Implementation of the proof-ref machinery}
%
% \subsubsection{Small general stuff}
%
% \begin{macro}{\proofonname}
%   The macro |\proofonname| displays ``Proof on \ldots.''  Here, |#2| is the
%   full page reference and |#1| is the label name of the referenced theorem.
%    \begin{macrocode}
\providecommand\proofonname[2]{Proof on #2.}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\proofrefsize}
%   Format the proof reference ``Proof on page \ldots''.  This macro is meant to
%   set the font size (or other font properties), but it may also be defined to
%   take one argument, the proof reference text.
%    \begin{macrocode}
\def\proofrefsize{\footnotesize}
%    \end{macrocode}
% \end{macro}
% 
%
% \begin{macro}{\noproofref}
%   Use |\noproofref| inside a theorem to signify that no proof reference should
%   be attempted.
%
%   The implementation just defines |\phfthm@val@noproofref|.  If this macro is
%   defined, then no proof ref should be generated for the current thmlabel.
%   Also, restore |\label| to its original definition in case it was overridden.
%    \begin{macrocode}
\def\noproofref{%
  \def\phfthm@val@noproofref{1}%
  \ifdefined\phfthm@old@label \let\label\phfthm@old@label \fi%
}
%    \end{macrocode}
% \end{macro}
%
% 
% \begin{macro}{\phfthm@proofref@warnnolabel}
%   Produce a warning that no label was provided in order to infer the proof
%   reference.
%    \begin{macrocode}
\def\phfthm@proofref@warnnolabel{%
  \PackageWarning{phfthm}{No label provided for proof reference!}%
}
%    \end{macrocode}
% \end{macro} 
%
% \begin{macro}{\phfthm@proofref@expandthmlabeltoarg}
%   Utility to expand the value of |\phfthm@val@thmlabel| as an argument to a
%   callback command.  |#1| = the macro to relay the call to.
%    \begin{macrocode}
\def\phfthm@proofref@expandthmlabeltoarg#1{%
%    \end{macrocode}
%
% First, check if the proof-ref mechanism was explicitly temporarily disabled,
% and do nothing if that is the case.
%    \begin{macrocode}
  \ifdefined\phfthm@val@noproofref\relax%
  \else%
%    \end{macrocode}
%
% Then make sure |\phfthm@val@thmlabel| is defined (maybe empty), and then
% either call the callback macro |#1| with the value of |\phfthm@val@thmlabel|
% as argument, or generate a warning if that value is empty.
%    \begin{macrocode}
    \providecommand\phfthm@val@thmlabel{}%
    \edef\phfthm@tmpa{{\phfthm@val@thmlabel}}%
    \expandafter\notblank\phfthm@tmpa{%
      \expandafter#1\phfthm@tmpa%
    }{%
      \phfthm@proofref@warnnolabel% no label provided
    }%
  \fi%
}
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Utilities for interacting with \phfverb{\autoref} labels}
%
% In this context, we also need some generic utilities for interacting with
% |\autoref| labels.
%
% \begin{macro}{\phfthm@autorefnameof}
%   The macro |\phfthm@autorefnameof| extracts the name of the counter which
%   generated this reference (e.g.\@ ``section'' or ``theorem'').
%    \begin{macrocode}
\def\phfthm@autorefnameof#1{%
%    \end{macrocode}
% 
% Extract the counter part of the reference |section.NN|, which is 4th element in the
% |\r@label| macro. (Code extracted from |hyperref.sty|.)
%    \begin{macrocode}
  \expandafter\ifx\csname r@#1\endcsname\relax%
    \textbf{??}%
  \else%
    \expandafter\expandafter\expandafter\phfthm@HyPsd@autorefname%
        \csname r@#1\endcsname{}{}{}{}\@nil%
  \fi%
}
\def\phfthm@HyPsd@autorefname#1#2#3#4#5\@nil{%
  \ifx\\#4\\%
  \else%
    \phfthm@HyPsd@@autorefname#4.\@nil%
  \fi%
}
\def\phfthm@HyPsd@@autorefname#1.#2\@nil{%
  \ltx@IfUndefined{#1autorefname}{%
    \ltx@IfUndefined{#1name}{%
    }{%
      \csname#1name\endcsname%
    }%
  }{%
    \csname#1autorefname\endcsname%
  }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\phfthm@min@pageref}
%   A minimal pageref macro, which just extracts the page number on which the
%   given label is located.
% 
%   The dark magic going on here is beyond me. The code was copied from
%   |hyperref.sty|, in ``|\def\HyPsd@@@pageref...|'' and seems to work.
% 
%    \begin{macrocode}
\def\phfthm@min@pageref#1{%
  \ifcsname r@#1\endcsname%
    \expandafter\expandafter\expandafter\expandafter
    \expandafter\expandafter\expandafter\@car
    \expandafter\expandafter\expandafter\@gobble
    \csname r@#1\endcsname{}\@nil
  \else%
    0%
  \fi%
}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsubsection{Default proof-ref style, with basic machinery}
% \label{sec:impl-default-proof-ref-style}
%
% Now we define the relevant callbacks for the default style.  See documentation
% in \autoref{sec:proof-ref-customize-appearance}.  Recall a proof-ref style
% just needs to define |\phfthm@proofrefstyle@<stylename>@setup|, which in turn
% should just define the callbacks |\phfthm@proofref@impl@start|,
% |\phfthm@proofref@impl@afterlabel| and |\phfthm@proofref@impl@end|.  For our
% default style, these callbacks further call other callbacks of the form
% |\phfthm@proofref@impl@...|, such that these definitions can be re-used to
% create new styles.  The main proof-ref generation routine is
% |\phfthm@proofrefstyle@default@main|, which can be used for either the
% |...@afterlabel| or the |...@end| callback.
%
% \begin{macro}{\phfthm@proofrefstyle@default@fmt}
%   Format and display the proof reference. |#1| = the theorem's label
%   (e.g. |prop:1|); |#2| = the full reference (e.g. ``page XYZ'').
%
%   This macro is the default value of the callback |\phfthm@proofref@impl@fmt|,
%   which is called by the default style itself.
%
%   Use correct spacing for right-aligning the reference.\footnote{Thanks
%   \url{http://tex.stackexchange.com/a/43239/32188}!}  If there is room on the
%   current line, just right-align the proof-ref text; if not, add it on a
%   separate line.  [We can achieve this with the sequences |\hfil\null\hfil|:
%   if there is space, it all fits on the same line, if not, the line breaks at
%   the |\null| point.]
%    \begin{macrocode}
\def\phfthm@proofrefstyle@default@fmt#1#2{%
  {\parfillskip=0pt\relax%
    \hfil\null\hfil\null\hfil%
    \hbox{\proofrefsize{(\proofonname{#1}{#2})}}\par}%
}
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\phfthm@proofrefstyle@default@fmtfarback}
% \begin{macro}{\phfthm@proofrefstyle@default@fmtfarahead}
% \begin{macro}{\phfthm@proofrefstyle@default@fmtcloseby}
%   These macros are the default values of the callbacks
%   |\phfthm@proofref@impl@fmtfarback|, |\phfthm@proofref@impl@fmtfarahead|, and
%   |\phfthm@proofref@impl@fmtcloseby|, which are called by the default style
%   itself.  These callbacks define how to format and (possibly not) display the
%   proof reference depending on whether the proof is ``far behind'' (several
%   pages back), ``far ahead'' (several pages ahead) or ``close by'' (neither
%   far back nor far ahead), as defined by |\phfProofrefPageBackTolerance|
%   and |\phfProofrefPageAheadTolerance|.
%    \begin{macrocode}
\def\phfthm@proofrefstyle@default@fmtfarback#1#2{%
  \phfthm@proofref@impl@fmt{#1}{#2}}
\def\phfthm@proofrefstyle@default@fmtfarahead#1#2{%
  \phfthm@proofref@impl@fmt{#1}{#2}}
\def\phfthm@proofrefstyle@default@fmtcloseby#1#2{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\phfProofrefPageBackTolerance}
% \begin{macro}{\phfProofrefPageAheadTolerance}
%   The macros |\phfProofrefPageBackTolerance| and
%   |\phfProofrefPageAheadTolerance| define how many pages back or ahead the
%   proof should be in order to consider it ``far back'' or ``far ahead.''
%
%   Either value may be set to |-1| to force the proof to be considered ``far
%   back'' or ``far ahead.''
%    \begin{macrocode}
\newcommand\phfProofrefPageBackTolerance{1}
\newcommand\phfProofrefPageAheadTolerance{1}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% Define the internal counter which allows to check on which page we are at the
% place of the proof reference.  This is used by
% |\phfthm@proofrefstyle@default@main|.
%    \begin{macrocode}
\newcounter{phfthmInternalProofrefCounter}
%    \end{macrocode}
% 
% \begin{macro}{\phfthm@proofrefstyle@default@main}
%   The main proof-ref generation routine.  The argument |#1| is the current
%   label of the theorem; the referenced label is |proof:#1|.
%    \begin{macrocode}
\def\phfthm@proofrefstyle@default@main#1{%
%    \end{macrocode}
% 
% Check to see if the proof is far away ahead or back (as defined by the
% tolerance macros above).  Depending on each case, call the corresponding
% callbacks.{\makeatletter\footnote{See \url{http://tex.stackexchange.com/a/2526} to test
% whether ref is on same page. Note that was problematic, probably due to
% hyperref. I needed to use my own \phfverb{\phfthm@min@pageref}
% without any hyper linking mechanism in place.}}
%
%    \begin{macrocode}
  \refstepcounter{phfthmInternalProofrefCounter}%
  \label{internalproofref\thephfthmInternalProofrefCounter}%
  \edef\phfthm@proofref@tmp@proofpage{\phfthm@min@pageref{proof:#1}}%
  \edef\phfthm@proofref@tmp@thispage{%
    \phfthm@min@pageref{internalproofref\thephfthmInternalProofrefCounter}}%
  \edef\phfthm@proofref@tmp@pagediff{%
    \the\numexpr\phfthm@proofref@tmp@proofpage-\phfthm@proofref@tmp@thispage\relax}%
%    \end{macrocode}
% 
% If the proof is ``far back,'' call the corresponding callback. 
%    \begin{macrocode}
  \ifnum\numexpr\phfthm@proofref@tmp@pagediff\relax%
      <\numexpr-\phfProofrefPageBackTolerance\relax%
    \phfthm@proofref@impl@fmtfarback{#1}{\autopageref{proof:#1}}%
  \else%
%    \end{macrocode}
% 
% If the proof is ``far ahead,'' call the corresponding callback. 
%    \begin{macrocode}
    \ifnum\numexpr\phfthm@proofref@tmp@pagediff\relax%
        >\numexpr\phfProofrefPageAheadTolerance\relax%
      \phfthm@proofref@impl@fmtfarahead{#1}{\autopageref{proof:#1}}%
%    \end{macrocode}
% 
% Otherwise, it is close by.
%    \begin{macrocode}
    \else%
      \phfthm@proofref@impl@fmtcloseby{#1}{\autopageref{proof:#1}}%
    \fi%
  \fi%
%%  [\number\numexpr\phfthm@proofref@tmp@proofpage\relax{} vs % DEBUG
%%  \number\numexpr\phfthm@proofref@tmp@thispage\relax or % DEBUG
%%  \number\numexpr1+\phfthm@proofref@tmp@thispage\relax] % DEBUG
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\phfthm@proofrefstyle@default@setup}
%   The main set-up macro for the |default| proof-ref style.  It sets all the
%   call-backs to the default ones.
%    \begin{macrocode}
\def\phfthm@proofrefstyle@default@setup{%
  \let\phfthm@proofref@impl@start\relax
  \let\phfthm@proofref@impl@afterlabel\@gobble
  \let\phfthm@proofref@impl@end\phfthm@proofrefstyle@default@main
  \let\phfthm@proofref@impl@fmtfarback\phfthm@proofrefstyle@default@fmtfarback
  \let\phfthm@proofref@impl@fmtfarahead\phfthm@proofrefstyle@default@fmtfarahead
  \let\phfthm@proofref@impl@fmtcloseby\phfthm@proofrefstyle@default@fmtcloseby
  \let\phfthm@proofref@impl@fmt\phfthm@proofrefstyle@default@fmt
}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsubsection{Other proof-ref styles: only \phfverb{margin} for now}
%
% These styles simply use the same mechanism as the default style, but plug in
% different sub-callbacks.
%
% \begin{macro}{\phfthm@proofrefstyle@margin@setup}
%   Set-up macro for the ``|margin|'' proof-ref style (displays the proof
%   reference in the margin of the page).
%    \begin{macrocode}
\def\phfthm@proofrefstyle@margin@setup{%
  \phfthm@proofrefstyle@default@setup
%    \end{macrocode}
% 
% The proof reference should be displayed directly at the top, not at the end of
% the theorem, so plug in |\phfthm@proofref@default@main| onto |...@afterlabel|
% and not onto |...@end|.  Don't forget that these macros accept one argument,
% the theorem label.
%    \begin{macrocode}
  \let\phfthm@proofref@impl@afterlabel\phfthm@proofrefstyle@default@main
  \let\phfthm@proofref@impl@end\@gobble
%    \end{macrocode}
% 
% Define the formatting callback to put the note in the margin of the page using
% a |\marginpar|.  We need |\leavevmode| to make sure it's aligned properly
% vertically with the paragraph.\footnote{See
% \url{http://tex.stackexchange.com/a/16161/32188}}
%    \begin{macrocode}
  \def\phfthm@proofref@impl@fmt##1##2{%
    \leavevmode\marginpar{\proofrefsize{\proofonname{##1}{##2}}}%
  }%
}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsection{Thmheading definition-like environments}
%
%
% \subsubsection{Manually define a thmheading environment}
%
% Define the key-value options accepted by |\phfMakeThmheadingEnvironment|.
%    \begin{macrocode}
\define@cmdkey{phfthmmkthmheading}{thmstyle}{}
\define@cmdkey{phfthmmkthmheading}{internalcounter}{}
%    \end{macrocode}
% 
%    \begin{macrocode}
\newcounter{phfthmheadingcounter}%
%    \end{macrocode}
%
% \begin{macro}{\phfMakeThmheadingEnvironment}
%
%   Creates a new environment |\begin{thmheading}{Title}...\end{thmheading}| for
%   customizing the heading on-the-fly (see documentation in
%   \autoref{sec:thmheading}).  Useful for an alternative formatting of
%   definitions.  The syntax is:
%
%   \noindent|\phfMakeThmheadingEnvironment|\oarg{key-value options}\marg{environment name}
% 
%   You can also use |\label| and |\ref| (the latter simply displays the given
%   title).
%
% 
%    \begin{macrocode}
\newcommand\phfMakeThmheadingEnvironment[2][]{% }
%    \end{macrocode}
% 
% Parse the options. First set defaults, and then parse the input string.
%    \begin{macrocode}
  \def\cmdKV@phfthmmkthmheading@thmstyle{plain}%
  \def\cmdKV@phfthmmkthmheading@internalcounter{phfthmheadingcounter}%
  \setkeys{phfthmmkthmheading}{#1}%
%    \end{macrocode}
% 
% And now, produce the relevant definitions:
%    \begin{macrocode}
  \csdef{phfthm@thmheading@#2@val@title}{$\langle$No Title Given$\rangle$}%
  \theoremstyle{\cmdKV@phfthmmkthmheading@thmstyle}%
%    \end{macrocode}
% 
% We use |\newtheorem*| to create an unnumbered theorem. The fixed title is just
% a single token, the macro which will be set to the relevant title at the last
% moment.
%    \begin{macrocode}
  \newtheorem*{phfthm@internal@thmheading@#2}{%
    \csname phfthm@thmheading@#2@val@title\endcsname}%
%    \end{macrocode}
%
% Define the actual environment.
%    \begin{macrocode}
  \newenvironment{#2}[1]{%}
    \csdef{phfthm@thmheading@#2@val@title}{##1}%
    \letcs\thephfthmheadingcounter{phfthm@thmheading@#2@val@title}%
%    \end{macrocode}
% Relay call to the internal \textit{AMS}-defined ``theorem:''
%    \begin{macrocode}
    \csname phfthm@internal@thmheading@#2\endcsname%
%    \end{macrocode}
% Pin down an anchor.  The use of |\hspace*{0pt}| is explained at
% \url{http://tex.stackexchange.com/a/88493/32188} (see especially the first
% comment).
%    \begin{macrocode}
    \hspace*{0pt}\refstepcounter{\cmdKV@phfthmmkthmheading@internalcounter}%
    \csname phfthm@hook@thmheading@#2@start\endcsname{##1}%
%    \end{macrocode}
% Also, let's add some flexibility in the hspace:
%    \begin{macrocode}
    \hskip 0em plus 0.5em minus 0em%
    \ignorespaces%
  }%
%    \end{macrocode}
% 
% Now, the END part of the environment: just call the callback and close the
% internal AMS-defined theorem.
%    \begin{macrocode}
  {%
    \csname phfthm@hook@thmheading@#2@end\endcsname%
    \csname endphfthm@internal@thmheading@#2\endcsname%
  }%
%    \end{macrocode}
% 
% Also define the relevant callbacks, which just relay their calls to the
% default callbacks.
%    \begin{macrocode}
  \csdef{phfthm@hook@thmheading@#2@start}##1{%
    \phfthm@hook@thmheading@start{##1}}%
  \csdef{phfthm@hook@thmheading@#2@end}{\phfthm@hook@thmheading@end}%
}
%    \end{macrocode}
%
% Provide as well the obsolete command |\phfthmMakeThmheadingEnvironment| which
% was provided in earlier versions of this package:
%    \begin{macrocode}
\def\phfthmMakeThmheadingEnvironment{\phfMakeThmheadingEnvironment}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\phfthm@hook@thmheading@start}
% \begin{macro}{\phfthm@hook@thmheading@end}
%   Global callbacks which are called for all thmheading-type environments
%   defined with |\phfMakeThmheadingEnvironment| (unless their hooks have
%   been changed in order for them not to call these global hooks).
%    \begin{macrocode}
\def\phfthm@hook@thmheading@start#1{}
\def\phfthm@hook@thmheading@end{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% 
%
% \subsection{Theorem sets}
%
% Here, we define the theorem sets proposed by the package for quick loading.
%
% We first define the names.  These are defined in any case regardless of
% whether we are loading a theorem set or of which theorem set we are loading.
%    \begin{macrocode}
\def\theoremname{Theorem}
\def\propositionname{Proposition}
\def\lemmaname{Lemma}
\def\corollaryname{Corollary}
\def\conjecturename{Conjecture}
\def\remarkname{Remark}
\def\definitionname{Definition}
\def\ideaname{Idea}
\def\questionname{Question}
\def\problemname{Problem}
%    \end{macrocode}
% 
% As we define the theorem sets, remember the names in a comma-separated list
% which we can display in help text.  The |\phfthm@def@thmset| replaces the
% |\def| command and expects the definitions to follow immediately.
%    \begin{macrocode}
\def\phfthm@def@thmset@optlist{}
\def\phfthm@def@thmset#1{%
  \appto\phfthm@def@thmset@optlist{#1,}\csdef{phfthm@thmset@#1}}
%    \end{macrocode}
% 
%
% \begin{macro}{\phfthm@def@thmset@mktheorem}
% \begin{macro}{\phfthm@def@thmset@mkdefn}
%   In definitions of theorem sets, use these macros to define a new
%   theorem-like environment (theorem, proposition, corollary, etc.) or
%   definition-like environment (definition, remark).  The macros
%   |\phfthm@val@mkthmoptarg@theorem| and |\phfthm@val@mkthmoptarg@defn| are
%   defined by |\phfLoadThmSet|.
%    \begin{macrocode}
\def\phfthm@def@thmset@mktheorem{%
  \expandafter\phfMakeTheorem\phfthm@val@mkthmoptarg@theorem}
\def\phfthm@def@thmset@mkdefn{%
  \expandafter\phfMakeTheorem\phfthm@val@mkthmoptarg@defn}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% 
% The default set (empty name, or name ``|empty|'') provides no theorem.  (The
% first line uses |\def| directly so that we don't include an empty item in the
% list of available choices.)
%    \begin{macrocode}
\def\phfthm@thmset@{}
\phfthm@def@thmset{empty}{}
%    \end{macrocode}
% 
% Theorem set |simple|:
%    \begin{macrocode}
\phfthm@def@thmset{simple}{
  \phfthm@def@thmset@mktheorem{theorem}{\theoremname}
  \phfthm@def@thmset@mktheorem{proposition}{\propositionname}
  \phfthm@def@thmset@mktheorem{lemma}{\lemmaname}
  \phfthm@def@thmset@mktheorem{corollary}{\corollaryname}
  \phfthm@def@thmset@mkdefn{definition}{\definitionname}
}
%    \end{macrocode}
% 
% Theorem set |default|:
%    \begin{macrocode}
\phfthm@def@thmset{default}{
  \phfthm@def@thmset@mktheorem{theorem}{\theoremname}
  \phfthm@def@thmset@mktheorem{proposition}{\propositionname}
  \phfthm@def@thmset@mktheorem{lemma}{\lemmaname}
  \phfthm@def@thmset@mktheorem{corollary}{\corollaryname}
  \phfthm@def@thmset@mktheorem{conjecture}{\conjecturename}
  \phfthm@def@thmset@mktheorem{remark}{\remarkname}
  \phfthm@def@thmset@mkdefn{definition}{\definitionname}
}
%    \end{macrocode}
% 
% Theorem set |shortnames|:
%    \begin{macrocode}
\phfthm@def@thmset{shortnames}{
  \phfthm@def@thmset@mktheorem{thm}{\theoremname}
  \phfthm@def@thmset@mktheorem{prop}{\propositionname}
  \phfthm@def@thmset@mktheorem{lem}{\lemmaname}
  \phfthm@def@thmset@mktheorem{cor}{\corollaryname}
  \phfthm@def@thmset@mktheorem{conj}{\conjecturename}
  \phfthm@def@thmset@mktheorem{rem}{\remarkname}
  \phfthm@def@thmset@mkdefn{defn}{\definitionname}
}
%    \end{macrocode}
% 
% Theorem set |rich|. Add definitions to the |default| set:
%    \begin{macrocode}
\phfthm@def@thmset{rich}{
  \phfthm@thmset@default
  \phfthm@def@thmset@mktheorem{idea}{\ideaname}
  \phfthm@def@thmset@mktheorem{question}{\questionname}
  \phfthm@def@thmset@mktheorem{problem}{\problemname}
}
%    \end{macrocode}
% 
%
% \begin{macro}{\phfLoadThmSet}
%   The macro |\phfLoadThmSet| loads a theorem set.  See documentation at
%   \autoref{sec:load-thm-set-manually}.
%
%   |#1| = options to |\phfMakeTheorem| for theorem-like environments
%
%   |#2| = options to |\phfMakeTheorem| for definition-like environments
%
%   |#3| = name of the theorem set to load
%
%    \begin{macrocode}
\newcommand\phfLoadThmSet[3]{%
  \ifcsname phfthm@thmset@#3\endcsname%
    \edef\phfthm@val@mkthmoptarg@theorem{#1}%
    \edef\phfthm@val@mkthmoptarg@defn{#2}%
    \csname phfthm@thmset@#3\endcsname%
  \else%
    \PackageWarning{phfthm}{Unknown theorem set: `#3'!}%
  \fi%
}
%    \end{macrocode}
%
% For compatibility with my earlier versions of \pkgname{phfthm}, also provide
% the obsolete |\phfthmLoadThmSet|:
%    \begin{macrocode}
\def\phfthmLoadThmSet{\phfLoadThmSet}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsection{Package option handling}
%
% The machinery is in place, now define and parse the package options.
%
% \subsubsection{Declaring the package options}
%
% The package options all use the |keyval| parsing mechanism using the
% \pkgname{xkeyval} package.
%
% Recall when using |\define@XXXkey| that the optional argument after the second
% mandatory argument is the value which is assumed if the key is given with no
% explicit value; it is not the initial default value.
%
% \paragraph{The \pkgoptionfmt{resetstyle} package option}
% An option to reset all options so that the package provides only stand-alone
% definitions and is not invasive (see \autoref{sec:global-pkg-options}).
%
% This option does not expect any argument (i.e., you should specify
% |\usepackage[resetstyle,|\meta{other options}|]{phfthm}|, and not
% |\usepackage[resetstyle=true,|\meta{other options}|]{phfthm}|).
%    \begin{macrocode}
\define@key{phfthmpkg}{resetstyle}[]{%
  \KV@phfthmpkg@smallproofsfalse%
  \KV@phfthmpkg@qedsymbolblacksquarefalse%
  \KV@phfthmpkg@prooftitleitbffalse%
  \KV@phfthmpkg@sepcountersfalse%
  \KV@phfthmpkg@proofreffalse%
  \if\relax\detokenize{#1}\relax\else%
    \PackageError{phfthm}{'resetstyle' does not take any argument.}{You
      specified the 'resetstyle' argument and provided a value to it
      ('resetstyle=...'). However the 'resetstyle' option does not accept
      any value argument.}
  \fi%
}
%    \end{macrocode}
%
%
% \paragraph{Options for loading theorem sets}
% Define the various package options for the loading of predefined theorem sets
% (\autoref{sec:theorem-sets}).
%
% The \pkgoptionfmt{sepcounters} option, off by default.
%    \begin{macrocode}
\define@boolkey{phfthmpkg}{sepcounters}[true]{}
\KV@phfthmpkg@sepcountersfalse
%    \end{macrocode}
% 
%
% The \pkgoptionfmt{proofref} option.  The proof-ref is off initially by
% default.
%    \begin{macrocode}
\newif\ifKV@phfthmpkg@proofref
\KV@phfthmpkg@proofreffalse
\def\cmdKV@phfthmpkg@proofref@style{}
%    \end{macrocode}
% Actually define the option itself.  Here we do some customized parsing of the
% value of the |proofref=...| option, to treat the cases |proofref=| (empty
% argument) and |proofref=false| separately.
%    \begin{macrocode}
\define@key{phfthmpkg}{proofref}[]{%
  \ifblank{#1}{%
%    \end{macrocode}
% If a blank argument provided, set some sensible defaults with proofref on:
%    \begin{macrocode}
    \KV@phfthmpkg@proofreftrue%
    \def\cmdKV@phfthmpkg@proofref@style{default}%
  }{%
%    \end{macrocode}
% Otherwise, check to see if the value is |false|, in which case deactivate the
% proof-ref mechanism, or else, activate it and set the given style value as
% documented in \autoref{sec:theorem-sets}.
%    \begin{macrocode}
    \ifstrequal{#1}{false}{%
      \KV@phfthmpkg@proofreffalse%
    }{%
      \KV@phfthmpkg@proofreftrue%
      \def\cmdKV@phfthmpkg@proofref@style{#1}%
    }%
  }%
}
%    \end{macrocode}
% 
% The \pkgoptionfmt{thmset} option.  We subtly construct the command
% |\define@choicekey{phfthmpkg}{thmset}[\val]{\phfthm@def@thmset@optlist}|, but
% with the last macro (option list) expanded.
%    \begin{macrocode}
\def\@tmpa{\define@choicekey{phfthmpkg}{thmset}[\val]}
\edef\@tmpb{{\phfthm@def@thmset@optlist}}
\expandafter\@tmpa\@tmpb{%
  \xdef\cmdKV@phfthmpkg@thmset{\val}%
}
%    \end{macrocode}
% By default we should load the |default| set.
%    \begin{macrocode}
\def\cmdKV@phfthmpkg@thmset{default}
%    \end{macrocode}
% 
%
% The options \pkgoptionfmt{theoremstyle} and \pkgoptionfmt{definitionstyle} set
% which theorem style to use for theorems and definitions, when loading the
% given thmset.
%    \begin{macrocode}
\define@cmdkey{phfthmpkg}{theoremstyle}{}
\def\cmdKV@phfthmpkg@theoremstyle{plain}
\define@cmdkey{phfthmpkg}{definitionstyle}{}
\def\cmdKV@phfthmpkg@definitionstyle{definition}
%    \end{macrocode}
%
%
% \paragraph{Proof environment options}
% Define the package options \pkgoptionfmt{proofenv},
% \pkgoptionfmt{smallproofs}, \pkgoptionfmt{qedsymbolblacksquare}, and
% \pkgoptionfmt{prooftitleitbf} (\autoref{sec:proof-env}).
%    \begin{macrocode}
\define@boolkey{phfthmpkg}{proofenv}[true]{}
\define@boolkey{phfthmpkg}{smallproofs}[true]{}
\define@boolkey{phfthmpkg}{qedsymbolblacksquare}[true]{}
\define@boolkey{phfthmpkg}{prooftitleitbf}[true]{}
%    \end{macrocode}
% 
% Set the initial default values for these options.
%    \begin{macrocode}
\KV@phfthmpkg@smallproofstrue
\KV@phfthmpkg@qedsymbolblacksquaretrue
\KV@phfthmpkg@proofenvtrue
\KV@phfthmpkg@prooftitleitbffalse
%    \end{macrocode}
%
% 
% \paragraph{Options for a theorem-like heading environment}
% Define the \pkgoptionfmt{thmheading} and \pkgoptionfmt{thmheadingstyle}
% package options, documented in \autoref{sec:thmheading}.
%    \begin{macrocode}
\define@boolkey{phfthmpkg}{thmheading}[true]{}
\define@cmdkey{phfthmpkg}{thmheadingstyle}{}
%    \end{macrocode}
% 
% The |thmheading| environment is provided by default; it's a stand-alone
% definition anyway.  The style defaults to the |plain| style.
%    \begin{macrocode}
\KV@phfthmpkg@thmheadingtrue
\def\cmdKV@phfthmpkg@thmheadingstyle{plain}
%    \end{macrocode}
%
%
% \subsubsection{Parsing the package options}
% The usual stuff (\pkgname{xkeyval}-flavored).
%    \begin{macrocode}
\DeclareOptionX*{%
  \PackageWarning{phfthm}{Invalid option: `\CurrentOption'}%
}
\ProcessOptionsX<phfthmpkg>
%    \end{macrocode}
% 
%
% \subsubsection{Execute package options-controlled actions}
%
% \paragraph{Loading a theorem set}
% First, we need to take into account the options which alter the way the
% theorem sets will be loaded (separate counters, proof-ref, etc.).
%
% Take care of the proof-ref stuff.  First, define the possible styles (note
% that these are not the same as the values to the |proofrefstyle| argument to
% the |\phfMakeTheorem| command).
%    \begin{macrocode}
\def\phfthm@val@mkthmopt@proofrefstyle{}
\ifKV@phfthmpkg@proofref
  \def\phfthm@proofref@style@default{}
  \def\phfthm@proofref@style@{}
%    \end{macrocode}
% Note that |proofref=always| and |proofref=onlyifveryfar| have a global effect,
% because they set |\phfProofrefPageBackTolerance| and
% |\phfProofrefPageAheadTolerance| (see documentation in
% \autoref{sec:theorem-sets}).
%    \begin{macrocode}
  \def\phfthm@proofref@style@always{
    \def\phfProofrefPageBackTolerance{-1}
    \def\phfProofrefPageAheadTolerance{-1}
  }
  \def\phfthm@proofref@style@onlyifveryfar{
    \def\phfProofrefPageBackTolerance{2}
    \def\phfProofrefPageAheadTolerance{4}
  }
  \def\phfthm@proofref@style@margin{
    \def\phfthm@val@mkthmopt@proofrefstyle{proofrefstyle=margin}
  }
  \def\phfthm@proofref@style@longref{
%    \end{macrocode}
% For |longref|: by setting |\proofonname| globally, this option can be combined
% with other styles.  But then we also change the default style formatting to
% avoid ugly line breaks.
%    \begin{macrocode}
    \def\proofonname##1##2{The proof of this \phfthm@autorefnameof{##1} can
      be found on ##2.}
    \def\phfthm@proofrefstyle@default@fmt##1##2{%
      \par{\raggedleft\proofrefsize{(\proofonname{##1}{##2})}\par}%
    }
  }
  \def\phfthm@proofref@style@off{
    \def\phfthm@val@mkthmopt@proofrefstyle{proofref=false}
  }
%    \end{macrocode}
% 
% Now execute the given styles.  Construct the command
% |\phfthm@internal@execattribs{phfthm@proofref@style@}|\hspace 
% {0pt}|{ProofRef Style}|\hspace{0pt}|{\cmdKV@phfthmpkg@proofref@style}|, but with
% the last macro expanded.
%    \begin{macrocode}
  \def\x{%
    \phfthm@internal@execattribs{phfthm@proofref@style@}{ProofRef Style}}
  \expandafter\x\expandafter{\cmdKV@phfthmpkg@proofref@style}
\fi
%    \end{macrocode}
% 
%
% Take care of counters.  In any case, define a common counter, in case we use a
% common counter for all theorem types.  (The counter is defined in any case, to
% avoid breaking other code which might use it if suddenly the user decides to
% use |sepcounters=true| for their document.)
%    \begin{macrocode}
\newcounter{phfthmcounter}
\setcounter{phfthmcounter}{0}
%    \end{macrocode}
% 
% Prepare an argument to |\phfMakeTheorem| according to the
% \pkgoptionfmt{sepcounters} option.
%    \begin{macrocode}
\ifKV@phfthmpkg@sepcounters
  \def\phfthm@val@mkthmopt@counteropts{}
\else
  \def\phfthm@val@mkthmopt@counteropts{counter=phfthmcounter}
\fi
%    \end{macrocode}
% 
%
% Finally, load the theorem set defined by the options.  The first argument
% regroups the options for theorem environments (Theorem, Proposition,
% Corollary, \ldots); the second argument regroups the options for definition
% environments (Definition); the third argument is the theorem set name itself.
%    \begin{macrocode}
\phfLoadThmSet%
{[\phfthm@val@mkthmopt@counteropts,\phfthm@val@mkthmopt@proofrefstyle,
  thmstyle=\cmdKV@phfthmpkg@theoremstyle]}%
{[\phfthm@val@mkthmopt@counteropts,proofref=false,
  thmstyle=\cmdKV@phfthmpkg@definitionstyle]}%
{\cmdKV@phfthmpkg@thmset}
%    \end{macrocode}
% (Note the absence of the proof-ref for definitions.)
%
% \paragraph{Define the proof environment}
% If requested, define the |proof| environment (\autoref{sec:proof-env}).
% First, make sure we take into account the options \pkgoptionfmt{smallproofs},
% \pkgoptionfmt{qedsymbolblacksquare} and \pkgoptionfmt{prooftitleitbf}.
%    \begin{macrocode}
\def\phfthm@pkgopterr@require@proofenv#1{%
  \ifKV@phfthmpkg@proofenv\else%
    \PackageError{phfthm}{Option `#1' depends on `proofenv=true'}%
  \fi
}
\ifKV@phfthmpkg@smallproofs
  \phfthm@pkgopterr@require@proofenv{smallproofs}
  \apptocmd\phfthm@hookproof@startcommon{%
    \def\baselinestretch{1.2}\footnotesize}{}{%
    Failed to change command \string\phfthm@hook@start@proof}
\fi
\ifKV@phfthmpkg@qedsymbolblacksquare
  \phfthm@pkgopterr@require@proofenv{qedsymbolblacksquare}
  \RequirePackage{amssymb}
  \providecommand\filledsquare{\ensuremath{\blacksquare}}
  \renewcommand\qedsymbol{\text{\tiny\ensuremath{\filledsquare}}}
\fi
\ifKV@phfthmpkg@prooftitleitbf
  \phfthm@pkgopterr@require@proofenv{prooftitleitbf}
  \def\phfthm@ProofTitleFmt#1{{\itshape\bfseries#1.}}
\fi
%    \end{macrocode}
% Go ahead and define the |proof| environment.  Because we have already loaded
% \pkgname{amsthm}, we need to override the existing |proof| environment.
%    \begin{macrocode}
\ifKV@phfthmpkg@proofenv
  \phfMakeProofEnv[override=true]{proof}
\fi
%    \end{macrocode}
% 
% \paragraph{Define the theorem-heading environment}
% Define the |thmheading| environment, if requested.
%    \begin{macrocode}
\ifKV@phfthmpkg@thmheading
  \phfMakeThmheadingEnvironment%
      [thmstyle=\cmdKV@phfthmpkg@thmheadingstyle]{thmheading}
\fi
%    \end{macrocode}
%
%\Finale
\endinput