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
|
% \iffalse
%<*driver>
\ProvidesFile{polytable.drv}
\documentclass{ltxdoc}
\usepackage{polytable}
\begin{document}
\DocInput{polytable.dtx}
\end{document}
%</driver>
%
% Copyright (C) 2003--2004 by Andres Loeh
%
% \fi
%
% \ProvidesFile{polytable.dtx}
% [2003/12/26 v0.7.2 `polytable' package (Andres Loeh)]
% \GetFileInfo{polytable.dtx}
%
% \title{The \textsf{polytable} package}
% \author{Andres L\"oh\\ \texttt{polytable@andres-loeh.de}}
% \date{\filedate}
%
% \maketitle
%
% \begin{abstract}
% This package implements a variant of tabular-like environments
% where columns can be given a name and entries can flexibly
% be placed between arbitrary columns. Complex alignment-based
% layouts, for example for program code, are possible.
% \end{abstract}
%
% \changes{v0.1}{2002/11/17}{Works.}
% \changes{v0.2}{2003/04/03}{Added pboxed. Added debug option.}
% \changes{v0.3}{2003/05/26}{Greatly enhanced pboxed.}
% \changes{v0.4}{2003/06/11}{Optional argument for ptable and parray.}
% \changes{v0.4.1}{2003/07/13}{Saving and restoring enhancements.}
% \changes{v0.4.2}{2003/07/16}{Fixed bug that caused infinite reruns.}
% \changes{v0.4.3}{2003/07/20}{Added some missing relaxes.}
% \changes{v0.5}{2003/07/22}{Documentation improved. To be put on CTAN.}
% \changes{v0.6}{2003/07/24}{Depends on lazylist instead of lambda now.}
% \changes{v0.7}{2003/11/20}{Check for undefined columns. Save counters.}
% \changes{v0.7.1}{2003/12/26}{Counters have not been restored correctly.}
%
% \section{Introduction}
%
% This package implements a variant of tabular-like environments.
% We will call these environments the |poly|-environments to
% distinguish them from the standard ones as provided by the
% \LaTeX\ kernel or the \textsf{array} package.
%
% Other than in standard tables, each column has a name. For
% instance, the commands\\
% |\column{foo}{l}|\\
% |\column{bar}{r}|\\
% -- when used within a |poly|-environment -- define a column
% with name |foo| that is left-aligned, and a column with name
% |bar| that is right-aligned.
%
% Once a couple of columns have been defined, the text is specified
% in a series of |\fromto| commands. Instead of specifying text
% per column in order, separating columns with |&|, we give the
% name of the column where the content should start, and the name
% of the column before which the content should stop.
% To typeset the text ``I'm aligned!'' in the column |foo|,
% we could thus use the command\\
% |\fromto{foo}{bar}{I'm aligned}|\\
% Several |\fromto|-commands can be used to typeset a complete
% line of the table. A new line can be started with |\nextline|.
%
% The strength of this approach is that it implicitly handles
% cases where different lines have different alignment properties.
% Not all column names have to occur in all lines.
%
% \section{A complete example}
%
% Figure~\ref{tab:example}
% is an example that is designed to show the
% capabilities of this package. In particular, it is \emph{not}
% supposed to look beautiful.
%
% \begin{figure}[htb]
% \centering
% \begin{ptabular}
% \column{left}{|l|}
% \column{right}{l|}
% \column{m13}{l|}
% \column{m23}{l|}
% \column{m33}{l|}
% \column{m12}{r|}
% \column{m22}{r|}
% \column{end}{l}
% \fromto{left}{m13}{left}
% \fromto{m13}{m23}{first of three}
% \fromto{m23}{m33}{second of three}
% \fromto{m33}{right}{third of three}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m12}{left}
% \fromto{m12}{m22}{middle 1/2}
% \fromto{m22}{right}{middle 2/2}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m13}{left}
% \fromto{m13}{m23}{middle 1/3}
% \fromto{m23}{m33}{middle 2/3}
% \fromto{m33}{right}{middle 3/3}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m12}{left}
% \fromto{m12}{m22}{first of two middle columns}
% \fromto{m22}{right}{second of two middle columns}
% \fromto{right}{end}{right}
% \end{ptabular}
% \caption{Example table}%
% \label{tab:example}
% \end{figure}
%
% The example table consists of four lines. All lines have
% some text on the left and on the right, but the middle part
% follows two different patterns: the first and the third line
% have three middle columnss that should be aligned, the second
% and the fourth line have two (right-aligned) middle columns
% that should be aligned, but otherwise independent of the three
% middle columns in the other lines.
%
% Vertical bars are used to clarify where one column ends and
% the next column starts in a particular line. Note that the first
% and the third line are completely aligned. Likewise, the
% second and the fourth line are. However, the fact that the
% bar after the text ``middle 1/2'' ends up between the two
% bars delimiting the column with ``second of three'' in it
% is just determined by the length of the text ``first of two
% middle columns'' in the last line. This text fragment
% is wider than the first of the three middle columns, but not
% wider than the first two of the three middle columns.
%
% Let's have a look at the input for the example table:
% \begin{verbatim}
% \begin{ptabular}
% \column{left}{|l|}
% \column{right}{l|}
% \column{m13}{l|}
% \column{m23}{l|}
% \column{m33}{l|}
% \column{m12}{r|}
% \column{m22}{r|}
% \column{end}{l}
% \fromto{left}{m13}{left}
% \fromto{m13}{m23}{first of three}
% \fromto{m23}{m33}{second of three}
% \fromto{m33}{right}{third of three}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m12}{left}
% \fromto{m12}{m22}{middle 1/2}
% \fromto{m22}{right}{middle 2/2}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m13}{left}
% \fromto{m13}{m23}{middle 1/3}
% \fromto{m23}{m33}{middle 2/3}
% \fromto{m33}{right}{middle 3/3}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m12}{left}
% \fromto{m12}{m22}{first of two middle columns}
% \fromto{m22}{right}{second of two middle columns}
% \fromto{right}{end}{right}
% \end{ptabular}
% \end{verbatim}
% First, columns are declared, including the vertical lines.
% Note that there is a final column |end| being declared
% that is only used as the end column in the |\fromto| statements.
% A future version of this package
% will probably get rid of the need to define
% such a column.
% After the column definitions, the lines are typeset by a
% series of |\fromto| commands, separated by |\nextline|.
% Note that the first and third column do not use |m12|, |m22|.
% Similarly, the second and fourth column do not use
% |m13|, |m23|, and |m33|.
%
% So far, one could achieve the same with an ordinary |table|
% environment. The table would have 6 columns. One left and right,
% the other four for the middle: the first and third line would
% use the first of the four columns, then place the second entry
% in a |\multicolumn| of length 2, and then use the fourth column
% for the third entry. Likewise, the other lines would place both
% their entries in a |\multicolumn| of length 2. In fact, this
% procedure is very similar to the way the |ptabular| environment
% is implemented.
%
% The problem is, though, that we need the information that
% the first of the two middle columns ends somewhere in the middle
% of the second of the three columns, as observed above. If we
% slightly modify the texts to be displayed in the middle columns,
% this situation changes. Figure~\ref{tab:variation} shows
% two variants of the example table. The input is the same,
% only that the texts contained in some columns have slightly
% changed. As you can see, the separator between the first and
% second middle column in the second and fourth lines of the
% tables now once ends up within the first, once within the
% third of the three middle columns of the other lines.
%
% \begin{figure}[htb]
% \centering
% \begin{ptabular}
% \column{left}{|l|}
% \column{right}{l|}
% \column{m13}{l|}
% \column{m23}{l|}
% \column{m33}{l|}
% \column{m12}{r|}
% \column{m22}{r|}
% \column{end}{l}
% \fromto{left}{m13}{left}
% \fromto{m13}{m23}{first of three}
% \fromto{m23}{m33}{second of three}
% \fromto{m33}{right}{third of three}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m12}{left}
% \fromto{m12}{m22}{middle 1/2}
% \fromto{m22}{right}{middle 2/2}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m13}{left}
% \fromto{m13}{m23}{middle 1/3}
% \fromto{m23}{m33}{middle 2/3}
% \fromto{m33}{right}{middle 3/3}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m12}{left}
% \fromto{m12}{m22}{first of two}
% \fromto{m22}{right}{second of two}
% \fromto{right}{end}{right}
% \end{ptabular}
% \par\bigskip
% \begin{ptabular}
% \column{left}{|l|}
% \column{right}{l|}
% \column{m13}{l|}
% \column{m23}{l|}
% \column{m33}{l|}
% \column{m12}{r|}
% \column{m22}{r|}
% \column{end}{l}
% \fromto{left}{m13}{left}
% \fromto{m13}{m23}{first of three}
% \fromto{m23}{m33}{second of three}
% \fromto{m33}{right}{third of three}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m12}{left}
% \fromto{m12}{m22}{middle 1/2}
% \fromto{m22}{right}{middle 2/2}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m13}{left}
% \fromto{m13}{m23}{middle 1/3}
% \fromto{m23}{m33}{middle 2/3}
% \fromto{m33}{right}{middle 3/3}
% \fromto{right}{end}{right}
% \nextline
% \fromto{left}{m12}{left}
% \fromto{m12}{m22}{the first of two middle columns}
% \fromto{m22}{right}{2/2}
% \fromto{right}{end}{right}
% \end{ptabular}
% \caption{Variants of the example table}%
% \label{tab:variation}
% \end{figure}
%
% If one wants the general case using the |\multicolumn|
% approach, one thus has to measure the widths of the entries
% of the columns to compute their relative position. In essence,
% this is what the package does for you.
%
% \section{Haskell code example}
%
% I have written this package mainly for one purpose: to be able
% to beautifully align Haskell source code. Haskell is a functional
% programming language where definitions are often grouped into
% several declarations. I've seen programmers exhibit symmetric
% structures in different lines by adding spaces in their source
% code files in such a way that corresponding parts in different
% definitions line up. On the other hand, as Haskell allows
% user-defined infix operators, some programmers like their symbols
% to be typeset as \LaTeX\ symbols, not as typewriter code.
% But using \LaTeX\ symbols and a beautiful proportional font
% usually destroys the carefully crafted layout and alignment.
%
% With lhs2\TeX, there is now a preprocessor available that preserves
% the source code's internal alignment by mapping the output onto
% \textsf{polytable}'s environments.
% Figure~\ref{tab:haskell} is an example of how
% the output of lhs2\TeX\ might look like.
%
% \newcommand{\HsVar}[1]{\mathop{\mathit{\strut #1}}}
% \newcommand{\HsCon}[1]{\mathop{\mathsf{\strut #1}}}
% \newcommand{\HsKey}[1]{\mathop{\mathbf{\strut #1}}}
% \newcommand{\map}{\HsVar{map}}
% \newcommand{\round}{\HsVar{round}}
% \newcommand{\TA}{\HsVar{a}}
% \newcommand{\TB}{\HsVar{b}}
% \newcommand{\TF}{\HsVar{f}}
% \newcommand{\TX}{\HsVar{x}}
% \newcommand{\TY}{\HsVar{y}}
% \newcommand{\TN}{\HsVar{n}}
% \newcommand{\TM}{\HsVar{m}}
% \newcommand{\TR}{\HsVar{r}}
% \newcommand{\TXS}{\HsVar{xs}}
% \newcommand{\RealFrac}{\mathop{\mathsf{RealFrac}}}
% \newcommand{\Integral}{\mathop{\mathsf{Integral}}}
% \newcommand{\LET}{\mathop{\mathbf{let}}}
% \newcommand{\IN}{\mathop{\mathbf{in}}}
% \newcommand{\IF}{\mathop{\mathbf{if}}}
% \newcommand{\THEN}{\mathop{\mathbf{then}}}
% \newcommand{\ELSE}{\mathop{\mathbf{else}}}
% \newcommand{\OF}{\mathop{\mathbf{of}}}
% \newcommand{\CASE}{\mathop{\mathbf{case}}}
% \newcommand{\CLASS}{\mathop{\mathbf{class}}}
% \newcommand{\WHERE}{\mathop{\mathbf{where}}}
% \newcommand{\Eq}{\mathop{\mathsf{Eq}}}
% \newcommand{\Ord}{\mathop{\mathsf{Ord}}}
%
% \setlength{\arraycolsep}{.5ex}
%
% \begin{figure}
% \[
% \begin{parray}
% \column{left}{l}
% \column{body}{l}
% \column{end}{l}
% \column{rel}{c}
% \column{rhs}{l}
% \fromto{left}{end}{\CLASS\ (\Eq\TA)\Rightarrow\Ord\TA\WHERE}
% \nextline
% \fromto{left}{body}{\qquad\ }
% \fromto{body}{rel}{\HsVar{compare}}
% \fromto{rel}{rhs}{::}
% \fromto{rhs}{end}{\TA\rightarrow\TA\rightarrow\HsCon{Ordering}}
% \nextline
% \fromto{left}{body}{\qquad\ }
% \fromto{body}{rel}{(<),(\leq),(\geq),(>)}
% \fromto{rel}{rhs}{::}
% \fromto{rhs}{end}{\TA\rightarrow\TA\rightarrow\HsCon{Bool}}
% \nextline
% \fromto{left}{body}{\qquad\ }
% \fromto{body}{rel}{\HsVar{max},\HsVar{min}}
% \fromto{rel}{rhs}{::}
% \fromto{rhs}{end}{\TA\rightarrow\TA\rightarrow\HsCon{Bool}}
% \nextline
% \nextline
% \fromto{body}{end}{\mbox{--- Minimal complete definition:
% $(\leq)$ or $\HsVar{compare}$}}
% \nextline
% \fromto{body}{end}{\mbox{--- using $\HsVar{compare}$ can be more efficient
% for complex types}}
% \nextline
% \column{cgrd}{l}
% \fromto{body}{cgrd}{\HsVar{compare}\TX\TY}
% \fromto{cgrd}{rel}{\vert\ \TX\equiv\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsCon{EQ}}
% \nextline
% \fromto{cgrd}{rel}{\vert\ \TX\leq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsCon{LT}}
% \nextline
% \fromto{cgrd}{rel}{\vert\ \HsVar{otherwise}}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsCon{GT}}
% \nextline
% \fromto{body}{rel}{\TX\leq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsVar{compare}\TX\TY\not\equiv\HsCon{GT}}
% \nextline
% \fromto{body}{rel}{\TX < \TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsVar{compare}\TX\TY\equiv\HsCon{LT}}
% \nextline
% \fromto{body}{rel}{\TX\geq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsVar{compare}\TX\TY\not\equiv\HsCon{LT}}
% \nextline
% \fromto{body}{rel}{\TX > \TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsVar{compare}\TX\TY\equiv\HsCon{GT}}
% \nextline
% \fromto{body}{cgrd}{\HsVar{max}\TX\TY}
% \fromto{cgrd}{rel}{\vert\ \TX\leq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\TY}
% \nextline
% \fromto{cgrd}{rel}{\vert\ \HsVar{otherwise}}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\TX}
% \nextline
% \fromto{body}{cgrd}{\HsVar{min}\TX\TY}
% \fromto{cgrd}{rel}{\vert\ \TX\leq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\TX}
% \nextline
% \fromto{cgrd}{rel}{\vert\ \HsVar{otherwise}}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\TY}
% \end{parray}
% \]
% \caption{Haskell code example}
% \label{tab:haskell}
% \end{figure}
%
% Of course, this could be useful for other programming languages
% as well. In fact, lhs2\TeX\ can be tweaked to process several
% experimental languages that are based on Haskell, but I can imagine
% that this package could generally prove useful to typeset program
% code.
%
% \section{Other applications}
%
% Although I have written this package for a specific purpose, I
% am very much interested to hear of other potential application
% areas. Please tell me if you found a use for this package and
% do not hesitate to ask for additional features that could convince
% you to use the package for something.
%
% \section{The \textsf{lazylist} package}
%
% Internally, this package makes use of Alan Jeffrey's excellent
% \textsf{lazylist} package, which provides an implementation of
% the lambda calculus using fully expandable control sequences.
% Unfortunately, |lazylist.sty| is not included in most common
% \TeX\ distributions, so you might need to fetch it from CTAN
% separately.
%
% \section{Reference}
%
% \subsection{The environments}
%
% \DescribeEnv{ptabular}
% \DescribeEnv{parray}
% \DescribeEnv{pboxed}
% There are currently three environments that this package provides:
% |ptabular| and |parray| are based on (and translated into) the usual
% |tabular| and |array| environments as provided by the |array|
% package. In particular, |parray| assumes math mode, whereas
% |ptabular| assumes text mode. The third environment, |pboxed|,
% typesets the material in boxes of the calculated length, but in
% normal paragraph mode. The advantage is that
% there can be page breaks within the table.
% Note that you should start a new, nonindented
% paragraph before beginning a |pboxed|. All lines in a |pboxed|
% should be of equal length, so it might be possible to center
% or right-align the material, although this has not been
% extensively tested.
%
% One more environment is planned: |plongtable|, a poly-version
% of the |longtable| environment.
%
% The interface is the same for all of the environments.
%
% \subsection{The commands}
%
% In each of the environments, the following commands can be
% used (and \emph{only} these commands should be used):
%
% \DescribeMacro{\column}
% With |\column{|\meta{columnid}|}{|\meta{spec}|}|, a new column
% \meta{columnid} is specified. The name of the column can be
% any sequence of alphanumerical characters. The \meta{spec} is
% a format string for that particular column, and it can contain
% the same constructs that can be used in format strings of normal
% tables or arrays (this also holds for the |pboxed| environment).
% However, it should only contain the description for \emph{one}
% column. (I've never tested what happens if you do something else,
% but you have been warned \dots)
%
% If the save/restore feature (explained below) is not used, |\column|
% definitions are always local to one table.
% One can define a column multiple times within one table.
% A warning will be produced,
% and the second format string will be used for the complete table.
%
% \DescribeMacro{\fromto}
% The call |\fromto{|\meta{fromid}|}{|\meta{toid}|}{|\meta{text}|}|
% will typeset \meta{text} in the current line, starting at column
% \meta{fromid} and ending before column \meta{toid}, using the
% format string specified for \meta{fromid}.
%
% A line of a table usually consists of multiple |\fromto| statements.
% Each statement's starting column should be either the same as the
% end column of the previous statement, or it will be assumed that
% the start column is located somewhere to the right of the previous
% end column. The user is responsible to not introduce cycles in the
% (partial) order of columns. If such a cycle is specified, the
% current algorithm will loop, causing a |dimension too large| error
% ultimately. TODO: catch this error.
%
% \DescribeMacro{\nextline}
% The command |\nextline| ends one line and begins the next.
% There is no need to end the last line. One can pass an optional
% argument, as in |\nextline[|\meta{dimen}|]|, that will add
% \meta{dimen} extra space between the lines. TODO: make this
% command available as |\\|.
%
% \subsection{A warning}
%
% The contents of the table are processed multiple times because
% the widths of the entries are measured. Global assigments that
% modify registers and similar things can thus result in unexpected
% behaviour.
% New in v0.7: \LaTeX\ counters (i.e. counters defined by
% |\newcounter|) are protected now. They will be reset after
% each of the trial runs.
%
% \subsection{Saving column width information}
%
% WARNING: this feature does \emph{only} work correctly with
% the |pboxed| environment right now. TODO: make this work
% with the other environments (this essentially amounts to
% implementing a |tabbing|-like |\kill| statement for |tabular|
% and |array|; does that already exist somewhere?).
%
% Sometimes, one might want to reuse not only the same column, but
% exactly the same alignment as in a previous table. An example
% would be a fragment of program code, which has been broken into
% several pieces, with documentation paragraphs added in between.
%
% \DescribeMacro{\savecolumns}
% \DescribeMacro{\restorecolumns}
% With |\savecolumns[|\meta{setid}|]|, one can save the information
% of the current table for later reuse. The name |setid| can be an
% arbitrary sequence of alphanumeric characters. It does \emph{not} share
% the same namespace as the column names. The argument is optional;
% if it is omitted, a default name is assumed. Later, one can restore
% the information (multiple times, if needed) in other tables, by
% issuing a |\restorecolumns[|\meta{setid}|]|.
%
% This feature requires to pass information backwards in the general
% case, as column widths in later environments using one specific
% column set might influence the layout of earlier environments.
% Therefore, information is written into the |.aux| file, and
% sometimes, a warning is given that a rerun is needed. Multiple
% reruns might be required to get all the widths right.
%
% I have tried very hard to avoid producing rerun warnings infinitely
% except if there are really cyclic dependencies between columns.
% Still, if it happens or something seems to be broken, it often
% is a good idea to remove the |.aux| file and start over. Be sure
% to report it as a bug, though.
%
% Figure~\ref{tab:saverestore} is an example of the Haskell code
% example with several comments inserted. The source of this file
% shows how to typeset the example.
% \begin{figure}
% \setlength{\parindent}{0pt}%
% \setlength{\parskip}{1ex}%
% \hrule
% We introduce a new type class $\Ord$ for objects that admit
% an ordering. It is based on the $\Eq$ class:
%
% \(
% \begin{pboxed}
% \savecolumns
% \column{left}{>{\quad}l}
% \column{end}{l}
% \fromto{left}{end}{\CLASS\ (\Eq\TA)\Rightarrow\Ord\TA\WHERE}
% \end{pboxed}
% \)
%
% The next three lines give the type signatures for all
% the methods of the class.
%
% \(
% \begin{pboxed}
% \restorecolumns
% \column{body}{l}
% \column{rel}{c}
% \column{rhs}{l}
% \fromto{left}{body}{\qquad\ }
% \fromto{body}{rel}{\HsVar{compare}}
% \fromto{rel}{rhs}{::}
% \fromto{rhs}{end}{\TA\rightarrow\TA\rightarrow\HsCon{Ordering}}
% \nextline
% \fromto{left}{body}{\qquad\ }
% \fromto{body}{rel}{(<),(\leq),(\geq),(>)}
% \fromto{rel}{rhs}{::}
% \fromto{rhs}{end}{\TA\rightarrow\TA\rightarrow\HsCon{Bool}}
% \nextline
% \fromto{left}{body}{\qquad\ }
% \fromto{body}{rel}{\HsVar{max},\HsVar{min}}
% \fromto{rel}{rhs}{::}
% \fromto{rhs}{end}{\TA\rightarrow\TA\rightarrow\HsCon{Bool}}
% \nextline[1ex]
% \fromto{body}{end}{\mbox{--- Minimal complete definition:
% $(\leq)$ or $\HsVar{compare}$}}
% \nextline
% \fromto{body}{end}{\mbox{--- using $\HsVar{compare}$ can be more efficient
% for complex types}}
% \end{pboxed}
% \)
%
% As the comment above says, it is sufficient to define either
% $(\leq)$ or $\HsVar{compare}$ to get a complete instance. All
% of the class methods have default definitions. First, we can
% define $\HsVar{compare}$ in terms of $(\leq)$. The result type
% of $\HsVar{compare}$ is an $\HsCon{Ordering}$, a type consisting
% of only three values: $\HsCon{EQ}$ for ``equality'',
% $\HsCon{LT}$ for ``less than'', and $\HsCon{GT}$ for ``greater
% than''.
%
% \(
% \begin{pboxed}
% \restorecolumns
% \column{cgrd}{l}
% \fromto{body}{cgrd}{\HsVar{compare}\TX\TY}
% \fromto{cgrd}{rel}{\vert\ \TX\equiv\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsCon{EQ}}
% \nextline
% \fromto{cgrd}{rel}{\vert\ \TX\leq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsCon{LT}}
% \nextline
% \fromto{cgrd}{rel}{\vert\ \HsVar{otherwise}}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsCon{GT}}
% \end{pboxed}
% \)
%
% All the other comparison operators can be defined
% in terms of $\HsCon{compare}$:
%
% \(
% \begin{pboxed}
% \restorecolumns
% \fromto{body}{rel}{\TX\leq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsVar{compare}\TX\TY\not\equiv\HsCon{GT}}
% \nextline
% \fromto{body}{rel}{\TX < \TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsVar{compare}\TX\TY\equiv\HsCon{LT}}
% \nextline
% \fromto{body}{rel}{\TX\geq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsVar{compare}\TX\TY\not\equiv\HsCon{LT}}
% \nextline
% \fromto{body}{rel}{\TX > \TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\HsVar{compare}\TX\TY\equiv\HsCon{GT}}
% \end{pboxed}
% \)
%
% Finally, there are default definitions for
% $\HsVar{max}$ and $\HsVar{min}$ in terms of $(\leq)$.
%
% \(
% \begin{pboxed}
% \restorecolumns
% \fromto{body}{cgrd}{\HsVar{max}\TX\TY}
% \fromto{cgrd}{rel}{\vert\ \TX\leq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\TY}
% \nextline
% \fromto{cgrd}{rel}{\vert\ \HsVar{otherwise}}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\TX}
% \nextline
% \fromto{body}{cgrd}{\HsVar{min}\TX\TY}
% \fromto{cgrd}{rel}{\vert\ \TX\leq\TY}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\TX}
% \nextline
% \fromto{cgrd}{rel}{\vert\ \HsVar{otherwise}}
% \fromto{rel}{rhs}{=}
% \fromto{rhs}{end}{\TY}
% \end{pboxed}
% \)
% \hrule
% \caption{Commented Haskell code example}
% \label{tab:saverestore}
% \end{figure}
%
% \section{The Code}
% \begin{macrocode}
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{polytable}%
[2004/02/27 v0.7.2 `polytable' package (Andres Loeh)]
% \end{macrocode}
% New in v0.7.2:
% The \textsf{amsmath} package clashes with \textsf{lazylist}:
% both define the command |\And|. Although it would certainly
% be better to find another name in \textsf{lazylist}, we
% take precautions for now. (Note that this will still fail
% if \textsf{lazylist} is already loaded -- but then it's not
% our problem \dots
% \begin{macrocode}
\let\PT@original@And\And
\RequirePackage{lazylist}
\let\PT@And\And
\def\PT@prelazylist
{\let\And\PT@And}
\def\PT@postlazylist
{\let\And\PT@original@And}
\PT@postlazylist
\RequirePackage{array}
% \end{macrocode}
%
% The option debug will cause (a considerable amount of)
% debugging output to be printed. The option silent, on
% the other hand, will prevent certain warnings from being
% printed.
% \begin{macrocode}
\DeclareOption{debug}{\AtEndOfPackage\PT@debug}
\DeclareOption{silent}{\AtEndOfPackage\PT@silent}
\ProcessOptions
% \end{macrocode}
%
% First, we declare a couple of registers that we will need later.
% \begin{macrocode}
\newdimen\PT@colwidth
%\newdimen\PT@delta
\newcount\PT@cols
\newcount\PT@table
\newif\ifPT@changed
% \end{macrocode}
% In |\PT@allcols|, we will store the list of all columns, as a
% list as provided by the \textsf{lazylist} package. We initialise
% it to the empty list, which is represented by |\Nil|. In v0.8,
% we will have a second list that only contains the public columns.
% \begin{macrocode}
\def\PT@allcols{\Nil}
%\def\PT@allpubliccols{\Nil}
\let\PT@infromto\empty
% \end{macrocode}
% These are flags and truth values. TODO: Reduce and simplify.
% \begin{macrocode}
\let\PT@currentwidths\empty
\def\PT@false{0}
\def\PT@true{1}
\let\PT@inrestore\PT@false
% \end{macrocode}
% The dimension |\PT@delta| is currently not used. The dimension
% comparisons should probably have a small tolerance, to prevent
% infinite loops due to rounding errors. (Can this really happen?)
% \begin{macrocode}
%\PT@delta\hfuzz
% \end{macrocode}
%
% \begin{macro}{\PT@debug}
% \begin{macro}{\PT@typeout@}
% \begin{macro}{\PT@silent}
% \begin{macro}{\PT@warning}
% Similar to the \textsf{tabularx} package, we add macros to
% print debugging information to the log. Depending on package
% options, we can set or unset them.
% \begin{macrocode}
\def\PT@debug
{\def\PT@typeout@ ##1{\typeout{(polytable) ##1}}}
\let\PT@typeout@\@gobble
\def\PT@warning{\PackageWarning{polytable}}%
\def\PT@silent
{\let\PT@typeout@\@gobble\let\PT@warning\@gobble}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\PT@rerun}
% This macro can be called at a position where we know that
% we have to rerun LaTeX to get the column widths right. It
% issues a warning at the end of the document.
% \begin{macrocode}
\def\PT@rerun
{\PT@typeout@{We have to rerun LaTeX ...}%
\AtEndDocument
{\PackageWarning{polytable}%
{Column widths have changed. Rerun LaTeX.\@gobbletwo}}%
\global\let\PT@rerun\relax}
% \end{macrocode}
% \end{macro}
%
% \subsection{Macro definition tools}
%
% \begin{macro}{\PT@listopmacro}
% \begin{macro}{\PT@consmacro}
% \begin{macro}{\PT@appendmacro}
% This assumes that |#2| is a list macro and |#3| is a new list element.
% The macro |#2| should, after the call, expand to the list with the
% new element |#1|ed. Because we don't know the number of tokens in |#3|,
% we use a temporary macro |\PT@temp| (which is used frequently throughout
% the package).
% \begin{macrocode}
\def\PT@listopmacro #1#2#3% #1 #3 to the list #2
{\def\PT@temp{#1{#3}}%
\expandafter\expandafter\expandafter
\def\expandafter\expandafter\expandafter
#2\expandafter\expandafter\expandafter
{\expandafter\PT@temp\expandafter{#2}}}
\def\PT@consmacro{\PT@listopmacro\Cons}
\def\PT@appendmacro{\PT@listopmacro\Cat}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% The follwing two macros can be used to add something to
% the beginning or the end of a constrol structure.
% \begin{macrocode}
\def\PT@addbeginmacro #1#2% add #2 to the beginning of #1
{\def\PT@temp{#2}%
\expandafter\expandafter\expandafter
\def\expandafter\expandafter\expandafter
#1\expandafter\expandafter\expandafter
{\expandafter\PT@temp #1}}
\def\PT@gaddendmacro #1#2% add #2 to the end of #1
{\expandafter\gdef\expandafter #1\expandafter{#1#2}}
% \end{macrocode}
% \begin{macro}{\PT@enamedef}
% This is much like |\@namedef|, but it expands |#2| once.
% \begin{macrocode}
\def\PT@enamedef #1#2% sets name #1 to the expansion of #2
{\expandafter\Twiddle\expandafter\@namedef\expandafter{#2}{#1}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@adddeftomacroas}
% Given the name of a control structure |#1| and a name of another
% control structure |#2| and an expression |#3|, we add the
% definition of |#2| to the expansion of |#3| to the macro |#1|.
% \begin{macrocode}
\def\PT@adddeftomacroas#1#2#3%
{\expandafter\expandafter\expandafter
\def\expandafter\expandafter\expandafter\PT@temp
\expandafter\expandafter\expandafter
{\expandafter\expandafter\expandafter\def
\expandafter\expandafter\csname #2\endcsname
\expandafter{#3}}%
\expandafter\expandafter\expandafter\PT@gaddendmacro
\expandafter\expandafter\expandafter
{\expandafter\expandafter\csname #1\endcsname
\expandafter}\expandafter{\PT@temp}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@adddeftomacro}
% This is a special case of |\PT@adddeftomacroas| where |#3| is
% the expansion of |#2|.
% \begin{macrocode}
\def\PT@adddeftomacro#1#2%
{\def\PT@temp{\PT@adddeftomacroas{#1}{#2}}%
\expandafter\PT@temp\csname #2\endcsname}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@addoptargtomacro}
% \begin{macrocode}
\def\PT@addoptargtomacro
{\PT@add@argtomacro\PT@makeoptarg}
\def\PT@addargtomacro
{\PT@add@argtomacro\PT@makearg}
\def\PT@add@argtomacro#1#2#3%
{\expandafter\expandafter\expandafter\gdef
\expandafter\expandafter\expandafter\PT@temp
\expandafter\expandafter\expandafter{\csname #3\endcsname}%
#1%
\expandafter\PT@gaddendmacro\expandafter
{\expandafter#2\expandafter}\expandafter{\PT@temp}}
\def\PT@makeoptarg%
{\expandafter\def\expandafter\PT@temp\expandafter
{\expandafter[\PT@temp]}}
\def\PT@makearg%
{\expandafter\def\expandafter\PT@temp\expandafter
{\expandafter{\PT@temp}}}
%
% \begin{macro}{\PT@mtimesn}
% Expands to |#1| times |#2|. (Work in progress.)
% \begin{macrocode}
% \def\PT@mtimesn #1#2%
% {\expandafter\PT@mtimtesn@\romannumeral #1011{#2}}
% \def\PT@mtimesn@ #1i#2%
% {\if#1m%
% #2\expandafter\PT@mtimesn@#1i#3}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@gobbleoptional}
% Gobbles one optional argument. Ignores spaces.
% \begin{macrocode}
\newcommand*{\PT@gobbleoptional}[1][]{\ignorespaces}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@origomit}
% Save the original definition of omit.
% \begin{macrocode}
\let\PT@origomit\omit
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@disableomitonce}
% Undefines the next use of omit.
% \begin{macrocode}
\def\PT@disableomitonce
{\def\omit
{\let\omit\PT@origomit}}
% \end{macrocode}
% \end{macro}
%
% \subsection{The environment}
%
% The general idea is to first scan the contents of the environment
% and store them in a token register. In a few test runs, the positions
% of the column borders are determined. After that, the columns are sorted
% and the table is typeset, translating the named ranges into appropriate
% calls to |\multicolumn|.
%
% \begin{macro}{\beginpolytable}
% This macro starts the environment. It should, however, not
% be called directly, but rather in a \LaTeX\ environment.
% We just initialize the
% token register to the empty string and then start scanning.
% \begin{macrocode}
\newcommand{\beginpolytable}%
% \end{macrocode}
% We save the current enclosing \LaTeX\ environment in
% |\PT@environment|. This will be the |\end| we will be
% looking for, and this will be the environment we manually
% close in the end.
% \begin{macrocode}
{\edef\PT@environment{\@currenvir}%
\begingroup
% new in v0.7: save counters
\PT@savecounters
\toks@{}% initialise token register
\PT@scantoend}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\endpolytable}
% This is just defined for convenience.
% \begin{macrocode}
\let\endpolytable=\relax
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@scantoend}
% We scan until the next occurence of |\endpolytable| and
% store the tokens. Then we continue with determining the
% column widths.
% \begin{macrocode}
\long\def\PT@scantoend #1\end #2%
{\toks@\expandafter{\the\toks@ #1}%
\def\PT@temp{#2}%
\ifx\PT@temp\PT@environment
\expandafter\PT@getwidths
\else
\toks@\expandafter{\the\toks@\end{#2}}%
\expandafter\PT@scantoend
\fi}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@getwidths}
% Here, we make as many test runs as are necessary to
% determine the correct column widths.
% \begin{macrocode}
\def\PT@getwidths
% \end{macrocode}
% We let the |\column| command initialize a column
% in the first run.
% \begin{macrocode}
{\let\column\PT@firstrun@column
% \end{macrocode}
% There is the possibility to save or restore columns.
% This is new in v0.4.
% \begin{macrocode}
\let\savecolumns\PT@savewidths
\let\restorecolumns\PT@restorewidths
% \end{macrocode}
% We \emph{always} define a pseudo-column |@begin@|.
% This denotes the begin of a row.
% \begin{macrocode}
\column{@begin@}{@{}l@{}}
\PT@cols=0\relax%
% \end{macrocode}
% The two other commands that are allowed inside of the
% environment, namely |\fromto| and |\nextline| are
% initialized. The |\fromto| command may increase the
% current widths of some columns, if necessary, whereas
% |\nextline| just resets the counter that keeps track
% of the ``current'' column, to |0|.
% \begin{macrocode}
\let\fromto\PT@fromto
\let\nextline\PT@resetcolumn
\PT@changedfalse % nothing has changed so far
\PT@resetcolumn % we are at the beginning of a line
% \end{macrocode}
% Now we are ready for a test run.
% \begin{macrocode}
\the\toks@
% \end{macrocode}
% After the first run, we print extra information.
% We use the contents of the macro |\column| to check
% whether we are in the first run, because it will be
% reset below for all other runs to do nothing.
% \begin{macrocode}
\ifx\column\PT@otherrun@column
\else
% we are in first run, print extra info
\PT@typeout@{Number of columns: \the\PT@cols}%
\PT@prelazylist
\PT@typeout@{Column list: \Print\PT@allcols}%
\PT@postlazylist
\fi
% \end{macrocode}
% The columns are initialised after the first run.
% Therefore, we make sure that the |\column| command
% won't do much in the other runs. Also, saving and
% restoring columns is no longer needed.
% \begin{macrocode}
\let\PT@firstrun@column\PT@otherrun@column
\let\savecolumns\PT@gobbleoptional
\let\restorecolumns\PT@gobbleoptional
\let\PT@savewidths\PT@gobbleoptional
\let\PT@restorewidths\PT@gobbleoptional
% \end{macrocode}
% New in v0.7.1: restore counters after each trial run.
% \begin{macrocode}
\PT@restorecounters
% \end{macrocode}
% If some column widths have indeed changed in the
% test run, this will be indicated by the flag
% |\ifPT@changed|. Depending on this flag, we will
% either loop and rerun, or we will continue in
% |\PT@sortcols|.
% \begin{macrocode}
\ifPT@changed
% we need to rerun if something has changed
\expandafter\PT@getwidths
\else
% we are done and can do the sorting
\PT@typeout@{Reached fixpoint.}%
\expandafter\PT@sortcols
\fi}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@savecounters}
% Save all \LaTeX\ counters so that they can be restored after a
% trial run.
% \begin{macrocode}
\def\PT@savecounters
{\begingroup
\def\@elt ##1%
{\global\csname c@##1\endcsname\the\csname c@##1\endcsname}%
\xdef\PT@restorecounters{\cl@@ckpt}%
\endgroup}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@sortcols}
% The column borders are sorted by their horizontal
% position on the page (width). The they get numbered
% consecutively. After that, we are well prepared to
% typeset the table.
% \begin{macrocode}
\def\PT@sortcols
% \end{macrocode}
% First, we sort the list. To make sure that the
% computation is only executed once, we save the
% sorted list by means of an |\edef|. Sorting happens
% with \textsf{lazylist}'s |\Insertsort| which expects
% an order and a list. As order, we provide |\PT@ltwidth|,
% which compares the widths of the columns.
% To prevent expansion of the list structure, given by
% |\Cons| and |\Nil|, we fold the list with the
% |\noexpand|ed versions of the list constructors.
% \begin{macrocode}
{\PT@prelazylist
\edef\PT@sortedlist
{\Foldr{\noexpand\Cons}{\noexpand\Nil}%
{\Insertsort\PT@ltmax\PT@allcols}}%
\PT@typeout@{Sorted columns: \Print\PT@sortedlist}%
\PT@postlazylist
% \end{macrocode}
% Now, each column is assigned a number, starting from
% zero.
% \begin{macrocode}
\PT@cols=0\relax%
\PT@prelazylist
\Execute{\Map\PT@numbercol\PT@sortedlist}%
\PT@postlazylist
\PT@typeout@{Numbered successfully, last column is \StripColumn\PT@lastcol}%
% \end{macrocode}
% Now is a good time to save table information, if needed later.
% We will also compare our computed information with the restored
% maximum widths.
% \begin{macrocode}
\ifx\PT@currentwidths\empty
\else
\PT@typeout@{Saving table information for \PT@currentwidths .}%
\expandafter\PT@saveinformation\expandafter{\PT@currentwidths}%
\fi
% \end{macrocode}
% Finally, we can typeset the table.
% \begin{macrocode}
\PT@typeset}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@typeset}
% \begin{macrocode}
\def\PT@typeset
% \end{macrocode}
% As a first step, we generate the table's preamble and
% print it for debugging purposes.
% \begin{macrocode}
{\PT@typeout@{Typesetting the table ...}%
\PT@prelazylist
\edef\PT@temp{@{}\Execute{\Map\PT@preamble\PT@sortedlist}}%
\PT@postlazylist
%\PT@typeout@{Preamble: \PT@temp}%
% \end{macrocode}
% Now, we redefine |\fromto| and |\nextline| to their
% final meaning in the typesetting process. The
% |\fromto| statements will be replaced by appropriate
% calls to |\multicolumn|, whereas the |\nextline| will
% again reset the counter for the current column, but
% also call the table environment's newline macro.
% \begin{macrocode}
\let\fromto\PT@multicolumn
\PT@resetcolumn % we are at the beginning of a line
\let\nextline=\PT@resetandcr
% \end{macrocode}
% Now we start the tabular environment with the
% computed preamble.
% \begin{macrocode}
\expandafter\PT@begin\expandafter{\PT@temp}%
% \end{macrocode}
% Run, and this time, typeset, the contents.
% \begin{macrocode}
\the\toks@
% \end{macrocode}
% End the array, close the group, close the environment.
% We are done!
% \begin{macrocode}
\PT@end
\endgroup
\PT@typeout@{Finished.}%
\expandafter\end\expandafter{\PT@environment}}%
% \end{macrocode}
% \end{macro}
%
% \subsection{The trial runs}
%
% For each column, we store information in macros that are
% based on the column name. We store a column's type (i.e.~its
% contribution to the table's preamble), its current width
% (i.e.~its the horizontal position where the column will start
% on the page), and later, its number, which will be used for
% the |\multicolumn| calculations.
%
% \begin{macro}{\PT@firstrun@column}
% During the first trial run, we initialise all the columns. We
% store their type, as declared in the |\column| command inside
% the environment, and we set their initial width to 0pt.
% Furthermore, we add the column to the list of all available
% columns, increase the column counter, and tell \TeX\ to ignore
% spaces that might follow the |\column| command.
% New in v0.4.1: We make a case distinction on an empty type
% field to prevent warnings for columns that have been defined
% via |\PT@setmaxwidth| -- see there for additional comments.
% New in v0.4.2: We allow redefinition of width if explicitly
% specified, i.e. not equal to 0pt.
% \begin{macrocode}
\newcommand\PT@firstrun@column[3][0pt]%
{\@ifundefined{PT@col@#2.type}%
{\PT@typeout@{Defining column #2 at #1.}%
\@namedef{PT@col@#2.type}{#3}%
\@namedef{PT@col@#2.width}{#1}% initialize the width of the column
% add the new column to the (sortable) list of all columns
\PT@consmacro\PT@allcols{PT@col@#2}%
\advance\PT@cols by 1\relax}%
{\expandafter\ifx\csname PT@col@#2.type\endcsname\empty
\relax % will be defined in a later table of the same set
\else
\PT@warning{Redefining column #2}%
\fi
\@namedef{PT@col@#2.type}{#3}%
\expandafter\ifdim#1>0pt\relax
\PT@typeout@{Redefining column #2 at #1.}%
\@namedef{PT@col@#2.width}{#1}%
\fi
}%
% \end{macrocode}
% For the case that we are saving and there is not yet information
% from the |.aux| file, we define the |.max| and |.trusted| fields
% if they are undefined. If information becomes available later, it
% will overwrite these definitions.
% \begin{macrocode}
\@ifundefined{PT@col@#2.max}%
{\@namedef{PT@col@#2.max}{#1}%
\expandafter\let\csname PT@col@#2.trusted\endcsname\PT@true}{}%
\ignorespaces}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@otherrun@column}
% In all but the first trial run, we do not need any additional
% information about the columns any more, so we just gobble the
% two arguments, but still ignore spaces.
% \begin{macrocode}
\newcommand\PT@otherrun@column[3][]%
{\ignorespaces}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@checkcoldefined}
% This macro verifies that a certain column is defined and
% produces an error message if it is not.
% \begin{macrocode}
\def\PT@checkcoldefined #1%
{\@ifundefined{PT@col@#1.type}%
{\PackageError{polytable}{Undefined column #1}{}}{}}%
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@fromto}
% Most of the work during the trial runs is done here. We increase
% the widths of certain columns, if necessary. Note that there
% are two conditions that have to hold if |\fromto{A}{B}| is encountered:
% \begin{itemize}
% \item the width of |A| has to be at least the width of the current
% (i.e.~previous) column.
% \item the width of |B| has to be at least the width of |A|, plus the
% width of the entry.
% \end{itemize}
% \begin{macrocode}
\def\PT@fromto #1#2#3%
% \end{macrocode}
% We start by checking a switch.
% \begin{macrocode}
{\PT@infromto
\def\PT@infromto{%
\PackageError{polytable}{Nested fromto}{}}%
% \end{macrocode}
% Next, we check that both columns are defined.
% \begin{macrocode}
\PT@checkcoldefined{#1}%
\PT@checkcoldefined{#2}%
% \end{macrocode}
% Here, we check the first condition.
% \begin{macrocode}
\def\PT@temp{PT@col@#1}%
\ifx\PT@currentcol\PT@temp
\PT@typeout@{No need to skip columns.}%
\else
\PT@colwidth=\expandafter\@nameuse\expandafter
{\PT@currentcol.width}\relax
\ifdim\PT@colwidth>\csname PT@col@#1.width\endcsname\relax
% we need to change the width
\PT@typeout@{s #1: old=\@nameuse{PT@col@#1.width} new=\the\PT@colwidth}%
\PT@changedtrue
\PT@enamedef{PT@col@#1.width}{\the\PT@colwidth}%
\fi
% \end{macrocode}
% The same for the untrusted |.max| values.
% \begin{macrocode}
\PT@colwidth=\expandafter\@nameuse\expandafter
{\PT@currentcol.max}\relax
\ifdim\PT@colwidth>\csname PT@col@#1.max\endcsname\relax
% we need to change the width
\PT@typeout@{S #1: old=\@nameuse{PT@col@#1.max} new=\the\PT@colwidth}%
\PT@changedtrue
\PT@checkrerun
\PT@enamedef{PT@col@#1.max}{\the\PT@colwidth}%
\fi
\ifnum\csname PT@col@#1.trusted\endcsname=\PT@false\relax
\ifdim\PT@colwidth=\csname PT@col@#1.max\endcsname\relax
\PT@typeout@{#1=\the\PT@colwidth\space is now trusted}%
\expandafter\let\csname PT@col@#1.trusted\endcsname\PT@true%
\fi
\fi
\fi
% \end{macrocode}
% To test the second condition, we have to test-typeset the contents of
% the column, contained in |#3|. We prepare a ``safe environment'' for these
% contents. We determine whether we are in math mode
% or not, put the contents into an hbox in the same mode, and we are
% typesetting the contents in the same environment as we will typeset the
% table in the end.
% \begin{macrocode}
\begingroup
\ifmmode
\let\d@llarbegin=$%$
\let\d@llarend=$%$
\let\col@sep=\arraycolsep
\else
\let\d@llarbegin=\begingroup
\let\d@llarend=\endgroup
\let\col@sep=\tabcolsep
\fi
%\def\PT@currentcol{PT@col@#1}%
%\ifx\PT@currentcol\PT@nullcol
%\else
% \PT@addbeginmacro\PT@currentpreamble{@{}}%
%\fi
\expandafter\expandafter\expandafter
\def\expandafter\expandafter\expandafter\PT@currentpreamble
\expandafter\expandafter\expandafter
{\csname PT@col@#1.type\endcsname}%
\setbox0=\hbox{%
\expandafter\@mkpream\expandafter{\PT@currentpreamble}%
\def\@sharp{\strut #3}%
%\show\@preamble
\@preamble}%
\expandafter\gdef\expandafter\PT@temp\expandafter{\the\wd0}%
\endgroup
% \end{macrocode}
% Now begins the real comparison.
% \begin{macrocode}
\global\PT@colwidth=\@nameuse{PT@col@#1.width}%
\global\advance\PT@colwidth by \PT@temp\relax%
\ifdim\PT@colwidth>\csname PT@col@#2.width\endcsname\relax
% we need to change the width
\PT@typeout@{c #2:
old=\@nameuse{PT@col@#2.width}
new=\the\PT@colwidth}%
\PT@changedtrue
\PT@enamedef{PT@col@#2.width}{\the\PT@colwidth}%
\fi
% \end{macrocode}
% And again, we have to do the same for the untrusted maximums.
% \begin{macrocode}
\global\PT@colwidth=\@nameuse{PT@col@#1.max}%
\global\advance\PT@colwidth by \PT@temp\relax%
\ifdim\PT@colwidth>\csname PT@col@#2.max\endcsname\relax
% we need to change the width
\PT@typeout@{C #2:
old=\@nameuse{PT@col@#2.max}
new=\the\PT@colwidth}%
\PT@changedtrue
\PT@checkrerun
\PT@enamedef{PT@col@#2.max}{\the\PT@colwidth}%
\fi
\ifnum\csname PT@col@#2.trusted\endcsname=\PT@false\relax
\ifdim\PT@colwidth=\csname PT@col@#2.max\endcsname\relax
\PT@typeout@{#2=\the\PT@colwidth\space is now trusted}%
\expandafter\let\csname PT@col@#2.trusted\endcsname\PT@true%
\fi
\fi
% \end{macrocode}
% Finally, we update the current column to |#2|, and, of course, we
% ignore spaces after the |\fromto| command.
% \begin{macrocode}
\def\PT@currentcol{PT@col@#2}%
\let\PT@infromto\empty
\ignorespaces}%
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@checkrerun}
% If we have changed something with the trusted widths, we have
% to check whether we are in a situation where we are using previously
% defined columns. If so, we have to rerun \LaTeX.
% \begin{macrocode}
\def\PT@checkrerun
{\ifnum\PT@inrestore=\PT@true\relax
\PT@rerun
\fi}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@resetcolumn}
% At the end of a line, we reset the current column to the special
% column |@begin@|.
% \begin{macrocode}
\newcommand*{\PT@resetcolumn}[1][]%
{\let\PT@currentcol\PT@nullcol}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@nullcol}
% The name of the |@begin@| column as a macro, to be able to compare
% to it with |\ifx|.
% \begin{macrocode}
\def\PT@nullcol
{PT@col@@begin@}
% \end{macrocode}
% \end{macro}
%
% \subsection{Sorting and numbering the columns}
%
% Not much needs to be done here, all the work is done by the
% macros supplied by the \textsf{lazylist} package.
% We just provide a few additional commands to facilitate
% their use.
%
% \begin{macro}{\Execute}
% \begin{macro}{\Sequence}
% With |\Execute|, a list of commands (with sideeffects) can
% be executed in sequence. Usually, first a command will be
% mapped over a list, and then the resulting list will be
% executed.
% \begin{macrocode}
\def\Execute{\Foldr\Sequence\empty}
\def\Sequence #1#2{#1#2}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\ShowColumn}
% This is a debugging macro, that is used to output the list
% of columns in a pretty way. The columns internally get
% prefixes to their names, to prevent name conflicts with
% normal commands. In the debug output, we gobble this prefix
% again.
% \begin{macrocode}
\def\ShowColumn #1%
{\ShowColumn@#1\ShowColumn@}
\def\ShowColumn@ PT@col@#1\ShowColumn@
{#1 }
\def\StripColumn #1%
{\expandafter\StripColumn@#1\StripColumn@}
\def\StripColumn@ PT@col@#1\StripColumn@
{#1}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\Print}
% Prints a list of columns, using |\ShowColumn|.
% \begin{macrocode}
\def\Print#1{\Execute{\Map\ShowColumn#1}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@TeXif}
% This is an improved version of \textsf{lazylist}'s |\TeXif|.
% It does have an additional |\relax| to terminate the condition.
% The |\relax| is gobbled again to keep it fully expandable.
% \begin{macrocode}
\def\PT@TeXif #1%
{\expandafter\@gobble#1\relax
\PT@gobblefalse
\else\relax
\gobbletrue
\fi}
\def\PT@gobblefalse\else\relax\gobbletrue\fi #1#2%
{\fi #1}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@ltmax}
% The order by which the columns are sorted is given by
% the order on their (untrusted) widths.
% \begin{macrocode}
\def\PT@ltmax #1#2%
{\PT@TeXif{\ifdim\csname #1.max\endcsname<\csname #2.max\endcsname}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@numbercol}
% This assigns the next consecutive number to a column.
% We also reassign |PT@lastcol| to remember the final column.
% \begin{macrocode}
\def\PT@numbercol #1%
{%\PT@typeout@{numbering #1 as \the\PT@cols}%
\PT@enamedef{#1.num}{\the\PT@cols}%
\def\PT@lastcol{#1}%
\advance\PT@cols by 1\relax}
% \end{macrocode}
% \end{macro}
%
% \subsection{Typesetting the table}
%
% \begin{macro}{\PT@preamble}
% The table's preamble is created by mapping this function
% over the column list and then |\Execute|ing \dots
% New: We always use |l|, as the specific type is always
% given by the |\multicolumn|.
% Yet new: We use |@{}l@{}|, to prevent column separation
% space from being generated.
% \begin{macrocode}
\def\PT@preamble #1%
% {\csname #1.type\endcsname}
% {l}
{l@{}}
% \end{macrocode}
% \end{macro}
%
% Remember that there are three important macros that
% occur in the body of the polytable: |\column|, |\fromto|,
% and |\nextline|. The |\column| macro is only really used
% in the very first trial run, so there is nothing new
% we have to do here, but the other two have to be
% redefined.
%
% \begin{macro}{\PT@resetandcr}
% This is what |\nextline| does in the typesetting phase.
% It resets the current column, but it also calls the table
% environment's newline macro |\\| \dots
% If we are \emph{not} in the last column, we insert
% an implicit |fromto|. This is needed for the boxed environment to
% make each column equally wide. Otherwise, if the boxed environment
% is typeset in a centered way, things will go wrong.
% \begin{macrocode}
\newcommand{\PT@resetandcr}[1][0pt]%
{\ifx\PT@currentcol\PT@lastcol
\else
\ifx\PT@currentcol\PT@nullcol
\edef\PT@currentcol{\Head{\Tail\PT@sortedlist}}%
\fi
\edef\PT@currentcol@{\StripColumn\PT@currentcol}%
\edef\PT@lastcol@
{\StripColumn\PT@lastcol}%
\PT@typeout@{adding implicit fromto from \PT@currentcol@
\space to \PT@lastcol@}%
\expandafter\expandafter\expandafter\fromto
\expandafter\expandafter\expandafter{%
\expandafter\expandafter\expandafter\PT@currentcol@
\expandafter}\expandafter{\PT@lastcol@}{}%
\fi
\PT@typeout@{Next line ...}%
\PT@resetcolumn\\[#1]}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@multicolumn}
% All the |\fromto|s are expanded into |\multicolumn| calls,
% which is achieved by this quite tricky macro. Part of the
% trickyness stems from the fact that a |\multicolumn|'s
% expansion starts with |\omit| which is a plain \TeX\ primitive
% that causes the template for a table column to be ignored.
% But |\omit| has to be the first token (after expansion)
% in a column to be valid, which is why the alignment tabs |&|
% and the |\multicolumn| calls have to be close to each other.
% It would maybe be better to call |\omit| manually and hack
% |\multicolumn| later!!
%
% This macro gets three arguments. The first is the column
% in which the entry begins, the second is the column
% \emph{before} which the entry stops, and the third contains
% the contents that should be typeset in this range.
% \begin{macrocode}
\def\PT@multicolumn #1#2#3%
% \end{macrocode}
% We start by producing an |\omit| to indicate that we want
% to ignore the column format that has been specified in the
% table header. After that, we disable the |\omit| command,
% because we will later call |\multicolumn| which contains
% another one. A second |\omit| would usually cause an error.
% TODO: Make this work to simplify the rest. For now, we don't
% use this.
% \begin{macrocode}
{%\omit\PT@disableomitonce
% \end{macrocode}
% We skip ahead until we are in the column in which the entry
% should start. For this, we store the
% number of the column we want to start in and subtract
% the current columns number. If the current column is the
% null column, we have to adjust by $-1$ which is not nice,
% but necessary \dots
% In 0.4.3: added missing relax after |\global\advance|.
% \begin{macrocode}
% skip to current position
\global\PT@cols=\@nameuse{PT@col@#1.num}%
\global\advance\PT@cols
by -\expandafter\csname\PT@currentcol.num\endcsname\relax
\ifx\PT@currentcol\PT@nullcol
\global\advance\PT@cols by -1\relax%
\fi
% \end{macrocode}
% We now skip by inserting alignment tabs and using a
% multicolumn with no content. It might be nicer to just
% use as many tabs as necessary, because we could do with
% less case distinctions. The current value of |\PT@cols|
% indicates how many tabs we have to insert, minus one.
% We will insert that one tab (which is the minimum we have
% to insert) just before we insert the content, and first
% deal with the extra tabs.
% \begin{macrocode}
\PT@typeout@{skipping:
nf=\expandafter\ShowColumn
\expandafter{\PT@currentcol}nt=#1 %
from=\expandafter
\csname\PT@currentcol.num\endcsname\space
to=\@nameuse{PT@col@#1.num}}%
\ifnum\PT@cols>0\relax
% \end{macrocode}
% So there are extra tabs necessary.
% \begin{macrocode}
\ifnum\PT@cols>1\relax
% \end{macrocode}
% We can use a multicolumn to save time.
% \begin{macrocode}
\global\advance\PT@cols by -1\relax
\PT@typeout@{after next &, multicolumn \the\PT@cols\space blank}%
\PT@NextCol
\multicolumn{\the\PT@cols}{@{}l@{}}{}%
\fi
\PT@NextCol
\fi
% \end{macrocode}
% We now are in the correct column and can print the contents. Again,
% we have to check if we have to use a |\multicolumn|. If we do, we will
% use the formatting type of the first column that it spans, in contrast
% to normal |\multicolumn|s which always take an extra parameter to
% determine how to format their contents. An optional parameter should
% be introduced here to make overriding the default template possible!!
% New: we always use a |\multicolumn|, otherwise spacing will be
% inconsistent sometimes.
% \begin{macrocode}
\global\PT@cols=\@nameuse{PT@col@#2.num}%
\global\advance\PT@cols by -\@nameuse{PT@col@#1.num}\relax%
%\ifnum\PT@cols>1\relax
% we always skip one column
\PT@typeout@{after next &,
putting text in \the\PT@cols\space multicol}%
\PT@typeout@{nf=#1 nt=#2 %
from=\@nameuse{PT@col@#1.num}
to=\@nameuse{PT@col@#2.num}}%
\expandafter\global\expandafter\let\expandafter\PT@temp
\csname PT@col@#1.type\endcsname%
\PT@NextCol
% use multicolumn
\expandafter\multicolumn
\expandafter{\expandafter\the\expandafter\PT@cols
\expandafter}\expandafter{\PT@temp}{#3}%
%\PT@typeout@{!!!!}%
%\else
% \PT@NextCol
% #3%
%\fi
% \end{macrocode}
% We reset the current column to |#2| and ignore spaces after
% the command. Then we are done \dots
% \begin{macrocode}
% set current column
\def\PT@currentcol{PT@col@#2}%
\ignorespaces}%
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@NextCol}
% We hide the tab |&| in a macro, mostly to be able to
% add debugging output.
% \begin{macrocode}
\def\PT@NextCol
{\PT@typeout@{ & }%
&}%
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@placeinbox}
% This macro is an alternative for |\PT@multicolumn|. It can be used
% to produce a simple box-based output instead of a table. We use
% the precomputed width information to typeset the contents of the
% table in aligned boxes. The arguments are the same as for
% |\PT@multicolumn|, i.e.~the start and the end columns, plus the contents.
% \begin{macrocode}
\def\PT@placeinbox#1#2#3%
% \end{macrocode}
% We start by computing the amount of whitespace that
% must be inserted before the entry begins. We then insert that amount
% of space.
% \begin{macrocode}
{\PT@colwidth=\@nameuse{PT@col@#1.max}%
\advance\PT@colwidth by -\expandafter\csname\PT@currentcol.max\endcsname
\leavevmode
\hb@xt@\PT@colwidth{%
\expandafter\@mkpream\expandafter{@{}l@{}}%
\let\@sharp\empty%
%\show\@preamble
\@preamble}%
% We continue by computing the width of the current entry.
% \begin{macrocode}
\PT@colwidth=\@nameuse{PT@col@#2.max}%
\advance\PT@colwidth by -\@nameuse{PT@col@#1.max}\relax%
% \end{macrocode}
% In the previous version, we really generated a hbox at this place.
% However, this is not so nice with respect to spacing and tabular
% specifiers. Therefore, we now use either an array or a tabular
% environment that can reuse the given specifier.
% \begin{macrocode}
\ifmmode
\PT@typeout@{*math mode*}%
\let\d@llarbegin=$%$
\let\d@llarend=$%$
\let\col@sep=\arraycolsep
\else
\PT@typeout@{*text mode*}%
\let\d@llarbegin=\begingroup
\let\d@llarend=\endgroup
\let\col@sep=\tabcolsep
\fi
%\def\PT@currentcol{PT@col@#1}%
\expandafter\expandafter\expandafter
\def\expandafter\expandafter\expandafter\PT@currentpreamble
\expandafter\expandafter\expandafter
{\csname PT@col@#1.type\endcsname}%
%\ifx\PT@currentcol\PT@nullcol
%\else
% \PT@addbeginmacro\PT@currentpreamble{@{}}%
%\fi
% \end{macrocode}
% Now we proceed very much like in the test run(s), but we
% really output the box, and we use a specific width.
% \begin{macrocode}
\hb@xt@\PT@colwidth{%
\expandafter\@mkpream\expandafter{\PT@currentpreamble}%
\def\@sharp{\strut #3}%
%\show\@preamble
\@preamble}%
% \end{macrocode}
% Finally, we have to reset the current column and
% ignore spaces.
% \begin{macrocode}
\def\PT@currentcol{PT@col@#2}%
\ignorespaces}%
% \end{macrocode}
% \end{macro}
%
% \subsection{Saving and restoring column widths}
%
% Column width information can be saved under a name and thus
% be reused in other tables. The idea is that the command
% |\savecolumns| can be issued inside a polytable to save the
% current column information, and |\restorecolumns| can be used
% to make that information accessible in a later table. All tables
% using the same information should have the same column widths,
% which means that some information might need to be passed back.
% Therefore, we need to write to an auxiliary file.
% TODO: As implemented now, this only really works in conjunction
% with the |pboxed| environment.
%
% Both |\savecolumns| and |\restorecolumns| are mapped to the internal
% commands |\PT@savewidths| and |\PT@restorewidths|. Both take an
% optional argument specifying a name for the column width
% information. Thereby, multiple sets of such information can be
% used simultaneously.
%
% One important thing to consider is that the widths read from the
% auxiliary file must not be trusted. The user may have edited the
% source file before the rerun, and therefore, the values read might
% actually be too large (or too small, but this is less dangerous).
%
% The way we solve this problem is to distinguish two width values
% per column: the trusted width, only using information from the
% current run, and the untrusted width, incorportating information
% from the |.aux| file. An untrusted width can become (conditionally)
% trusted if it is reached in the computation with respect to an
% earlier column. (Conditionally, because its trustworthiness still
% depends on the earlier columns being trustworthy.) In the end, we
% can check whether all untrusted widths are conditionally trusted.
%
% We write the final, the maximum widths, into the auxiliary file.
% We perform the
% write operation when we are sure that a specific set is
% no longer used. This is the case when we save a new set under the
% same name, or at the end of the document. The command
% |\PT@verifywidths| takes care of this procedure.
% This command will also check if a rerun is necessary, and issue
% an appropriate warning if that should be the case.
%
% \begin{macro}{\PT@setmaxwidth}
% First, we need a macro to help us interpreting the contents of
% the |.aux| file. New v0.4.1: We need to define the restored
% columns with the |\column| command, because otherwise we will
% have problems in the case that later occurences of tables in
% the document that belong to the same set, but define additional
% columns. (Rerun warnings appear ad infinitum.)
% In v0.4.2: columns with width 0.0 are now always trusted.
% \begin{macrocode}
\newcommand*{\PT@setmaxwidth}[3][\PT@false]% #2 column name, #3 maximum width
{\@namedef{PT@col@#2.max}{#3}%
\ifdim#3=0pt\relax
\expandafter\let\csname PT@col@#2.trusted\endcsname=\PT@true%
\else
\expandafter\let\csname PT@col@#2.trusted\endcsname=#1%
\fi
\column{#2}{}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@loadtable}
% Now, we can load table information that has been read from the
% |.aux| file. Note that a |\csname| construct expands to |\relax|
% if undefined.
% \begin{macrocode}
\def\PT@loadtable#1% #1 table id number
{%\expandafter\show\csname PT@restore@\romannumeral #1\endcsname
%\show\column
\PT@typeout@
{Calling \expandafter\string
\csname PT@restore@\romannumeral #1\endcsname.}%
\let\maxcolumn\PT@setmaxwidth
%\expandafter\show\csname PT@load@\romannumeral #1\endcsname
\csname PT@restore@\romannumeral #1\endcsname}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@loadtablebyname}
% Often, we want to access table information by a column width set
% name. We make the maximum column widths accessible, but also the
% information from the previous table that has been using the same
% column width set.
% \begin{macrocode}
\def\PT@loadtablebyname#1% #1 set name
{\PT@typeout@{Loading table information for column width set #1.}%
\expandafter\PT@loadtable\expandafter{\csname PT@widths@#1\endcsname}}%
% \advance\PT@cols by \PT@restoredcols\relax
% \expandafter\PT@appendmacro\expandafter\PT@allcols
% \expandafter{\PT@restoredallcols}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@saveinformation}
% In each table for which the widths get reused (i.e., in all tables
% that use either |\savecolumns| or |\restorecolumns|, we have to store
% all important information for further use.
% \begin{macrocode}
\def\PT@saveinformation#1% #1 set name
{\expandafter\def\expandafter\PT@temp\expandafter
{\csname PT@widths@#1\endcsname}%
\expandafter\def\expandafter\PT@temp\expandafter
{\csname PT@restore@\romannumeral\PT@temp\endcsname}%
\expandafter\gdef\PT@temp{}% start empty
% this is: \Execute{\Map{\PT@savecolumn{\PT@temp}}\PT@sortedlist}
\expandafter\Execute\expandafter{\expandafter
\Map\expandafter{\expandafter\PT@savecolumn
\expandafter{\PT@temp}}\PT@sortedlist}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@savecolumn}
% A single column is saved by this macro.
% \begin{macrocode}
\def\PT@savecolumn#1#2% #1 macro name, #2 column name
{\PT@typeout@{saving column #2 in \string #1 ...}%
\def\PT@temp{#2}%
\ifx\PT@temp\PT@nullcol
\PT@typeout@{skipping nullcol ...}%
\else
\PT@typeout@{max=\csname #2.max\endcsname, %
width=\csname #2.width\endcsname, %
trusted=\csname #2.trusted\endcsname}%
% we need the column command in here
% we could do the same in \column, but then the location of
% \save / \restore matters ...
\PT@gaddendmacro{#1}{\maxcolumn}%
\ifnum\csname #2.trusted\endcsname=\PT@true\relax
\PT@gaddendmacro{#1}{[\PT@true]}%
\fi
\edef\PT@temp{\StripColumn{#2}}%
\PT@addargtomacro{#1}{PT@temp}%
\PT@addargtomacro{#1}{#2.max}%
\PT@gaddendmacro{#1}{\column}%
\PT@addoptargtomacro{#1}{#2.width}%
\edef\PT@temp{\StripColumn{#2}}%
\PT@addargtomacro{#1}{PT@temp}%
\PT@addargtomacro{#1}{#2.type}%
%\show#1%
\fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@savewidths}
% If we really want to save column width information, then the first
% thing we should worry about is that there might already have been
% a set with the name in question. Therefore, we will call
% |\PT@verifywidths| for that set. In the case that there is no set
% of this name yet, we will schedule the set for verification at the
% end of document.
% \begin{macrocode}
\newcommand*{\PT@savewidths}[1][default@]
{\PT@typeout@{Executing \string\savecolumns [#1].}%
\def\PT@currentwidths{#1}%
\PT@verifywidths{#1}%
% \end{macrocode}
% We now reserve a new unique number for this column width set
% by increasing the |\PT@table| counter. We then associate
% the given name (or |default@|) with the counter value and
% restore the widths from the |.aux| file if they are present.
% \begin{macrocode}
\global\advance\PT@table by 1\relax
\expandafter\xdef\csname PT@widths@#1\endcsname
{\the\PT@table}%
\PT@loadtable{\PT@table}%
\ignorespaces}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@restorewidths}
% Restoring information is quite simple. We just load all information
% available.
% \begin{macrocode}
\newcommand*{\PT@restorewidths}[1][default@]
{\PT@typeout@{Executing \string\restorecolumns [#1].}%
\def\PT@currentwidths{#1}%
\let\PT@inrestore\PT@true
\PT@loadtablebyname{#1}%
\ignorespaces}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@comparewidths}
% \begin{macrocode}
\def\PT@comparewidths#1% #1 full column name
{\@ifundefined{#1.max}%
{\PT@typeout@{computed width for #1 is fine ...}}%
{\ifdim\csname #1.max\endcsname>\csname #1.width\endcsname\relax
\PT@typeout@{Preferring saved width for \StripColumn{#1}.}%
\PT@changedtrue
\PT@colwidth=\@nameuse{#1.max}\relax
\PT@enamedef{#1.width}{\the\PT@colwidth}%
\fi}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@trustedmax}
% \begin{macrocode}
\def\PT@trustedmax#1%
{\PT@TeXif{\ifnum\csname #1.trusted\endcsname=\PT@true}}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\PT@equalwidths}
% \begin{macrocode}
\def\PT@equalwidths#1% #1 full column name
{\@ifundefined{#1.max}{}%
{\ifdim\csname #1.max\endcsname=\csname #1.width\endcsname\relax
\PT@typeout@{col #1 is okay ...}%
\else
\PT@rerun% a rerun is needed
\fi}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@verifywidths}
% \begin{macrocode}
\def\PT@verifywidths#1% #1 column width set name
{\@ifundefined{PT@widths@#1}%
{\PT@typeout@{Nothing to verify yet for set #1.}%
\PT@typeout@{Scheduling set #1 for verification at end of document.}%
\AtEndDocument{\PT@verifywidths{#1}}}%
{\PT@typeout@{Verifying column width set #1.}%
\expandafter\PT@verify@widths\expandafter
{\csname PT@widths@#1\endcsname}{#1}}}
\def\PT@verify@widths#1#2% #1 set id number, #2 set name
{\@ifundefined{PT@restore@\romannumeral #1}{}%
{\begingroup
\let\column\PT@firstrun@column
\PT@cols=0\relax%
\def\PT@allcols{\Nil}%
\PT@loadtablebyname{#2}%
\PT@table=#1\relax
% nullcolumn is not loaded, therefore:
\expandafter\def\csname\PT@nullcol .width\endcsname{0pt}%
% checking trust
\PT@prelazylist
\All{\PT@trustedmax}{\PT@allcols}%
{\PT@typeout@{All maximum widths can be trusted -- writing .max!}%
\PT@save@table{.max}}%
{\PT@typeout@{Untrustworthy maximums widths -- writing .width!}%
\PT@rerun
\PT@save@table{.width}}%
\PT@postlazylist
\endgroup}%
\PT@typeout@{Verification for #2 successful.}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@save@table}
% Here we prepare to write maximum column widths to the
% |.aux| file.
% \begin{macrocode}
\def\PT@save@table#1%
{\PT@typeout@{Saving column width information.}%
\if@filesw
\PT@prelazylist
{\immediate\write\@auxout{%
\gdef\expandafter\noexpand
\csname PT@restore@\romannumeral\PT@table\endcsname
{\Execute{\Map{\PT@write@column{#1}}\PT@allcols}}}}%
\PT@postlazylist
\fi}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PT@write@column}
% We define the column command to write to the file.
% \begin{macrocode}
\def\PT@write@column #1#2%
{\noexpand\maxcolumn^^J%
{\StripColumn{#2}}%
{\@nameuse{#2#1}}}%
% \end{macrocode}
% \end{macro}
%
% \subsection{The user environments}
%
% It remains to define the three environments to be
% called by the user.
% \begin{macrocode}
\newenvironment{ptabular}[1][c]%
{\def\PT@begin{\tabular[#1]}%
\let\PT@end\endtabular
\beginpolytable}
{\endpolytable}
\newenvironment{parray}[1][c]%
{\def\PT@begin{\array[#1]}%
\let\PT@end\endarray
\beginpolytable}
{\endpolytable}
\def\pboxed{%
\let\PT@begin\@gobble
\let\PT@end\empty
\let\PT@multicolumn\PT@placeinbox
\expandafter\beginpolytable\ignorespaces}
\let\endpboxed\endpolytable
% \end{macrocode}
%
% That is all.
% \begin{macrocode}
%</package>
% \end{macrocode}
%
% \Finale
\endinput
|