1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
|
\documentclass[twoside,letterpaper]{rapport3}
%\nofiles
\usepackage{comment,makeidx}
\usepackage{times}
\renewcommand{\ttdefault}{cmtt}
\usepackage[plainpages=true,pagebackref=true]{hyperref}
\usepackage{german}
% german
\righthyphenmin=3
\mdqoff
\captionsenglish
\makeindex
\usepackage{fancyhdr}
% headers & footers
\pagestyle{fancy}
% foot
\lfoot[\thepage]{\protect\small\protect\it Victor Eijkhout -- \protect\TeX\ by Topic}
\rfoot[{\protect\small\protect\it Victor Eijkhout -- \protect\TeX\ by Topic}]{\thepage}
\cfoot{}
% head
\lhead[\let\\\relax \let\uppercase\relax \leftmark]{\relax}
\chead{}
\rhead[\relax]{\let\\\relax \let\uppercase\relax \rightmark}
\newdimen\tempdima \newdimen\tempdimb
% these are fine
\def\nl{\protect\\}\def\n#1{{\tt #1}}\def\cs#1{{\tt\char`\\#1}}\let\csc\cs
\def\lb{{\tt\char`\{}}\def\rb{{\tt\char`\}}}
\def\gr#1{$\langle$#1$\rangle$}\def\key#1{{\tt#1}}
\def\alt{}\def\altt{}%this way in manstijl
\def\ldash{\unskip\ --\nobreak\ \ignorespaces}
\def\rdash{\unskip\nobreak\ --\ \ignorespaces}
% check these
\def\hex{{\tt"}}
\def\ascii{{\sc ascii}}
\def\ebcdic{{\sc ebcdic}}
\def\IniTeX{Ini\TeX}\def\LamsTeX{LAMS\TeX}\def\VirTeX{Vir\TeX}
\def\AmsTeX{Ams\TeX}
\def\TeXbook{the \TeX\ book}\def\web{{\sc web}}
% needs major thinking
\newenvironment{disp}{\begin{quotation}}{\end{quotation}}
\newenvironment{Disp}{\begin{quotation}}{\end{quotation}}
\newenvironment{tdisp}{\begin{quotation}}{\end{quotation}}
\newenvironment{example}{\begin{quotation}}{\end{quotation}}
\newenvironment{inventory}{\begin{description}}{\end{description}}
\newenvironment{glossinventory}{\begin{description}}{\end{description}}
\def\gram#1{\gr{#1}}%???
%
% index
%
\def\term#1\par{\index{#1}}
\def\howto#1\par{}
\def\cstoidx#1\par{\index{#1@\cs{#1}@}}
\def\csterm#1\par{\cstoidx #1\par\cs{#1}}
\def\csidx#1{\cstoidx #1\par\cs{#1}}
\begin{document}
\def\tmc{\tracingmacros=2 \tracingcommands\tracingmacros}
%%%%%%%%%%%%%%%%%%%
\makeatletter
\def\snugbox{\hbox\bgroup\setbox\z@\vbox\bgroup
\leftskip\z@
\bgroup\aftergroup\make@snug
\let\next=}
\def\make@snug{\par\sn@gify\egroup \box\z@\egroup}
\def\sn@gify
{\skip\z@=\lastskip \unskip
\advance\skip\z@\lastskip \unskip
\unpenalty
\setbox\z@\lastbox
\ifvoid\z@ \nointerlineskip \else {\sn@gify} \fi
\hbox{\unhbox\z@}\nointerlineskip
\vskip\skip\z@
}
\def\figfont{\SansSerif \PointSize:8 \Style:roman }
\newdimen\fbh \fbh=60pt % dimension for easy scaling:
\newdimen\fbw \fbw=60pt % height and width of character box
\newdimen\dh \newdimen\dw % height and width of current character box
\newdimen\lh % height of previous character box
\newdimen\lw \lw=.4pt % line weight, instead of default .4pt
\def\hdotfill{\noindent
\leaders\hbox{\vrule width 1pt height\lw
\kern4pt
\vrule width.5pt height\lw}\hfill\hbox{}
\par}
\def\hlinefill{\noindent
\leaders\hbox{\vrule width 5.5pt height\lw }\hfill\hbox{}
\par}
\def\stippel{$\qquad\qquad\qquad\qquad$}
\makeatother
%%%%%%%%%%%%%%%%%%%
\begin{comment}
\def\SansSerif{\Typeface:macHelvetica }
\def\SerifFont{\Typeface:macTimes }
\def\SansSerif{\Typeface:bsGillSans }
\def\SerifFont{\Typeface:bsBaskerville }
\end{comment}
\let\SansSerif\relax \def\italic{\it}
\let\SerifFont\relax \def\MainFont{\rm}
\let\SansSerif\relax
\let\SerifFont\relax
\let\PopIndentLevel\relax \let\PushIndentLevel\relax
\let\ToVerso\relax \let\ToRecto\relax
\begin{comment}
\def\stop@command@suffix{stop}
\let\PopListLevel\PopIndentLevel
\let\FlushRight\relax
\let\flushright\FlushRight
\let\SetListIndent\LevelIndent
\def\awp{\ifhmode\vadjust{\penalty-10000 }\else
\penalty-10000 \fi}
\end{comment}
\let\awp\relax
\let\PopIndentLevel\relax \let\PopListLevel\relax
\showboxdepth=-1
\def\endofchapter{\vfill\noindent}
\title{\TeX\ by Topic, A \TeX nician's Reference}
\date{}
\author{Victor Eijkhout}
\maketitle
\begin{minipage}[h]{1.0\linewidth}
Copyright \copyright\ 2007 Victor Eijkhout.\\
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
Texts. A copy of the license is included in the section entitled "GNU
Free Documentation License".
\medskip
This document is based on the book \TeX\ by Topic,
copyright 1991-2007 Victor Eijkhout. This book was
printed in~1991 by Addison-Wesley UK, ISBN 0-201-56882-9, reprinted
in~1993, pdf version first made freely available in~2001.
\end{minipage}
\tableofcontents
\pagebreak
\addcontentsline{toc}{section}{License}
\paragraph*{\bf License}
GNU Free Documentation License
Version 1.2, November 2002
Copyright \copyright\ 2000,2001,2002 Free Software Foundation, Inc.
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
0. PREAMBLE
The purpose of this License is to make a manual, textbook, or other
functional and useful document "free" in the sense of freedom: to
assure everyone the effective freedom to copy and redistribute it,
with or without modifying it, either commercially or noncommercially.
Secondarily, this License preserves for the author and publisher a way
to get credit for their work, while not being considered responsible
for modifications made by others.
This License is a kind of "copyleft", which means that derivative
works of the document must themselves be free in the same sense. It
complements the GNU General Public License, which is a copyleft
license designed for free software.
We have designed this License in order to use it for manuals for free
software, because free software needs free documentation: a free
program should come with manuals providing the same freedoms that the
software does. But this License is not limited to software manuals; it
can be used for any textual work, regardless of subject matter or
whether it is published as a printed book. We recommend this License
principally for works whose purpose is instruction or reference.
1. APPLICABILITY AND DEFINITIONS
This License applies to any manual or other work, in any medium, that
contains a notice placed by the copyright holder saying it can be
distributed under the terms of this License. Such a notice grants a
world-wide, royalty-free license, unlimited in duration, to use that
work under the conditions stated herein. The "Document", below, refers
to any such manual or work. Any member of the public is a licensee,
and is addressed as "you". You accept the license if you copy, modify
or distribute the work in a way requiring permission under copyright
law.
A "Modified Version" of the Document means any work containing the
Document or a portion of it, either copied verbatim, or with
modifications and/or translated into another language.
A "Secondary Section" is a named appendix or a front-matter section of
the Document that deals exclusively with the relationship of the
publishers or authors of the Document to the Document's overall
subject (or to related matters) and contains nothing that could fall
directly within that overall subject. (Thus, if the Document is in
part a textbook of mathematics, a Secondary Section may not explain
any mathematics.) The relationship could be a matter of historical
connection with the subject or with related matters, or of legal,
commercial, philosophical, ethical or political position regarding
them.
The "Invariant Sections" are certain Secondary Sections whose titles
are designated, as being those of Invariant Sections, in the notice
that says that the Document is released under this License. If a
section does not fit the above definition of Secondary then it is not
allowed to be designated as Invariant. The Document may contain zero
Invariant Sections. If the Document does not identify any Invariant
Sections then there are none.
The "Cover Texts" are certain short passages of text that are listed,
as Front-Cover Texts or Back-Cover Texts, in the notice that says that
the Document is released under this License. A Front-Cover Text may be
at most 5 words, and a Back-Cover Text may be at most 25 words.
A "Transparent" copy of the Document means a machine-readable copy,
represented in a format whose specification is available to the
general public, that is suitable for revising the document
straightforwardly with generic text editors or (for images composed of
pixels) generic paint programs or (for drawings) some widely available
drawing editor, and that is suitable for input to text formatters or
for automatic translation to a variety of formats suitable for input
to text formatters. A copy made in an otherwise Transparent file
format whose markup, or absence of markup, has been arranged to thwart
or discourage subsequent modification by readers is not Transparent.
An image format is not Transparent if used for any substantial amount
of text. A copy that is not "Transparent" is called "Opaque".
Examples of suitable formats for Transparent copies include plain
ASCII without markup, Texinfo input format, LaTeX input format, SGML
or XML using a publicly available DTD, and standard-conforming simple
HTML, PostScript or PDF designed for human modification. Examples of
transparent image formats include PNG, XCF and JPG. Opaque formats
include proprietary formats that can be read and edited only by
proprietary word processors, SGML or XML for which the DTD and/or
processing tools are not generally available, and the
machine-generated HTML, PostScript or PDF produced by some word
processors for output purposes only.
The "Title Page" means, for a printed book, the title page itself,
plus such following pages as are needed to hold, legibly, the material
this License requires to appear in the title page. For works in
formats which do not have any title page as such, "Title Page" means
the text near the most prominent appearance of the work's title,
preceding the beginning of the body of the text.
A section "Entitled XYZ" means a named subunit of the Document whose
title either is precisely XYZ or contains XYZ in parentheses following
text that translates XYZ in another language. (Here XYZ stands for a
specific section name mentioned below, such as "Acknowledgements",
"Dedications", "Endorsements", or "History".) To "Preserve the Title"
of such a section when you modify the Document means that it remains a
section "Entitled XYZ" according to this definition.
The Document may include Warranty Disclaimers next to the notice which
states that this License applies to the Document. These Warranty
Disclaimers are considered to be included by reference in this
License, but only as regards disclaiming warranties: any other
implication that these Warranty Disclaimers may have is void and has
no effect on the meaning of this License.
2. VERBATIM COPYING
You may copy and distribute the Document in any medium, either
commercially or noncommercially, provided that this License, the
copyright notices, and the license notice saying this License applies
to the Document are reproduced in all copies, and that you add no
other conditions whatsoever to those of this License. You may not use
technical measures to obstruct or control the reading or further
copying of the copies you make or distribute. However, you may accept
compensation in exchange for copies. If you distribute a large enough
number of copies you must also follow the conditions in section 3.
You may also lend copies, under the same conditions stated above, and
you may publicly display copies.
3. COPYING IN QUANTITY
If you publish printed copies (or copies in media that commonly have
printed covers) of the Document, numbering more than 100, and the
Document's license notice requires Cover Texts, you must enclose the
copies in covers that carry, clearly and legibly, all these Cover
Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
the back cover. Both covers must also clearly and legibly identify you
as the publisher of these copies. The front cover must present the
full title with all words of the title equally prominent and visible.
You may add other material on the covers in addition. Copying with
changes limited to the covers, as long as they preserve the title of
the Document and satisfy these conditions, can be treated as verbatim
copying in other respects.
If the required texts for either cover are too voluminous to fit
legibly, you should put the first ones listed (as many as fit
reasonably) on the actual cover, and continue the rest onto adjacent
pages.
If you publish or distribute Opaque copies of the Document numbering
more than 100, you must either include a machine-readable Transparent
copy along with each Opaque copy, or state in or with each Opaque copy
a computer-network location from which the general network-using
public has access to download using public-standard network protocols
a complete Transparent copy of the Document, free of added material.
If you use the latter option, you must take reasonably prudent steps,
when you begin distribution of Opaque copies in quantity, to ensure
that this Transparent copy will remain thus accessible at the stated
location until at least one year after the last time you distribute an
Opaque copy (directly or through your agents or retailers) of that
edition to the public.
It is requested, but not required, that you contact the authors of the
Document well before redistributing any large number of copies, to
give them a chance to provide you with an updated version of the
Document.
4. MODIFICATIONS
You may copy and distribute a Modified Version of the Document under
the conditions of sections 2 and 3 above, provided that you release
the Modified Version under precisely this License, with the Modified
Version filling the role of the Document, thus licensing distribution
and modification of the Modified Version to whoever possesses a copy
of it. In addition, you must do these things in the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct
from that of the Document, and from those of previous versions (which
should, if there were any, be listed in the History section of the
Document). You may use the same title as a previous version if the
original publisher of that version gives permission. B. List on the
Title Page, as authors, one or more persons or entities responsible
for authorship of the modifications in the Modified Version, together
with at least five of the principal authors of the Document (all of
its principal authors, if it has fewer than five), unless they release
you from this requirement. C. State on the Title page the name of the
publisher of the Modified Version, as the publisher. D. Preserve all
the copyright notices of the Document. E. Add an appropriate
copyright notice for your modifications adjacent to the other
copyright notices. F. Include, immediately after the copyright
notices, a license notice giving the public permission to use the
Modified Version under the terms of this License, in the form shown in
the Addendum below. G. Preserve in that license notice the full lists
of Invariant Sections and required Cover Texts given in the Document's
license notice. H. Include an unaltered copy of this License. I.
Preserve the section Entitled "History", Preserve its Title, and add
to it an item stating at least the title, year, new authors, and
publisher of the Modified Version as given on the Title Page. If there
is no section Entitled "History" in the Document, create one stating
the title, year, authors, and publisher of the Document as given on
its Title Page, then add an item describing the Modified Version as
stated in the previous sentence. J. Preserve the network location, if
any, given in the Document for public access to a Transparent copy of
the Document, and likewise the network locations given in the Document
for previous versions it was based on. These may be placed in the
"History" section. You may omit a network location for a work that was
published at least four years before the Document itself, or if the
original publisher of the version it refers to gives permission. K.
For any section Entitled "Acknowledgements" or "Dedications", Preserve
the Title of the section, and preserve in the section all the
substance and tone of each of the contributor acknowledgements and/or
dedications given therein. L. Preserve all the Invariant Sections of
the Document, unaltered in their text and in their titles. Section
numbers or the equivalent are not considered part of the section
titles. M. Delete any section Entitled "Endorsements". Such a section
may not be included in the Modified Version. N. Do not retitle any
existing section to be Entitled "Endorsements" or to conflict in title
with any Invariant Section. O. Preserve any Warranty Disclaimers. If
the Modified Version includes new front-matter sections or appendices
that qualify as Secondary Sections and contain no material copied from
the Document, you may at your option designate some or all of these
sections as invariant. To do this, add their titles to the list of
Invariant Sections in the Modified Version's license notice. These
titles must be distinct from any other section titles.
You may add a section Entitled "Endorsements", provided it contains
nothing but endorsements of your Modified Version by various
parties--for example, statements of peer review or that the text has
been approved by an organization as the authoritative definition of a
standard.
You may add a passage of up to five words as a Front-Cover Text, and a
passage of up to 25 words as a Back-Cover Text, to the end of the list
of Cover Texts in the Modified Version. Only one passage of
Front-Cover Text and one of Back-Cover Text may be added by (or
through arrangements made by) any one entity. If the Document already
includes a cover text for the same cover, previously added by you or
by arrangement made by the same entity you are acting on behalf of,
you may not add another; but you may replace the old one, on explicit
permission from the previous publisher that added the old one.
The author(s) and publisher(s) of the Document do not by this License
give permission to use their names for publicity for or to assert or
imply endorsement of any Modified Version.
5. COMBINING DOCUMENTS
You may combine the Document with other documents released under this
License, under the terms defined in section 4 above for modified
versions, provided that you include in the combination all of the
Invariant Sections of all of the original documents, unmodified, and
list them all as Invariant Sections of your combined work in its
license notice, and that you preserve all their Warranty Disclaimers.
The combined work need only contain one copy of this License, and
multiple identical Invariant Sections may be replaced with a single
copy. If there are multiple Invariant Sections with the same name but
different contents, make the title of each such section unique by
adding at the end of it, in parentheses, the name of the original
author or publisher of that section if known, or else a unique number.
Make the same adjustment to the section titles in the list of
Invariant Sections in the license notice of the combined work.
In the combination, you must combine any sections Entitled "History"
in the various original documents, forming one section Entitled
"History"; likewise combine any sections Entitled "Acknowledgements",
and any sections Entitled "Dedications". You must delete all sections
Entitled "Endorsements."
6. COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other
documents released under this License, and replace the individual
copies of this License in the various documents with a single copy
that is included in the collection, provided that you follow the rules
of this License for verbatim copying of each of the documents in all
other respects.
You may extract a single document from such a collection, and
distribute it individually under this License, provided you insert a
copy of this License into the extracted document, and follow this
License in all other respects regarding verbatim copying of that
document.
7. AGGREGATION WITH INDEPENDENT WORKS
A compilation of the Document or its derivatives with other separate
and independent documents or works, in or on a volume of a storage or
distribution medium, is called an "aggregate" if the copyright
resulting from the compilation is not used to limit the legal rights
of the compilation's users beyond what the individual works permit.
When the Document is included in an aggregate, this License does not
apply to the other works in the aggregate which are not themselves
derivative works of the Document.
If the Cover Text requirement of section 3 is applicable to these
copies of the Document, then if the Document is less than one half of
the entire aggregate, the Document's Cover Texts may be placed on
covers that bracket the Document within the aggregate, or the
electronic equivalent of covers if the Document is in electronic form.
Otherwise they must appear on printed covers that bracket the whole
aggregate.
8. TRANSLATION
Translation is considered a kind of modification, so you may
distribute translations of the Document under the terms of section 4.
Replacing Invariant Sections with translations requires special
permission from their copyright holders, but you may include
translations of some or all Invariant Sections in addition to the
original versions of these Invariant Sections. You may include a
translation of this License, and all the license notices in the
Document, and any Warranty Disclaimers, provided that you also include
the original English version of this License and the original versions
of those notices and disclaimers. In case of a disagreement between
the translation and the original version of this License or a notice
or disclaimer, the original version will prevail.
If a section in the Document is Entitled "Acknowledgements",
"Dedications", or "History", the requirement (section 4) to Preserve
its Title (section 1) will typically require changing the actual
title.
9. TERMINATION
You may not copy, modify, sublicense, or distribute the Document
except as expressly provided for under this License. Any other attempt
to copy, modify, sublicense or distribute the Document is void, and
will automatically terminate your rights under this License. However,
parties who have received copies, or rights, from you under this
License will not have their licenses terminated so long as such
parties remain in full compliance.
10. FUTURE REVISIONS OF THIS LICENSE
The Free Software Foundation may publish new, revised versions of the
GNU Free Documentation License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in
detail to address new problems or concerns. See
http://www.gnu.org/copyleft/.
Each version of the License is given a distinguishing version
number. If the Document specifies that a particular numbered version
of this License "or any later version" applies to it, you have the
option of following the terms and conditions either of that specified
version or of any later version that has been published (not as a
draft) by the Free Software Foundation. If the Document does not
specify a version number of this License, you may choose any version
ever published (not as a draft) by the Free Software Foundation.
\pagebreak
\paragraph*{\bf Preface}
To the casual observer, \TeX\
is not a state-of-the-art typesetting system.
No flashy multilevel menus and interactive manipulation
of text and graphics dazzle the onlooker.
On a less superficial level, however, \TeX\ is a very sophisticated
program, first of all because of the ingeniousness of its
built-in algorithms for such things as paragraph breaking
and make-up of mathematical formulas, and
second because of its almost complete programmability.
The combination of these factors makes it possible for \TeX\
to realize almost every imaginable layout in a highly automated
fashion.
Unfortunately, it also means that \TeX\ has an
unusually large number of commands and parameters,
and that programming \TeX\ can be far from easy.
Anyone wanting to program in \TeX, and maybe
even the ordinary user, would seem to need two books:
a~tutorial that gives a first glimpse of the many
nuts and bolts of \TeX, and after that
a~systematic, complete reference manual.
This book tries to fulfil the latter function.
A~\TeX er who has already made a start
(using any of a number of introductory books
on the market)
should be able to use this book indefinitely thereafter.
In this volume the universe of \TeX\ is presented as
about forty different subjects, each in a separate
chapter.
Each chapter starts out with a list of control sequences
relevant to the topic of that chapter
and proceeds to treat the
theory of the topic.
Most chapters conclude with remarks and examples.
Globally, the chapters are ordered as follows.
The chapters on basic mechanisms are first,
the chapters on text treatment and mathematics are next,
and finally there are some
chapters on output and aspects of \TeX's connections to
the outside world.
%
The book also contains a glossary of \TeX\
commands, tables,
and indexes by example, by control sequence, and by subject.
The subject index refers for most concepts to
only one page, where most of the information
on that topic can be found, as well as references
to the locations of related information.
This book does not treat any specific \TeX\ macro package.
Any parts of the plain format that are treated are those
parts that belong to the `core' of plain \TeX: they
are also present in, for instance, \LaTeX.
Therefore, most remarks about the plain format
are true for \LaTeX, as well as most other formats.
Putting it differently,
if the text refers to the plain format, this should be taken
as a contrast to pure \IniTeX, not to \LaTeX.
By way of illustration, occasionally macros from plain \TeX\
are explained that do not belong to the core.
\medskip\noindent
{\bf Acknowledgment}\nl
I am indebted to Barbara Beeton, Karl Berry, and Nico Poppelier,
who read previous versions of this book. Their comments
helped to improve the presentation.
Also I~would like to thank the participants of
the discussion lists \TeX hax, \TeX-nl, and {\tt comp.text.tex}.
Their questions and answers gave me much food for thought.
Finally, any acknowledgement in a book about \TeX\ ought to
include Donald Knuth for inventing \TeX\ in the
first place. This book is no exception.
\begin{flushright}
Victor Eijkhout\\
Urbana, Illinois, August 1991\\
Knoxville, Tennessee, May 2001
\end{flushright}
\pagebreak
\chapter{The Structure of the \TeX\ Processor}
This book treats the various aspects of \TeX\ in chapters
that are concerned with relatively small, well-delineated,
topics. In this chapter, therefore,
a global picture of the way \TeX\ operates will be given.
Of necessity, many details will be omitted here, but all of
these are treated in later chapters. On the other hand,
the few examples given in this chapter will be repeated
in the appropriate places later on; they are included here
to make this chapter self-contained.
%\point Four \TeX\ processors
\section{Four \TeX\protect\ processors}
The way \TeX\ processes its input can be viewed as
happening on four levels. One might say that
the \TeX\ processor is split into four separate units,
each one accepting the output of the previous stage, and
delivering the input for the next stage. The input of
the first stage is then the \n{.tex} input file; the output
of the last stage is a \n{.dvi} file.
For many purposes it is most convenient, and most insightful,
to consider these four levels of processing as happening
after one another, each one accepting the {\em completed\/}
output of the previous level. In reality this is not true:
all levels are simultaneously
active, and there is interaction between them.
The four levels are (corresponding roughly
to the `eyes', `mouth', `stomach', and `bowels' respectively
in Knuth's original terminology) as follows.
\begin{enumerate}\item
The input processor. This is the piece of \TeX\ that
accepts input lines from the file system of whatever computer
\TeX\ runs on, and turns them into tokens.
Tokens are the internal objects of \TeX:
there are character tokens that constitute the typeset
text, and control sequence tokens that are commands
to be processed by the next two levels.
\item The expansion processor.
Some but not all of the tokens generated in the first level
\ldash macros, conditionals, and a number
of primitive \TeX\ commands \rdash are subject to expansion.
Expansion is the process that replaces some (sequences of)
tokens by other (or no) tokens.
\item The execution processor.
Control sequences that are not expandable are executable,
and this execution takes place on the third level of the
\TeX\ processor.
One part of the activity here concerns changes to
\TeX's internal state: assignments (including
macro definitions) are typical activities in this
\awp
category. The other major thing happening on this level
is the construction of horizontal, vertical, and
mathematical lists.
\item The visual processor.
In the final level of processing
the visual part of \TeX\ processing is performed. Here
horizontal lists are broken into paragraphs,
vertical lists are broken into pages,
and formulas are built out of math lists.
Also the output to the \n{dvi} file takes place on this level.
The algorithms working here are not accessible to the user,
but they can be influenced by a number of parameters.
\end{enumerate}
%\point The input processor
\section{The input processor}
The input processor of \TeX\ is that part of \TeX\ that
translates whatever characters it gets from the input file
into tokens. The output of this processor is a stream
of tokens: a token list. Most tokens fall into one of two categories:
character tokens and control sequence tokens.
The remaining category is that of the parameter tokens;
these will not be treated in this chapter.
%\spoint Character input
\subsection{Character input}
For simple input text, characters are made into
character tokens. However, \TeX\ can ignore input characters:
a row of spaces in the input is usually equivalent to just one
space. Also, \TeX\ itself can insert tokens that do not correspond
to any character in the input, for instance the space token
at the end of the line, or the \cs{par} token after an empty line.
Not all character tokens signify characters to be typeset.
\altt
Characters fall into sixteen categories \ldash each one
specifying a certain function that a character can have \rdash
of which only two contain the characters that will be
typeset. The other categories contain such characters
as~\n{\char`\{}, \n{\char`\}},
\n\&, and~\n\#. A~character token can be considered
as a pair of numbers: the character code \ldash typically the \ascii\
code \rdash and the category code.
It is possible to change
the category code that is associated with a particular
character code.
When the escape character (by default~\cs{}$\,$) appears in the input,
\TeX's behaviour in forming tokens is more complicated.
Basically,
\TeX\ builds a control sequence by taking a number of characters
from the input and lumping them together into a single token.
The behaviour with which \TeX's input processor
reacts to category codes can be described
as a machine that switches between three internal states:
$N$,~new line; $M$,~middle of line; $S$,~skipping spaces.
These states and the transitions between them are treated
in Chapter~\ref{mouth}.
%\spoint Two-level input processing
\subsection{Two-level input processing}
\TeX's input processor is in fact itself a two-level processor.
Because of limitations of the terminal, the editor, or the operating
\awp
system, the user may not be able to input certain desired characters.
Therefore, \TeX\ provides a mechanism to access
with two superscript characters all of the available character
positions. This may be considered
a separate stage of \TeX\ processing, taking place
prior to the three-state machine mentioned above.
For instance, the sequence \verb>^^+> is replaced by~\n{k} because
the \ascii{} codes of \n k and \n + differ by~64.
Since this replacement takes place before tokens are formed,
writing \verb>\vs^^+ip 5cm> has the same effect as
\verb>\vskip 5cm>. Examples more useful than this exist.
Note that this first stage is a transformation from
characters to characters, without considering category
codes. These come into play only in the second phase
of input processing where characters are converted
to character tokens by coupling the category code
to the character code.
%\point The expansion processor
\section{The expansion processor}
\TeX's expansion processor accepts a stream of tokens
and, if possible,
expands the tokens in this stream one by one
until only unexpandable tokens remain.
Macro expansion is the clearest example of this:
if a control sequence is a macro name, it is replaced
(together possibly with parameter tokens) by
the definition text of the macro.
Input for the expansion processor is provided mainly
by the input processor. The stream of tokens coming
from the first stage of \TeX\ processing is subject
to the expansion process, and the result is a stream
of unexpandable tokens which is fed to the execution processor.
However, the expansion processor comes into play
also when (among others) an \cs{edef} or \cs{write} is processed.
The parameter token list of these commands is
expanded very much as if the lists had been
on the top level, instead of the argument to a command.
%\spoint The process of expansion
\subsection{The process of expansion}
Expanding a token consists of the following steps:
\begin{enumerate}
\item See whether the token is expandable.
\item If the token is unexpandable, pass it to the token
list currently being built, and take on the next token.
\item If the token is expandable, replace it by its expansion.
For macros without parameters, and a few primitive commands
such as \cs{jobname}, this is indeed a simple replacement.
Usually, however, \TeX\ needs to absorb some argument tokens from
the stream in order to be able to form the replacement
of the current token.
For instance, if the token was a macro with parameters,
sufficiently many tokens need to be absorbed to form
the arguments corresponding to these parameters.
\item Go on expanding, starting with the first token of the
expansion.
\end{enumerate}
%
Deciding whether a token is expandable is
a simple decision. Macros and active characters,
conditionals, and a number of primitive \TeX\ commands
\awp
(see the list on page~\pageref{expand:lijst})
are expandable, other tokens are not.
Thus the expansion processor replaces macros by their expansion,
it evaluates conditionals and eliminates any irrelevant parts of
these, but tokens such as \cs{vskip} and character tokens,
including characters such as dollars and braces, are passed untouched.
%\endinput
%\spoint Special cases: \cs{expandafter}, \cs{noexpand}, and \cs{the}
\subsection{Special cases: \cs{expandafter}, \cs{noexpand}, and \cs{the}}
As stated above,
after a token has been expanded, \TeX\ will start expanding
the resulting tokens. At first sight the \cs{expandafter}
command would seem to be an exception to this rule, because
it expands only one step. What actually happens is that
the sequence \begin{disp}\cs{expandafter}\gr{token$_1$}\gr{token$_2$}\end{disp}
is replaced by
\begin{disp}\gr{token$_1$}\gr{\italic expansion of token$_2$}\end{disp}
and this replacement is in fact reexamined by the expansion
processor.
Real exceptions do exist, however. If the
current token is the \cs{noexpand} command, the next
token is considered for the moment to be unexpandable:
it is handled as if it were \cs{relax}, and it is
passed to the token list being built.
For example,
in the macro definition
\begin{verbatim}
\edef\a{\noexpand\b}
\end{verbatim}
the replacement text \verb>\noexpand\b> is expanded at definition
time. The expansion of \cs{noexpand} is the next token, with
a temporary meaning of \cs{relax}. Thus, when the expansion
processor tackles the next token, the~\cs{b}, it will consider
that to be unexpandable, and just pass it to the token list
being built, which is the replacement text of the macro.
Another exception is that the tokens
resulting from \cs{the}\gr{token variable}
are not expanded further if this statement occurs
inside an \cs{edef} macro definition.
%\spoint Braces in the expansion processor
\subsection{Braces in the expansion processor}
Above, it was said that braces are passed as unexpandable
character tokens. In general this is true. For instance,
the \cs{romannumeral} command is handled by the expansion
processor; when confronted with
\begin{verbatim}
\romannumeral1\number\count2 3{4 ...
\end{verbatim}
\TeX\ will expand until the brace is encountered:
if \cs{count2} has the value of zero, the result will be
the roman numeral representation of~\n{103}.
As another example, \begin{verbatim}
\iftrue {\else }\fi
\end{verbatim}
is handled by the expansion processor
completely analogous to
\begin{disp}\cs{iftrue} {\italic a}\cs{else} {\italic b}\cs{fi}\end{disp}
\awp
The result is a character token, independent of its category.
However, in the context of macro expansion
the expansion processor will
recognize braces.
First of all, a balanced pair of braces marks off a group of tokens
to be passed as one argument.
If a macro has an argument \begin{verbatim}
\def\macro#1{ ... }
\end{verbatim}
one can call it with a single token, as in
\begin{verbatim}
\macro 1 \macro \$
\end{verbatim}
or with a group of tokens, surrounded by braces
\begin{verbatim}
\macro {abc} \macro {d{ef}g}
\end{verbatim}
Secondly, when the arguments for a macro with
parameters are read, no expressions with unbalanced braces
are accepted. In
\begin{verbatim}
\def\a#1\stop{ ... }
\end{verbatim}
the argument consists of all
tokens up to the first occurrence of \cs{stop}
that is not in braces: in
\begin{verbatim}
\a bc{d\stop}e\stop
\end{verbatim}
the argument of~\cs{a} is \verb>bc{d\stop}e>.
Only balanced expressions
are accepted here.
%\point The execution processor
\section{The execution processor}
The execution processor builds lists: horizontal, vertical,
and math lists. Corresponding to these lists, it works
in horizontal, vertical, or math mode. Of these three modes
`internal' and `external' variants exist.
In addition to building lists, this part of the \TeX\ processor
also performs mode-independent processing, such as
assignments.
Coming out of the expansion processor is a stream of
unexpandable tokens to be processed by
the execution processor.
\relax From the point of view of the execution processor, this
stream contains two types of tokens:
\begin{itemize}
\item Tokens signalling an assignment (this includes
macro definitions), and
other tokens signalling actions
that are independent of the mode, such
as \cs{show} and \cs{aftergroup}.
\item Tokens that build lists:
characters, boxes, and glue. The way they are handled
depends on the current mode.
\end{itemize}
Some objects can be used in any mode; for instance boxes
can appear in horizontal, vertical, and math lists.
The effect of such an object will of course still depend on the mode.
Other objects are specific for one mode.
For instance, characters (to be more precise:
character tokens of categories 11 and~12),
are intimately connected to horizontal mode:
if the execution processor
is in vertical mode when it encounters a character, it will
switch to horizontal mode.
Not all character tokens signal characters to be typeset:
the execution processor can also encounter math shift
\awp
characters (by default~\n{\char`\$}) and beginning/end of group
characters (by default \n{\char`\{} and~\n{\char`\}}).
Math shift characters let \TeX\ enter or exit
math mode, and braces let it enter or exit a~new level of
grouping.
One control sequence handled by the execution processor
deserves special mention: \cs{relax}.
This control sequence is not expandable, but the execution
is to do nothing. Compare the effect of \cs{relax} in
\begin{verbatim}
\count0=1\relax 2
\end{verbatim}
with that of \cs{empty}
defined by \begin{verbatim}
\def\empty{}
\end{verbatim}
in
\begin{verbatim}
\count0=1\empty 2
\end{verbatim}
In the first case the expansion
process that is forming the number stops at \cs{relax} and
the number {\tt 1} is assigned; in the second case
\cs{empty} expands to nothing, so {\tt 12} is assigned.
%\point The visual processor
\section{The visual processor}
\TeX's output processor encompasses those algorithms that
are outside direct user control: paragraph breaking,
alignment, page breaking, math typesetting, and \n{dvi} file
generation. Various parameters control the operation
of these parts of \TeX.
Some of these algorithms return their results in a form that
can be handled by the execution processor. For instance,
a paragraph that has been broken into lines is added to
the main vertical list as a sequence of horizontal boxes
with intermediate glue and penalties. Also, the page breaking
algorithm stores its result in \cs{box255}, so output
routines can dissect it. On the other hand, a math formula
can not be broken into pieces, and, naturally,
shipping a box to the \n{dvi} file is irreversible.
%\point Examples
\section{Examples}
%\spoint Skipped spaces
\subsection{Skipped spaces}
Skipped spaces provide an illustration of the view that
\TeX's levels of processing accept the completed input
of the previous level. Consider the commands
\begin{verbatim}
\def\a{\penalty200}
\a 0
\end{verbatim}
This is {\italic not\/} equivalent to
\begin{verbatim}
\penalty200 0
\end{verbatim}
\awp
which would place a penalty of \n{200}, and
typeset the digit~\n0. Instead it expands to
\begin{verbatim}
\penalty2000
\end{verbatim}
because the space after \cs{a} is skipped in the
input processor. Later stages of processing then receive
the sequence \begin{verbatim}
\a0
\end{verbatim}
%\spoint Internal quantities and their representations
\subsection{Internal quantities and their representations}
\TeX\ uses various sorts of internal quantities,
such as integers and dimensions. These internal
quantities have an external representation,
which is a string of characters, such as
\n{4711} or~\n{91.44cm}.
Conversions between the internal value and the external
representation take place on two different levels,
depending on what direction the conversion goes.
A~string of characters is converted to an internal
value in assignments such as
\begin{verbatim}
\pageno=12 \baselineskip=13pt
\end{verbatim}
or statements such as
\begin{verbatim}
\vskip 5.71pt
\end{verbatim}
and all of these statements are handled by the execution
processor.
On the other hand, the conversion of the internal
values into a representation as a string of
characters is handled by the expansion processor.
For instance, \begin{verbatim}
\number\pageno \romannumeral\year
\the\baselineskip
\end{verbatim}
are all processed by expansion.
As a final example, suppose \verb>\count2=45>, and
consider the statement
\begin{verbatim}
\count0=1\number\count2 3
\end{verbatim}
The expansion processor tackles \verb>\number\count2>
to give the characters \n{45}, and the space after
the \n 2 does not end the number being assigned:
it only serves as a delimiter
of the number of the \cs{count} register.
In the next stage of processing, the execution processor
will then see the statement
\begin{verbatim}
\count0=1453
\end{verbatim}
and execute this.
%\endinput
%%%% end of input file [bigpic]
%\InputFile:mouth
%%%% this is input file [mouth]
%\tracingmacros=2 \tracingcommands\tracingmacros
%\subject[mouth] Category Codes \nl and Internal States
\endofchapter
\chapter{Category Codes and Internal States}\label{mouth}
When characters are read,
\TeX\ assigns them
category codes. The reading mechanism has three internal
states, and transitions between these states are effected
by category codes of characters in the input.
This chapter describes how \TeX\ reads its input and
how the category codes of characters influence the
reading behaviour. Spaces and line ends are discussed.
\begin{inventory}
\item [\cs{endlinechar}]
The character code of the end-of-line character
appended to input lines.
\IniTeX\ default:~13.
\item [\cs{par}]
Command to close off a paragraph and go into vertical mode.
Is generated by empty lines.
\item [\cs{ignorespaces}]
Command that reads and expands until something is
encountered that is not a \gr{space token}.
\item [\cs{catcode}]
Query or set category codes.
\item [\cs{ifcat}]
Test whether two characters have the same category code.
\item [\cs{\char32}]
Control space.
Insert the same amount of space that a space token would
when \cs{spacefactor}${}=1000$.
\item [\cs{obeylines}]
Macro in plain \TeX\ to make line ends significant.
\item [\cs{obeyspaces}]
Macro in plain \TeX\ to make (most) spaces significant.
\end{inventory}
%\point Introduction
\section{Introduction}
\TeX's input processor scans input lines from a file or terminal, and
makes tokens out of the characters.
The input processor can be viewed as
a simple finite state automaton with three internal states;
depending on the state its scanning behaviour may differ.
This automaton will be treated here both from the point of view of the
internal states and of the category codes governing the
transitions.
%\point Initial processing
\section{Initial processing}
Input from a file (or from the user terminal, but this
will not be mentioned specifically
most of the time) is handled one line at a time.
Here follows a discussion of what exactly is an input line
for \TeX.
Computer systems differ with respect to
\term line! input\par\term line! end\par\term machine independence\par
the exact definition of an input
\mdqon
line. The carriage return/""line feed
\mdqoff
\awp
\message{slash-dash}%
sequence terminating a line is most common,
but some systems use just a line feed, and
some systems with fixed record length (block) storage do not have
a line terminator at all. Therefore \TeX\ has its
own way of terminating an input line.
\begin{enumerate}
\item An input line is read from an input file (minus the
line terminator, if any).
\item Trailing spaces are removed (this is for the systems
with block storage, and it prevents confusion because these
spaces are hard to see in an editor).
\item The \cstoidx endlinechar\par, by default \gram{return}
(code~13) is appended.
If the value of \cs{endlinechar} is negative
\label{append:elc}%
or more than~255 (this was 127 in versions of \TeX\ older
than version~3; see page~\pageref{2vs3} for more differences),
no character is appended.
The effect then is the same as
if the line were to end with a comment character.
\end{enumerate}
Computers may also differ in the character encoding
(the most common schemes are \ascii{} and \ebcdic{}), so \TeX\
converts the characters that are read from the file to its
own character codes. These codes are then used exclusively,
so that \TeX\ will perform the same on any system.
For more on this, see Chapter~\ref{char}.
%\point Category codes
\section{Category codes}
Each of the 256 character codes (0--255) has an
\term category codes\par
associated category code, though not necessarily always the same one.
There are 16 categories, numbered 0--15.
When scanning the input, \TeX\
thus forms character-code--category-code pairs.
The input processor sees only these pairs; from them are formed
character tokens, control sequence tokens, and parameter tokens.
These tokens are then passed to \TeX's expansion and execution
processes.
A~character token is a character-code--category-code
pair that is passed unchanged.
A~control sequence token consists of one or more characters
preceded by an escape character; see below.
Parameter tokens are also explained below.
This is the list of the categories, together with a brief
description. More elaborate explanations follow in this and
later chapters.
\begin{enumerate} \message{set counter}%\SetCounter:item=-1
\setcounter{enumi}{-1}
\item\label{ini:esc} Escape character; this signals the start of a control
sequence. \IniTeX\ makes the backslash \verb-\- (code~92)
an escape character.
\item Beginning of group; such a character causes \TeX\ to enter a new
level of grouping. The plain format makes the open brace \verb-{-
\mdqon
a beginning"-of-group character.
\mdqoff
\item End of group; \TeX\ closes the current level of grouping.
Plain \TeX\ has the closing brace \verb-}- as end-of-group
character.
\item Math shift; this is the opening and closing delimiter for
math formulas. Plain \TeX\ uses the dollar sign~\verb-$-
for this.
\item Alignment tab; the column (row) separator in tables
made with \cs{halign} (\cs{valign}). In plain
\TeX\ this is the ampersand~\verb-&-.
\item\label{ini:eol} End of line; a character that \TeX\ considers
to signal the
end of an input line.
\IniTeX\ assigns this code to the \gram{return}, that is, code~13.
Not coincidentally, 13~is also the value that \IniTeX\
assigns to the \cs{endlinechar} parameter; see above.
\awp
\item Parameter character; this indicates parameters for macros.
In plain \TeX\ this is the hash sign~\verb-#-.
\item Superscript; this precedes superscript expressions
in math mode. It is also used to denote character
codes that cannot
be entered in an input file; see below.
In plain \TeX\ this is the circumflex~\verb-^-.
\item Subscript; this precedes subscript expressions in math mode.
In plain \TeX\ the underscore~\verb-_- is used for this.
\item Ignored; characters of this category are removed
from the input, and have therefore no influence on
further \TeX\ processing. In plain \TeX\ this is
the \gr{null} character, that is, code~0.
\item\label{ini:sp} Space; space characters receive special treatment.
\IniTeX\ assigns this category to the \ascii{} \gr{space}
character, code~32.
\item\label{ini:let} Letter; in \IniTeX\ only the characters \n{a..z}, \n{A..Z}
are in this category. Often, macro packages make
some `secret' character (for instance~\n@) into a letter.
\item\label{ini:other} Other; \IniTeX\ puts everything that is
not in the other categories into this category. Thus
it includes, for instance, digits and punctuation.
\item Active; active characters function as a \TeX\ command,
without being preceded by an escape character.
In plain \TeX\ this is only the tie character~\verb-~-,
which is defined to produce
an unbreakable space; see page~\pageref{tie}.
\item\label{ini:comm} Comment character; from a comment character onwards,
\TeX\ considers the rest of an input line to be
comment and ignores it. In \IniTeX\ the per cent sign \verb-%-
is made a comment character.
\item\label{ini:invalid} Invalid character; this category is for characters that
should not appear in the input. \IniTeX\ assigns the
\ascii\ \gr{delete} character, code~127, to this category.
\end{enumerate}
The user can change the mapping
of character codes to category codes
with the \cstoidx catcode\par\ command (see Chapter~\ref{gramm}
for the explanation of concepts such as~\gr{equals}):
\begin{disp}\cs{catcode}\gram{number}\gr{equals}\gram{number}.\end{disp}
In such a statement, the first number is often given in the form
\begin{disp}\verb>`>\gr{character}\quad or\quad \verb>`\>\gr{character}\end{disp}
both of which denote the character code of the character
(see pages \pageref{char:code} and~\pageref{int:denotation}).
The plain format defines
\csterm active\par
\begin{verbatim}
\chardef\active=13
\end{verbatim}
so that one can write statements such as
\begin{verbatim}
\catcode`\{=\active
\end{verbatim}
The \cs{chardef} command is treated
on pages \pageref{chardef} and~\pageref{num:chardef}.
The \LaTeX\ format has the control sequences
\begin{verbatim}
\def\makeatletter{\catcode`@=11 }
\def\makeatother{\catcode`@=12 }
\end{verbatim}
in order to switch on and off the `secret' character~\n@
(see below).
\awp
The \cs{catcode} command can also be used to query category
codes: in \begin{verbatim}
\count255=\catcode`\{
\end{verbatim}
it yields a number, which can be assigned.
Category codes can be tested by
\begin{disp}\cs{ifcat}\gr{token$_1$}\gr{token$_2$}\end{disp}
\TeX\ expands whatever is after \cs{ifcat} until two
unexpandable tokens are found; these are then compared
with respect to their category codes. Control sequence
tokens are considered to have category code~16,
which makes them all equal to each other, and unequal to
all character tokens.
Conditionals are treated further in Chapter~\ref{if}.
%\point From characters to tokens
\section{From characters to tokens}
The input processor
of \TeX\ scans input lines from a file or from the
user terminal, and converts the characters in the input
to tokens. There are three types of tokens.
\begin{itemize}\item Character tokens: any character that is
passed on its own to \TeX's
further levels of processing with an appropriate
category code attached.
\item Control sequence tokens, of which there are two kinds:
an escape character
\ldash that is,\message{ldash nobreak?}
a character of category~0 \rdash followed
by a string of `letters' is
lumped together into a {\em control word}, which is a single token.
An escape character followed by a single character that is not of
category~11, letter, is made into a
{\em control symbol}\term control! symbol\par.
If the distinction between control word and control symbol is
irrelevant, both are called
{\em control sequences}\term control! sequence\par.
The control symbol that results from an escape character followed
\csterm \char32\par
by a space character is called
{\em control space}\term control! space\par.
\item Parameter tokens: a parameter character
\ldash that is, a character of category~6, by default~\verb=#= \rdash
followed by a digit \n{1..9} is replaced by a parameter token.
Parameter tokens are allowed only in the context of
macros (see Chapter~\ref{macro}).
A macro parameter character followed by another macro parameter
character (not necessarily with the same character code)
is replaced by a single character token.
This token has category~6 (macro parameter), and the character
code of the second parameter character.
The most common instance is of this is
replacing \n{\#\#} by~\n{\#$_6$}, where the subscript
denotes the category code.
\end{itemize}
%\point[input:states] The input processor as a finite state automaton
\section{The input processor as a finite state automaton}
\label{input:states}
\TeX's input processor can be considered to be a finite state
automaton with three internal states,
that is, at any moment in time it is in one of three states,
\term state! internal\par
and after transition to another state there is no memory of the
\awp
previous states.
%\spoint State {\italic N}: new line
\subsection{State {\italic N}: new line}
State {\italic N} is entered at the beginning of each new input line,
and that is the only time \TeX\ is in this state.
In state~{\italic N} all space tokens (that is, characters of category~10)
are ignored; an end-of-line character is converted
into a \cs{par} token.
All other tokens bring \TeX\ into state~{\italic M}.
%\spoint State {\italic S}: skipping spaces
\subsection{State {\italic S}: skipping spaces}
State {\italic S} is entered in any mode after a control word or
control space (but after no other control symbol),
or, when in state~{\italic M}, after a space.
In this state all subsequent spaces or end-of-line characters
in this input line are discarded.
%\spoint State {\italic M}: middle of line
\subsection{State {\italic M}: middle of line}
By far the most common state is~{\italic M}, `middle of line'.
It is entered after characters of categories
1--4, 6--8, and 11--13, and after control symbols
other than control space.
An end-of-line character encountered in this state
results in a space token.
\input figflow \message{left align flow diagram}
\vskip12pt plus 1pt minus 4pt\relax %before spoint skip
\begin{tdisp}%\PopIndentLevel
\leavevmode\relax
%\figmouth
\message{fig mouth missing}
\end{tdisp}
%\point[hathat] Accessing the full character set
\section{Accessing the full character set}
\label{hathat}
Strictly speaking, \TeX's input processor
is not a finite state automaton.
This is because during the scanning of the input line
all trios consisting of two {\sl equal\/} superscript characters
\term \char94\char94\ replacement\par
(category code~7) and a subsequent character
(with character code~$<128$)
are replaced by a single character with a character
code in the range 0--127,
differing by 64 from that of the original character.
This mechanism can be used, for instance, to access positions in a font
corresponding to character codes that cannot
be input, for instance because they are \ascii{} control characters.
The most obvious examples are the \ascii{} \gr{return}
and \gr{delete} characters; the corresponding
positions 13 and 127 in a font are
accessible as \verb>^^M> and~\verb>^^?>.
However, since the category of \verb>^^?> is 15, invalid,
that has to be changed before character 127 can be accessed.
\awp
In \TeX3 this mechanism has been
modified and extended to access 256 characters:
any quadruplet \verb-^^xy- where both \n x and \n y are lowercase
hexadecimal digits \n0--\n9, \n a--\n f,
is replaced by a character in the
range 0--255, namely the character the number of which is
represented hexadecimally as~\n{xy}.
This imposes a slight restriction on the applicability
of the earlier mechanism: if, for instance, \verb>^^a>
is typed to produce character~33, then a following
\n0--\n9, \n{a}--\n{f} will be misunderstood.
While this process makes \TeX's input processor
somewhat more powerful
than a true finite state automaton,
it does not interfere with the rest of
the scanning. Therefore it is conceptually simpler to pretend that
such a replacement of triplets or quadruplets
of characters, starting with~\verb>^^>, is performed in advance.
In actual practice this is not possible,
because an
input line may assign category code~7 to some
character other than the circumflex, thereby
influencing its further processing.
%\point Transitions between internal states
\section{Transitions between internal states}
Let us now discuss the effects on the internal state
of \TeX's input processor when
certain category codes are encountered in the input.
%\spoint 0: escape character
\subsection{0: escape character}
When an escape character is encountered\term character !escape\par,
\TeX\ starts forming a control sequence token.
Three different types of control sequence can result,
depending on the category code of the character that
follows the escape character.
\begin{itemize}\item
If the character following the escape is of category~11,
letter, then \TeX\ combines the escape,
that character and all following
characters of category~11, into a control word.
After that \TeX\
goes into state~{\italic S}, skipping spaces.
\item
With a character of category~10, space,
a control symbol called control space results,
and \TeX\ goes into state~{\italic S}.
\item
With a character of any other category code
a control symbol results, and \TeX\ goes into state~{\italic M},
middle of line.
\end{itemize}
The letters of a control sequence name have to be all on one line;
a control sequence name is not continued on the next line
if the current line ends with a comment sign, or if (by letting
\cs{endlinechar} be outside the range~0--255)
there is no terminating character.
%\spoint 1--4, 7--8, 11--13: non-blank characters
\subsection{1--4, 7--8, 11--13: non-blank characters}
Characters of category codes 1--4, 7--8, and 11--13 are made
into tokens, and \TeX\ goes into state~{\italic M}.
%\spoint 5: end of line
\subsection{5: end of line}
Upon encountering an end-of-line character,
\TeX\ discards the rest of the
line, and starts processing the next line,
in state~{\italic N}. If the current state was~{\italic N},
\awp
that is, if the
line so far contained at most spaces, a~\cs{par} token
is inserted; if the state was~{\italic M}, a~space token is inserted,
and in state~{\italic S} nothing is inserted.
Note that by `end-of-line character' a character with category
code~5 is meant. This is not necessarily the \cs{endlinechar},
nor need it appear at the end of the line.
See below for further remarks on line ends.
%\spoint 6: parameter
\subsection{6: parameter}
Parameter characters \ldash usually~\verb=#= \rdash can be
\term character !parameter\par
followed by either a digit \n{1..9}
in the context of macro definitions
\altt
or by another parameter character.
In the first case a `parameter token' results,
in the second case only a single parameter character
is passed on as a character token for further processing.
In either case \TeX\ goes into state~{\italic M}.
A parameter character can also appear on its own in an
alignment preamble (see Chapter~\ref{align}).
%\spoint 7: superscript
\subsection{7: superscript}
A superscript character is handled like most non-blank
characters, except in the case where it is followed
by a superscript character of the same character code.
The process
that replaces these two characters plus the following character
(possibly two characters in \TeX3) by another character
was described above.
%\spoint 9: ignored character
\subsection{9: ignored character}
Characters of category 9 are ignored; \TeX\ remains in the same state.
%\spoint 10: space
\subsection{10: space}
A token with category code 10 \ldash this is called a \gr{space token},
irrespective of the character code \rdash
is ignored in states {\italic N} and~{\italic S}
(and the state does not change);
in state~{\italic M} \TeX\ goes into state~{\italic S}, inserting
a token that has category~10 and character code~32
(\ascii{} space)\term character !space\par,
that is, the character code of the space token may change
from the character that was actually input.
%\spoint 14: comment
\subsection{14: comment}
A comment character causes \TeX\ to discard
the rest of the line, including the comment character.
In particular, the end-of-line character is not seen,
so even if the comment was encountered in state~{\italic M}, no space
token is inserted.
%\spoint 15: invalid
\subsection{15: invalid}
Invalid characters cause an error message. \TeX\ remains in
the state it was in.
However, in the context of a control symbol an invalid character
is acceptable. Thus \verb>\^^?> does not cause any error messages.
\awp
%\point[cat12] Letters and other characters
\section{Letters and other characters}
\label{cat12}
In most programming languages identifiers can consist
of both letters and digits (and possibly some other
character such as the underscore), but control sequences in \TeX\
are only allowed to be formed out of characters of category~11,
letter. Ordinarily, the digits and punctuation symbols have
category~12, other character.
However, there are contexts where \TeX\ itself
generates a string of characters, all of which have
category code~12, even if that is not their usual
category code.
This happens when the operations
\cs{string},
\cs{number},
\cs{romannumeral},
\cs{jobname},
\cs{fontname},
\cs{meaning},
and \cs{the}
are used to generate a stream of character tokens.
If any of the characters delivered by such a command
is a space character (that is, character code~32),
it receives category code~10, space.
For the extremely rare case where a hexadecimal digit has been
hidden in a control sequence, \TeX\ allows \n A$_{12}$--\n F$_{12}$
to be hexadecimal digits, in addition to the ordinary
\n A$_{11}$--\n F$_{11}$ (here
the subscripts denote the category codes).
For example,
\begin{disp}\verb>\string\end>\quad gives four character tokens\quad
\n{\char92$_{12}$e$_{12}$n$_{12}$d$_{12}$} \end{disp}
Note that
\n{\char92$_{12}$}\term character !escape\par\label{use:escape}
is used in the output only because the
value of \cs{escapechar} is the character code for the
backslash. Another value of \cs{escapechar} leads to another
character in the output of \cs{string}.
The \cs{string} command is treated further in Chapter~\ref{char}.
Spaces can wind up in control sequences:
\begin{disp}\verb>\csname a b\endcsname>\end{disp} gives a control sequence
token in which one of the three characters is a space.
Turning this control sequence token into a string of characters
\begin{disp}\verb>\expandafter\string\csname a b\endcsname>\end{disp}
gives \n{\char92$_{12}$a$_{12}$\char32$_{10}$b$_{12}$}.
As a more practical example, suppose there exists a sequence
of input files \n{file1.tex}, \n{file2.tex}\label{ex:jobnumber},
and we want to
write a macro that finds the number of the input file
that is being processed. One approach would be to write
\begin{verbatim}
\newcount\filenumber \def\getfilenumber file#1.{\filenumber=#1 }
\expandafter\getfilenumber\jobname.
\end{verbatim}
where the letters \n{file} in the parameter text of the
macro (see Section~\ref{param:text}) absorb that part of the
jobname, leaving the number as the sole parameter.
However, this is slightly incorrect: the letters \n{file} resulting
from the \cs{jobname} command have category code~12, instead of
11 for the ones in the definition of \cs{getfilenumber}.
This can be repaired as follows:
\begin{verbatim}
{\escapechar=-1
\expandafter\gdef\expandafter\getfilenumber
\string\file#1.{\filenumber=#1 }
}
\end{verbatim}
\awp
Now the sequence \verb>\string\file> gives the four
letters \n{f$_{12}$i$_{12}$l$_{12}$e$_{12}$};
the \cs{expandafter} commands let this be executed prior to
the macro definition;
the backslash is omitted because we put \verb>\escapechar=-1>.
Confining this value to a group makes it necessary to use~\cs{gdef}.
%\global\def\pppar.{\par}
%\point The \lowercase{\n{\char92par}} token
\section{The \lowercase{\n{\char92par}} token}
\TeX\ inserts a \cstoidx par\par\ token into the input after
\term line !empty\par
encountering a character with category code~5,
end of line, in state~{\italic N}.
It is good to realize when exactly this happens:
since \TeX\ leaves state~{\italic N}
when it encounters any token but a space,
a~line giving a \cs{par} can only contain characters
of category~10. In particular, it cannot end with a comment
character. Quite often this fact is used the other way around:
if an empty line is wanted for the layout of the input
one can put a comment sign on that line.
Two consecutive empty lines generate two \cs{par} tokens.
For all practical purposes this is equivalent to one \cs{par},
because after the first one \TeX\ enters vertical mode, and
in vertical mode a \cs{par} only
exercises the page builder,
and clears the paragraph shape parameters.
A \cs{par} is also inserted into the input when \TeX\ sees a
\gram{vertical command} in unrestricted horizontal mode.
After the \cs{par} has been read and expanded, the
vertical command is examined anew (see Chapters~\ref{hvmode}
and~\ref{par:end}).
The \cs{par} token may also be inserted by the \cs{end}
command that finishes off the run of \TeX; see Chapter~\ref{output}.
It is important to realize that \TeX\ does what it normally does
when encountering an empty line
(which is ending a paragraph)
only because of the default definition of the \cs{par} token.
By redefining \cs{par} the behaviour
caused by empty lines and vertical commands can be changed completely,
and interesting special effects can be achieved.
In order to continue to be able to cause the actions normally
associated with \cs{par}, the synonym \cs{endgraf} is
available in the plain format. See further Chapter~\ref{par:end}.
The \cs{par} token is not allowed to be part of a macro
argument, unless the macro has been declared to be \cs{long}.
A \cs{par} in the argument of a non-\cs{long} macro
prompts \TeX\ to give a `runaway argument' message.
Control sequences that have been \cs{let} to \cs{par}
(such as \cs{endgraf}) are allowed, however.
%\point Spaces
\section{Spaces}
This section treats some of the aspects of
\term token !space\par
space characters and space tokens in the initial processing
stages of \TeX. The topic of spacing in text typesetting
is treated in Chapter~\ref{space}.
%\spoint Skipped spaces
\subsection{Skipped spaces}
From the discussion of the internal states of \TeX's
input processor
it is clear that some spaces in the input never reach the
\awp
output; in fact they never get past the input processor.
These are for instance the spaces at the beginning
of an input line, and the spaces following the one
that lets \TeX\ switch to state~{\italic S}.
On the other hand, line ends can generate spaces (which are not
in the input) that may wind up in the output.
There is a third kind of space: the spaces that get past the
input processor,
or are even generated there, but still do not wind up in the
output. These are the \gram{optional spaces} that the
syntax of \TeX\ allows in various places.
%\spoint Optional spaces
\subsection{Optional spaces}
The syntax of \TeX\ has the concepts of `optional spaces'
\term space! optional \par
and `one optional space':
\begin{disp}\gr{one optional space} $\longrightarrow$
\gr{space token} $|$ \gr{empty}\nl
\gr{optional spaces} $\longrightarrow$
\gr{empty} $|$ \gr{space token}\gr{optional spaces}\end{disp}
In general, \gr{one optional space} is allowed after
numbers and glue specifications, while \gr{optional spaces} are
allowed whenever a space can occur inside a number
(for example, between a minus sign and the digits of the number)
or glue specification (for example, between \n{plus} and \n{1fil}).
Also, the definition of \gr{equals} allows \gr{optional spaces}
before the \n= sign.
Here are some examples of optional spaces.
\begin{itemize}
\item A number can be delimited by \gr{one optional space}.
This prevents accidents (see Chapter~\ref{number}),
and it speeds up processing, as \TeX\ can
detect more easily where the \gram{number} being read ends.
Note, however, that not every `number' is a \gram{number}:
for instance the {\tt 2} in \cs{magstep2} is not a number,
but the single token that is the parameter of the
\cs{magstep} macro. Thus a space or line end after this
is significant. Another example is a parameter number,
for example~\n{\#1}: since at most nine parameters are allowed, scanning
one digit after the parameter character suffices.
\item From the grammar of \TeX\
it follows that the
keywords \n{fill} and \n{filll}
consist of \n{fil} and
separate {\tt l}$\,$s, each of which is a keyword
(see page~\pageref{keywords} for a more elaborate discussion),
and hence can be followed by optional spaces.
Therefore forms such as \hbox{\n{fil L l}} are also valid.
This is a potential source of strange accidents.
In most cases, appending a \cs{relax} token prevents
such mishaps.
\item The primitive command \cstoidx ignorespaces\par\
may come in handy as the final command in a macro definition.
As it gobbles up
optional spaces, it can be used to prevent spaces following the
closing brace of an argument from winding up in the output
inadvertently. For example, in
\begin{verbatim}
\def\item#1{\par\leavevmode
\llap{#1\enspace}\ignorespaces}
\item{a/}one line \item{b/} another line \item{c/}
yet another
\end{verbatim}
the \cs{ignorespaces} prevents spurious
spaces in the second and third item.
An empty line
after \cs{ignorespaces} will still insert a \cs{par}, however.
\end{itemize}
\awp
%\spoint Ignored and obeyed spaces
\subsection{Ignored and obeyed spaces}
After control words spaces are ignored. This is not an
instance of optional spaces, but it is due to the fact that
\TeX\ goes into state~{\italic S}, skipping spaces, after control
words. Similarly an end-of-line character is skipped
after a control word.
Numbers are delimited by only \gr{one optional space},
but still
\begin{disp}\n{a\char92 count0=3\char32\char32b}\quad gives\quad `ab',\end{disp}
because \TeX\ goes into state~{\italic S} after the first
space token. The second space is therefore skipped
in the input processor of \TeX; it never becomes a space token.
Spaces are skipped furthermore when \TeX\ is in state~{\italic N},
newline. When \TeX\ is processing in vertical mode
space tokens (that is, spaces that were not skipped)
are ignored. For example, the space inserted (because of the line end)
after the first box in
\begin{verbatim}
\par
\hbox{a}
\hbox{b}
\end{verbatim}
has no effect.
Both plain \TeX\ and \LaTeX\ define a command \cs{obeyspaces}
\altt
that makes spaces significant: after one space other spaces are no
longer ignored. In both cases the basis is
\altt
\begin{verbatim}
\catcode`\ =13 \def {\space}
\end{verbatim}
However, there is a difference between the two cases:
in plain \TeX\ \begin{verbatim}
\def\space{ }
\end{verbatim}
while in \LaTeX\ \begin{verbatim}
\def\space{\leavevmode{} }
\end{verbatim}
although the macros bear other names there.
The difference between the two macros becomes
apparent in the context of \cs{obeylines}:
each line end is then a \cs{par} command, implying that
each next line is started in vertical mode.
An active space is expanded by the plain macro to a space token,
which is ignored in vertical mode.
The active spaces in \LaTeX\ will immediately switch to horizontal
mode, so that each space is significant.
%\spoint More ignored spaces
\subsection{More ignored spaces}
There are three further places where \TeX\ will ignore space tokens.
\alt
\begin{enumerate}
\item When \TeX\ is looking for
an undelimited macro argument it will accept the
first token (or group) that is not a space. This is treated
in Chapter~\ref{macro}.
\item In math mode space tokens are ignored (see Chapter~\ref{math}).
\item After an alignment tab character spaces are ignored
(see Chapter~\ref{align}).
\end{enumerate}
\awp
%\spoint \gr{space token}
\subsection{\gr{space token}}
Spaces are anomalous in \TeX.
For instance, the \cs{string} operation
assigns category code~12 to all
characters except spaces; they receive category~10.
Also, as was said above, \TeX's input processor converts (when in
state~{\italic M}) all tokens with category code~10 into real spaces:
they get character code~32.
Any character token with category~10 is called
\gram{space token}\term space! token\par.
Space tokens with character
code not equal to 32 are called `funny spaces'
\term space !funny\par.
\begin{example} After giving the character \n Q
the category code of a space character,
and using it in a definition
\begin{verbatim}
\catcode`Q=10 \def\q{aQb}
\end{verbatim}
we get
\begin{verbatim}
\show\q
macro:-> a b
\end{verbatim}
because the input processor
changes the character code of the funny space
in the definition.
\end{example}
Space tokens with character codes other than 32 can be
created using, for instance, \cs{uppercase}.
However, `since the various forms of
space tokens are almost identical in behaviour, there's no
point dwelling on the details'; see~\cite{Knuth:TeXbook}~p.~377.
%\spoint Control space
\subsection{Control space}
The `control space' command \verb-\-\n{\char32}
\cstoidx\char32\par\
contributes the amount of space that a \gr{space token} would
when the \verb=\spacefactor= is~1000.
A~control space
is not treated like a space token, or like a macro
expanding to one (which is how \cs{space} is defined in plain \TeX).
For instance, \TeX\ ignores spaces
at the beginning of an input line, but
control space is a \gr{horizontal command}, so it
makes \TeX\ switch from vertical to horizontal mode
(and insert an indentation box).
See Chapter~\ref{space} for the space factor, and
chapter~\ref{hvmode} for horizontal and vertical modes.
%\spoint `\n{\char32}'
\subsection{`\n{\char32}'}
The explicit symbol `\n{\char32}' for a space
is character~32 in the Computer Modern typewriter typeface.
However, switching to \cs{tt} is not sufficient to get
spaces denoted this way, because spaces will still
receive special treatment in the input processor.
One way to
let spaces be typeset by \n{\char32}
is to set \begin{verbatim}
\catcode`\ =12
\end{verbatim}
\TeX\ will then take a space as the instruction to
typeset character number~32. Moreover, subsequent spaces
are not skipped, but also typeset this way: state~{\italic S}
\awp
is only entered after a character with category code~10.
Similarly, spaces after a control sequence are made
visible by changing the category code of the space character.
%\point More about line ends
\section{More about line ends}
\TeX\ accepts lines from an input file, excluding any line
terminator that may be used\term line! end\par.
Because of this, \TeX's behaviour here is not dependent
on the operating system and the line terminator it uses (\key{CR}-\key{LF},
\key{LF}, or none at all for block storage).
From the input line any trailing spaces are removed.
The reason for this is historic; it has to do with
the block storage mode on \key{IBM} mainframe computers.
For some computer-specific problems with end-of-line
characters, see~\cite{B:ctrl-M}.
A~terminator character is then appended
with a character code of \cs{endlinechar},
unless this parameter has a value that
is negative or more than~255.
Note that this terminator character
need not have category code~5, end of line.
%\spoint Obeylines
\subsection{Obeylines}
Every once in a while it is desirable that the line ends in
\message{Check spurious space obeylines+1}%
\cstoidx obeylines\par\howto Change the meaning of the line end\par
the input correspond to those in the output.
The following piece of code does the trick:
\begin{verbatim}
\catcode`\^^M=13 %
\def^^M{\par}%
\end{verbatim}
The \cs{endlinechar} character is here made active,
and its meaning becomes \cs{par}.
The comment signs prevent \TeX\ from seeing the terminator of the
\alt
lines of this definition, and expanding it since it is active.
However, it takes some care to embed this code in a macro.
The definition
\begin{verbatim}
\def\obeylines{\catcode`\^^M=13 \def^^M{\par}}
\end{verbatim}
will be misunderstood:
\TeX\ will discard everything
after the second \verb>^^M>, because this has category code~5.
Effectively, this line is then
\begin{verbatim}
\def\obeylines{\catcode`\^^M=13 \def
\end{verbatim}
To remedy this,
the definition itself has to be
performed in a context where \verb>^^M> is an active
character:\begin{verbatim}
{\catcode`\^^M=13 %
\gdef\obeylines{\catcode`\^^M=13 \def^^M{\par}}%
}
\end{verbatim}
Empty lines in the input are not taken into account
in this definition: these disappear, because two consecutive \cs{par}
tokens are (in this case) equivalent to one.
A slightly modified definition for the line end as
\begin{verbatim}
\def^^M{\par\leavevmode}
\end{verbatim}
remedies this:
now every line end forces \TeX\ to start a paragraph. For empty
lines this will then be an empty paragraph.
\awp
%\spoint Changing the \cs{\endlinechar}
\subsection{Changing the \cs{endlinechar}}
Occasionally you may want to change the \cs{endlinechar}, or
the \cs{catcode} of the ordinary line terminator \verb.^^M.,
for instance to obtain special effects such as macros where
the argument is terminated by the line end.
See page~\pageref{pick:eol} for a worked-out example.
There are a couple of traps. Consider the following:
\begin{verbatim}
{\catcode`\^^M=12 \endlinechar=`\^^J \catcode`\^^J=5
...
... }
\end{verbatim}
This causes unintended output of both character~13 (\verb-^^M-)
and~10 (\verb-^^J-), caused by the line terminators of the
first and last line.
Terminating the first and last line with a comment works,
but replacing the first line by the two lines
\begin{verbatim}
{\endlinechar=`\^^J \catcode`\^^J=5
\catcode`\^^M=12
\end{verbatim}
is also a solution.
Of course, in many cases it is not necessary to substitute
another end-of-line character; a~much simpler solution
is then to put \begin{verbatim}
\endlinechar=-1
\end{verbatim}
which treats all lines as if they end with a comment.
%\spoint More remarks about the end-of-line character
\subsection{More remarks about the end-of-line character}
The character that \TeX\ appends at the end of an input line
is treated like any other character. Usually one is not aware
of this, as its category code is special, but there are a few
ways to let it be processed in an unusual way.
\begin{example} Terminating an input line with \verb>^^> will
(ordinarily, when \cs{endlinechar} is~13) give `M' in the output,
which is the
\ascii{} character with code~13+64.
\end{example}
\begin{example} If \verb>\^^M> has been defined,
terminating an input line with a backslash will execute this command.
The plain format defines
\begin{verbatim}
\def\^^M{\ }
\end{verbatim}
which makes a `control return' equivalent to a control space.
\end{example}
%\point More about the input processor
\section{More about the input processor}
%\spoint The input processor as a separate process
\subsection{The input processor as a separate process}
\TeX's levels of processing are all working at the
\awp
same time and incrementally, but conceptually they can often be
considered to be separate processes that each accept the
completed output of the previous stage. The juggling with
spaces provides a nice illustration for this.
Consider the definition
\begin{verbatim}
\def\DoAssign{\count42=800}
\end{verbatim}
and the call
\begin{verbatim}
\DoAssign 0
\end{verbatim}
The input processor, the part
of \TeX\ that builds tokens, in scanning this call
skips the space before the zero, so the expansion of this
call is \begin{verbatim}
\count42=8000
\end{verbatim}
It would be incorrect to reason
`\cs{DoAssign} is read, then expanded, the space delimits the
number 800, so 800 is assigned and the zero is printed'.
Note that the same would happen if the zero appeared on the next line.
Another illustration shows that optional spaces appear in a different
stage of processing from that for skipped spaces:
\begin{disp}\verb>\def\c.{\relax}>\nl
\verb>a\c.>{\tt\char32 b}\end{disp}
expands to
\begin{disp}\n{a\cs{relax}\char32 b}\end{disp}
which gives as output\begin{disp} `a b'\end{disp}
because spaces after the \cs{relax} control sequence are only
skipped when the line is first read, not when it is expanded.
The fragment
\begin{disp} \verb-\def\c.{\ignorespaces}-\nl \verb-a\c. b-\end{disp}
on the other hand, expands to
\begin{disp}\n{a\cs{ignorespaces}\char32 b}\end{disp}
Executing the \cs{ignorespaces} command removes the subsequent
space token, so the output is \begin{disp} `ab'.\end{disp}
In both definitions
the period after \cs{c} is a delimiting token; it is used here
to prevent spaces from being skipped.
%\spoint The input processor not as a separate process
\subsection{The input processor not as a separate process}
Considering the tokenizing of \TeX\ to be a separate process
is a convenient view, but sometimes it leads to confusion.
The line \begin{verbatim}
\catcode`\^^M=13{}
\end{verbatim}
\awp
makes the line end active,
and subsequently gives an `undefined control sequence' error
for the line end of this line itself. Execution of the commands
on the line thus influences the scanning process of that
same line.
By contrast, \begin{verbatim}
\catcode`\^^M=13
\end{verbatim}
does not give an error.
The reason for this is that \TeX\ reads the line end while it is still
scanning the number~13; that is, at a time when the assignment
has not been performed yet.
The line end is then converted to the optional space character
delimiting the number to be assigned.
%\spoint Recursive invocation of the input processor
\subsection{Recursive invocation of the input processor}
Above, the activity of replacing a parameter
character plus a digit by a parameter token was described
as something similar to the lumping together of letters
into a control sequence token. Reality is somewhat more
complicated than this. \TeX's token scanning mechanism
is invoked both for input from file and for input from
lists of tokens such as the macro definition. Only in the
first case is the terminology of internal states applicable.
Macro parameter characters are treated the same in both
cases, however. If this were not the case it would
not be possible to write things such as
\begin{verbatim}
\def\a{\def\b{\def\c####1{####1}}}
\end{verbatim}
See page \pageref{nest:def} for an explanation of such
nested definitions.
%\point The \verb@- convention
\section{The \n{@} convention}
Anyone who has ever browsed through either the plain format or
the \LaTeX\ format will have noticed that a lot of control sequences
contain an `at' sign:~\verb-@-. These are control sequences that
are meant to be inaccessible to the ordinary user.
Near the beginning of the format files the instruction
\begin{verbatim}
\catcode`@=11
\end{verbatim}
occurs, making the at sign into a letter,
meaning that it can be used in control sequences. Somewhere near the
end of the format definition the at sign is made `other' again:
\begin{verbatim}
\catcode`@=12
\end{verbatim}
Now why is it that users cannot
call a control sequence with an at sign
directly, although they can call macros that contain lots of those
`at-definitions'? The reason is that the control sequences
containing an \n@ are internalized by \TeX\ at definition time,
after which they are a token, not a string of characters.
Macro expansion then
just inserts such tokens, and at that time the category codes
of the constituent characters do not matter any more.
%%%% end of input file [mouth]
%\InputFile:char
%%%% this is input file [char]
%\subject[char] Characters
\endofchapter
\chapter{Characters}\label{char}
Internally, \TeX\ represents characters by their (integer)
character code. This chapter treats those codes, and the
commands that have access to them.
\begin{inventory}
\item [\cs{char}]
Explicit denotation of a character to be typeset.
\item [\cs{chardef}]
Define a control sequence to be a synonym for
a~character code.
\item [\cs{accent}]
Command to place accent characters.
\item [\cs{if}]
Test equality of character codes.
\item [\cs{ifx}]
Test equality of both character and category codes.
\item [\cs{let}]
Define a control sequence to be a synonym of a token.
\item [\cs{uccode}]
Query or set
the character code that is the uppercase variant of a given code.
\item [\cs{lccode}]
Query or set
the character code that is the lowercase variant of a given code.
\item [\cs{uppercase}]
Convert the \gr{general text} argument to its uppercase form.
\item [\cs{lowercase}]
Convert the \gr{general text} argument to its lowercase form.
\item [\cs{string}]
Convert a token to a string of one or more characters.
\item [\cs{escapechar}]
Number of the character that is to be used
for the escape character
when control sequences are being converted
into character tokens. \IniTeX\ default:~92~(\cs{}).
\end{inventory}
%\point[char:code] Character codes
\section{Character codes}
\label{char:code}
Conceptually it is easiest to think that \TeX\ works with
\term character! codes\par
characters internally, but in fact
\TeX\ works with integers: the `character codes'.
The way characters are encoded in a computer may differ
from system to system.
Therefore \TeX\ uses its own scheme of character codes.
Any character that is read from a file (or from the user terminal)
is converted to a character code according to the
character code table.
A~category code is then assigned based on this (see Chapter~\ref{mouth}).
The character code table is based on the 7-bit \ascii{} table
for numbers under~128 (see Chapter~\ref{table}).
There is an explicit conversion between characters
(better: character tokens)
and character codes using the left quote (grave, back quote)
character~\n{`{}}:
at all places where \TeX\ expects a \gram{number} you
can use the left quote followed by a character
token or
a single-character control sequence.
Thus both \verb.\count`a. and \verb.\count`\a. are synonyms
\awp
for \verb.\count97.. See also Chapter~\ref{number}.
The possibility of a single-character control
sequence is necessary in certain cases such as
\begin{disp}\verb>\catcode`\%=11>\quad or\quad \verb>\def\CommentSign{\char`\%}>\end{disp}
which would be misunderstood if the backslash were left out.
For instance \begin{verbatim}
\catcode`%=11
\end{verbatim}
would consider
the \n{=11} to be a comment.
Single-character
control sequences can be formed from characters with any
category code.
After the conversion to character codes any connection
with external representations has disappeared. Of course,
for most characters the visible output will `equal' the input
(that is, an `\n{a}' causes an~`a').
There are exceptions, however, even among the common symbols.
In the Computer Modern
roman fonts there are no `less than' and `greater than'
\message{Check <>! Dammit!}%
signs, so the input `\verb.<>.' will give `<>' in the output.
%{\MathRMx<>}
In order to make \TeX\ machine independent at the output
side, the character codes are also used in the \n{dvi} file:
opcodes $n=0\ldots127$ denote simply the instruction `take
character $n$ from the current font'. The complete definition
of the opcodes in a \n{dvi} file can be found in~\cite{Knuth:TeXprogram}.
%\point Control sequences for characters
\section{Control sequences for characters}
There are a number of ways in which a control sequence can denote
a character. The \cs{char} command specifies a character to be
typeset; the \cs{let} command introduces
a synonym for a character token, that is,
the combination of character code and category code.
%\point Denoting characters to be typeset: \cs\char
\section{Denoting characters to be typeset: \protect\cs{char}}
Characters can be denoted numerically by, for example,
\verb.\char98.\cstoidx char\par.
This command tells \TeX\ to add character number~98 of the
current font to the horizontal list currently under construction.
Instead of decimal notation, it is often more convenient to
use octal or hexadecimal notation. For octal the single quote is used:
\verb.\char'142.; hexadecimal uses the double quote: \verb.\char"62..
Note that \verb.\char''62. is incorrect; the process that replaces
two quotes by a double quote works at a later stage of processing
(the visual processor) than number scanning (the execution processor).
Because of the explicit conversion to character codes by the
back quote character it is also possible to get a `b' \ldash provided
that you are using a font organized a bit like the \ascii{} table \rdash
with \verb.\char`b. or \verb.\char`\b..
The \cs{char} command looks superficially a bit like
the \verb-^^- substitution mechanism (Chapter~\ref{mouth}).
Both mechanisms access characters without directly denoting them.
However, the \verb-^^- mechanism operates in a very early stage of
processing (in the input processor of \TeX,
but before category code
assignment); the \cs{char} command, on the other hand,
comes in the final stages of processing.
In effect it says `typeset character number
so-and-so'.
\awp
There is a construction to let a control sequence stand
for some character code: the \cstoidx chardef\par\ command.
The syntax of this is \label{chardef}
\begin{disp}\cs{chardef}\gram{control sequence}\gr{equals}\gram{number},
\end{disp}
where the number can be an explicit
representation or a counter value, but it can also be
a character code
obtained using the left quote command (see above;
the full definition of \gr{number} is given in Chapter~\ref{number}).
In the plain format
the latter possibility is used in
definitions such as \begin{verbatim}
\chardef\%=`\%
\end{verbatim}
which could have been given equivalently as
\begin{verbatim}
\chardef\%=37
\end{verbatim}
After this command, the control symbol \verb>\%>
used on its own is a synonym for \verb>\char37>,
that is, the command to typeset character~37
(usually the per cent character).
A control sequence that has been defined with a \cs{chardef}
command can also be used as a \gr{number}.
This fact is used in allocation commands such as
\cs{newbox} (see Chapters~\ref{number} and~\ref{alloc}).
Tokens defined with \cs{mathchardef} can also be used this
way.
%\spoint Implicit character tokens: \cs{let}
\subsection{Implicit character tokens: \protect\cs{let}}
Another construction defining a control sequence
\term character !implicit\par
to stand for (among other things)
a character is~\cs{let}\cstoidx let\par:
\begin{disp}\cs{let}\gr{control sequence}\gr{equals}\gr{token}\end{disp}
with a character token on the right hand side of the (optional)
equals sign. The result is called an implicit character token.
(See page~\pageref{let} for a further discussion of~\cs{let}.)
In the
plain format there are for instance synonyms for
the open and close brace:
\begin{verbatim}
\let\bgroup={ \let\egroup=}
\end{verbatim}
The resulting control sequences are called `implicit braces'
(see Chapter~\ref{group}).
Assigning characters by \cs{let}
is different from defining control sequences by \cs{chardef},
in the sense that \cs{let}
makes the control sequence stand for the combination
of a character code and category code.
As an example
\begin{verbatim}
\catcode`|=2 % make the bar an end of group
\let\b=| % make \b a bar character
{\def\m{...}\b \m
\end{verbatim}
gives an `undefined control sequence \cs{m}'
because the \cs{b} closed the group inside which \cs{m}
was defined. On the other hand,
\begin{verbatim}
\let\b=| % make \b a bar character
\catcode`|=2 % make the bar character end of group
{\def\m{...}\b \m
\end{verbatim}
leaves one group open, and it prints a vertical bar
(or whatever is in position 124 of the current font).
The first of these examples
implies that even when the braces have been redefined
(for instance into active characters for macros that
format C code) the beginning-of-group and end-of-group
functionality is available through the control sequences
\cs{bgroup} and~\cs{egroup}.
Here is
another example to show
that implicit character tokens are hard to distinguish
from real character tokens. After the above sequence
\begin{verbatim}
\catcode`|=2 \let\b=|
\end{verbatim}
the tests \begin{verbatim}
\if\b|
\end{verbatim}
and \begin{verbatim}
\ifcat\b}
\end{verbatim}
are both true.
Yet another example can be found in the plain format:
the commands
\begin{verbatim}
\let\sp=^ \let\sb=_
\end{verbatim}
allow people without an
underscore or circumflex on their keyboard to
make sub- and superscripts in mathematics.
For instance:
\begin{disp}\verb>x\sp2\sb{ij}>\quad gives\quad $x\sp2\sb{ij}$\end{disp}
If a person typing in the format itself does not have
these keys, some further tricks are needed:\label{spsb:truc}
\begin{verbatim}
{\lccode`,=94 \lccode`.=95 \catcode`,=7 \catcode`.=8
\lowercase{\global\let\sp=, \global\let\sb=.}}
\end{verbatim}
will do the job; see below for an explanation of lowercase codes.
The \verb>^^> method as it was in \TeX\ version~2
(see page~\pageref{hathat}) cannot be used here,
as it would require typing two characters that can ordinarily
not be input.
With the extension in \TeX\ version~3 it would also be possible
to write \begin{verbatim}
{\catcode`\,=7
\global\let\sp=,,5e \global\let\sb=,,5f}
\end{verbatim}
denoting the codes 94 and 95 hexadecimally.
Finding out just what a control sequence has been defined to be with
\cs{let} can be done using \cs{meaning}:
the sequence \begin{verbatim}
\let\x=3 \meaning\x
\end{verbatim}
gives
`\n{the character 3}'.\awp
%\point Accents
\section{Accents}
Accents can be placed by the
\gr{horizontal command}~\cstoidx accent\par\term accents\par
\label{character}:
\begin{disp}\cs{accent}\gr{8-bit number}\gr{optional assignments}%
\gr{character}\end{disp}
where \gr{character} is a character of category 11 or~12,
a~\cs{char}\gr{8-bit number} command,
or a~\cs{chardef} token. If none of these
four types of \gr{character} follows, the accent is taken to be a
\cs{char} command itself; this gives an accent `suspended
in mid-air'. Otherwise the accent is placed
on top of the following character.
Font changes between the accent and the character can be effected
by the \gr{optional assignments}.
An unpleasant implication of the fact that an \cs{accent} command
has to be followed by a \gr{character} is that it is not
possible to place an accent on a ligature, or
two accents on top of each other.
In some languages, such as Hindi or Vietnamese,
such double accents do occur.
Positioning accents on top of each other is possible,
however, in math mode.
The width of a character with an accent is the same as that of
the unaccented character. \TeX\ assumes that the
accent as it appears in the font file
is properly positioned for a character that is as high
as the x-height of the font; for characters with other heights
it correspondingly lowers or raises the accent.
No genuine under-accents exist in \TeX. They are
implemented as low placed over-accents. A~way of handling
them more correctly would be to write a macro that
measures the following character, and raises or drops
the accent accordingly.
The cedilla macro, \cs{c}\cstoidx c\par,
in plain \TeX\ does something along these lines. However,
it does not drop the accent for characters with descenders.
The horizontal positioning of an accent is controlled by
\cs{fontdimen1}, slant per point. Kerns are used
for the horizontal movement. Note that, although they
are inserted automatically, these kerns are classified
as {\italic explicit\/} kerns. Therefore they inhibit hyphenation
in the parts of the word before and after the kern.
As an example of kerning for accents,
here follows the dump of a horizontal list.
\message{maybe italic correction for extra line}
\begin{verbatim}
\setbox0=\hbox{\it \`l}
\showbox0
\end{verbatim}
gives\begin{verbatim}
\hbox(9.58334+0.0)x2.55554
.\kern -0.61803 (for accent)
.\hbox(6.94444+0.0)x5.11108, shifted -2.6389
..\tenit ^^R
.\kern -4.49306 (for accent)
.\tenit l
\end{verbatim}
Note that the accent is placed first, so afterwards the italic
correction of the last character is still available.
\awp
%\point Testing characters
\section{Testing characters}
Equality of character codes is tested by \cs{if}:
\begin{disp}\cs{if}\gr{token$_1$}\gr{token$_2$}\end{disp}
Tokens following this conditional are expanded until two
unexpandable tokens are left. The condition is then true
if those tokens are character tokens with the same character
code, regardless of category code.
An unexpandable control
sequence is considered to have character code 256 and
category code~16 (so that it is unequal to anything except
another control sequence), except in the case
where it had been \cs{let} to a non-active character token.
In that case it is considered to have the character code
and category code of that character. This was mentioned above.
The test \cs{ifcat} for category codes was mentioned
in Chapter~\ref{mouth}; the test
\begin{disp}\cs{ifx}\gr{token$_1$}\gr{token$_2$}\end{disp}
can be used to test for category code and character code
simultaneously.
The tokens following this test are not expanded.
However, if they are macros, \TeX\
tests their expansions for equality.
Quantities defined by \cs{chardef} can be tested with
\cs{ifnum}:
\begin{verbatim}
\chardef\a=`x \chardef\b=`y \ifnum\a=\b % is false
\end{verbatim}
based on the fact (see Chapter~\ref{number}) that
\gr{chardef token}s can be used as numbers.
%\point Uppercase and lowercase
\section{Uppercase and lowercase}
%\spoint[uc/lc] Uppercase and lowercase codes
\subsection{Uppercase and lowercase codes}
\label{uc/lc}
To each of the character codes correspond
\term uppercase\par\term lowercase\par
\cstoidx lccode\par\cstoidx uccode\par
an uppercase code and a lowercase code (for still more codes see below).
These can be assigned
by
\begin{Disp}\cs{uccode}\gram{number}\gr{equals}\gram{number}\end{Disp}
and
\begin{Disp}\cs{lccode}\gram{number}\gr{equals}\gram{number}.\end{Disp}
In \IniTeX\ codes \verb-`a..`z-, \verb-`A..`Z- have uppercase code
\label{ini:uclc}
\verb-`A..`Z- and lowercase code \verb-`a..`z-.
All other character codes have both uppercase and lowercase
code zero.
%\spoint[upcase] Uppercase and lowercase commands
\subsection{Uppercase and lowercase commands}
\label{upcase}
The commands \verb-\uppercase{...}- and \verb-\lowercase{...}-
\cstoidx uppercase\par\cstoidx lowercase\par
go through their argument lists, replacing all character
codes of explicit character tokens
by their uppercase and lowercase code respectively
if these are non-zero,
without changing the category codes.
\awp
The argument of \cs{uppercase} and \cs{lowercase}
is a \gr{general text}, which is defined as
\begin{Disp} \gr{general text} $\longrightarrow$ \gr{filler}\lb
\gr{balanced text}\gr{right brace}\end{Disp}
(for the definition of \gr{filler} see Chapter~\ref{gramm})
meaning that the left brace can be implicit, but the closing
right brace must be an explicit character token with category
code~2. \TeX\ performs expansion to find the opening
brace.
Uppercasing and lowercasing are executed in the execution processor;
they are not `macro expansion' activities
like \cs{number} or \cs{string}.
The sequence (attempting to produce~\cs{A})
\begin{verbatim}
\expandafter\csname\uppercase{a}\endcsname
\end{verbatim}
gives an error (\TeX\ inserts an \cs{endcsname} before the
\cs{uppercase} because \cs{uppercase} is unexpandable), but
\begin{verbatim}
\uppercase{\csname a\endcsname}
\end{verbatim}
works.
As an example of the correct use of \cs{uppercase}, here
is a macro that tests if a character is uppercase:
\begin{verbatim}
\def\ifIsUppercase#1{\uppercase{\if#1}#1}
\end{verbatim}
The same test can be
performed by \verb>\ifnum`#1=\uccode`#1>.
Hyphenation of words starting with an uppercase character,
that is, a character not equal to its own \cs{lccode},
is subject to the \cs{uchyph} parameter: if this
is positive, hyphenation of capitalized words is allowed.
See also Chapter~\ref{line:break}.
%\spoint Uppercase and lowercase forms of keywords
\subsection{Uppercase and lowercase forms of keywords}
Each character in \TeX\ keywords, such as \n{pt}, can be
given in uppercase or lowercase form.
For instance, \n{pT}, \n{Pt}, \n{pt}, and~\n{PT} all have
the same meaning. \TeX\ does not use
the \cs{uccode} and \cs{lccode} tables here to
determine the lowercase form. Instead it
converts uppercase characters to lowercase by adding~32
\ldash the \ascii{} difference between uppercase and lowercase
characters \rdash to their character code. This has some implications
for implementations of \TeX\ for non-roman alphabets;
see page 370 of \TeXbook, \cite{Knuth:TeXbook}.
%\spoint Creative use of \cs{uppercase} and \cs{lowercase}
\subsection{Creative use of \cs{uppercase} and \cs{lowercase}}
The fact that \cs{uppercase} and \cs{lowercase} do not change
category codes can sometimes be used to create certain
character-code--category-code combinations that would
otherwise be difficult to produce. See for instance the
explanation of the \cs{newif} macro in Chapter~\ref{if},
and another example on page~\pageref{spsb:truc}.
For a slightly different application, consider the
problem (solved by Rainer Sch\"opf) of,
given a counter \verb-\newcount\mycount-, writing character
number \verb-\mycount- to the terminal.
Here is a solution:
%\begin{verbatim}
%\lccode`a=\mycount \chardef\terminal=16
%\lowercase{\write\terminal{a}}
%\end{verbatim}
\begin{verbatim}
\lccode`a=\mycount \chardef\terminal=16
\end{verbatim}
\awp
\begin{verbatim}
\lowercase{\write\terminal{a}}
\end{verbatim}
The \cs{lowercase} command effectively changes the
argument of the \cs{write} command from~`\n a'
into whatever it should be.
%\point[codename] Codes of a character
\section{Codes of a character}
\label{codename}
Each character code has a number of \gr{codename}s associated
\term codenames\par
with it. These are integers in various ranges that determine
how the character is treated in various contexts, or
how the occurrence of that character changes the workings
of \TeX\ in certain contexts.
The code names are as follows:
\begin{description}\item [\cs{catcode}]
\gr{4-bit number} (0--15); the category to which a character belongs.
This is treated in Chapter~\ref{mouth}.
\item [\cs{mathcode}]
\gr{15-bit number} (0--\verb-"7FFF-) or \verb-"8000-;
determines how a character is treated
in math mode. See Chapter~\ref{mathchar}.
\item [\cs{delcode}]
\gr{27-bit number} (0--\n{\hex7$\,$FFF$\,$FFF});
determines how a character is treated after
\cs{left} or \cs{right} in math mode.
See page~\pageref{delcodes}.
\item [\cs{sfcode}]
integer; determines how spacing is affected after this character.
See Chapter~\ref{space}.
\item [\cs{lccode}, \cs{uccode}]
\gr{8-bit number} (0-255); lowercase and
uppercase codes \rdash these were treated above.
\end{description}
%\point Converting tokens into character strings
\section{Converting tokens into character strings}
The command \cs{string} takes the next token and expands it
\cstoidx string\par
into a string of separate characters. Thus
\begin{verbatim}
\tt\string\control
\end{verbatim}
will give \cs{control} in the
output, and
\begin{verbatim}
\tt\string$
\end{verbatim}
will give~\verb-$-, but, noting that the string
operation comes after the tokenizing,
\begin{verbatim}
\tt\string%
\end{verbatim}
will {\em not\/} give~\verb$%$,
because the comment
sign is removed by \TeX's input processor.
Therefore, this command will `string' the first token on the next line.
The \cs{string} command is executed by the expansion processor, thus
it is expanded unless explicitly inhibited (see Chapter~\ref{expand}).
%\spoint Output of control sequences
\subsection{Output of control sequences}
In the above examples the typewriter font was selected, because
\cstoidx escapechar\par
the Computer Modern roman font does not have a backslash character.
\awp
However,
\TeX\ need not have used the backslash character to display
a control sequence: it uses character number \cs{escapechar}.
This same value is also used when a control sequence is
output with \cs{write}, \cs{message}, or \cs{errmessage},
and it is used in the output of \cs{show}, \cs{showthe} and \cs{meaning}.
If \cs{escapechar} is negative or more than~255,
the escape character is not
output; the default value (set in \IniTeX) is~92, the number
of the backslash character.
For use in a \cs{write} statement the \cs{string} can
in some circumstances be
replaced by \cs{noexpand} (see page~\pageref{expand:write}).
%\spoint Category codes of a \cs{string}
\subsection{Category codes of a \cs{string}}
The characters that are the result of a \cs{string} command have
category code~12, except for any spaces in
a stringed control sequence;
they have category code~10. Since inside a control
sequence there are no category codes,
any spaces resulting from \cs{string} are
of necessity only space {\em characters}, that is,
characters with code~32.
However, \TeX's input processor converts
all space tokens that have a character code other than~32
into character tokens with character code~32,
so the chances are pretty slim that
`funny spaces' wind up in control sequences.
Other commands with the same behaviour with respect to
category codes as \cs{string}, are
\cs{number},
\cs{romannumeral}, \cs{jobname}, \cs{fontname}, \cs{meaning},
and \cs{the}.
%%%% end of input file [char]
%\InputFile:fontfam
%%%% this is input file [fontfam]
%\subject[font] Fonts
\endofchapter
\chapter{Fonts}\label{font}
In text mode \TeX\ takes characters from a `current font'.
\term fonts\par
This chapter describes how fonts are identified to \TeX,
and what attributes a font can have.
\begin{inventory}
\item [\cs{font}]
Declare the identifying control sequence of a font.
\item [\cs{fontname}]
The external name of a font.
\item [\cs{nullfont}]
Name of an empty font that \TeX\ uses in emergencies.
\item [\cs{hyphenchar}]
Number of the hyphen character of a font.
\item [\cs{defaulthyphenchar}]
Value of \cs{hyphenchar} when a font is loaded.
Plain \TeX\ default:~\verb>`\->.
\item [\cs{fontdimen}]
Access various parameters of fonts.
\item [\cs{char47}]
Italic correction.
\item [\cs{noboundary}]
Omit implicit boundary character.
\end{inventory}
%\point Fonts
\section{Fonts}
In \TeX\ terminology a font is the set of characters that
is contained in one external font file.
During processing, \TeX\ decides from
what font a character should be taken. This decision is
taken separately for text mode and math mode.
When \TeX\ is processing ordinary text, characters are taken
from the `current font'.
External font file names are coupled to control sequences
by statements such as
\begin{verbatim}
\font\MyFont=myfont10
\end{verbatim}
which makes \TeX\ load the file \n{myfont10.tfm}.
Switching the current font to the font described in that file
is then done by
\begin{verbatim}
\MyFont
\end{verbatim}
The status of the current font
can be queried: the sequence \begin{verbatim}
\the\font
\end{verbatim}
produces the control sequence for the current font.
Math mode completely ignores the current font. Instead
it looks at the `current family', which can contain
three fonts: one for text style, one for script style,
and one for scriptscript style. This is treated
in Chapter~\ref{mathchar}.
\awp
See \cite{S} for a consistent terminology of fonts and typefaces.
With `virtual fonts' (see~\cite{K:virt}) it is possible that
what looks like one font to \TeX\ resides in more than
one physical font file.
\alt
See further page~\pageref{virtual:fonts}.
%\point Font declaration
\section{Font declaration}
Somewhere during a run of \TeX\ or \IniTeX\
\cstoidx font\par
the coupling between an internal identifying control sequence
and the external file name of a font has to be made.
The syntax of the command for this is
\begin{disp}\cs{font}\gr{control sequence}\gr{equals}%
\gr{file name}\gr{at clause}\end{disp}
where
\begin{disp}\gr{at clause} $\longrightarrow$ \n{at} \gr{dimen}
$|$ \n{scaled} \gr{number} $|$ \gr{optional spaces}\end{disp}
Font declarations are local to a group.
By the \gr{at clause} the user specifies that some
magnified version of the font is wanted. The \gr{at clause} comes
in two forms: if the font is given \n{scaled}~{\italic f\/} \TeX\
multiplies all its font dimensions for that font by~$f/1000$;
if the font
has a design size~{\italic d\/}\n{pt} and
the \gr{at clause} is \n{at}~{\italic p\/}\n{pt}
\TeX\ multiplies all font data by~$p/d$.
The presence of an \gr{at clause} makes no difference for
the external font file (the \n{.tfm} file)
that \TeX\ reads for the font; it just multiplies
the font dimensions by a constant.
After such a font declaration, using the defined control sequence
will set the current font to the font of the
control sequence.
%\spoint Fonts and \n{tfm} files
\subsection{Fonts and \n{tfm} files}
The external file needed for the font is a \n{tfm}
(\TeX\ font metrics) file,
which is taken independent of any \gr{at clause}
in the \cs{font} declaration. If the \n{tfm}
file has been loaded already (for instance by \IniTeX\
when it constructed the format),
an assignment of that font file can be reexecuted
without needing recourse to the \n{tfm} file.
Font design sizes are given in the font metrics files.
The \n{cmr10} font, for instance, has a design size
of 10~point. However, there is not much in the font
that actually has a size of 10~points: the opening and closing
parentheses are two examples, but capital
letters are considerably smaller.
%\spoint Querying the current font and font names
\subsection{Querying the current font and font names}
It was already mentioned above that the control sequence
which set the current font can be retrieved by the
command \verb>\the\font>. This is a special case of
\begin{Disp}\cs{the}\gr{font}\end{Disp} where
\begin{disp}\gr{font} $\longrightarrow$
\cs{font} $|$ \gr{fontdef token} $|$ \gr{family member}\nl
\gr{family member} $\longrightarrow$
\gr{font range}\gr{4-bit number}\nl
\gr{font range} $\longrightarrow$
\cs{textfont} $|$ \cs{scriptfont} $|$ \cs{scriptscriptfont}\end{disp}
\awp
A \gr{fontdef token} is a control sequence defined by \cs{font},
or the predefined control sequence \cs{nullfont}.
The concept of \gr{family member} is only
relevant in math mode.
Also, the
\cstoidx fontname\par
external name of fonts can be retrieved:
\begin{Disp}\cs{fontname}\gr{font}\end{Disp}
gives a sequence of character tokens of category~12
(but space characters get category~10) that spells the font file
name, plus an \gr{at clause} if applicable.
\begin{example} After
\begin{verbatim}
\font\tenroman=cmr10 \tenroman
\end{verbatim}
the calls
\verb>\the\font> and \verb>\the\tenroman> both give \cs{tenroman}.
The call \verb>\fontname\tenroman> gives \n{cmr10}.
\end{example}
%\spoint \cs{nullfont}
\subsection{\cs{nullfont}}
\TeX\ always knows a font that has no characters: the \csidx{nullfont}.
If no font has been specified, or if in math mode a family member
is needed that has not been specified,
\TeX\ will take its characters from the nullfont.
This control sequence qualifies as a \gr{fontdef token}:
it acts like any other control sequence that stands for a font;
it just does not have an associated \n{tfm} file.
%\point Font information
\section{Font information}
During a run of \TeX\ the main information needed about the
\term \n{tfm} files\par
font consists of the dimensions of the characters.
\TeX\ finds these in the font metrics files, which usually have
extension \n{.tfm}. Such files
contain \begin{itemize} \item global information: the \cs{fontdimen}
parameters, and some other information,
\item dimensions and the italic corrections of characters, and
\altt
\item ligature and kerning programs for characters.
\end{itemize}
Also, the design size of a font is specified in the \n{tfm} file;
see above. The definition of the \n{tfm} format can be found
in~\cite{Knuth:TeXprogram}.
%\spoint[font:dims] Font dimensions
\subsection{Font dimensions}
\label{font:dims}
Text fonts need to have at least seven \csidx{fontdimen} parameters
(but \TeX\ will take zero for unspecified parameters);
\term font! dimensions\par
math symbol and math extension fonts have more
(see page~\pageref{fam23:fontdims}).
For text fonts the minimal set of seven comprises the following:
\begin{enumerate} \item the slant per point; this dimension is used
for the proper horizontal positioning of accents;
\awp
\item the interword space: this is used unless the user
specifies an explicit \cs{spaceskip};
see Chapter~\ref{space};
\item interword stretch: the stretch component of the interword
space;
\item interword shrink: the shrink component of
the interword space;
\item the x-height: the value of
the \gr{internal unit} \n{ex}, which is usually about the
height of the lowercase letter~`x';
\item the quad width:
the value of the \gr{internal unit} \n{em}, which is
approximately the width of the capital letter~`M'; and
\item the extra space: the space added to the interword space
at the end of sentences (that is, when \cs{spacefactor}${}\geq2000$)
unless the user specifies an explicit \cs{x\-space\-skip}.
\end{enumerate}
Parameters 1 and~5 are purely information about the font
and there is no point in varying them.
The values of other parameters can be changed in order to
adjust spacing; see Chapter~\ref{space} for examples
of changing parameters 2, 3, 4, and~7.
Font dimensions can be altered in a \gr{font assignment},
which is a \gr{global assignment} (see page~\pageref{global:assign}):
\begin{Disp}\cs{fontdimen}\gr{number}\gr{font}\gr{equals}\gr{dimen}
\end{Disp} See above for the definition of \gr{font}.
%\spoint Kerning
\subsection{Kerning}
Some combinations of characters should be moved closer
\term kerning\par
together than would be the case if their bounding boxes
were to be just abutted. This fine spacing is called kerning,
and a proper kerning is as essential to a font as the
design of the letter shapes.
Consider as an example\message{Kerning!}
\begin{Disp} `Vo' versus the unkerned variant `V\hbox{}o'\end{Disp}
Kerning in \TeX\ is controlled by information in the
\n{tfm} file, and is therefore outside the influence of the
user. The \n{tfm} file can be edited, however (see Chapter~\ref{TeXcomm}).
The \cs{kern} command has (almost) nothing to do with the
phenomenon of kerning; it is explained in Chapter~\ref{glue}.
%\spoint Italic correction
\subsection{Italic correction}
The primitive control symbol \verb-\/- inserts the `italic
\term italic correction\par\cstoidx /\par
correction' of the previous character or ligature.
Such a correction may be necessary owing to the definition
of the `bounding box' of a character. This box always
has vertical sides, and the width of the character as \TeX\
perceives it is the distance between these sides.
However, in order to achieve proper spacing for slanted or
italic typefaces, characters may very well project outside their
bounding boxes. The italic correction is then needed if
such an overhanging character is followed by a
character from a non-slanting typeface.
\awp
Compare for instance\message{Visible italic correction!}
\begin{Disp} `{\italic\TeX} has'
to `{\italic\TeX\/} has',
\end{Disp} where the second version was typed as
\begin{verbatim}
{\italic\TeX\/} has
\end{verbatim}
The size of the italic correction of each character
is determined by font information
in the font metrics file; for the Computer Modern fonts it is
approximately half the `overhang' of the characters;
see~\cite{K:partE}.
Italic correction is not the same as \cs{fontdimen1}, slant
per point. That font dimension is used only for positioning
accents on top of characters.
An italic correction can only be inserted if the previous item
processed
by \TeX\ was a character or ligature. Thus the
following solution for roman text inside an italic passage
does not work:
\begin{verbatim}
{\italic Some text {\/\roman not} emphasized}
\end{verbatim}
The italic correction has no effect here,
because the previous item is glue.
%\spoint Ligatures
\subsection{Ligatures}
Replacement of character sequences by ligatures is controlled
\term ligatures\par
by information in the \n{tfm} file of a font.
Ligatures are formed from \gr{character} commands:
sequences such as \n{fi} are replaced by `fi' in some fonts.
Other ligatures traditionally in use are
between \n{ff}, \n{ffi}, \n{fl}, and \n{ffl};
in some older works \n{ft} and \n{st} can be found,
and similarly to the \n{fl} ligature \n{fk} and \n{fb}
can also occur.
Ligatures in \TeX\ can be formed between explicit character
tokens, \cs{char} commands, and \gr{chardef token}s.
For example,
the sequence \verb-\char`f\char`i- is replaced by the
`fi' ligature, if such a ligature is part of the font.
Unwanted ligatures can be suppressed in a number of ways:
the unwanted ligature `\hbox{halflife}' can
for instance be prevented by
\begin{disp} \verb>half{}life>, \verb>half{l}ife>, \verb>half\/life>,
or \verb>half\hbox{}life>\end{disp}
but the solution using italic correction is not equivalent
to the others.
%\spoint Boundary ligatures
\subsection{Boundary ligatures}
Each word is surrounded by a left and a right
boundary character (\TeX3 only).
This makes phenomena possible
such as the two different sigmas in Greek:
one at the end of a word, and one for every other position.
This can be realized through a ligature with the
boundary character. A~\csidx{noboundary} command immediately
before or after a word suppresses the boundary character
at that place.
In general, the ligature mechanism has become more complicated
with the transition to \TeX\ version~3; see~\cite{K:TeX23}.
%%%% end of input file [fontfam]
%\InputFile:boxes
%%%% this is input file [boxes]
%\tracingmacros=2 \tracingcommands\tracingmacros
%\subject[boxes] Boxes
\endofchapter
\chapter{Boxes}\label{boxes}
The horizontal and vertical boxes of \TeX\ are containers for
\term box\par
pieces of horizontal and vertical lists.
Boxes can be stored in box registers.
This chapter treats box registers and such
aspects of boxes as their dimensions, and the way their components
are placed relative to each other.
\begin{inventory}
\item [\cs{hbox}]
Construct a horizontal box.
\item [\cs{vbox}]
Construct a vertical box with reference point of the last item.
\item [\cs{vtop}]
Construct a vertical box with reference point of the first item.
\item [\cs{vcenter}]
Construct a vertical box vertically centred
on the math axis; this command can only be used in math mode.
\item [\cs{vsplit}]
Split off the top part of a vertical box.
\item [\cs{box}]
Use a box register, emptying it.
\item [\cs{setbox}]
Assign a box to a box register.
\item [\cs{copy}]
Use a box register, but retain the contents.
\item [\cs{ifhbox \cs{ifvbox}}]
\mdqon
Test whether a box register contains a horizontal/""vertical box.
\mdqoff
\item [\cs{ifvoid}]
Test whether a box register is empty.
\item [\cs{newbox}]
Allocate a new box register.
\item [\cs{unhbox \cs{unvbox}}]
Unpack a box register containing a horizontal/vertical box,
adding the contents to the current horizontal/vertical list,
and emptying the register.
\item [\cs{unhcopy \cs{unvcopy}}]
The same as \cs{unhbox}$\,$/$\,$\cs{unvbox},
but do not empty the register.
\item [\cs{ht \cs{dp} \cs{wd}}]
Height/depth/width of the box in a box register.
\item [\cs{boxmaxdepth}]
Maximum allowed depth of boxes.
Plain \TeX\ default:~\cs{maxdimen}.
\item [\cs{splitmaxdepth}]
Maximum allowed depth of boxes generated by \cs{vsplit}.
\item [\cs{badness}]
Badness of the most recently constructed box.
\item [\cs{hfuzz \cs{vfuzz}}]
Excess size that \TeX\ tolerates before it considers
\mdqon
a horizontal/""vertical box overfull.
\mdqoff
\item [\cs{hbadness \cs{vbadness}}]
Amount of tolerance before \TeX\ reports an underfull
\mdqon
or overfull horizontal/""vertical box.
\mdqoff
\item [\cs{overfullrule}]
Width of the rule that is printed to indicate
overfull horizontal boxes.
\item [\cs{hsize}]
Line width used for text typesetting inside a vertical box.
\awp
\item [\cs{vsize}]
Height of the page box.
\item [\cs{lastbox}]
Register containing the last item added to the current list,
if this was a box.
\item [\cs{raise \cs{lower}}]
Adjust vertical positioning of a box in horizontal mode.
\item [\cs{moveleft \cs{moveright}}]
Adjust horizontal positioning of a box in vertical mode.
\item [\cs{everyhbox \cs{everyvbox}}]
\mdqon
Token list inserted at the start of a horizontal/""vertical box.
\mdqoff
\end{inventory}
%\point Boxes
\section{Boxes}
In this chapter we shall look at boxes. Boxes are containers
for pieces of horizontal or vertical lists.
Boxes that are needed more than once can be stored in box registers.
When \TeX\ expects a \gr{box}, any of the following forms
is admissible:
\begin{itemize}
\item \cs{hbox}\gr{box specification}\lb\gr{horizontal material}\rb
\item \cs{vbox}\gr{box specification}\lb\gr{vertical material}\rb
\item \cs{vtop}\gr{box specification}\lb\gr{vertical material}\rb
\item \cs{box}\gr{8-bit number}
\item \cs{copy}\gr{8-bit number}
\item \cs{vsplit}\gr{8-bit number}\n{to}\gr{dimen}
\item \cs{lastbox}
\end{itemize}
A \gr{box specification} is defined as\label{box:spec}
\begin{disp}\gr{box specification} $\longrightarrow$ \gr{filler}
\nl\indent$|$ \n{to} \gr{dimen}\gr{filler}
$|$ \n{spread} \gr{dimen}\gr{filler}
\end{disp}
An \gr{8-bit number} is a number in the range~0--255.
The braces surrounding box material define a group;
they can be explicit characters
of categories 1 and~2 respectively,
or control sequences \cs{let} to such characters;
see also below.
A \gr{box} can in general be used in horizontal, vertical,
and math mode, but see below for the \cs{lastbox}.
The connection between
boxes and modes is explored further in Chapter~\ref{hvmode}.
The box produced by \cs{vcenter} \ldash a command that is allowed only in
math mode \rdash is not a \gr{box}. For instance,
it can not be assigned with \verb=\setbox=; see further
Chapter~\ref{math}.
The \cs{vsplit} operation is treated in Chapter~\ref{page:break}.
%\point Box registers
\section{Box registers}
There are 256 box registers, numbered 0--255.
\term box! registers\par
Either a box register is empty (`void'), or it contains a horizontal
or vertical box.
This section discusses specifically box {\em registers};
the sizes of boxes, and the way material is arranged inside them,
is treated below.
\awp
%\spoint Allocation: \cs{newbox}
\subsection{Allocation: \cs{newbox}}
The plain \TeX\ \csidx{newbox} macro allocates an unused
box register:
\begin{verbatim}
\newbox\MyBox
\end{verbatim}
after which one can say
\begin{verbatim}
\setbox\MyBox=...
\end{verbatim}
or \begin{verbatim}
\box\MyBox
\end{verbatim}
and so on.
Subsequent calls to this macro give subsequent box numbers;
this way macro collections can allocate their own boxes
without fear of collision with other macros.
The number of the box is assigned by \cs{chardef}
(see Chapter~\ref{alloc}).
This implies that \cs{MyBox} is equivalent to,
and can be used as, a~\gr{number}.
The control sequence
\altt
\cs{newbox} is an \cs{outer} macro.
Newly allocated box registers are initially empty.
\subsection{Usage: \cs{setbox}, \cs{box}, \cs{copy}}
A~register is filled by assigning a \gr{box}
\cstoidx setbox\par
to it:
\begin{Disp}\verb>\setbox>\gr{number}\gr{equals}\gr{box}\end{Disp}
For example, the \gr{box} can be explicit
\begin{Disp}\verb>\setbox37=\hbox{...}>\quad or\quad \verb>\setbox37=\vbox{...}>
\end{Disp}
or it can be a box register:
\begin{verbatim}
\setbox37=\box38
\end{verbatim}
Usually, box numbers will have been assigned by a \cs{newbox}
command.
The box in a box register is appended
by the commands \cs{box} and~\cs{copy}
to whatever list \TeX\ is building: the call
\begin{verbatim}
\box38
\end{verbatim}
appends box~38.
To save memory space, box registers become empty by using them:
\TeX\ assumes that after you have inserted a box by
calling \csidx{box}$nn$ in some mode, you do not need the
contents of that register any more and empties it.
In case you {\em do\/} need the contents of
a box register more than once,
you can \csidx{copy} it. Calling \cs{copy}$nn$ is
equivalent to \cs{box}$nn$ in all respects except that
the register is not cleared.
It is possible to unwrap the contents of a box register
by `unboxing' it using the commands \cs{unhbox} and \cs{unvbox},
and their copying versions \cs{unhcopy} and \cs{unvcopy}.
Whereas a box can be used in any mode, the
unboxing operations can only be used in the appropriate mode,
since in effect they contribute a partial
horizontal or vertical list (see also Chapter~\ref{hvmode}).
See below for more information on unboxing registers.
\awp
%\spoint Testing: \cs{ifvoid}, \cs{ifhbox}, \cs{ifvbox}
\subsection{Testing: \cs{ifvoid}, \cs{ifhbox}, \cs{ifvbox}}
Box
registers can be tested for their contents:
\begin{disp}\cs{ifvoid}\gr{number}\end{disp}
is true if the box register is empty.
Note that an empty, or `void',
box register is not the same as a register containing an empty box.
An empty box is still either a horizontal or a vertical box;
a~void register can be used as both.
The test
\begin{disp}\cs{ifhbox}\gr{number}\end{disp}
is true if the box register contains a horizontal box;
\begin{disp}\cs{ifvbox}\gr{number}\end{disp}
is true if the box register contains a vertical box.
Both tests are false for void registers.
%\spoint[lastbox] The \cs{lastbox}
\subsection{The \cs{lastbox}}
\label{lastbox}
When \TeX\ has built a partial list, the last box in this
list is accessible as the \csidx{lastbox}. This behaves
like a box register, so you can remove the last box from the
list by assigning the \cs{lastbox} to some box register.
If the last item on the current list is not a box,
the \cs{lastbox} acts like a void box register.
It is not possible to get hold of the last box
in the case of the main vertical list.
The \cs{lastbox} is then always void.
As an example, the statement \begin{verbatim}
{\setbox0=\lastbox}
\end{verbatim}
removes
the last box from the current list, assigning it to box
register~0. Since this assignment occurs inside a group,
the register is cleared at the end of the group.
At the start of a paragraph this can be used to remove the
indentation box (see Chapter~\ref{par:start}).
Another example of \cs{lastbox} can be found on page~\pageref{varioset}.
Because the \verb-\lastbox- is always empty in external vertical mode,
it is not possible to get hold of boxes that have been
added to the page. However, it is possible to dissect
the page once it is in \cs{box255}, for instance doing
\begin{verbatim}
\vbox{\unvbox255{\setbox0=\lastbox}}
\end{verbatim}
inside the output routine.
If boxes in vertical mode have been shifted by \cs{moveright}
or \cs{moveleft}, or if boxes in horizontal mode have
been raised by \cs{raise} or lowered by \cs{lower},
any information about this
displacement due to such a command is lost when
the \cs{lastbox} is taken from the list.
\awp
%\point Natural dimensions of boxes
\section{Natural dimensions of boxes}
%\spoint Dimensions of created horizontal boxes
\subsection{Dimensions of created horizontal boxes}
Inside an \csidx{hbox} all constituents are lined up next to each other,
\term box! dimensions\par
with their reference points on the baseline of the box,
unless they are moved explicitly in the vertical direction
by \cs{lower} or~\cs{raise}.
The resulting width of the box is the sum of the widths
of the components. Thus the width of
\begin{verbatim}
\hbox{\hskip1cm}
\end{verbatim}
is positive, and the width of
\begin{verbatim}
\hbox{\hskip-1cm}
\end{verbatim}
is negative. By way of example,
\begin{disp}\verb>a\hbox{\kern-1em b}-->\end{disp}
gives as output
\begin{disp}\leavevmode\hphantom{b}a\hbox{\kern-1em b}--\end{disp}
\message{check align input/output}
which shows that a horizontal box can have negative
width.
The height and depth of an \cs{hbox} are the
maximum amount that constituent boxes project above and
below the baseline of the box. They are non-negative when the
box is created.
The commands \cs{lower} and \cs{raise} are the only possibilities
for vertical movement inside an \cs{hbox} (other than
including a \cs{vbox} inside the \cs{hbox}, of course);
a~\gr{vertical command} \ldash such as \cs{vskip} \rdash
is not allowed in a horizontal box, and
\cs{par}, although allowed,
does not do anything inside a horizontal box.
%\spoint Dimensions of created vertical boxes
\subsection{Dimensions of created vertical boxes}
Inside a \csidx{vbox} vertical material is lined up with the
\cstoidx vtop\par
reference points on the vertical line through the reference
point of the box,
unless components are moved explicitly in the horizontal direction
by \csidx{moveleft} or~\csidx{moveright}.
The reference point of a vertical box
is always located at the left boundary of the box.
The width of a vertical box
is then the maximal amount that any material in the
box sticks to the right of the reference point.
Material to the left of the reference point is
not taken into account in the width.
Thus the result of
\begin{disp}\verb>a\vbox{\hbox{\kern-1em b}}-->\end{disp}
is
\begin{disp}\leavevmode\hphantom{b}a\vbox{\hbox{\kern-1em b}}--\end{disp}
This should be contrasted with the above example.
The calculation of height and depth is different
for vertical boxes constructed by \cs{vbox} and \cs{vtop}.
The ground rule is that
\awp
a \cs{vbox} has a reference point that lies on
the baseline of its last component,
and a \cs{vtop} has its reference point on the baseline of the
first component.
In general, the depth (height) of a \cs{vbox} (\cs{vtop})
\alt
can be non-zero if the last (first) item is a box or rule.
The height of a \cs{vbox} is then the sum of the heights and
depths of all components except the last, plus the height
of that last component; the depth of the \cs{vbox} is the
depth of its last component.
The depth of a \cs{vtop}
is the sum of the depth of the first component and the heights
and depths of all subsequent material; its height is the
height of the first component.
However, the actual rules are a bit
more complicated when the first component of a \cs{vtop}
or the last component of a \cs{vbox} is not a box or rule.
If the last component of a \cs{vbox} is a kern or a glue,
the depth of that box is zero; a \cs{vtop}'s
height is zero
unless its first component is a box or rule.
\altt
(Note the asymmetry in these definitions; see below for
an example illustrating this.)
The depth of a \cs{vtop}, then, is equal to the total
height plus depth of all enclosed material minus
the height of the \cs{vtop}.
There is a limit on the depth of vertical boxes:
if the depth of a \cs{vbox} or \cs{vtop}
calculated by the above rules would exceed
\cstoidx boxmaxdepth\par,
the reference point of the box
is moved down by the excess amount.
More precisely, the excess depth is added to the
natural height of the box. If the box had a \n{to} or
\n{spread} specification, any glue is set anew to take
the new height into account.
Ordinarily,
\cs{boxmaxdepth} is set to the maximum dimension
possible in \TeX. It is for instance reduced during some of
the calculations in the plain \TeX\ output routine;
see Chapter~\ref{output}.
%\spoint Examples
\subsection{Examples}
Horizontal boxes are relatively straightforward. Their width is the
distance between the `beginning' and the `end' of the
box,
and consequently the width is not necessarily positive.
With
\begin{verbatim}
\setbox0=\hbox{aa} \setbox1=\hbox{\copy0 \hskip-\wd0}
\end{verbatim}
the \cs{box1} has width zero;
\begin{Disp} \verb-/\box1/-\quad gives\quad
`{\setbox0=\hbox{aa}\setbox1=\hbox{\copy0 \hskip-\wd0}/\box1/}\kern.75em'
\end{Disp}
The height and depth of a horizontal box cannot be negative: in
\begin{verbatim}
\setbox0=\hbox{\vrule height 5pt depth 5pt}
\setbox1=\hbox{\raise 10pt \box0}
\end{verbatim}
the \cs{box1} has depth \n{0pt} and height~\n{15pt}
Vertical boxes are more troublesome than horizontal boxes.
Let us first treat their width.
After \begin{verbatim}
\setbox0=\hbox{\hskip 10pt}
\end{verbatim}
the box in the
\cs{box0} register has a width of \n{10pt}. Defining
\begin{verbatim}
\setbox1=\vbox{\moveleft 5pt \copy0}
\end{verbatim}
\awp
the \cs{box1} will have width \n{5pt}; material to the
left of the reference point is not accounted for in the
width of a vertical box. With
\begin{verbatim}
\setbox2=\vbox{\moveright 5pt \copy0}
\end{verbatim}
the \cs{box2} will have width \n{15pt}.
The depth of a \cs{vbox} is the depth of the last item if
that is a box, so
\begin{verbatim}
\vbox{\vskip 5pt \hbox{\vrule height 5pt depth 5pt}}
\end{verbatim}
has height \n{10pt} and depth \n{5pt},
and \begin{verbatim}
\vbox{\vskip -5pt \hbox{\vrule height 5pt depth 5pt}}
\end{verbatim}
has height \n{0pt} and depth~\n{5pt}.
With a glue or kern as the last item in the box, the resulting depth
is zero, so
\begin{verbatim}
\vbox{\hbox{\vrule height 5pt depth 5pt}\vskip 5pt}
\end{verbatim}
has height \n{15pt} and depth~\n{0pt};
\begin{verbatim}
\vbox{\hbox{\vrule height 5pt depth 5pt}\vskip -5pt}
\end{verbatim}
has height \n{5pt} and depth~\n{0pt}.
The height of a \cs{vtop} behaves (almost) the same with respect to
the first item of the box, as the depth of a \cs{vbox} does
with respect to the last item. Repeating the above examples with
a \cs{vtop} gives the following:
\begin{verbatim}
\vtop{\vskip 5pt \hbox{\vrule height 5pt depth 5pt}}
\end{verbatim}
has height \n{0pt} and depth \n{15pt},
and \begin{verbatim}
\vtop{\vskip -5pt \hbox{\vrule height 5pt depth 5pt}}
\end{verbatim}
has height \n{0pt} and depth~\n{5pt};
\begin{verbatim}
\vtop{\hbox{\vrule height 5pt depth 5pt} \vskip 5pt}
\end{verbatim}
has height \n{5pt} and depth~\n{10pt}, and
\begin{verbatim}
\vtop{\hbox{\vrule height 5pt depth 5pt} \vskip -5pt}
\end{verbatim}
has height \n{5pt} and depth~\n{0pt}.
%\point More about box dimensions
\section{More about box dimensions}
%\spoint Predetermined dimensions
\subsection{Predetermined dimensions}
The size of a box can be specified in advance
with a \gr{box specification}; see above for the syntax.
Any glue
in the box is then set in order to reach the required size.
Prescribing the size of the box is done by
\begin{disp}\cs{hbox} \n{to} \gr{dimen} \n{\lb...\rb},
\cs{vbox} \n{to} \gr{dimen} \n{\lb...\rb}\end{disp}
\awp
If stretchable or shrinkable glue is present in the box,
it is stretched or shrunk in order to give the box the
specified size. Associated with this glue setting is a badness value
(see Chapter~\ref{glue}). If no stretch or shrink \ldash whichever
is necessary \rdash is present, the resulting box will be underfull
or overfull respectively. Error reporting for over/underfull
boxes is treated below.
Another command to let a box have a size other than
the natural size is
\begin{disp}\cs{hbox} \n{spread} \gr{dimen} \n{\lb...\rb},
\cs{vbox} \n{spread} \gr{dimen} \n{\lb...\rb}\end{disp}
which tells \TeX\ to set the glue in such a way that
the size of the box is a specified amount more than the
natural size.
Box specifications for \cs{vtop} vertical boxes are
somewhat difficult to interpret. \TeX\ constructs a \cs{vtop}
by first making a \cs{vbox}, including
glue settings induced by a \gr{box specification};
then it computes the height and depth by the above rules.
Glue setting is described in Chapter~\ref{glue}.
%\spoint Changes to box dimensions
\subsection{Changes to box dimensions}
The dimensions of a box register are accessible by the
commands \csidx{ht}, \csidx{dp}, and~\csidx{wd};
for instance \cs{dp13} gives the depth of box~13.
However, not only can boxes be measured this way;
by assigning values to these
dimensions \TeX\ can even be fooled into thinking that
a box has a size different from its actual.
However, changing the dimensions of a box does not change
anything about the contents; in particular it does not
change the way the glue is set.
Various formats use this in `smash' macros: the macro defined by
\cstoidx smash\par
\begin{verbatim}
\def\smash#1{{\setbox0=\hbox{#1}\dp0=0pt \ht0=0pt \box0\relax}}
\end{verbatim}
places its argument but annihilates its height and depth;
\altt
that is, the output does show the whole box, but further calculations
by \TeX\ act as if the height and depth were zero.
Box dimensions can be changed only by setting them.
They are \gr{box dimen}s, which can only be set
in a \gr{box size assignment}, and not, for instance
changed with \cs{advance}.
Note that a \gr{box size assignment} is a \gr{global assignment}:
its effect transcends any groups in which it occurs
(see Chapter~\ref{group}).
Thus the output of \begin{verbatim}
\setbox0=\hbox{---} {\wd0=0pt} a\box0b
\end{verbatim}
is `{\setbox0=\hbox{---}{\wd0=0pt}a\box0b}\kern.5em'.
The limits that hold on the dimensions with which a
box can be created (see above) do not hold for explicit changes to the
\mdqon
size of a box: the assignment \cs{dp0=}""\n{-2pt} for a
\mdqoff
horizontal box is perfectly admissible.
%\spoint Moving boxes around
\subsection{Moving boxes around}
In a horizontal box all constituent elements are lined up
\cstoidx raise\par\cstoidx lower\par
with their reference points at the same height as the
reference point of the box. Any box inside a horizontal
box can be lifted or dropped using the macros
\cs{raise} and~\cs{lower}.
\awp
Similarly, in a vertical box all constituent elements
are lined up with their reference points underneath one another,
in line with the reference point of the box.
Boxes can now be moved sideways by the macros
\csidx{moveleft} and~\csidx{moveright}.
Only boxes can be shifted thus; these operations cannot
be applied to, for instance, characters or rules.
%\spoint Box dimensions and box placement
\subsection{Box dimensions and box placement}
\TeX\ places the components of horizontal and
vertical lists by maintaining a reference line and a
current position on that line. For horizontal lists
the reference line is the baseline of the surrounding
\cs{hbox}; for vertical lists it is the vertical line
through the reference point of the surrounding \cs{vbox}.
In horizontal mode a component is placed as follows.
The current position coincides initially
with the reference point of the surrounding box. After that,
the following actions are carried out.
\begin{enumerate} \item If the component has been shifted by
\cs{raise} or \cs{lower}, shift the current
position correspondingly.
\item If the component is a horizontal box, use
this algorithm recursively for its contents;
if it is a vertical box, go up by the height of this box,
putting a new current position for the enclosed vertical list there,
and place its components using the algorithm for vertical
lists below.
\item Move the current position (on the reference line)
to the right by the width of the component.
\end{enumerate}
For the list in a vertical box \TeX's current position is
initially at the upper left corner of that box, as explained above,
and the reference line is the vertical line through that point;
it also runs through the reference point of the box.
Enclosed components are then placed as follows.
\begin{enumerate} \item If a component has been shifted using
\cs{moveleft} or \cs{moveright}, shift the current position
accordingly.
\item Put the component with its upper left corner at the
current position.
\item If the component is a vertical box, use this algorithm
recursively for its contents; if it is a horizontal box,
its reference point can be found below the current position
by the height of the box. Put the current position for that
box there, and use the above algorithm for horizontal lists.
\item Go down by the height plus depth of the box
(that is, starting at the upper left corner of the box)
on the reference line,
and continue processing vertically.
\end{enumerate}
Note that the above processes do not describe the construction
of boxes. That would (for instance)
involve for vertical boxes the insertion
of baselineskip glue. Rather, it describes the way the components
of a finished box are arranged in the output.
%\spoint Boxes and negative glue
\subsection{Boxes and negative glue}
Sometimes it is useful to have boxes overlapping instead of
\awp
line up. An easy way to do this is to use negative glue.
In horizontal mode
\begin{verbatim}
{\dimen0=\wd8 \box8 \kern-\dimen0}
\end{verbatim}
places box 8 without moving the current location.
More versatile are the macros \csidx{llap} and \csidx{rlap}\label{rlap},
defined as \begin{verbatim}
\def\llap#1{\hbox to 0pt{\hss #1}}
\end{verbatim}
and \begin{verbatim}
\def\rlap#1{\hbox to 0pt{#1\hss}}
\end{verbatim}
that allow material to protrude left or right from the
current location.
The \cs{hss} glue is equivalent to \verb>\hskip 0pt plus 1fil minus 1fil>,
which absorbs any positive or negative width
of the argument of \cs{llap} or \cs{rlap}.
\begin{example} The sequence \begin{verbatim}
\llap{\hbox to 10pt{a\hfil}}
\end{verbatim}
is effectively the same as
\begin{verbatim}
\hbox{\hskip-10pt \hbox to 10pt{a\hfil}}
\end{verbatim}
which has a total width of~\n{0pt}.
\end{example}
%\point[over/underfull] Overfull and underfull boxes
\section{Overfull and underfull boxes}
\label{over/underfull}
If a box has a size specification \TeX\ will
\term box !overfull\par\term box !underfull\par
stretch or shrink glue in the box. For glue with
only finite stretch or shrink components the {\em badness\/}
(see Chapter~\ref{line:break}) of stretching or shrinking
is computed.
In \TeX\ version~3 the badness
\cstoidx badness\par\term \TeX\ version 3\par
of the box most recently
constructed is available for inspection
by the user through the \cs{badness} parameter. Values for
badness range 0--$10\,000$, but if the box is overfull
it is~$1\,000\,000$.
When \TeX\ considers the badness too large,
it gives a diagnostic message. Let us first consider error reporting
for horizontal boxes.
Horizontal boxes of which the glue has to stretch are never reported if
\cstoidx hbadness\par\cstoidx vbadness\par
\cs{hbadness}${}\geq10\,000$; otherwise \TeX\ reports them
as `underfull' if their badness is more than \cs{hbadness}.
Glue shrinking can lead to `overfull' boxes: a box is called
\cstoidx hfuzz\par\cstoidx vfuzz\par
overfull if the available shrink is less than the shrink
necessary to meet the box specification. An overfull box
is only reported if the difference in shrink is more than
\cs{hfuzz}, or if \cs{hbadness}${}<100$ (and it turns out that
using all available shrinkability has badness~$100$).
\begin{example} Setting \verb>\hfuzz=1pt> will let \TeX\ ignore
boxes that can not shrink enough if they lack less than~\n{1pt}.
In \begin{verbatim}
\hbox to 1pt{\hskip3pt minus .5pt}
\end{verbatim}
\awp
\begin{verbatim}
\hbox to 1pt{\hskip3pt minus 1.5pt}
\end{verbatim}
only the first box will give an error message:
it is \n{1.5pt} too big, whereas the second lacks
\n{.5pt} which is less than \cs{hfuzz}.
\end{example}
Also, boxes that shrink but that are not overfull can be reported:
if a box is `tight', that is, if it uses at least half its
shrinkability, \TeX\ reports this fact if the
computed badness (which is between 13 and~100) is more than
\cs{hbadness}.
For horizontal and vertical boxes this error reporting is almost
\cstoidx overfullrule\par
the same, with parameters \cs{vbadness} and \cs{vfuzz}.
The difference is that for horizontal overfull boxes
\TeX\ will draw a rule to the right of the box that has the
same height as the box, and width \cs{overfullrule}.
No overfull rule ensues if
the \cs{tabskip} glue in an \cs{halign} cannot be
shrunk enough.
%\point Opening and closing boxes
\section{Opening and closing boxes}
The opening and closing braces of a box can be either explicit,
that is, character tokens of category 1 and~2, or implicit,
a control sequence \verb=\let= to such a character.
After the opening brace
the \csidx{everyhbox} or \csidx{everyvbox}
tokens are inserted.
If this box appeared in a \csidx{setbox} assignment
any \csidx{afterassignment}
token is inserted even before the `everybox' tokens.
\begin{example} \label{every:box:assign}\begin{verbatim}
\everyhbox{b}
\afterassignment a
\setbox0=\hbox{c}
\showbox0
\end{verbatim}
gives
\begin{verbatim}
> \box0=
\hbox(6.94444+0.0)x15.27782
.\tenrm a
.\tenrm b
.\kern0.27779
.\tenrm c
\end{verbatim}
\end{example}
Implicit braces can be used to let a box be opened or closed
by a macro, for example:
\begin{verbatim}
\def\openbox#1{\setbox#1=\hbox\bgroup}
\def\closebox#1{\egroup\DoSomethingWithBox#1}
\openbox0 ... \closebox0
\end{verbatim}
This mechanism can be used to scoop up paragraphs:
\begin{verbatim}
\everypar{\setbox\parbox=
\vbox\bgroup
\everypar{}
\def\par{\egroup\UseBox\parbox}}
\end{verbatim}
Here the \cs{everypar} opens the box and lets the text be
set in the box: starting for instance
\begin{verbatim}
Begin a text ...
\end{verbatim}
gives the equivalent of
\begin{verbatim}
\setbox\parbox=\vbox{Begin a text ...
\end{verbatim}
Inside the box \cs{par} has been redefined, so
\begin{verbatim}
... a text ends.\par
\end{verbatim}
is equivalent to
\begin{verbatim}
... a text ends.}\Usebox\parbox
\end{verbatim}
In this example, the \cs{UseBox} command can only treat the
box as a whole; if the elements of the box should somehow
be treated separately another approach is necessary.
In
\begin{verbatim}
\everypar{\setbox\parbox=
\vbox\bgroup\everypar{}%
\def\par{\endgraf\HandleLines
\egroup\box\parbox}}
\def\HandleLines{ ... \lastbox ... }
\end{verbatim}
the macro \cs{HandleLines} can have access to successive
elements from the vertical list of the paragraph.
See also the example on page~\pageref{varioset}.
%\point Unboxing
\section{Unboxing}
Boxes can be unwrapped by the commands \csidx{unhbox} and
\term box! unboxing\par
\csidx{unvbox}, and by their copying versions
\csidx{unhcopy} and \csidx{unvcopy}.
These are horizontal and vertical commands
(see Chapter~\ref{hvmode}), considering that in effect
they contribute a partial horizontal or vertical list.
It is not possible to \cs{unhbox} a register
containing a \cs{vbox} or vice versa,
but a void box register can both be \cs{unhbox}ed and
\cs{unvbox}ed.
Unboxing takes the contents of a box in a box register and appends
them to the surrounding list; any glue can then
be set anew. Thus
\begin{verbatim}
\setbox0=\hbox to 1cm{\hfil} \hbox to 2cm{\unhbox0}
\end{verbatim}
is completely equivalent to
\begin{verbatim}
\hbox to 2cm{\hfil}
\end{verbatim}
and not to
\begin{verbatim}
\hbox to 2cm{\kern1cm}
\end{verbatim}
\awp
The intrinsically horizontal nature of \cs{unhbox} is
\cstoidx leavevmode\par
used to define
\begin{verbatim}
\def\leavevmode{\unhbox\voidb@x}
\end{verbatim}
This command switches from vertical mode to horizontal without
adding anything to the horizontal list.
However, the subsequent \cs{indent} caused by this transition
adds an indentation box.
In horizontal mode the \cs{leavevmode} command has no effect.
Note that here it is not necessary to use \cs{unhcopy},
because the register is empty anyhow.
Beware of the following subtlety: unboxing in vertical
mode does not add interline glue between the box contents and
any preceding item.
Also, the value of \cs{prevdepth} is not
changed, so glue between the box contents and any following
item will occur only if there was something preceding the box;
interline glue will be based on the depth of that preceding item.
Similarly, unboxing in horizontal mode does not influence
the \cs{spacefactor}.
%\point Text in boxes
\section{Text in boxes}
Both horizontal and vertical boxes can contain text. However,
\term boxes !text in\par
the way text is treated differs.
In horizontal boxes
the text is placed in one straight line, and the width of
the box is in principle the natural width of the text
(and other items) contained in it. No \gram{vertical command}s
are allowed inside a horizontal box, and \cs{par} does
nothing in this case.
For vertical boxes the situation is radically different.
As soon as a character, or any other \gram{horizontal command}
(see page~\pageref{h:com:list}),
is encountered in a vertical box, \TeX\ starts building a paragraph
in unrestricted horizontal mode, that is, just as if the paragraph
were directly part of the page.
At the occurrence of a \gram{vertical command}
(see page~\pageref{v:com:list}), or at the end
of the box, the paragraph is broken into lines using the
current values of parameters such as~\cs{hsize}.
Thus \begin{verbatim}
\hbox to 3cm{\vbox{some reasonably long text}}
\end{verbatim}
will {\sl not\/} give a paragraph of width 3 centimetres
(it gives an overfull horizontal box if \cs{hsize}${}>{}$\n{3cm}).
However,
\begin{verbatim}
\vbox{\hsize=3cm some reasonably long text}
\end{verbatim}
will be 3 centimetres wide.
A paragraph of text inside a vertical box is broken into
lines, which are packed in horizontal boxes.
These boxes are then stacked
in internal vertical mode, possibly with
\cs{baselineskip} and \cs{lineskip} separating them
(this is treated in Chapter~\ref{baseline}).
This process is also used for text on the page; the boxes
are then stacked in outer vertical mode.
If the internal vertical list is empty, no \cs{parskip}
glue is added at the start of a paragraph.
Because text in a horizontal box is not
\label{wide:vbox}%
broken into lines, there is a further
difference between text in restricted and unrestricted
\awp
horizontal mode. In restricted horizontal mode no
discretionary nodes and whatsit items changing the
value of the current language are inserted.
This may give problems if the text is subsequently
unboxed to form part of a paragraph.
See Chapter~\ref{line:break} for an explanation of these
items, and \cite{Downs} for a way around this problem.
%\point Assorted remarks
\section{Assorted remarks}
%\spoint Forgetting the \cs{box}
\subsection{Forgetting the \cs{box}}
After \verb.\newcount\foo., one can use \cs{foo} on its own
to get the \cs{foo} counter.
For boxes, however, one has to use \verb.\box\foo. to get
the \cs{foo} box.
The reason for this is that there exists
no separate \cs{boxdef} command, so \cs{chardef} is
used (see Chapter~\ref{alloc}).
\begin{example}
Suppose \verb.\newbox\foo. allocates box register~25; then
typing \cs{foo} is equivalent to typing
\verb.\char25..
\end{example}
%\spoint Special-purpose boxes
\subsection{Special-purpose boxes}
Some box registers
have a special
purpose:
\begin{itemize}
\item \cs{box255} is by used \TeX\ internally
to give the page to the output routine.
\item \cs{voidb@x} is the number of
a box register allocated in
\n{plain.tex}; it is supposed to be empty always.
It is used in the macro \cs{leavevmode} and others.
\item when a new \cs{insert} is created with the plain \TeX\
\cs{newinsert} macro, a \cs{count},
\cs{dimen}, \cs{skip}, and \cs{box} all with the same number
are reserved for that insert.
The numbers for these registers count down from~254.
\end{itemize}
%\spoint The height of a vertical box in horizontal mode
\subsection{The height of a vertical box in horizontal mode}
In horizontal mode a vertical box is placed with its
reference point aligned vertically with the reference
point of the surrounding box.
\TeX\ then traverses its contents starting at the left
upper corner; that is, the point that lies above the reference
point by a distance of the height of the box.
Changing the height of the box implies then that the
contents of the box are placed at a different height.
Consider as an example
\begin{verbatim}
\hbox{a\setbox0=\vbox{\hbox{b}}\box0 c}
\end{verbatim}
which gives
\begin{disp}\leavevmode\hbox{a\setbox0=\vbox{\hbox{b}}\box0 c}\end{disp}
and
\begin{verbatim}
\hbox{a\setbox0=\vbox{\hbox{b}}\ht0=0cm \box0 c}
\end{verbatim}
\awp
which gives
\begin{disp}\leavevmode\hbox{a\setbox0=\vbox{\hbox{b}}\ht0=0cm \box0 c}\end{disp}
By contrast, changing the width of a box placed in vertical
mode has no effect on its placement.
%\spoint More subtleties with vertical boxes
\subsection{More subtleties with vertical boxes}
Since there are two kinds of vertical boxes, the \cs{vbox} and
the \cs{vtop}, using these two kinds nested may lead to
confusing results. For instance, \begin{verbatim}
\vtop{\vbox{...}}
\end{verbatim}
is completely equivalent to just \begin{verbatim}
\vbox{...}
\end{verbatim}
It was stated above that
the depth of a \cs{vbox} is zero if the last item
is a kern or glue, and the height of a \cs{vtop} is
zero unless the first item in it is a box.
The above examples used a kern for that first or last item,
but if, in the case of a \cs{vtop},
this item is not a glue or kern, one is apt to
overlook the effect that it has on the surrounding box.
For instance,
\begin{verbatim}
\vtop{\write16{...}...}
\end{verbatim}
has zero height,
because the write instruction
is packed into a `whatsit' item that is placed on the current,
that is, the vertical, list.
The remedy here is
\begin{verbatim}
\vtop{\leavevmode\write16{...}...}
\end{verbatim}
which puts the whatsit in the beginning of the paragraph,
instead of above it.
Placement of items in a vertical list is sometimes
a bit tricky. There is for instance a difference between
how vertical and horizontal boxes are treated in a
vertical list. Consider the following examples.
After \cs{offinterlineskip} the first example\begin{verbatim}
\vbox{\hbox{a}
\setbox0=\vbox{\hbox{(}}
\ht0=0pt \dp0=0pt \box0
\hbox{ b}}
\end{verbatim}
gives \begin{disp}\offinterlineskip\leavevmode\vbox{\hbox{a}
\setbox0=\vbox{\hbox{(}}
\ht0=0pt \dp0=0pt \box0
\hbox{ b}}
\end{disp}
while a slight variant\begin{verbatim}
\vbox{\hbox{a}
\setbox0=\hbox{(}
\ht0=0pt \dp0=0pt \box0
\hbox{ b}}
\end{verbatim}
\awp
gives
\begin{disp}\offinterlineskip\leavevmode\vbox{\hbox{a}
\setbox0=\hbox{(}
\ht0=0pt \dp0=0pt
\box0
\hbox{ b}}
\end{disp}
The difference is caused by the fact that horizontal boxes
are placed with respect to their reference point, but vertical
boxes with respect to their upper left corner.
%\spoint Hanging the \cs{lastbox} back in the list
\subsection{Hanging the \cs{lastbox} back in the list}
You can pick the last box off a vertical list that has been
compiled in (internal) vertical mode.
However, if you try to hang it back in the list the vertical
spacing may go haywire. If you just hang it back,
\begin{verbatim}
\setbox\tmpbox=\lastbox
\usethetmpbox \box\tmpbox
\end{verbatim}
baselineskip glue is added a second time. If you `unskip' prior
to hanging the box back,
\begin{verbatim}
\setbox\tmpbox=\lastbox \unskip
\usethetmpbox \box\tmpbox
\end{verbatim}
things go wrong in a more subtle way.
The \gram{internal dimen} \cs{prevdepth}
(which controls interline glue; see Chapter~\ref{baseline})
will have a
value based on the last box, but what you need for the proper
interline glue is a depth based on one box earlier.
The solution is not to unskip,
but to specify \cs{nointerlineskip}:
\begin{verbatim}
\setbox\tmpbox=\lastbox
\usethetmpbox \nointerlineskip \box\tmpbox
\end{verbatim}
%\spoint[varioset] Dissecting paragraphs with \cs{lastbox}
\subsection{Dissecting paragraphs with \cs{lastbox}}
\label{varioset}
Repeatedly applying \cs{last...} and \cs{un...} macros
\howto Take a paragraph apart\par
can be used to take a paragraph apart.
Here is an example of that.
\indent\vbox{\message{Check vario look!}
\hyphenpenalty10000 \exhyphenpenalty10000 %\Indent:no
\advance\hsize by -2\parindent
\newif\ifsnap \spaceskip=\fontdimen2\font plus \fontdimen3\font
\def\eatlines{
\setbox2\lastbox % check the last line
\ifvoid2\global\snaptrue
\else % if it's not empty
\unskip\unpenalty % take whatever is
{\eatlines} % above it;
\setbox4\hbox{\unhcopy2} % collapse this line
\ifdim\wd4<.98\wd2 % if the difference is too large,
\ifsnap \box2 \global\snapfalse
\else \box4 \global\snaptrue
\fi
\else \box2 \global\snapfalse
\fi
\fi}
In typesetting advertisement copy, a way of justifying
paragraphs has become popular in recent years
that is somewhere between flushright and raggedright
setting.
Lines that would stretch beyond certain limits
are set with their glue at natural width. This paragraph
exemplifies this procedure; the macros
follow next.\par\eatlines}\par
\begin{verbatim}
\newbox\linebox \newbox\snapbox
\def\eatlines{
\setbox\linebox\lastbox % check the last line
\ifvoid\linebox
\else % if it's not empty
\unskip\unpenalty % take whatever is
{\eatlines} % above it;
% collapse the line
\setbox\snapbox\hbox{\unhcopy\linebox}
% depending on the difference
\ifdim\wd\snapbox<.98\wd\linebox
\box\snapbox % take the one or the other,
\else \box\linebox \fi
\fi}
\end{verbatim}
This macro can be called as
\begin{verbatim}
\vbox{ ... some text ... \par\eatlines}
\end{verbatim}
or it can be inserted automatically
with \cs{everypar}; see~\cite{E1}.
In the macro \cs{eatlines}, the \cs{lastbox} is taken
from a vertical list. If the list is empty
the last box will test true on \cs{ifvoid}.
These boxes containing lines from a paragraph
are actually horizontal boxes: the test
\cs{ifhbox} applied to them would give a true
result.
%%%% end of input file [boxes]
%\InputFile:modes
%%%% this is input file [modes]
%\subject[hvmode] Horizontal and \nl Vertical Mode
\endofchapter
\chapter{Horizontal and Vertical Mode}\label{hvmode}
At any point in its processing \TeX\ is in some mode.
\term mode\par
There are six modes, divided in three categories:
\begin{enumerate} \item horizontal mode and restricted horizontal
mode, \item vertical mode and internal vertical mode, and
\item math mode and display math mode.\end{enumerate}
The math modes will be treated elsewhere (see page~\pageref{math:modes}).
Here we shall look
at the horizontal and vertical modes, the kinds of objects
that can occur in the corresponding lists, and the
commands that are exclusive for one mode or the other.
\begin{inventory}
\item [\cs{ifhmode}]
Test whether the current mode is (possibly restricted) horizontal mode.
\item [\cs{ifvmode}]
Test whether the current mode is (possibly internal) vertical mode.
\item [\cs{ifinner}]
Test whether the current mode is an internal mode.
\item [\cs{vadjust}]
Specify vertical material for the enclosing vertical list
while in horizontal mode.
\item [\cs{showlists}]
Write to the log file the contents of the partial lists
currently being built in all modes.
\end{inventory}
%\point Horizontal and vertical mode
\section{Horizontal and vertical mode}
When not typesetting mathematics, \TeX\ is in horizontal
or vertical mode, building horizontal or vertical lists
respectively. Horizontal mode is typically used to
make lines of text; vertical mode is typically used
to stack the lines of a paragraph on top of each other.
Note that
these modes
are different from the internal states of \TeX's input processor
(see page~\pageref{input:states}).
%\spoint Horizontal mode
\subsection{Horizontal mode}
The main activity in horizontal mode is building lines of text.
\term mode !horizontal\par
Text on the page and text in a \cs{vbox} or \cs{vtop} is built in
horizontal mode (this might be called `paragraph mode');
if the text is in an \cs{hbox} there is only one line
of text, and the corresponding mode is the restricted
\awp
horizontal mode.
In horizontal mode all material is added to a horizontal list.
If this list is built in unrestricted horizontal mode, it
will later be broken into lines and added to the surrounding vertical list.
Each element of a horizontal list is one of the following:
\term list !horizontal\par
\begin{itemize} \item a box (a character, ligature, \cs{vrule},
or a \gr{box}),
\item a discretionary break,
\item a whatsit (see Chapter~\ref{io}),
\item vertical material enclosed in \cs{mark},
\cs{vadjust}, or \cs{insert},
\item
\mdqon
glue or leaders, a kern, a penalty, or a math-on/""off item.
\mdqoff
\end{itemize}
The items in the last point are all discardable.
Discardable items are called that, because they disappear in
\term discardable items\par
a break. Breaking of horizontal
lists is treated in Chapter~\ref{line:break}.
%\spoint Vertical mode
\subsection{Vertical mode}
Vertical mode can be used to stack items on top of one another.
\term mode !vertical\par
Most of the time, these items are boxes
containing the lines of paragraphs.
Stacking material can take place inside a
vertical box, but the
items that are stacked can also
appear by themselves on the page. In the latter case
\TeX\ is in vertical mode; in the former case, inside a
vertical box, \TeX\ operates in internal vertical mode.
In vertical mode all material is added to a vertical list.
If this list is built in external vertical mode, it
will later be broken when pages are formed.
Each element of a vertical list is one of the following:
\term list !vertical\par
\begin{itemize} \item a box (a horizontal or vertical box or
an \cs{hrule}),
\item a whatsit,
\item a mark,
\item glue or leaders, a kern, or a penalty.\end{itemize}
The items in the last point are all discardable.
Breaking of vertical lists
is treated in Chapter~\ref{page:break}.
There are a few exceptional conditions at the beginning
of a vertical list: the value of \cs{prevdepth} is set
to \n{-1000pt}. Furthermore, no \cs{parskip} glue is added
at the top of an internal vertical list;
at the top of the main vertical list (the top of the
`current page') no glue or other discardable items
are added, and \cs{topskip} glue is added when the
first box is placed on this list
(see Chapters \ref{page:shape} and~\ref{page:break}).
%\point Horizontal and vertical commands
\section{Horizontal and vertical commands}
Some commands are so intrinsically horizontal or vertical
in nature that they force \TeX\ to go into that mode, if
possible. A~command that forces \TeX\ into horizontal mode
is called a \gr{horizontal command}; similarly a command that
forces \TeX\ into vertical mode is called a
\awp
\gr{vertical command}.
However, not all transitions are possible:
\TeX\ can switch from both vertical modes to
(unrestricted) horizontal mode and back
through horizontal and vertical commands, but no transitions
to or from restricted horizontal mode are possible
(other than by enclosing horizontal boxes in vertical boxes or
the other way around).
A~vertical command in restricted horizontal mode thus gives
an error; the \cs{par} command in restricted horizontal mode
has no effect.
The horizontal commands are the following:
\label{h:com:list}\term horizontal commands\par
\begin{itemize}
\item any \gr{letter}, \gr{otherchar}, \cs{char},
a control sequence defined by \cs{chardef}, or \cs{noboundary};
\item \cs{accent}, \cs{discretionary}, the discretionary
hyphen~\verb|\-| and control space~\verb|\|\n{\char32};
\item \cs{unhbox} and \cs{unhcopy};
\item \cs{vrule} and the
\gr{horizontal skip} commands
\cs{hskip}, \cs{hfil}, \cs{hfill}, \cs{hss}, and \cs{hfilneg};
\item \cs{valign};
\item math shift (\n\$).
\end{itemize}
The vertical commands are the following:
\label{v:com:list}\term vertical! commands\par
\begin{itemize}
\item \cs{unvbox} and \cs{unvcopy};
\item \cs{hrule} and the \gr{vertical skip} commands
\cs{vskip}, \cs{vfil}, \cs{vfill}, \cs{vss}, and \cs{vfilneg};
\item \cs{halign};
\item \cs{end} and \cs{dump}.
\end{itemize}
Note that the vertical commands do not include \cs{par};
nor are \cs{indent} and \cs{noindent} horizontal commands.
The connection between boxes and modes is explored below;
see Chapter~\ref{rules} for more on the connection between
rules and modes.
%\point The internal modes
\section{The internal modes}
Restricted horizontal mode and internal vertical mode
\term mode !restricted\par\term mode !internal\par
are the variants of horizontal mode and vertical mode
that hold inside an \cs{hbox} and \cs{vbox} (or \cs{vtop}
or \cs{vcenter}) respectively.
However, restricted horizontal mode is rather more
restricted in nature than internal vertical mode.
The third internal mode is non-display math mode
(see Chapter~\ref{math}).
%\spoint Restricted horizontal mode
\subsection{Restricted horizontal mode}
The main difference between restricted horizontal mode,
the mode in an \cs{hbox}, and unrestricted horizontal mode,
the mode in which paragraphs in vertical boxes
and on the page are built,
is that you cannot break out of restricted horizontal mode:
\cs{par}~does nothing in this mode.
Furthermore, a~\gram{vertical command} in restricted horizontal
mode gives an error.
In unrestricted horizontal mode it would cause a
\cs{par} token to be inserted and vertical mode to be entered
(see also Chapter~\ref{par:end}).
\awp
%\spoint Internal vertical mode
\subsection{Internal vertical mode}
Internal vertical mode, the vertical mode inside
a~\cs{vbox}, is a lot like external vertical
mode, the mode in which pages are built.
A~\gram{horizontal command} in internal vertical mode,
for instance, is perfectly valid:
\TeX\ then starts building a paragraph in
unrestricted horizontal mode.
One difference is that the commands
\cs{unskip} and \cs{unkern} have no effect
in external vertical mode, and
\cs{lastbox} is always empty in external vertical mode.
See further pages \pageref{lastbox} and~\pageref{unskip}.
The entries of alignments (see Chapter~\ref{align}) are
processed in internal modes: restricted horizontal mode
for the entries of an \cs{halign}, and internal vertical
mode for the entries of a~\cs{valign}.
The material in \cs{vadjust} and \cs{insert} items
is also processed in internal vertical mode; furthermore,
\TeX\ enters this mode when processing the \cs{output} token list.
The commands \cs{end} and \cs{dump} (the latter exists only in \IniTeX)
are not allowed in
internal vertical mode; furthermore, \cs{dump} is not allowed
inside a group (see Chapter~\ref{TeXcomm}).
%\point[hvbox] Boxes and modes
\section{Boxes and modes}
\label{hvbox}
There are horizontal and vertical boxes, and there is
horizontal and vertical mode. Not surprisingly, there is
a connection between the boxes and the modes.
One can ask about this connection in two ways.
%\spoint What box do you use in what mode?
\subsection{What box do you use in what mode?}
This is the wrong question. Both horizontal and vertical boxes
can be used in both horizontal and vertical mode.
Their placement is determined by the prevailing mode at that moment.
%\spoint What mode holds in what box?
\subsection{What mode holds in what box?}
This is the right question.
When an \cs{hbox} starts, \TeX\ is in restricted horizontal
mode. Thus everything in a horizontal box is lined up horizontally.
When a \cs{vbox} is started, \TeX\ is in internal vertical mode.
Boxes of both kinds and other items are then stacked
on top of each other.
%\spoint Mode-dependent behaviour of boxes
\subsection{Mode-dependent behaviour of boxes}
Any \gr{box} (see Chapter \ref{boxes} for the full definition)
can be used in horizontal, vertical, and math mode.
Unboxing commands, however, are specific for horizontal or vertical mode.
Both \cs{unhbox} and \cs{unhcopy} are \gr{horizontal command}s,
so they can make \TeX\ switch from vertical to horizontal
mode;
\awp
both \cs{unvbox} and \cs{unvcopy} are \gr{vertical command}s,
so they can make \TeX\ switch from horizontal to vertical
mode.
In horizontal mode the \cs{spacefactor} is set to 1000
after a box has been placed. In vertical mode the
\cs{prevdepth} is set to the depth of the box placed.
Neither statement holds for
unboxing commands: after an \cs{unhbox} or \cs{unhcopy} the
spacefactor is not altered, and after \cs{unvbox} or \cs{unvcopy}
the \cs{prevdepth} remains unchanged.
After all, these commands do not add a box,
but a piece of a~(horizontal or vertical) list.
The operations \cs{raise} and \cs{lower} can only be
applied to a box in horizontal mode; similarly, \cs{moveleft} and
\cs{moveright} can only be applied in vertical mode.
%\point Modes and glue
\section{Modes and glue}
Both in horizontal and vertical mode
\TeX\ can insert glue items the size of which is
determined by the preceding object in the list.
For horizontal mode the amount of glue that is inserted
for a space token depends on the \cs{spacefactor} of
the previous object in the list. This is treated
in Chapter~\ref{space}.
In vertical mode \TeX\ inserts glue to keep boxes at a certain
distance from each other. This glue is influenced by the
height of the current item and the depth of the previous one.
The depth of items is recorded in the \cs{prevdepth} parameter
(see Chapter~\ref{baseline}).
The two quantities \cs{prevdepth}
and \cs{spacefactor}
use the same internal register of \TeX. Thus the \cs{prevdepth}
can be used or asked only in vertical mode, and the \cs{spacefactor}
only in horizontal mode.
%\point[migrate] Migrating material
\section{Migrating material}
\label{migrate}
The three control sequences \cs{insert}, \cs{mark}, and \cs{vadjust}
can be given in a paragraph
\term migrating material\par
(the first two can also occur
in vertical mode) to specify material that will wind up on the
surrounding vertical list. Note that this need not be
the main vertical list: it can be a vertical box
containing a paragraph of text. In this case a \cs{mark}
or \cs{insert} command will not reach the page breaking algorithm.
When several migrating items are specified in a certain line
of text, their left-to-right order is preserved when they are
placed on the surrounding vertical list. These items are placed
directly after the horizontal box containing the line of text
in which they were specified: they come before any
penalty or glue items that are automatically inserted
(see page~\pageref{between:lines}).
%\spoint \cs{vadjust}
\subsection{\cs{vadjust}}
The command
\cstoidx vadjust\par
\begin{disp}\cs{vadjust}\gr{filler}\lb\gr{vertical mode material}\rb\end{disp}
\awp
is only allowed in horizontal and math modes (but it is
not a \gr{horizontal command}).
Vertical mode material specified by \cs{vadjust} is moved from
the horizontal list in which the command is given
to the surrounding vertical list, directly after the box
in which it occurred.
In the current line
\vadjust{\setbox0=\hbox{$\bullet$\hskip1em}\ht0=0pt \dp0=0pt \llap{\box0}}
a \cs{vadjust} item was placed to put the bullet in the margin.
Any vertical material in a \cs{vadjust} item is processed
in internal vertical mode, even though it will wind up
on the main vertical list. For instance, the \cs{ifinner}
test is true in a \cs{vadjust}, and at the start
\mdqon
of the vertical material \cs{prevdepth}$=$""\n{-1000pt}.
\mdqoff
%\point Testing modes
\section{Testing modes}
The three conditionals \cs{ifhmode}, \cs{ifvmode}, and
\cs{ifinner} can distinguish between the four modes of
\TeX\ that are not math modes.
The \cs{ifinner} test is true if \TeX\ is in
restricted horizontal mode or internal vertical mode
(or in non-display math mode).
Exceptional condition: during a \cs{write} \TeX\
is in a `no mode' state. The tests \cs{ifhmode},
\cs{ifvmode}, and \cs{ifmmode} are then all false.
Inspection of all current lists, including the `recent
contributions' (see Chapter~\ref{page:break}),
is possible through the command \csidx{showlists}\label{showlists}.
This command writes to the log file the contents of all
lists that are being built at the moment the command is given.
Consider the example \begin{verbatim}
a\hfil\break b\par
c\hfill\break d
\hbox{e\vbox{f\showlists
\end{verbatim}
Here the first paragraph has been broken into two lines, and
these have been added to the current page. The second paragraph
has not been concluded or broken into lines.
The log file shows the following. \TeX\ was busy
building a paragraph (starting with an indentation box
\n{20pt} wide):\begin{verbatim}
### horizontal mode entered at line 3
\hbox(0.0+0.0)x20.0
\tenrm f
spacefactor 1000
\end{verbatim}
This paragraph was inside a vertical box:\begin{verbatim}
### internal vertical mode entered at line 3
prevdepth ignored
\end{verbatim}
The vertical box was in a horizontal box,
\begin{verbatim}
### restricted horizontal mode entered at line 3
\tenrm e
spacefactor 1000
\end{verbatim}
\awp
which was part of
an as-yet unfinished paragraph:\begin{verbatim}
### horizontal mode entered at line 2
\hbox(0.0+0.0)x20.0
\tenrm c
\glue 0.0 plus 1.0fill
\penalty -10000
\tenrm d
etc.
spacefactor 1000
\end{verbatim}
Note how the infinite glue and the \cs{break} penalty
are still part of the horizontal list.
Finally, the first paragraph has been broken into lines and
added to the current page:\begin{verbatim}
### vertical mode entered at line 0
### current page:
\glue(\topskip) 5.69446
\hbox(4.30554+0.0)x469.75499, glue set 444.75497fil
.\hbox(0.0+0.0)x20.0
.\tenrm a
.\glue 0.0 plus 1.0fil
.\penalty -10000
.\glue(\rightskip) 0.0
\penalty 300
\glue(\baselineskip) 5.05556
\hbox(6.94444+0.0)x469.75499, glue set 464.19943fil
.\tenrm b
.\penalty 10000
.\glue(\parfillskip) 0.0 plus 1.0fil
.\glue(\rightskip) 0.0
etc.
total height 22.0 plus 1.0
goal height 643.20255
prevdepth 0.0
\end{verbatim}
%%%% end of input file [modes]
%\InputFile:number
%%%% this is input file [number]
%\subject[number] Numbers
\endofchapter
\chapter{Numbers}\label{number}
In this chapter integers and their
denotations will be treated,
the conversions that are possible either way,
allocation and use of \cs{count} registers, and
arithmetic with integers.
\begin{inventory}
\item [\cs{number}]
Convert a \gr{number} to decimal representation.
\item [\cs{romannumeral}]
Convert a positive \gr{number} to lowercase roman representation.
\item [\cs{ifnum}]
Test relations between numbers.
\item [\cs{ifodd}]
Test whether a number is odd.
\item [\cs{ifcase}]
Enumerated case statement.
\item [\cs{count}]
Prefix for count registers.
\item [\cs{countdef}]
Define a control sequence to be a synonym for
a~\cs{count} register.
\item [\cs{newcount}]
Allocate an unused \cs{count} register.
\item [\cs{advance}]
Arithmetic command to add to or subtract from
a~\gr{numeric variable}.
\item [\cs{multiply}]
Arithmetic command to multiply a \gr{numeric variable}.
\item [\cs{divide}]
Arithmetic command to divide a \gr{numeric variable}.
\end{inventory}
%\point Numbers and \gr{number}s
\section{Numbers and \gr{number}s}
An important part of the grammar of \TeX\
\term numbers\par\term integers\par
is the rigorous definition of a \gr{number}, the syntactic
entity that \TeX\ expects when semantically an integer is
expected. This definition will take the largest part of this
chapter. Towards the end, \cs{count} registers, arithmetic,
and tests for numbers are treated.
For clarity of discussion a distinction will be made
here between integers and numbers,
but note that a \gr{number} can be both
an `integer' and a `number'.
`Integer' will be taken to denote a mathematical number:
a~quantity that can be added or multiplied.
`Number' will be taken to refer to the printed representation
of an integer: a string of digits, in other words.
%\point Integers
\section{Integers}
Quite a few different sorts of objects can function
as integers in \TeX. In this section they will all
be treated, accompanied by the relevant lines from
the grammar of \TeX.
\awp
First of all, an integer can be positive or negative:
\begin{disp}\gr{number} $\longrightarrow$
\gr{optional signs}\gr{unsigned number}\nl
\gr{optional signs} $\longrightarrow$ \gr{optional spaces}\nl
\indent $|$ \gr{optional signs}\gr{plus or minus}\gr{optional spaces}
\end{disp}
A first possibility for an unsigned integer is a string of digits
in decimal, octal, or hexadecimal notation.
Together with the alphabetic constants these will be named
here \gr{integer denotation}.
Another possibility for an integer is an
internal integer quantity, an \gr{internal integer};
together with the denotations these form the
\gr{normal integer}s.
Lastly an integer can be a \gr{coerced integer}:
an internal \gr{dimen} or \gr{glue}
quantity that is converted to an integer value.
\begin{disp}\gr{unsigned number} $\longrightarrow$ \gr{normal integer}
$|$ \gr{coerced integer}\nl
\gr{normal integer} $\longrightarrow$ \gr{integer denotation}
$|$ \gr{internal integer}\nl
\gr{coerced integer} $\longrightarrow$ \gr{internal dimen}
$|$ \gr{internal glue}\end{disp}
All of these possibilities will be treated in sequence.
%\spoint[int:denotation] Denotations: integers
\subsection{Denotations: integers}
\label{int:denotation}
Anything that looks like a number
can be used as a \gr{number}: thus \verb-42- is a number.
However, bases other than decimal can also be used:
\begin{verbatim}
'123
\end{verbatim}
is the octal notation for $1\times8^2+2\times8^1+3\times8^0=83$,
and \begin{verbatim}
"123
\end{verbatim}
is the hexadecimal notation
for $1\times16^2+2\times16^1+3\times16^0=291$.
\begin{disp}\gr{integer denotation} $\longrightarrow$
\gr{integer constant}\gr{one optional space} \nl
\indent $|$ \n{\char`\'}\gr{octal constant}\gr{one optional space}\nl
\indent $|$ \n{\char`\"}\gr{hexadecimal constant}\gr{one optional space}
\end{disp}
The octal digits are \n0--\n7; a~digit \n8 or~\n9 following an
octal denotation is not part of the number:
after \begin{verbatim}
\count0='078
\end{verbatim}
the \cs{count0} will have the value~7, and the
digit~\n8 is typeset.
The hexadecimal digits are \n0--\n9, \n A--\n F,
where the \n A--\n F can
have category code 11 or~12. The latter has a somewhat
far-fetched justification: the characters resulting from a
\cs{string} operation have category code~12.
Lowercase \n a--\n f are not
hexadecimal digits, although (in \TeX3) they are used
for hexadecimal notation in
the `circumflex method' for accessing all character codes
(see Chapter~\ref{char}).
%\spoint Denotations: characters
\subsection{Denotations: characters}
A character token is a pair consisting of a character code,
which is a~number in the range 0--255,
and a category code. Both of these codes are accessible,
and can be used as a \gr{number}.
\awp
The character code of a character token, or of a single letter
control sequence, is accessible through the left quote command:
both \verb-`a- and~\verb-`\a- denote the character code of~{\tt a},
which can be used as an integer.
\begin{disp}\gr{integer denotation} $\longrightarrow$
\n{\char`\`}\gr{character token}\gr{one optional space}\end{disp}
In order to emphasize that accessing the character code is
in a sense using a denotation, the syntax of \TeX\ allows
an optional space after such a `character constant'.
The left quote must have category~12.
%\spoint Internal integers
\subsection{Internal integers}
The class of \gr{internal integers} can
be split into five parts.
The \gr{codename}s and \gr{special integer}s
will be treated separately below; furthermore, there are the following.
\begin{itemize} \item The contents of \cs{count} registers;
either explicitly used by writing for instance \cs{count23},
or by referring to such a register by means of a
control sequence
that was defined by \cs{countdef}:
after \begin{verbatim}
\countdef\MyCount=23
\end{verbatim}
\cs{MyCount} is called a
\gr{countdef token}, and it is fully equivalent to \cs{count23}.
\item All parameters of \TeX\ that hold integer values;
this includes obvious ones such as \cs{linepenalty}, but
also parameters such as
\cs{hyphenchar}\gr{font} and \cs{parshape}
(if a paragraph shape has been defined for $n$ lines,
using \cs{parshape} in the context of a \gr{number}
will yield this value of~$n$).
\item\label{num:chardef} Tokens defined by \cs{chardef} or \cs{mathchardef}.
After \begin{verbatim}
\chardef\foo=74
\end{verbatim}
the control sequence \cs{foo}
can be used on its own to mean \cs{char74}, but in a context
where a \gr{number} is wanted it can be used to denote~74:
\begin{verbatim}
\count\foo
\end{verbatim}
is equivalent to \verb=\count74=.
This fact is
exploited in the allocation routines for registers (see
Chapter~\ref{alloc}).
A control sequence thus defined by \cs{chardef} is called a
\gr{chardef token}; if it is defined by \cs{mathchardef} it
is called a \gr{mathchardef token}.
\end{itemize}
Here is the full list:
\begin{disp}\gr{internal integer} $\longrightarrow$
\gr{integer parameter} \nl
\indent $|$ \gr{special integer} $|$ \cs{lastpenalty}\nl
\indent $|$ \gr{countdef token} $|$ \cs{count}\gr{8-bit number}\nl
\indent $|$ \gr{chardef token} $|$ \gr{mathchardef token}\nl
\indent $|$ \gr{codename}\gr{8-bit number}\nl
\indent $|$ \cs{hyphenchar}\gr{font} $|$ \cs{skewchar}\gr{font}
$|$ \cs{parshape}\nl
\indent $|$ \cs{inputlineno} $|$ \cs{badness}\nl
\gr{integer parameter} $\longrightarrow$\vadjust{\nobreak}
$|$ \cs{adjdemerits} $|$ \cs{binoppenalty}\nl
\indent $|$ \cs{brokenpenalty} $|$ \cs{clubpenalty} $|$ \cs{day}%
\awp
\nl
\indent $|$ \cs{defaulthyphenchar} $|$ \cs{defaultskewchar} \nl
\indent $|$ \cs{delimiterfactor} $|$ \cs{displaywidowpenalty} \nl
\indent $|$ \cs{doublehyphendemerits} $|$ \cs{endlinechar}
$|$ \cs{escapechar}\nl
\indent $|$ \cs{exhypenpenalty} $|$ \cs{fam} $|$ \cs{finalhyphendemerits}\nl
\indent $|$ \cs{floatingpenalty} $|$ \cs{globaldefs} $|$ \cs{hangafter}\nl
\indent $|$ \cs{hbadness} $|$ \cs{hyphenpenalty}
$|$ \cs{interlinepenalty}\nl
\indent $|$ \cs{linepenalty} $|$ \cs{looseness} $|$ \cs{mag}\nl
\indent $|$ \cs{maxdeadcycles} $|$ \cs{month} \nl
\indent $|$ \cs{newlinechar} $|$ \cs{outputpenalty} $|$ \cs{pausing}\nl
\indent $|$ \cs{postdisplaypenalty} $|$ \cs{predisplaypenalty}\nl
\indent $|$ \cs{pretolerance} $|$ \cs{relpenalty} $|$ \cs{showboxbreadth}\nl
\indent $|$ \cs{showboxdepth} $|$ \cs{time} $|$ \cs{tolerance}\nl
\indent $|$ \cs{tracingcommands} $|$ \cs{tracinglostchars}
$|$ \cs{tracingmacros}\nl
\indent $|$ \cs{tracingonline} $|$ \cs{tracingoutput}
$|$ \cs{tracingpages}\nl
\indent $|$ \cs{tracingparagraphs} $|$ \cs{tracingrestores}
$|$ \cs{tracingstats}\nl
\indent $|$ \cs{uchyph} $|$ \cs{vbadness} $|$ \cs{widowpenalty}
$|$ \cs{year}
\end{disp}
Any internal integer can function as an \gr{internal unit},
which \ldash preceded by \gr{optional spaces} \rdash
can serve as a \gr{unit of measure}.
Examples of this are given in Chapter~\ref{glue}.
%\spoint Internal integers: other codes of a character
\subsection{Internal integers: other codes of a character}
The \cs{catcode} command
(which was described in Chapter~\ref{mouth})
is a \gr{codename}, and like the other code names
it can be used as an integer.
\begin{disp}\gr{codename} $\longrightarrow$ \cs{catcode} $|$ \cs{mathcode}
$|$ \cs{uccode} $|$ \cs{lccode}\nl \indent $|$ \cs{sfcode} $|$ \cs{delcode}
\end{disp}
A~\gr{codename} has to be followed by an \gr{8-bit number}.
Uppercase and lowercase codes were treated in Chapter~\ref{char};
the \cs{sfcode} is treated
in Chapter~\ref{space};
the \cs{mathcode} and~\cs{delcode} are treated in
Chapter~\ref{mathchar}.
%\spoint[special:int:list] \gr{special integer}
\subsection{\gr{special integer}}
\label{special:int:list}
One of the subclasses of the internal integers is
that of the special integers.
\begin{disp}\gr{special integer} $\longrightarrow$
\cs{spacefactor} $|$ \cs{prevgraf}\nl
\indent $|$ \cs{deadcycles} $|$ \cs{insertpenalties}
\end{disp}
An assignment to any of these is called an \gr{intimate
assignment}, and is automatically global
(see Chapter~\ref{group}).
%\spoint Other internal quantities: coersion to integer
\subsection{Other internal quantities: coersion to integer}
\TeX\ provides a conversion between dimensions and integers:
if an integer is expected, a \gr{dimen} or \gr{glue} used
in that context is converted by taking its
\awp
(natural) size
in scaled points.
However, only \gr{internal dimen}s and \gr{internal glue}
can be used this way: no dimension or glue denotations
can be coerced to integers.
%\spoint Trailing spaces
\subsection{Trailing spaces}
The syntax of \TeX\ defines integer denotations (decimal,
octal, and hexadecimal) and `back-quoted' character tokens
to be followed by \gr{one optional space}. This means that
\TeX\ reads the token after the number, absorbing it
if it was a space token, and backing up if it was not.
Because \TeX's input processor goes into the state `skipping spaces'
after it has seen one space token, this
scanning behaviour implies that
integer denotations can be followed by
arbitrarily many space characters in the input.
Also, a line end is admissible.
However, only one space token is allowed.
%\point Numbers
\section{Numbers}
\TeX\ can perform an implicit conversion from a string
\term number! conversion\par\term number!roman numerals\par
\cstoidx number\par\cstoidx romannumeral\par
of digits to an integer. Conversion from a representation
in decimal, octal, or hexadecimal notation was
treated above. The conversion the other way,
from an \gr{internal integer} to a printed representation,
has to be performed explicitly.
\TeX\ provides two conversion routines,
\cs{number} and \cs{romannumeral}.
The command \cs{number} is equivalent to \cs{the}
when followed by an internal integer.
These commands are performed in the expansion processor of \TeX, that is,
they are expanded whenever expansion has not been inhibited.
Both commands
yield a string of tokens with category code~12;
their argument is a~\gr{number}.
Thus \verb-\romannumeral51-, \verb-\romannumeral\year-,
and~\verb-\number\linepenalty- are valid, and so is~\verb-\number13-.
Applying \cs{number} to a denotation has some uses:
it removes leading zeros and superfluous plus and minus signs.
A roman numeral is a string of lowercase `roman digits',
which are characters of category code~12.
The sequence\howto Uppercase roman numberals\par
\begin{verbatim}
\uppercase\expandafter{\romannumeral ...}
\end{verbatim}
gives uppercase roman numerals.
This works because \TeX\ expands
tokens in order to find the opening brace of the argument
of \verb=\uppercase=. If \cs{romannumeral} is applied to
a negative number, the result is simply empty.
%\point Integer registers
\section{Integer registers}
Integers can be stored in \csidx{count} registers:
\begin{Disp}\cs{count}\gr{8-bit number}\end{Disp}
is an \gr{integer variable} and an \gr{internal integer}.
As an integer variable it can be used in a
\gr{variable assignment}:
\begin{Disp}\gr{variable assignment} $\longrightarrow$
\gr{integer variable}\gr{equals}\gr{number} $|$ \dots\end{Disp}
\awp
As an internal integer it can be used as a \gr{number}:
\begin{Disp}\gr{number} $\rightarrow$ \gr{optional signs}\gr{internal integer}
$|$ \dots
\end{Disp}
Synonyms for \cs{count} registers can be introduced by the
\csidx{countdef} command in a \gr{shorthand definition}:
\begin{Disp}\cs{countdef}\gr{control sequence}\gr{equals}\gr{8-bit number}
\end{Disp} A control sequence defined this way
is called a \gr{countdef token}, and it serves as an
\gr{internal integer}.
The plain \TeX\ macro \csidx{newcount}
(which is declared \cs{outer}) uses the \cs{countdef} command
to allocate an unused \cs{count} register.
Counters 0--9 are scratch registers, like all
registers with numbers~0--9.
However, counters 0--9 are used for page identification
in the \n{dvi} file (see Chapter~\ref{TeXcomm}),
so they should be used as scratch
registers only inside a group.
Counters 10--22 are
used for plain \TeX's bookkeeping of allocation of registers.
Counter 255 is also scratch.
%\point Arithmetic
\section{Arithmetic}
The user can perform some arithmetic in \TeX, and
\term arithmetic\par
\TeX\ also performs arithmetic internally. User arithmetic
is concerned only with integers; the internal arithmetic
is mostly on fixed-point quantities, and only in the
case of glue setting on floating-point numbers.
%\spoint Arithmetic statements
\subsection{Arithmetic statements}
\TeX\ allows the user to
\cstoidx advance\par\cstoidx multiply\par\cstoidx divide\par
perform some arithmetic on integers. The statement
\begin{Disp}\cs{advance}\gr{integer variable}\gr{optional \n{by}}%
\gr{number}\end{Disp}
adds the value of the \gr{number}
\ldash which may be negative \rdash to the \gr{integer variable}.
Similarly,
\begin{Disp}\cs{multiply}\gr{integer variable}\gr{optional \n{by}}%
\gr{number}\end{Disp}
multiplies the value of the \gr{integer variable}, and
\begin{Disp}\cs{divide}\gr{integer variable}\gr{optional \n{by}}%
\gr{number}\end{Disp}
divides an \gr{integer variable}.
Multiplication and division are also available for any so-called
\gr{numeric variable}: their most general form is
\begin{disp}\cs{multiply}\gr{numeric variable}\gr{optional \n{by}}\gr{number}
\end{disp} where
\begin{disp}\gr{numeric variable} $\longrightarrow$
\gr{integer variable} $|$ \gr{dimen variable}\nl
\indent $|$ \gr{glue variable} $|$ \gr{muglue variable}\end{disp}
The result of an arithmetic operation should not exceed
\awp
$2^{30}$ in absolute value.
Division of integers yields an integer; that is, the remainder
is discarded. This raises the question of how rounding is performed
when either operand is negative. In such cases \TeX\ performs
the division with the absolute values of the operands, and
takes the negative of the result if exactly one operand was negative.
%\spoint Floating-point arithmetic
\subsection{Floating-point arithmetic}
Internally some arithmetic on floating-point quantities
\term arithmetic! floating-point\par
is performed, namely
in the calculation of glue set ratios.
%and slant for accents!!
However, machine-dependent aspects of rounding cannot
influence the decision process of \TeX, so machine independence
of \TeX\ is guaranteed in this respect (sufficient
accuracy of rounding is enforced by the \n{Trip} test of~\cite{K:trip}).
%\spoint Fixed-point arithmetic
\subsection{Fixed-point arithmetic}
All fractional arithmetic in \TeX\ is performed in fixed-point
\term arithmetic! fixed-point\par
arithmetic of `scaled integers': multiples of~$2^{-16}$.
This ensures the machine independence of \TeX.
Printed representations of scaled integers are rounded
to 5 decimal digits.
In ordinary 32-bit implementations of \TeX\ the largest
integers are $2^{31}-1$ in absolute size.
The user is not allowed to specify
dimensions larger in absolute size than~$2^{30}-1$: two
such dimensions can be added or subtracted without
overflow on a 32-bit system.
%\point Number testing
\section{Number testing}
The most general test for integers in \TeX\ is
\begin{disp}\cs{ifnum}\gr{number$_1$}\gr{relation}\gr{number$_2$}\end{disp}
where \gr{relation} is a~\n<, \n>, or~\n= character,
all of category~12.
Distinguishing between odd and even numbers is done
by \begin{disp}\cs{ifodd}\gr{number}\end{disp}
A numeric case statement is provided by
\begin{disp}\cs{ifcase}\gr{number}\gr{case$_0$}\cs{or}\n{...}\cs{or}%
\gr{case$_n$}\cs{else}\gr{other cases}\cs{fi}\end{disp}
where the \cs{else}-part is optional. The tokens for \gr{case$_i$}
are processed if the number turns out to be~$i$; other cases are
skipped, similarly to what ordinarily happens in conditionals
(see Chapter~\ref{if}).
%\point Remarks
\section{Remarks}
%\spoint Character constants
\subsection{Character constants}
In formats and macro collections numeric constants
are often needed. There are several ways to implement these
in \TeX.
\awp
Firstly,
\begin{verbatim}
\newcount\SomeConstant \SomeConstant=42
\end{verbatim}
This is wasteful, as it uses up a \cs{count} register.
Secondly,
\begin{verbatim}
\def\SomeConstant{42}
\end{verbatim}
Better but accident prone: \TeX\ has to expand to find the number
\ldash which in itself is a slight overhead \rdash and may inadvertently
expand some tokens that should have been left alone.
Thirdly,
\begin{verbatim}
\chardef\SomeConstant=42
\end{verbatim}
This one is fine.
A \gr{chardef token} has the same status as a \cs{count}
register: both are \gr{internal integer}s.
Therefore a number defined this way can be used everywhere that
a \cs{count} register is feasible.
For large numbers the \cs{chardef} can be replaced by \cs{mathchardef},
which runs to \verb>"7FFF>${}=32\,767$.
Note that a \gr{mathchardef token} can usually only appear
in math mode, but in the context of a number it can appear anywhere.
%\spoint Expanding too far / how far
\subsection{Expanding too far / how far}
It is a common mistake to write pieces of \TeX\ code
where \TeX\ will inadvertently expand something because it
is trying to compose a number. For example:
\begin{verbatim}
\def\par{\endgraf\penalty200}
...\par \number\pageno
\end{verbatim}
Here the page number will be absorbed into the value of the penalty.
Now consider
\begin{verbatim}
\newcount\midpenalty \midpenalty=200
\def\par{\endgraf\penalty\midpenalty}
...\par \number\pageno
\end{verbatim}
Here the page number is not scooped up by mistake:
\TeX\ is trying to locate a \gr{number} after the \cs{penalty},
and it finds a \gr{countdef token}. This is {\em not\/}
converted to a representation in digits, so there is never any
danger of the page number being touched.
It is possible to convert a \gr{countdef token} first to
a representation in digits before assigning it:
\begin{verbatim}
\penalty\number\midpenalty
\end{verbatim}
and this brings back again all previous problems of expansion.
%%%% end of input file [number]
%\InputFile:glue
%%%% this is input file [glue]
%\subject[glue] Dimensions and Glue
\endofchapter
\chapter{Dimensions and Glue}\label{glue}
In \TeX\ vertical and horizontal white space
can have a possibility to adjust itself through `stretching' or
\term glue\par
`shrinking'. An~adjustable white space is called `glue'.
This chapter treats all technical concepts related to
dimensions and glue, and it explains how the badness of stretching or shrinking
a certain amount is calculated.
\begin{inventory}
\item [\cs{dimen}]
Dimension register prefix.
\item [\cs{dimendef}]
Define a control sequence to be a synonym for
a~\cs{dimen} register.
\item [\cs{newdimen}]
Allocate an unused dimen register.
\item [\cs{skip}]
Skip register prefix.
\item [\cs{skipdef}]
Define a control sequence to be a synonym for
a~\cs{skip} register.
\item [\cs{newskip}]
Allocate an unused skip register.
\item [\cs{ifdim}]
Compare two dimensions.
\item [\cs{hskip}]
Insert in horizontal mode a glue item.
\item [\csidx{hfil}]
Equivalent to
\verb-\hskip 0cm plus 1fil-.
\item [\csidx{hfilneg}]
Equivalent to
\verb-\hskip 0cm minus 1fil-.
\item [\csidx{hfill}]
Equivalent to
\verb-\hskip 0cm plus 1fill-.
\item [\csidx{hss}]
Equivalent to
\verb-\hskip 0cm plus 1fil minus 1fil-.
\item [\cs{vskip}]
Insert in vertical mode a glue item.
\item [\csidx{vfil}]
Equivalent to
\verb-\vskip 0cm plus 1fil-.
\item [\csidx{vfill}]
Equivalent to
\verb-\vskip 0cm plus 1fill-.
\item [\csidx{vfilneg}]
Equivalent to
\verb-\vskip 0cm minus 1fil-.
\item [\csidx{vss}]
Equivalent to
\verb-\vskip 0cm plus 1fil minus 1fil-.
\item [\cs{kern}]
Add a kern item to the current horizontal or vertical list.
\item [\cs{lastkern}]
If the last item on the current list was a kern, the size of it.
\item [\cs{lastskip}]
If the last item on the current list was a~glue, the size of it.
\item [\cs{unkern}]
If the last item of the current list was a~kern, remove it.
\item [\cs{unskip}]
If the last item of the current list was a~glue, remove it.
\item [\cs{removelastskip}]
Macro to append the negative of the \cs{lastskip}.
\item [\cs{advance}]
Arithmetic command to add to or subtract from
a~\gr{numeric variable}.
\item [\cs{multiply}]
Arithmetic command to multiply a~\gr{numeric variable}.
\item [\cs{divide}]
Arithmetic command to divide a~\gr{numeric variable}.
\end{inventory}
%\point Definition of \gr{glue} and \gr{dimen}
\section{Definition of \gr{glue} and \gr{dimen}}
This section gives
the syntax of the quantities
\gr{dimen} and \gr{glue}.
In the next section the practical aspects of glue are treated.
Unfortunately the terminology for glue is slightly confusing.
The syntactical quantity~\gr{glue} is a dimension (a distance) with
\mdqon
possibly a stretch and/""or shrink component.
\mdqoff
In order to add a glob of `glue' (a white space) to a list one has to
let a \gr{glue} be preceded by commands such as \cs{vskip}.
%\spoint Definition of dimensions
\subsection{Definition of dimensions}
A~\gr{dimen} is what \TeX\ expects to see when
it needs to indicate a dimension; it can be positive or negative.
\begin{disp}\gr{dimen} $\longrightarrow$ \gr{optional signs}%
\gr{unsigned dimen}\end{disp}
The unsigned part of a \gr{dimen} can be
\begin{disp}\gr{unsigned dimen} $\longrightarrow$ \gr{normal dimen}
$|$ \gr{coerced dimen}\nl
\gr{normal dimen} $\longrightarrow$ \gr{internal dimen}
$|$ \gr{factor}\gr{unit of measure}\nl
\gr{coerced dimen} $\longrightarrow$ \gr{internal glue}
\end{disp}
That is, we have the following three cases:
\begin{itemize} \item an \gr{internal dimen}; this is
any register or parameter of \TeX\ that has a \gr{dimen} value:
\begin{disp}\PopIndentLevel\gr{internal dimen} $\longrightarrow$
\gr{dimen parameter}\nl
\indent $|$ \gr{special dimen} $|$ \cs{lastkern}\nl
\indent $|$ \gr{dimendef token} $|$ \cs{dimen}\gr{8-bit number}\nl
\indent $|$ \cs{fontdimen}\gr{number}\gr{font}\nl
\indent $|$ \gr{box dimension}\gr{8-bit number}\nl
\gr{dimen parameter} $\longrightarrow$ \cs{boxmaxdepth}\nl
\indent $|$ \cs{delimitershortfall} $|$ \cs{displayindent}\nl
\indent $|$ \cs{displaywidth} $|$ \cs{hangindent}\nl
\indent $|$ \cs{hfuzz} $|$ \cs{hoffset} $|$ \cs{hsize}\nl
\indent $|$ \cs{lineskiplimit} $|$ \cs{mathsurround}\nl
\indent $|$ \cs{maxdepth} $|$ \cs{nulldelimiterspace}\nl
\indent $|$ \cs{overfullrule} $|$ \cs{parindent}\nl
\indent $|$ \cs{predisplaysize} $|$ \cs{scriptspace}\nl
\indent $|$ \cs{splitmaxdepth} $|$ \cs{vfuzz}\nl
\indent $|$ \cs{voffset} $|$ \cs{vsize}
\end{disp}
\item a dimension denotation,
consisting of \gr{factor}\gr{unit of measure},
for example \verb>0.7\vsize>; or
\item an \gr{internal glue} (see below)
coerced to a dimension by omitting
the stretch and shrink components, for example \cs{parfillskip}.
\end{itemize}
A dimension denotation is a somewhat complicated entity:
\begin{itemize} \item a \gr{factor} is an integer denotation,
a decimal constant denotation (a number with an integral and
a fractional part),
or an \gr{internal integer}
\begin{disp}\PopIndentLevel
\gr{factor} $\longrightarrow$ \gr{normal integer}
$|$ \gr{decimal constant}\nl
\gr{normal integer} $\longrightarrow$ \gr{integer denotation}\nl
\indent $|$ \gr{internal integer}\nl
\gr{decimal constant} $\longrightarrow$ \n{.$_{12}$}
$|$ \n{,$_{12}$}\nl
\indent $|$ \gr{digit}\gr{decimal constant}\nl
\indent $|$ \gr{decimal constant}\gr{digit}
\end{disp}
An internal integer is a parameter that is `really' an
\alt
integer (for instance, \cs{count0}), and not coerced from a dimension or glue.
See Chapter~\ref{number}
for the definition of various kinds of integers.
\item a \gr{unit of measure} can be
a \gr{physical unit}, that is, an ordinary unit such as~\n{cm}
(possibly preceded by \n{true}),
an internal unit such as~\n{em}, but also an \gr{internal integer}
(by conversion to scaled points),
an \gr{internal dimen}, or an \gr{internal glue}.
\begin{disp}\PopIndentLevel
\gr{unit of measure} $\longrightarrow$
\gr{optional spaces}\gr{internal unit}\nl
\indent $|$
\gr{optional \n{true}}\gr{physical unit}\gr{one optional space}\nl
\gr{internal unit} $\longrightarrow$
\n{em}\gr{one optional space}\nl
\indent $|$ \n{ex}\gr{one optional space}
$|$ \gr{internal integer}\nl
\indent $|$ \gr{internal dimen} $|$ \gr{internal glue}
\end{disp}
\end{itemize}
Some \gr{dimen}s are called \gr{special dimen}s:\label{special:dimen:list}
\begin{disp}\gr{special dimen} $\longrightarrow$ \cs{prevdepth}\nl
\indent $|$ \cs{pagegoal} $|$ \cs{pagetotal} $|$ \cs{pagestretch}\nl
\indent $|$ \cs{pagefilstretch} $|$ \cs{pagefillstretch}\nl
\indent $|$ \cs{pagefilllstretch} $|$ \cs{pageshrink} $|$ \cs{pagedepth}
\end{disp}
An assignment to any of these is
called an \gr{intimate assignment}, and it is automatically
global (see Chapter~\ref{group}). The meaning of these
dimensions is explained in Chapter \ref{page:break}, with the
exception of \cs{prevdepth} which is treated in
Chapter~\ref{baseline}.
%\spoint Definition of glue
\subsection{Definition of glue}
A \gr{glue} is either some form of glue variable, or
a glue denotation with explicitly indicated stretch and
shrink. Specifically,
\begin{disp}\gr{glue} $\longrightarrow$ \gr{optional signs}\gr{internal glue}
$|$ \gr{dimen}\gr{stretch}\gr{shrink}\nl
\gr{internal glue} $\longrightarrow$ \gr{glue parameter}
$|$ \cs{lastskip}\nl
\indent $|$ \gr{skipdef token} $|$ \cs{skip}\gr{8-bit number}\nl
\gr{glue parameter} $\longrightarrow$ \cs{abovedisplayshortskip}\nl
\indent $|$ \cs{abovedisplayskip} $|$ \cs{baselineskip}\nl
\indent $|$ \cs{belowdisplayshortskip} $|$ \cs{belowdisplayskip}\nl
\indent $|$ \cs{leftskip} $|$ \cs{lineskip} $|$ \cs{parfillskip}
$|$ \cs{parskip}\nl
\indent $|$ \cs{rightskip} $|$ \cs{spaceskip}
$|$ \cs{splittopskip} $|$ \cs{tabskip}\nl
\indent $|$ \cs{topskip} $|$ \cs{xspaceskip}
\end{disp}
The stretch and shrink components in a glue denotation
are optional, but when both are specified they have to
be given in sequence; they are defined as
\begin{disp}
\gr{stretch} $\longrightarrow$ \n{plus} \gr{dimen}
$|$ \n{plus}\gr{fil dimen} $|$ \gr{optional spaces}\nl
\gr{shrink} $\longrightarrow$ \n{minus} \gr{dimen}
$|$ \n{minus}\gr{fil dimen} $|$ \gr{optional spaces}\nl
\gr{fil dimen} $\longrightarrow$ \gr{optional signs}\gr{factor}%
\gr{fil unit}\gr{optional spaces}\nl
\gr{fil unit} $\longrightarrow$ \n{ $|$ fil $|$ fill $|$ filll}
\end{disp}
The actual definition of \gr{fil unit} is recursive
(see Chapter~\ref{gramm}), but these are the only valid
possibilities.
%\spoint Conversion of \gr{glue} to \gr{dimen}
\subsection{Conversion of \gr{glue} to \gr{dimen}}
The grammar rule
\begin{disp}\gr{dimen} $\longrightarrow$
\gr{factor}\gr{unit of measure}
\end{disp}
has some noteworthy consequences, caused by the fact
that a \gr{unit of measure} need not look like a `unit of measure'
at all (see the list above).
For instance, from this definition we conclude that the statement
\begin{verbatim}
\dimen0=\lastpenalty\lastpenalty
\end{verbatim}
is
syntactically correct because \cs{lastpenalty} can function
both as an integer and as \gr{unit of measure} by taking
its value in scaled points.
After \verb>\penalty8> the \cs{dimen0} thus defined will
have a size of~\n{64sp}.
More importantly, consider the case where the \gr{unit of measure} is
an \gr{internal glue}, that is, any sort of glue parameter.
Prefixing such a glue with a number (the \gr{factor})
makes it a valid \gr{dimen} specification.
Thus \begin{verbatim}
\skip0=\skip1
\end{verbatim}
is very different
from \begin{verbatim}
\skip0=1\skip1
\end{verbatim}
The first statement makes
\cs{skip0} equal to \cs{skip1}, the second converts
the \cs{skip1} to a \gr{dimen} before assigning it.
In other words, the \cs{skip0} defined by the second statement
has no stretch or shrink.
%\spoint Registers for \cs{dimen} and \cs{skip}
\subsection{Registers for \cs{dimen} and \cs{skip}}
\TeX\ has registers for storing \gr{dimen} and \gr{glue}
values: the \csidx{dimen} and \csidx{skip} registers
respectively. These are accessible by the expressions
\begin{disp}\cs{dimen}\gr{number}\end{disp} and
\begin{disp}\cs{skip}\gr{number}\end{disp}
As with all registers of \TeX, these registers are
numbered~0--255.
Synonyms for registers can be made with the \csidx{dimendef} and
\csidx{skipdef} commands. Their syntax is
\begin{Disp}\cs{dimendef}\gr{control sequence}\gr{equals}\gr{8-bit number}
\end{Disp}
and
\begin{Disp}\cs{skipdef}\gr{control sequence}\gr{equals}\gr{8-bit number}\end{Disp}
For example, after \verb-\skipdef\foo=13- using \cs{foo}
is equivalent to using \cs{skip13}.
Macros \csidx{newdimen} and \csidx{newskip} exist in plain \TeX
for allocating an unused dimen or skip register.
These macros are defined to be \cs{outer} in the plain format.
%\spoint Arithmetic: addition
\subsection{Arithmetic: addition}
As for integer variables, arithmetic operations exist for
\cstoidx advance\par\term glue!arithmetic on\par\term arithmetic! on glue\par
dimen, glue, and muglue (mathematical glue; see page~\pageref{muglue})
variables.
The expressions
\begin{Disp}\cs{advance}\gr{dimen variable}\gr{optional \n{by}}%
\gr{dimen}\nl
\cs{advance}\gr{glue variable}\gr{optional \n{by}}%
\gr{glue}\nl
\cs{advance}\gr{muglue variable}\gr{optional \n{by}}%
\gr{muglue}\end{Disp}
add to the size of a dimen, glue, or muglue.
Advancing a \gr{glue variable} by \gr{glue} is done by
adding the natural sizes, and the stretch and shrink components.
Because \TeX\ converts between \gr{glue} and \gr{dimen},
it is possible to write for instance
\begin{verbatim}
\advance\skip1 by \dimen1
\end{verbatim}
or
\begin{verbatim}
\advance\dimen1 by \skip1
\end{verbatim}
In the first case \cs{dimen1} is coerced to \gr{glue} without
stretch or shrink; in the second case the \cs{skip1} is coerced
to a \gr{dimen} by taking its natural size.
%\spoint Arithmetic: multiplication and division
\subsection{Arithmetic: multiplication and division}
Multiplication and division operations exist for glue
\cstoidx multiply\par\cstoidx divide\par
and dimensions. One may for instance write
\begin{verbatim}
\multiply\skip1 by 2
\end{verbatim}
which multiplies the natural size, and the stretch and shrink
components of \cs{skip1} by~2.
The second operand of a \cs{multiply} or \cs{divide}
operation can only be a \gr{number}, that is, an integer.
Introducing the notion of \gr{numeric variable}:
\begin{disp}\gr{numeric variable} $\longrightarrow$ \gr{integer variable}
$|$ \gr{dimen variable} \nl
\indent $|$ \gr{glue variable} $|$ \gr{muglue variable}\end{disp}
these operations take the form
\begin{Disp}\cs{multiply}\gr{numeric variable}\gr{optional \n{by}}%
\gr{number}\end{Disp}
and
\begin{Disp}\cs{divide}\gr{numeric variable}\gr{optional \n{by}}%
\gr{number}\end{Disp}
Glue and dimen can be multiplied by
non-integer quantities:
\begin{verbatim}
\skip1=2.5\skip2
\dimen1=.78\dimen2
\end{verbatim}
However, in the first line the \cs{skip2} is first coerced
to a \gr{dimen} value by omitting its stretch and shrink.
%\point More about dimensions
\section{More about dimensions}
%\spoint Units of measurement
\subsection{Units of measurement}
In \TeX\ dimensions can be indicated in
\term units of measurement\par
\begin{description} \item [centimetre]
denoted \n{cm} or
\item [millimetre]
denoted \n{mm}; these are SI~units ({\italic Syst\`eme International
d'Unit\'es}, the
international system of standard units of measurements).
\item [inch]
\n{in}; more common in the Anglo-American world.
One inch is 2.54~centimetres.
\item [pica]
denoted \n{pc}; one pica is 12~points.
\item [point]
denoted \n{pt}; the common system
for Anglo-American printers. One inch is 72.27 points.
\item [didot point]
denoted \n{dd}; the common system for continental European printers.
Furthermore, 1157 didot points are 1238~points.
\item [cicero]
denoted \n{cc}; one cicero is 12~didot points.
\item [big point]
denoted \n{bp}; one inch is 72 big points.
\item [scaled point]
denoted \n{sp}; this is the smallest unit in \TeX, and all measurements
are integral multiples of one scaled point.
There are $65\,536$ scaled points in a~point.
\end{description}
Decimal fractions can be written using both the
Anglo-American system with the decimal point
(for example, \n{1in}=\n{72.27pt})
and the continental European system with a decimal
comma; \n{1in}=\n{72,27pt}.
Internally \TeX\ works with multiples of a smallest
dimension: the scaled point.
Dimensions larger (in absolute value) than $2^{30}-1$\n{sp},
which is about 5.75~metres or 18.9~feet, are illegal.
Both the pica system and the didot system are of French
origin: in 1737 the type founder Pierre Simon Fournier
introduced typographical points based on the French foot.
Although at first he introduced a system based on lines and
points, he later took the point as unit:
there are 72 points in an inch,
which is one-twelfth of a foot.
About 1770 another founder, Fran\c{c}ois Ambroise Didot, introduced
points based on the more common, and slightly longer,
`pied du roi'.
%\spoint Dimension testing
\subsection{Dimension testing}
Dimensions and natural sizes of glue can be compared with
the \cs{ifdim} test. This takes the form
\begin{disp}\cs{ifdim}\gr{dimen$_1$}\gr{relation}\gr{dimen$_2$}\end{disp}
where the relation can be an \n>, \n<, or~\n= token,
all of category~12.
%\spoint Defined dimensions
\subsection{Defined dimensions}
\begin{inventory}
\item [\cs{z@}]
\n{0pt}
\item [\cs{maxdimen}]
\n{16383.99999pt}; the largest legal dimension.
\end{inventory}
These \gr{dimen}s are predefined in the plain format;
for instance \begin{verbatim}
\newdimen\z@ \z@=0pt
\end{verbatim}
Using such abbreviations for commonly used dimensions
has at least two advantages. First of all it saves main memory
if such a dimension occurs in a macro: a control sequence
is one token, whereas a string such as \n{0pt} takes three.
Secondly, it saves time in processing, as \TeX\ does not need
to perform conversions to arrive at the correct type of
object.
Control sequences such as \cs{z@}
are only available to a user who changes the
category code of the `at' sign. Ordinarily, these control sequences
appear only in the macros defined in packages such as the
plain format.
%\point More about glue
\section{More about glue}
Glue items can be added to a vertical list with one of the
\alt
commands \csidx{vskip}\gr{glue}, \cs{vfil}, \cs{vfill}, \cs{vss} or
\cs{vfilneg};
glue items can be added to a horizontal list with one of the
commands \csidx{hskip}\gr{glue}, \cs{hfil}, \cs{hfill}, \cs{hss} or
\cs{hfilneg}. We will now treat the properties of glue.
%\spoint Stretch and shrink
\subsection{Stretch and shrink}
In the syntax given above, \gr{glue} was defined as having
\term stretch\par\term shrink\par
\term glue!stretch component of\par\term glue!shrink component of\par
\begin{itemize}\item a `natural size', which is a \gr{dimen}, and optionally
\item a `stretch' and `shrink' component built out of a \gr{fil dimen}.
\end{itemize}
Each list that \TeX\ builds has amounts of stretch and shrink
(possibly zero),
which are the sum of the
stretch and shrink components of individual pieces of glue in the list.
Stretch and shrink are used if the context in which the list
appears requires it to assume a size that is different from
its natural size.
There is an important difference in behaviour between stretch
and shrink components when they are finite \ldash that is,
when the \gr{fildimen} is not \n{fil}(\n{l}(\n{l})).
A~finite amount of shrink is indeed the maximum shrink
that \TeX\ will take: the amount of glue specified
as \begin{verbatim}
5pt minus 3pt
\end{verbatim}
can shrink to \n{2pt}, but not further.
In contrast to this, a finite amount of stretch
can be stretched arbitrarily far.
Such arbitrary stretching
has a large `badness', however.
Badness calculation is treated below.
\begin{example}
The sequence with natural size \n{20pt}
\begin{verbatim}
\hskip 10pt plus 2pt \hskip 10pt plus 3pt
\end{verbatim}
has \n{5pt} of stretch, but it has no shrink. In
\begin{verbatim}
\hskip 10pt minus 2pt \hskip 10pt plus 3pt
\end{verbatim}
there is \n{3pt} of stretch, and \n{2pt} of shrink,
so its minimal size is~\n{18pt}.
Positive shrink is not the same as negative stretch:
\begin{verbatim}
\hskip 10pt plus -2pt \hskip 10pt plus 3pt
\end{verbatim}
looks a lot like the previous example, but it cannot
be shrunk as there are no \hbox{\n{minus}\gr{dimen}}
specifications. It does have \n{1pt} of stretch, however.
This is another example of negative amounts of shrink and stretch.
It is not possible to stretch
glue (in the informal sense) by shrinking it (in the technical
sense): \begin{verbatim}
\hbox to 5cm{a\hskip 0cm minus -1fil}
\end{verbatim}
is an underfull box, because \TeX\ looks for a \n{plus}~\gr{dimen}
specification when it needs to stretch the contents.
Finally, \begin{verbatim}
\hskip 10pt plus -3pt \hskip 10pt plus 3pt
\end{verbatim}
can neither stretch nor shrink.
The fact that there is only stretch
available means that the sequence cannot
shrink. However, the stretch components cancel out: the
total stretch is zero. Another way of looking at this
is to consider that for each point that the second glue item would
stretch, the first one would `stretch back' one point.
\end{example}
Any amount of infinite stretch or shrink overpowers all
finite stretch or shrink available:
\begin{verbatim}
\hbox to 5cm{\hskip 0cm plus 16384pt
text\hskip 0cm plus 0.0001fil}
\end{verbatim}
has the \n{text} at the extreme left of the box.
There are three orders of `infinity', each one infinitely
stronger than the previous one:
\begin{verbatim}
\hbox to 5cm{\hskip 0cm plus 16384fil
text\hskip 0cm plus 0.0001fill}
\end{verbatim}
and
\begin{verbatim}
\hbox to 5cm{\hskip 0cm plus 16384fill
text\hskip 0cm plus 0.0001filll}
\end{verbatim}
both have the \n{text} at the left end of the box.
%\spoint Glue setting
\subsection{Glue setting}
In the process of `glue setting', the desired width (or height)
\term glue! setting\par
of a box is compared with the natural dimension of its contents,
which is the sum of all natural dimensions of boxes and globs of glue.
If the two differ, any available stretchability or shrinkability is used
to bridge the gap.
To attain the desired dimension of the box
only the glue of the highest available order is set:
each piece of glue of that order is stretched or shrunk by the
same ratio.
For example, in
\begin{verbatim}
\hbox to 6pt{\hskip 0pt plus 3pt \hskip 0pt plus 9pt}
\end{verbatim}
the natural size of the box is~\n{0pt}, and
the total stretch is~\n{12pt}. In order to obtain a box
of~\n{6pt} each glue item is set with a stretch ratio
of~$1/2$. Thus the result is equivalent to
\begin{verbatim}
\hbox {\hskip 1.5pt \hskip 4.5pt}
\end{verbatim}
Only the highest order of stretch or shrink is used:
in \begin{verbatim}
\hbox to 6pt{\hskip 0pt plus 1fil \hskip 0pt plus 9pt}
\end{verbatim}
the second glue will assume its natural size of~\n{0pt},
and only the first glue will be stretched.
\TeX\ will never exceed the maximum value of a finite
amount of shrink.
A~box that cannot be shrunk enough is called `overfull'.
Finite stretchability can be exceeded to provide an
escape in difficult situations; however, \TeX\ is likely
to give an \verb-Underfull \hbox- message about this
(see page~\pageref{over/underfull}).
For an example of infinite shrink see page~\pageref{rlap}.
%\spoint Badness
\subsection{Badness}
When stretching or shrinking a list \TeX\ calculates
\term badness! calculation\par
badness based on the
ratio between actual stretch and the amount of stretch
present in the line. See Chapter~\ref{line:break}
for the application of badness to the paragraph algorithm.
%\tracingmacros=2 \tracingcommands\tracingmacros
The formula for badness of a list that is stretched (shrunk) is
\label{bad:form}\message{Check roman min}
\begin{disp} $\displaystyle b=\hbox{min}\left(10\,000,
100\times \left({\hbox{actual amount stretched (shrunk)}
\over\hbox{possible amount of stretch (shrink)}}\right)^3\right)$\end{disp}
In reality \TeX\ uses a slightly different formula that is
easier to calculate, but behaves the same. Since glue setting is
one of the main activities of \TeX, this must be performed
as efficiently as possible.
This formula lets the badness be a reasonably small number
if the glue set ratio (the fraction in the above expression)
is reasonably small, but will let it grow rapidly once
the ratio is more than~1. Badness is infinite if the
glue would have to shrink more than the allotted amount;
stretching glue beyond its maximum is possible, so this
provides an escape for very difficult lines of text or pages.
In \TeX3, the \cs{badness} parameter records the badness
of the most recently formed box.
%\spoint Glue and breaking
\subsection{Glue and breaking}
\TeX\ can break lines and pages in several kinds of places.
One of these places is before a glue item.
The glue is then discarded. For line breaks this is treated
in Chapter~\ref{line:break},
for page breaks see Chapter~\ref{page:break}.
There are two macros in plain \TeX, \csidx{hglue} and \csidx{vglue},
that give non-disappearing glue in horizontal and
vertical mode respectively. For the horizontal case this is
accomplished by
placing:
\begin{verbatim}
\vrule width 0pt \nobreak \hskip ...
\end{verbatim}
Because \TeX\ breaks at the front end of glue,
this glue will always stay attached to the rule,
and will therefore never disappear.
The actual macro definitions are somewhat more complicated,
because they take care to preserve the \cs{spacefactor} and the
\cs{prevdepth}.
%\spoint \cs{kern}
\subsection{\cs{kern}}
The \csidx{kern} command specifies
a~kern item in whatever mode \TeX\ is currently
in. A~kern item is much like a glue item without
stretch or shrink.
It differs from glue in that it is
in general not a legal breakpoint. Thus in
\begin{verbatim}
.. text .. \hbox{a}\kern0pt\hbox{b}
\end{verbatim}
\TeX\ will not break lines in between the boxes; in
\begin{verbatim}
.. text .. \hbox{a}\hskip0pt\hbox{b}
\end{verbatim}
a line can be broken in between the boxes.
However, if a kern is followed by glue, \TeX\ can break at the
kern (provided that it is not in math mode).
In horizontal mode
both the kern and the glue then disappear in the break.
In vertical mode they are discarded when they are moved to
the (empty) current page after the material before
the break has been disposed of by the output routine
(see Chapter~\ref{page:break}).
%\spoint Glue and modes
\subsection{Glue and modes}
All horizontal skip commands are \gr{horizontal command}s and
all vertical skip commands are \gr{vertical commands}s.
This means that, for instance, an \cs{hskip} command
makes \TeX\ start a paragraph if it is given in vertical mode.
The \cs{kern} command can be given in both modes.
%\spoint The last glue item in a list: backspacing
\subsection{The last glue item in a list: backspacing}
The last glue item in a list can be measured, and
it can be removed in all modes but external vertical mode.
The internal variables
\csidx{lastskip} and \csidx{lastkern} can be used
to measure the last glob of glue in all modes;
if the last glue was not a skip or kern respectively
they give~\n{0pt}.
In math mode the \cs{lastskip}
functions as \gr{internal muglue}, but in general
it classifies as \gr{internal glue}.
The \cs{lastskip} and \cs{lastkern}
are also \n{0pt} if that was the size of the last glue or
kern item on the list.
The operations\label{unskip}
\csidx{unskip} and \csidx{unkern} remove the last item of a list,
if this is a glue or kern respectively. They have no effect
in external vertical mode; in that case the
best substitute is
\verb=\vskip-\lastskip=
and~\verb=\kern-\lastkern=.
In the process of paragraph building \TeX\ itself performs
an important \cs{unskip}: a~paragraph ending with a
white line will have a space token inserted by \TeX's input processor.
This is removed by an \cs{unskip} before the \cs{parfillskip} glue
(see Chapter~\ref{par:end}) is inserted.
Glue is treated by \TeX\ as a special case of leaders,
which becomes apparent when \cs{unskip} is applied to
leaders: they are removed.
%\spoint Examples of backspacing
\subsection{Examples of backspacing}
The plain \TeX\ macro \csidx{removelastskip} is defined
as \begin{verbatim}
\ifdim\lastskip=0pt \else \vskip-\lastskip \fi
\end{verbatim}
If the last item on the list was a glue, this macro will
backspace by its value, provided its natural size was not zero.
In all other cases, nothing is added to the list.
Sometimes an intelligent version of commands such as \cs{vskip}
is necessary, in the sense that two subsequent skip commands
should result only in the larger of the two glue amounts.
On page~\pageref{skip:scheme} such a macro is used:
\begin{verbatim}
\newskip\tempskipa
\def\vspace#1{\tempskipa=#1\relax
\ifvmode \ifdim\tempskipa<\lastskip
\else \vskip-\lastskip \vskip\tempskipa
\fi
\else \vskip\tempskipa \fi}
\end{verbatim}
First of all, this tests whether the mode is vertical;
if not, the argument can safely be placed.
Copying the argument into a skip register is necessary
because \cs{v\-space}\verb>{2pt plus 3pt}> would lead to
problems in an \verb>\ifdim#1<\lastskip> test.
If the surrounding mode was vertical, the argument
should only be placed if it is not less than what is
already there. The macro would be incorrect
if the test read
\begin{verbatim}
\ifdim\tempskipa>\lastskip
\vskip-\lastskip \vskip\tempskipa
\fi
\end{verbatim}
In this case the sequence
\begin{verbatim}
... last word.\par \vspace{0pt plus 1fil}
\end{verbatim}
would not place any glue, because after
the \cs{par} we are in vertical mode and
\cs{lastskip} has a value of \n{0pt}.
%\spoint Glue in trace output
\subsection{Glue in trace output}
If the workings of \TeX\ are traced by setting
\cs{tracingoutput} positive, or if \TeX\
writes a box to the log file
(because of a \cs{showbox} command, or because it
is overfull or underfull),
glue is denoted by the control sequence \cs{glue}.
This is not a \TeX\ command; it merely indicates the presence
of glue in the current list.
The box representation that \TeX\ generated from,
\alt
for instance, \cs{showbox}
inserts a space after every explicit \cs{kern},
but no space is inserted after an implicit
kern that was inserted by the kerning information in the font
\n{tfm} file. Thus \hbox{\verb-\kern 2.0pt-} denotes a kern
that was inserted by the user or by a macro, and
\verb-\kern2.0pt- denotes an implicit kern.
Glue that is inserted automatically (\cs{topskip}, \cs{baselineskip},
et cetera) is denoted by name in \TeX's trace output.
For example, the box
\begin{verbatim}
\vbox{\hbox{Vo}\hbox{b}}
\end{verbatim}
looks like
\begin{verbatim}
\vbox(18.83331+0.0)x11.66669
.\hbox(6.83331+0.0)x11.66669
..\tenrm V
..\kern-0.83334
..\tenrm o
.\glue(\baselineskip) 5.05556
.\hbox(6.94444+0.0)x5.55557
..\tenrm b
\end{verbatim}
Note the implicit kern inserted between `V' and~`o'.
%%%% end of input file [glue]
%\InputFile:rules
%%%% this is input file [rules]
%\subject[rules] Rules and Leaders
\endofchapter
\chapter{Rules and Leaders}\label{rules}
Rules and leaders are two ways of getting \TeX\ to draw a line.
Leaders are more general than rules: they can also fill
available space with copies of a certain box. This chapter
explain how rules and leaders work, and how they interact with modes.
\begin{inventory}
\item [\cs{hrule}]
Rule that spreads in horizontal direction.
\item [\cs{vrule}]
Rule that spreads in vertical direction.
\item [\cs{leaders}]
Fill a specified amount of space with a rule or copies of box.
\item [\cs{cleaders}]
Like \verb=\leaders=, but with box leaders
any excess space is split equally before and after the leaders.
\item [\cs{xleaders}]
Like \verb=\leaders=, but with box leaders any excess space is
spread equally before, after, and between the boxes.
\end{inventory}
%\point Rules
\section{Rules}
\TeX's rule commands give
\term rules\par
rectangular black patches with horizontal and vertical sides.
Most of the times, a rule command will give output that
looks like a rule, but~\hbox{\vrule height 1.5ex width 1.5ex}
can also be produced by a rule.
\TeX\ has both horizontal and vertical rules,
but the names do not necessarily imply anything about the shape.
They do, however, imply something about modes:
an \csidx{hrule} command can only be used in vertical mode,
and a \csidx{vrule} only in horizontal mode.
In fact, an \cs{hrule} is a \gr{vertical command}, and a \cs{vrule}
is a \gr{horizontal command}, so \TeX\ may change
modes when encountering these commands.
Why then is a \cs{vrule} called a {\em vertical\/} rule?
The reason is that a \cs{vrule} can expand arbitrarily
far in the vertical direction: if its height and depth are not
specified explicitly it will take as much room as its
surroundings allow\altt.
\begin{example}
\begin{verbatim}
\hbox{\vrule\ text \vrule}
\end{verbatim}
looks like \begin{disp}\leavevmode\hbox{\vrule\ text \vrule}\end{disp}
and \begin{verbatim}
\hbox{\vrule\ A gogo! \vrule}
\end{verbatim}
looks like
\begin{disp}\leavevmode\hbox{\vrule\ A gogo! \vrule}\end{disp}
\end{example}
For the \cs{hrule} command a similar statement is true:
a horizontal rule can spread to assume the width of
its surroundings. Thus
\begin{verbatim}
\vbox{\hbox{One line of text}\hrule}
\end{verbatim}
looks like
\begin{disp}\leavevmode\vtop{\hbox{One line of text}\hrule}\end{disp}
%\spoint Rule dimensions
\subsection{Rule dimensions}
Horizontal and vertical rules have a default thickness:
\begin{Disp} \cs{hrule}\quad is the same as\quad \verb-\hrule height.4pt depth0pt-
\end{Disp}
and
\begin{Disp} \cs{vrule}\quad is the same as\quad \verb-\vrule width.4pt- \end{Disp}
and if the remaining dimension remains unspecified, the rule
extends in that direction to fill the enclosing box.
Here is the formal specification of how to indicate rule sizes:
\begin{disp}\gr{vertical rule} $\longrightarrow$
\cs{vrule}\gr{rule specification}\nl
\gr{horizontal rule} $\longrightarrow$
\cs{hrule}\gr{rule specification}\nl
\gr{rule specification} $\longrightarrow$
\gr{optional spaces} \nl \indent$|$
\gr{rule dimensions}\gr{rule specification}\nl
\gr{rule dimension} $\longrightarrow$
\n{width}\gr{dimen} $|$ \n{height}\gr{dimen} $|$
\n{depth}\gr{dimen}
\end{disp}
If a rule dimension is specified twice, the second instance
takes precedence over the first. This makes it possible
to override the default dimensions. For instance,
after
\alt
\howto Change the default dimensions of rules\par
\begin{verbatim}
\let\xhrule\hrule \def\hrule{\xhrule height .8pt}
\end{verbatim}
the macro \cs{hrule} gives a horizontal rule
of double the original height, and it is still possible
with \begin{verbatim}
\hrule height 2pt
\end{verbatim}
to specify other heights.
It is possible to specify all three dimensions; then
\begin{verbatim}
\vrule height1ex depth0pt width1ex
\end{verbatim}
and
\begin{verbatim}
\hrule height1ex depth0pt width1ex
\end{verbatim}
look the same.
Still, each of them can be used only in the appropriate mode.
%\point Leaders
\section{Leaders}
Rules are intimately connected to modes, which makes it easy
\term leaders\par
to obtain some effects. For instance, a typical application
of a vertical rule looks like
\begin{verbatim}
\hbox{\vrule width1pt\ Important text! \vrule width 1pt}
\end{verbatim}
which gives
\begin{disp}\leavevmode\hbox{\vrule width 1pt\ Important text!
\vrule width 1pt}\end{disp}
However, one might want to have a horizontal rule
in horizontal mode for effects such as
\begin{disp}\leavevmode
\vbox{\hbox to 5cm{$\longleftarrow$\hfil 5cm\hfil$\longrightarrow$}
\hbox to 5cm{from here\leaders\hrule\hfil to there}}\end{disp}
An \cs{hrule} can not be used in horizontal mode, and
a vertical rule will not spread automatically.
However, there is a way to use an \cs{hrule} command in
horizontal mode and a \cs{vrule} in vertical mode,
and that is with `leaders', so called because
they lead your eye across the page.
A~leader command tells \TeX\
to fill a~specified space, in whatever mode it is in,
with as many copies of some box or rule specification
as are needed. For instance, the above example
was given as
\begin{disp}\verb>\hbox to 5cm{from here\leaders\hrule\hfil to there}>\end{disp}
that is, with an \cs{hrule} that was allowed to stretch along
an \cs{hfil}.
Note that the leader was given a horizontal skip,
corresponding to the horizontal mode in which it appeared.
A general leader command looks like
\begin{Disp} \gr{leaders}\gr{box or rule}%
\gr{vertical/horizontal/mathematical skip}\end{Disp}
where \gr{leaders} is \cs{leaders}, \cs{cleaders},
or~\cs{xleaders}, a \gr{box~or~rule}
is a~\gr{box}, \cs{vrule}, or~\cs{hrule}, and the
lists of horizontal and vertical skips appear in Chapter~\ref{hvmode};
a~mathematical skip is either a horizontal skip or an~\cs{mskip}
(see page~\pageref{muglue}).
Leaders can thus be used in all three modes. Of course, the
appropriate kind of skip must be specified.
A horizontal (vertical) box containing leaders has at least
the height and depth (width) of the \gr{box~or~rule} used
in the leaders, even if, as can happen in the case of box leaders,
no actual leaders are placed.
%\spoint Rule leaders
\subsection{Rule leaders}
Rule leaders fill the specified amount of space with a rule
\term leaders !rule\par\cstoidx leaders\par
extending in the direction of the skip specified.
The other dimensions of the resulting rule leader
are determined by the sort of rule that is used:
either dimensions can be specified explicitly, or
the default values can be used.
For instance,
\begin{verbatim}
\hbox{g\leaders\hrule\hskip20pt f}
\end{verbatim}
gives \begin{disp}\leavevmode\hbox{g\leaders\hrule\hskip20pt f}\end{disp}
because a horizontal rule has a default height of~\n{.4pt}.
On the other hand,
\begin{verbatim}
\hbox{g\leaders\vrule\hskip20pt f}
\end{verbatim}
gives \begin{disp}\leavevmode\hbox{g\leaders\vrule\hskip20pt f}\end{disp}
because the height and depth of a vertical rule
by default fill the surrounding box.
Spurious rule dimensions are ignored: in horizontal mode
\begin{verbatim}
\leaders\hrule width 10pt \hskip 20pt
\end{verbatim}
is equivalent to
\begin{verbatim}
\leaders\hrule \hskip 20pt
\end{verbatim}
If the width or height-plus-depth
of either the skip or the box is negative,
\TeX\ uses ordinary glue instead of leaders.
%\spoint Box leaders
\subsection{Box leaders}
Box leaders fill the available spaces with copies of
a given box, instead of with a rule.
\newbox\centerdot \setbox\centerdot=\hbox{\hskip.7em.\hskip.7em}
For all of the following examples, assume that a box register
has been allocated:
\begin{verbatim}
\newbox\centerdot \setbox\centerdot=\hbox{\hskip.7em.\hskip.7em}
\end{verbatim}
Now the output of
\begin{verbatim}
\hbox to 8cm {here\leaders\copy\centerdot\hfil there}
\end{verbatim}
is
\begin{disp}\leavevmode\hbox to 8cm {here\leaders\copy\centerdot\hfil there}
\end{disp} That is, copies of the box register fill up the
available space.
Dot leaders, as in the above example, are often used for
tables of contents. In such applications it is desirable that
dots on subsequent lines are vertically aligned.
The \cs{leaders} command does this automatically:
\begin{verbatim}
\hbox to 8cm {here\leaders\copy\centerdot\hfil there}
\hbox to 8cm {over here\leaders\copy\centerdot\hfil over there}
\end{verbatim}
gives \begin{disp}\leavevmode
\vtop{\hbox to 8cm {here\leaders\copy\centerdot\hfil there}
\hbox to 8cm {over here\leaders\copy\centerdot\hfil over there\strut}}
\end{disp}
The mechanism behind this is the following:
\TeX\ acts as if an infinite row of boxes starts (invisibly) at
the left edge of the surrounding box,
and the row of copies actually placed is
merely the part of this row that is not obscured by
the other contents of the box.
Stated differently, box leaders are a window on an infinite
row of boxes, and the row starts at the left edge of the
surrounding box. Consider the following example:
\begin{verbatim}
\hbox to 8cm {\leaders\copy\centerdot\hfil}
\hbox to 8cm {word\leaders\copy\centerdot\hfil}
\end{verbatim}
which gives
\begin{disp}\leavevmode\vtop{\hbox to 8cm {\leaders\copy\centerdot\hfil}
\hbox to 8cm {word\leaders\copy\centerdot\hfil\strut}}\end{disp}
The row of leaders boxes becomes visible as soon as it
does not coincide with other material.
The above discussion only talked about leaders in horizontal
mode. Leaders can equally well be placed in vertical mode;
for box leaders the `infinite row' then starts at the top
of the surrounding box.
%\spoint Evenly spaced leaders
\subsection{Evenly spaced leaders}
Aligning subsequent box leaders in the way described above
means that the white space before and after the
leaders will in general be different.
If vertical alignment is not
an issue it may be aesthetically more pleasing to have
the leaders evenly spaced.
The \csidx{cleaders} command is like \cs{leaders},
except that it splits excess space before and after the leaders
into two equal parts, centring the row of boxes in the
available space.
\begin{example}\message{check verbatim indentation}
\begin{verbatim}
\hbox to 7.8cm {here\cleaders\copy\centerdot\hfil there}
\hbox to 7.8cm {here is\cleaders\copy\centerdot\hfil there}
\end{verbatim}
gives \begin{disp}\leavevmode\vbox{
\hbox to 7.8cm {here\cleaders\copy\centerdot\hfil there}
\hbox to 7.8cm {here is\cleaders\copy\centerdot\hfil there\strut}
}\end{disp}
The `expanding leaders' \csidx{xleaders} spread excess space evenly
between the boxes, with equal globs of glue before, after,
and in between leader boxes.
\end{example}
\begin{example} \begin{verbatim}
\hbox to 7.8cm{here\hskip.7em
\xleaders\copy\centerdot\hfil \hskip.7em there}
\end{verbatim}
gives \begin{disp}\leavevmode
\hbox to 7.8cm {here\hskip.7em\xleaders\copy\centerdot\hfil\hskip.7em there}
\end{disp} Note that the glue in the leader box is balanced here
with explicit glue before and after the leaders;
leaving out these glue items, as in\begin{verbatim}
\hbox to 7.8cm {here\xleaders\copy\centerdot\hfil there}
\end{verbatim}
gives \begin{disp}\leavevmode
\hbox to 7.8cm {here\xleaders\copy\centerdot\hfil there}
\end{disp}
which is clearly not what was intended.
\end{example}
%\point Assorted remarks
\section{Assorted remarks}
%\spoint Rules and modes
\subsection{Rules and modes}
Above it was explained how rules can only occur in the
appropriate modes. Rules also influence mode-specific
quantities:
no baselineskip is added before rules in
vertical mode. In order to prevent glue after rules,
\TeX\ sets \cs{prevdepth} to
\n{\hbox{-}1000pt}
(see Chapter~\ref{baseline}).
Similarly the \cs{spacefactor} is set to 1000 after a \cs{vrule}
in horizontal mode (see Chapter~\ref{line:break}).
%\spoint[par:leaders:end] Ending a paragraph with leaders
\subsection{Ending a paragraph with leaders}
\label{par:leaders:end}
An attempt to simulate an \cs{hrule} at the end of a paragraph by
\howto End a paragraph with leaders\par
\begin{verbatim}
\nobreak\leaders\hrule\hfill\par
\end{verbatim}
does not work. The reason for this is that \TeX\
performs an \cs{unskip} at the end of a paragraph,
which removes the leaders. Normally this \cs{unskip} removes
any space token inserted by the input processor after the
last line. Remedy: stick an \verb.\hbox{}. at the end of
the leaders.
%\spoint Leaders and box registers
\subsection{Leaders and box registers}
In the above examples the leader box was inserted with
\cs{copy}. The output of
\begin{verbatim}
\hbox to 8cm {here\leaders\box\centerdot\hfil there}
\hbox to 8cm {over here\leaders\box\centerdot\hfil
over there}
\end{verbatim}
is
\begin{disp}\leavevmode
\vtop{\hbox to 8cm {here\leaders\box\centerdot\hfil there}
\hbox to 8cm {over here\leaders\box\centerdot\hfil over there}
}\end{disp}
The box register is emptied after the first leader command,
but more than one copy is placed in that first command.
%\spoint Output in leader boxes
\subsection{Output in leader boxes}
Any \cs{write}, \cs{openout}, or \cs{closeout} operation
appearing in leader boxes is ignored.
Otherwise such an operation would be executed once for every
copy of the box that would be shipped out.
%\spoint Box leaders in trace output
\subsection{Box leaders in trace output}
The dumped box representation obtained from,
for instance, \cs{tracingoutput}
does not write out box leaders in full: only the total size and
one copy of the box used are dumped. In particular,
the surrounding white space before and after the leaders
is not indicated.
%\spoint Leaders and shifted margins
\subsection{Leaders and shifted margins}
If margins have been shifted,
leaders may look different
depending on how the shift has been realized.
For an illustration of how \cs{hangindent} and \cs{leftskip}
influence the look of leaders, consider the following
examples, where
\begin{verbatim}
\setbox0=\hbox{K o }
\end{verbatim}
The horizontal boxes above the leaders
\altt
serve to indicate the starting point of the row of leaders.
First
\begin{verbatim}
\hbox{\leaders\copy0\hskip5cm}
\noindent\advance\leftskip 1em
\leaders\copy0\hskip5cm\hbox{}\par
\end{verbatim}
gives\message{examples on}
\begin{disp}\leavevmode\vbox{\leftskip=0pt \hsize=7cm
\setbox0=\hbox{K o }
\hbox{\leaders\copy0\hskip5cm}
\noindent\advance\leftskip 1em
\leaders\copy0\hskip5cm\hbox{}\par
}\end{disp}
Then
\begin{verbatim}
\hbox{\kern1em\hbox{\leaders\copy0\hskip5cm}}
\hangindent=1em \hangafter=-1 \noindent
\leaders\copy0\hskip5cm\hbox{}\par
\end{verbatim}
gives (note the shift with respect to the previous example)
\begin{disp}\leavevmode\vbox{\leftskip=0pt \hsize=7cm
\setbox0=\hbox{K o }
\hbox{\kern1em\hbox{\leaders\copy0\hskip5cm}}
\hangindent=1em \hangafter=-1 \noindent
\leaders\copy0\hskip5cm\hbox{}\par}\end{disp}
\message{one page}
In the first paragraph the \cs{leftskip} glue only obscures
the first leader box; in the second paragraph the hanging
indentation actually shifts the orientation point for the
row of leaders. Hanging indentation is performed in \TeX\
by a \cs{moveright} of the boxes containing the lines
of the paragraph.
%%%% end of input file [rules]
%\InputFile:group
%%%% this is input file [group]
%\subject[group] Grouping
\endofchapter
\chapter{Grouping}\label{group}
\TeX\ has a grouping mechanism that is able to confine most
changes to a~particular locality. This chapter explains
what sort of actions can be local, and how groups are formed.
\begin{inventory}
\item [\cs{bgroup}]
Implicit beginning of group character.
\item [\cs{egroup}]
Implicit end of group character.
\item [\cs{begingroup}]
Open a group that must be closed with \cs{endgroup}.
\item [\cs{endgroup}]
Close a group that was opened with \cs{begingroup}.
\item [\cs{aftergroup}]
Save the next token for insertion after the current group ends.
\item [\cs{global}]
Make assignments, macro definitions, and arithmetic global.
\item [\cs{globaldefs}]
Parameter for overriding \cs{global} prefixes.
\IniTeX\ default:~0.
\end{inventory}
%\point The grouping mechanism
\section{The grouping mechanism}
A group is a sequence of tokens starting with a
\term grouping\par
`beginning of group' token,
and ending with an `end of group'
token, and in which all such tokens are properly balanced.
The grouping mechanism of \TeX\ is not the same as
the block structure
of ordinary programming languages.
Most languages with block structure are only able to have
local definitions. \TeX's grouping mechanism is stronger:
most assignments made inside a group
are local to that group unless explicitly indicated otherwise,
and outside the group old values are restored.
An example of local definitions
\begin{verbatim}
{\def\a{b}}\a
\end{verbatim}
gives an `undefined control sequence'
message because \cs{a} is only defined inside the group.
Similarly, the code
\begin{verbatim}
\count0=1 {\count0=2 } \showthe\count0
\end{verbatim}
will display the value~1; the assignment made inside the group
is undone at the end of the group.
Bookkeeping of values that are to be restored outside the group
is done through the mechanism
\term save stack\par
of the `save stack'. Overflow of the save stack is treated
in Chapter~\ref{error}. The save stack is also used for
a few other purposes: in calls such as \hbox{\verb>\hbox to 100pt{...}>}
the specification \hbox{\n{to 100pt}} is put on the save
stack before a new level of grouping is opened.
In order to prevent a lot of trouble with the save stack,
\IniTeX\ does not allow dumping a format inside a group.
The \cs{end} command is allowed to occur inside a group,
but \TeX\ will give a diagnostic message about this.
The \cs{aftergroup} control sequence saves a token for
insertion after the current group. Several tokens can be
set aside by this command, and they are inserted in the left-to-right
order in which they were stated.
This is treated in Chapter~\ref{expand}.
%\point[global:assign] Local and global assignments
\section{Local and global assignments}
\label{global:assign}
An assignment or macro definition
is usually made global by prefixing it with \csidx{global},
\term statements !local\par\term statements !global\par
\term local statements\par\term global statements\par
but non-zero values of the \gr{integer parameter}
\csidx{globaldefs} override \cs{global}
specifications: if \cs{globaldefs} is positive every assignment
is implicitly prefixed with \cs{global}, and if
\cs{globaldefs} is negative, \cs{global} is
ignored. Ordinarily this parameter is zero.
Some assignment are always global: the \gr{global assignment}s are
\begin{description}%\FlushRight:no
\item [\gr{font assignment}]
assignments involving \cs{fontdimen}, \cs{hyphenchar},
and \cs{skew\-char}.
\item [\gr{hyphenation assignment}]
\cs{hyphenation} and \cs{patterns} commands
(see Chapter~\ref{line:break}).
\item [\gr{box size assignment}]
altering box dimensions with \cs{ht}, \cs{dp}, and~\cs{wd}
(see Chapter~\ref{boxes}).
\item [\gr{interaction mode assignment}]
run modes for a \TeX\ job (see Chapter~\ref{run}).
\item [\gr{intimate assignment}]
assignments to a \gr{special integer} or \gr{special dimen};
see %Chapters \ref{number} and~\ref{glue}.
pages \pageref{special:int:list} and~\pageref{special:dimen:list}.
\end{description}
%\point Group delimiters
\section{Group delimiters}
A group can be delimited by character tokens of category code~1
\term delimiter! group\par
for `beginning of group' and code~2 for `end of group', or
control sequence tokens that are \cs{let} to such characters,
the \cs{bgroup} and \cs{egroup} in plain \TeX.
Implicit and explicit braces can match to delimit
a group.
Groups can also be delimited by \csidx{begingroup} and
\csidx{endgroup}. These two control sequences must
be used together: they cannot be matched with implicit
or explicit braces, nor can they function as the braces
surrounding, for instance, boxed material.
Delimiting with \cs{begingroup} and \cs{endgroup} can
\label{begin:end:macros}%
provide a limited form of run-time error checking.
In between these two group delimiters an excess
open or close brace would result in
\begin{verbatim}
\begingroup ... } ... \endgroup
\end{verbatim}
or
\begin{verbatim}
\begingroup ... { ... \endgroup
\end{verbatim}
In both cases \TeX\ gives an error message about improper
balancing. Using \cs{bgroup} and \cs{egroup} here would
make an error much harder to find, because of the incorrect
matching that would occur. This idea is used in the environment
macros of several formats.
The choice of the brace characters for the beginning and end of group
characters is not hard-wired in \TeX. It is arranged
\cstoidx bgroup\par\cstoidx egroup\par
like this in the plain format:
\begin{verbatim}
\catcode`\{=1 % left brace is begin-group character
\catcode`\}=2 % right brace is end-group character
\end{verbatim}
Implicit braces have also been defined in the plain format:
\begin{verbatim}
\let\bgroup={ \let\egroup=}
\end{verbatim}
Special cases are the following:
\begin{itemize} \item The replacement text of a macro must be enclosed
in explicit beginning and end of group character tokens.
\item The open and close braces for boxes, \cs{vadjust},
and \cs{insert} can be implicit. This makes it possible
to define, for instance
\begin{verbatim}
\def\openbox#1{\setbox#1=\hbox\bgroup}
\def\closebox#1{\egroup\box#1}
\openbox{15}Foo bar\closebox{15}
\end{verbatim}
\item The right-hand side of a token list assignment and the
argument of the commands \cs{write}, \cs{message}, \cs{errmessage},
\cs{uppercase}, \cs{lowercase},
\cs{special}, and \cs{mark} is a \gr{general text}, defined
as
\begin{Disp} \gr{general text} $\longrightarrow$ \gr{filler}\lb
\gr{balanced text}\gr{right brace}\end{Disp}
meaning that the left brace can be implicit, but the closing
right brace must be an explicit character token with category
code~2. \end{itemize}
In cases where an implicit left brace suffices, and where
expansion is not explicitly inhibited, \TeX\ will
expand tokens until a left brace is encountered. This
is the basis for such constructs as
\verb=\uppercase\expandafter{\romannumeral80}=,
which in this unexpanded form do not adhere to the
syntax. If the first unexpandable token is not a left
brace \TeX\ gives an error message.
The grammar of \TeX\ (see Chapter~\ref{gramm}) uses
\gr{left brace} and \gr{right brace} for explicit
characters, that is, character tokens,
and \n{\lb} and~\n{\rb}
for possibly implicit characters,
\altt
that is, control sequences that have been \cs{let} to such
explicit characters.
%\point More about braces
\section{More about braces}
%\spoint Brace counters
\subsection{Brace counters}
\TeX\ has two counters for keeping track of grouping levels:
\term braces\par
the {\it master counter} and the {\it balance counter}.
Both of these counters are syntactic counters: they count the
explicit brace character tokens, but are not affected by implicit
braces (such as \cs{bgroup}) that are semantically equivalent
to an explicit brace.
The balance counter handles braces in all cases except in
alignment. Its workings are intuitively clear: it goes up
by one for every opening and down for every closing
brace that is not being skipped. Thus
\begin{verbatim}
\iffalse{\fi
\end{verbatim}
increases the balance counter if
this statement is merely scanned (for instance if it
appears in a macro definition text); if this statement
is executed the brace is skipped, so there is no effect on
the balance counter.
The master counter is more tricky;
it is used in alignments instead of the balance counter.
This counter records all braces, even when they are skipped
such as in \verb>\iffalse{\fi>.
For this counter uncounted skipped braces are still possible:
the alphabetic constants \n{`\lb} and \n{`\rb} have
no effect on this counter when they are
use by the execution processor as a~\gr{number};
they do affect this counter when they are seen by the
input processor (which merely sees characters, and not
the context).
%\spoint The brace as a token
\subsection{The brace as a token}
Explicit braces are character tokens, and as such they are
unexpandable. This implies that they survive until the
last stages of \TeX\ processing. For example,
\begin{verbatim}
\count255=1{2}
\end{verbatim}
will assign~1 to \cs{count255},
and print~`2', because the
opening brace functions as a delimiter for the number~1.
Similarly \begin{verbatim}
f{f}
\end{verbatim}
will prevent \TeX\ from forming
an `\hbox{ff}' ligature.
From the fact that braces are unexpandable,
it follows that their nesting is independent
of the nesting of conditionals. For instance
\begin{verbatim}
\iftrue{\else}\fi
\end{verbatim}
will give an open brace,
as conditionals are handled by expansion. The closing
brace is simply skipped as part of the \gr{false text};
any consequences it has for grouping only come into
play in a later stage of \TeX\ processing.
Undelimited macro arguments are either single tokens
or groups of tokens enclosed in explicit braces.
Thus it is not possible for an explicit open or close brace
to be a macro argument. However, braces can be assigned
with \cs{let}, for instance as in \begin{verbatim}
\let\bgroup={
\end{verbatim}
This is used in the plain \cs{footnote} macro
(see page~\pageref{footnote:ex}).
%\spoint \csc{\char 123} and \csc{\char 125}
\subsection{Open and closing brace control symbols}
% \csc{\char 123} and \csc{\char 125}}
The control sequences \verb-\{- and \verb-\}- do not really belong
\cstoidx\char123\par\cstoidx\char125\par
in this chapter, not being concerned with grouping.
They have been defined with \cs{let} as synonyms of
\cs{lbrace} and \cs{rbrace} respectively,
and these control sequences are \cs{delimiter} instructions
(see Chapter~\ref{mathchar}).
The Computer Modern Roman font has no braces, but there are
braces in the typewriter font, and for mathematics
there are braces of different sizes \ldash and extendable ones \rdash in
the extension font.
%%%% end of input file [group]
%\InputFile:macro
%%%% this is input file [macro]
%\subject[macro] Macros
\endofchapter
\chapter{Macros}\label{macro}
Macros are \TeX's abbreviation mechanism for sequences of commands
that are needed more than once,
somewhat like procedures in ordinary programming languages.
\TeX's parameter mechanism, however, is quite unusual.
This chapter explains how \TeX\ macros work. It also
treats the commands \cs{let} and~\cs{futurelet}.
\begin{inventory}
\item [\cs{def}]
Start a macro definition.
\item [\cs{gdef}]
Synonym for \verb-\global\def-.
\item [\cs{edef}]
Start a macro definition;
the replacement text is expanded at definition time.
This command is treated also in the next chapter.
\item [\cs{xdef}]
Synonym for \verb-\global\edef-.
\item [\cs{csname}]
Start forming the name of a control sequence.
\item [\cs{endcsname}]
Stop forming the name of a control sequence.
\item [\cs{global}]
Make the next definition, arithmetic statement,
or assignment global.
\item [\cs{outer}]
Prefix indicating that the macro being defined
can be used on the `outer' level only.
\item [\cs{long}]
Prefix indicating that the arguments of the macro being defined
may contain \cs{par} tokens.
\item [\cs{let}]
Define a control sequence to be equivalent to the next token.
\item [\cs{futurelet}]
Define a control sequence to be equivalent to
the token after the next token.
\end{inventory}
%\point Introduction
\section{Introduction}
A macro is basically a sequence of tokens that has
\term macro\par
been abbreviated into a control sequence.
Statements starting with (among others) \cs{def}
are called {\italic macro definitions}\alt, and
writing \begin{verbatim}
\def\abc{\de f\g}
\end{verbatim}
defines the macro \cs{abc},
with the {\italic replacement text\/} \verb>\de f\g>.
Macros can be used in this way to abbreviate
pieces of text or sequences of commands
that have to be given more than once.
Any time that \TeX's expansion processor
encounters the control sequence \cs{abc},
it replaces it by the replacement text.
If a macro should be sensitive to the context
where it is used, it can be defined with parameters:
\begin{verbatim}
\def\PickTwo#1#2{(#1,#2)}
\end{verbatim}
takes two arguments and reproduces them in parentheses.
The call \cs{PickTwo 12} gives `(1,2)'.
The activity of substituting the replacement text
for a macro is called {\italic macro expansion}.
%\point Layout of a macro definition
\section{Layout of a macro definition}
A macro definition consists of, in sequence,
\term definition !macro\par
\begin{enumerate} \item any number of \cs{global},
\cs{long}, and \cs{outer} prefixes,
\item a \gr{def} control sequence, or anything
that has been \cs{let} to one,
\item a control sequence or active character to be defined,
\item possibly a \gr{parameter text} specifying among other things
how many parameters the macro has, and
\item a replacement text enclosed in explicit character tokens
with category codes 1 and~2, by default \verb-{- and~\verb-}-
in plain \TeX.
\end{enumerate}
After a macro definition is completed, any saved \cs{afterassignment}
token (see section~\ref{sec:afterassignment}) is inserted.
The `expanding' definitions \cs{edef} and \cs{xdef}
are treated in Chapter~\ref{expand}.
%\point Prefixes
\section{Prefixes}
There are three prefixes that alter the status of the
\term prefixes !macro\par
macro definition: \begin{description}
\item [\csidx{global}]
If the definition occurs inside a group, this prefix
makes the definition global.
This prefix can also be used for assignments other than
macro definitions; in fact,
for macro definitions abbreviations exist obviating the
use of \cs{global}:
\begin{disp}\verb>\gdef\foo...>\quad is equivalent to\quad \verb>\global\def\foo...>
\end{disp} and
\begin{disp}\verb>\xdef\foo...>\quad is equivalent to\quad \verb>\global\edef\foo...>
\end{disp}
If the parameter \cs{globaldefs}
is positive, all assignments are
implicitly global;
if \cs{globaldefs} is negative any \cs{global} prefixes are
ignored,
and \cs{gdef} and \cs{xdef} make local definitions
(see Chapter~\ref{group}).
\item [\cs{outer}]
The mechanism of `outer' macros is supposed to facilitate
\term macro !outer\par\cstoidx outer\par
locating (among other errors) unbalanced braces: an \cs{outer}
macro is supposed
to appear only in non-embedded contexts.
To be precise, it is not allowed to occur
\begin{itemize}
\item in macro replacement texts (but it can appear in
for instance \cs{edef} after
\cs{noexpand}, and after \cs{meaning}),
\item in parameter texts,
\item in skipped conditional text,
\item in alignment preambles, and
\item in the \gram{balanced text} of a \cs{message}, \cs{write},
et cetera. \end{itemize}
For certain applications, however, it is inconvenient
that some of the plain macros are outer,
in particular macros such as \cs{newskip}. One remedy is to
redefine them, without the `outer' option, which
is done for instance in \LaTeX, but cleverer tricks are possible.
\item [\cs{long}]
Ordinarily, macro parameters are not supposed to contain
\cstoidx long\par
\cs{par} tokens. This restriction is useful (much more so
than the \cs{outer} definitions) in locating
forgotten closing braces.
For example, \TeX\ will complain about a `runaway argument'
\message{Example on}
in the following sequence:\begin{verbatim}
\def\a#1{ ... #1 ... }
\a {This sentence should be in braces.
And this is not supposed to be part of the argument
\end{verbatim}
\message{one page}
The empty line generates a \cs{par}, which most of the times
means that a closing brace has been forgotten.
If arguments to a particular macro should be allowed
to contain \cs{par} tokens, then the macro must be declared
to be \cs{long}. \end{description}
The \cs{ifx} test for equality of tokens
(see Chapter~\ref{if}) takes prefixes into
account when testing whether two tokens have the same definition.
\begin{comment}
With a little ingenuity it is possible
for \cs{par} tokens to sneak into macro arguments anyway.
Consider the example
\begin{verbatim}
\def\a#1\par!{ ... }
\a bc\par ef\par!
\end{verbatim}
Here the macro \cs{a} is not \cs{long}, but the argument
is \verb>bc\par ef>, which contains a \cs{par} token.
However,
this is of no importance in general.
\end{comment}
%\point The definition type
\section{The definition type}
There are four \gr{def} control sequences in \TeX:
\csidx{def}, \csidx{gdef}, \csidx{edef}, and \csidx{xdef}.
The control sequence
\alt
\cs{gdef} is a synonym for \verb>\global\def> and
\cs{xdef} is a synonym for \verb>\global\edef>.
The `expanding definition' \cs{edef} is treated in
Chapter~\ref{expand}.
The difference between the various types of macro definitions
is only relevant at the time of the definition.
When a macro is called there is no way of telling how
it was defined.
%\point[param:text] The parameter text
\section{The parameter text}
\label{param:text}
Between the control sequence or active character to be defined
\term parameter\par\term argument\par
and the opening brace of the replacement text, a \gr{parameter
text} can occur. This specifies whether the macro has parameters,
how many, and how they are delimited.
The \gr{parameter text} cannot contain
explicit braces.
A macro can have at most nine parameters.
A~parameter is indicated by a parameter token,
consisting of a macro parameter character
(that is, a character of category code~6, in plain \TeX~\verb=#=)
followed by a digit~\n1--\n9.
For instance, \verb>#6>~denotes the sixth parameter of a macro.
Parameter tokens cannot appear outside the context
of a macro definition.
In the parameter text,
parameters must be numbered consecutively, starting at~1.
A~space after a parameter token is significant,
both in the parameter text and the replacement text.
Parameters can be delimited or undelimited. A~parameter
is called undelimited if it is followed immediately
by another parameter in the \gr{parameter text}
or by the opening brace of the replacement text;
it is called delimited if it is followed by any other token.
The tokens (zero or more) that are substituted for
a parameter when a macro is expanded (or `called')
are called
the `argument' corresponding to that parameter.
%\spoint Undelimited parameters
\subsection{Undelimited parameters}
When a macro with an undelimited parameter, for instance
\term parameter !undelimited\par
a macro \cs{foo} with one parameter
\begin{verbatim}
\def\foo#1{ ... #1 ...}
\end{verbatim}
is expanded, \TeX\ scans ahead (without expanding)
until a non-blank token is found.
If this token is not an explicit \gr{left brace},
it is taken to be the argument
corresponding to the parameter. Otherwise a \gr{balanced text}
is absorbed by scanning until the matching explicit
\gr{right brace} has been found.
This balanced text then
constitutes the argument.
An example with three undelimited parameters follows: with
\begin{verbatim}
\def\foo#1#2#3{#1(#2)#3}
\end{verbatim}
the macro call \cs{foo123} gives `\hbox{1(2)3}';
but \hbox{\verb-\foo 1 2 3-} also gives the same result.
In the call
\begin{disp}\cs{foo}\n{\char32 1\char32 2\char 32 3}\end{disp}
the first space is skipped in the input processor of \TeX.
The argument corresponding to the first parameter is then
the~\n1. In order to find the second parameter \TeX\ then
skips all blanks, in this case exactly one. As second
parameter \TeX\ finds then the~\n2. Similarly the third
parameter is~\n3.
In order to pass several tokens as one undelimited argument
one can use braces. With the above definition of \cs{foo}
the call \verb>\foo a{bc}d> gives `\hbox{a(bc)d}'.
When the argument of a macro is a balanced text instead of
a single token, the delimiting braces are not inserted when
the argument is
inserted in the replacement text.
For example:\begin{verbatim}
\def\foo#1{\count0=1#1\relax}
\foo{23}
\end{verbatim}
will expand to \verb>\count0=123\relax>,
which assigns the value of 123 to the counter.
On the other hand, the statement \begin{verbatim}
\count0=1{23}
\end{verbatim}
would
assign~1 and print~23.
%\spoint Delimited parameters
\subsection{Delimited parameters}
Apart from enclosing it in braces there is another way
\term parameter !delimited\par
to pass a sequence of tokens as a single argument to a macro,
namely by using delimited parameters.
Any non-parameter tokens in the \gr{parameter text} occurring
after a macro parameter (that is, after the parameter number
following the parameter character)
act as a delimiter for that parameter. This includes space tokens:
a space after a parameter number is significant.
Delimiting tokens can also occur between the control
sequence being defined and the first parameter token~\verb>#1>.
Character tokens acting as delimiters in the parameter text
have both their character code and
category code stored; the delimiting character tokens of the
actual arguments have to match both.
Category codes of such characters may include some that
can normally only appear in special contexts; for instance, after
the definition \begin{verbatim}
\def\foo#1_#2^{...}
\end{verbatim}
the macro \cs{foo}
can be used outside math mode.
When looking for the argument corresponding to
a delimited parameter, \TeX\ absorbs all tokens without expansion (but
balancing braces) until the
(exact sequence of) delimiting tokens is encountered.
The delimiting tokens are not part of the argument;
they are removed from the input stream during the macro call.
%\spoint Examples with delimited arguments
\subsection{Examples with delimited arguments}
As a simple example, \begin{verbatim}
\def\DoASentence#1#2.{{#1#2.}}
\end{verbatim}
defines a macro with an undelimited first parameter,
and a second parameter delimited by a period.
In the call\begin{verbatim}
\DoASentence \bf This sentence is the argument.
\end{verbatim}
the arguments are:
\begin{verbatim}
#1<-\bf
#2<-This sentence is the argument
\end{verbatim}
Note that the closing period is not in the argument, but it has
been absorbed; it is no longer in the input stream.
A~commonly used delimiter is \cs{par}:
\begin{verbatim}
\def\section#1. #2\par{\medskip\noindent {\bf#1. #2\par}}
\end{verbatim}
This macro has a first parameter that is delimited by~`\n{.\char32}',
and a second parameter that is delimited by \cs{par}.
The call\message{example on one page}
\begin{verbatim}
\section 2.5. Some title
The text of the section...
\end{verbatim}
will give
\begin{disp}\verb>#1<-2.5>\nl
\verb>#2<-Some title>\n{\char32}\end{disp}
Note that there is a space at the end of the second argument
generated by the line end. If this space is unwanted one might
define \begin{verbatim}
\def\section#1. #2 \par{...}
\end{verbatim}
with \n{\char32}\cs{par} delimiting the second
argument. This approach, however,
precludes the user's writing the \cs{par} explicitly:
\begin{verbatim}
\section 2.5 Some title\par
\end{verbatim}
One way out of this dilemma is to write
\verb>#2\unskip> on all places in the definition text
where the trailing space would be unwanted.
Control sequences acting as delimiters need not be defined,
as they are absorbed without expansion. Thus
\begin{verbatim}
\def\control#1\sequence{...}
\end{verbatim}
is a useful
definition, even if \cs{sequence} is undefined.
The importance of category codes in delimited arguments
is shown by the following example:
\begin{verbatim}
\def\a#1 #2.{ ... }
\catcode`\ =12
\a b c
d.
\end{verbatim}
which gives
\begin{verbatim}
\a #1 #2.-> ...
#1<- b c
#2<-d
\end{verbatim}
Explanation: the delimiter between parameters 1 and~2 is a space
of category~10. In between \n{a} and \n{b} there is a space
of category~12; the first space of category~10
is the space that is generated by the line end.
For a `real-life' application of matching of category codes,
see the explanation of \cs{newif} in Chapter~\ref{if},
and the example on page~\pageref{ex:jobnumber}.
%\spoint Empty arguments
\subsection{Empty arguments}
If the user specifies a \gr{balanced text} in braces
when \TeX\ expects a macro
argument, that text is used as the argument.
Thus, specifying \verb-{}- will give an argument that is
an empty list of tokens; this is called an `empty argument'.
Empty arguments can also arise from the use of delimited
parameters. For example, after the definition
\begin{verbatim}
\def\mac#1\ro{ ... }
\end{verbatim}
the call
\begin{verbatim}
\mac\ro
\end{verbatim}
will give an empty argument.
\begin{comment}
However, only
one empty argument can be created this way:
if the macro had been defined as
\begin{verbatim}
\def\mac#1#2\ro{ ... }
\end{verbatim}
the same call
\begin{verbatim}
\mac\ro \othermacro \stillothermacro
\end{verbatim}
will probably cause a `\n{Runaway argument?}' error message.
Explanation: the first parameter is undelimited, so the corresponding
argument is `\cs{ro}'; after that \TeX\ starts looking for a list
of tokens delimited by~\cs{ro}.
\end{comment}
%\spoint The macro parameter character
\subsection{The macro parameter character}
When \TeX's input processor scans a macro definition text,
\term character !parameter\par
it inserts a parameter token for any
occurrence of a macro parameter character followed by a digit.
In effect, a parameter token in the replacement text
states `insert parameter number such and such here'.
Two parameter characters in a row are replaced by a single one.
The latter fact can be used for nested macro definitions.
\label{nest:def}\howto Nested macro definitions\par
Thus \begin{verbatim}
\def\a{\def\b#1{...}}
\end{verbatim}
gives an error message
because \cs{a} was defined without parameters, and
yet there is a parameter token in its replacement text.
The following
\begin{verbatim}
\def\a#1{\def\b#1{...}}
\end{verbatim}
defines a macro \cs{a} that
defines a macro \cs{b}. However, \cs{b} still does not
have any parameters: the call
\begin{verbatim}
\a z
\end{verbatim}
defines a macro \cs{b} without parameters,
that has to be followed by a~\n z.
Note that this
does not attempt to define a macro \cs{bz}, because the
control sequence \cs{b} has already been formed in \TeX's
input processor when that input line was read.
Finally,
\begin{verbatim}
\def\a{\def\b##1{...}}
\end{verbatim}
defines a macro \cs{b}
with one parameter.
Let us examine the handling of the parameter character
in some detail.
Consider \begin{verbatim}
\def\a#1{ .. #1 .. \def\b##1{ ... }}
\end{verbatim}
When this is read as input, the input processor
\begin{itemize}
\item replaces the characters \verb>#1> by \gr{parameter token$_1$}, and
\item replaces the characters \verb>##> by \verb>#>\end{itemize}
A macro call of \cs{a} will then let the input processor scan
\begin{verbatim}
\def\b#1{ ... }
\end{verbatim}
in which the two characters \verb>#1> are
\alt
replaced by a parameter token.
%\spoint Brace delimiting
\subsection{Brace delimiting}
Ordinarily, it is not possible to have left or right
braces in the \gr{parameter text} of a definition.
There is a special mechanism, however, that can make
the last parameter of a macro act as if it is delimited
by an opening brace.
If the last parameter token
is followed by a parameter character (\verb>#>),
which in turn is followed by the opening brace of the
replacement text, \TeX\ makes the last parameter
be delimited by a beginning-of-group character.
Furthermore, unlike other delimiting tokens in
parameter texts, this opening brace is not
removed from the input stream.
Consider an example.
Suppose we want to have a macro
\cs{every} that can fill token lists as follows:
\begin{verbatim}
\every par{abc} \every display{def}
\end{verbatim}
This macro can be defined as
\begin{verbatim}
\def\every#1#{\csname every#1\endcsname}
\end{verbatim}
In the first call above, the argument corresponding to
the parameter is \n{abc}, so the call
expands to
\begin{verbatim}
\csname everypar\endcsname{abc}
\end{verbatim}
which gives the desired result.
%\point[cs:name] Construction of control sequences
\section{Construction of control sequences}
\label{cs:name}
The commands \csidx{csname} and \csidx{endcsname} can be used
to construct a control sequence.
For instance \begin{verbatim}
\csname hskip\endcsname 5pt
\end{verbatim}
is equivalent to \verb=\hskip5pt=.
During this construction process
all macros and other expandable control sequences
between \cs{csname} and \cs{endcsname}
are expanded as usual, until only unexpandable
character tokens remain. A~variation of the above example,
\begin{verbatim}
\csname \ifhmode h\else v\fi skip\endcsname 5pt
\end{verbatim}
performs an \cs{hskip} or \cs{vskip} depending on the mode.
The final result of the expansion should
consist of only character tokens, but
their category codes do not matter.
An unexpandable control sequence gives an error here:
\TeX\ will insert an \cs{endcsname} right before it
as an attempt at error recovery.
With \cs{csname} it is possible to construct
control sequences that cannot ordinarily be written,
because the constituent character tokens may have another category
\alt
than~11, letter. This principle can be used to hide
\howto Hide counters from the user\par
inner control sequences of a macro package from the user.
\begin{example}\begin{verbatim}
\def\newcounter#1{\expandafter\newcount
\csname #1:counter\endcsname}
\def\stepcounter#1{\expandafter\advance
\csname #1:counter\endcsname 1\relax}
\end{verbatim}
In the second definition the \cs{expandafter} is superfluous,
but it does no harm, and it is conceptually clearer.
\end{example}
The name of the actual counter created by \cs{newcounter}
contains a colon, so that it takes some effort to write this
control sequence. In effect, the counter
is now hidden from the user, who can only
access it through control sequences such as \cs{stepcounter}.
By the way, the macro \cs{newcount} is defined \cs{outer} in
the plain format, so the above definition of \cs{newcounter}
can only be written after \cs{newcount} has been redefined.
If a control sequence formed with \verb>\csname...\endcsname>
has not been defined
before, its meaning is set to \cs{relax}.
Thus if \verb=\xx= is an undefined control sequence, the
command \begin{verbatim}
\csname xx\endcsname
\end{verbatim}
will {\em not\/}
give an error message, as it is equivalent to \verb=\relax=.
Moreover, after this execution of the
\verb-\csname...\endcsname- statement, the control sequence
\verb=\xx= is itself equivalent to \cs{relax}, so it
will no longer give an `undefined control sequence' error
(see also page~\pageref{relax:cs}).
%\point Token assignments by \cs{let} and \cs{futurelet}
\section{Token assignments by \protect\cs{let} and \protect\cs{futurelet}}
There are two \gr{let assignment}s in \TeX.
Their syntax is
\begin{disp}\cs{let}\gr{control sequence}\gr{equals}%
\gr{one optional space}\gr{token}\nl
\cs{futurelet}\gr{control sequence}\gr{token}\gr{token}
\end{disp}
In the syntax of a \cs{futurelet} assignment
no optional equals sign appears.
%\spoint[let] \cs{let}
\subsection{\protect\cs{let}}
\label{let}
The primitive command \csidx{let} assigns the current meaning
of a~token to a control sequence or active character.
For instance, in the plain format \cs{endgraf} is defined
as \begin{verbatim}
\let\endgraf=\par
\end{verbatim}
This enables macro writers to redefine \cs{par}, while
still having the functionality of the primitive \cs{par}
command available. For example,
\begin{verbatim}
\everypar={\bgroup\it\def\par{\endgraf\egroup}}
\end{verbatim}
The case where the \gr{token} to be assigned is not a control
sequence but a character token instead has been treated
in Chapter~\ref{char}.
%\spoint \cs{futurelet}
\subsection{\protect\cs{futurelet}}
As was explained above, the sequence with \cs{let}
\begin{disp}\cs{let}\gr{control sequence}\gr{token$_1$}\gr{token$_2$}%
\gr{token$_3$}\gr{token$\cdots$}\end{disp}
assigns (the meaning of) \gr{token$_1$} to the control sequence,
and the remaining input stream looks like
\begin{disp}\gr{token$_2$}\gr{token$_3$}\gr{token$\cdots$}\end{disp}
That is, the \gr{token$_1$} has disappeared from the stream.
The command \csidx{futurelet} works slightly differently:
given the input stream
\begin{disp}\cs{futurelet}\gr{control sequence}\gr{token$_1$}\gr{token$_2$}%
\gr{token$_3$}\gr{token$\cdots$}\end{disp}
it assigns (the meaning of) \gr{token$_2$} to the control sequence,
and the remaining stream looks like
\begin{disp}\gr{token$_1$}\gr{token$_2$}\gr{token$_3$}\gr{token$\cdots$}\end{disp}
That is, neither \gr{token$_1$} nor \gr{token$_2$} has
been lifted from the stream.
However, now \gr{token$_1$}
`knows' what \gr{token$_2$} is, without having had to absorb it
as a macro parameter. See an example below.
If a character token has been \cs{futurelet} to a control
sequence, its category code is fixed.
The subsequent \gr{token$_1$} cannot change
it anymore.
%\point Assorted remarks
\section{Assorted remarks}
%\spoint Active characters
\subsection{Active characters}
Character tokens of category~13, `active characters',
\altt
can be defined just like
\term active character\par\term character !active\par
control sequences.
If the definition of the character appears inside a macro,
the character has to be active at the time of the definition
of that macro.
Consider for example the following definition
(taken from Chapter~\ref{mouth}):\begin{verbatim}
{\catcode`\^^M=13 %
\gdef\obeylines{\catcode`\^^M=13 \def^^M{\par}}%
}
\end{verbatim}
The unusual category of the \verb>^^M> character
has to be set during the definition of \cs{obeylines},
otherwise \TeX\ would think that the line ended
after \cs{def}.
%\spoint Macros versus primitives
\subsection{Macros versus primitives}
The distinction between primitive commands and user macros
\term primitive commands\par\term command !primitive\par
is not nearly as important in \TeX\ as it is in other
programming languages.\begin{itemize}
\item The user can use primitive commands under different names:
\begin{verbatim}
\let\StopThisParagraph=\par
\end{verbatim}
\item Names of primitive commands can be used for
user macros: \begin{verbatim}
\def\par{\hfill$\bullet$\endgraf}
\end{verbatim}
\item Both user macros and a number of \TeX\ primitives
are subject to expansion, for instance all conditionals,
and commands such as \cs{number} and~\cs{jobname}.
\end{itemize}
%\spoint Tail recursion
\subsection{Tail recursion}
Macros in \TeX, like procedures in most modern programming
\term recursion\par
languages, are allowed to be recursive: that is, the
definition of a macro can contain a call to this same macro,
or to another macro that will call this macro.
Recursive macros tend to clutter up \TeX's memory
if too many `incarnations' of such a macro are active
at the same time. However, \TeX\ is able to prevent this
in one frequently occurring case of recursion: tail recursion.
In order to appreciate what goes on here, some background
knowledge is needed. When \TeX\ starts executing a macro
it absorbs the parameters, and places an item pointing to
the replacement text on the input stack,
\term input! stack\par
so that the scanner will next be directed to
this replacement. Once it has been processed, the item on the
input stack can be removed.
However, if the definition text
of a macro contains further macros, this process will be
repeated for them: new items may be placed on the input stack
directing the scanner to other macros
even before the first one has been completed.
In general this `stack build-up' is a necessary evil, but
it can be prevented if the nested macro call is the
{\em last\/} token in the replacement text of the original
macro. After the last token no further tokens need to be
considered, so one might as well clear the top item
from the input stack
before a new one is put there.
This is what \TeX\ does.
The \csidx{loop} macro of plain \TeX\ provides a good illustration
\label{loop:ex}
of this principle. The definition is
\begin{verbatim}
\def\loop#1\repeat{\def\body{#1}\iterate}
\def\iterate{\body \let\next=\iterate
\else \let\next=\relax\fi \next}
\end{verbatim}
and this macro can be called for example as follows:
\begin{verbatim}
\loop \message{\number\MyCount}
\advance\MyCount by 1
\ifnum\MyCount<100 \repeat
\end{verbatim}
The macro \cs{iterate} can call itself and, when it does so,
the recursive call is performed by the last token in the list.
It would have been possible to define \cs{iterate}
as \begin{verbatim}
\def\iterate{\body \iterate\fi}
\end{verbatim}
but then \TeX\ would not have been able to resolve the recursion
as the call \cs{iterate} is not the last token in the replacement
text of \cs{iterate}. Assigning \verb>\let\next=\iterate>
is here a way to let
the recursive call be the last token in the list.
Another way of resolving tail recursion is to use
\cs{expandafter} (see page~\pageref{after:cond}): in
\begin{verbatim}
\def\iterate{\body \expandafter\iterate\fi}
\end{verbatim}
it removes the \cs{fi} token.
Tail recursion would also be resolved if the last
tokens in the list were arguments for the
recursive macro.
An aside: by defining \cs{iterate} as
\begin{verbatim}
\def\iterate{\let\next\relax
\body \let\next\iterate \fi \next}
\end{verbatim}
it becomes possible to write
\begin{verbatim}
\loop ... \if... ... \else ... \repeat
\end{verbatim}
%\point Macro techniques
\section{Macro techniques}
%\spoint Unknown number of arguments
\subsection{Unknown number of arguments}
In some applications,
\howto Macros with an undetermined number
of arguments\par
a macro is needed that can have a
number of arguments that is not specified in advance.
Consider the problem of translating a position on a chess board
(for full macros and fonts, see~\cite{chess} and~\cite{Tut}),
given like
\begin{verbatim}
\White(Ke1,Qd1,Na1,e2,f4)
\end{verbatim}
to a sequence of typesetting instructions
\begin{verbatim}
\WhitePiece{K}{e1} \WhitePiece{Q}{d1} \WhitePiece{N}{a1}
\WhitePiece{P}{e2} \WhitePiece{P}{f4}
\end{verbatim}
Note that for pawns the `P' is omitted in the list of positions.
The first problem is that the list of pieces
is of variable length, so we append a terminator piece:
\begin{verbatim}
\def\White(#1){\xWhite#1,xxx,}
\def\endpiece{xxx}
\end{verbatim}
for which we can test.
Next, the macro \cs{xWhite} takes one position from the list,
tests whether it is the terminator, and if not,
subjects it to a test to see whether it is a pawn.
\begin{verbatim}
\def\xWhite#1,{\def\temp{#1}%
\ifx\temp\endpiece
\else \WhitePieceOrPawn#1XY%
\expandafter\xWhite
\fi}
\end{verbatim}
An \cs{expandafter} command is necessary to remove the
\cs{fi} (see page~\pageref{after:cond}), so that
\cs{xWhite} will get the next position as argument
instead of \cs{fi}.
Positions are either two or three characters long.
The call to \cs{White\-Piece\-OrPawn}, a four-parameter macro,
appended a terminator string \n{XY}.
In the case of a pawn, therefore, argument~3 is the character~\n X
and argument~4 is empty; for all other pieces argument~1
is the piece, 2~and~3 are the position, and argument~4 is~\n X.
\begin{verbatim}
\def\WhitePieceOrPawn#1#2#3#4Y{
\if#3X \WhitePiece{P}{#1#2}%
\else \WhitePiece{#1}{#2#3}\fi}
\end{verbatim}
%\spoint Examining the argument
\subsection{Examining the argument}
It may be necessary in some cases to test whether a macro
\howto Examine a macro argument for the presence of some element\par
\howto Apply \cs{uppercase} when the argument has a \cs{footnote}\par
argument contains some element. For a real-life example,
consider the following (see also the \cs{DisplayEquation}
\alt
example on page~\pageref{left:display}).
Suppose the title and author of an article are given as
\begin{verbatim}
\title{An angle trisector}
\author{A.B. Cee\footnote*{Research supported by the
Very Big Company of America}}
\end{verbatim}
with multiple authors
given as
\begin{verbatim}
\author{A.B. Cee\footnote*{Supported by NSF grant 1}
\and
X.Y. Zee\footnote{**}{Supported by NATO grant 2}}
\end{verbatim}
Suppose further that the \cs{title} and \cs{author} macros
are defined as
\begin{verbatim}
\def\title#1{\def\TheTitle{#1}} \def\author#1{\def\TheAuthor{#1}}
\end{verbatim}
which will be used as
\begin{verbatim}
\def\ArticleHeading{ ... \TheTitle ... \TheAuthor ... }
\end{verbatim}
For some journals it is required to
have the authorship and the title of the article in all capitals.
The implementation of this could be
\begin{verbatim}
\def\ArticleCapitalHeading
{ ...
\uppercase\expandafter{\TheTitle}
...
\uppercase\expandafter{\TheAuthor}
...
}
\end{verbatim}
Now the \cs{expandafter} commands will expand the title and
author into the actual texts, and the \cs{uppercase} commands
will capitalize them. However, for the authors this is wrong,
since the \cs{uppercase} command will also capitalize the
footnote texts.
The problem is then to uppercase only the parts
of the title in between the footnotes.
As a first attempt, let us take the case of one author, and
let the basic call be
\begin{verbatim}
\expandafter\UCnoFootnote\TheAuthor
\end{verbatim}
This expands into
\begin{verbatim}
\UCnoFootnote A.B. Cee\footnote*{Supported ... }
\end{verbatim}
The macro
\begin{verbatim}
\def\UCnoFootnote#1\footnote#2#3{\uppercase{#1}\footnote{#2}{#3}}
\end{verbatim}
will analyse this correctly:
\begin{verbatim}
#1<-A.B. Cee
#2<-*
#3<-Supported ...
\end{verbatim}
However, if there is no footnote, this macro is completely wrong.
As a first refinement we add a footnote ourselves, just to make
sure that one is present:
\begin{verbatim}
\expandafter\UCnoFootnote\TheAuthor\footnote 00
\end{verbatim}
Now we have to test what kind of footnote we find:
\begin{verbatim}
\def\stopper{0}
\def\UCnoFootnote#1\footnote#2#3{\uppercase{#1}\def\tester{#2}%
\ifx\stopper\tester
\else\footnote{#2}{#3}\fi}
\end{verbatim}
With \cs{ifx} we test the delimiter footnote sign against the
actual sign encountered. Note that a solution with
\begin{verbatim}
\ifx0#2
\end{verbatim}
would be wrong if the footnote sign consists
of more than one token, for instance~\verb>{**}>.
The macro so far is correct if there was no footnote,
but if there was one it is wrong:
the terminating tokens remain to be disposed of.
They are taken care of in the following version:
\begin{verbatim}
\def\stopper{0}
\def\UCnoFootnote#1\footnote#2#3{\uppercase{#1}\def\tester{#2}%
\ifx\stopper\tester
\else\footnote{#2}{#3}\expandafter\UCnoFootnote
\fi}
\end{verbatim}
A repeated call to \cs{UCnoFootnote} removes the delimiter tokens
(the \cs{expandafter} first removes the \cs{fi}),
and as an added bonus, this macro is also correct for multiple
authors.
%\spoint Optional macro parameters with \cs{futurelet}
\subsection{Optional macro parameters with \protect\cs{futurelet}}
One standard application of \cs{futurelet} is implementing
\howto Macros with optional parameters\par
optional parameters of macros. The general course of action
is as follows:
\begin{verbatim}
\def\Com{\futurelet\testchar\MaybeOptArgCom}
\def\MaybeOptArgCom{\ifx[\testchar \let\next\OptArgCom
\else \let\next\NoOptArgCom \fi \next}
\def\OptArgCom[#1]#2{ ... }\def\NoOptArgCom#1{ ... }
\end{verbatim}
Note that \cs{ifx} is used even though it tests
for a character. The reason is of course that,
if the optional argument is omitted, there might be an
expandable control sequence behind the~\cs{Com}.
The macro \cs{Com} now has one optional and one regular
argument; it can be called as
\begin{verbatim}
\Com{argument}
\end{verbatim}
or as\begin{verbatim}
\Com[optional]{argument}
\end{verbatim}
Often the call without the optional argument will insert some
default value:
\begin{disp}\verb>\def\NoOptArgCom#1{\OptArgCom[>%
{\italic default\/}\verb>]{#1}}>\end{disp}
This mechanism is widely used in formats such as \LaTeX\ and
\LamsTeX; see also~\cite{svb:future}.
%\spoint Two-step macros
\subsection{Two-step macros}
Often what looks to the user like one macro is in reality
a two-step process, where one macro will set up conditions,
and a second macro will do the work.
As an example, here is
a macro \cs{PickToEol}\label{pick:eol}
\howto Take an input line as macro argument\par
with an argument that is delimited by the line end.
First we write a macro without arguments that
changes the category code of the line end, and then
calls the second macro.
\begin{verbatim}
\def\PickToEol{\begingroup\catcode`\^^M=12 \xPickToEol}
\end{verbatim}
The second macro can then take as an argument everything
up to the end of the line:
\begin{verbatim}
\def\xPickToEol#1^^M{ ... #1 ... \endgroup}
\end{verbatim}
There is one problem with this definition: the \verb>^^M> character
should have category~12. We arrive at the following:
\begin{verbatim}
\def\PickToEol{\begingroup\catcode`\^^M=12 \xPickToEol}
{\catcode`\^^M=12 %
\gdef\xPickToEol#1^^M{ ... #1 ... \endgroup}%
}
\end{verbatim}
where the category code of \verb>^^M> is changed for the
sake of the definition of \cs{xPickToEol}. Note that
the \verb>^^M> in \cs{PickToEol} occurs in a control symbol,
so there the category code is irrelevant. Therefore that
definition can be outside the group where the category code
of \verb>^^M> is redefined.
%\spoint A comment environment
\subsection{ A comment environment}
As an application of the above idea of two-step macros,
\howto Comment environment\par
and in order to illustrate tail recursion, here are
macros for a `comment' environment.
Often it is necessary to remove a part of \TeX\
input temporarily. For this one would like to
write \begin{verbatim}
\comment
...
\endcomment
\end{verbatim}
The simplest implementation of this,
\begin{verbatim}
\def\comment#1\endcomment{}
\end{verbatim}
has a number of weaknesses. For instance,
it cannot cope with outer macros or input that
does not have balanced braces. Its worst
shortcoming, however, is that it reads the complete
comment text as a macro argument. This limits the size
of the comment to that of \TeX's input buffer.
It would be a better idea to take on the out-commented
text one line at a time. For this we want to write
a recursive macro with a basic structure
\begin{verbatim}
\def\comment#1^^M{ ... \comment }
\end{verbatim}
In order to be able to write this definition at all,
the category code of the line end must be changed; as above
\altt
we will have
\begin{verbatim}
\def\comment{\begingroup \catcode`\^^M=12 \xcomment}
{\catcode`\^^M=12 \endlinechar=-1 %
\gdef\xcomment#1^^M{ ... \xcomment}
}
\end{verbatim}
Changing the \cs{endlinechar} is merely to
prevent having to put comment characters at the end
of every line of the definition.
Of course, the process must stop at a certain time.
To this purpose we investigate the line that was
scooped up as macro argument:
\begin{verbatim}
{\catcode`\^^M=12 \endlinechar=-1 %
\gdef\xcomment#1^^M{\def\test{#1}
\ifx\test\endcomment \let\next=\endgroup
\else \let\next=\xcomment \fi
\next}
}
\end{verbatim}
and we have to define \cs{endcomment}:
\begin{verbatim}
\def\endcomment{\endcomment}
\end{verbatim}
This command will never be executed: it is merely for purposes
of testing whether the end of the environment has been reached.
We may want to comment out text that is not syntactically
correct. Therefore we switch to a verbatim mode
\term verbatim mode\par
when commenting. The following macro is given
in plain \TeX:
\begin{verbatim}
\def\dospecials{\do\ \do\\\do\{\do\}\do\$\do\&%
\do\#\do\^\do\^^K\do\_\do\^^A\do\%\do\~}
\end{verbatim}
We use it to define \cs{comment} as follows:
\begin{verbatim}
\def\makeinnocent#1{\catcode`#1=12 }
\def\comment{\begingroup
\let\do=\makeinnocent \dospecials
\endlinechar`\^^M \catcode`\^^M=12 \xcomment}
\end{verbatim}
Apart from the possibility mentioned above of commenting
out text that is not syntactically correct, for instance
because of unmatched braces, this solution can handle
outer macros. The former implementation of \cs{xcomment}
would cause a \TeX\ error if one occurred in the comment text.
However, using verbatim mode poses the problem of concluding the
environment.
\altt
The final line of the comment is now not the control sequence
\cs{endcomment}, but the characters constituting it. We have
to test for these then:
\begin{verbatim}
{\escapechar=-1
\xdef\endcomment{\string\\endcomment}
}
\end{verbatim}
The sequence \verb>\string\\> gives a backslash.
We could not have used
\begin{verbatim}
\edef\endcomment{\string\endcomment}
\end{verbatim}
because
the letters of the word \n{endcomment} would then have
category code~12, instead of the 11 that the ones on the
last line of the comment will have.
%%%% end of input file [macro]
%\InputFile:expand
%%%% this is input file [expand]
%\subject[expand] Expansion
\endofchapter
\chapter{Expansion}\label{expand}
Expansion in \TeX\ is rather different from procedure calls
\term expansion\par
in most programming languages. This chapter treats the
commands connected with expansion, and gives a number of
(non-trivial) examples.
\begin{inventory}
\item [\cs{relax}]
Do nothing.
\item [\cs{expandafter}]
Take the next two tokens and place the expansion of the
second after the first.
\item [\cs{noexpand}]
Do not expand the next token.
\item [\cs{edef}]
Start a macro definition;
the replacement text is expanded at definition time.
\item [\cs{aftergroup}]
Save the next token for insertion after the current group.
\item [\cs{afterassignment}]
Save the next token for execution after the next assignment
or macro definition.
\item [\cs{the}]
Expand the value of various quantities in \TeX\ into a string
of character tokens.
\end{inventory}
%\point Introduction
\section{Introduction}
\TeX's expansion processor accepts a stream of tokens
coming out of the input processor, and its result is
again a stream of tokens, which it feeds to the execution
processor. For the input processor there are two
kinds of tokens: expandable and unexpandable ones.
The latter category is passed untouched, and it contains
largely assignments and typesettable material;
the former category
is expanded, and the result of that expansion is examined anew.
%\point Ordinary expansion
\section{Ordinary expansion}
The following list gives those constructs
that are expanded, unless
expansion is inhibited:
\begin{itemize}
\item macros\label{expand:lijst}
\term expansion!expandable control sequences\par
\item conditionals
\item \cs{number}, \cs{romannumeral}
\item \cs{string}, \cs{fontname}, \cs{jobname},
\cs{meaning}, \cs{the}
\item \verb,\csname ... \endcsname,
\item \cs{expandafter}, \cs{noexpand}
\item \cs{topmark}, \cs{botmark}, \cs{firstmark},
\cs{splitfirstmark}, \cs{splitbotmark}
\item \cs{input}, \cs{endinput}
\end{itemize}
This is the list of all instances where
expansion is inhibited:
\begin{itemize}\label{noexp:list}
\item when \TeX\ is reading a token to be defined by
\begin{itemize} \item a \gr{let assignment}, that is,
by \cs{let} or \cs{futurelet},
\item a \gr{shorthand definition}, that is,
by \cs{chardef} or \cs{mathchardef}, or a
\gr{register def}, that is, \cs{countdef},
\cs{dimendef}, \cs{skipdef}, \cs{muskipdef},
or~\cs{toksdef},
\item a \gr{definition}, that is a macro definition
with \cs{def}, \cs{gdef}, \cs{edef}, or~\cs{xdef},
\item the \gr{simple assignment}s \cs{read} and \cs{font};
\end{itemize}
\item when a \gr{parameter text} or macro arguments
are being read; also when the replacement text of a
control sequence
being defined by \cs{def}, \cs{gdef}, or \cs{read}
is being read;
\item when the token list for a \gr{token variable} or
\cs{uppercase}, \cs{lowercase}, or \cs{write}
is being read; however, the token list for \cs{write}
will be expanded later when it is shipped out;
\item when tokens are being deleted during error recovery;
\item when part of a conditional is being skipped;
\item in two instances when \TeX\ has to know what follows
\begin{itemize}\item after a left quote in a context where
that is used to denote an integer (thus in
\verb-\catcode`\a- the \cs{a} is not expanded), or
\item after a math shift character that begins math mode
to see whether another math shift character follows (in which case
a display opens);
\end{itemize}
\item when an alignment preamble is being scanned; however,
in this case a~token
preceded by \cs{span} and the tokens in a \cs{tabskip}
assignment are still expanded.
\end{itemize}
%\point Reversing expansion order
\section{Reversing expansion order}
Every once in a while you need to change the normal order of
expansion of tokens. \TeX\ provides several mechanisms for
this. Some of the control sequences in this section are
not strictly concerned with expansion.
%\spoint One step expansion: \cs{expandafter}
\subsection{One step expansion: \protect\cs{expandafter}}
The most obvious tool for reversed expansion order is
\csidx{expandafter}. The sequence
\begin{disp}\cs{expandafter}\gr{token$_1$}\gr{token$_2$}\end{disp}
expands to \begin{disp}\gr{token$_1$}\gr{\italic the expansion of token$_2$}
\end{disp}
Note the following.
\begin{itemize} \item If \gr{token$_2$} is a macro, it is replaced
by its replacement text, not by its final expansion.
Thus, if
\begin{verbatim}
\def\tokentwo{\ifsomecondition this \else that \fi}
\def\tokenone#1{ ... }
\end{verbatim}
the call \begin{verbatim}
\expandafter\tokenone\tokentwo
\end{verbatim}
will give \cs{ifsomecondition} as the parameter
to \cs{tokenone}:
\begin{verbatim}
\tokenone #1-> ...
#1<-\ifsomecondition
\end{verbatim}
\item If the \cs{tokentwo} is a macro with one or more
parameters, sufficiently many subsequent tokens will be absorbed
to form the replacement text.\end{itemize}
%\spoint[expand:edef] Total expansion: \cs{edef}
\subsection{Total expansion: \protect\cs{edef}}
\label{expand:edef}
Macros are usually defined by \cs{def}, but for the cases where
one wants the replacement text to reflect current conditions
(as opposed to conditions at the time of the call),
there is an `expanding define', \csidx{edef}, which expands
everything in the replacement text, before assigning it to the
control sequence.
\begin{example}\begin{verbatim}
\edef\modedef{This macro was defined in
`\ifvmode vertical\else \ifmmode math
\else horizontal\fi\fi' mode}
\end{verbatim}
The mode tests will be executed at definition time, so the
replacement text will be a single string.
As a more useful example, suppose that in a file that will be
\cs{input} the category code of the~\n@ will be changed.
One could then write
\begin{verbatim}
\edef\restorecat{\catcode`@=\the\catcode`@}
\end{verbatim}
at the start, and
\begin{verbatim}
\restorecat
\end{verbatim}
at the end. See page~\pageref{store:cat}
for a fully worked-out version of this.
\end{example}
Contrary to the `one step expansion' of
\cs{expandafter}, the expansion inside an \cs{edef} is complete:
it goes on
until only unexpandable character and control sequence
tokens remain.
There are two exceptions to this total expansion:
\begin{itemize} \item any control sequence preceded by \cs{noexpand}
is not expanded, and,
\item if \cs{sometokenlist} is a token list, the expression
\begin{verbatim}
\the\sometokenlist
\end{verbatim}
is expanded to the contents
of the list, but the contents are not expanded
any further (see Chapter~\ref{token} for examples).\end{itemize}
On certain occasions the \cs{edef} can conveniently be
abused, in the sense that one is not interested in defining
a control sequence, but only in the result of the expansion.
For example, with the definitions
\alt
\begin{verbatim}
\def\othermacro{\ifnum1>0 {this}\else {that}\fi}
\def\somemacro#1{ ... }
\end{verbatim}
the call\begin{verbatim}
\expandafter\somemacro\othermacro
\end{verbatim}
gives the parameter assignment
\begin{verbatim}
#1<-\ifnum
\end{verbatim}
This can be repaired by calling
\begin{verbatim}
\edef\next{\noexpand\somemacro\othermacro}\next
\end{verbatim}
Conditionals are completely expanded inside an \cs{edef},
so the replacement text of \cs{next} will consist of the sequence
\begin{verbatim}
\somemacro{this}
\end{verbatim}
and a~subsequent call to \cs{next} executes this statement.
%\spoint \cs{afterassignment}
\subsection{\protect\cs{afterassignment}}
\label{sec:afterassignment}
The \cstoidx afterassignment\par\ command
takes one token and sets it aside for insertion
in the token stream
after the next assignment or macro definition.
If the first assignment is of a~box
to a box register,
the token will be inserted right after the opening
\alt
brace of the box (see page~\pageref{every:box:assign}).
Only one token can be saved this way; a subsequent token
saved by \cs{afterassignment} will override the first.
Let us consider an example of the use of \cs{afterassignment}.
It is often desirable to have a macro that will
\begin{itemize} \item assign the argument to some variable, and then
\item do a little calculation, based on the new value
of the variable.\end{itemize}
The following example illustrates the
straightforward approach:
\begin{verbatim}
\def\setfontsize#1{\thefontsize=#1pt\relax
\baselineskip=1.2\thefontsize\relax}
\setfontsize{10}
\end{verbatim}
A more elegant solution is possible using \cs{afterassignment}:
\begin{verbatim}
\def\setbaselineskip
{\baselineskip=1.2\thefontsize\relax}
\def\fontsize{\afterassignment\setbaselineskip
\thefontsize}
\fontsize=10pt
\end{verbatim}
Now the macro looks like an assignment: the equals sign
is even optional. In reality its expansion
ends with a variable to be assigned to. The control sequence
\cs{setbaselineskip} is saved for execution after
the assignment to \cs{thefontsize}.
Examples of \cs{afterassignment} in plain \TeX\ are
the \cs{magnification} and \cs{hglue} macros.
See \cite{Maus} for another creative application of
this command.
%\spoint \cs{aftergroup}
\subsection{\protect\cs{aftergroup}}
Several tokens can be saved for insertion after the current
\cstoidx aftergroup\par
group with an \begin{disp}\cs{aftergroup}\gr{token}\end{disp} command.
The tokens are inserted after the group in the sequence
the \cs{aftergroup} commands were given in.
The group can be delimited either by implicit or explicit
braces, or by \cs{begingroup} and \cs{endgroup}.
\begin{example}\begin{verbatim}
{\aftergroup\a \aftergroup\b}
\end{verbatim}
is equivalent to \begin{verbatim}
\a \b
\end{verbatim}
\end{example}
This command has many applications. One can be found
\alt
in the \cs{textvcenter} macro on page~\pageref{text:vcenter};
another one is provided
by the footnote mechanism of plain \TeX.
The footnote command of plain \TeX\ has the layout
\label{footnote:ex}
\begin{disp}\cs{footnote}\gr{footnote symbol}\lb\gr{footnote text}\rb
\end{disp} which looks like a macro with two arguments.
However, it is undesirable to scoop up the footnote text,
since this precludes for
instance category code changes in the footnote.
What happens in the plain footnote macro is (globally) the following.
\begin{itemize}\item The \cs{footnote} command opens
an insert, \begin{verbatim}
\def\footnote#1{ ...#1... %treat the footnote sign
\insert\footins\bgroup
\end{verbatim}
\item In the insert box a group is opened,
and an \cs{aftergroup} command
is given to close off the insert properly:
\begin{verbatim}
\bgroup\aftergroup\@foot
\end{verbatim}
This command is meant to wind up after the closing brace of
the text that the user typed to end the footnote text;
the opening brace of the user's footnote text must
be removed by
\begin{verbatim}
\let\next=}%end of definition \footnote
\end{verbatim}
which assigns the next token, the brace, to \cs{next}.
\item The footnote text is set as ordinary text
in this insert box.
\item After the footnote the command \cs{@foot}
defined by \begin{verbatim}
\def\@foot{\strut\egroup}
\end{verbatim}
will be executed.\end{itemize}
%\point Preventing expansion
\section{Preventing expansion}
Sometimes it is necessary to prevent expansion in a place
where it normally occurs. For this purpose the control
sequences \csidx{string} and \csidx{noexpand} are available.
The use of \cs{string} is rather limited, since it converts
a control sequence token into a string of characters, with
the value of \cs{escapechar} used for the character of
category code~0. It is eminently suitable for use in a
\cs{write}, in order to output a control sequence name
(see also Chapter~\ref{io}); for another application see
the explanation of \cs{newif} in Chapter~\ref{if}.
All characters resulting from \cs{string} have category
code~12, `other', except for space characters; they receive
code~10. See also Chapter~\ref{char}.
%\spoint \cs{noexpand}
\subsection{\protect\cs{noexpand}}
The \cs{noexpand} command is expandable, and its expansion
is the following token. The meaning of that token is
made temporarily equal to \cs{relax}, so that it cannot
be expanded further.
For \cs{noexpand} the most important application is probably
in \cs{edef} commands (but in write statements it can often
replace \cs{string}). Consider as an example
\begin{verbatim}
\edef\one{\def\noexpand\two{\the\prevdepth}}
\end{verbatim}
Without the \cs{noexpand}, \TeX\ would try to expand
\cs{two}, thus giving an `undefined control sequence' error.
A (rather pointless)
illustration of the fact that \cs{noexpand} makes the following
token effectively into a \cs{relax} is
\begin{verbatim}
\def\a{b}
\noexpand\a
\end{verbatim}
This will not produce any output, because the
effect of the \cs{noexpand} is to make the control sequence
\cs{a} temporarily equal to \cs{relax}.
%\spoint \cs{noexpand} and active characters
\subsection{\protect\cs{noexpand} and active characters}
The combination \cs{noexpand}\gr{token} is
\term character !active, and \cs{noexpand}\par
equivalent to \cs{relax}, even if the token
is an active character. Thus,
\begin{verbatim}
\csname\noexpand~\endcsname
\end{verbatim}
will not be the same as~\verb>\char`\~>.
Instead it will give an error message, because
unexpandable commands \ldash such as \cs{relax} \rdash are not allowed to appear
in between \cs{csname} and \cs{endcsname}.
The solution is to use \cs{string} instead; see page~\pageref{store:cat}
for an example.
In another context, however, the sequence
\cs{noexpand}\gr{active character} is equivalent
to the character, but in unexpandable form. This is
when the conditionals \cs{if} and \cs{ifcat} are used
(for an explanation of these, see Chapter~\ref{if}).
Compare
\begin{verbatim}
\if\noexpand~\relax % is false
\end{verbatim}
where the character code of the tilde is tested, with
\begin{verbatim}
\def\a{ ... } \if\noexpand\a\relax % is true
\end{verbatim}
where two control sequences are tested.
%\point \cs{relax}
\section{\protect\cs{relax}}
The control sequence \csidx{relax} cannot be expanded, but
when it is executed nothing happens.
This statement sounds a bit paradoxical, so consider
an example. Let counters \begin{verbatim}
\newcount\MyCount
\newcount\MyOtherCount \MyOtherCount=2
\end{verbatim}
be given.
In the assignment \begin{verbatim}
\MyCount=1\number\MyOtherCount3\relax4
\end{verbatim}
the command \cs{number} is expandable, and \cs{relax} is not.
When \TeX\ constructs the number that is to be assigned
it will expand all commands, either until a non-digit is
found, or until an unexpandable command is encountered.
Thus it reads the~\n1; it expands the sequence \verb>\number\MyOtherCount>,
which gives~\n2; it reads the~\n3; it sees the \cs{relax}, and
as this is unexpandable it halts. The number to be assigned
is then \n{123}, and the whole call has been expanded into
\begin{verbatim}
\MyCount=123\relax4
\end{verbatim}
Since the \cs{relax} token has no effect when it is executed,
the result of this line is that \n{123} is assigned to
\cs{MyCount}, and the digit 4 is printed.
Another example of how \cs{relax} can be used to indicate
the end of a command\label{fil:l:l}\ is
\begin{verbatim}
\everypar{\hskip 0cm plus 1fil }
\indent Later that day, ...
\end{verbatim}
This will be misunderstood: \TeX\ will see
\begin{verbatim}
\hskip 0cm plus 1fil L
\end{verbatim}
and \hbox{\n{fil L}} is a~valid,
if bizarre,
way of writing \n{fill} (see Chapter~\ref{gramm}).
One remedy is to write
\begin{verbatim}
\everypar{\hskip 0cm plus 1fil\relax}
\end{verbatim}
%\spoint[relax:cs] \cs{relax} and \cs{csname}
\subsection{\cs{relax} and \cs{csname}}
\label{relax:cs}
If a \verb-\csname ... \endcsname- command forms the name
of a previously undefined control sequence,
that control sequence is made equal to \cs{relax},
and the whole statement is also equivalent to \cs{relax}
(see also page~\pageref{cs:name}).
However, this assignment of \cs{relax} is
\altt
only local:
\begin{verbatim}
{\xdef\test{\expandafter\noexpand\csname xx\endcsname}}
\test
\end{verbatim}
gives an error message for an
undefined control sequence~\cs{xx}.
Consider as an example the \LaTeX\ environments,
which are delimited by \begin{verbatim}
\begin{...} ... \end{...}
\end{verbatim}
The begin and end commands are (in essence)
defined as follows:
\begin{verbatim}
\def\begin#1{\begingroup\csname#1\endcsname}
\def\end#1{\csname end#1\endcsname \endgroup}
\end{verbatim}
Thus, for the list environment the commands
\cs{list} and \cs{endlist} are defined, but any
command can be used as an environment name,
even if no corresponding \cs{end...} has been defined.
For instance, \begin{verbatim}
\begin{it} ... \end{it}
\end{verbatim}
is equivalent to
\begin{verbatim}
\begingroup\it ... \relax\endgroup
\end{verbatim}
See page~\pageref{begin:end:macros} for the rationale
behind using \cs{begingroup} and \cs{endgroup}
instead of \cs{bgroup} and \cs{egroup}.
%\spoint Preventing expansion with \cs{relax}
\subsection{Preventing expansion with \cs{relax}}
Because \cs{relax}
cannot be expanded, a control sequence can be prevented
from being expanded (for instance in an \cs{edef} or a \cs{write})
by making it temporarily equal to \cs{relax}:
\begin{verbatim}
{\let\somemacro=\relax \write\outfile{\somemacro}}
\end{verbatim}
will write the string `\cs{somemacro}' to an output file.
It would write the expansion
of the macro \cs{somemacro} (or give an error message
if the macro is undefined) if the \cs{let} statement
had been omitted.
%\spoint[bump:relax] \TeX\ inserts a \cs{relax}
\subsection{\TeX\ inserts a \cs{relax}}
\label{bump:relax}
\TeX\ itself inserts \cs{relax} on some occasions.
For instance, \cs{relax} is inserted if \TeX\ encounters an
\cs{or}, \cs{else}, or~\cs{fi} while still determining
the extent of the test.
\begin{example}
\begin{verbatim}
\ifvoid1\else ... \fi
\end{verbatim}
is changed into
\begin{verbatim}
\ifvoid1\relax \else ...\fi
\end{verbatim}
internally.
\end{example}
Similarly, if one of the tests \cs{if}, \cs{ifcat}
is given only one comparand, as in \begin{verbatim}
\if1\else ...
\end{verbatim}
a \cs{relax} token is inserted. Thus this test
is equivalent to \begin{verbatim}
\if1\relax\else ...
\end{verbatim}
Another place where \cs{relax} is used is the following.
While a control sequence is being defined in a \gr{shorthand
definition} \ldash that is, a \gr{registerdef} or \cs{chardef}
or \cs{mathchardef} \rdash its meaning is temporarily made
equal to \cs{relax}. This makes it possible to write
\verb>\chardef\foo=123\foo>.
%\spoint The value of non-macros; \cs{the}
\subsection{The value of non-macros; \cs{the}}
Expansion is a precisely defined activity in \TeX.
\cstoidx the\par
The full list of tokens that can be expanded
was given above.
Other tokens than those in the above list may have an `expansion'
in an informal sense. For instance one may wish to `expand'
the \cs{parindent} into its value, say~\n{20pt}.
Converting the value of (among others) an
\gr{integer parameter}, a \gr{glue parameter},
\gr{dimen parameter} or a \gr{token parameter}
into a string of character tokens is done by the expansion processor.
The command \cs{the}
is expanded whenever expansion is not inhibited,
and it takes the value of various sorts of parameters.
Its result (in most cases)
is a string of tokens of category~12, except
that spaces have category code~10.
Here is the list of everything that can be prefixed with \cs{the}.
\begin{description}\item [\gr{parameter} or \gr{register}]
If the parameter or register is of type integer, glue, dimen
or muglue,
its value is given as a string of character tokens;
if it is of type token list (for instance
\cs{everypar} or \cs{toks5}), the result is a string of tokens.
Box registers are excluded here.
\item [\gr{codename}\gr{8-bit number}]
See page~\pageref{codename}.
\item [\gr{special register}]
The integer registers \cs{prevgraf}, \cs{deadcycles}, \cs{insertpenalties}
\cs{inputlineno}, \cs{badness}, \cs{parshape}, \cs{spacefactor}
(only in horizontal mode), or \cs{prevdepth} (only in vertical mode).
The dimension registers \cs{pagetotal}, \cs{pagegoal}, \cs{pagestretch},
\cs{pagefilstretch}, \cs{pagefillstretch}, \cs{pagefilllstretch},
\cs{pageshrink}, or \cs{pagedepth}.
\item [Font properties:]
\cs{fontdimen}\gr{parameter number}\gr{font},
\cs{skew\-char}\gr{font},
\cs{hy\-phen\-char}\gr{font}.
\item [Last quantities:]
\cs{lastpenalty}, \cs{lastkern}, \cs{lastskip}.
\item [\gr{defined character}]
Any control sequence defined by \cs{chardef} or \cs{mathchardef};
the result is the decimal value.
\end{description}
In some cases \cs{the} can give a control sequence token
or list of such tokens.
\begin{description}\item [\gr{font}]
The result is the control sequence that stands for the
font.
\item [\gr{token variable}]
Token list registers and \gr{token parameter}s can be prefixed
with \cs{the}; the result is their contents.
\end{description}
Let us consider an example of the use of \cs{the}.
If in a file that is to be \cs{input} the
category code of a character, say the at~sign, is changed,
one could write
\begin{verbatim}
\edef\restorecat{\catcode`@=\the\catcode`@}
\end{verbatim}
and call \cs{restorecat} at the end of the file.
If the category code was~11, \cs{restorecat}
is defined equivalent to \begin{verbatim}
\catcode`@=11
\end{verbatim}
See page~\pageref{store:cat} for more elaborate macros
for saving and restoring catcodes.
%\point Examples
\section{Examples}
%\spoint Expanding after
\subsection{Expanding after}
The most obvious use of \cs{expandafter} is to reach over
a control sequence:
\begin{verbatim}
\def\stepcounter
#1{\expandafter\advance\csname
#1:counter\endcsname 1\relax}
\stepcounter{foo}
\end{verbatim}
Here the \cs{expandafter} lets the \cs{csname} command form
the control sequence \cs{foo:counter}; after \cs{expandafter}
is finished the statement has reduced to
\begin{verbatim}
\advance\foo:counter 1\relax
\end{verbatim}
It is possible to reach over tokens other than control sequences: in
\begin{verbatim}
\uppercase\expandafter{\romannumeral \year}
\end{verbatim}
it expands \cs{romannumeral} on the other side of the opening
brace.
You can expand after two control sequences:
\begin{verbatim}
\def\globalstepcounter
#1{\expandafter\global\expandafter\advance
\csname #1:counter\endcsname 1\relax}
\end{verbatim}
If you think of \cs{expandafter} as reversing the evaluation
order of {\sl two\/} control sequences, you can reverse
{\sl three\/} by
\begin{verbatim}
\expandafter\expandafter\expandafter\a\expandafter\b\c
\end{verbatim}
which reaches across the three control sequences
\begin{verbatim}
\expandafter \a \b
\end{verbatim}
to expand \cs{c} first.
There is even an unexpected use for \cs{expandafter} in
conditionals;
with \begin{verbatim}
\def\bold#1{{\bf #1}}
\end{verbatim}
the sequence \begin{verbatim}
\ifnum1>0 \bold \fi {word}
\end{verbatim}
will not give a boldface `word', but
\begin{verbatim}
\ifnum1>0 \expandafter\bold \fi {word}
\end{verbatim}
will.
The \cs{expandafter} lets \TeX\ see the \cs{fi} and remove it
before it tackles the macro \cs{bold}
(see also page~\pageref{after:cond}).
%\spoint Defining inside an \cs{edef}
\subsection{Defining inside an \cs{edef}}
There is one \TeX\ command that is executed instead of
expanded that is worth pointing out explicitly:
the primitive command \cs{def} (and all other \gr{def} commands)
is not expanded.
Thus the call
\begin{verbatim}
\edef\next{\def\thing{text}}
\end{verbatim}
will give an `undefined
control sequence' for \cs{thing}, even though after
\cs{def} expansion is ordinarily inhibited (see page~\pageref{noexp:list}).
After \begin{verbatim}
\edef\next{\def\noexpand\thing{text}}
\end{verbatim}
the `meaning' of \cs{next} will be \begin{verbatim}
macro: \def \thing {text}
\end{verbatim}
The definition \begin{verbatim}
\edef\next{\def\noexpand\thing{text}\thing}
\end{verbatim}
will again give an `undefined control sequence' for \cs{thing}
(this time on its second occurrence),
as it will only be defined when \cs{next} is called,
not when \cs{next} is defined.
%\spoint[expand:write] Expansion and \cs{write}
\subsection{Expansion and \cs{write}}
\label{expand:write}
The argument token list of \csidx{write} is treated in much
the same way as the replacement text of an \cs{edef};
that is, expandable control sequences and active characters
are completely expanded. Unexpandable control sequences
are treated by \cs{write} as if they are prefixed
by \cs{string}.
Because of the expansion performed by \cs{write},
some care has to be taken when outputting control
sequences with \cs{write}.
Even more complications arise from the fact that
the expansion of the argument of \cs{write} is only performed
when it is shipped out. Here follows a worked-out
example.
Suppose \cs{somecs} is a macro, and you
want to write the string
\begin{disp}\verb-\def\othercs-\lb {\italic the expansion of \cs{somecs}}\rb
\end{disp}
to a file.
The first attempt is
\begin{verbatim}
\write\myfile{\def\othercs{\somecs}}
\end{verbatim}
This gives an error `undefined control sequence' for \cs{othercs},
\altt
because the \cs{write} will try to expand that token.
Note that the \cs{somecs} is also expanded,
so that part is right.
The next attempt is
\begin{verbatim}
\write\myfile{\def\noexpand\othercs{\somecs}}
\end{verbatim}
This is almost right, but not quite. The
statement written is
\begin{disp}\verb>\def\othercs>\lb{\italic expansion of \cs{somecs}}\rb\end{disp}
which looks right.
However, writes \ldash and the expansion of their argument \rdash
are not executed
on the spot, but saved until the part of the page on which
they occur is shipped out (see Chapter~\ref{io}).
So, in the meantime, the value of \cs{somecs} may have
changed. In other words, the value written may not be the
value at the time the \cs{write} command was given.
Somehow, therefore, the current expansion must be
inserted in the write command.
The following is an attempt at repair:
\begin{verbatim}
\edef\act{\write\myfile{\def\noexpand\othercs{\somecs}}}
\act
\end{verbatim}
Now the write command will be
\begin{disp}\verb>\write\myfile{\def\othercs{>\italic value of\/
\verb>\somecs}}>\end{disp}
The \cs{noexpand} prevented the \cs{edef} from expanding
the \cs{othercs}, but after the definition it has disappeared,
so that execution of the write will again give an undefined control
sequence. The final solution is
\begin{verbatim}
\edef\act{\write\myfile
{\def \noexpand\noexpand \noexpand\othercs{\somecs}}}
\act
\end{verbatim}
In this case the write command caused by the expansion of \cs{act}
will be
\begin{disp}\verb>\write\myfile{\def\noexpand\othercs>\lb
{\italic current value of \cs{somecs}}\rb\end{disp}
and the string actually written is
\begin{disp}\verb>\def\othercs>\lb
{\italic current value of \cs{somecs}}\rb\end{disp}
This mechanism is the basis for cross-referencing
macros in several macro packages.
%\spoint Controlled expansion inside an \cs{edef}
\subsection{Controlled expansion inside an \cs{edef}}
Sometimes you may need an \cs{edef} to evaluate current
\howto Control expansion inside an \cs{edef}\par
conditions, but you want to expand something in the replacement
text only to a certain level. Suppose that
\begin{verbatim}
\def\a{\b} \def\b{c} \def\d{\e} \def\e{f}
\end{verbatim}
is given, and you want to define \cs{g} as \cs{a} expanded
one step, followed by \cs{d} fully expanded. The following
works:
\begin{verbatim}
\edef\g{\expandafter\noexpand\a \d}
\end{verbatim}
Explanation: the \cs{expandafter} reaches over the \cs{noexpand}
to expand \cs{a} one step, after which the
sequence \verb-\noexpand\b- is left.
This trick comes in handy when you need to
construct a control sequence with \cs{csname} inside
an \cs{edef}. The following sequence inside an \cs{edef}
\begin{verbatim}
\expandafter\noexpand\csname name\endcsname
\end{verbatim}
will expand exactly to \cs{name}, but not further.
As an example, suppose
\begin{verbatim}
\def\condition{true}
\end{verbatim}
has been given, then
\begin{verbatim}
\edef\setmycondition{\expandafter\noexpand
\csname mytest\condition\endcsname}
\end{verbatim}
will let \cs{setmycondition} expand to \cs{mytesttrue}.
%\spoint Multiple prevention of expansion
\subsection{Multiple prevention of expansion}
As was pointed out above, prefixing a command with
\cs{noexpand} prevents its expansion in commands
such as \cs{edef} and~\cs{write}. However, if a sequence of tokens
passes through more than one expanding command
stronger measures are needed.
The following trick can be used:
in order to protect a command against expansion
it can be prefixed with \csidx{protect}.
During the stages of processing where expansion is
not desired the definition of \cs{protect} is
\begin{verbatim}
\def\protect{\noexpand\protect\noexpand}
\end{verbatim}
Later on, when the command is actually needed,
\cs{protect} is defined as
\begin{verbatim}
\def\protect{}
\end{verbatim}
Why does this work? The expansion of
\begin{verbatim}
\protect\somecs
\end{verbatim}
is at first
\begin{verbatim}
\noexpand\protect\noexpand\somecs
\end{verbatim}
Inside an \cs{edef} this sequence is expanded further,
and the subsequent expansion is
\begin{verbatim}
\protect\somecs
\end{verbatim}
That is, the expansion is equal to the original sequence.
%\spoint More examples with \cs{relax}
\subsection{More examples with \cs{relax}}
Above, a first example was given in which \cs{relax} served
to prevent \TeX\ from scanning too far.
Here are some more examples, using \cs{relax} to bound
numbers.
After
\begin{verbatim}
\countdef\pageno=0 \pageno=1
\def\Par{\par\penalty200}
\end{verbatim}
the sequence \begin{verbatim}
\Par\number\pageno
\end{verbatim}
is misunderstood as
\begin{verbatim}
\par\penalty2001
\end{verbatim}
In this case it is sufficient to define
\begin{verbatim}
\def\Par{\par\penalty200 }
\end{verbatim}
as an \gr{optional space} is allowed to follow a number.
Sometimes, however, such a simple escape is not possible.
Consider the definition
\begin{verbatim}
\def\ifequal#1#2{\ifnum#1=#2 1\else 0\fi}
\end{verbatim}
The question is whether the space after \verb-#2-
is necessary, superfluous, or simply wrong.
Calls such as \verb-\ifequal{27}{28}- that compare two
numbers (denotations) will correctly give \n1 or~\n0,
and the space is necessary to prevent misinterpretation.
However, \verb-\ifequal\somecounter\othercounter- will
give \n{\char 32 1} if the counters are equal; in this
case the space could have been dispensed with.
The solution that works in both cases is
\begin{verbatim}
\def\ifequal#1#2{\ifnum#1=#2\relax 1\else 0\fi}
\end{verbatim}
Note that \cs{relax} is not expanded, so
\begin{verbatim}
\edef\foo{1\ifequal\counta\countb}
\end{verbatim}
will define \cs{foo} as either \verb-1\relax1- or~\n{10}.
%\spoint[store:cat] Example: category code saving and restoring
\subsection{Example: category code saving and restoring}
\label{store:cat}
In many applications it is necessary to change
\howto Save and restore category codes\par
the category code of a certain character during the
execution of some piece of code. If the writer of
that code is also the writer of the surrounding code,
s/he can simply change the category code back and forth.
However, if the surrounding code is by another author,
the value of the category code will have to be stored
and restored.
Thus one would like to write
\begin{verbatim}
\storecat@
... some code ...
\restorecat@
\end{verbatim}
or maybe \begin{verbatim}
\storecat\%
\end{verbatim}
for characters that
are possibly a comment character (or ignored or invalid).
\alt
The basic idea is to define
\begin{verbatim}
\def\storecat#1{%
\expandafter\edef\csname restorecat#1\endcsname
{\catcode`#1=\the\catcode`#1}}
\end{verbatim}
so that, for instance, \verb>\storecat$> will define
the single control sequence `\verb>\restorecat$>'
(one control sequence) as \begin{verbatim}
\catcode`$=3
\end{verbatim}
The macro \cs{restorecat} can then be implemented as
\begin{verbatim}
\def\restorecat#1{%
\csname restorecat#1\endcsname}
\end{verbatim}
Unfortunately, things are not so simple.
The problems occur with active characters, because these
are expanded inside the \verb>\csname ... \endcsname> pairs.
One might be tempted to write \verb>\noexpand#1> everywhere,
but this is wrong. As was explained above, this is essentially
equal to \cs{relax}, which is unexpandable, and will therefore
lead to an error message when it appears between
\cs{csname} and \cs{endcsname}. The proper solution is then
to use \verb>\string#1>. For the case where the argument
was given as a control symbol (for example~\verb>\%>),
the escape character has to be switched off for a while.
Here are the complete macros. The \cs{storecat} macro
gives its argument a default category code of~12.
\begin{verbatim}
\newcount\tempcounta % just a temporary
\def\csarg#1#2{\expandafter#1\csname#2\endcsname}
\def\storecat#1%
{\tempcounta\escapechar \escapechar=-1
\csarg\edef{restorecat\string#1}%
{\catcode`\string#1=
\the\catcode\expandafter`\string#1}%
\catcode\expandafter`\string#1=12\relax
\escapechar\tempcounta}
\def\restorecat#1%
{\tempcounta\escapechar \escapechar=-1
\csname restorecat\string#1\endcsname
\escapechar\tempcounta}
\end{verbatim}
%\spoint Combining \cs{aftergroup} and boxes
\subsection{Combining \cs{aftergroup} and boxes}
%\tracingmacros=2 \tracingcommands=2
At times, one wants to construct a box and immediately
after it has been constructed to
do something with it. The \cs{aftergroup} command
can be used to put both the commands creating the box,
and the ones handling it, in one macro.
As an example, here is a macro
\cs{textvcenter}\label{text:vcenter}\
which defines a variant of the \cs{vcenter} box
\howto \cs{vcenter} outside math mode\par
(see page~\pageref{vcenter}\label{tvcenter})
that can be used outside math mode.
\begin{verbatim}
\def\textvcenter
{\hbox \bgroup$\everyvbox{\everyvbox{}%
\aftergroup$\aftergroup\egroup}\vcenter}
\end{verbatim}
The idea is that the macro inserts \verb>\hbox {$>,
and that the matching \verb>$}> gets inserted
by the \cs{aftergroup} commands. In order to get the
\cs{aftergroup} commands inside the box, an
\cs{everyvbox} command is used.
This macro can even be used with a \gr{box specification}
(see page~\pageref{box:spec}), for example
\begin{verbatim}
\textvcenter spread 8pt{\hbox{a}\vfil\hbox{b}}
\end{verbatim}
and because it is really just an \cs{hbox}, it can also
be used in a \cs{setbox} assignment.
%\spoint More expansion
\subsection{More expansion}
There is a particular charm to macros that work
purely by expansion. See the articles by
\cite{E2}, \cite{Jeffrey:lists}, and~\cite{Maus2}.
%%%% end of input file [expand]
%\InputFile:ifelsefi
%%%% this is input file [ifelsefi]
%\subject[if] Conditionals
\endofchapter
\chapter{Conditionals}\label{if}
Conditionals are an indispensible tool for powerful macros.
\term conditional\par
\TeX\ has a large repertoire of conditionals for querying
such things as category codes or processing modes.
This chapter gives an inventory of the various conditionals,
and it treats the evaluation of
conditionals in detail.
\begin{inventory}
\item [\cs{if}]
Test equality of character codes.
\item [\cs{ifcat}]
Test equality of category codes.
\item [\cs{ifx}]
Test equality of macro expansion, or equality of character code and
category code.
\item [\cs{ifcase}]
Enumerated case statement.
\item [\cs{ifnum}]
Test relations between numbers.
\item [\cs{ifodd}]
Test whether a number is odd.
\item [\cs{ifhmode}]
Test whether the current mode is (possibly restricted) horizontal mode.
\item [\cs{ifvmode}]
Test whether the current mode is (possibly internal) vertical mode.
\item [\cs{ifmmode}]
Test whether the current mode is (possibly display) math mode.
\item [\cs{ifinner}]
Test whether the current mode is an internal mode.
\item [\cs{ifdim}]
Compare two dimensions.
\item [\cs{ifvoid}]
Test whether a box register is empty.
\item [\cs{ifhbox}]
Test whether a box register contains a horizontal box.
\item [\cs{ifvbox}]
Test whether a box register contains a vertical box.
\item [\cs{ifeof}]
Test for end of input stream or non-existence of file.
\item [\cs{iftrue}]
A test that is always true.
\item [\cs{iffalse}]
A test that is always false.
\item [\cs{fi}]
Closing delimiter for all conditionals.
\item [\cs{else}]
Select \gr{false text} of a conditional
or default case of \cs{ifcase}.
\item [\cs{or}]
Separator for entries of an \cs{ifcase}.
\item [\cs{newif}]
Create a new test.
\end{inventory}
%\point The shape of conditionals
\section{The shape of conditionals}
Conditionals in \TeX\ have one of the following two forms
\cstoidx else\par\cstoidx fi\par
\begin{disp}\cs{if...}\gr{test tokens}\gr{true text}\cs{fi}\nl
\cs{if...}\gr{test tokens}\gr{true text}\cs{else}%
\gr{false text}\cs{fi}\end{disp}
where the \gr{test tokens} are zero or more tokens, depending on
the particular conditional; the \gr{true text} is a series of tokens
to be processed if the test turns out true, and the \gr{false text}
is a series of tokens to be processed if the test turns out false.
Both the \gr{true text} and the \gr{false text} can be empty.
The exact process of how \TeX\ expands conditionals is treated
below.
%\point Character and control sequence tests
\section{Character and control sequence tests}
Three tests exist for testing character tokens and
control sequence tokens.
%\spoint \cs{if}
\subsection{\cs{if}}
Equality of character codes can be tested by
\cstoidx if\par
\begin{Disp}\cs{if}\gr{token$_1$}\gr{token$_2$}\end{Disp}
In order to allow the tokens to be control sequences,
\TeX\ assigns character code~256 to control sequences,
the lowest positive number that is not the character code of a
character token (remember that the legal character codes
are~0--255).
Thus all control sequences are equal as far as \cs{if} is
concerned, and they are unequal to all character tokens.
As an example, this fact can be used to define
\howto Test whether a token is a control sequence\par
\begin{verbatim}
\def\ifIsControlSequence#1{\if\noexpand#1\relax}
\end{verbatim}
which tests whether a token is a control sequence token
instead of a character token (its result is unpredictable
if the argument is a \verb>{...}> group).
After \cs{if} \TeX\ will expand until two unexpandable
tokens are obtained, so it is necessary to prefix
expandable control sequences and active characters
with \cs{noexpand} when testing them with~\cs{if}.
\begin{example} After
\begin{verbatim}
\catcode`\b=13 \catcode`\c=13 \def b{a} \def c{a} \let\d=a
\end{verbatim}
we find that
\begin{tdisp}
\verb-\if bc- is true, because both \n b and \n c expand to \n a,\nl
\verb-\if\noexpand b\noexpand c- is false, and\nl
\verb-\if b\d- is true because \n{b} expands to the character~\n{a},
and \cs{d} is an implicit character token~\n{a}.
\end{tdisp}
\end{example}
%\spoint \cs{ifcat}
\subsection{\cs{ifcat}}
The \cs{if} test ignores category codes; these can be tested
\cstoidx ifcat\par
by \begin{Disp}\cs{ifcat}\gr{token$_1$}\gr{token$_2$}\end{Disp}
This test is a lot like \cs{if}: \TeX\ expands after it
until unexpandable tokens remain. For this test
control sequences
are considered to have category code~16
(ordinarily, category codes are in the range~0--15), which makes them
all equal to each other, and different from all character
tokens.
%\spoint \cs{ifx}
\subsection{\protect\cs{ifx}}
Equality of tokens is tested in a stronger sense than
\cstoidx ifx\par
the above by \begin{Disp}\cs{ifx}\gr{token$_1$}\gr{token$_2$}\end{Disp}
\begin{itemize}\item Character tokens are equal for \cs{ifx} if
they have the same character code and category code.
\item Control sequence tokens are equal if they represent the
same \TeX\ primitive, or have been similarly defined by
\cs{font}, \cs{countdef}, or some such. For example,
\begin{verbatim}
\let\boxhor=\hbox \ifx\boxhor\hbox %is true
\font\a=cmr10 \font\b=cmr10 \ifx\a\b %is true
\end{verbatim}
\item Control sequences are also equal if they are
macros with the same parameter text and replacement text,
and the same status with respect to \cs{outer} and~\cs{long}.
For example,
\begin{verbatim}
\def\a{z} \def\b{z} \def\c1{z} \def\d{\a}
\ifx\a\b %is true
\ifx\a\c %is false
\ifx\a\d %is false
\end{verbatim}
\end{itemize}
Tokens following this test are not expanded.
By way of example of the use of \cs{ifx} consider string testing.
A simple implementation of string testing in \TeX\ is as follows:
\begin{verbatim}
\def\ifEqString#1#2{\def\testa{#1}\def\testb{#2}%
\ifx\testa\testb}
\end{verbatim}
The two strings are used as the replacement text of two macros,
and equality of these macros is tested.
This is about as efficient as string testing can get:
\TeX\ will traverse the definition texts of the
macros \cs{testa} and \cs{testb}, which has precisely the
right effect.
As another example, one can test whether a control sequence is defined
by\howto Test whether a control sequence is (un)defined\par
\begin{verbatim}
\def\ifUnDefinedCs#1{\expandafter
\ifx\csname#1\endcsname\relax}
\ifUnDefinedCs{parindent} %is not true
\ifUnDefinedCs{undefined} %is (one hopes) true
\end{verbatim}
This uses the fact that a \verb>\csname...\endcsname> command
is equivalent to \cs{relax} if the control sequence
has not been defined before. Unfortunately, this test also
turns out true if a control sequence has been \cs{let} to
\cs{relax}.
%\point Mode tests
\section{Mode tests}
In order to determine in which of the six modes
(see Chapter~\ref{hvmode}) \TeX\
is currently operating, the tests \csidx{ifhmode},
\csidx{ifvmode}, \csidx{ifmmode}, and~\csidx{ifinner}
are available.
\begin{itemize}\item\cs{ifhmode} is true if \TeX\ is in horizontal mode
or restricted horizontal mode.
\item\cs{ifvmode} is true if \TeX\ is in vertical mode or
internal vertical mode.
\item\cs{ifmmode} is true if \TeX\ is in math mode or display
math mode.\end{itemize}
The \cs{ifinner} test is true if \TeX\ is in any of the three
internal modes: restricted horizontal mode, internal vertical
mode, and non-display math mode.
%\point Numerical tests
\section{Numerical tests}
Numerical relations between \gr{number}s can be tested
\cstoidx ifnum\par
with \begin{disp}\cs{ifnum}\gr{number$_1$}\gr{relation}%
\gr{number$_2$}\end{disp}
where the relation is a character \n{<}, \n{=}, or~\n{>},
of category~12.
Quantities such as glue can be used as a number here
through the conversion to scaled points, and \TeX\
will expand in order to arrive at the two \gr{number}s.
Testing for odd or even numbers can be done with \csidx{ifodd}:
the test\begin{disp}\cs{ifodd}\gr{number}\end{disp}
is true if the \gr{number} is odd.
%\point Other tests
\section{Other tests}
%\spoint Dimension testing
\subsection{Dimension testing}
Relations between \gr{dimen} values (Chapter~\ref{glue})
can be tested with
\csidx{ifdim} using the same three relations as in \cs{ifnum}.
%\spoint Box tests
\subsection{Box tests}
Contents of box registers (Chapter~\ref{boxes}) can be tested with
\cstoidx ifhbox\par\cstoidx ifvbox\par\cstoidx ifvoid\par
\begin{disp}\cs{ifvoid}\gr{8-bit number}\end{disp}
which is true if the register contains no box,
\begin{disp}\cs{ifhbox}\gr{8-bit number}\end{disp}
which is true if the register contains a horizontal box, and
\begin{disp}\cs{ifvbox}\gr{8-bit number}\end{disp}
which is true if the register contains a vertical box.
%\spoint I{/}O tests
\subsection{I{/}O tests}
The status of input streams (Chapter~\ref{io}) can be tested with
\cstoidx ifeof\par
the end-of-file test
\cs{ifeof}\gr{number}, which is only false
if the number is in the range 0--15, and the corresponding
stream is open and not fully read. In particular, this test
is true if the file name connected
to this stream (through \cs{openin})
does not correspond to an existing file.
See the example on page~\pageref{ex:eof}.
%\spoint Case statement
\subsection{Case statement}
The \TeX\ case statement is called \cs{ifcase};
\cstoidx ifcase\par\cstoidx or\par
its syntax is\begin{disp}\cs{ifcase}\gr{number}\gr{case$_0$}\cs{or}%
\n{...}\cs{or}\gr{case$_n$}\cs{else}\gr{other cases}\cs{fi}
\end{disp} where for $n$ cases there are $n-1$ \cs{or}
control sequences. Each of the \gr{case$_i$}
parts can be empty,
and the \cs{else}\gr{other cases} part is optional.
%\spoint Special tests
\subsection{Special tests}
The tests \cs{iftrue} and \cs{iffalse} are always
\cstoidx iftrue\par\cstoidx iffalse\par
true and false respectively.
They are mainly useful as tools in macros.
For instance, the sequences \begin{verbatim}
\iftrue{\else}\fi
\end{verbatim}
and \begin{verbatim}
\iffalse{\else}\fi
\end{verbatim}
yield a left and right
brace respectively, but they have balanced braces, so they
can be used inside a macro replacement text.
The \cs{newif} macro, treated below,
provides another use of \cs{iftrue} and \cs{iffalse}.
On page 260 of \TeXbook\ these control sequences
are also used in an interesting manner.
%\point[newif:def] The \cs{newif} macro
\section{The \protect\cs{newif} macro}
\label{newif:def}
The plain format defines an (outer) macro \csidx{newif} by
which the user can define new conditionals.
If the user defines \begin{verbatim}
\newif\iffoo
\end{verbatim}
\TeX\ defines three new control sequences,
\cs{footrue} and \cs{foofalse} with which the user can set
the condition, and \cs{iffoo} which tests the `foo' condition.
The macro call \verb-\newif\iffoo- expands to
\begin{verbatim}
\def\footrue{\let\iffoo=\iftrue} \def\foofalse{\let\iffoo=\iffalse}
\foofalse
\end{verbatim}
The actual definition, especially the part that ensures that
the \cs{iffoo} indeed starts with \cs{if}, is a pretty hack.
An explanation follows here.
This uses concepts from Chapters~\ref{macro}
and~\ref{expand}.
The macro \cs{newif} starts as follows:
\begin{verbatim}
\outer\def\newif#1{\count@\escapechar \escapechar\m@ne
\end{verbatim}
This saves the current escape character in \cs{count@}, and
sets the value of \cs{escapechar} to~\n{-1}.
The latter action has the
effect that no escape character is used in the output
of \cs{string}\gr{control sequence}.
An auxiliary macro \verb>\if@> is defined by
\begin{verbatim}
{\uccode`1=`i \uccode`2=`f \uppercase{\gdef\if@12{}}}
\end{verbatim}
Since the uppercase command changes only character codes, and
not category codes, the macro \cs{if@} now has
to be followed by the characters \n{if} of category~12.
Ordinarily, these characters have category code~11.
In effect this
macro then eats these two characters, and \TeX\ complains if
they are not present.
Next there is a macro \verb>\@if> defined by
\begin{verbatim}
\def\@if#1#2{\csname\expandafter\if@\string#1#2\endcsname}
\end{verbatim}
which will be called like \verb>\@if\iffoo{true}> and
\verb>\@if\iffoo{false}>.
Let us examine the call \verb>\@if\iffoo{true}>.
\begin{itemize}\item The \cs{expandafter} reaches over the \verb>\if@>
to expand \cs{string} first. The part \verb>\string\iffoo>
expands to \n{iffoo} because the escape character is not printed,
and all characters have category~12.
\item The \verb>\if@> eats the first two characters
\n i$_{12}$\n f$_{12}$ of this.
\item As a result, the final expansion of \verb>\@if\iffoo{true}>
is then \begin{verbatim}
\csname footrue\endcsname
\end{verbatim}
\end{itemize}
Now we can treat the relevant parts of \cs{newif} itself:
\begin{verbatim}
\expandafter\expandafter\expandafter
\edef\@if#1{true}{\let\noexpand#1=\noexpand\iftrue}%
\end{verbatim}
The three \cs{expandafter} commands may look intimidating, so let us
take one step at a time.
\begin{itemize}\item One \cs{expandafter} is necessary to reach over the \cs{edef},
such that \verb>\@if> will expand:
\begin{verbatim}
\expandafter\edef\@if\iffoo{true}
\end{verbatim}
gives
\begin{verbatim}
\edef\csname footrue\endcsname
\end{verbatim}
\item Then another \cs{expandafter} is necessary to activate
\altt
the \cs{csname}:
\begin{verbatim}
\expandafter \expandafter \expandafter \edef \@if ...
% new old new
\end{verbatim}
\item This makes the final expansion
\begin{verbatim}
\edef\footrue{\let\noexpand\iffoo=\noexpand\iftrue}
\end{verbatim}
\end{itemize}
After this follows a similar statement for the \n{false} case:
\begin{verbatim}
\expandafter\expandafter\expandafter
\edef\@if#1{false}{\let\noexpand#1=\noexpand\iffalse}%
\end{verbatim}
The conditional starts out false, and the escape character
has to be reset:
\begin{verbatim}
\@if#1{false}\escapechar\count@}
\end{verbatim}
%\point Evaluation of conditionals
\section{Evaluation of conditionals}
\TeX's conditionals behave differently from those
\term evaluation! conditionals\par\term conditionals! evaluation of\par
in ordinary programming languages. In many instances
one may not notice the difference, but in certain contexts
it is important to know precisely what happens.
When \TeX\ evaluates a conditional, it first determines
what is to be tested. This in itself may involve some
expansion; as we saw in the previous chapter,
only after an \cs{ifx} test
does \TeX\ not expand. After all other tests \TeX\ will
expand tokens until the extent of the test and the tokens
to be tested have been determined. On the basis of the outcome
of this test the \gr{true text} and the \gr{false text}
are either expanded or skipped.
For the processing of the parts of the conditional
let us consider some cases separately.
\begin{itemize}
\item \verb>\if... ... \fi> and the result of the test is false.
After the test \TeX\ will start skipping material
without expansion, without counting braces, but balancing
nested conditionals, until a \cs{fi} token is encountered.
If the \cs{fi} is not found an error message results
at the end of the file:
\begin{disp}\tt Incomplete \cs{if...}; all text was ignored after line \n{...}
\end{disp} where the line number indicated is that of the line
where \TeX\ started skipping, that is, where the conditional
occurred.
\item \verb>\if... \else ... \fi> and the result of the test is false.
Any material in between the condition and the \cs{else} is skipped
without expansion, without counting braces, but balancing nested
conditionals.
The \cs{fi} token can be the result of expansion; if it never
turns up \TeX\ will give a diagnostic message
\begin{disp}\tt \cs{end} occurred when \cs{if...} on line \n{...}
was incomplete\end{disp}
This sort of error is not visible in the output.
This point plus the previous may jointly be described as follows:
after a false condition \TeX\ skips until an \cs{else} or \cs{fi}
is found; any material in between \cs{else} and \cs{fi} is processed.
\item \verb>\if... ... \fi> and the result of the test is true.
\TeX\ will start processing the material following the condition.
As above, the \cs{fi} token may be inserted by expansion of
a macro.
\item \verb>\if... \else ... \fi> and the result of the test is true.
Any material following the condition is processed until the \cs{else}
is found; then \TeX\ skips everything until the matching \cs{fi}
is found.
This point plus the previous may be described as follows:
after a true test \TeX\ starts processing material until
an \cs{else} or \cs{fi} is found; if an \cs{else} is found
\TeX\ skips until it finds the matching \cs{fi}.
\end{itemize}
%\point Assorted remarks
\section{Assorted remarks}
%\spoint The test gobbles up tokens
\subsection{The test gobbles up tokens}
A common mistake is to write the following:
\begin{verbatim}
\ifnum\x>0\someaction \else\anotheraction \fi
\end{verbatim}
which has the effect that the \verb.\someaction. is expanded,
regardless of whether the test succeeds or not.
The reason for this is that \TeX\ evaluates the input stream until
it is certain that it has found the arguments to be tested.
In this case it is perfectly possible for the \verb.\someaction.
to yield a digit, so it is expanded. The remedy is to insert
\altt
a space or a \cs{relax} control sequence
after the last digit of the number to be tested.
%\spoint The test wants to gobble up the \cs{else} or \cs{fi}
\subsection{The test wants to gobble up the \cs{else} or \cs{fi}}
The same mechanism that underlies the phenomenon in the previous
point can lead to even more surprising effects if \TeX\
bumps into an \verb.\else., \verb.\or., or \verb.\fi.
while still busy determining the extent of the test itself.
Recall that \verb.\pageno. is a synomym for \verb.\count0., and
consider the following examples:
\begin{verbatim}
\newcount\nct \nct=1\ifodd\pageno\else 2\fi 1
\end{verbatim}
and
\begin{verbatim}
\newcount\nct \nct=1\ifodd\count0\else 2\fi 1
\end{verbatim}
The first example will assign either 11 or~121 to \cs{nct},
but the second one will assign 1 or~121.
The explanation is that
in cases like the second, where
\altt
an \verb.\else. is encountered while the
test still has not been delimited, a \verb.\relax. is inserted.
In the case that \verb.\count0. is odd the result will thus be \verb.\relax.,
and the example will yield \begin{verbatim}
\nct=1\relax2
\end{verbatim}
which will assign~1 to \cs{nct}, and print~2.
%\spoint[after:cond] Macros and conditionals; the use of \cs{expandafter}
\subsection{Macros and conditionals; the use of \cs{expandafter}}
\label{after:cond}
Consider the following example:
\begin{verbatim}
\def\bold#1{{\bf #1}} \def\slant#1{{\sl #1}}
\ifnum1>0 \bold \else \slant \fi {some text} ...
\end{verbatim}
This will make not only `some text',
but {\sl all\/} subsequent text bold.
Also, at the end of the job there will be a notice that
`end occurred inside a group at level~1'.
Switching on \cs{tracingmacros} reveals that the argument
of \verb.\bold. was \verb.\else..
This means that, after expansion of \verb.\bold.,
the input stream looked like
\begin{verbatim}
\ifnum1>0 {\bf \else }\fi {some text} rest of the text
\end{verbatim}
so the closing brace was skipped as part of the \gram{false text}.
Effectively, then, the resulting stream is
\begin{verbatim}
{\bf {some text} rest of the text
\end{verbatim}
which is unbalanced.
One solution to this sort of problem would be to write
\begin{verbatim}
\ifnum1>0 \let\next=\bold \else \let\next=\slant \fi \next
\end{verbatim}
but a solution using \cs{expandafter} is also possible:
\begin{verbatim}
\ifnum1>0 \expandafter \bold \else \expandafter \slant \fi
\end{verbatim}
This works, because the \cs{expandafter} commands let \TeX\ determine
the boundaries of the \gram{true text} and the \gram{false text}.
In fact, the second solution may be preferred over the first,
since conditionals are handled by the expansion processor,
and the \cs{let} statements are tackled only by the execution
processor; that is, they are not expandable.
Thus the second solution will (and the first will not)
work, for instance,
inside an~\cs{edef}.
Another example with \cs{expandafter} is the sequence
\begin{verbatim}
\def\get#1\get{ ... }
\expandafter \get \ifodd1 \ifodd3 5\fi \fi \get
\end{verbatim}
This gives\begin{verbatim}
#1<- \ifodd3 5\fi \fi
\end{verbatim}
and
\begin{verbatim}
\expandafter \get \ifodd2 \ifodd3 5\fi\fi \get
\end{verbatim}
gives\begin{verbatim}
#1<-
\end{verbatim}
This illustrates again that the result of evaluating a
conditional is not the final expansion, but the start
of the expansion of the \gr{true text} or \gr{false text},
depending on the outcome of the test.
A detail should be noted: with \cs{expandafter}
it is possible that the \verb.\else. is encountered
before the \gram{true text} has been expanded completely.
This raises the question as to the exact timing of expansion
and skipping.
In the example
\begin{verbatim}
\def\hello{\message{Hello!}}
\ifnum1>0 \expandafter \hello \else \message{goodbye} \bye
\end{verbatim}
the error message caused by the missing \verb.\fi. is given
without \verb.\hello. ever having been expanded.
The conclusion must be that the \gram{false text} is
skipped as soon as it has been located, even if this is at a time
when the \gram{true text} has not been expanded completely.
%\spoint Incorrect matching
\subsection{Incorrect matching}
\TeX's matching of \verb.\if., \verb.\else., and \verb.\fi.
is easily upset. For instance, \TeXbook\ warns you that
you should not say \begin{verbatim}
\let\ifabc=\iftrue
\end{verbatim}
inside a
conditional, because if this text is skipped \TeX\ sees
at least one \verb.\if. to be matched.
The reason for this is that when \TeX\ is skipping
it recognizes all \cs{if...}, \cs{or}, \cs{else}, and \cs{fi}
tokens, and everything that has been declared a synonym of
such a token by \cs{let}. In \verb>\let\ifabc=\iftrue>
\TeX\ will therefore at least see the \cs{iftrue} as
the opening of a conditional, and, if the current meaning
of \cs{ifabc} was for instance \cs{iffalse}, it will also
be considered as the opening of a conditional statement.
As another example, if
\begin{verbatim}
\csname if\sometest\endcsname \someaction \fi
\end{verbatim}
is skipped as part of conditional text,
the \verb.\fi. will unintentionally close the
outer conditional.
It does not help to enclose such potentially dangerous
constructs inside a group, because grouping is independent of
conditional structure. Burying such commands inside macros is
the safest approach.
Sometimes another solution is possible, however.
The \cs{loop} macro of plain \TeX\ (see page~\pageref{loop:ex})
is used as \begin{verbatim}
\loop ... \if ... \repeat
\end{verbatim}
where the \cs{repeat} is not an actually executable
command, but is merely a delimiter:
\begin{verbatim}
\def\loop#1\repeat{ ... }
\end{verbatim}
Therefore,
by declaring \begin{verbatim}
\let\repeat\fi
\end{verbatim}
the \cs{repeat} balances the \cs{if...} that terminates
the loop, and it becomes possible to have loops in
skipped conditional text.
%\spoint Conditionals and grouping
\subsection{Conditionals and grouping}
It has already been mentioned above that group nesting in \TeX\
is independent of conditional nesting.
The reason for this is that conditionals are handled by the
expansion part of \TeX; in that stage braces are just
unexpandable tokens that require no special treatment.
Grouping is only performed in the later stage of execution
processing.
An example of this independence is now given.
One may write a macro that yields part of
a conditional:
\begin{verbatim}
\def\elsepart{\else \dosomething \fi}
\end{verbatim}
The other way around, the following macros
yield a left brace and a right brace respectively:
\begin{verbatim}
\def\leftbrace{\iftrue{\else}\fi}
\def\rightbrace{\iffalse{\else}\fi}
\end{verbatim}
Note that braces in these definitions are properly nested.
%\spoint A trick
\subsection{A trick}
In some contexts it may be hard to get rid of
\cs{else} or \cs{fi} tokens in a proper
manner. The above approach with \cs{expandafter}
works only if there is a limited number of tokens involved.
In other cases the following trick may provide a way out:
\begin{verbatim}
\def\hop#1\fi{\fi #1}
\end{verbatim}
Using this as
\begin{disp}\verb>\if... \hop >\gr{lots of tokens}\verb>\fi>\end{disp}
will place the tokens outside the conditional.
This is for instance used in~\cite{E2}.
As a further example of this sort of trick,
consider the problem (suggested to me and solved by
Alan Jeffrey) of implementing a conditional
\verb-\ifLessThan#1#2#3#4-
such that the arguments corresponding to \verb-#3- or
\verb-#4- result, depending on whether \verb-#1- is
less than \verb-#2- or not.
The problem here is how to get rid of the \cs{else} and the~\cs{fi}.
The \ldash or at least, one \rdash solution is to scoop them up
as delimiters for macros:
\begin{verbatim}
\def\ifLessThan#1#2{\ifnum#1<#2\relax\taketrue \else \takefalse \fi}
\def\takefalse\fi#1#2{\fi#2}
\def\taketrue\else\takefalse\fi#1#2{\fi#1}
\end{verbatim}
Note that \cs{ifLessThan} has only two parameters
(the things to be tested); however, its
result is a macro that
chooses between the next two arguments.
%\spoint More examples of expansion in conditionals
\subsection{More examples of expansion in conditionals}
Above, the macro \cs{ifEqString} was given
\alt
that compares two strings:
\howto Compare two strings\par
\begin{verbatim}
\def\ifEqString#1#2%
{\def\csa{#1}\def\csb{#2}\ifx\csa\csb }
\end{verbatim}
However, this macro relies on \cs{def}, which is not an
expandable command. If we need a string tester that will
work, for instance, inside an \cs{edef}, we need some
more ingenuity (this solution was taken from~\cite{E2}).
The basic principle of this solution is to compare the strings
one character at a time. Macro delimiting by \cs{fi} is used;
this was explained above.
First of all, the \cs{ifEqString} call is replaced by a
sequence \verb>\ifAllChars ...\Are ...\TheSame>, and both
strings are delimited by a dollar sign, which is not supposed
to appear in the strings themselves.
\begin{verbatim}
\def\ifEqString
#1#2{\ifAllChars#1$\Are#2$\TheSame}
\end{verbatim}
The test for equality of characters first determines
whether either string has ended. If both have ended, the original
strings were equal; if only one has ended, they were of unequal
length, hence unequal. If neither string has ended, we test
whether the first characters are equal, and if so, we make a recursive
call to test the remainder of the string.
\begin{verbatim}
\def\ifAllChars#1#2\Are#3#4\TheSame
{\if#1$\if#3$\say{true}%
\else \say{false}\fi
\else \if#1#3\ifRest#2\TheSame#4\else
\say{false}\fi\fi}
\def\ifRest#1\TheSame#2\else#3\fi\fi
{\fi\fi \ifAllChars#1\Are#2\TheSame}
\end{verbatim}
The \cs{say} macro is supposed to give \cs{iftrue} for
\verb>\say{true}> and \cs{iffalse} for \verb>\say{false}>.
Observing that all calls to this macro occur two conditionals deep,
we use the `hop' trick explained above as follows.
\begin{verbatim}
\def\say#1#2\fi\fi
{\fi\fi\csname if#1\endcsname}
\end{verbatim}
Similar to the above example, let us write a macro
that will test lexicographic (`dictionary') precedence
of two strings:
\howto Compare two strings lexicographically\par
\begin{verbatim}
\let\ex=\expandafter
\def\ifbefore
#1#2{\ifallchars#1$\are#2$\before}
\def\ifallchars#1#2\are#3#4\before
{\if#1$\say{true\ex}\else
\if#3$\say{false\ex\ex\ex}\else
\ifnum`#1>`#3 \say{false%
\ex\ex\ex\ex\ex\ex\ex}\else
\ifnum`#1<`#3 \say{true%
\ex\ex\ex\ex\ex\ex\ex
\ex\ex\ex\ex\ex\ex\ex\ex}\else
\ifrest#2\before#4\fi\fi\fi\fi}
\def\ifrest#1\before#2\fi\fi\fi\fi
{\fi\fi\fi\fi
\ifallchars#1\are#2\before}
\def\say#1{\csname if#1\endcsname}
\end{verbatim}
In this macro a slightly
different implementation of \cs{say} is used.
Simplified, a call to \cs{ifbefore} will eventually lead to a situation
that looks (in the `true' case) like
\begin{verbatim}
\ifbefore{...}{...}
\if... %% some comparison that turns out true
\csname iftrue\expandafter\endcsname
\else .... \fi
... %% commands for the `before' case
\else
... %% commands for the `not-before' case
\fi
\end{verbatim}
When the comparison has turned out true, \TeX\ will start processing
the \gr{true text}, and make a mental note to remove any
\verb>\else ... \fi> part once an \cs{else} token is seen.
Thus, the sequence
\begin{verbatim}
\csname iftrue\expandafter\endcsname \else ... \fi
\end{verbatim}
is replaced by \begin{verbatim}
\csname iftrue\endcsname
\end{verbatim}
as the \cs{else} is seen while \TeX\ is still processing
\verb>\csname...\endcsname>.
Calls to \cs{say} occur inside nested conditionals, so
the number of \cs{expandafter} commands necessary may be
\alt
larger than~1: for level two it is~3, for level three
it is~7, and for level~4 it is 15. Slightly more compact
implementations of this macro do exist.
%%%% end of input file [ifelsefi]
%\InputFile:token
%%%% this is input file [token]
%\subject[token] Token Lists
\endofchapter
\chapter{Token Lists}\label{token}
\TeX\ has only one type of data structure: the token list.
\term token! lists\par\term list !token\par
There are token list registers that are available to the user,
and \TeX\ has some special token lists: the \cs{every...}
variables, \cs{errhelp}, and \cs{output}.
\begin{inventory}
\item [\cs{toks}]
Prefix for a token list register.
\item [\cs{toksdef}]
Define a control sequence to be a synonym for
a~\cs{toks} register.
\item [\cs{newtoks}]
Macro that allocates a token list register.
\end{inventory}
%\point Token lists
\section{Token lists}
Token lists are the only type of data structure that \TeX\ knows.
They can contain character tokens and control sequence tokens.
Spaces in a token list are significant.
The only operations on token lists are assignment and
unpacking.
\TeX\ has 256 token list registers \verb|\toks|$nnn$ that can be
allocated using the macro \verb|\newtoks|, or explicitly
assigned by \cs{toksdef}; see below.
%\point Use of token lists
\section{Use of token lists}
Token lists are assigned by a \gr{variable assignment},
which is in this case takes one of the forms
\begin{disp}\gr{token variable}\gr{equals}\gr{general text}\nl
\gr{token variable}\gr{equals}\gr{filler}\gr{token variable}\end{disp}
Here a \gr{token variable} is an explicit \cs{toks}$nnn$
register, something that has been defined to such a register
by \cs{toksdef} (probably hidden in \cs{newtoks}),
or one of the special \gr{token parameter}
lists below.
A~\gr{general text} has an explicit closing brace, but the
open brace can be implicit.
Examples of token lists are (the first two lines are equivalent):
\begin{verbatim}
\toks0=\bgroup \a \b cd}
\toks0={\a \b cd}
\toks1=\toks2
\end{verbatim}
Unpacking a token list is done by the command \cs{the}:
the expansion of \cs{the}\gr{token variable} is the
sequence of tokens that was in the token list.
Token lists have a special behaviour in \cs{edef}:
when prefixed by \verb|\the| they are unpacked,
but the resulting tokens
are not evaluated further. Thus
\begin{verbatim}
\toks0={\a \b} \edef\SomeCs{\the\toks0}
\end{verbatim}
gives
\begin{verbatim}
\SomeCs: macro:-> \a \b
\end{verbatim}
This is in contrast to what happens ordinarily in an~\cs{edef};
see page~\pageref{expand:edef}.
%\point \gr{token parameter}
\section{\gr{token parameter}}
There are in \TeX\ a number of token lists that are automatically
inserted at certain points. These \gr{token parameter}s are
the following:
\begin{description} \item [\cs{output}]
this token list is inserted
whenever \TeX\ decides it has sufficient material for a page,
or when the user forces activation by a penalty~$\leq-10\,000$
in vertical mode
(see Chapter~\ref{output});
\item [\cs{everypar}]
is inserted when \TeX\ switches from external or internal
vertical mode to unrestricted horizontal mode
(see Chapter~\ref{par:start});
\item [\cs{everymath}]
is inserted after a single math-shift character that starts
a formula;
\item [\cs{everydisplay}]
is inserted after a double math-shift character that starts
a display formula;
\item [\cs{everyhbox}]
is inserted when an \cs{hbox} begins (see Chapter~\ref{boxes});
\item [\cs{everyvbox}]
is inserted when a vertical box begins (see Chapter~\ref{boxes});
\item [\cs{everyjob}]
is inserted when a job begins (see Chapter~\ref{run});
\item [\cs{everycr}]
is inserted in alignments after \cs{cr} or a non-redundant
\cs{crcr} (see Chapter~\ref{align});
\item [\cs{errhelp}]
contains tokens to supplement an \cs{errmessage}
(see Chapter~\ref{error}).
\end{description}
A \gr{token parameter} behaves the same as an explicit \cs{toks}$nnn$
list, or a quantity defined by \cs{toksdef}.
%\point Token list registers
\section{Token list registers}
Token lists can be stored in \csidx{toks} registers:
\begin{Disp}\cs{toks}\gr{8-bit number}\end{Disp}
which is a \gr{token variable}.
Synonyms for token list registers can be made by the \gr{registerdef}
command \csidx{toksdef} in a \gr{shorthand definition}:
\begin{Disp}\cs{toksdef}\gr{control sequence}\gr{equals}\gr{8-bit number}
\end{Disp} A control sequence defined this way is called
a \gr{toksdef token}, and this is also a token variable
(the remaining third kind of token variable is
the \gr{token parameter}).
The plain \TeX\ macro \csidx{newtoks} uses \cs{toksdef} to
allocate unused token list registers. This macro is \cs{outer}.
%\point Examples
\section{Examples}
Token lists are probably among the least obvious components
of \TeX: most \TeX\ users will never find occasion for their use,
but format designers and other macro writers
can find interesting applications.
Following are some examples of the sorts of things that can be
done with token lists.
%\spoint Operations on token lists: stack macros
\subsection{Operations on token lists: stack macros}
The number of primitive operations available for token lists is
\howto Stack macros\par
rather limited: assignment and unpacking. However, these are
sufficient to implement other operations such as appending.
Let us say we have allocated a token register
\begin{verbatim}
\newtoks\list \list={\c}
\end{verbatim}
and we want to add tokens to it,
\alt
using the syntax
\begin{verbatim}
\Prepend \a \b (to:)\list
\end{verbatim}
such that \begin{verbatim}
\showthe\list
\end{verbatim}
gives \begin{verbatim}
> \a \b \c .
\end{verbatim}
For this the original list has to be unpacked, and
\alt
the new tokens followed by the old contents have to assigned
again to the register. Unpacking can be done with \cs{the}
inside an \cs{edef}, so we arrive at the following macro:
\begin{verbatim}
\def\Prepend#1(to:)#2{\toks0={#1}%
\edef\act{\noexpand#2={\the\toks0 \the#2}}%
\act}
\end{verbatim}
Note that the tokens that are to be added are first packed
\alt
into a temporary token list, which is then again unpacked
inside the \cs{edef}. Including them directly would have
led to their expansion.
Next we want to use token lists as a sort of stack:
we want a `pop' operation that removes the first element
from the list. Specifically,
\begin{verbatim}
\Pop\list(into:)\first
\show\first \showthe\list
\end{verbatim}
should give
\begin{verbatim}
> \first=macro:
->\a .
\end{verbatim}
and for the remaining list
\begin{verbatim}
> \b \c .
\end{verbatim}
Here we make creative use of delimited and undelimited
parameters. With an \cs{edef} we unpack the list,
and the auxiliary macro \cs{SplitOff} scoops up the elements
as one undelimited argument, the first element, and one
delimited argument, the rest of the elements.\begin{verbatim}
\def\Pop#1(into:)#2{%
\edef\act{\noexpand\SplitOff\the#1%
(head:)\noexpand#2(tail:)\noexpand#1}%
\act}
\def\SplitOff#1#2(head:)#3(tail:)#4{\def#3{#1}#4={#2}}
\end{verbatim}
%\spoint Executing token lists
\subsection{Executing token lists}
The \cs{the} operation for unpacking token lists was used above
only inside an \cs{edef}. Used on its own it has the effect
of feeding the tokens of the list to \TeX's expansion mechanism.
If the tokens have been added to the list in a uniform syntax,
this gives rise to some interesting possibilities.
Imagine that we are implementing the bookkeeping of external
files for a format. Such external files can be used for
table of contents, list of figures, et cetera.
If the presence
of such objects is under the control of the user, we need some
general routines for opening and closing files, and keeping
track of what files we have opened at the user's request.
Here only some routines for bookkeeping will be described.
Let us say there is a list of auxiliary files, and an auxiliary
counter: \begin{verbatim}
\newtoks\auxlist \newcount\auxcount
\end{verbatim}
First of all there must be an operation to add auxiliary files:
\begin{verbatim}
\def\NewAuxFile#1{\AddToAuxList{#1}%
% plus other actions
}
\def\AddToAuxList#1{\let\\=\relax
\edef\act{\noexpand\auxlist={\the\auxlist \\{#1}}}%
\act}
\end{verbatim}
This adds the name to the list in a uniform format:
\begin{verbatim}
\NewAuxFile{toc} \NewAuxFile{lof}
\showthe\auxlist
> \\{toc}\\{lof}.
\end{verbatim}
using the control sequence \verb>\\> which is left undefined.
Now this control sequence can be used for instance to
count the number of elements in the list:\begin{verbatim}
\def\ComputeLengthOfAuxList{\auxcount=0
\def\\##1{\advance\auxcount1\relax}%
\the\auxlist}
\ComputeLengthOfAuxList \showthe\auxcount
> 2.
\end{verbatim}
Another use of this structure is the following:
at the end of the job we can now close all auxiliary
files at once, by\begin{verbatim}
\def\CloseAuxFiles{\def\\##1{\CloseAuxFile{##1}}%
\the\auxlist}
\def\CloseAuxFile#1{\message{closing file: #1. }%
% plus other actions
}
\CloseAuxFiles
\end{verbatim}
which gives the output
\begin{verbatim}
closing file: toc. closing file: lof.
\end{verbatim}
% \begin{comment}
% %\spoint Dynamic macro definition
% \subsection{Dynamic macro definition}
% Unpacking token lists inside an \cs{edef} can be put to a
% rather ambitious use: dynamic definition of macros.
% Consider a simple example.
% \altt
% We set ourselves the goal of letting
% the user define macros, without ever having to use \cs{def}.
% The syntax for this could look like\begin{verbatim}
% \startdefinition
% \do:this
% \do:that
% \define:MyMacro
% \end{verbatim}
% such that \verb>\show\MyMacro> gives \begin{verbatim}
% > \MyMacro=macro:
% ->\this \that .
% \end{verbatim}
% An implementation of this uses a token list to collect
% the commands that the user specifies:\begin{verbatim}
% \newtoks\actionlist
% \end{verbatim}
% The first command is easy:\begin{verbatim}
% \def\startdefinition{\actionlist{}}
% \end{verbatim}
% Now the \cs{do} command has to hang control sequences
% in the \cs{actionlist}:\begin{verbatim}
% \def\do:#1 {%
% \edef\act{\noexpand\appendaction
% \expandafter\noexpand\csname#1\endcsname}%
% \act}
% \end{verbatim}
% The \cs{edef} is used solely to form the actual control sequence.
% The next macro uses \cs{edef} to unpack the \cs{actionlist} so far:
% \begin{verbatim}
% \def\appendaction#1{%
% \edef\act{\noexpand\actionlist=
% {\the\actionlist \noexpand#1}}%
% \act}
% \end{verbatim}
% Finally, definition of the user macro also needs an \cs{edef}.
% Some \cs{expandafter} trickery is necessary here to form
% the control sequence of the user macro:\begin{verbatim}
% \def\define:#1 {%
% \expandafter\edef\csname#1\endcsname{\the\actionlist}}
% \end{verbatim}
% Of course, this is a very simple, rather pointless, example.
% However, it illustrates an important principle of how
% token lists can be used to implement another syntax level
% in \TeX\ (see~\cite{EL}). This principle underlies the
% \term Lollipop\par
% `Lollipop' format that was used to typeset this book.
% \end{comment}
%%%% end of input file [token]
%\InputFile:baseline
%%%% this is input file [baseline]
%\subject[baseline] Baseline Distances
\endofchapter
\chapter{Baseline Distances}\label{baseline}
\hbox{}\vfil\vfil\hbox{}
Lines of text are in most cases not of equal height or depth.
Therefore \TeX\ adds interline glue to keep baselines at a uniform
distance from one another.
This chapter treats the computation of such
interline glue.
\begin{inventory}
\item [\cs{baselineskip}]
The `ideal' baseline distance between neighbouring
boxes on a vertical list. Plain \TeX\ default:~\n{12pt}.
\item [\cs{lineskiplimit}]
Distance to be maintained between the bottom and top of
neighbouring boxes on a vertical list.
Plain \TeX\ default:~\n{0pt}.
\item [\cs{lineskip}]
Glue added if the distance between bottom
and top of neighbouring boxes
is less than \cs{lineskiplimit}.
Plain \TeX\ default:~\n{1pt}.
\item [\cs{prevdepth}]
Depth of the last box added to a vertical list as it is
perceived by \TeX.
\item [\cs{nointerlineskip}]
Macro to prevent interline glue insertion once.
\item [\cs{offinterlineskip}]
Macro to prevent interline glue globally
henceforth.
\item [\cs{openup}]
Increase \cs{baselineskip}, \cs{lineskip},
and \cs{lineskiplimit} by specified amount.
\end{inventory}
\hbox{}\vfil\hbox{}
%\point Interline glue
\section{Interline glue}
%\input figs17
\message{fig17 missing}
\TeX\ tries to keep a certain distance between the reference
\term glue !interline\par\term baseline! distance\par
points of boxes that are added to a vertical list;
in particular it tries to keep the baselines of ordinary text
at a constant distance, the \csidx{baselineskip}. Actually,
the \cs{baselineskip} is a \gr{glue}, so line distances can
stretch or shrink. However, the natural sizes,
as well as the stretch and the shrink, are the same
between all lines.
When boxes, whether they are lines of a paragraph or explicit boxes,
are appended to a vertical list, glue
is added usually so that the depth of the preceding box
and the height of the current one add up to the \cs{baselineskip}.
This has the effect of keeping the reference points
of subsequent lines at regular intervals.
\eject
\message{fig one missing}
%\begin{disp}\leavevmode\hbox{}\nl\figone\end{disp}
However, this process can bring the bottom and top of two
subsequent boxes to be less than \cs{lineskiplimit} apart:
\message{fig two missing}
%\begin{disp}\leavevmode\figtwo\end{disp}
In that case, \cs{lineskip} glue is added:
\message{fig three missing}
%\begin{disp}\leavevmode\figthree\end{disp}
Note that this will usually increase the distance
between the baselines of the boxes to more than the
\cs{baselineskip}.
The exact process is this:
\begin{itemize}
\item if \cs{prevdepth} is \n{-1000pt} or less,
no glue is added, otherwise
\item \TeX\ calculates the distance between the bottom of the previous box
and the top of the current one as the natural width of the
\cs{baselineskip} minus \cs{prev\-depth} (the
depth of the last box) and minus the height of the current box;
\item if this distance is at least \csidx{lineskiplimit},
glue is added with the calculated distance as natural size,
and with the stretch and shrink of the \cs{baselineskip},
\item otherwise \csidx{lineskip} glue is added.
\item \csidx{prevdepth} is set to the depth of the
current item.
\end{itemize}
There are two exceptional situations:
no interline glue is added before and after a rule,
and the \cs{prevdepth} is not updated by an \cs{unvbox}
or \cs{unvcopy} command. After a rule interline glue
is prevented by a value of \n{-1000pt} of the \cs{prevdepth}.
The above process is carried out, irrespective of what extra
glue may have been inserted in between the boxes.
Thus a skip in between boxes in vertical mode will not
affect the distance calculated from the baseline distances,
and therefore also not the amount of baselineskip glue.
The same holds for glue added with \cs{vadjust} inside
a paragraph.
\begin{example}\begin{verbatim}
\baselineskip=10pt \lineskiplimit=2pt \lineskip=2pt
\setbox0=\vbox{\hbox{\vrule depth4pt}
\hbox{\vrule height 3pt}}
\showbox0
\end{verbatim}
gives\begin{verbatim}
\box0=
\vbox(10.0+0.0)x0.4
.\hbox(0.0+4.0)x0.4
..\rule(*+4.0)x0.4
.\glue(\baselineskip) 3.0
.\hbox(3.0+0.0)x0.4
..\rule(3.0+*)x0
\end{verbatim}
Bringing the boxes to within \cs{lineskiplimit}
of each other, that is\begin{verbatim}
\setbox0\vbox{\hbox{\vrule depth4pt}
\hbox{\vrule height 5pt}}
\showbox0
\end{verbatim}
gives\begin{verbatim}
\box0=
\vbox(11.0+0.0)x0.4
.\hbox(0.0+4.0)x0.4
..\rule(*+4.0)x0.4
.\glue(\lineskip) 2.0
.\hbox(5.0+0.0)x0.4
..\rule(5.0+*)x0.4
\end{verbatim}
where \cs{lineskip} glue has been inserted
instead of the usual \cs{baselineskip} glue.
\end{example}
The plain \TeX\ default values are
\begin{verbatim}
\lineskiplimit=0pt lineskip=1pt
\end{verbatim}
so, when boxes start to touch each other, they are
moved one point apart.
%\point The perceived depth of boxes
\section{The perceived depth of boxes}
The decision process for interline glue uses \csidx{prevdepth}
as the perceived depth of the preceding box on the vertical
list. The \cs{prevdepth} parameter can be used only in
vertical mode.
The \cs{prevdepth} is set to the depth of boxes added to the
vertical list, but it is not affected by \cs{unvbox}
or \cs{unvcopy}. After an \cs{hrule} it is set to
\n{-1000pt} to prevent interline glue before the next box.
At the beginning of a vertical list \cs{prevdepth}
is set to \n{-1000pt}, except in an \cs{halign}
and \cs{noalign} code contained therein, where it
is carried over from the surrounding list.
At the end of the alignment the value of \cs{prevdepth}
set by the last alignment row is carried to the outer list.
In order to prevent interline glue just once, all that
is needed is to alter the \cs{prevdepth}.
\cstoidx nointerlineskip\par
\begin{verbatim}
\def\nointerlineskip{\prevdepth=-1000pt}
\end{verbatim}
The \csidx{offinterlineskip} macro is much more drastic:
it prevents {\sl all\/} interline glue from the moment
of its call onwards, or, if it is used inside a paragraph,
from the start of that paragraph.
Its definition is
\begin{verbatim}
\baselineskip=-1000pt \lineskip=0pt
\lineskiplimit\maxdimen
\end{verbatim}
where the second line is the essential one: it
causes \TeX\ to add \cs{lineskip} glue (which is zero)
always.
Settings for \cs{baselineskip} do not matter any more then.
The \cs{offinterlineskip} macro has an important application
in alignments (see Chapter~\ref{align}).
By setting \begin{verbatim}
\lineskiplimit=-\maxdimen
\end{verbatim}
you can force \TeX\ to apply the \cs{baselineskip}
always, regardless of whether this would bring boxes too close
together or, indeed, if this would make them overlap.
%\point Terminology
\section{Terminology}
In hot metal typesetting, all letters of a particular font
were on a `body' of the same
size. Thus every line of type had the same height and depth, and
the resulting distance between the baselines would be some suitable
value for that type. If for some reason this distance should
be larger (see~\cite{White:line} for a discussion of this),
strips of lead would be inserted. The extra distance was
called the `leading' (pronounced `ledding').
With phototypesetting, when the baseline distance was sometimes
called the `film transport', this terminology blurred, and the
term `leading' was also used for the baseline distance. Some of this
confusion is also present in \TeX: the parameter \cs{baselineskip}
specifies the baseline distance, but in the trace output
(see the examples above) the glue inserted to make the
baseline distance equal to \cs{baselineskip} is called
\cs{baselineskip}.
%\point Additional remarks
\section{Additional remarks}
In general, for documents longer than one page it is desirable
to have the same baseline distance throughout. However,
for one-page documents you may add stretchability to the
baselineskip, for instance if the text has to be flush bottom.
Increasing the distance between just one pair of lines
can be done with \cs{vadjust}. The argument of this
command is vertical material that\vadjust{\kern2pt}
will be inserted in the
vertical list right after the line where this command was given.
The second line of this paragraph, for instance,
contains the command \verb-\vadjust{\kern2pt}-.
The amount of leading cannot be changed in the middle of
a paragraph, because the value for \cs{baselineskip}
that is used is the one that
is current when the paragraph is finally broken and
added to the main vertical list. The same holds
for the \cs{lineskip} and \cs{lineskiplimit}.
The plain \TeX\ macro \csidx{openup}
increases the \cs{baselineskip}, \cs{lineskip}, and
\cs{lineskiplimit} by the amount of the argument
to the macro. In effect, this increases line distances
by this amount regardless of whether they are governed
by \cs{baselineskip} or \cs{lineskip}.
%%%% end of input file [baseline]
%\InputFile:par
%%%% this is input file [par]
%\subject[par:start] Paragraph Start
\endofchapter
\chapter{Paragraph Start}\label{par:start}
At the start of a paragraph \TeX\ inserts a vertical skip
as a separation from the preceding paragraph, and a horizontal
skip as an indentation for the current paragraph.
This chapter explains the exact sequence
of actions,
and it discusses how \TeX's decisions can be altered.
\begin{inventory}
\item [\cs{indent}]
Switch to horizontal mode and insert a box of width \cs{parindent}.
\item [\cs{noindent}]
Switch to horizontal mode with an empty horizontal list.
\item [\cs{parskip}]
Amount of glue added to
the surrounding vertical list when a paragraph starts.
Plain \TeX\ default:~\n{0pt plus 1pt}.
\item [\cs{parindent}]
Size of the indentation box added in front of a paragraph.
Plain \TeX\ default:~\n{20pt}.
\item [\cs{everypar}]
Token list inserted in front of paragraph text;
\item [\cs{leavevmode}]
Macro to switch to horizontal mode if necessary.
\end{inventory}
%\point When does a paragraph start
\section{When does a paragraph start}
\TeX\ starts a paragraph whenever it switches from
vertical mode to (unrestricted) horizontal mode. This switch can
be effected by one of the commands
\cs{indent} and
\cs{noindent}, for example\begin{verbatim}
{\bf And now~\dots}
\vskip3pt
\noindent It's~\dots
\end{verbatim}
or by any \gram{horizontal command}.
Horizontal commands include characters, in-line formulas,
and horizontal skips, but not boxes.
Consider the following examples.
\alt
The character `I' is a horizontal command:
\begin{verbatim}
\vskip3pt
It's~\dots
\end{verbatim}
A single \n\$ is a horizontal command:
\begin{verbatim}
$x$ is supposed~\dots
\end{verbatim}
The control sequence \cs{hskip} is a horizontal command:
\begin{verbatim}
\hskip .5\hsize Long indentation~\dots
\end{verbatim}
The full list of horizontal commands is given on
page~\pageref{h:com:list}.
Upon recognizing a horizontal command in vertical mode,
\TeX\ will perform an \cs{indent} command (and all the actions
associated with it; see below),
and after that it will reexamine the horizontal command,
this time executing it.
%\point What happens when a paragraph starts
\section{What happens when a paragraph starts}
The \csidx{indent} and \csidx{noindent} commands
\term paragraph! start\par
cause a paragraph to be started.
An~\cs{indent} command can either be placed explicitly by
the user or a macro, or it can be inserted by \TeX\ when
a \gr{horizontal command} occurs in vertical mode;
a~\cs{noindent} command can only be placed explicitly.
After either command is encountered,
\csidx{parskip} glue is appended to the surrounding vertical
list
unless \TeX\ is in internal vertical mode
and that list is empty
(for example, at the start of a \cs{vbox} or \cs{vtop}).
\TeX\ then switches to unrestricted horizontal mode
with an empty horizontal list.
In the case of \cs{indent} (which may be inserted
implicitly) an empty \cs{hbox} of width
\cstoidx parindent\par
\cs{parindent} is placed at the start of the horizontal list;
after \cs{noindent} no indentation
box is inserted.
The contents of the \csidx{everypar} \gr{token parameter}
are then inserted into the input (see some applications below).
After that,
the page builder is exercised (see Chapter~\ref{page:break}).
Note that this happens in horizontal mode: this is to
move the \cs{parskip} glue to the current page.
If an \cs{indent} command is given while \TeX\ is already in
horizontal mode, the indentation box is inserted just the same.
This is not very useful.
%\point Assorted remarks
\section{Assorted remarks}
%\spoint Starting a paragraph with a box
\subsection{Starting a paragraph with a box}
An \cs{hbox} does not imply horizontal mode, so
an attempt to start a paragraph with a box, for instance
\begin{verbatim}
\hbox to 0cm{\hss$\bullet$\hskip1em}Text ....
\end{verbatim}
will make the text following the box
wind up one line below the box.
It is necessary to switch to horizontal mode
explicitly, using for instance \cs{noindent} or
\cs{leavevmode}.
The latter is defined using \cs{unhbox},
which is a horizontal command.
%\spoint Starting a paragraph with a group
\subsection{Starting a paragraph with a group}
If the first \gram{horizontal command} of a paragraph
is enclosed in braces, the \cs{everypar} is evaluated
inside the group. This may give unexpected results.
Consider this example:
\begin{verbatim}
\everypar={\setbox0=\vbox\bgroup\def\par{\egroup}}
{\bf Start} a paragraph ... \par
\end{verbatim}
The \gr{horizontal command} starting the paragraph is the
character~`S', so when \cs{everypar} has been inserted
the input is essentially
\begin{verbatim}
{\bf \indent\setbox0=\vbox\bgroup
\def\par{\egroup}Start} a paragraph ... \par
\end{verbatim}
which is equivalent to
\begin{verbatim}
{\bf \setbox0=\vbox{Start} a paragraph ... \par
\end{verbatim}
The effect of this is rather different from what was intended.
\alt
Also, \TeX\ will probably end the job inside a group.
%\point Examples
\section{Examples}
%\spoint Stretchable indentation
\subsection{Stretchable indentation }
Considering that \cs{parindent} is a \gram{dimen}, not a \gram{glue},
it is not possible to declare
\begin{verbatim}
\parindent=1cm plus 1fil
\end{verbatim}
in order to get
a variable indentation at the start of a paragraph.
This problem may be solved by putting
\begin{verbatim}
\everypar={\nobreak\hskip 1cm plus 1fil\relax}
\end{verbatim}
The \cs{nobreak} serves to prevent (in rare cases) a line break
at the stretchable glue.
%\spoint Suppressing indentation
\subsection{Suppressing indentation}
Inserting
\verb.{\setbox0=\lastbox}. in the horizontal list
at the beginning of the paragraph
removes the indentation:
indentation consists of a box, which is available through
\cs{lastbox}. Assigning it effectively removes it from the list.
However, this command sequence
has to be inserted at a moment when \TeX\ has
already switched to horizontal mode, so explicit insertion
of these commands in front of the first \gram{horizontal
command} of the paragraph does not work.
The moment of insertion of the \cs{everypar} tokens
is a better candidate: specifying
\begin{verbatim}
\everypar={{\setbox0=\lastbox}}
\end{verbatim}
leads to unindented paragraphs, even if \cs{parindent} is
not zero.
%\spoint[indent:scheme] An indentation scheme
\subsection{An indentation scheme}
\label{indent:scheme}
The above idea of letting the indentation box be removed
\howto Control indentation systematically\par
by \cs{everypar} can be put to use in a systematic approach
to indentation, where two conditionals
\begin{verbatim}
\newif\ifNeedIndent %as a rule
\newif\ifneedindent %special cases
\end{verbatim}
control whether paragraphs should indent as a rule, and
whether in special cases indentation is needed.
This section is taken from~\cite{E3}.
We take a fixed \cs{everypar}:
\begin{verbatim}
\everypar={\ControlledIndentation}
\end{verbatim}
which executes in some cases the macro \cs{RemoveIndentation}
\begin{verbatim}
\def\RemoveIndentation{{\setbox0=\lastbox}}
\end{verbatim}
The implementation of \cs{ControlledIndentation} is:\begin{verbatim}
\def\ControlledIndentation
{\ifNeedIndent \ifneedindent
\else \RemoveIndentation\needindenttrue \fi
\else \ifneedindent \needindentfalse
\else \RemoveIndentation
\fi \fi}
\end{verbatim}
In order to regulate indentation for a whole document,
the user now once specifies, for instance,
\begin{verbatim}
\NeedIndenttrue
\end{verbatim}
to indicate that, in principle,
all paragraphs should indent.
Macros such as \cs{section} can then prevent
indentation in individual cases:
\begin{verbatim}
\def\section#1{ ... \needindentfalse}
\end{verbatim}
%\spoint[skip:scheme] A paragraph skip scheme
\subsection{A paragraph skip scheme}
\label{skip:scheme}
The use of \cs{everypar} to control indentation,
\howto Control vertical white space systematically\par
as was sketched above, can be extended to the
paragraph skip.
A visible white space between paragraphs can be
created by the \cs{parskip} parameter, but, once this
parameter has been set to some value, it is difficult
to prevent paragraph skip in certain places elegantly.
Usually, white space above and below environments
and section headings should be specifiable independently
of the paragraph skip. This section sketches an
approach where \cs{parskip} is set to zero directly
above and below certain constructs, while the \cs{everypar}
is used to restore former values. This section is
taken from~\cite{E4}.
First of all, here are two tools. The control sequence
\cs{csarg} will be used only inside other macros;
a typical call will look like
\begin{verbatim}
\csarg\vskip{#1Parskip}
\end{verbatim}
Here is the definition:\begin{verbatim}
\def\csarg#1#2{\expandafter#1\csname#2\endcsname}
\end{verbatim}
Next follows a generalization of \cs{vskip}: the macro
\cs{vspace} will not place its argument if the previous glue item
is larger; otherwise it will eliminate the preceding
glue, and place its argument.\begin{verbatim}
\newskip\tempskipa
\def\vspace#1{\tempskipa=#1\relax
\ifvmode \ifdim\tempskipa<\lastskip
\else \vskip-\lastskip \vskip\tempskipa \fi
\else \vskip\tempskipa \fi}
\end{verbatim}
Now assume that any construct \n{foo}
with surrounding white space
starts and ends with macro calls \verb>\StartEnvironment{foo}> and
\verb>\EndEnvironment{foo}> respectively.
Furthermore, assume that to this environment there correspond
three glue registers:
the \cs{fooStartskip} (glue
above the environment), \cs{fooParskip} (the paragraph skip
inside the environment), and the \cs{fooEndskip} (glue below
the environment).
For restoring the value of the paragraph skip
a conditional and a glue register are needed:\begin{verbatim}
\newskip\TempParskip \newif\ifParskipNeedsRestoring
\end{verbatim}
The basic sequence for the
starting and ending macros for the environments is then
\begin{verbatim}
\TempParskip=\parskip\parskip=0cm\relax
\ParskipNeedsRestoringtrue
\end{verbatim}
The implementations can now be given as:\begin{verbatim}
\def\StartEnvironment#1{\csarg\vspace{#1Startskip}
\begingroup % make changes local
\csarg\TempParskip{#1Parskip} \parskip=0cm\relax
\ParskipNeedsRestoringtrue}
\def\EndEnvironment#1{\csarg\vspace{#1Endskip}
\endgroup % restore global values
\ifParskipNeedsRestoring
\else \TempParskip=\parskip \parskip=0cm\relax
\ParskipNeedsRestoringtrue
\fi}
\end{verbatim}
The \cs{EndEnvironment} macro needs a little comment:
if an environment is used inside another one, and
it occurs before the first paragraph in that environment,
the value of the paragraph skip for the outer environment
has already been saved. Therefore no further actions are
required in that case.
Note that both macros start with a vertical skip. This prevents
the \cs{begingroup} and \cs{endgroup} statements from
occurring in a paragraph.
We now come to the main point: if necessary, the
\cs{everypar} will restore the value of the paragraph skip.
\begin{verbatim}
\everypar={\ControlledIndentation\ControlledParskip}
\def\ControlledParskip
{\ifParskipNeedsRestoring
\parskip=\TempParskip \ParskipNeedsRestoringfalse
\fi}
\end{verbatim}
%\subject[par:end] Paragraph End
\endofchapter
\chapter{Paragraph End}\label{par:end}
\TeX's mechanism for ending a paragraph is ingenious and effective.
This chapter explains the mechanism, the role of \cs{par} in it,
and it gives a number of practical remarks.
\begin{inventory}
\item [\cs{par}]
Finish off a paragraph and go into vertical mode.
\item [\cs{endgraf}]
Synonym for \cs{par}: \verb>\let\endgraf=\par>
\item [\cs{parfillskip}]
Glue that is placed between the last
element of the paragraph and the line end.
Plain \TeX\ default:~\n{0pt plus 1fil}.
\end{inventory}
%\point The way paragraphs end
\section{The way paragraphs end}
A paragraph is terminated by the primitive \cs{par} command,
\term paragraph! end\par
which can
be explicitly typed by the user (or inserted by
a macro expansion):\begin{verbatim}
... last words.\par
A new paragraph ...
\end{verbatim}
It can be implicitly generated in the input processor of \TeX\
by an empty line (see Chapter~\ref{mouth}):\begin{verbatim}
... last words.
A new paragraph ...
\end{verbatim}
The \cs{par} can be inserted because a \gr{vertical command}
occurred in unrestricted horizontal mode:\begin{verbatim}
... last words.\vskip6pt
A new paragraph ...
\end{verbatim}
Also, a paragraph ends if a closing brace is found
in horizontal mode inside \cs{vbox}, \cs{insert}, or \cs{output}.
After the \cs{par} command \TeX\ goes into vertical mode
and exercises the page builder (see page~\pageref{par:page:build}).
If the \cs{par} was inserted because a vertical command occurred in
horizontal mode, the vertical command is then examined anew.
The \cs{par} does not insert any vertical
glue or penalties itself. A~\cs{par} command also clears
the paragraph shape parameters (see Chapter~\ref{par:shape}).
%\spoint The \cs{par} command and the \cs{par} token
\subsection{The \cs{par} command and the \cs{par} token}
It is important to distinguish between the \cs{par} token
and the primitive \cs{par} command that is the initial meaning of
that token. The \cs{par} token is inserted when the input
processor sees an empty
line, or when the execution processor finds a \gram{vertical command}
in horizontal mode;
the \cs{par} command is what actually closes off a paragraph.
Decoupling the token and the command is an important tool
for special effects in paragraphs (see some examples in
Chapters \ref{boxes} and~\ref{rules}).
%\spoint Paragraph filling: \cs{parfillskip}
\subsection{Paragraph filling: \cs{parfillskip}}
After the last element of the paragraph \TeX\ implicitly inserts
the equivalent of
\cstoidx parfillskip\par
\begin{verbatim}
\unskip \penalty10000 \hskip\parfillskip
\end{verbatim}
The \cs{unskip} serves to remove any spurious glue at the
paragraph end, such as the space generated by the
line end if the \cs{par} was inserted by the input processor.
For example:\message{check unsplit paragraph example}
\begin{verbatim}
end.
\noindent Begin
\end{verbatim}
results in the tokens
\begin{disp}\n{end.\char32}\cs{par} \n{Begin}\end{disp}
With the sequence inserted by the \cs{par} this becomes
\begin{disp}\n{end.\char32}\verb>\unskip\penalty10000\hskip ...>\end{disp}
which in turn gives
\begin{disp}\verb>end.\penalty ...>\end{disp}
The \cs{parfillskip} is in plain \TeX\ first-order infinite
(\n{0pt plus 1fil}),
so ending a paragraph with \verb.\hfil$\bullet$\par.
will give a bullet halfway between the last word and the
line end; with \verb.\hfill$\bullet$\par. it will be
flush right.
%\point Assorted remarks
\section{Assorted remarks}
%\spoint Ending a paragraph and a group at the same time
\subsection{Ending a paragraph and a group at the same time}
If a paragraph is set in a group,
it may be necessary to ensure that the \cs{par} ending
the paragraph occurs inside the group.
The parameters influencing the typesetting of the paragraph,
such as the \cs{leftskip} and the \cs{baselineskip},
are only looked at when the paragraph is finished.
Thus finishing off a paragraph with
\begin{verbatim}
... last words.}\par
\end{verbatim}
causes the values to be used
that prevail outside the group, instead of those inside.
Better ways to end the paragraph are
\begin{verbatim}
... last words.\par}
\end{verbatim}
or
\begin{verbatim}
... last words.\medskip}
\end{verbatim}
In the second example the vertical command \cs{medskip}
causes the \cs{par} token to be inserted.
%\spoint Ending a paragraph with \cs{hfill}\cs{break}
\subsection{Ending a paragraph with \cs{hfill}\cs{break}}
The sequence \verb.\hfill\break. is a way to force
a `newline' inside a paragraph. If you end a paragraph
with this, however, you will probably
get an \verb-Underfull \hbox- error.
Surprisingly, the underfull box is not the broken line
\ldash after all, that one was filled \rdash
but a completely empty box following it (actually, it
does contain the \cs{leftskip} and \cs{rightskip}).
What happens?
The paragraph ends with \begin{verbatim}
\hfill\break\par
\end{verbatim}
which turns into
\begin{verbatim}
\hfill\break\unskip\nobreak\hskip\parfillskip
\end{verbatim}
The \cs{unskip} finds no preceding glue, so the \cs{break}
is followed by a penalty item and a glue item, both of
which disappear after the line break has been chosen at the
\cs{break}.
However, \TeX\ has already decided that there should be an extra
line, that is, an \verb.\hbox to \hsize.. And there is nothing
\alt
to fill it with, so an underfull box results.
%\spoint Ending a paragraph with a rule
\subsection{Ending a paragraph with a rule}
See page~\pageref{par:leaders:end} for paragraphs ending with
rule leaders instead of the default \cs{parfillskip}
white space.
%\spoint No page breaks in between paragraphs
\subsection{No page breaks in between paragraphs}
The \cs{par} command does not insert any glue in the
\howto Prevent page breaks in between paragraphs\par
vertical list, so
in the sequence
\begin{verbatim}
... last words.\par \nobreak \medskip
\noindent First words ...
\end{verbatim}
no page breaks will occur between the paragraphs.
The vertical list generated is
\begin{verbatim}
\hbox(6.94444+0.0)x ... % last line of paragraph
\penalty 10000 % \nobreak
\glue 6.0 plus 2.0 minus 2.0 % \medskip
\glue(\parskip) 0.0 plus 1.0 % \parskip
\glue(\baselineskip) 5.05556 % interline glue
\hbox(6.94444+0.0)x ... % first line of paragraph
\end{verbatim}
\TeX\ will not break this vertical list above the \cs{medskip},
because the penalty value prohibits it; it will not break
at any other place, because it can only break at glue if
that glue is preceded by a non-discardable item.
%\spoint Finite \cs{parfillskip}
\subsection{Finite \cs{parfillskip}}
In plain \TeX, \cs{parfillskip} has a (first-order) infinite
stretch component. All other glue in the last line of a
paragraph will then be set at natural width.
If the \cs{parfillskip} has only finite (or possibly zero)
stretch, other glue will be stretched or shrunk.
A display formula in a paragraph with such a last line
will be surrounded by \cs{abovedisplayskip} and \cs{belowdisplayskip},
even if \cs{abovedisplayshortskip} glue would be in order.
The reason for this is that glue setting is slightly
machine-dependent, and any such processes should be kept
out of \TeX's global decisions.
%\spoint A precaution for paragraphs that do not indent
\subsection{A precaution for paragraphs that do not indent}
If you are setting a text with both the paragraph indentation
and the white space between paragraphs zero, you run the risk
that the start of a new paragraph may be indiscernible when
the last line of the previous paragraph ends almost
or completely flush right.
A~sensible precaution for this is to set the \cs{parfillskip}
to, for instance \begin{verbatim}
\parfillskip=1cm plus 1fil
\end{verbatim}
instead of the usual \n{0cm~plus~1fil}.
On the other hand, you may let yourself be convinced by
\cite{Tsch} that paragraphs should always indent.
%\subject[par:shape] Paragraph Shape
\endofchapter
\chapter{Paragraph Shape}\label{par:shape}
This chapter treats the parameters and commands that influence the
\term paragraph! shape\par
shape of a paragraph.
\begin{inventory}
\item [\cs{parindent}]
Width of the indentation box added in front of a paragraph.
Plain \TeX\ default:~\n{20pt}.
\item [\cs{hsize}]
Line width used for typesetting a paragraph.
Plain \TeX\ default:~\n{6.5in}.
\item [\cs{leftskip}]
Glue that is placed to the left of all lines of a paragraph.
\item [\cs{rightskip}]
Glue that is placed to the right of all lines of a paragraph.
\item [\cs{hangindent}]
If positive, this indicates indentation from the left margin;
if negative, this is the negative of the indentation
from the right margin.
\item [\cs{hangafter}]
If positive, this denotes the number of lines
before indenting starts;
if negative, the absolute value of this is the number
of indented lines starting with the first line of the paragraph.
Default:~\n1.
\item [\cs{parshape}]
Command for general paragraph shapes.
\end{inventory}
%\point The width of text lines
\section{The width of text lines}
When \TeX\ has finished absorbing a paragraph,
\term line! width\par
it has formed a horizontal list, starting with an indentation
box, and ending with \cs{parfillskip} glue.
This list is then broken into lines of length \cs{hsize}.
\cstoidx hsize\par\cstoidx leftskip\par\cstoidx rightskip\par
Each line of a paragraph is padded left and right with
certain amounts of glue, the \cs{leftskip} and \cs{rightskip},
which are taken into account in reaching \cs{hsize}.
The values of \cs{leftskip} and \cs{rightskip} are taken
into account in the line-breaking algorithm.
Thus the main point about the \csidx{raggedright}
macro in plain \TeX\ and the \LaTeX\ `flushleft'
environment is that they
set the \cs{rightskip} to zero plus some stretch.
The commands \cs{parshape} and \cs{hangindent}
also affect line width. They work by altering the
\cs{hsize} and afterwards shifting the boxes
containing the lines.
%\point Shape parameters
\section{Shape parameters}
%\spoint Hanging indentation
\subsection{Hanging indentation}
\message{twolines?}
A simple, and frequently occurring, paragraph shape is that
\term hanging! indentation\par
\cstoidx hangafter\par\cstoidx hangindent\par
with a number of starting or trailing lines indented.
\TeX\ can realize such shapes using two parameters:
\cs{hangafter} and \cs{hangindent}.
Both can assume positive and negative values.
The \cs{hangindent} controls the amount of indentation:
\begin{itemize}\item \cs{hangindent}${}>0$: the paragraph
is indented at the left margin by this amount.
\item\cs{hangindent}${}<0$: the paragraph is indented
at the right margin by the absolute value of this amount.
\end{itemize}
\def\exnul{\leftskip=0pt \rightskip=0pt \relax}
For example (assume \cs{parindent=0pt}),
\begin{disp}\leavevmode\message{Check parshape example!}%
\hbox{%\Distance:verbatimwhiteleft=0pt
$\vcenter{\snugbox{\begin{verbatim}
a a a a a a a a a a a a ...
\hangindent=10pt
a a a a a a a a a a a a ...
\hangindent=-10pt
a a a a a a a a a a a a ...
\end{verbatim}
}}$\quad gives\quad %\Spaces:2 gives \Spaces:2
$\vcenter{\parindent0pt \setbox0\hbox{a a a a a}\hsize\wd0
\leftskip=0pt %\parskip6pt
a a a a a a a a a a a a \dots\par%\vskip\baselineskip
\hangindent=10pt
a a a a a a a a a a a a \dots\par%\vskip\baselineskip
\hangindent=-10pt
a a a a a a a a a a a a \dots\par}$
}\end{disp}
The default value of \cs{hangindent} is~\n{0pt}.
The \cs{hangafter} parameter determines the number of
lines that is indented:
\begin{itemize}\item \cs{hangafter}${}\geq0$:
after this number of lines the rest of the lines will be
indented; in other words, this many lines from the
start of the paragraph will not be indented.
\item \cs{hangafter}${}<0$: the absolute value of this
is the number of lines that will be indented starting
at the beginning of the paragraph.\end{itemize}
For example,
\message{check left align}
\begin{disp}\leavevmode\hbox{%\Distance:verbatimwhiteleft=0pt
$\vcenter{\snugbox{\begin{verbatim}
a a a a a a a a a a a a ...
\hangindent=10pt \hangafter=2
a a a a a a a a a a a a ...
\hangindent=10pt \hangafter=-2
a a a a a a a a a a a a ...
\end{verbatim}
}}$%\quad looks like\quad% \Spaces:2 looks like \Spaces:2
$\vcenter{\parindent0pt \setbox0\hbox{a a a a a}\hsize\wd0
\leftskip=0pt %\parskip6pt
a a a a a a a a a a a a \dots\par%\vskip\baselineskip
\hangindent=10pt \hangafter=2
a a a a a a a a a a a a \dots\par%\vskip\baselineskip
\hangindent=10pt \hangafter=-2
a a a a a a a a a a a a \dots\par}$
}\end{disp}
The default value for \cs{hangafter} is~\n1.
With both parameters having the possibility to
be positive and negative,
four ways of hanging indentation result. See below
for hanging indentation into the margin (`outdent').
Hanging indentation is implemented as follows.
The amount of hanging indentation is subtracted
from the \cs{hsize} for the lines that indent;
after the paragraph has been broken into horizontal
boxes, the lines that should indent on the left are
shifted right.
Regular indentation of size \cs{parindent} is not
influenced by hanging indentation. Thus you should
start a paragraph with hanging indentation
explicitly by~\cs{noindent} if the extra
indentation is unwanted.
The default values of \cs{hangindent} and \cs{hangafter} are
restored after every \cs{par} command.
%\spoint General paragraph shapes: \cs{parshape}
\subsection{General paragraph shapes: \cs{parshape}}
Quite general paragraph shapes can be implemented
using \csidx{parshape}. With this command line lengths and indentation
for the first $n$ lines
of a paragraph can be specified. Thus this command
takes $2n+1$ parameters: the number of lines $n$, followed
by $n$ pairs of an indentation and a line length.
\begin{disp} \cs{parshape}\gr{equals}
$n$ $i_1$ $\ell_1$ $\ldots$ $i_n$ $\ell_n$\end{disp}
The specification for the last line is repeated if the
paragraph following has more than $n$ lines. If there are fewer
than $n$ lines the remaining specifications are ignored.
The default value is (naturally) \cs{parshape${}={}$0}.
A \cs{parshape} command takes precedence over a \cs{hangindent}
if both have been specified.
%Regular \cs{parindent} indentation
%is suppressed if \cs{parshape} is in effect.
Regular \cs{parindent}, \cs{leftskip},
and \cs{rightskip} are still obeyed if \cs{parshape} is in effect.
The \cs{parshape} parameter is, like \cs{hangindent}, \cs{hangafter},
and \cs{looseness} (see Chapter~\ref{line:break}),
cleared after a \cs{par}
command. Since every empty line generates a \cs{par} token,
one should not leave an empty line
between a paragraph shape (or hanging indentation)
declaration and the following paragraph.
The control sequence
\alt
\cs{parshape} is an \gr{internal integer}:
its value is the number of lines $n$ with which
it was set.
%\point Assorted remarks
\section{Assorted remarks}
%\spoint Centred last lines
\subsection{Centred last lines}
Equal stretch and shrink amounts for the \cs{leftskip} and
\cs{rightskip}
give centred texts, in the sense that each line is
centred.
For proper centring of the first
and last lines of a paragraph the \cs{parindent} and
\cs{parfillskip} have to be made zero.
However, the margins are ragged.
A surprising application of \cs{leftskip} and \cs{rightskip}
\mdqon
\howto Centre the first/""last line of a paragraph\par
\mdqoff
leads to paragraphs with flush margins and a centred
last line.
\begin{verbatim}
\leftskip=0cm plus 0.5fil \rightskip=0cm plus -0.5fil
\parfillskip=0cm plus 1fil
\end{verbatim}
For all lines of a paragraph but the
last one the stretch components
add up to zero so the \cs{leftskip} and \cs{rightskip}
inserted are zero.
On the last line the \cs{parfillskip} adds \hbox{\n{plus 1fil}}
of stretch; therefore there is a total of
\hbox{\n{plus 0.5fil}} of stretch at both the left and right
end of the line.
It would have been incorrect to specify
\begin{verbatim}
\leftskip=0cm plus 0.5fil \rightskip=0cm minus 0.5fil
\end{verbatim}
\TeX\ gives an error about this: it complains about
`infinite shrinkage'.
Centring not only the last line, but also the
first line of a paragraph can be done by
the parameter settings
\begin{verbatim}
\parindent=0pt \everypar{\hskip 0pt plus -1fil}
\leftskip=0pt plus .5fil
\rightskip=0pt plus -.5fil
\end{verbatim}
This time a horizontal skip inserted by \cs{everypar}
combines with the \cs{leftskip} to give the same
amount of stretchability on both sides of the
first line of the paragraph.
%\spoint Indenting into the margin
\subsection{Indenting into the margin}
Suppose you want a hanging indent of \n{1cm} {\sl into\/}
\howto Indent into the margin\par
the left margin after the first two lines of a paragraph.
Specifying \verb/\hangindent=-1cm/ will give
a hanging indentation of one centimetre from the {\sl right\/}
margin, so another approach is necessary. The following does the
job:
\begin{verbatim}
\leftskip=-1cm \hangindent=1cm \hangafter=-2
\end{verbatim}
The only problem with this is that
the leftskip needs to be reset after the paragraph.
Suitable redefinition of \cs{par} removes this objection:
\begin{verbatim}
\def\hangintomargin{\bgroup
\leftskip=-1cm \hangindent=1cm \hangafter=-2
\def\par{\endgraf\egroup}}
\end{verbatim}
The redefinition of \cs{par} is here local to the paragraph that
should be outdented.
Another, elegant, solution uses \cs{parshape}:
\begin{verbatim}
\dimen0=\hsize \advance\dimen0 by 1cm
\parshape=3 % three lines:
0cm\hsize % first line specification
0cm\hsize % second line specification
-1cm\dimen0 % third line specification
\end{verbatim}
%\spoint Hang a paragraph from an object
\subsection{Hang a paragraph from an object}
The \LaTeX\ format has a macro, \cs{@hangfrom}, to have
\howto Hang a paragraph from an object\par
one paragraph of text hanging from some object, usually a box
or a short line of text.
\begingroup
\medskip
\def\hangobject{Example \ }
\setbox0=\hbox{\hangobject}
\hangindent \wd0 \noindent \hangobject
This paragraph is an example of the \cs{hangfrom} macro
defined below.
In the \LaTeX\ document
styles, the \cs{@hangfrom} macro (which is similar to this)
is used for multi-line section headings.\par
\endgroup
Consider then the macro \cs{hangfrom}:
\begin{verbatim}
\def\hangfrom#1{\def\hangobject{#1}\setbox0=\hbox{\hangobject}%
\hangindent \wd0 \noindent \hangobject \ignorespaces}
\end{verbatim}
Because of the default \cs{hangafter=1}, this
will produce one line of width \cs{hsize}, after which the
rest of the paragraph will be left indented by the width of the
\cs{hangobject}.
%\spoint Another approach to hanging indentation
\subsection{Another approach to hanging indentation}
Hanging indentation can also be attained by a combination
of shifting the left margin and outdenting.
Itemized lists can for instance be implemented in this manner:
\begin{verbatim}
\newdimen\listindent
\def\itemize{\begingroup
\advance\leftskip by \listindent
\parindent=-\listindent}
\def\stopitemize{\par\endgroup}
\def\item#1{\par\leavevmode
\hbox to \listindent{#1\hfil}\ignorespaces
}
\end{verbatim}
If an item should encompass more than one paragraph, the
implementation could be
\begin{verbatim}
\newdimen\listindent \newdimen\listparindent
\def\itemize{\begingroup
\advance\leftskip by \listindent
\parindent=\listparindent}
\def\stopitemize{\par\endgroup}
\def\item#1{\par\noindent
\hbox to 0cm{\kern-\listindent #1\hfil}\ignorespaces
}
\end{verbatim}
\begin{example}
\begin{verbatim}
\itemize\item{1.}First item\par
Is two paragraphs long.
\item{2.}Second item.\stopitemize
\end{verbatim}
gives
\begin{disp}
\def\itemize{\begingroup
\advance\leftskip by \parindent
\parindent=1em\relax}
\def\stopitemize{\par\endgroup}
\def\item#1{\par\noindent
\hbox to 0cm{\kern-\parindent #1\hfil}\ignorespaces
}
\itemize\item{1.}First item\par
Is two paragraphs long.
\item{2.}Second item.\stopitemize
\end{disp}
\end{example}
%\spoint Hanging indentation versus \cs{leftskip} shifting
\subsection{Hanging indentation versus \cs{leftskip} shifting}
From the above examples it would seem that
hanging indentation and modifying the \cs{leftskip} and \cs{rightskip}
are interchangeable. They are, but only to a certain extent.
\altt
Setting \cs{leftskip} to some positive value for a paragraph
means that the \cs{hsize} stays the same, but every line
starts with a glue item. Hanging indentation, on the other hand,
is implemented by decreasing the \cs{hsize} value for the
lines that hang, and shifting the finished
horizontal boxes horizontally in the surrounding vertical list.
The difference between the two approaches becomes visible
mainly in the fact that display formulas are not shifted
when the \cs{leftskip} is altered.
See Chapter~\ref{rules} for an example showing how leaders
are affected by margin shifting.
%\spoint More examples
\subsection{More examples}
Some more examples of paragraph shapes (effected by
various means) can be found in~\cite{E1}. One example
from that article appears on page~\pageref{varioset}.
%\subject[line:break] Line Breaking
\endofchapter
\chapter{Line Breaking}\label{line:break}
This chapter treats line breaking and the concept of `badness' that \TeX\
uses to decide how to break a paragraph into lines,
or where to break a page.
The various penalties contributing to the cost of line breaking
are treated here, as is hyphenation.
Page breaking is treated in Chapter~\ref{page:break}.
\begin{inventory}
\item [\cs{penalty}]
Specify desirability of not breaking at this point.
\item [\cs{linepenalty}]
Penalty value associated with each line break.
Plain \TeX\ default:~\n{10}.
\item [\cs{hyphenpenalty}]
Penalty associated with break at a discretionary item
in the general case.
Plain \TeX\ default:~\n{50}.
\item [\cs{exhyphenpenalty}]
Penalty for breaking a horizontal line at a discretionary
item in the special case where the prebreak text is empty.
Plain \TeX\ default:~\n{50}.
\item [\cs{adjdemerits}]
Penalty for adjacent visually incompatible lines.
Plain \TeX\ default:~\n{10$\,$000}.
\item [\cs{doublehyphendemerits}]
Penalty for consecutive lines ending with a hyphen.
Plain \TeX\ default:~\n{10$\,$000}.
\item [\cs{finalhyphendemerits}]
Penalty added when the penultimate line of a
paragraph ends with a hyphen.
Plain \TeX\ default:~\n{5000}.
\item [\cs{allowbreak}]
Macro for creating a breakpoint by inserting a
\cs{penalty0}.
\item [\cs{pretolerance}]
Tolerance value for a paragraph without hyphenation.
Plain \TeX\ default:~\n{100}.
\item [\cs{tolerance}]
Tolerance value for lines in a paragraph with hyphenation.
Plain \TeX\ default:~\n{200}.
\item [\cs{emergencystretch}]
(\TeX3 only)
Assumed extra stretchability in lines of a paragraph.
\item [\cs{looseness}]
Number of lines by which this paragraph has to be made longer
than it would be ideally.
\item [\cs{prevgraf}]
The number of lines in the paragraph last
added to the vertical list.
\item [\cs{discretionary}]
Specify the way a character sequence is split up at a line break.
\item [\cs{-}]
Discretionary hyphen; this is
equivalent to \verb|\discretionary{-}{}{}|.
\item [\cs{hyphenchar}]
Number of the hyphen character of a font.
\item [\cs{defaulthyphenchar}]
Value of \cs{hyphenchar} when a font is loaded.
Plain \TeX\ default:~\n{`\cs{-}}.
\item [\cs{uchyph}]
Positive to allow hyphenation of words starting with a capital
letter.
Plain \TeX\ default:~\n{1}.
\item [\cs{lefthyphenmin}]
(\TeX3 only)
Minimal number of characters before a hyphenation.
Plain \TeX\ default:~\n{2}.
\item [\cs{righthyphenmin}]
(\TeX3 only)
Minimum number of characters after a hyphenation.
Plain \TeX\ default:~\n{3}.
\item [\cs{patterns}]
Define a list of hyphenation patterns for the current
value of \cs{language}; allowed only in \IniTeX.
\item [\cs{hyphenation}]
Define hyphenation exceptions for the current value of \cs{language}.
\item [\cs{language}]
Choose a set of hyphenation patterns and exceptions.
\item [\cs{setlanguage}]
Reset the current language.
\end{inventory}
%\point Paragraph break cost calculation
\section{Paragraph break cost calculation}
A paragraph is broken such that the amount $d$ of {\em demerits\/}
associated with breaking it is minimized.
The total amount of demerits for a paragraph is the sum
of those for the individual lines, plus possibly some extra
penalties. Considering a paragraph as a whole instead of
breaking it on a line-by-line basis can lead to better
line breaking: \TeX\ can choose to take a slightly less beautiful
line in the beginning of the paragraph in order to avoid
bigger trouble later on.
For each line demerits are calculated from the {\em badness\/}~$b$
of stretching or shrinking the line to the break, and
the {\em penalty\/}~$p$ associated with the break.
The badness is not allowed to exceed a certain prescribed
tolerance.
In addition to the demerits for breaking individual lines,
\TeX\ assigns demerits for the way lines combine; see below.
The
\mdqon
implementation of \TeX's paragraph"-breaking algorithm
\mdqoff
is explained in~\cite{K:break}.
%\spoint Badness
\subsection{Badness}
From the ratio between the stretch or shrink present in a
\term badness! and line breaking\par\term line breaking!badness\par
line, and the actual stretch or shrink taken,
the `badness' of breaking a line at a certain point is calculated.
This badness is an important
factor in the process of line breaking.
See page~\pageref{bad:form} for the formula for badness.
In this chapter
badness will only be discussed in the context of line breaking.
Badness is also computed when a vertical list is stretched
or shrunk (see Chapter~\ref{page:break}).
The following terminology is used to describe badness:
\begin{description} \item [tight (3)]
is any line that has shrunk with a badness~$b\geq13$,
that is, by using at least one-half of its amount of shrink
(see page~\pageref{bad:form} for the computation).
\item [decent (2)]
is any line with a badness~$b\leq12$.
\item [loose (1)]
is any line that has stretched with a badness~$b\geq13$,
that is, by using at least one-half of its amount of stretch.
\item [very loose (0)]
is any line that has stretched with a badness~$b\geq100$,
that is, by using its full amount of stretch or more. Recall
that glue can stretch, but not shrink more than its
allowed amount.
\end{description}
The numbering is used in trace output (Chapter~\ref{trace}), and
it is also used in the following definition:
if the classifications of two adjacent lines differ by more than~1,
the lines are said to be {\em visually incompatible\/}.
See below for the \cs{adjdemerits} parameter associated with this.
Overfull horizontal and vertical
boxes are passed unnoticed if their excess width
or height is less than \cs{hfuzz} or \cs{vfuzz} respectively;
they are not reported if the badness is less than
\cs{hbadness} or \cs{vbadness} (see Chapter~\ref{boxes}).
%\spoint Penalties and other break locations
\subsection{Penalties and other break locations}
Line breaks can occur at the following places in horizontal
\cstoidx penalty\par
\term lists !horizontal! breakpoints in\par
\term lists !horizontal! penalties in \par
lists:
\begin{enumerate} \item At a penalty. The penalty value is the
`aesthetic cost' of breaking the line at that place.
Negative penalties are considered as bonuses.
A~penalty of $10\,000$ or more inhibits, and a penalty
of $-10\,000$ or less forces, a~break.
Putting more than one penalty
in a row is equivalent to putting just the one with the
minimal value, because that one is the best candidate for line breaking.
Penalties in horizontal mode are inserted by the user (or a
user macro). The only exception is the \cs{nobreak}
inserted before the \cs{parfillskip} glue.
\item At a glue, if it is not part of a math formula, and
if it is preceded by a non-discardable item (see Chapter~\ref{hvmode}).
There is no penalty associated with breaking at glue.
The condition about the non-discardable precursor is necessary,
because otherwise breaking in between two pieces of glue would
be possible, which would cause ragged edges to the paragraph.
\item At a kern, if it is not part of a math formula
and if it is followed by glue.
There is no penalty associated with breaking at a~kern.
\item At a math-off, if that is followed by glue.
Since math-off
(and math-on) act as kerns (see Chapter~\ref{math}),
this is very much like the previous case.
There is no penalty associated with breaking at a~math-off.
\item At a discretionary break. The penalty
is the \cs{hyphenpenalty} or the \cs{exhyphenpenalty}.
This is treated below.
\end{enumerate}
Any discardable material following the break \ldash glue, kerns,
\mdqon
math-on/""off and penalties \rdash is discarded. If one considers
\mdqoff
a line break at glue (kern, math-on{/}off) to occur at the
front end of the glue item, this implies that that piece
of glue disappears in the break.
%\spoint Demerits
\subsection{Demerits}
From the badness of a line and the penalty, if any, the demerits
of the line are calculated. Let $l$ be the value of
\csidx{linepenalty}, $b$~the badness of the line,
$p$~the penalty at the break; then the demerits $d$
\term demerits\par
are given by
\begin{disp}$\displaystyle d=\cases{(l+b)^2+p^2&if $0\leq p<10\,000$\cr
(l+b)^2-p^2&if $-10\,000<p<0$\cr
(l+b)^2 &if $p\leq-10\,000$\cr}$\end{disp}
Both this formula and the one for the badness are described
\alt
in \cite{K:break} as `quite arbitrary',
but they have been shown to lead to
good results in practice.
The demerits for a paragraph are the sum of the demerits for
the lines, plus \begin{itemize}
\item the \csidx{adjdemerits} for any two
adjacent lines that are not visually compatible (see above),
\item \csidx{doublehyphendemerits} for any two
consecutive lines ending with a hyphen, and the
\item \csidx{finalhyphendemerits}
if the penultimate line of a paragraph
ends with a hyphen.\end{itemize}
At the start of a paragraph \TeX\ acts as if
there was a preceding line which was `decent'.
Therefore \cs{adjdemerits} will be added if the first
line is `very loose'. Also, the last line
of a paragraph is ordinarily also `decent'
\ldash all spaces are set at natural width
owing to the infinite stretch in the \cs{parfillskip} \rdash
so \cs{adjdemerits} are added if
the preceding line is `very loose'.
Note that the penalties at which a line break
is chosen weigh about as heavily as the badness of
the line, so they can be relatively small.
However, the three extra demerit parameters
have to be of the order of the square of
penalties and badnesses to weigh equally heavily.
%\spoint The number of lines of a paragraph
\subsection{The number of lines of a paragraph}
After a paragraph has been completed (or partially
completed prior to a display), the variable \csidx{prevgraf}
records the number of lines in the paragraph.
By assigning to this variable \ldash and
because this is a \gr{special integer}
such an assignment is automatically global \rdash
\TeX's decision processes can be influenced.
This may be useful in combination with hanging indentation
or \cs{parshape} specifications (see Chapter~\ref{par:shape}).
\mdqon
Some direct influence of the line"-breaking process
\mdqoff
on the resulting number of lines exists. One factor
is the \cs{linepenalty} which is included in the demerits
of each line. By increasing the line penalty \TeX\ can be
made to minimize the number of lines in a paragraph.
Deviations from the optimal number of lines, that is, the
number of lines stemming from the optimal way of breaking a
paragraph into lines, can be forced by the user by means
of the \csidx{looseness} parameter. This parameter, which is
reset every time the shape parameters
are cleared (see Chapter~\ref{par:shape}),
indicates by how many lines the current
paragraph should be made longer than is optimal. A~negative
value of \cs{looseness} will attempt to make the paragraph shorter
by a number of lines that is the absolute value of the parameter.
\TeX\ will still observe the values
of \cs{pretolerance} and \cs{tolerance} (see below)
when lengthening or shortening a paragraph under influence
of \cs{looseness}.
Therefore,
\TeX\ will only lengthen or shorten a paragraph for as far
as is possible without exceeding these parameters.
%\spoint[between:lines] Between the lines
\subsection{Between the lines}
\label{between:lines}
\TeX's
paragraph mechanism packages lines into horizontal boxes
that are appended to the surrounding vertical list.
The resulting sequence of vertical items is then a
repeating sequence of
\begin{itemize}\item a box containing a line of text,
\item possibly migrated vertical material (see page~\pageref{migrate}),
\item a penalty item reflecting the cost of a page break
at that point, which is normally the \cs{interlinepenalty}
(see Chapter~\ref{page:break}), and
\item interline glue, which is calculated automatically
on basis of the \cs{prevdepth} (see Chapter~\ref{baseline}).
\end{itemize}
%\point The process of breaking
\section{The process of breaking}
\TeX\ tries to break paragraphs in such a way that
\term paragraph! breaking into lines\par
the badness of each line does not exceed a certain tolerance.
If there exists more than one solution to this, the one with
the fewest demerits is taken.
By setting \csidx{tracingparagraphs} to a positive value,
\TeX\ can be made to report the calculations of the
paragraph mechanism in the log file. Some implementations of \TeX\
may have this option disabled to make \TeX\ run faster.
%\spoint Three passes
\subsection{Three passes}
First an attempt is made to split the paragraph into lines
without hyphenating, that is, without inserting discretionary
hyphens. This attempt succeeds if none of the
lines has a badness exceeding \csidx{pretolerance}.
Otherwise, a second pass is made, inserting discretionaries
and using \csidx{tolerance}.
If \cs{pretolerance} is negative, the first pass is omitted.
\TeX\ can be made to make a third pass if the first and
second pass fail.
If \csidx{emergencystretch} is a positive dimension,
\TeX\ will assume this much extra stretchability
in each line when badness and demerits are calculated.
Thus solutions that only slightly exceeded the given
tolerances will now become feasible.
However, no glue of size \cs{emergencystretch} is
actually present, so underfull box messages
may still occur.
%\spoint Tolerance values
\subsection{Tolerance values}
How much
trouble \TeX\ will have typesetting a piece of text
depends partly on the tolerance value.
Therefore it is sensible to have some idea of
what badness values mean in visual terms.
For lines that are stretched, the badness is
100 times the cube of the stretch ratio.
A~badness of 800 thus means that the stretch ratio
is~2.
If the space is,
\alt
as in the ten-point Computer Modern Font,
\begin{verbatim}
3.33pt plus 1.67pt minus 1.11pt
\end{verbatim}
a badness of 800 means that spaces have been stretched to
\begin{disp} \n{3.33pt}${}+2\times{}$\n{1.67pt}${}={}$\n{6.66pt}\end{disp}
that is, to exactly double their natural size.
It is up to you to decide whether this is too large.
%\point Discretionaries
\section{Discretionaries}
A discretionary item \verb-\discretionary{..}{..}{..}-
\term discretionary item\par\cstoidx discretionary\par
marks a place where a word can be broken.
Each of the three arguments is a \gr{general text}
(see Chapter~\ref{gramm}):
they are, in sequence,
\begin{itemize} \item the {\em pre-break\/} text, which is appended
to the part of the word before the break,
\item the {\em post-break\/} text, which is prepended to the part
of the word after the break, and
\item the {\em no-break\/} text, which is used if the word
is not broken at the discretionary item.\end{itemize}
For example: \verb>ab\discretionary{g}{h}{cd}ef>
is the word \hbox{\n{abcdef}}, but it can be hyphenated
\alt
with \n{abg} before the break and \n{hef} after.
Note that there is no automatic hyphen character.
All three texts may contain any sorts of tokens,
but any primitive commands and macros
should expand to boxes, kerns, and characters.
%\spoint Hyphens and discretionaries
\subsection{Hyphens and discretionaries}
Internally, \TeX\ inserts the equivalent of
\cstoidx hyphenchar\par\term character !hyphen\par
\begin{verbatim}
\discretionary{\char\hyphenchar\font}{}{}
\end{verbatim}
at every place where a word can be broken. No
such discretionary is inserted if \verb>\hyphenchar\font>
is not in the range 0--255, or if its position in the
font is not filled.
When a font is loaded, its \cs{hyphenchar} value
is set to \csidx{defaulthyphenchar}. The \cs{hyphenchar}
value can be changed after this.
In plain \TeX\ the \cs{defaulthyphenchar} has the value~\verb>`\->, so
for all fonts character~45 (the \ascii\ hyphen character)
is the hyphen sign, unless
it is specified otherwise.
The primitive command \verb|\-| (called a `discretionary hyphen')
\csterm -\par\term discretionary hyphen\par
is equivalent to the above
\verb|\discretionary{\char\hyphenchar\font}{}{}|.
Breaking at such a discretionary, whether inserted implicitly
by \TeX\ or explicitly by the user, has
a cost of \csidx{hyphenpenalty}.
In unrestricted horizontal mode an empty discretionary
\cs{disc\-re\-tio\-na\-ry}\verb-{}{}{}-
is automatically inserted after characters
whose character code is the \cs{hyphenchar} value
of the font, thus enabling hyphenation at that point.
The penalty for breaking a line at
such a discretionary with an empty pre-break text
is \csidx{exhyphenpenalty}, that is, the `explicit hyphen' penalty.
If a word contains
discretionary breaks, for instance
because of explicit hyphen characters,
\TeX\ will not consider it for further hyphenation.
People have solved the ensuing problems by tricks
such as
\howto Enable hyphenation of a word containing a hyphen\par
\begin{verbatim}
\def\={\penalty10000 \hskip0pt -\penalty0 \hskip0pt\relax}
... integro\=differential equations...
\end{verbatim}
The skips before and after the hyphen lead \TeX\ into
treating the first and second half of the
compound expression as separate words; the penalty
before the first skip inhibits breaking before the hyphen.
%\spoint Examples of discretionaries
\subsection{Examples of discretionaries}
Languages such as German or Dutch have words that change
\term languages\par
spelling when hyphenated (German: `\hbox{backen}'
becomes `\hbox{bak-ken}'; Dutch: `\hbox{autootje}'
becomes `\hbox{auto-tje}'). This problem can be solved
with \TeX's discretionaries.
For instance, for German (this is inspired by~\cite{Partl}):
\begin{verbatim}
\catcode`\"=\active
\def"#1{\ifx#1k\discretionary{k-}{k}{ck}\fi}
\end{verbatim}
which enables the user to write \verb>ba"ken>.
In Dutch there is a further problem which allows a nice
systematic solution. Umlaut characters (`trema' is the
Dutch term) should often
disappear in a break, for instance `\hbox{na"apen}'
hyphenates as `\hbox{na-apen}', and `\hbox{onbe"invloedbaar}'
hyphenates as `\hbox{onbe-invloedbaar}'. A solution
(inspired by~\cite{Babel}) is
\begin{verbatim}
\catcode`\"=\active
\def"#1{\ifx#1i\discretionary{-}{i}{\"\i}%
\else \discretionary{-}{#1}{\"#1}\fi}
\end{verbatim}
which enables the user to type \verb>na"apen> and
\verb>onbe"invloedbaar>.
%\point Hyphenation
\section{Hyphenation}
\TeX's hyphenation algorithm uses a list of patterns to
\term hyphenation\par
determine at what places a word that is a candidate for
hyphenation can be broken.
Those aspects of hyphenation connected with these
patterns are
treated in appendix~H of \TeXbook;
the method of generating hyphenation patterns automatically
is described in~\cite{Liang}. People have been known
to generate lists of patterns by hand;
see for instance~\cite{Vas:add}. Such hand-generated lists
may be superior to automatically generated lists.
Here it will mainly be described how \TeX\ declares a word to
be a candidate for hyphenation. The problem here is
how to cope with punctuation and things such as quotation marks
that can be attached to a word. Also, {\em implicit kerns\/},
that is, kerns inserted because of font information,
must be handled properly.
%\spoint Start of a word
\subsection{Start of a word}
\TeX\ starts at glue items (if they are not in math mode)
looking for a {\em starting letter\/} of a word:
a character with non-zero \cs{lccode}, or a ligature starting
\mdqon
with such a character (upper/""lowercase codes are explained
\mdqoff
on page~\pageref{uc/lc}).
Looking for this starting letter,
\TeX\ bypasses any implicit kerns, and
characters with zero \cs{lccode} (this includes,
for instance, punctuation and quotation marks),
or ligatures starting with
such a character.
If no suitable starting letter turns up, that is, if
something is found that is not a character or ligature,
\TeX\ skips to the next glue, and starts this algorithm anew.
Otherwise a trial word is collected consisting of
all following characters with non-zero \cs{lccode}
from the same font as the starting letter, or ligatures consisting
completely of such characters. Implicit kerns are allowed
between the characters and ligatures.
If the starting letter is from a font for which the value
of \cs{hyphenchar} is invalid, or for which this character
does not exist, hyphenation is abandoned for this word.
If the starting letter is an uppercase letter (that is,
it is not equal to its own \cs{lccode}), \TeX\ will
abandon hyphenation unless \csidx{uchyph} is positive.
The default value for this parameter is~1 in
plain \TeX,
implying that capitalized words are subject to hyphenation.
%\spoint End of a word
\subsection{End of a word}
Following the trial word can be characters (from another
font, or with zero \cs{lccode}), ligatures or implicit kerns.
After these items, if any, must follow
\begin{itemize}\item glue or an explicit kern,
\item a penalty,
\item a whatsit, or
\item a \cs{mark}, \cs{insert}, or \cs{vadjust} item.
\end{itemize}
In particular, the word will not be hyphenated if it is
followed by a \begin{itemize}\item box, \item rule, \item math
formula, or \item discretionary item.\end{itemize}
Since discretionaries are inserted after the \cs{hyphenchar}
of the font, occurrence of this character inhibits further
hyphenation. Also, placement of accents is implemented using
explicit kerns (see Chapter~\ref{char}), so any \cs{accent}
command is considered to be the end of a word, and inhibits
hyphenation of the word.
%\spoint \TeX2 versus \TeX3
\subsection{\TeX2 versus \TeX3}
There is a noticeable difference in the treatment of
\term \TeX\ version 3\par
hyphenated fragments between \TeX2 and \TeX3.
\TeX2 insists that the part before the break should be
at least two characters, and the part after the break three
characters, long.
Typographically this is a sound decision: this way
there are no two-character pieces of a word stranded at the
end or beginning of the line. Both before and after the break
there are at least three characters.
In \TeX3 two integer parameters have been introduced to control
the length of these fragments:
\csidx{lefthyphenmin} and \csidx{righthyphenmin}. These are
set to 2 and~3 respectively in the plain format for \TeX3.
If the sum of these two is 63 or more, all hyphenation is
suppressed.
Another addition in \TeX3,
the possibility to have several sets of hyphenation patterns,
is treated below.
%\spoint Patterns and exceptions
\subsection{Patterns and exceptions}
The statements \begin{disp}\cs{patterns}\gr{general text}\nl
\csidx{hyphenation}\gr{general text}\end{disp}
are \gr{hyphenation assignment}s, which are
\gr{global assignment}s.
The \csidx{patterns} command, which specifies a list
of hyphenation patterns, is allowed only in \IniTeX\
(see Chapter~\ref{TeXcomm}),
and all patterns must be specified before the first
paragraph is typeset.
Hyphenation exceptions can be specified at any time
\howto Specify exceptional hyphenations\par
with statements such as
\begin{verbatim}
\hyphenation{oxy-mo-ron gar-goyle}
\end{verbatim}
which specify locations where a word may be hyphenated.
Subsequent \cs{hyphenation} statements are cumulative.
In \TeX3 these statements are taken to hold for the
language that is the current value of the \cs{language}
parameter.
%\point Switching hyphenation patterns
\section{Switching hyphenation patterns}
When typesetting paragraphs, \TeX\ (version~3) can use several
\alt
\term language\par
sets of patterns and hyphenation exceptions, for at most 256
languages.
If a \cs{patterns} or \cs{hyphenation}
command is given (see above), \TeX\ stores the patterns or exceptions
under the current value of the \csidx{language} parameter.
The \cs{patterns} command is only allowed in \IniTeX, and
patterns must be specified before any typesetting is done.
Hyphenation exceptions, however, can
be specified cumulatively, and not only in \IniTeX.
In addition to the \cs{language} parameter,
\term language !current\par
which can be set by the user, \TeX\ has internally a `current
language'. This is set to zero at the start of every paragraph.
For every character that is added to a paragraph
the current language is compared with the value of \cs{language},
and if they differ a whatsit element is added to the horizontal
list, resetting the current language to the value of \cs{language}.
At the start of a paragraph, this whatsit is inserted
\altt
after the \cs{everypar} tokens, but \cs{lastbox}
can still access the indentation box.
As an example, suppose that a format has been created such that
language~0 is English, and language~1 is Dutch. English hyphenations
will then be used if the user does not specify otherwise;
if a job starts with \begin{verbatim}
\language=1
\end{verbatim}
the whole document
will be set using Dutch hyphenations, because \TeX\ will insert
a command changing the current language at the start of
every paragraph. For example:
\begin{verbatim}
\language=1
T...
\end{verbatim}
gives
\begin{verbatim}
.\hbox(0.0+0.0)x20.0 % indentation
.\setlanguage1 (hyphenmin 2,3) % language whatsit
.\tenrm T % start of text
\end{verbatim}
The whatsit can be inserted explicitly, without changing
the value of \csidx{language}, by specifying
\begin{disp}\cs{setlanguage}\gr{number}\end{disp}
However, this will hardly ever be needed.
One case where it may be necessary is when the contents of
a horizontal box are unboxed to a paragraph: inside the box no
whatsits are added automatically, since inside such a box
no hyphenation can take place.
See page~\pageref{wide:vbox} for another problem with text
in horizontal boxes.
%%%% end of input file [par]
%\InputFile:space
%%%% this is input file [space]
%\subject[space] Spacing
\endofchapter
\chapter{Spacing}\label{space}
The usual interword space in \TeX\ is specified in the
\term spacing\par
font information, but the user can override this.
This chapter explains the rules by which
\TeX\ calculates interword space.
\begin{inventory}
\item [\cs{\char32}]
Control space.
Insert the same amount of space as a space token would
if \cs{spacefactor}${}=1000$.
\item [\cs{spaceskip}]
Interword glue if non-zero.
\item [\cs{xspaceskip}]
Interword glue if non-zero and \cs{spacefactor}${}\geq2000$.
\item [\cs{spacefactor}]
1000 times the ratio by which the stretch (shrink) component of the
interword glue should be multiplied (divided).
\item [\cs{sfcode}]
Value for \cs{spacefactor} associated with a character.
\item [\cs{frenchspacing}]
Macro to switch off extra space after punctuation.
\item [\cs{nonfrenchspacing}]
Macro to switch on extra space after punctuation.
\end{inventory}
\section{Introduction}
In between words in a text, \TeX\ inserts space. This space has a
natural component, plus stretch and shrink to make justified
(right-aligned) text possible. Now, in certain styles of typesetting,
there is more space after punctuation. This chapter discusses the
mechanism that \TeX\ uses to realize such effect.
Here is the general idea:
\begin{itemize}
\item After every character token, the \cs{spacefactor} quantity is
updated with the space factor code of that character.
\item When space is inserted, its natural size can be augmented
(if \cs{spacefactor}${}\geq2000$), and in general its stretch is
multiplied, and its shrink divided, by \cs{spacefactor}${}/1000$.
\item There are further rules, for instance so that in \n{...word.)
And...} the space is modified according to the period, not the
closing parenthesis.
\end{itemize}
%\point Automatic interword space
\section{Automatic interword space}
For every space token in horizontal mode the interword glue
of the current font
is inserted, with stretch and shrink components, all
determined by \cs{fontdimen} parameters.
To be specific, font dimension~2 is the normal interword space,
dimension~3 is the amount of stretch of the interword
space, and 4~is the amount of shrink. Font dimension
7 is called the `extra space'; see below (the list
of all the font dimensions appears on page~\pageref{font:dims}).
Ordinarily all spaces between words (in one font) would be treated
the same. To allow for differently sized spaces \ldash for instance
a typeset equivalent of the double spacing after
punctuation in typewritten documents \rdash
\term space! factor\par
\TeX\ associates with each character a so-called `space factor'.
When a character is added to the current horizontal list,
the space factor code (\csidx{sfcode})
of that character
is assigned to the space factor \csidx{spacefactor}.
There are two exceptions to this rule:
\begin{itemize}
\item When the space factor code is zero, the \cs{spacefactor} does
not change. This mechanism allows space factors to persist through
parentheses and such; see section~\ref{sec:sf-through-paren}.
\item When the space factor code of the last character is ${>}1000$
and the current space factor is ${<}1000$, the space factor
becomes~1000. This mechanism prevents elongated spaces after
initials; see section~\ref{sec:sf-punct}.
\end{itemize}
The maximum space factor is~$32\,767$.
The stretch component of the interword space is
multiplied by the space factor divided by 1000;
the shrink component is divided by this factor.
The extra space (font dimension~7) is
added to the natural component of the
interword space when the space factor is~${}\geq2000$.
%\point User interword space
\section{User interword space}
The user can override the interword space contained in
the \cs{fontdimen} parameters
by setting the
\csidx{spaceskip} and the \csidx{xspaceskip} to non-zero values.
If \cs{spaceskip} is non-zero, it is taken instead
of the normal interword space
(\cs{fontdimen2} plus \cs{fontdimen3} minus \cs{fontdimen4}), but
a non-zero \cs{xspaceskip} is used as interword space if
the space factor is~${}\geq2000$.
If the \cs{spaceskip} is used,
its stretch and shrink components are
multiplied and divided respectively by \cs{spacefactor}$/1000$.
Note that, if \cs{spaceskip} and \cs{xspaceskip} are
defined in terms of \n{em}, they change with the font.
\begin{example} Let the following macros be given:
\begin{verbatim}
\def\a.{\vrule height10pt width4pt\spacefactor=1000\relax}
\def\b.{\vrule height10pt width4pt\spacefactor=3000\relax}
\def\c{\vrule height10pt width4pt\relax}
\end{verbatim}
then
%\begin{disp}\leavevmode\PopIndentLevel
\hbox{%
$\vcenter{\snugbox{%
\begin{verbatim}
\vbox{
\fontdimen2\font=4pt % normal space
\fontdimen7\font=3pt % extra space
\a. \b. \c\par
% zero extra space
\fontdimen7\font=0pt
\a. \b. \c\par
% set \spaceskip for normal space
\spaceskip=2\fontdimen2\font
\a. \b. \c\par
% set \xspaceskip
\xspaceskip=2pt
\a. \b. \c\par
}
\end{verbatim}
}}$%
%
\quad gives\quad
%
\message{Check snug and drop!}%
$\vcenter{\snugbox{\parindent0pt\parskip=0pt
\def\a.{\vrule height10pt width4pt\spacefactor=1000\relax}
\def\b.{\vrule height10pt width4pt\spacefactor=3000\relax}
\def\c{\vrule height10pt width4pt\relax}
\leavevmode\strut\par\hbox{}\hbox{}
% set the normal space and extra space
\fontdimen2\font=4pt \fontdimen7\font=3pt
\a. \b. \c\par \vskip2\baselineskip
% zero extra space
\fontdimen7\font=0pt
\a. \b. \c\par \vskip2\baselineskip
% set \spaceskip for normal space
\spaceskip=2\fontdimen2\font
\a. \b. \c\par \vskip2\baselineskip
% set \xspaceskip
\xspaceskip=2pt
\a. \b. \c\par \leavevmode\strut
}}$%
%
}
%\end{disp}
In all of these lines the glue is set at natural width. In the first
line the high space factor value after \cs{b} causes the extra
space \cs{fontdimen7} to be added. If this is zero (second line), the
only difference between space factor values is the stretch/shrink
ratio. In the third line the \cs{spaceskip} is taken
for all space factor values. If the \cs{xspaceskip} is nonzero,
it is taken (fourth line) instead of the \cs{spaceskip}
for the high value of the space factor.
\end{example}
%\point[tie] Control space and tie
\section{Control space and tie}
\label{tie}
Control space, \csc{\char32}, is a horizontal command
which inserts a space,
\term control! space\par\term space !control~--\par\csidx{\char32}
acting as if the current space factor is~1000.
However, it does not affect the value of \cs{spacefactor}.
Control space has two main uses. First, it is convenient to use after
a control sequence: \verb+\TeX\ is fun!+
Secondly, it can be used after abbreviations when \cs{nonfrenchspacing}
(see below) is in effect. For example:
\begin{verbatim}
\hbox spread 9pt{\nonfrenchspacing
The Reverend Dr. Drofnats}
\end{verbatim}
gives
\begin{disp} \hbadness=10000 \leavevmode
\hbox spread 9pt{\nonfrenchspacing
The Reverend Dr. Drofnats}\end{disp}
while
\begin{verbatim}
\hbox spread 9pt{\nonfrenchspacing
The Reverend Dr.\ Drofnats}
\end{verbatim}
gives
\begin{disp} \hbadness=10000 \leavevmode
\hbox spread 9pt{\nonfrenchspacing
The Reverend Dr.\ Drofnats}\end{disp}
(The \n{spread 9pt} is used to make the effect more visible.)
The active character (in the plain format) tilde,~\n{\char126},
\term tie\par\term ~@\char126\par
uses control space: it is defined as
\begin{verbatim}
\catcode`\~=\active
\def~{\penalty10000\ }
\end{verbatim}
Such an active tilde is called a `tie'; it inserts an ordinary
amount of space, and prohibits breaking at this space.
%\point More on the space factor
\section{More on the space factor}
%\spoint Space factor assignments
\subsection{Space factor assignments}
The space factor of a particular character can be assigned as
\term spacefactor code\par\cstoidx sfcode\par
\begin{disp}\cs{sfcode}\gr{8-bit number}\gr{equals}\gr{number}\end{disp}
\IniTeX\ assigns a space factor code of 1000 to all characters
\label{ini:sf}%
except uppercase characters; they get a space factor code of~999.
The plain format then assigns space factor codes greater than
1000 to various punctuation symbols, for instance
\verb-\sfcode`\.=3000-, which triples the stretch and shrink
after a full stop. Also, for all space factor values $\geq2000$
the extra space is added; see above.
%\spoint Punctuation
\subsection{Punctuation}
\label{sec:sf-punct}
Because the space factor cannot jump from a value below 1000
to one above, a punctuation symbol after an uppercase
character will not have the effect on the interword space
that punctuation after a lowercase character has.
\begin{example}\begin{verbatim}
a% \sfcode`a=1000, space factor becomes 1000
.% \sfcode`.=3000, spacefactor becomes 3000
% subsequent spaces will be increased.
A% \sfcode`A=999, space factor becomes 999
.% \sfcode`.=3000, space factor becomes 1000
% subsequent spaces will not be increased.
\end{verbatim}
\end{example}
Thus, initials
are not mistaken for sentence ends.
If an uppercase character does end a sentence, for instance
\begin{verbatim}
... and NASA.
\end{verbatim}
there are several solutions:
\begin{verbatim}
... NASA\spacefactor=1000.
\end{verbatim}
or
\begin{verbatim}
... NASA\hbox{}.
\end{verbatim}
which abuses the fact that after
a box the space factor is set to~1000.
The \LaTeX\ macro \cs{@} is equivalent to the first
possibility.
In the plain format two macros are defined that switch between
\term frenchspacing\par\cstoidx frenchspacing\par
\cstoidx nonfrenchspacing\par
uniform interword spacing and extra space after punctuation.
The macro \cs{frenchspacing} sets the space factor code
of all punctuation to~1000; the macro \cs{nonfrenchspacing}
sets it to values greater than~1000.
Here are the actual definitions from \n{plain.tex}:\begin{verbatim}
\def\frenchspacing{\sfcode`\.\@m \sfcode`\?\@m
\sfcode`\!\@m \sfcode`\:\@m
\sfcode`\;\@m \sfcode`\,\@m}
\def\nonfrenchspacing{\sfcode`\.3000 \sfcode`\?3000
\sfcode`\!3000 \sfcode`\:2000
\sfcode`\;1500 \sfcode`\,1250 }
\end{verbatim}
where \begin{verbatim}
\mathchardef\@m=1000
\end{verbatim}
is given in the plain format.
French spacing is a somewhat controversial issue:
\TeXbook\ acts as if non-French spacing
is standard practice in printing, but for instance in~\cite{Hart}
one finds `The space of the line should be used after
all points in normal text'.
Extra space after punctuation
may be considered a `typewriter habit', but this is
not entirely true. It used to be a lot more common
than it is nowadays, and there are rational arguments
against it: the full stop (point, period) at the end of a
sentence, where extra punctuation is most visible,
is rather small, so it carries some extra visual space
of its own above it. This book does not use extra space
after punctuation.
%\spoint Other non-letters
\subsection{Other non-letters}
\label{sec:sf-through-paren}
The zero value of the space factor code makes
characters that are not a letter and not punctuation
`transparent' for the space factor.
\message{check break after Example}
\begin{example}\begin{verbatim}
a% \sfcode`a=1000, space factor becomes 1000
.% \sfcode`.=3000, spacefactor becomes 3000
% subsequent spaces will be increased.
a% \sfcode`a=1000, space factor becomes 1000
.% \sfcode`.=3000, space factor becomes 3000
)% \sfcode`)=0, space factor stays 3000
% subsequent spaces will be increased.
\end{verbatim}
\end{example}
%\spoint Other influences on the space factor
\subsection{Other influences on the space factor}
The space factor is 1000 when \TeX\ starts forming a
horizontal list, in particular after \cs{indent}, \cs{noindent},
and directly after a display. It is also 1000 after
a \cs{vrule}, an accent, or a \gr{box} (in horizontal mode), but
it is not influenced by \cs{unhbox} or \cs{unhcopy}
commands.
In the first column of a \cs{valign} the space factor of
the surrounding horizontal list is carried over; similarly,
after a vertical alignment the space factor is set to the
value reached in the last column.
%%%% end of input file [space]
%\InputFile:math
%%%% this is input file [math]
%\subject[mathchar] Characters in Math Mode
\endofchapter
\chapter{Characters in Math Mode}\label{mathchar}
In math mode every character specifies by its
\cs{mathcode} what position of
a font to access, among other things.
For delimiters this story is a bit
more complicated. This chapter explains the concept
of math codes, and shows how \TeX\ implements variable
size delimiters.
\begin{inventory}
\item [\cs{mathcode}]
Code of a character determining its treatment in math mode.
\item [\cs{mathchar}]
Explicit denotation of a mathematical character.
\item [\cs{mathchardef}]
Define a control sequence to be a synonym for
a~math character code.
\item [\cs{delcode}]
Code specifying how a character should be used as delimiter.
\item [\cs{delimiter}]
Explicit denotation of a delimiter.
\item [\cs{delimiterfactor}]
1000 times the fraction of a delimited formula that should be
covered by a delimiter.
Plain \TeX\ default:~\n{901}
\item [\cs{delimitershortfall}]
Size of the part of a delimited formula that is allowed
to go uncovered by a delimiter.
Plain \TeX\ default:~\n{5pt}
\item [\cs{nulldelimiterspace}]
Width taken for empty delimiters.
Plain \TeX\ default:~\n{1.2pt}
\item [\cs{left}]
Use the following character as an open delimiter.
\item [\cs{right}]
Use the following character as a closing delimiter.
\item [\cs{big}]
One line high delimiter.
\item [\cs{Big}]
One and a half line high delimiter.
\item [\cs{bigg}]
Two lines high delimiter.
\item [\cs{Bigg}]
Two and a half lines high delimiter.
\item [\cs{bigl {\MainFont etc.}}]
Left delimiters.
\item [\cs{bigm {\MainFont etc.}}]
Delimiters used as binary relations.
\item [\cs{bigr {\MainFont etc.}}]
Right delimiters.
\item [\cs{radical}]
Command for setting things such as root signs.
\item [\cs{mathaccent}]
Place an accent in math mode.
\item [\cs{skewchar}]
Font position of an after-placed accent.
\item [\cs{defaultskewchar}]
Value of \cs{skewchar} when a font is loaded.
\item [\cs{skew}]
Macro to shift accents on top of characters explicitly.
\item [\cs{widehat}]
Hat accent that can
accommodate wide expressions.
\item [\cs{widetilde}]
Tilde accent that can
accommodate wide expressions.
\end{inventory}
%\point Mathematical characters
\section{Mathematical characters}
Each of the 256 permissible character codes has
\term math characters\par
an associated \csidx{mathcode}, which can be assigned by
\begin{disp}\cs{mathcode}\gr{8-bit number}\gr{equals}\gr{15-bit number}\end{disp}
When processing in math mode, \TeX\ replaces all characters of
categories 11 and~12, and \cs{char} and \cs{chardef} characters,
by their associated mathcode.
The 15-bit math code is most conveniently denoted hexadecimally
as \verb-"xyzz-, where\begin{disp}
\n x${}\leq7$ is the class (see page~\pageref{math:class}),\nl
\n y is the font family number \alt
(see Chapter~\ref{mathfont}), and \nl
\n{zz} is the position of the character in the font.\end{disp}
Math codes can also be specified directly by
\cstoidx mathchar\par\cstoidx mathchardef\par
a \gr{math character}, which can be\label{math:character}
\begin{itemize}\item\cs{mathchar}\gr{15-bit number};
\item \gr{mathchardef token}, a control sequence that was defined by
\begin{disp}\cs{mathchardef}\gr{control sequence}\gr{equals}\gr{15-bit number}
\end{disp}
or
\item a delimiter command\alt
\begin{disp}\cs{delimiter}\gr{27-bit number}\end{disp}
where the last 12 bits
are discarded.\end{itemize}
The commands \cs{mathchar} and \cs{mathchardef}
are analogous to \cs{char} and \cs{char\-def} in text mode.
Delimiters are treated below.
A~\gr{mathchardef token}
can be used as a \gr{number}, even outside math mode.
In \IniTeX\ all letters receive \cs{mathcode} \verb-"71zz- and
all digits receive \verb-"70zz-, where \verb-"zz- is the
hexadecimal position of the character in the font.
Thus, letters are initially from family~1
(math italic in plain \TeX), and digits are from family~0
(roman).
For all other characters, \IniTeX\ assigns
\begin{disp}\cs{mathcode}$\,x=x$,\end{disp}
thereby placing them also in family~0.
If the mathcode is \verb-"8000-,
\label{mcode:8000}the smallest integer that is
not a \gr{15-bit number}, the character is treated as an active
character with the original character code. Plain \TeX\
assigns a \cs{mathcode} of \verb-"8000- to the space, underscore and prime.
%\point Delimiters
\section{Delimiters}
After \csidx{left} and \csidx{right}
\term delimiters\par
commands \TeX\ looks for a delimiter. A~delimiter
is either an explicit \cs{delimiter} command (or a
macro abbreviation for it), or a character with a non-zero
delimiter code.
The \cs{left} and \cs{right} commands
implicitly delimit a group, which is considered as a subformula.
Since the enclosed formula can
be arbitrarily large, the quest for the proper delimiter is
a complicated story of looking at variants in two different
fonts, linked chains of variants in a font, and building
extendable delimiters from repeatable pieces.
The fact that a group enclosed in \verb>\left...\right> is
treated as an independent subformula implies that a
sub- or superscript at the start of this formula is
not considered to belong to the delimiter.
For example, \TeX\ acts as if
\verb>\left(_2> is equivalent to \verb>\left({}_2>.
(A~subscript after a \cs{right} delimiter is positioned
with respect to that delimiter.)
%\spoint[delcodes] Delimiter codes
\subsection{Delimiter codes }
\label{delcodes}
To each character code there corresponds a delimiter
\cstoidx delcode\par\term delimiter codes\par
code, assigned by
\begin{disp}\cs{delcode}\gr{8-bit number}\gr{equals}%
\gr{24-bit number}\end{disp}
A delimiter code thus consists of six hexadecimal digits
\verb-"uvvxyy-, where\begin{disp}
\n{uvv} is the small variant of the delimiter, and\nl
\n{xyy} is the large variant;\nl
\n u, \n x are the font families of the variants, and\nl
\n{vv}, \n{yy} are the locations in those fonts.\end{disp}
Delimiter codes are used after \cs{left} and \cs{right}
commands.
\IniTeX\ sets all delimiter codes to~$-1$,
except\label{ini:del}
\verb-\delcode`.=0-, which makes the period an empty delimiter.
In plain \TeX\ delimiters have typically \n{u}${}=2$ and~\n{x}${}=3$,
that is, first family~2 is tried, and if no big
enough delimiter turns up family~3 is tried.
%\spoint Explicit \cs{delimiter} commands
\subsection{Explicit \cs{delimiter} commands}
Delimiters can also be denoted
\cstoidx delimiter\par
explicitly by a \gr{27-bit number},
\begin{verbatim}
\delimiter"tuvvxyy
\end{verbatim}
where \n{uvvxyy} are the small and large variant of the
delimiter as above;
the extra digit \n{t} (which is~$<8$) denotes the class
(see page~\pageref{math:class}).
For instance, the \cs{langle} macro is defined as
\begin{verbatim}
\def\langle{\delimiter "426830A }
\end{verbatim}
which means it belongs to class~4, opening. Similarly,
\cs{rangle} is of class~5, closing; and \cs{uparrow} is of class~3,
relation.
After \cs{left} and \cs{right} \ldash that is, when \TeX\
is looking for a delimiter \rdash the class digit is ignored;
otherwise \ldash when \TeX\ is not looking for a delimiter \rdash
the rightmost three digits are ignored, and the
four remaining digits are treated as a~\cs{mathchar}; see above.
%\spoint[successor] Finding a delimiter; successors
\subsection{Finding a delimiter; successors}
\label{successor}
Typesetting a delimiter is a somewhat involved affair.
\term delimiter sizes\par\term successors\par
First \TeX\ determines the size $y$ of the formula to be covered,
which is twice the maximum of the height and depth of the
formula. Thus the formula may not look optimal if
it is not centred itself.
The size of the delimiter should be at least
\csidx{delimiterfactor}${}\times y/1000$ and at least
$y-{}$\csidx{delimitershortfall}.
\TeX\ then tries first the small variant, and if that one
is not satisfactory (or if the \n{uvv} part of the delimiter
is~\n{000}) it tries the large variant. If trying the large variant
does not meet with success, \TeX\ takes the largest delimiter
encountered in this search; if no delimiter at all was found
(which can happen if the \n{xyy} part is
\altt
also~\n{000}),
an empty box of width~\csidx{nulldelimiterspace} is taken.
Investigating a variant means, in sequence,
\begin{itemize} \item if the current style (see page~\pageref{math:styles})
is scriptscriptstyle
the \cs{scriptscriptfont} of the family is tried;
\item if the current style is scriptstyle or smaller
the \cs{scriptfont} of the family is tried;
\item otherwise the \cs{textfont} of the family is tried.\end{itemize}
The plain format puts the \verb-cmex10- font in all three
\term extension fonts\par
styles of family~3.
Looking for a delimiter at a certain position in a certain font
means\begin{itemize}\item if the character is large enough, accept it;
\item if the character is extendable, accept it;
\item otherwise, if the character has a successor, that is, it is
part of a chain of increasingly bigger delimiters in the same
font, try the successor.\end{itemize}
Information about successors and extensibility of a delimiter
is coded in the font metric file of the font.
An extendable character has a top, a bottom, possibly a mid piece,
and a piece which is repeated directly below the top piece, and
directly above the bottom piece if there is a mid piece.
%\spoint \cs{big}, \cs{Big}, \cs{bigg}, and \cs{Bigg}
\subsection{\cs{big}, \cs{Big}, \cs{bigg}, and \cs{Bigg}
delimiter macros}
In order to be able to use a delimiter outside the
\verb-\left...\right- context, or to specify a delimiter of
a different size than \TeX\ would have chosen,
four macros for `big' delimiters exist: \cs{big},
\cs{Big}, \cs{bigg}, and \cs{Bigg}. These can be used with
anything that can follow \cs{left} or \cs{right}.
Twelve further macros (for instance \cs{bigl}, \cs{bigm},
\cstoidx big \rm etc.\par
%\csterm big \Style:roman etc.\par
and~\cs{bigr}) force such delimiters in the context of
an opening symbol, a binary relation, and a closing symbol
respectively:\begin{verbatim}
\def\bigl{\mathopen\big}
\def\bigm{\mathrel\big} \def\bigr{\mathclose\big}
\end{verbatim}
The `big' macros themselves put the requested delimiter and
a null delimiter around an empty vertical box:
\begin{verbatim}
\def\big#1{{\nulldelimiterspace=0pt \mathsurround=0pt
\hbox{$\left#1\vbox to 8.5pt{}\right.$}}}
\end{verbatim}
As an approximate measure,
the \n{Big} delimiters are one and a half times as large (11.5pt) as
\n{big} delimiters; \n{bigg} ones are twice (14.5pt), and \n{Bigg}
ones are two and a half times as large (17.5pt).
%\point Radicals
\section{Radicals}
A radical is a compound of a left delimiter and an overlined
math expression.
\term radicals\par\cstoidx radical\par
The overlined expression is set in the
cramped version of the surrounding style
\alt
(see page~\pageref{math:styles}).
In the plain format and the Computer Modern
math fonts there is only one radical: the square root
construct \begin{verbatim}
\def\sqrt{\radical"270370 }
\end{verbatim}
The control sequence \cs{radical} is followed by a \gr{24-bit number}
which specifies a small and a large variant of the left delimiter
as was explained above. Joining the delimiter and the rule
is done by letting the delimiter have a large depth, and a height
which is equal to the desired rule thickness. The rule can then
be placed on the current baseline. After the delimiter and the
ruled expression have been joined the whole is shifted
vertically to achieve the usual vertical centring
(see Chapter~\ref{math}).
%\point Math accents
\section{Math accents}
Accents in math mode are specified by
\cstoidx mathaccent\par\term accents in math mode\par
\begin{disp}\cs{mathaccent}\gr{15-bit number}\gr{math field}\end{disp}
Representing the 15-bit number as \verb>"xyzz>,
only the family~\n{y} and the character position~\n{zz}
are used: an accented expression acts as \cs{mathord} expression
(see Chapter~\ref{math}).
In math mode whole expressions can be accented,
\alt
whereas in text mode only characters can be accented.
Thus in math mode accents can be stacked. However, the top
accent may (or, more likely, will) not be properly positioned
horizontally. Therefore the plain format has a macro \csidx{skew}
that effectively shifts the top accent. Its definition is
\begin{verbatim}
\def\skew#1#2#3{{#2{#3\mkern#1mu}\mkern-#1mu}{}}
\end{verbatim}
and it is used for instance like
\begin{verbatim}
$\skew4\hat{\hat x}$
\end{verbatim}
\message{skew thing.}
%which gives~{\font\tmp=cmmi10 $\textfont\VMIfam=\tmp\skew4\hat{\hat x}$}.
which gives~{$\skew4\hat{\hat x}$}.
For the correct positioning of accents over single characters
the symbol and extension font have a \csidx{skewchar}:
this is the largest accent that adds to the width of an
accented character. Positioning of any accent
is based on the width of the character to be accented,
followed by the skew character.
The skew characters of the Computer Modern
math italic and symbol fonts are character \n{\hex7F},
\alt
`$\mathchar"7F$',\message{skew characters}
and \n{\hex30}, `$\mathchar"30$', respectively. The \csidx{defaultskewchar}
value is assigned to the \cs{skewchar} when a font is loaded.
In plain \TeX\ this is~\n{-1}, so fonts ordinarily have no
\cs{skewchar}.
Math accents can adapt themselves to the size of the accented
expression: \TeX\ will look for a successor of an accent
in the same way that it looks for a successor of a delimiter.
In the Computer Modern math fonts this mechanism is used in
\cstoidx widehat\par\cstoidx widetilde\par
the \cs{widehat} and \cs{widetilde} macros.
For example,
\begin{disp}\verb>\widehat x>, \verb>\widehat{xy}>, \verb>\widehat{xyz}>
\end{disp} give
\begin{disp}$\widehat x$, $\widehat{xy}$, $\widehat{xyz}$
\end{disp} respectively.
%\subject[mathfont] Fonts in Formulas
\endofchapter
\chapter{Fonts in Formulas}\label{mathfont}
For math typesetting a single current font is not sufficient, as it
is for text typesetting. Instead \TeX\ uses several font families,
and each family can contain three fonts. This chapter
explains how font families are organized, and how \TeX\ determines
from what families characters should be taken.
\begin{inventory}
\item [\cs{fam}]
The number of the current font family.
\item [\cs{newfam}]
Allocate a new math font family.
\item [\cs{textfont}]
Access the textstyle font of a family.\alt
\item [\cs{scriptfont}]
Access the scriptstyle font of a family.\alt
\item [\cs{scriptscriptfont}]
Access the scriptscriptstyle font of a family.\alt
\end{inventory}
%\point Determining the font of a character in math mode
\section{Determining the font of a character in math mode}
The characters in math formulas can be taken from several
\term font families\par
different fonts (or better, font families) without any user
commands. For instance, in plain \TeX\ math formulas use
the roman font, the math italic font,
the symbol font and the math extension font.
In order to determine from which font a character is to be
taken, \TeX\ considers for each character in a formula its
\cs{mathcode} (this is treated in Chapter~\ref{mathchar}).
A~\cs{mathcode} is a 15-bit number of the form
\verb."xyzz., where the hex digits
have the following meaning:\begin{disp}
\n x:~class,\nl
\n y:~family,\nl
\n{zz}:~position in font.\end{disp}
In general only the family determines from what font
a character is to be taken.
The class of a math character is mostly used to
control spacing and other aspects of typesetting.
Typical classes include `relation', `operator', `delimiter'.
Class~7 is special in this respect:
it is called `variable family'.
If a character has a \cs{mathcode} of the form \verb."7yzz.
it is taken from family \n{y},
unless the parameter \cs{fam} has a value in the range 0--15;
then it is taken from family~\cs{fam}.
%\point Initial family settings
\section{Initial family settings}
Both lowercase and uppercase letters
are defined by \IniTeX\ to have math codes \verb>"71zz>,
\label{ini:fam}%
which means that they are of variable family, initially from
family~1.
As \TeX\ sets \verb.fam=-1., that is,
an invalid value, when a formula starts,
characters are indeed taken from
family~1, which in plain \TeX\ is math italic.
Digits have math code \verb>"70zz> so they are initially from
family~0, in plain \TeX\ the roman font.
All other character codes have a mathcode
assigned by \IniTeX\ as
\begin{disp}\cs{mathcode}$\,x=x$\end{disp} which puts them in class~0,
ordinary, and family~0, roman in plain \TeX.
In plain \TeX, commands such as \cs{sl} then set both a font and
a family:
\begin{verbatim}
\def\sl{\fam\slfam\tensl}
\end{verbatim}
so putting \cs{sl} in a formula will cause all letters, digits,
and uppercase Greek characters, to change to
slanted style.
In most cases, any font can be assigned to any family, but
two families in \TeX\ have a special meaning: these are
families 2 and~3.
For instance, their number of \cs{fontdimen} parameters
is different from the usual~7. Family~2 needs 22 parameters,
and family~3 needs~13. These parameters have all a very
specialized meaning for positioning in math typesetting.
Their meaning is explained below, but for the full story
the reader is referred to appendix~G of \TeXbook.
%\point Family definition
\section{Family definition}
\TeX\ can access 16 families of fonts in math mode;
font families have numbers 0--15.
The number of the
current family is recorded in the parameter~\csidx{fam}.
The macro \csidx{newfam} gives the number of an unused family.
This number is assigned using \cs{chardef} to the control sequence.
Each font family can have a font meant for text style, script style,
and scriptscript style. Below it is explained how \TeX\
determines in what style a (sub-) formula is to be typeset.
Fonts are assigned to a family
\cstoidx textfont\par\cstoidx scriptfont\par\cstoidx scriptscriptfont\par
as follows:
\begin{verbatim}
\newfam\MyFam
\textfont\MyFam=\tfont \scriptfont\MyFam=\sfont
\scriptscriptfont\MyFam=\ssfont
\end{verbatim}
for the text, script, and scriptscript fonts of a family.
In general it is not necessary to fill all three members
of a family (but it is for family~3).
If \TeX\ needs a character from a family member
that has not been filled,
it uses the \cs{nullfont} instead,
a~primitive font that has no characters (nor a \n{.tfm} file).
%\point Some specific font changes
\section{Some specific font changes}
%\spoint Change the font of ordinary characters and uppercase Greek
\subsection{Change the font of ordinary characters and uppercase Greek}
All letters and the uppercase Greek characters are
by default in plain \TeX\ of class~7,
variable family, so changing \cs{fam} will change the font
from which they are taken.
For example
\begin{verbatim}
{\fam=9 x}
\end{verbatim}
gives an \n{x} from family~9.
Uppercase Greek characters are defined by
\cs{mathchardef} statements in the plain format as \verb>"70zz>,
that is, variable family, initially roman.
Therefore, uppercase Greek character also change with the family.
%\spoint Change uppercase Greek independent of text font
\subsection{Change uppercase Greek independent of text font}
In the Computer Modern font layout, uppercase Greek letters
are part of the roman font; see page~\pageref{cmr:table}.
\alt
Therefore, introducing another
text font (with another layout)
will change the uppercase Greek characters
(or even make them disappear).
One way of remedying this is by introducing a new family in
which the \n{cmr} font, which contains the uppercase Greek,
resides.
The control sequences accessing these characters then have
to be redefined:
\begin{verbatim}
\newfam\Kgreek
\textfont\Kgreek=cmr10 ...
\def\hex#1{\ifcase#10\or 1\or 2\or 3\or 4\or 5\or 6\or
7\or 8\or 9\or A\or B\or C\or D\or E\or F\fi}
\mathchardef\Gamma="0\hex\Kgreek00 % was: "0100
\mathchardef\Beta ="0\hex\Kgreek01 % was: "0101
\mathchardef\Gamma ...
\end{verbatim}
Note, by the way,
the absence of a either a space or a \cs{relax} token after
\n{\#1} in the definition of \cs{hex}. This implies that this
macro can only be called with an argument that is a
control sequence.
%\spoint Change the font of lowercase Greek
\subsection{Change the font of lowercase Greek }
and mathematical symbols
Lowercase Greek characters have math code
\verb>"01zz>, meaning they are always from the math italic family.
In order to change this one might redefine them,
for instance \verb.\mathchardef\alpha="710B.,
to make them variable family.
This is not done in plain \TeX, because the Computer Modern
roman font does not
have Greek lowercase, although it does have the uppercase characters.
Another way is to redefine them like \verb.\mathchardef\alpha="0n0B.
where \n{n} is the (hexadecimal) number of a family
compatible with math italic, containing for instance a bold
math italic font.
%\point Assorted remarks
\section{Assorted remarks}
%\spoint New fonts in formulas
\subsection{New fonts in formulas}
There are two ways to access a font inside mathematics.
\howto Change fonts in a math formula\par
\mdqon
After \cs{font}""\cs{newfont=....} it is not possible to get
\mdqoff
the `a' of the new font by \verb-$...{\newfont a}...$-
because \TeX\ does not look at the current font in math mode.
What does work is
\begin{verbatim}
$ ... \hbox{\newfont a} ...$
\end{verbatim}
but this precludes the use of the new font in script and
scriptscript styles.
The proper solution takes a bit more work:
\begin{verbatim}
\font\newtextfont=...
\font\newscriptfont=... \font\newsscriptfont=...
\newfam\newfontfam
\textfont\newfontfam=\newtextfont
\scriptfont\newfontfam=\newscriptfont
\scriptscriptfont\newfontfam=\newsscriptfont
\def\newfont{\newtextfont \fam=\newfontfam}
\end{verbatim}
after which the font can be used as
\begin{verbatim}
$... {\newfont a_{b_c}} ...$
\end{verbatim}
in all three styles.
%\spoint Evaluating the families
\subsection{Evaluating the families}
\TeX\ will only look at what is actually in the \cs{textfont}
et cetera of the various families at the end of the whole
formula. Switching fonts in the families is thus not possible
inside a single formula.
The number of 16 families may therefore turn out to be restrictive
for some applications.
%\subject[math] Mathematics Typesetting
\endofchapter
\chapter{Mathematics Typesetting}\label{math}
\TeX\ has two math modes, display and non-display, and
four styles, display, text, script, and scriptscript style, and
\altt
every object in math mode belongs to one of eight classes.
This chapter treats these concepts.
\begin{inventory}
\item [\cs{everymath}]
Token list inserted at the start of a non-display formula.
\item [\cs{everydisplay}]
Token list inserted at the start of a display formula.
\item [\cs{displaystyle}]
Select the display style of mathematics typesetting.
\item [\cs{textstyle}]
Select the text style of mathematics typesetting.
\item [\cs{scriptstyle}]
Select the script style of mathematics typesetting.
\item [\cs{scriptscriptstyle}]
Select the scriptscript style of mathematics typesetting.
\item [\cs{mathchoice}]
Give four variants of a formula for the four styles
of mathematics typesetting.
\item [\cs{mathord}]
Let the following character or subformula function
as an ordinary object.
\item [\cs{mathop}]
Let the following character or subformula function
as a large operator.
\item [\cs{mathbin}]
Let the following character or subformula function
as a binary operation.
\item [\cs{mathrel}]
Let the following character or subformula function as a relation.
\item [\cs{mathopen}]
Let the following character or subformula function
as a opening symbol.
\item [\cs{mathclose}]
Let the following character or subformula function
as a closing symbol.
\item [\cs{mathpunct}]
Let the following character or subformula function
as a punctuation symbol.
\item [\cs{mathinner}]
Let the following character or subformula function
as an inner formula.
\item [\cs{mathaccent}]
Place an accent in math mode.
\item [\cs{vcenter}]
Construct a vertical box, vertically centred
on the math axis.
\item [\cs{limits}]
Place limits over and under a large operator.
\item [\cs{nolimits}]
Place limits of a large operator as subscript and
superscript expressions.
\item [\cs{displaylimits}]
Restore default placement for limits.
\item [\cs{scriptspace}]
Extra space after subscripts and superscripts.
Plain \TeX\ default:~\n{0.5pt}
\item [\cs{nonscript}]
Cancel the next glue item if it occurs in
scriptstyle or scriptscriptstyle.
\item [\cs{mkern}]
Insert a kern measured in mu units.
\item [\cs{mskip}]
Insert glue measured in mu units.
\item [\cs{muskip}]
Prefix for skips measured in mu units.
\item [\cs{muskipdef}]
Define a control sequence to be a synonym for
a~\cs{muskip} register.
\item [\cs{newmuskip}]
Allocate a new muskip register.
\item [\cs{thinmuskip}]
Small amount of mu glue.
\item [\cs{medmuskip}]
Medium amount of mu glue.
\item [\cs{thickmuskip}]
Large amount of mu glue.
\item [\cs{mathsurround}]
Kern amount placed before and after in-line formulas.
\item [\cs{over}]
Fraction.
\item [\cs{atop}]
Place objects over one another.
\item [\cs{above}]
Fraction with specified bar width.
\item [\cs{overwithdelims}]
Fraction with delimiters.
\item [\cs{atopwithdelims}]
Place objects over one another with delimiters.
\item [\cs{abovewithdelims}]
Generalized fraction with delimiters.
\item [\cs{underline}]
Underline the following \gr{math symbol} or group.
\item [\cs{overline}]
Overline the following \gr{math symbol} or group.
\item [\cs{relpenalty}]
Penalty for breaking after a binary relation
not enclosed in a subformula.
Plain \TeX\ default:~\n{500}
\item [\cs{binoppenalty}]
Penalty for breaking after a binary operator not enclosed in
a subformula.
Plain \TeX\ default:~\n{700}
\item [\cs{allowbreak}]
Macro for creating a breakpoint.
\end{inventory}
%\point[math:modes] Math modes
\section{Math modes}
\label{math:modes}
\TeX\ changes to math mode when it encounters a math shift
\term math modes\par\term math shift character\par
character, category~3, in the input. After such an opening
math shift it investigates (without expansion) the next
token to see whether this is another math shift.
In the latter case \TeX\ starts processing in display math mode
until a closing double math shift is encountered:
\begin{disp}\verb> .. $$ >{\italic displayed formula}\verb> $$ ..>\end{disp}
Otherwise it starts processing an in-line formula
in non-display math mode:
\begin{disp}\verb> .. $ >{\italic in-line formula}\verb> $ ..>\end{disp}
The single math shift character is a \gr{horizontal command}.
Exception: displays are not possible in restricted horizontal
mode, so inside an \cs{hbox} the sequence
\verb>$$> is an empty math formula and
not the start of a displayed formula.
Associated with the two math modes are two \gr{token parameter}
registers (see also Chapter~\ref{token}):
at the start of an in-line formula the \csidx{everymath} tokens
are inserted; at the start of a displayed formula the
\cs{everydisplay} tokens are inserted.
Display math is treated further in the next chapter.
Math modes can be tested for: \cs{ifmmode} is true
in display and non-display math mode, and \cs{ifinner}
is true in non-display mode, but not in display mode.
%\point[math:styles] Styles in math mode
\section{Styles in math mode}
\label{math:styles}
Math formulas are set in any of eight styles:
\term math styles\par
\begin{description} \item [D]
display style, \item [T]
text style, \item [S]
script style, \item [SS]
scriptscript style,
\end{description}
and the four `cramped' variants $D'$, $T'$, $S'$, $SS'$ of
\term cramped styles\par
these. The cramped styles differ mainly in the
fact that superscripts are not raised as far as in
the original styles.
%\spoint Superscripts and subscripts
\subsection{Superscripts and subscripts}
\TeX\ can typeset a symbol or group
\term superscript\par\term subscript\par
as a superscript (or subscript) to the preceding
symbol or group, if that preceding item
does not already have a superscript
(subscript). Superscripts (subscripts) are specified by
the syntax
\begin{disp}\gr{superscript}\gr{math field}\end{disp}
or
\begin{disp}\gr{subscript}\gr{math field}\end{disp}
where a \gr{superscript} (\gr{subscript}) is either a character
of category~7 (8), or a control sequence \cs{let} to such
a character.
The plain format has the control
\cstoidx\char94\par\cstoidx\char95\par
sequences
\begin{verbatim}
\let\sp=^ \let\sb=_
\end{verbatim}
as implicit superscript
and subscript characters.
Specifying a superscript (subscript) expression as the first
item in an empty math list is equivalent to specifying
it as the superscript (subscript) of an empty expression.
For instance, \begin{disp}
\verb>$^{...}>\quad is equivalent to\quad \verb>${}^{...}>\end{disp}
For \TeX's internal calculations, superscript and subscript
expressions are made wider by \csidx{scriptspace};
the value of this in plain \TeX\ is~\n{0.5pt}.
%\spoint Choice of styles
\subsection{Choice of styles}
Ordering the four styles $D$, $T$, $S$, and~$SS$, and
considering the other four as mere variants, the
style rules for math mode are as follows:
\begin{itemize}\item In any style superscripts and subscripts
are taken from the next smaller style. Exception:
in display style they are taken in script style.
\item Subscripts are always in the cramped variant of
the style; superscripts are only cramped if the original
style was cramped.
\item In an \verb-{..\over..}- formula in any style
the numerator and denominator are taken from the next
smaller style.
\item The denominator is always in cramped style;
the numerator is only in cramped style if the original
style was cramped.
\item Formulas under a \cs{sqrt} or \cs{overline}
are in cramped style.\end{itemize}
Styles can be forced by the explicit commands
\alt
\cstoidx displaystyle\par\cstoidx textstyle\par
\cstoidx scriptstyle\par\cstoidx scriptscriptstyle\par
\cs{displaystyle}, \cs{textstyle}, \cs{scriptstyle},
and~\cs{scriptscriptstyle}.
In display style and text style the \cs{textfont} of the
current family is used,
in scriptstyle the \cs{scriptfont} is used, and in
\alt
scriptscriptstyle the \cs{scriptscriptfont} is used.
The primitive command
\cstoidx mathchoice\par
\begin{disp}\cs{mathchoice}\lb {\it D\/\rb\lb T\/\rb\lb S\/\rb\lb SS\/}\rb
\end{disp}
lets the user specify four variants of a formula for the
four styles.
\TeX\ constructs all four and inserts the appropriate one.
%\point[math:class] Classes of mathematical objects
\section{Classes of mathematical objects}
\label{math:class}
Objects in math mode belong to one of eight classes. Depending
\term math classes\par
on the class the object may be surrounded by
some amount of white space,
or treated specially in some way. Commands exist to force
symbols, or sequences of symbols, to act as
belonging to a certain class.
In the hexadecimal representation \verb>"xyzz>
the class is the \gr{3-bit number}~\n x.
This is the list of classes and commands that force those
classes. The examples are from the plain format
(see the tables starting at page~\pageref{math:sym:tables}).
\begin{enumerate} \message{set a counter here!}%\SetCounter:item=-1
\item {\em ordinary\/}: lowercase Greek characters and those symbols
that are `just symbols';
the command \csidx{mathord} forces this class.
\item {\em large operator\/}: integral and sum signs,
and `big' objects such as \cs{bigcap} or \cs{bigotimes};
the command \csidx{mathop} forces this class.
Characters that are
large operators are centred vertically, and they
may behave differently in display style from in the
other styles; see below.
\item {\em binary operation\/}: plus and minus,
and things such as \cs{cap} or \cs{otimes};
the command \csidx{mathbin} forces this class.
\item {\em relation\/} (also called {\em binary relation\/}):
equals, less than, and greater than signs, subset and
superset, perpendicular, parallel;
the command \csidx{mathrel} forces this class.
\item {\em opening symbol\/}: opening brace, bracket, parenthesis, angle,
\altt
floor, ceiling;
the command \csidx{mathopen} forces this class.
\item {\em closing symbol\/}: closing brace, bracket, parenthesis, angle,
\altt
floor, ceiling;
the command \csidx{mathclose} forces this class.
\item {\em punctuation\/}: most punctuation marks, but
\n:~is a relation, the \cs{colon} is a punctuation colon;
the command \csidx{mathpunct} forces this class.
\item {\em variable family\/}: symbols in this class change font
with the \cs{fam} parameter; in plain \TeX\ uppercase
Greek letters and ordinary letters and digits are
in this class.
\end{enumerate}
There is one further class: the {\em inner\/} subformulas.
No characters can be assigned to this class, but characters and
subformulas can be forced into it by \csidx{mathinner}.
The \gr{generalized fraction}s and \verb-\left...\right- groups
are inner formulas. Inner formulas are surrounded
by some white space; see the table below.
Other subformulas than those that are inner are treated as
ordinary symbols. In particular, subformulas enclosed in
braces are ordinary: \verb-$a+b$- looks like `$a\mathop+b$', but
\message{Check a+b look}%
\verb-$a{+}b$- looks like~`$a{+}b$'. Note, however, that
in \verb-${a+b}$- the whole subformula is treated as an
ordinary symbol, not its components;
therefore the result is~`${a+b}$'.
%\point Large operators and their limits
\section{Large operators and their limits}
The large operators in the Computer Modern fonts come in
two sizes: one for text style and one for display style.
Control sequences such as \cs{sum} are simply defined by
\cs{mathchardef} to correspond to a position in a font:
\begin{verbatim}
\mathchardef\sum="1350
\end{verbatim}
but if the
current style is display style, \TeX\ looks to see whether
that character has a successor in the font.
Large operators in text style behave as if they are followed
\cstoidx limits\par\cstoidx nolimits\par
by \cs{nolimits}, which places the limits as sub/superscript
expressions after the
operator:\begin{disp}$\sum_{k=1}^\infty$\end{disp}
In display style they behave as if they are followed by
\cs{limits}, which places the limits over and under
the operator:\begin{disp}$\displaystyle\sum_{k=1}^\infty$\end{disp}
The successor mechanism (see page~\pageref{successor})
\alt
lets \TeX\ take a larger variant
of the delimiter here.
The integral sign has been defined in plain \TeX\ as
\begin{verbatim}
\mathchardef\intop="1352 \def\int{\intop\nolimits}
\end{verbatim}
which places the limits after the operator, even in display style:
\begin{disp}$\displaystyle\int_0^\infty e^{-x^2}\,dx=\sqrt{\pi}/2$
\end{disp}
With \verb-\limits\nolimits- or \verb-\nolimits\limits- the
last specification has precedence; the default placement
can be restored by \csidx{displaylimits}. For instance,
\begin{verbatim}
$ ... \sum\limits\displaylimits ... $
\end{verbatim}
is equivalent to \begin{verbatim}
$ ... \sum ... $
\end{verbatim}
and
\begin{verbatim}
$$ ... \sum\nolimits\displaylimits ... $$
\end{verbatim}
is equivalent to
\begin{verbatim}
$$ ... \sum ... $$
\end{verbatim}
%\point Vertical centring: \cs{vcenter}
\section{Vertical centring: \protect\cs{vcenter}}
Each formula has an {\em axis\/}, which is for an in-line
\term axis of math formulas\par\term centring of math formulas\par
formula about half the x-height of the surrounding
text; the exact value is the \cs{fontdimen22} of the
font in family~2, the symbol font, in the current style.
The bar line in fractions is placed on the axis; large
operators, delimiters and \cs{vcenter} boxes are centred on it.
A \csidx{vcenter}\label{vcenter}
box is a vertical box that is arranged
so that it is centred on the math axis.
It is possible to give a \n{spread} or \n{to}
specification with a \cs{vcenter} box.
The \cs{vcenter} box is allowed only in math mode, and
it does not behave like other boxes; for instance, it can
not be stored in a box register. It does not qualify as
a~\gr{box}. See page~\pageref{tvcenter} for a macro that
repairs this.
%\point[muglue] Mathematical spacing: \n{mu} glue
\section{Mathematical spacing: \n{mu} glue}
\label{muglue}
Spacing around mathematical objects is measured in \n{mu}
\term math spacing\par\term math unit\par\term mu glue\par
units. A~\n{mu} is $1/18$th part of \cs{fontdimen6}
of the font in family~2 in the current style,
the `quad' value of the symbol font.
%\spoint Classification of \n{mu} glue
\subsection{Classification of \n{mu} glue}
The user can specify \n{mu} spacing by \cs{mkern} or~\cs{mskip},
\cstoidx mkern\par\cstoidx mskip\par
but most \n{mu} glue is inserted automatically by \TeX,
based on the classes to which objects belong (see above).
First, here are some rules of thumb describing the global
behaviour.
\begin{itemize} \item A \cs{thickmuskip} (default value in plain
\TeX: \n{5mu plus 5mu})
\cstoidx thickmuskip\par
is inserted around (binary) relations, except where these are
preceded or followed by other relations or punctuation, and
except if they follow an open, or precede a close symbol.
\item A \csidx{medmuskip} (default value in plain
\TeX: \n{4mu plus 2mu minus 4mu})
is put around binary operators.
\item A \csidx{thinmuskip}
(default value in plain \TeX: \n{3mu}) follows after
punctuation, and is put around inner objects, except where these
are followed by a close or preceded by an open symbol, and
except if the other object is a large operator or a
binary relation.
\item No \n{mu} glue is inserted after an open or before a close
symbol except where the latter is preceded by punctuation;
no \n{mu} glue is inserted also before punctuation, except where
the preceding object is punctuation or an inner object.
\end{itemize}
The following table gives the complete definition of mu glue
between math objects.
\begin{disp}\leavevmode
\vbox{\offinterlineskip
\halign{#\enspace\hfil&#\enspace\hfil\vrule
&&\hfil\enspace#\hfil\strut\cr
\omit\hfil&\omit\hfil& 0:& 1:& 2:& 3:& 4:& 5:& 6:\cr
\omit\hfil&\omit\hfil&\hfill Ord&\hfill Op&\hfill Bin&\hfill Rel&
\hfill Open&\hfill Close&\hfill Punct&\hfill Inner\cr
\omit\hfil&\omit\hfil&\multispan8\hrulefill\cr
0:&Ord& 0& 1&(2)&(3)& 0& 0& 0&(1)\cr
1:&Op& 1& 1& *&(3)& 0& 0& 0&(1)\cr
2:&Bin& (2)&(2)& *& *&(2)& *& *&(2)\cr
3:&Rel& (3)&(3)& *& 0&(2)& *& *&(2)\cr
4:&Open& 0& 0& *& 0& 0& 0& 0& 0\cr
5:&Close& 0& 1&(2)&(3)& 0& 0& 0&(1)\cr
6:&Punct&(1)&(1)& *&(1)&(1)&(1)&(1)&(1)\cr
&Inner&(1)& 1&(2)&(3)&(1)& 0&(1)&(1)\cr
% \omit\hfil&\omit\hfil&\multispan8\hrulefil\cr
}}
\end{disp}
where the symbols have the following meanings:
\begin{itemize}\item 0, no space; 1, thin space; 2, medium space;
3, thick space;
\item $(\cdot)$, insert only in text and display
mode, not in script or scriptscript mode;
\item cases * cannot occur, because a Bin object is converted
to Ord if it is the first in the list, preceded by
Bin, Op, Open, Punct, Rel, or followed by Close,
Punct, and Rel; also, a Rel is converted to Ord when
\alt
it is followed by Close or Punct.
\end{itemize}
Stretchable \n{mu} glue is set according to the same rules that
govern ordinary glue. However, only \n{mu} glue on the outer
level can be stretched or shrunk; any \n{mu} glue enclosed
in a~group is set at natural width.
%\spoint Muskip registers
\subsection{Muskip registers}
Like ordinary glue, \n{mu} glue can be stored in registers,
\cstoidx muskip\par\cstoidx muskipdef\par\cstoidx newmuskip\par
the \cs{muskip} registers,
of which there are 256 in \TeX.
The registers are denoted by
\begin{disp}\cs{muskip}\gr{8-bit number}\end{disp}
and they can be assigned to a control sequence by
\begin{disp}\cs{muskipdef}\gr{control sequence}\gr{equals}\gr{8-bit number}
\end{disp}
and there is a macro that allocates unused registers:
\begin{disp}\cs{newmuskip}\gr{control sequence}\end{disp}
Arithmetic for mu glue exists as for glue; see
Chapter~\ref{glue}.
%\spoint Other spaces in math mode
\subsection{Other spaces in math mode}
In math mode space tokens are ignored; however,
the math code of the space character is \verb-"8000-
in plain \TeX,
so if its category is made `letter' or `other character', it
will behave like an active character in math mode.
See also page~\pageref{mcode:8000}.
Admissible glue in math mode is of type~\gr{mathematical skip},
which is either a \gr{horizontal skip} (see Chapter~\ref{hvmode})
or~\cs{mskip}\gr{muglue}. Leaders in math mode can be specified
with a \gr{mathematical skip}.
A glue item preceded by \csidx{nonscript}
is cancelled if it occurs in scriptstyle or scriptscriptstyle.
Control space functions in math mode
\alt
as it does in horizontal mode.
In-line formulas are surrounded by kerns of size
\csidx{mathsurround}, the so-called `math-on' and
`math-off' items. Line breaking can occur at the front of
the math-off kern if it is followed by glue.
%\point Generalized fractions
\section{Generalized fractions}
Fraction-like objects can be set with six primitive commands
of type \gr{generalized fraction}.
\term generalized fractions\par
Each of these takes the preceding and the following subformulas
and puts them over one another, if necessary with a fraction
bar and with delimiters.
\begin{description} \item [\csidx{over}]
is the ordinary fraction; the bar thickness is \cs{fontdimen8}
of the extension font:
\begin{disp}\verb>$\pi\over2$>\quad gives\quad `$\pi\over2$'\message{pi over 2}\end{disp}
\item [\csidx{atop}]
is equivalent to a fraction with zero bar thickness:
\begin{disp}\verb>$\pi\atop2$>\quad gives\quad `$\pi\atop2$'\end{disp}
\item [\csidx{above}\gr{dimen}]
specifies the thickness
of the bar line explicitly:
\begin{disp}\verb>$\pi\above 1pt 2$>\quad gives\quad `$\pi\above 1pt 2$'\end{disp}
\end{description}
To each of these three there corresponds a \cs{...withdelims} variant
\cstoidx overwithdelims\par\cstoidx atopwithdelims\par
\cstoidx abovewithdelims\par
that lets the user specify delimiters for the expression.
For example, the most general command, in terms of which
all five others could have been defined, is
\begin{disp}\cs{abovewithdelims}\gr{delim$_1$}\gr{delim$_2$}\gr{dimen}.
\end{disp}
Delimiters in these generalized fractions do not grow with the
enclosed expression: in display mode a delimiter is taken
which is at least \cs{fontdimen20} high, otherwise
\alt
it has to be
at least \cs{fontdimen21} high.
These dimensions are taken
from the font in family~2, the symbol font, in the current style.
The control sequences \cs{over}, \cs{atop}, and \cs{above}
are primitives, although they could have been defined
as \cs{...withdelims..}, that is, with two null delimiters.
Because of these implied surrounding null delimiters,
there is a kern of size \cs{nulldelimiterspace} before and after
these simple generalized fractions.
%\point Underlining, overlining
\section{Underlining, overlining}
The primitive commands \csidx{underline} and \csidx{overline} take a
\gr{math field} argument, that is, a \gr{math symbol} or
a group, and draw a line under or over it.
The result is an `Under' or `Over' atom, which
is appended to the current math list.
The line thickness is font dimension~8 of the extension font,
which also determines the clearance between the line and
the \gr{math field}.
Various other \cs{over...} and \cs{under...} commands exist
in plain \TeX;
these are all macros
that use the \TeX\ \cs{halign} command.
%\point Line breaking in math formulas
\section{Line breaking in math formulas}
In-line formulas can be broken after relations and binary operators.
\cstoidx relpenalty\par\cstoidx binoppenaly\par
\term penalties in math mode\par
The respective penalties are the \cs{relpenalty}
and the~\cs{binoppenalty}. However, \TeX\ will only break
after such symbols if they are not enclosed in braces.
Other breakpoints can be created with~\cs{allowbreak},
\cstoidx allowbreak\par\term breakpoints in math lists\par
which is an abbreviation for~\cs{penalty0}.
Unlike in horizontal or vertical mode where putting two penalties
in a row is equivalent to just placing the smallest one,
in math mode a penalty placed at a break point \ldash that is,
after a relation or binary operator \rdash will effectively
replace the old penalty by the new one.
%\point[fam23:fontdims] Font dimensions of families 2 and 3
\section{Font dimensions of families 2 and 3}
\label{fam23:fontdims}
If a font is used in text mode, \TeX\ will look at its
first 7 \cs{fontdimen} parameters
(see page~\pageref{font:dims}), for instance to
control spacing.
In math, however, more font dimensions are needed.
\TeX\ will look at the first 22 parameters of the
fonts in family~2, and the first 13 of the fonts in
family~3, to control various
aspects of math typesetting. The next two subsections
have been quoted loosely from~\cite{BB:ISO}.
%\spoint Symbol font attributes
\subsection{Symbol font attributes}
Attributes of the font in family 2 mainly specify the
\term symbol font\par
initial vertical positioning
of parts of fractions, subscripts, superscripts, et cetera.
The position determined by applying these
attributes may be further modified because of other
conditions, for example the presence of a fraction bar.
One text font dimension, number~6,
the quad, determines the size of mu glue;
see above.
Fraction numerator attributes: minimum shift up, from
the main baseline, of the baseline of the numerator
of a generalized fraction,
\begin{enumerate} \message{set a counter here!}%\SetCounter:item=7
\item num1:
for display style,
\item num2:
for text style or smaller if a fraction bar is present,
\item num3:
for text style or smaller if no fraction bar is present.
\end{enumerate}
Fraction denominator attributes: minimum shift down, from
the main baseline, of the baseline of the denominator
of a generalized fraction,
\begin{enumerate} \message{set a counter here!}%\SetCounter:item=10
\item denom1:
for display style,
\item denom2:
for text style or smaller.
\end{enumerate}
Superscript attributes: minimum shift up, from the main baseline,
of the baseline of a superscript,
\begin{enumerate} \message{set a counter here!}%\SetCounter:item=12
\item sup1:
for display style,
\item sup2:
for text style or smaller, non-cramped,
\item sup3:
for text style or smaller, cramped.
\end{enumerate}
Subscript attributes: minimum shift down, from the main baseline,
of the baseline of a subscript,
\begin{enumerate} \message{set a counter here!}%\SetCounter:item=15
\item sub1:
when no superscript is present,
\item sub2:
when a superscript is present.
\end{enumerate}
Script adjustment attributes: for use only with non-glyph,
that is, composite, objects.
\begin{enumerate} \message{set a counter here!}%\SetCounter:item=17
\item sup\_drop:
maximum distance of superscript baseline below top of nucleus
\item sub\_drop:
minimum distance of subscript baseline below bottom of nucleus.
\end{enumerate}
Delimiter span attributes: height plus depth of delimiter enclosing
a generalized fraction,
\begin{enumerate} \message{set a counter here!}%\SetCounter:item=19
\item delim1:
in display style,
\item delim2:
in text style or smaller.
\end{enumerate}
A parameter with many uses, the height of the math axis,
\begin{enumerate} \message{set a counter here!}%\SetCounter:item=21
\item axis\_height:
the height above the baseline
of the fraction bar, and the centre of large delimiters
and most operators and relations. This position is
used in vertical centring operations.
\end{enumerate}
%\spoint Extension font attributes
\subsection{Extension font attributes}
Attributes of the font in family 3 mostly specify
the way the limits of large operators are set.
The first parameter, number 8, default\_rule\_thickness,
serves many purposes. It
is the thickness of the rule used for overlines,
underlines, radical extenders (square root),
and fraction bars. Various clearances are also specified
in terms of this dimension: between the fraction bar and
the numerator and denominator, between an object and
the rule drawn by an underline, overline, or radical,
and between the bottom of superscripts and top of subscripts.
Minimum clearances around large operators are as follows:
\begin{enumerate} \message{set a counter here!}%\SetCounter:item=8
\item big\_op\_spacing1:
minimum clearance between baseline of upper limit and top
of large operator; see below.
\item big\_op\_spacing2:
minimum clearance between bottom of large operator and top of
lower limit.
\item big\_op\_spacing3:
minimum clearance between baseline of
upper limit and top of large operator,
taking into account depth of upper limit; see below.
\item big\_op\_spacing4:
minimum clearance between bottom of large operator and top of lower
limit, taking into account height of lower limit; see below.
\item big\_op\_spacing5:
clearance above upper limit or below lower limit of a large operator.
\end{enumerate}
The resulting clearance above an operator is the maximum
of parameter~7, and parameter~11 minus the depth of the
upper limit.
The resulting clearance below an operator is the maximum
of parameter~10, and parameter~12 minus the height of the
lower limit.
%\spoint Example: subscript lowering
\subsection{Example: subscript lowering}
The location of a subscript depends on whether there
\alt
\howto Adjust subscript lowering\par
is a superscript; for instance
\begin{disp} $X_1+Y^2_1=1$\end{disp}
If you would rather have that look like
\begin{disp} $\global\tempdima=\fontdimen16\textfont2\relax
\global\tempdimb=\fontdimen17\textfont2\relax
\fontdimen16\textfont2=3pt \fontdimen17\textfont2=3pt
X_1+Y^2_1=1$,$\fontdimen16\textfont2=\tempdima\relax
\fontdimen17\textfont2=\tempdimb\relax$
\end{disp}
\message{check lowering}
it suffices to specify
\begin{verbatim}
\fontdimen16\textfont2=3pt \fontdimen17\textfont2=3pt
\end{verbatim}
which makes the subscript drop equal in both cases.
%\subject[displaymath] Display Math
\endofchapter
\chapter{Display Math}\label{displaymath}
Displayed formulas are set on a line of their own, usually
somewhere in a paragraph. This chapter explains
how surrounding white space (both above/below and to the
left/right) is calculated.
\begin{inventory}
\item [\cs{abovedisplayskip \cs{belowdisplayskip}}]
\mdqon
Glue above/""below a display.
\mdqoff
Plain \TeX\ default:~\n{12pt plus 3pt minus 9pt}
\item [\cs{abovedisplayshortskip \cs{belowdisplayshortskip}}]
\mdqon
Glue above/""below a display if the line preceding the display
\mdqoff
was short.
Plain \TeX\ defaults:~\n{0pt plus 3pt} and
\n{7pt plus 3pt minus 4pt} respectively.
\item [\cs{predisplaypenalty \cs{postdisplaypenalty}}]
\mdqon
Penalty placed in the vertical list above/""below a display.
\mdqoff
Plain \TeX\ defaults:~\n{10$\,$000} and~\n{0}
respectively.
\item [\cs{displayindent}]
Distance by which the box, in which the display
is centred, is indented owing to hanging indentation.
\item [\cs{displaywidth}]
Width of the box in which the display is centred.
\item [\cs{predisplaysize}]
Effective width of the line preceding the display.
\item [\cs{everydisplay}]
Token list inserted at the start of a display.
\item [\cs{eqno}]
Place a right equation number in a display formula.
\item [\cs{leqno}]
Place a left equation number in a display formula.
\end{inventory}
%\point Displays
\section{Displays}
\TeX\ starts building a display when it encounters two
\term displays\par
math shift characters (characters of category~3,
\verb>$>~in plain \TeX) in a row.
Another such pair (possibly followed
\alt by one optional space) indicates the end of the display.
Math shift is a \gr{horizontal command}, but displays are only
allowed in unrestricted horizontal mode
(\verb>$$>~is an empty math formula in restricted horizontal mode).
Displays themselves, however, are started in the
surrounding (possibly internal) vertical mode in order to calculate
quantities such as~\cs{prevgraf}; the result of the display is
appended to the vertical list.
The part of the paragraph above the display is broken into
lines as an independent paragraph (but \cs{prevgraf} is
carried over; see below), and the remainder of the
paragraph is set, starting with an empty list and \cs{spacefactor}
equal to~1000.
The \cs{everypar} tokens are not inserted for the part of the
paragraph after the display, nor is \cs{parskip} glue inserted.
Right at the beginning of the display the \csidx{everydisplay}
token list is inserted (but after the calculation of
\cs{displayindent}, \cs{displaywidth}, and \cs{predisplaysize}).
See page~\pageref{left:display} for an example of the use
of \cs{everydisplay}.
The page builder is exercised
before the display
(but after the \cs{everydisplay} tokens have been inserted),
and after the display finishes.
The `display style' of math typesetting was treated in
Chapter~\ref{mathfont}.
%\point Displays in paragraphs
\section{Displays in paragraphs}
Positioning of a display in a paragraph may be influenced
by hanging indentation or a \cs{parshape} specification.
For this, \TeX\ uses the \cs{prevgraf} parameter
(see Chapter~\ref{par:shape}), and
acts as if the display is three lines deep.
If $n$ is the value of \cs{prevgraf} when the display starts
\ldash so there are $n$ lines of text above the display \rdash
\cs{prevgraf} is set to to $n+3$ when the paragraph resumes.
The display occupies, as it were, lines $n+1$, $n+2$, and~$n+3$.
The shift and line width for the display are those
that would hold for line~$n+2$.
The shift for the display is recorded in \cs{displayindent};
\cstoidx displayindent\par\cstoidx displaywidth\par
the line width is recorded in \cs{displaywidth}. These parameters
(and the \cs{predisplaysize} explained below)
are set immediately after the \verb>$$> has been scanned.
Usually they are equal to zero and \cs{hsize} respectively.
The user can change the values of these parameters;
\TeX\ will use the
values that hold after the math list
of the display has been processed.
Note that a display is vertical material, and therefore
not influenced by settings of \cs{leftskip} and \cs{rightskip}.
%\point Vertical material around displays
\section{Vertical material around displays}
A display is preceded in the vertical list by
\begin{itemize}\item a penalty of size \cs{predisplaypenalty}
\cstoidx predisplaypenalty\par\cstoidx abovedisplayskip\par
\cstoidx abovedisplayshortskip\par
(plain \TeX\ default~$10\,000$), and
\item glue of size \cs{abovedisplayskip}
or \cs{abovedisplayshortskip}; this glue is omitted in
cases where a~\cs{leqno} equation number is set on
a line of its own (see below).\end{itemize}
A display is followed by
\begin{itemize}\item a penalty of size \cs{postdisplaypenalty}
\cstoidx postdisplaypenalty\par\cstoidx belowdisplayskip\par
\cstoidx belowdisplayshortskip\par
(default~0), and possibly
\item glue of size \cs{belowdisplayskip} or
\cs{belowdisplayshortskip}; this glue is omitted in cases
where an~\cs{eqno} equation number is set on a line of
its own (see below).\end{itemize}
The `short' variants of the glue are taken if
there is no \cs{leqno} left equation number, and if
the last line of the paragraph above the display is
short enough for the display to be raised a bit without
coming too close to that line.
In order to decide this, the effective width of the
preceding line is saved in \csidx{predisplaysize}.
This value is calculated immediately after the opening \verb>$$>
of the display has
been scanned, together with the \cs{displaywidth}
and \cs{displayindent} explained above.
Remembering that the part of the paragraph above the display
has already been broken into lines, the following method
for finding the effective width of the last line ensues.
\TeX\ takes the last box of the list, which is a horizontal
box containing the last line, and locates the right edge
of the last box in it. The \cs{predisplaysize} is then
the place of that rightmost edge, plus any amount by which
the last line was shifted, plus two ems in the current font.
There are two exceptions to this. The \cs{predisplaysize}
is taken to be $-$\cs{maxdimen} if there was no previous line,
that is,
the display started the paragraph, or it followed another display;
\cs{predisplaysize} is taken to be \cs{maxdimen}
\term machine dependence\par
if the glue in the last line was not set at its natural width,
which may happen if the \cs{parfillskip} contained only finite
stretch. The reason for the last clause is that glue
\mdqon
setting is slightly machine"-dependent, and such dependences
\mdqoff
should be kept out of \TeX's global decision processes.
%\point Glue setting of the display math list
\section{Glue setting of the display math list}
The display has to fit in \cs{displaywidth},
but in addition to the formula there
may be an equation number. The minimum separation
between the formula and the equation number should
be one em in the symbol font, that is,
\mdqon
\cs{font\-dimen\-6}""\cs{textfont2}.
\mdqoff
If the formula plus any equation number
and separation fit into \cs{displaywidth},
the glue in the formula is set at its natural width.
If it does not fit,
but the formula contains enough shrink, it is shrunk.
Otherwise \TeX\ puts any equation number
on a line of its own, and the glue in the formula is
set to fit it in \cs{displaywidth}.
With the equation
number on a separate line the formula may now very well fit in the
display width; however,
if it was a very long formula the box in which it is
set may still be overfull. \TeX\ nevers breaks a displayed
formula.
%\point Centring the display formula: displacement
\section{Centring the display formula: displacement}
Based on the width of the box containing the formula \ldash which
may not really `contain' it; it may be overfull \rdash
\TeX\ tries to centre the formula in the \cs{displaywidth},
that is, without taking the equation number into account.
Initially, a displacement is calculated that is
half the difference between \cs{displaywidth} and the
width of the formula box.
However, if there is an equation number that will not
be put on a separate line and the displacement is less than
twice the width of the equation number, a new displacement
is calculated. This new displacement is zero if the formula
started with glue; otherwise it is such that the
formula box is centred in the space left by the equation
number.
If there was no equation number, or if the equation number
will be put on a separate line, the formula box
is now placed, shifted right by \cs{displayindent} plus
the displacement calculated above.
%\point Equation numbers
\section{Equation numbers}
The user can specify a equation number for a display
by ending it with
\cstoidx eqno\par\cstoidx leqno\par\term equation numbering\par
\begin{Disp}\cs{eqno}\gr{math mode material}\verb>$$>\end{Disp}
for an equation number placed on the right, or
\begin{Disp}\cs{leqno}\gr{math mode material}\verb>$$>\end{Disp}
for an equation number placed on the left.
%\spoint Ordinary equation numbers
\subsection{Ordinary equation numbers}
Above it was described how \TeX\ calculates a displacement
from the display formula and the equation number, if this
is to be put on the same line as the formula.
If the equation number was a \cs{leqno} number,
\TeX\ places a box containing
\begin{itemize}\item the equation number,
\item a kern with the size of the displacement calculated, and
\item the formula.\end{itemize}
This box is shifted right by \cs{displayindent}.
If the equation number was an \cs{eqno} number,
\TeX\ places a box containing
\begin{itemize}\item the formula,
\item a kern with the size of the displacement calculated, and
\item the equation number.\end{itemize}
This box is shifted right by \cs{displayindent} plus
the displacement calculated.
%\spoint The equation number on a separate line
\subsection{The equation number on a separate line}
Since displayed formulas may become rather big, \TeX\ can decide
(as was described above)
that any equation number should be placed on a line of its own.
A~left-placed equation number is then to be placed above the
display, in a box that is shifted right by \cs{displayindent};
a right-placed equation number will be placed below the display,
in a box that is shifted to the right
by \cs{displayindent} plus \cs{displaywidth} minus the width of
the equation number box.
In both cases a penalty of $10\,000$ is placed between the equation
number box and the formula.
\TeX\ does not put extra glue above a left-placed
equation number or below
a right-placed equation number; \TeX\ here relies on
the baselineskip mechanism.
%\point[left:display] Non-centred displays
\section{Non-centred displays}
\label{left:display}
As a default, \TeX\ will centre displays.
\term displays, non-centred\par
In order to get non-centred displays some
macro trickery is needed.
One approach would
be to write a macro \cs{DisplayEquation}
that would basically look like
\begin{verbatim}
\def\DisplayEquation#1{%
\par \vskip\abovedisplayskip
\hbox{\kern\parindent$\displaystyle#1$}
\vskip\belowdisplayskip \noindent}
\end{verbatim}
but it would be nicer if one could just write
\begin{verbatim}
$$ ... \eqno ... $$
\end{verbatim}
\mdqon
and having this come out as a left"-aligning display.
\mdqoff
Using the \cs{everydisplay} token list, the above
idea can be realized. The basic idea is to write
\begin{verbatim}
\everydisplay{\IndentedDisplay}
\def\IndentedDisplay#1$${ ...
\end{verbatim}
so that the macro \cs{IndentedDisplay}
will receive the formula, including any equation number.
The first step is now to extract an equation number
if it is present. This makes creative use of delimited
macro parameters.\begin{verbatim}
\def\ExtractEqNo#1\eqno#2\eqno#3\relax
{\def\Equation{#1}\def\EqNo{#2}}
\def\IndentedDisplay#1$${%
\ExtractEqNo#1\eqno\eqno\relax
\end{verbatim}
Next the equation should be set in the available
space \cs{displaywidth}:
\begin{verbatim}
\hbox to \displaywidth
{\kern\parindent
$\displaystyle\Equation$\hfil$\EqNo$}$$
}
\end{verbatim}
Note that the macro ends in the closing \verb>$$>
to balance the opening dollars that caused
insertion of the \cs{everydisplay} tokens.
This also means that the box containing the
displayed material will automatically be
surrounded by \cs{abovedisplayskip} and
\cs{belowdisplayskip} glue.
There is no need to use \cs{displayindent} anywhere
in this macro, because \TeX\ itself will shift the
display appropriately.
% \begin{comment}
% \endinput
% baselineskip around displays?
% \end{comment}
%%%% end of input file [math]
%\InputFile:align
%%%% this is input file [align]
%\subject[align] Alignment
\endofchapter
\chapter{Alignment}\label{align}
\TeX\ provides a general alignment mechanism for making tables.
\term alignments\par\term tables\par
\begin{inventory}
\item [\cs{halign}]
Horizontal alignment.
\item [\cs{valign}]
Vertical alignment.
\item [\cs{omit}]
Omit the template for one alignment entry.
\item [\cs{span}]
Join two adjacent alignment entries.
\item [\cs{multispan}]
Macro to join a number of adjacent alignment entries.
\item [\cs{tabskip}]
Amount of glue in between columns (rows)
of an \cs{halign} (\cs{valign}).
\item [\cs{noalign}]
Specify vertical (horizontal)
material to be placed in between rows (columns) of
an \cs{halign} (\cs{valign}).
\item [\cs{cr}]
Terminate an alignment line.
\item [\cs{crcr}]
Terminate an alignment line if it has
not already been terminated by~\cs{cr}.
\item [\cs{everycr}]
Token list inserted after every \cs{cr} or non-redundant
\cs{crcr}.
\item [\cs{centering}]
Glue register in plain \TeX\ for centring
\cs{eqalign} and \cs{eqalignno}.
Value: \n{0pt plus 1000pt minus 1000pt}
\item [\cs{hideskip}]
Glue register in plain \TeX\ to make alignment entries invisible.
Value: \n{-1000pt plus 1fill}
\item [\cs{hidewidth}]
Macro to make preceding or following entry invisible.
\end{inventory}
%\point Introduction
\section{Introduction}
\TeX\ has a sophisticated alignment mechanism, based on
templates, with one template entry per column or row.
The templates may contain any common elements
of the table entries, and in general they contain
instructions for typesetting the entries.
\TeX\ first calculates widths (for \cs{halign}) or heights
(for \cs{valign}) of all entries;
then it typesets the whole alignment using in each column (row)
the maximum width (height) of entries in that column (row).
%\point Horizontal and vertical alignment
\section{Horizontal and vertical alignment}
The two alignment commands in \TeX\ are
\cstoidx halign\par\cstoidx valign\par
\begin{disp}\cs{halign}\gr{box specification}\lb\gr{alignment material}\rb
\end{disp} for horizontal alignment of columns, and
\begin{disp}\cs{valign}\gr{box specification}\lb\gr{alignment material}\rb
\end{disp} for vertical alignment of rows.
\cs{halign} is a \gr{vertical command}, and
\cs{valign} is a \gr{horizontal command}.
The braces induce a new level of grouping; they can be
implicit.
The discussion below will mostly focus on horizontal
alignments, but, replacing `column' by `row' and vice versa,
it applies to vertical alignments too.
%\spoint Horizontal alignments: \cs{halign}
\subsection{Horizontal alignments: \cs{halign}}
Horizontal alignments yield a list of horizontal boxes, the rows,
\term horizontal alignment\par
which are placed on the surrounding vertical list.
The page builder is exercised after the alignment rows have been
added to the vertical list.
The value of \cs{prevdepth} that holds before the alignment
is used for the baselineskip of the first row,
and after the alignment \cs{prevdepth} is set to a value based
on the last row.
Each entry is processed in a group of its own,
in restricted horizontal mode.
A special type of horizontal alignment exists: the
\term display alignment\par
display alignments, specified as
\begin{disp}\n{\$\$}\gr{assignments}\cs{halign}\gr{box specification}\lb\n{...}\rb
\gr{assignments}\n{\$\$}\end{disp}
Such an alignment is shifted by \cs{displayindent} (see
Chapter~\ref{displaymath}) and surrounded by
\cs{abovedisplayskip} and \cs{belowdisplayskip} glue.
%\spoint Vertical alignments: \cs{valign}
\subsection{Vertical alignments: \cs{valign}}
Vertical alignments are `rotated' horizontal alignments:
\term vertical alignment\par
they are placed on the surrounding horizontal lists,
and yield a row of columns. The \cs{spacefactor} value
is treated the same way as the \cs{prevdepth} for horizontal
alignments: the value current before the alignment is used
for the first column, and the value reached after the last column
is used after the alignment. In between columns the \cs{spacefactor}
value is~1000.
Each entry is in a group of its own, and it is processed
in internal vertical mode.
%\spoint Material between the lines: \cs{noalign}
\subsection{Material between the lines: \cs{noalign}}
Material that has to be contained in the alignment, but
should not be treated as an entry or series of entries,
\cstoidx noalign\par
can be given by
\begin{disp}\cs{noalign}\gr{filler}\lb\gr{vertical mode material}\rb
\end{disp} for horizontal alignments, and
\begin{disp}\cs{noalign}\gr{filler}\lb\gr{horizontal mode material}\rb
\end{disp} for vertical alignments.
Examples are
\begin{verbatim}
\noalign{\hrule}
\end{verbatim}
for drawing a horizontal rule
between two lines of an \cs{halign},
and \begin{verbatim}
\noalign{\penalty100}
\end{verbatim}
for discouraging a page break (or line break) in
between two rows (columns) of an \cs{halign} (\cs{valign}).
%\spoint Size of the alignment
\subsection{Size of the alignment}
The \gr{box specification} can be used to give the alignment
a predetermined size: for instance
\begin{verbatim}
\halign to \hsize{ ... }
\end{verbatim}
Glue contained in the entries of the alignment has no role in this;
any stretch or
shrink required is taken from the \cs{tabskip} glue.
This is explained below.
%\point The preamble
\section{The preamble}
Each line in an alignment is terminated by \cs{cr};
the first line is called the {\it template line}.
It is of the form
\begin{disp}\n{$u_1$\#$v_1$\&...\&$u_n$\#$v_n$}\cs{cr}\end{disp}
where each $u_i$, $v_i$ is a (possibly empty) arbitrary sequence
of tokens, and the template entries are separated by
the {\italic alignment tab
\term alignment tab\par
character} (\n\&~in plain \TeX),
that is, any character of category~4.
A $u_i$\n\#$v_i$ sequence is the template that will be
used for the $i\,$th column: whatever sequence $\alpha_i$
the user specifies
as the entry for that column will be inserted at the
parameter character. The sequence $u_i\alpha_iv_i$ is
then processed to obtain the actual entry for the $i\,$th
column on the current line. See below for more details.
The length $n$ of the template line need
not be equal to the actual number of columns in the alignment:
the template is used only for as many items as are specified
on a line. Consider as an example
\begin{verbatim}
\halign{a#&b#&c#\cr 1&2\cr 1\cr}
\end{verbatim}
which has a three-item template, but the rows have only
one or two items. The output of this is
\begin{disp}\leavevmode\vbox{\halign{a#&b#&c#\cr 1&2\cr 1\cr}}\end{disp}
%\spoint Infinite preambles
\subsection{Infinite preambles}
For the case where the number of columns is not known in advance,
for instance if the alignment is to be used in a macro where
the user will specify the columns, it is possible to
specify that a trailing piece of the
preamble can be repeated arbitrarily many times.
By preceding it with \n\&, an entry can be marked as the
start of this repeatable part of the preamble.
See the example of \cs{matrix} below.
When the whole preamble is to be repeated, there will be
an alignment tab character at the start of the first entry:
\begin{verbatim}
\halign{& ... & ... \cr ... }
\end{verbatim}
If a starting portion of the preamble is to be exempted from
repetition, a double alignment tab will occur:
\begin{verbatim}
\halign{ ... & ... & ... && ... & ... \cr ... }
\end{verbatim}
The repeatable part need not be used an integral
number of times. The alignment rows can end at any time;
the rest of the preamble is then not used.
%\spoint Brace counting in preambles
\subsection{Brace counting in preambles}
Alignments may appear inside alignments, so \TeX\ uses the
following rule to determine to which alignment
an \n\& or \cs{cr} control sequence belongs:
\begin{disp} All tab characters and \cs{cr} tokens of an alignment
should be on the same level of grouping.\end{disp}
From this it follows that tab characters and \cs{cr} tokens
can appear inside an entry if they are nested in braces.
This makes it possible to have nested alignments.
%\spoint Expansion in the preamble
\subsection{Expansion in the preamble}
All tokens in the preamble \ldash apart from the tab characters \rdash
are stored for insertion in the entries of the alignment,
but a token preceded by \csidx{span} is expanded while
the preamble is scanned. See below for the function of
\cs{span} in the rest of the alignment.
%\spoint \cs{tabskip}
\subsection{\cs{tabskip}}
Entries in an alignment are set to take the width of the
largest element in their column.
Glue for separating columns can be specified by assigning
to \csidx{tabskip}.
\altt
\TeX\ inserts this glue in
between each pair of columns, and before the first and after the
last column.
The value of \cs{tabskip} that holds outside the alignment is
used before the first column, and after all subsequent columns,
unless the preamble contains assignments to \cs{tabskip}.
Any assignment to \cs{tabskip} is executed while \TeX\ is scanning
the preamble; the value that holds when a tab character is
reached will be used at that place in each row, and after all subsequent
columns, unless further assignments occur.
The value of \cs{tabskip} that holds when \cs{cr} is reached
is used after the last column.
Assignments to \cs{tabskip} in the preamble are local to the
alignment, but not to the entry where they are given.
These assignments are ordinary glue assignments:
they remove any optional trailing space.
As an example, in the following table there is no tabskip
glue before the first and after the last column;
in between all columns there is stretchable tabskip.
\begin{verbatim}
\tabskip=0pt \halign to \hsize{
\vrule#\tabskip=0pt plus 1fil\strut&
\hfil#\hfil& \vrule#& \hfil#\hfil& \vrule#& \hfil#\hfil&
\tabskip=0pt\vrule#\cr
\noalign{\hrule}
&\multispan5\hfil Just a table\hfil&\cr
\noalign{\hrule}
&one&&two&&three&\cr &a&&b&&c&\cr
\noalign{\hrule}
}
\end{verbatim}
The result of this is
\begin{disp}\PopListLevel
\leavevmode\message{single indent and sufficient vertical}%
\hbox{\leftskip0pt \rightskip0pt
\vbox{\offinterlineskip
\tabskip=0pt \halign to \hsize{\strut
\vrule#\tabskip=0pt plus 1fil\strut&
\hfil#\hfil& \vrule#& \hfil#\hfil&
\vrule#& \hfil#\hfil&
\tabskip=0pt\vrule#\cr
\noalign{\hrule}
&\multispan5\hfil Just a table\hfil&\cr
\noalign{\hrule}
&one&&two&&three&\cr
&a&&b&&c&\cr
\noalign{\hrule}
}}}\end{disp}
All of the vertical rules
of the table are in a separate column. This is the only way
to get the space around the items to stretch.
%\point The alignment
\section{The alignment}
After the template line any number of lines terminated by \cs{cr}
can follow. \TeX\ reads all of these lines, processing the
entries in order to find the maximal width (height) in
each column (row).
Because all entries are kept in memory,
long tables can overflow \TeX's main memory.
For such tables it is better to write a special-purpose macro.
%\spoint Reading an entry
\subsection{Reading an entry}
Entries in an alignment are composed of the
constant $u$ and $v$ parts
of the template, and the variable $\alpha$ part.
Basically \TeX\ forms the sequence of tokens $u\alpha v$
and processes this. However, there are two special cases
where \TeX\ has to expand before it forms this sequence.
Above, the \cs{noalign} command was described.
Since this requires a different treatment from other
alignment entries,
\TeX\ expands, after it has read a \cs{cr},
the first token of the first $\alpha$ string
of the next line to
see whether that is or expands to \cs{noalign}.
Similarly, for all entries
in a line the first token is expanded to see
whether it is or expands to \cs{omit}. This control sequence
will be described below.
Entries starting with an \cs{if...} conditional, or a macro
expanding to one, may be misinterpreted owing to this
premature expansion. For example,
\begin{verbatim}
\halign{$#$\cr \ifmmode a\else b\fi\cr}
\end{verbatim}
will give
\begin{disp}\leavevmode
\vbox{\halign{$#$\cr \ifmmode a\else b\fi\cr}}\end{disp}
because the conditional is evaluated before math mode has been set up.
The solution is, as in many other cases, to insert a
\cs{relax} control sequence to stop the expansion.
Here the \cs{relax} has to be inserted at the start of the
alignment entry.
If neither \cs{noalign} nor \cs{omit} (see below) is found,
\TeX\ will process an input stream composed
of the $u$ part, the $\alpha$ tokens
(which are delimited by either \n\& or \cs{span}, see below),
and the $v$ part.
Entries are delimited by \n\&, \cs{span}, or \cs{cr}, but
only if such a token occurs on the same level of grouping.
This makes it possible to have an alignment as an entry of
another alignment.
%\spoint Alternate specifications: \cs{omit}
\subsection{Alternate specifications: \cs{omit}}
The template line will rarely be sufficient to describe
all lines of the alignment. For lines where items should be
set differently the command \csidx{omit} exists:
if the first token in an entry is (or expands to) \cs{omit}
the trivial template \n\# is used instead of
what the template line specifies.
\begin{example} The following alignment uses the same template for
all columns, but in the second column an \cs{omit} command
is given.
\begin{verbatim}
\tabskip=1em
\halign{&$<#>$\cr a&\omit (b)&c \cr}
\end{verbatim}
The output of this is
\begin{disp}\leavevmode\vbox{\tabskip=1em
\halign{&$<#>$\cr a&\omit (b)&c \cr}}
\end{disp}
\end{example}
%\spoint Spanning across multiple columns: \cs{span}
\subsection{Spanning across multiple columns: \cs{span}}
Sometimes it is desirable to have material spanning several
columns. The most obvious example is that of a heading above
a table. For this \TeX\ provides the \cs{span} command.
Entries are delimited either by \n\&, by \cs{cr}, or by \csidx{span}.
In the last case \TeX\ will omit the tabskip glue that
would normally follow the entry thus delimited, and
it will typeset the material just read plus the following
entry in the joint space available.
As an example,
\begin{verbatim}
\tabskip=1em
\halign{&#\cr a&b&c&d\cr a&\hrulefill\span\hrulefill&d\cr}
\end{verbatim}
gives
\begin{disp}\leavevmode\vbox{\tabskip=1em
\halign{&#\cr a&b&c&d\cr a&\hrulefill\span\hrulefill&d\cr}}
\end{disp} Note that there is no tabskip glue in between the
two spanned columns, but there is tabskip glue before the
\alt
first column and after the last.
Using the \cs{omit} command this same alignment could
have been generated as
\begin{verbatim}
\halign{&#\cr a&b&c&d\cr a&\hrulefill\span\omit&d\cr}
\end{verbatim}
The \cs{span}\cs{omit} combination is used in the
plain \TeX\ macro
\cs{multispan}: for instance
\begin{disp}\cs{multispan4}\quad gives\quad \verb>\omit\span\omit\span\omit\span\omit>
\end{disp} which spans across three tabs, and removes the templates
of four entries.
Repeating the above example once again:
\begin{verbatim}
\halign{&#\cr a&b&c&d\cr a&\multispan2\hrulefill&d\cr}
\end{verbatim}
The argument of \cs{multispan} is a single token,
not a number,
so in order to span more than 9 columns the argument
should be enclosed in braces, for instance \verb>\multispan{12}>.
\alt
Furthermore, a space after a single-digit argument
will wind up in the output.
For a `low budget' solution to spanning columns plain \TeX\ has the
macro \csidx{hidewidth}, defined by
\begin{verbatim}
\newskip\hideskip \hideskip=-1000pt plus 1fill
\def\hidewidth{\hskip\hideskip}
\end{verbatim}
Putting \cs{hidewidth} at the beginning or end of an alignment entry
will make its width zero, with the material in the entry
sticking out to the left or right respectively.
%\spoint Rules in alignments
\subsection{Rules in alignments}
Horizontal rules inside a horizontal alignment will mostly
\term rules in alignments\par
\howto Draw rules in an alignment\par
be across the width of the alignment. The easiest way
to attain this is to use \begin{verbatim}
\noalign{\hrule}
\end{verbatim}
lines inside the alignment. If the alignment is contained
in a vertical box, lines above and below the alignment
can be specified with
\begin{verbatim}
\vbox{\hrule \halign{...} \hrule}
\end{verbatim}
The most general way to get horizontal lines in an alignment
is to use
\cstoidx multispan\par
\begin{disp}\cs{multispan}$\,n$\cs{hrulefill}\end{disp}
which can be used to underline arbitrary adjacent columns.
Vertical rules in alignments take some more care.
Since a horizontal alignment breaks up into
horizontal boxes that will be placed on a vertical list,
\TeX\ will insert baselineskip glue in between the rows
of the alignment. If vertical rules in adjacent rows
are to abut, it is necessary to prevent baselineskip glue,
for instance by the \cs{offinterlineskip} macro.
In order to ensure that rows will still be properly spaced
it is then necessary to place a {\italic strut\/}
somewhere in the preamble.
A~strut is an invisible object with a certain height
and depth. Putting that in the preamble guarantees that
every line will have at least that height and depth.
In the plain format \csidx{strut} is
defined statically as
\begin{verbatim}
\vrule height8.5pt depth3.5pt width0pt
\end{verbatim}
so this must be changed when other fonts or sizes are used.
It is a good idea to use a whole column for a~vertical
rule, that is, to write
\begin{verbatim}
\vrule#&
\end{verbatim}
in the preamble and
to leave the corresponding entry in the alignment empty.
Omitting the vertical rule can then be done by specifying \cs{omit},
and the size of the rule can be specified explicitly by
putting, for instance,
\hbox{\n{height 15pt}} in the entry instead of leaving
it empty. Of course, tabskip glue will now be specified to the
left and right of the rule, so some extra tabskip assignments
may be needed in the preamble.
%\spoint End of a line: \cs{cr} and \cs{crcr}
\subsection{End of a line: \cs{cr} and \cs{crcr}}
All lines in an alignment are terminated by the \csidx{cr} control
sequence, including the last line.
\TeX\ is not able to infer from
a closing brace in the $\alpha$~part that the
alignment has ended, because an unmatched
closing brace is perfectly valid in
an alignment entry; it may match an opening brace in
the $u$~part of the corresponding preamble entry.
\TeX\ has a primitive command \csidx{crcr} that is equivalent
to \cs{cr}, but it has no effect if it immediately follows
a~\cs{cr}.
Consider as an example the definition in plain \TeX\
of \csidx{cases}:
\begin{verbatim}
\def\cases#1{%
\left\{\,\vcenter{\normalbaselines\m@th
\ialign{ $##\hfil$& \quad##\hfil \crcr #1\crcr}}%
\right.}
\end{verbatim}
Because of the \cs{crcr} after the user argument \verb.#1.,
the following two applications of this macro
\begin{disp}\verb>\cases{1&2\cr 3&4}>\quad and\quad \verb>\cases{1&2\cr 3&4\cr}>\end{disp}
both work. In the first case the \cs{crcr} in the macro
definition ends the last line;
in the second case the user's \cs{cr} ends the line,
and the \cs{crcr} is redundant.
After \cs{cr} and after a non-redundant \cs{crcr} the
\gr{token parameter} \csidx{everycr} is inserted.
This includes the \cs{cr} terminating the template line.
%\point Example: math alignments
\section{Example: math alignments}
The plain format has several alignment macros that function
in math mode. One example is \csidx{matrix}, defined by
\begin{verbatim}
\def\matrix#1{\null\,\vcenter{\normalbaselines\m@th
\ialign{\hfil$##$\hfil && \quad\hfil$##$\hfil\crcr
\mathstrut\crcr
\noalign{\kern-\baselineskip}
#1\crcr
\mathstrut\crcr
\noalign{\kern-\baselineskip}}}\,}
\end{verbatim}
This uses a repeating (starting with~\verb>&&>) second preamble entry;
each entry is centred by an \cs{hfil} before and after it,
and there is a \cs{quad} of space in between columns.
Tabskip glue was not used for this, because there should not
be any glue preceding or following the matrix.
The combination of a \cs{mathstrut} and \verb>\kern-\baselineskip>
above and below the matrix increases the vertical size
such that two matrices with the same number of rows will have
the same height and depth, which would not otherwise be the case
if one of them had subscripts in the last row, but the other
not. The \cs{mathstrut} causes interline glue to be inserted
and, because it has a size equal to \cs{baselineskip},
the negative kern will effectively leave only the interline glue,
thereby buffering any differences in the first and last line.
Only to a certain point, of course: objects bigger than the
opening brace will still result in a different height or depth of the
matrix.
Another, more
complicated, example of an alignment for math mode is \cs{eq\-alignno}.
\cstoidx eqalignno\par\cstoidx centering\par
\begin{verbatim}
\def\eqalignno#1{\begin{disp}l@y \tabskip\centering
\halign to\displaywidth{
\hfil$\@lign\displaystyle{##}$% -- first column
\tabskip\z@skip
&$\@lign\displaystyle{{}##}$\hfil% -- second column
\tabskip\centering
&\llap{$\@lign##$}% -- third column
\tabskip\z@skip\crcr % end of the preamble
#1\crcr}}
\end{verbatim}
Firstly, the tabskip is set to zero after the equation
number, so this number is set flush with the right margin.
Since it is placed by \cs{llap}, its effective width
is zero. Secondly, the tabskip between the
first and second columns is also zero, and the tabskip
before the first column and after the second is
\alt
\cs{centering}, which is \n{0pt plus 1000pt minus 1000pt},
so the first column and second are jointly centred
in the \cs{hsize}. Note that, because of the
\n{minus 1000pt}, these two columns will happily go
outside the left and right margins, overwriting any
equation numbers.
% \begin{comment}
% \endinput
% %\spoint Error messages
% \subsection{Error messages}
% \aftergroup in alignment?
% \end{comment}
%%%% end of input file [align]
%\InputFile:page
%%%% this is input file [page]
%\subject[page:shape] Page Shape
\endofchapter
\chapter{Page Shape}\label{page:shape}
This chapter treats some of the parameters that
determine the size of the page and how it appears on paper.
\begin{inventory}
\item [\cs{topskip}]
Minimum distance between the top of the page box
and the baseline of the first box on the page.
Plain \TeX\ default:~\n{10pt}
\item [\cs{hoffset \cs{voffset}}]
\mdqon
Distance by which the page is shifted right/""down
\mdqoff
with respect to the reference point.
\item [\cs{vsize}]
Height of the page box.
Plain \TeX\ default:~\n{8.9in}
\item [\cs{maxdepth}]
Maximum depth of the page box.
Plain \TeX\ default:~\n{4pt}
\item [\cs{splitmaxdepth}]
Maximum depth of a box split off by a \cs{vsplit} operation.
Plain \TeX\ default:~\cs{maxdimen}
\end{inventory}
%\point The reference point for global positioning
\section{The reference point for global positioning}
It is a \TeX\ convention, to which output device drivers
\term page positioning\par
must adhere, that the top left point of the page is
one inch from the page edges. Unfortunately this
may lead to lots of trouble, for instance if a printer
(or the page description language it uses)
takes, say, the {\em lower\/} left corner as the
reference point, and is factory set to US paper sizes,
but is used with European standard A4 paper.
The page is shifted on the paper if one assigns non-zero
values to \csidx{hoffset} or \csidx{voffset}: positive values
shift to the right and down respectively.
%\point \cs{topskip}
\section{\protect\cs{topskip}}
The \csidx{topskip} ensures to a certain point
that the first baseline of a page
will be at the same location from page to page,
even if font sizes
are switched between pages or if the first line has
no ascenders.
Before the first box on each page some glue is inserted.
This glue has the same stretch and shrink as \cs{topskip}, but
the natural size is the natural size of \cs{topskip}
minus the height of the first box, or zero if this
would be negative.
Plain \TeX\ sets \cs{topskip} to {\tt 10pt}.
Thus the top lines of pages will have their baselines
at the same place if
the top portion of the characters is ten point or less.
For the Computer Modern fonts this condition is satisfied
if the font size is less than (about) 13~points;
for larger fonts
the baseline of the top line will drop.
The height of the page box for a page containing only
text (and assuming a zero \cs{parskip})
will be the \cs{topskip} plus a number of times
the \cs{baselineskip}. Thus one can define a macro
to compute the \cs{vsize} from the number of lines
on a page:
\howto Specify page height in lines\par
\begin{verbatim}
\def\HeightInLines#1{\count@=#1\relax
\advance\count@ by -1\relax
\vsize=\baselineskip
\multiply\vsize by \count@
\advance\vsize by \topskip}
\end{verbatim}
Calculating the \cs{vsize} this way will prevent
underfull boxes for text-only pages.
In cases where the page does not start with a line of text
(for instance a rule), the topskip may give unwanted effects.
To prevent these, start the page with
\begin{verbatim}
\hbox{}\kern-\topskip
\end{verbatim}
followed by what you wanted on top.
Analogous to the \cs{topskip}, there is a \cs{splittopskip}
for pages generated by a \cs{vsplit} operation; see
the next chapter.
%\point Page height and depth
\section{Page height and depth}
\TeX\ tries to build pages as a \cs{vbox} of height \csidx{vsize};
\alt
see also \cs{pagegoal} in the next chapter.
If the last item on a page has an excessive depth,
\term page depth\par
that page would be noticeably longer than other pages.
To prevent this phenomenon \TeX\ uses \csidx{maxdepth} as
the maximum depth of the page box. If adding an item to the
page would make the depth exceed this quantity, then the
reference point of the page is moved down to make the depth
exactly \cs{maxdepth}.
The `raggedbottom' effect is obtained in plain \TeX\
\cstoidx raggedbottom\par
by giving the \cs{topskip} some finite stretchability:
\hbox{\n{10pt plus 60pt}}.
Thus the natural height of box~255 can vary when it reaches
the output routine.
Pages are then shipped out (more or less) as
\begin{verbatim}
\dimen0=\dp255 \unvbox255
\ifraggedbottom \kern-\dimen0 \vfil \fi
\end{verbatim}
The \cs{vfil} causes the topskip to be set at natural
width, so the effect is one of a fixed top line and a
variable bottom line of the page.
Before \cs{box255} is unboxed in the plain \TeX\ output routine,
\cs{boxmaxdepth} is set to \cs{maxdepth}
so that this box will made under the same assumptions
that the page builder used when putting together \cs{box255}.
The depth of box split off by a \cs{vsplit} operation
is controlled by the \cs{splitmaxdepth} parameter.
%\subject[page:break] Page Breaking
\endofchapter
\chapter{Page Breaking}\label{page:break}
This chapter treats the `page builder': the part of \TeX\
that decides where to break the main vertical list into pages.
The page builder operates before the output routine,
and it hands its result in \cs{box255} to the output routine.
\begin{inventory}
\item [\cs{vsplit}]
Split of a top part of a box. This is comparable
with page breaking.
\item [\cs{splittopskip}]
Minimum distance between the top of what remains after a
\cs{vsplit} operation, and the first item in that box.
Plain \TeX\ default:~\n{10pt}
\item [\cs{pagegoal}]
Goal height of the page box. This starts at \cs{vsize},
and is diminished by heights of insertion items.
\item [\cs{pagetotal}]
Accumulated natural height of the current page.
\item [\cs{pagedepth}]
Depth of the current page.
\item [\cs{pagestretch}]
Accumulated zeroth-order stretch of the current page.
\item [\cs{pagefilstretch}]
Accumulated first-order stretch of the current page.
\item [\cs{pagefillstretch}]
Accumulated second-order stretch of the current page.
\item [\cs{pagefilllstretch}]
Accumulated third-order stretch of the current page.
\item [\cs{pageshrink}]
Accumulated shrink of the current page.
\item [\cs{outputpenalty}]
Value of the penalty at the current page break,
or $10\,000$ if the break was not at a penalty.
\item [\cs{interlinepenalty}]
Penalty for breaking a page between lines of a paragraph.
Plain \TeX\ default:~\n{0}
\item [\cs{clubpenalty}]
Additional penalty for breaking a page after
the first line of a paragraph.
Plain \TeX\ default:~\n{150}
\item [\cs{widowpenalty}]
Additional penalty for breaking a page before
the last line of a paragraph.
Plain \TeX\ default:~\n{150}
\item [\cs{displaywidowpenalty}]
Additional penalty for breaking a page before the last line
above a display formula.
Plain \TeX\ default:~\n{50}
\item [\cs{brokenpenalty}]
Additional penalty for breaking a page after a hyphenated line.
Plain \TeX\ default:~\n{100}
\item [\cs{penalty}]
Place a penalty on the current list.
\item [\cs{lastpenalty}]
If the last item on the list was a penalty, the value of this.
\item [\cs{unpenalty}]
Remove the last item of the current list if this
was a penalty.
\end{inventory}
%\point The current page and the recent contributions
\section{The current page and the recent contributions}
The main vertical list of \TeX\ is divided in two parts:
\term current page\par\term recent contributions\par
\term page builder\par
the `current page' and the list of `recent contributions'.
Any material that is added to the main vertical list is
appended to the recent contributions; the act of moving
the recent contributions to the current page is known
as `exercising the page builder'.
Every time something is moved to the current page, \TeX\
computes the cost of breaking the page at that point.
If it decides that it is past the optimal point,
the current page up to
\altt
the best break so far
is put in \cs{box255} and the remainder of
the current page is moved back on top of the recent contributions.
If the page is broken at a penalty,
\label{break:penalty}%
that value is recorded in \cs{outputpenalty}, and
a penalty of size $10\,000$ is placed on top of the
recent contributions; otherwise, \csidx{outputpenalty}
is set to~$10\,000$.
If the current page is empty, discardable items that are moved
from the recent contributions are discarded. This is the mechanism
that lets glue disappear after a page break and at the top of
the first page. When the first non-discardable item is moved
to the current page, the \cs{topskip} glue is inserted;
see the previous chapter.
The workings of the page builder can be made visible by
setting \cs{tracingpages} to some positive value
(see Chapter~\ref{trace}).
%\point Activating the page builder
\section{Activating the page builder}
The page builder comes into play in the
following circumstances.
\begin{itemize}\item Around paragraphs: after the \cs{everypar}
tokens have been inserted, and after the paragraph has been
added to the vertical list. See the end of this chapter for
an example.
\item Around display formulas: after the \cs{everydisplay}
tokens have been inserted, and after the display has been
added to the list.
\item After \cs{par} commands, boxes, insertions,
and explicit penalties in vertical mode.
\item After an output routine has ended. \end{itemize}
In these places the page builder moves the recent
contributions to the current page. Note that \TeX\ need not be
in vertical mode when the page builder is exercised.
In horizontal mode, activating the page builder
serves to move preceding vertical glue (for example, \cs{parskip},
\cs{abovedisplayskip}) to the page.
The \cs{end} command \ldash which is only allowed in
external vertical mode \rdash terminates a \TeX\ job, but only if the
main vertical list is empty and \cs{deadcycles}${}=0$.
If this is not the case the combination
\label{end:play}%
\begin{disp}\verb>\hbox{}\vfill\penalty>$-2^{30}$\end{disp}
is appended, which forces the output routine to act.
%\point Page length bookkeeping
\section{Page length bookkeeping}
The height and depth of the page box that reaches the output
\term page length\par
routine are determined by \cs{vsize}, \cs{topskip},
and~\cs{maxdepth} as described in the previous chapter.
\TeX\ places the \cs{topskip} glue
when the first box is placed on the current page; the
\cs{vsize} and \cs{maxdepth} are read when the first
box or insertion occurs on the page. Any subsequent changes to these
parameters will not be noticeable until the next page or,
more strictly, until after the output routine has been called.
After the first box, rule, or insertion on the current page
the \cs{vsize} is recorded in \cs{pagegoal},
and its value is not looked at until \cs{output}
has been active.
Changing \cs{pagegoal} does have an effect on the current
page.
When the page is empty,
the pagegoal is \cs{maxdimen}, and \cs{pagetotal} is zero.
Accumulated dimensions and stretch are available in
the parameters \cs{pagetotal}, \cs{pagedepth},
\cs{pagestretch}, \cs{pagefilstretch}, \cs{pagefillstretch},
\cs{pageshrink},
and \cs{pagefilllstretch}.
\cstoidx pagetotal\par\cstoidx pagedepth\par
\cstoidx pagestretch\par\cstoidx pagefilstretch\par
\cstoidx pagefillstretch\par
\cstoidx pageshrink\par\cstoidx pagefilllstretch\par
They are set by the page builder. The stretch and
shrink parameters are updated every time glue is added
to the page. The depth parameter becomes zero
if the last item was kern or glue.
These parameters are \gr{special dimen}s; an assignment
to any of them is an \gr{intimate assignment},
and it is automatically global.
%\point Breakpoints
\section{Breakpoints}
%\spoint Possible breakpoints
\subsection{Possible breakpoints}
Page breaks can occur at the same kind of locations where
\term breakpoints in vertical lists\par
line breaks can occur:
\begin{itemize}\item at glue that is preceded by a non-discardable
item;\item at a kern that is immediately followed by glue;
\item at a penalty.\end{itemize}
\TeX\ inserts interline glue and various sorts of
interline penalties when the lines of a paragraph are
added to the vertical list, so there will usually be
sufficient breakpoints on the page.
%\spoint Breakpoint penalties
\subsection{Breakpoint penalties}
If \TeX\ decides to break a page at a penalty item, this
penalty will, most of the time, be one that
has been inserted automatically
between the lines of a paragraph.
If the last item on a list (not necessarily a vertical list)
\alt
is a penalty, the value of this is recorded
in the parameter \csidx{lastpenalty}. If the item is other than
a penalty, this parameter has the value zero.
The last penalty of a list can be removed with the command
\csidx{unpenalty}. See Section~\ref{varioset} for an example.
\message{Spoint ref varioset}
Here is a list of such penalties\term penalties in vertical mode\par:
\begin{inventory}
\item [\csidx{interlinepenalty}]
Penalty for breaking a page between lines of a paragraph.
In plain \TeX\ this is zero, so no penalty is added in
between lines. \TeX\ can then find a valid breakpoint at the
\cs{baselineskip} glue.
\item [\csidx{clubpenalty}]
Extra penalty for breaking a page after the first line of a paragraph.
In plain \TeX\ this is~\n{150}.
This amount, and the following penalties, are
added to the \cs{interlinepenalty}, and
a penalty of the resulting size is inserted after the
\cs{hbox} containing the first line of a paragraph
instead of the \cs{interlinepenalty}.
\item [\csidx{widowpenalty}]
Extra penalty for breaking a page before the last line of a paragraph.
In plain \TeX\ this is~\n{150}.
\item [\csidx{displaywidowpenalty}]
Extra penalty for breaking a page before the last line
above a display formula. The default value in plain \TeX\
is~\n{50}.
\item [\csidx{brokenpenalty}]
Extra penalty for breaking a page after a hyphenated line.
The default value in plain \TeX\ is~\n{100}.
\end{inventory}
If the resulting penalty is zero, it is not placed.
Penalties can also be inserted by the user. For instance,
the plain format has macros to encourage (possibly, force)
or prohibit page breaks\cstoidx penalty\par:
\begin{verbatim}
\def\break{\penalty-10000 } % force break
\def\nobreak{\penalty10000 } % prohibit break
\def\goodbreak{\par\penalty-500 } % encourage page break
\end{verbatim}
Also, \verb>\vadjust{\penalty ... }> is a way of getting
penalties in the vertical list. This can be used to
discourage or encourage page breaking after a certain
line of a paragraph.
%\spoint Breakpoint computation
\subsection{Breakpoint computation}
\advance\rightskip by 5.5cm
Whenever an item is moved to the current page, \TeX\
\term page breaking\par\term breakpoints, computation of\par
\vadjust{\advance\hsize by -5.5cm
\hbox to \hsize{\hfil\rlap{\hskip.4cm\vtop to 0pt
{\kern-2\baselineskip
\SansSerif %\pointSize:8 \Style:roman
\parindent0pt \offinterlineskip
\def\tbox#1{\hbox{\quad\quad #1%
\vrule height 10pt depth3pt width0cm }}
\hbox
{\vrule width\lw \kern-\lw
\vbox{\hsize=5cm
\hrule height\lw \ \vskip0cm
\kern40pt
\tbox{underfull page}
\tbox{$b=10\,000$}
\kern40pt
\hrule height\lw
\kern8pt
\tbox{feasible breakpoints}
\tbox{$b<10\,000$}
\kern8pt
\hrule height\lw
\kern8pt
\tbox{overfull page}
\tbox{$b=\infty$}
\kern3pt
\tbox{.\vrule height3.5pt depth1pt width0cm}
\tbox{.\vrule height3.5pt depth1pt width0cm}
\tbox{.\vrule height3.5pt depth1pt width0cm}
\kern8pt
}%
\kern-\lw \vrule width\lw}%
\vss}}}}
computes the penalty $p$ and the badness $b$ associated with
breaking the page at that place. From the penalty and
the badness the cost $c$ of breaking is computed.
The place of least cost is remembered, and when
the cost is infinite, that is, the page is overfull, or
when the penalty is $p\leq-10\,000$, the current page is broken
at the (last remembered) place of least cost.
The broken-off piece is then
put in \cs{box255} and the output routine token list
is inserted. Box 255 is always given a height of \cs{vsize},
regardless of how much material it has.
The badness calculation is based on the amount of stretching
or shrinking that is necessary to fit the page in
a box with height \cs{vsize}
and maximum depth \cs{maxdepth}. This calculation is
the same as for line breaking (see Chapter~\ref{glue}).
Badness is a value $0\leq b\leq 10\,000$, except when
pages are overfull; then~$b=\infty$.
\advance\rightskip by -5.5cm
Some penalties are implicitly inserted by \TeX,
for instance the \cs{interlinepenalty}
which is put in between every pair of lines of a paragraph.
Other penalties can
be explicitly inserted by the user or a user macro.
A~penalty
value $p\geq10\,000$ inhibits breaking; a penalty
$p\leq-10\,000$ (in external vertical mode)
\alt
forces a page break, and immediately
activates the output routine.
Cost calculation proceeds as follows:
\begin{enumerate} \item When a penalty is so low that it forces
a page break and immediate invocation of the output routine,
but the page is not overfull, that is
\begin{disp}$b<\infty\quad\hbox{and}\quad p\leq-10\,000$\end{disp}
the cost is equal to the penalty:~$c=p$.
\item When penalties do not force anything, and the page is not
overfull, that is
\begin{disp}$b<\infty\quad\hbox{and}\quad |p|<10\,000$\end{disp}
the cost is~$c=b+p$.
\item For pages that are very bad, that is
\begin{disp}$b=10\,000\quad\hbox{and}\quad |p|<10\,000$\end{disp}
the cost is~$c=10\,000$.
\item An overfull page, that is
\begin{disp}$b=\infty\quad\hbox{and}\quad p<10\,000$\end{disp}
gives infinite cost:~$c=\infty$.
In this case \TeX\ decides that the optimal break point
must have occurred earlier, and it invokes the output routine.
Values of \cs{insertpenalties} (see Chapter~\ref{insert})
that exceed $10\,000$
also give infinite cost.
\end{enumerate}
The fact that a penalty $p\leq-10\,000$ activates
the output routine is used extensively
in the \LaTeX\ output routine:
the excess $\mathopen|p\mathclose|-10\,000$ is
a code indicating the reason for calling the output routine;
see also the second example in the next chapter.
%\point[vsplit] \cs{vsplit}
\section{\protect\cs{vsplit}}
\label{vsplit}
The page-breaking operation is available to the user
through the \csidx{vsplit} operation.
\begin{example} \begin{verbatim}
\setbox1 = \vsplit2 to \dimen3
\end{verbatim}
assigns to box~1 the top part of size \cs{dimen3}
of box~2. This material is actually removed from box~2.
Compare this with splitting off a chunk of size \cs{vsize}
from the current page.
\end{example}
The extracted
result of \begin{disp}\cs{vsplit}\gr{8-bit number}\n{to}\gr{dimen}
\end{disp} is a box with the following properties.
\begin{itemize} \item Height equal to the specified \gr{dimen}; \TeX\ will
go through the original box register (which must contain
a vertical box) to find the best breakpoint. This may
result in an underfull box.
\item Depth at most \csidx{splitmaxdepth}; this is analogous to
the \cs{maxdepth} for the page box, rather than the \cs{boxmaxdepth}
that holds for any box.
\item A first and last mark in the \cs{splitfirstmark} and
\cs{splitbotmark} registers.
\end{itemize}
The remainder of the \cs{vsplit} operation is a box where
\begin{itemize} \item all discardables have been removed
from the top;
\item glue of size \csidx{splittopskip} has been inserted on top;
if the box being split was box~255, it
already had \cs{topskip} glue on top;
\item its depth has been forced to be at most \cs{splitmaxdepth}.
\end{itemize}
The bottom of the original box is always a valid breakpoint
for the \cs{vsplit} operation. If this breakpoint is taken,
the remainder box register is void. The extracted box
can be empty; it is only void if the original box
was void, or not a vertical box.
Typically, the \cs{vsplit} operation is used to split off part
of \cs{box255}. By setting \cs{splitmaxdepth} equal to \cs{boxmaxdepth}
the result is something that could have been made by \TeX's page
builder. After pruning the top of \cs{box255}, the
mark registers \cs{firstmark} and \cs{botmark} contain the first
and last marks on the remainder of box~255.
See the next chapter for more information on marks.
%\point Examples of page breaking
\section{Examples of page breaking}
%\spoint Filling up a page
\subsection{Filling up a page}
Suppose a certain vertical box is too large
to fit on the remainder of the page.
Then \begin{verbatim}
\vfil\vbox{ ... }
\end{verbatim}
is the wrong way
to fill up the page and push the box to the next.
\TeX\ can only break at the start of the glue, and
the \cs{vfil} is discarded after the break: the result
is an underfull, or at least horribly stretched, page.
On the other hand,
\begin{verbatim}
\vfil\penalty0 % or any other value
\vbox{ ... }
\end{verbatim}
is the correct way: \TeX\ will break
at the penalty, and the page will be filled.
%\spoint Determining the breakpoint
\subsection{Determining the breakpoint}
In the following examples the \cs{vsplit} operation is
used, which has the same
mechanism as page breaking.
Let the macros and
parameter settings
\begin{verbatim}
\offinterlineskip \showboxdepth=1
\def\High{\hbox{\vrule height5pt}}
\def\HighAndDeep{\hbox{\vrule height2.5pt depth2.5pt}}
\end{verbatim}
be given.
First let us consider
an example where a vertical list is simply stretched
in order to reach a break point.
\begin{verbatim}
\splitmaxdepth=4pt
\setbox1=\vbox{\High \vfil \HighAndDeep}
\setbox2=\vsplit1 to 9pt
\end{verbatim}
gives \begin{verbatim}
> \box2=
\vbox(9.0+2.5)x0.4, glue set 1.5fil
.\hbox(5.0+0.0)x0.4 []
.\glue 0.0 plus 1.0fil
.\glue(\lineskip) 0.0
.\hbox(2.5+2.5)x0.4 []
\end{verbatim}
The two boxes together have a height of \n{7.5pt},
so the glue has to stretch~\n{1.5pt}.
Next, we decrease the allowed depth of the resulting list.
\begin{verbatim}
\splitmaxdepth=2pt
\setbox1=\vbox{\High \vfil \HighAndDeep}
\setbox2=\vsplit1 to 9pt
\end{verbatim}
gives
\begin{verbatim}
> \box2=
\vbox(9.0+2.0)x0.4, glue set 1.0fil
.\hbox(5.0+0.0)x0.4 []
.\glue 0.0 plus 1.0fil
.\glue(\lineskip) 0.0
.\hbox(2.5+2.5)x0.4 []
\end{verbatim}
The reference point is moved down half a point,
and the stretch is correspondingly diminished,
\alt
but this motion cannot lead to a larger dimension
than was specified.
As an example of this,
\alt
consider the sequence \begin{verbatim}
\splitmaxdepth=3pt
\setbox1=\vbox{\High \kern1.5pt \HighAndDeep}
\setbox2=\vsplit1 to 9pt
\end{verbatim}
This gives a box exactly 9 points high and 2.5 points deep.
Setting \verb>\splitmaxdepth=2pt> does not increase
the height by half a point; instead, an underfull box
results because an earlier break is taken.
Sometimes the timing of actions is important.
\TeX\ first locates a breakpoint that will lead
to the requested height, then checks whether accommodating
the \cs{maxdepth} or \cs{splitmaxdepth} will not
violate that height.
Consider an example of this timing:
\alt
in
\begin{verbatim}
\splitmaxdepth=4pt
\setbox1=\vbox{\High \vfil \HighAndDeep}
\setbox2=\vsplit1 to 7pt
\end{verbatim}
the result is {\italic not\/} a box
of 7 points high and 3 points deep. Instead,
\begin{verbatim}
> \box2=
\vbox(7.0+0.0)x0.4
.\hbox(5.0+0.0)x0.4 []
\end{verbatim}
which is an underfull box.
%\spoint[par:page:build] The page builder after a paragraph
\subsection{The page builder after a paragraph}
\label{par:page:build}
After a paragraph, the page builder moves material
to the current page, but it does not decide whether a breakpoint
has been found yet.
\begin{example}\begin{verbatim}
\output{\interrupt \plainoutput}% show when you're active
\def\nl{\hfil\break}\vsize=22pt % make pages of two lines
a\nl b\nl c\par \showlists % make a 3-line paragraph
\end{verbatim}
will report
\begin{verbatim}
### current page:
[...]
total height 34.0
goal height 22.0
prevdepth 0.0, prevgraf 3 lines
\end{verbatim}
Even though more than enough
material has been gathered, \cs{output} is only invoked
when the next paragraph starts: typing a \n d gives
\begin{verbatim}
! Undefined control sequence.
<output> {\interrupt
\plainoutput }
<to be read again>
d
\end{verbatim}
when \cs{output} is inserted after \cs{everypar}.
\end{example}
%\subject[output] Output Routines
\endofchapter
\chapter{Output Routines}\label{output}
The final stages of page processing are performed by the
output routine. The page builder cuts off a certain portion
of the main vertical list and hands it to the output routine
in \cs{box255}. This chapter treats the commands and parameters
that pertain to the output routine, and it explains how
output routines can receive information through marks.
\begin{inventory}
\item [\cs{output}]
Token list with instructions for shipping out pages.
\item [\cs{shipout}]
Ship a box to the \n{dvi} file.
\item [\cs{mark}]
Specify a mark text.
\item [\cs{topmark}]
The last mark on the previous page.
\item [\cs{botmark}]
The last mark on the current page.
\item [\cs{firstmark}]
The first mark on the current page.
\item [\cs{splitbotmark}]
The last mark on a split-off page.
\item [\cs{splitfirstmark}]
The first mark on a split-off page.
\item [\cs{deadcycles}]
Counter that keeps track of how many times
the output routine has been called without a \cs{shipout}
taking place.
\item [\cs{maxdeadcycles}]
The maximum number of times that the output routine is allowed to
be called without a \cs{shipout} occurring.
\item [\cs{outputpenalty}]
Value of the penalty at the current page break,
\alt
or $10\,000$ if the break was not at a penalty.
\end{inventory}
%\point The \cs{output} token list
\section{The \protect\cs{output} token list}
Common parlance has it that
`the output routine is called' when \TeX\ has found a place
to break the main vertical list.
Actually, \cs{output} is not a macro but a token list that
is inserted into \TeX's command stream.
Insertion of the \cs{output} token list happens
\cstoidx output\par\term output routine\par
inside a group that is implicitly opened.
Also, \TeX\ enters internal vertical mode.
Because of the group, non-local assignments
(to the page number, for instance)
have to be prefixed with \cs{global}.
The vertical mode implies that during the workings of the
output routine
spaces are mostly harmless.
The \cs{output} token list belongs
to the class of the
\gr{token parameter}s. These behave the same as
\cs{toks}$nnn$ token lists; see Chapter~\ref{token}.
Assigning an output routine can therefore take the following
forms:
\begin{disp}\cs{output}\gr{equals}\gr{general text}\quad
or\quad
\cs{output}\gr{equals}\gr{filler}\gr{token variable}
\end{disp}
%\point[output255] Output and \cs{box255}
\section{Output and \protect\cs{box255}}
\label{output255}
\TeX's page builder breaks the current page at the optimal point,
and stores everything above that in \cs{box255};
then, the \cs{output} tokens are inserted into the input stream.
Any remaining material on the main vertical list
is pushed back to the recent
contributions.
If the page is broken at a penalty,
\alt
that value is recorded in \cs{outputpenalty}, and
a penalty of size $10\,000$ is placed on top of the
recent contributions; otherwise, \cs{outputpenalty}
is set to~$10\,000$.
When the output routine is finished, \cs{box255} is
supposed to be empty.
If it is not, \TeX\ gives an error message.
Usually, the output routine will take the pagebox,
\cstoidx shipout\par
\mdqon
append a headline and/""or footline,
\mdqoff
maybe merge in some insertions such as footnotes,
and ship the page to the \n{dvi} file:
\begin{verbatim}
\output={\setbox255=\vbox
{\someheadline
\vbox to \vsize{\unvbox255 \unvbox\footins}
\somefootline}
\shipout\box255}
\end{verbatim}
When box 255 reaches the output routine, its height has
been set to \cs{vsize}.
However, the material in it can have considerably
smaller height.
Thus, the above output routine may lead to underfull boxes.
This can be remedied with a \cs{vfil}.
The output routine is under no obligation to
\cstoidx deadcycles\par
do anything useful with \cs{box255}; it can empty it, or
unbox it to let \TeX\ have another go at finding a page
break. The number of times
that the output routing postpones the \cs{shipout}
is recorded in \cs{deadcycles}: this parameter is set to~0
by \cs{shipout}, and increased by~1 just before
every \cs{output}.
When the number of dead cycles reaches
\csidx{maxdeadcycles}, \TeX\ gives an error message,
and performs the default output routine
\begin{verbatim}
\shipout\box255
\end{verbatim}
instead of the routine it was about
to start.
The \LaTeX\ format has a much higher value for \cs{maxdeadcycles}
than plain \TeX, because the output routine in \LaTeX\
is often called for
intermediate handling of floats and marginal notes.
The \cs{shipout} command can send any \gr{box} to the \n{dvi} file;
this need not be box 255, or even a box
containing the current page.
It does not have to be called inside the output routine, either.
If the output routine produces any material, for instance
by calling \begin{verbatim}
\unvbox255
\end{verbatim}
this is put on top
of the recent contributions.
After the output routine finishes, the page builder is
activated. In particular, because the current page
has been emptied, the \cs{vsize} is read again.
Changes made to this parameter inside the output
routine (using \cs{global}) will therefore take effect.
%\point Marks
\section{Marks}
Information can be passed to the output routine through the
\term marks\par\cstoidx mark\par
mechanism of `marks'. The user can specify a token list
with \begin{disp}\cs{mark}\lb\gr{mark text}\rb\end{disp}
which is put in a mark item on the current vertical list.
The mark text is subject to expansion as in \cs{edef}.
If the mark is given in horizontal mode it migrates to
the surrounding vertical lists like an insertion item
(see page~\pageref{migrate});
however, if this is not the external vertical list, the
output routine will not find the mark.
Marks are the main mechanism through which the output routine
can obtain information about the contents of the currently
broken-off page, in particular its top and bottom.
\TeX\ sets three variables:
\begin{description}
\item [\csidx{botmark}]
the last mark occurring on the current page;
\item [\csidx{firstmark}]
the first mark occurring on the current page;
\item [\csidx{topmark}]
the last mark of the previous page,
that is, the value of \cs{botmark}
on the previous page.
\end{description}
If no marks have occurred yet, all three are empty;
if no marks occurred on the current page,
all three mark variables are equal
to the \cs{botmark} of the previous page.
For boxes generated by a \cs{vsplit} command (see previous chapter),
the \cs{splitbotmark} and \cs{splitfirstmark}
\cstoidx splitbotmark\par\cstoidx splitfirstmark\par
contain the marks of the split-off part; \cs{firstmark}
and \cs{botmark} reflect the state of what remains in the register.
\begin{example} Marks can be used to get a section heading into
\howto Do tricks with headlines\par
the headline or footline of the page.
\begin{verbatim}
\def\section#1{ ... \mark{#1} ... }
\def\rightheadline{\hbox to \hsize
{\headlinefont \botmark\hfil\pagenumber}}
\def\leftheadline{\hbox to \hsize
{\headlinefont \pagenumber\hfil\firstmark}}
\end{verbatim}
This places the title of the first section that starts on a
left page in the left headline, and the title of the last section
that starts on the right page in the right headline.
Placing the headlines on the page is the job of the output routine;
see below.
It is important that no page breaks can occur in between the
mark and the box that places the title:
\begin{verbatim}
\def\section#1{ ...
\penalty\beforesectionpenalty
\mark{#1}
\hbox{ ... #1 ...}
\nobreak
\vskip\aftersectionskip
\noindent}
\end{verbatim}
\end{example}
Let us consider
another example with headlines: often a page looks better if
the headline is omitted on pages where a chapter starts.
This can be implemented as follows:
\begin{verbatim}
\def\endofchapter
\chapter#1{ ... \def\chtitle{#1}\mark{1}\mark{0} ... }
\def\theheadline{\expandafter\ifx\firstmark1
\else \chapheadline \fi}
\end{verbatim}
Only on the page where a chapter starts will the mark be~1,
and on all other pages a headline is placed.
%\point Assorted remarks
\section{Assorted remarks}
%\spoint Hazards in non-trivial output routines
\subsection{Hazards in non-trivial output routines}
If the final call to the output routine does not
perform a \cs{shipout}, \TeX\ will call the output
routine endlessly, since a run will only stop if both
the vertical list is empty, and \cs{deadcycles}
is zero. The output routine can set \cs{deadcycles}
to zero to prevent this.
%\spoint Page numbering
\subsection{Page numbering}
The page number is not an intrinsic property of the output
\term page numbering\par
routine; in plain \TeX\ it is the value of \cs{count0}.
The output routine is responsible for increasing the
page number when a shipout of a page occurs.
Apart from \cs{count0}, counter registers~1--9 are also used
for page identification: at shipout \TeX\ writes the values
of these ten counters to the \n{dvi} file (see Chapter~\ref{TeXcomm}).
Terminal and log file output display only the non-zero counters,
and the zero counters for which a non-zero counter with
a higher number exists, that is, if \cs{count0}${}=1$ and
\cs{count3}${}=5$ are the only non-zero counters, the
displayed list of counters is~\n{[1.0.0.5]}.
%\spoint Headlines and footlines in plain \TeX\
\subsection{Headlines and footlines in plain \TeX}
Plain \TeX\ has token lists \cs{headline} and
\cs{footline}; these are used in the macros
\cs{makeheadline} and \cs{makefootline}.
The page is shipped out as (more or less)
\begin{verbatim}
\vbox{\makeheadline\pagebody\makefootline}
\end{verbatim}
Both headline and footline are inserted inside a \cs{line}.
For non-standard headers and footers it is easier to
redefine the macros \cs{makeheadline} and \cs{makefootline}
than to tinker with the token lists.
%\spoint Example: no widow lines
\subsection{Example: no widow lines}
Suppose that one does not want to allow widow lines,
but pages have in general no stretch or shrink,
for instance because they only contain plain text.
A~solution would be to increase the page length
by one line if a page turns out to be broken
at a widow line.
\TeX's output routine can perform this sort of
trick: if the \cs{widowpenalty} is set to
some recognizable value, the output routine
can see by the \cs{outputpenalty} if a widow
line occurred. In that case, the output routine
can temporarily increase the \cs{vsize}, and
let the page builder have another go at
finding a break point.
Here is the skeleton of such an output routine.
No headers or footers are provided for.
\begin{verbatim}
\newif\ifLargePage \widowpenalty=147
\newdimen\oldvsize \oldvsize=\vsize
\output={
\ifLargePage \shipout\box255
\global\LargePagefalse
\global\vsize=\oldvsize
\else \ifnum \outputpenalty=\widowpenalty
\global\LargePagetrue
\global\advance\vsize\baselineskip
\unvbox255 \penalty\outputpenalty
\else \shipout\box255
\fi \fi}
\end{verbatim}
The test \cs{ifLargePage} is set to true by the
output routine if the \cs{outputpenalty}
equals the \cs{widowpenalty}. The page box
is then \cs{unvbox}$\,$ed, so that the page builder
will tackle the same material once more.
%\spoint Example: no indentation top of page
\subsection{Example: no indentation top of page}
Some output routines can be classified
\howto Prevent indentation on top of page\par
as abuse of the output routine mechanism.
The output routine in this section is a good example of this.
It is imaginable that one wishes paragraphs not to indent
if they start at the top of a page. (There are plenty of objections
to this layout, but occasionally it is used.)
This problem can be solved using the output routine to
investigate whether the page is still empty and, if so,
to give a signal that a paragraph should not indent.
Note that we cannot use the fact here
that the page builder comes into play after
the insertion of \cs{everypar}: even if we could
force the output routine to be activated here,
there is no way for it to remove the indentation box.
The solution given here lets the \cs{everypar}
terminate the paragraph immediately
with \begin{verbatim}
\par\penalty-\specialpenalty
\end{verbatim}
which activates the output routine.
Seeing whether the pagebox is empty (after removing
the empty line and any \cs{parskip} glue),
the output routine then can set a switch
signalling whether the retry of the paragraph
should indent.
There are some minor matters in the following
routines, the sense of which is left
for the reader to ponder.
\begin{verbatim}
\mathchardef\specialpenalty=10001
\newif\ifPreventSwitch
\newbox\testbox
\topskip=10pt
\everypar{\begingroup \par
\penalty-\specialpenalty
\everypar{\endgroup}\parskip0pt
\ifPreventSwitch \noindent \else \indent \fi
\global\PreventSwitchfalse
}
\output{
\ifnum\outputpenalty=-\specialpenalty
\setbox\testbox\vbox{\unvbox255
{\setbox0=\lastbox}\unskip}
\ifdim\ht\testbox=0pt \global\PreventSwitchtrue
\else \topskip=0pt \unvbox\testbox \fi
\else \shipout\box255 \global\advance\pageno1 \fi}
\end{verbatim}
%\spoint More examples of output routines
\subsection{More examples of output routines}
A large number of examples of output routines
can be found in~\cite{Sal1} and~\cite{Sal2}.
%\subject[insert] Insertions
\endofchapter
\chapter{Insertions}\label{insert}
Insertions are \TeX's way of handling floating information.
\TeX's page builder calculates what insertions and how many
of them will fit on the page; these insertion items are then
placed in insertion boxes which are to be handled by the
output routine.
\begin{inventory}
\item [\cs{insert}]
Start an insertion item.
\item [\cs{newinsert}]
Allocate a new insertion class.
\item [\cs{insertpenalties}]
Total of penalties for split insertions.
Inside the output routine, the number of held-over insertions.
\item [\cs{floatingpenalty}]
Penalty added when an insertion is split.
\item [\cs{holdinginserts}]
(\TeX3 only)
If this is positive, insertions are not placed in their boxes
at output time.
\item [\cs{footins}]
Number of the footnote insertion class in plain \TeX.
\item [\cs{topins}]
Number of the top insertion class.
\item [\cs{topinsert}]
Plain \TeX\ macro to start a top insert.
\item [\cs{pageinsert}]
Plain \TeX\ macro to start an insert that will take
up a whole page.
\item [\cs{midinsert}]
Plain \TeX\ macro that places its argument if there is space,
and converts it into a top insert otherwise.
\item [\cs{endinsert}]
Plain \TeX\ macro to wind up an insertion item
that started with \cs{topinsert}, \cs{midinsert},
or \cs{pageinsert}.
\end{inventory}
%\point Insertion items
\section{Insertion items}
Insertions contain floating information.
\term insertions\par
Handling insertions is a strange interplay between the
user, \TeX's internal workings, and the output routine.
First the user specifies an insertion, which is
a certain amount of vertical material;
then \TeX's page builder decides what insertions should go
on the current page and puts these insertions in insertion boxes;
finally, the output routine has to do something with these boxes.
An insertion item looks like
\cstoidx insert\par
\begin{disp}\cs{insert}\gr{8-bit number}\lb\gr{vertical mode material}\rb
\end{disp} where the 8-bit number should not be~255,
because \cs{box255} is used by \TeX\ for passing the page to the output
routine.
The braces around the vertical mode material in an insertion
item can be implicit; they imply a new level of grouping.
The vertical mode material is processed in internal
vertical mode.
Values of \cs{splittopskip}, \cs{splitmaxdepth},
and \cs{floatingpenalty} are relevant for split insertions
(see below); the values that are current just before
the end of the group are used.
Insertion items can appear in vertical mode, horizontal
mode, and math mode. For the latter two modes they have to
migrate to the surrounding vertical list
(see page~\pageref{migrate}).
After an insertion item is put on the vertical list the
page builder is exercised.
%\point Insertion class declaration
\section{Insertion class declaration}
In the plain format
the number for a new insertion class
is allocated by \csidx{newinsert}:
\begin{verbatim}
\newinsert\myinsert % new insertion class
\end{verbatim}
which uses \cs{chardef} to assign a number to the control
sequence.
Insertion classes are allocated numbering from 254 downward.
As box~255 is used for output, this allocation scheme leaves
\cs{skip255}, \cs{dimen255}, and \cs{count255}
free for scratch use.
%\point Insertion parameters
\section{Insertion parameters}
For each insertion class~$n$ four registers are allocated:
\begin{itemize}
\item \cs{box}$\,n$ When the output routine is active this
box contains the insertion items of class~$n$ that should
be placed on the current page.
\item \cs{dimen}$\,n$ This is the maximum space allotted for
insertions of class~$n$ per page. If this amount would
be exceeded \TeX\ will split insertions.
\item \cs{skip}$\,n$ Glue of this size is added the first
time an insertion item of class~$n$ is added to the
current page. This is useful for such phenomena as a rule
separating the footnotes from the text of the page.
\item \cs{count}$\,n$ Each insertion item is a vertical list,
so it has a certain height. However, the effective height,
the amount of influence it has on the text height of the
page, may differ from this real height.
The value of \cs{count}$\,n$
is then 1000 times the factor by which the height should
be multiplied to obtain the effective height.
Consider the following examples:
\begin{itemize}\item Marginal notes do not affect
the text height, so the factor should be~0. \item Footnotes
set in double column mode affect the page by half of their height:
the count value should by~500. \item Conversely, footnotes
set at page width underneath a page in double column mode
affect both columns, so \ldash provided that the double column mode
is implemented by applying \cs{vsplit} to a double-height column \rdash
the count value should be~2000.\end{itemize}
\end{itemize}
%\point Moving insertion items from the contributions list
\section{Moving insertion items from the contributions list}
The most complicated issue with insertions is the algorithm
that adds insertion items to the main vertical list,
and calculates breakpoints if necessary.
\TeX\ never changes the \cs{vsize}, but it diminishes the
\csidx{pagegoal} by the (effective) heights of the insertion
items that will appear before a page break. Thus the output
routine will receive a \cs{box255} that has height \cs{pagegoal},
not necessarily \cs{vsize}.
\begin{enumerate}
\item When the first insertion of a certain class $n$ occurs
on the current page \TeX\ has to account for the quantity
\cs{skip}$\,n$. This step is executed only if no earlier
insertion item of this class occurs on the vertical list
\ldash this includes insertions that were split \rdash but \cs{box}$\,n$
need not be empty at this time.
If \cs{box}$\,n$ is not empty, its height plus depth is multiplied
by \cs{count}$\,n/1000$ and the result is subtracted
from \cs{pagegoal}. Then the \cs{pagegoal} is diminished
by the natural component of \cs{skip}$\,n$. Any stretch and
shrink of \cs{skip}$\,n$ are incorporated in \cs{pagestretch}
and \cs{pageshrink} respectively.
\item If there is a split insertion of class $n$ on the page
\ldash this case and the previous step in the algorithm are
mutually exclusive \rdash the \csidx{floatingpenalty} is added to
\csidx{insertpenalties}. A~split insertion is an insertion item
for which a breakpoint has been calculated as it will not
fit on the current page in its entirety. Thus the insertion
currently under consideration will certainly not wind up
on the current page.
\item After the preliminary action of the two previous points
\TeX\ will place the actual insertion item on the main vertical
list, at the end of the current contributions.
First it will check whether the item will fit without being split.
There are two conditions to be checked:\begin{itemize}\item
adding the insertion item (plus all previous insertions of that class)
to \cs{box}$\,n$ should not let
the height plus depth of that box exceed \cs{dimen}$\,n$, and
\item either the effective height of the insertion is negative, or
\cs{pagetotal} plus \cs{pagedepth} minus \cs{pageshrink}
plus the effective size of the insertion should be less than
\cs{pagegoal}.\end{itemize}
If these conditions are satisfied, \cs{pagegoal} is diminished
by the effective size of the insertion item, that is,
by the height plus depth, multiplied by \cs{count}$n/1000$.
\item Insertions that fail on one of the two conditions in the
previous step of the algorithm will be considered for splitting.
\TeX\ will calculate the size of the maximal portion to
be split off the insertion item, such that
\begin{enumerate}\item adding this portion
together with earlier insertions of this class to \cs{box}$\,n$
will not let the size of the box exceed \cs{dimen}$\,n$,
and \item the effective size of this portion,
added to \cs{pagetotal} plus \cs{pagedepth}, will not
exceed \cs{pagegoal}. Note that \cs{pageshrink} is not taken
into account this time, as it was in the previous step.
\end{enumerate}
Once this maximal size to be split off has been determined,
\TeX\ locates the least-cost breakpoint in the current
insertion item that will result in a box with a height
that is equal to this maximal size. The penalty associated
with this breakpoint is added to \cs{insertpenalties},
and \cs{pagegoal} is diminished by the effective height plus
depth of the box to be split off the insertion item.
\end{enumerate}
%\point Insertions in the output routine
\section{Insertions in the output routine}
When the output routine comes into action \ldash more precisely:
when \TeX\ starts processing the tokens in the \cs{output}
token list \rdash all insertions that should be placed on the
current page have been put in their boxes, and
it is the responsibility of the output routine
to put them somewhere in the box that is going to be shipped out.
\begin{example} The plain \TeX\ output routine
handles top inserts and footnotes by packaging the following
sequence:
\begin{verbatim}
\ifvoid\topins \else \unvbox\topins \fi
\pagebody
\ifvoid\footins \else \unvbox\footins \fi
\end{verbatim}
Unboxing the insertion boxes makes the glue on various parts
of the page stretch or shrink in a uniform manner.
\end{example}
With \TeX3 the insertion mechanism has been extended slightly:
\cstoidx holdinginserts\par\term \TeX\ version 3\par
the parameter \cs{holdinginserts} can be used to specify that
insertions should not yet be placed in their boxes.
This is very useful if the output routine wants to
recalculate the \cs{vsize}, or if the output routine
is called to do other intermediate calculations instead of
ejecting a page.
During the output routine the parameter
\csidx{insertpenalties} holds the number of insertion items that
are being held over for the next page.
In the plain \TeX\ output routine this is used after the
last page:\begin{verbatim}
\def\dosupereject{\ifnum\insertpenalties>0
% something is being held over
\line{}\kern-\topskip\nobreak\vfill\supereject\fi}
\end{verbatim}
%\point Plain \TeX\ insertions
\section{Plain \TeX\ insertions}
The plain \TeX\ format has only two insertion classes:
the footnotes and the top inserts.
The macro \csidx{pageinsert} generates
top inserts that are stretched to be exactly \cs{vsize} high.
The \csidx{midinsert} macro tests whether the vertical material
specified by the user fits on the page; if so, it is placed
there; if not, it is converted to a top insert.
Footnotes are allowed to be split, but once one has been
split no further footnotes should appear on the current
page. This effect is attained by setting
\begin{verbatim}
\floatingpenalty=20000
\end{verbatim}
The \cs{floatingpenalty} is added to \cs{insertpenalties}
if an insertion follows a split insertion of the same
class. However, \cs{floatingpenalty}${}>10\,000$ has infinite
cost, so \TeX\ will take an earlier breakpoint for
splitting off the page from the vertical list.
Top inserts essentially contain only a vertical box
which holds whatever the user specified. Thus such an insert
cannot be split. However, the \csidx{endinsert} macro
puts a \cs{penalty100} on top of the box, so the
insertion can be split with an empty part before the split.
The effect is that the whole insertion is carried over to
the next page. As the \cs{floatingpenalty} for top inserts
is zero, arbitrarily many of these inserts can be moved forward
until there is a page with sufficient space.
Further examples of insertion macros can be found
in~\cite{Sal3}.
%\message{Maybe spaceleft example?}
%%%% end of input file [page]
%\InputFile:io
%%%% this is input file [io]
%\subject[io] File Input and Output
\endofchapter
\chapter{File Input and Output}\label{io}
This chapter treats the various ways in which \TeX\ can read from
\mdqon
\term I/""O\par
\mdqoff
and write to external files.
\begin{inventory}
\item [\cs{input}]
Read a specified file as \TeX\ input.
\item [\cs{endinput}]
Terminate inputting the current file after the current line.
\item [\cs{pausing}]
Specify that \TeX\ should pause after each line that is
read from a file.
\item [\cs{inputlineno}]
Number of the current input line.
\item [\cs{write}]
Write a \gr{general text} to the terminal or to a file.
\item [\cs{read}]
Read a line from a stream into a control sequence.
\item [\cs{newread \cs{newwrite}}]
\mdqon
Macro for allocating a new input/""output stream.
\mdqoff
\item [\cs{openin \cs{closein}}]
Open/close an input stream.
\item [\cs{openout \cs{closeout}}]
Open/close an output stream.
\item [\cs{ifeof}]
Test whether a file has been fully read, or does not exist.
\item [\cs{immediate}]
Prefix to have output operations executed right away.
\item [\cs{escapechar}]
Number of the character that is used
when control sequences are being converted
into character tokens.
\IniTeX\ default:~92.
\item [\cs{newlinechar}]
Number of the character that triggers a new line in
\cs{write} statements.
\end{inventory}
%\point Including files: \cs{input} and \cs{endinput}
\section{Including files: \protect\cs{input} and \protect\cs{endinput}}
Large documents can be segmented in \TeX\ by putting
\term input files\par\cstoidx input\par
parts in separate files, and loading these with \cs{input}
into the master file. The exact syntax for
file names is implementation dependent; most of the
time a \n{.tex} file extension is assumed if no explicit
extension is given.
File names can be delimited with a space or with \cs{relax}.
The \cs{input} command is expandable.
If \TeX\ encounters in an input file the
\csidx{endinput} statement, it acts as if the file
ends after the line on which the statement occurs.
Any statements on the same line as \cs{endinput} are
still executed.
The \cs{endinput} statement is expandable.
%\point File I{/}O
\section{File I{/}O}
\TeX\ supports input and output streams for reading and writing
\altt
files one line at a time.
%\spoint Opening and closing streams
\subsection{Opening and closing streams}
\TeX\ supports up to 16 simultaneous input and 16 output streams.
\term streams\par
The plain \TeX\ macros
\csidx{newread} and \csidx{newwrite} give the number of an unused
stream. This number is assigned by a \cs{chardef} command.
Input streams are completely independent of output
streams.
Input streams are opened by
\cstoidx openin\par
\begin{disp}\cs{openin}\gr{4-bit number}\gr{equals}\gr{filename}\end{disp}
and closed by
\cstoidx closein\par
\begin{disp}\cs{closein}\gr{4-bit number}\end{disp}
Output streams are opened by
\cstoidx openout\par
\begin{disp}\cs{openout}\gr{4-bit number}\gr{equals}\gr{filename}\end{disp}
and closed by
\cstoidx closeout\par
\begin{disp}\cs{closeout}\gr{4-bit number}\end{disp}
If an output file does not yet exist, it is created
by \cs{openout}; if it did exist, an \cs{openout} will
cause it to be overwritten.
The output operations \cs{openout}, \cs{closeout},
and \cs{write} can all three be prefixed by \cs{immediate};
see below.
%\spoint Input with \cs{read}
\subsection{Input with \cs{read}}
In addition to the \cs{input} command, which reads a whole
file, \TeX\ has the \csidx{read} operation, which
reads one line from a file (or from the user terminal).
The syntax of the read command is
\begin{disp}\cs{read}\gr{number}\n{to}\gr{control sequence}\end{disp}
The effect of this statement is that one input line
is read from the designated stream, and the control
sequence is defined as a macro without parameters, having
that line as replacement text.
If the input line is not balanced with respect to braces,
\TeX\ will read more than one line, continuing for as long
as is necessary to get a balanced token list.
\TeX\ implicitly appends an empty line to each input stream,
\alt
so the last \cs{read} operation on a stream will always
yield a single \cs{par} token.
Read operations from any stream outside the range 0--15 \ldash or
streams not associated with an open file, or on which the file
end has been reached \rdash
read from the terminal. If the stream number is positive
the user is prompted with the name of the control sequence
being defined by the \cs{read} statement.
\begin{example}\begin{verbatim}
\read16 to \data
\end{verbatim}
displays a prompt \begin{verbatim}
\data=
\end{verbatim}
and typing `my name'
in response makes the read statement equivalent
to \begin{verbatim}
\def\data{my name }
\end{verbatim}
The space at the end of the input derives from the line end;
to prevent this one could write
\begin{verbatim}
{\endlinechar=-1 \global\read16 to \data}
\end{verbatim}
\end{example}
%\spoint Output with \cs{write}
\subsection{Output with \cs{write}}
\TeX's \csidx{write} command
\begin{disp}\cs{write}\gr{number}\gr{general text}\end{disp}
writes a balanced token list to a file which has been opened
by \cs{openout}, to the log file, or to the terminal.
Write operations to a stream outside 0--15 \ldash or to a
stream that is not associated with an open file \rdash go to the log file;
if the stream number is positive they
go to the terminal as well as to the log file.
The token list argument of \cs{write}, defined as
\begin{disp}\gr{general text} $\longrightarrow$ \gr{filler}%
\lb\gr{balanced text}\gr{right brace}\end{disp}
can have an implicit opening brace.
This argument is expanded as if it were the replacement
text of an \cs{edef}, so, for instance,
any macros and conditionals appearing are expanded.
No commands are executed, however.
This expansion occurs
at the time of shipping out; see below.
Until that time the argument token list is stored
in a whatsit item on the current list.
See further Chapter~\ref{expand} for
a discussion of expansion during writing.
A control sequence output by \cs{write} (or \cs{message})
is represented with a trailing space, and using
character number \cs{escapechar}
for the escape character.
The \IniTeX\ default for this is~92,
the code for the backslash.
The trailing space can be prevented by prefixing the control
sequence with \cs{string}.
%\point Whatsits
\section{Whatsits}
There is an essential difference
\term whatsits\par
in execution between input and output:
operations concerning output
(\cs{openout}, \cs{closeout}, \cs{write})
are not executed immediately; instead, they are saved until
the box in which they appear is shipped out
to the \n{dvi} file.
Writes and the other two output operations are placed
in `whatsit' items on whichever list is currently being built.
The actual operation occurs when the part of the page
that has the item is shipped out to the \n{dvi} file.
This delayed output is made necessary by \TeX's
asynchronous output routine behaviour.
See a worked-out example on page~\pageref{expand:write}.
An \verb.\immediate\write. \ldash or any other \csidx{immediate} output
operation \rdash is executed on the spot, and
does not place a whatsit item on the current list.
The argument of a \cs{special} command
(see page~\pageref{special}) is also placed in a whatsit.
Whatsit items in leader boxes are ignored.
%\point Assorted remarks
\section{Assorted remarks}
%\spoint Inspecting input
\subsection{Inspecting input}
\TeX\ records the current line number in the current input file
in the \gr{internal integer} parameter \csidx{inputlineno}
(in \TeX3).
If the parameter \csidx{pausing} is positive, \TeX\ shows
every line that is input on the terminal screen,
and gives the user the opportunity
to insert commands. These can for instance be \cs{show} commands.
Inserted commands are treated as if they were directly
in the source file: it is for instance not necessary
to prefix them with~`i', as would be necessary when
\TeX\ pauses for an error.
%\spoint Testing for existence of files
\subsection{Testing for existence of files}
\TeX\ is not the friendliest of systems when you
\howto Test whether a file exists\par
ask it to input a non-existing file. Therefore the following
sequence of commands can be used to prevent trouble\label{ex:eof}:
\begin{verbatim}
\newread\instream \openin\instream= fname.tex
\ifeof\instream \message{File 'fname' does not exist!}
\else \closein\instream \input fname.tex
\fi
\end{verbatim}
Here an input stream is opened with the given file name.
The end-of-file test is also true
if an input stream does not correspond to a physical file,
so if this conditional is not true,
the file exists and an \cs{input} command can safely be given.
%\spoint Timing problems
\subsection{Timing problems}
The synchronization between write operations on the
one hand, and opening/closing operations
of files on the other hand,
can be a crucial point. Auxiliary files, such as are
used by various formats to implement cross-references,
are a good illustration of this.
Suppose that during a run of \TeX\ the auxiliary file is written, and
\howto Input a file that was created in the same run of \TeX\par
at the end of the run it has to be input again for a variety
of purposes (such as seeing whether references have changed).
An \cs{input} command is executed right away, so
the file must have been closed with an \verb=\immediate\closeout=.
However, now it becomes possible that the file is closed
before all writes to it have been performed.
The following sequence remedies this:
\begin{verbatim}
\par\vfil\penalty -10000 \immediate\closeout\auxfile
\end{verbatim}
The first three commands activate the output routine
in order to close off the last page,
so all writes will indeed have been performed before the
file is closed.
%\spoint \cs{message} versus \cs{immediate}\cs{write}16
\subsection{\cs{message} versus \cs{immediate}\cs{write}16}
Messages to the user can be given using
\csidx{message}\gr{general text}, which writes to the terminal.
Messages are appended to one another;
the line is wrapped when the line
length (a~\TeX\ compile-time constant) has been reached.
In \TeX\ version2,
a~maximum of 1000 characters is written per message;
this is not a compile-time constant, but is hard-wired
into the \TeX\ program.
Each message given with \verb=\immediate\write=
starts on a new line; the user can force a new line
in the message by including the character with
number~\csidx{newlinechar}. This parameter also works
in \cs{message}.
%\spoint Write inside a vertical box
\subsection{Write inside a vertical box}
Since a write operation winds up on the vertical list in a whatsit,
issuing one at the start of a \cs{vtop}
will probably influence the height of that box
(see Chapter~\ref{boxes}). As an example,
\begin{verbatim}
have the \vtop{\write\terminal{Hello!}\hbox{more text}}
dangling from
\end{verbatim}
will have the~\vtop{\write-1{vtop gezien}\hbox{more text}}~dangling
from the baseline (and when this book is \TeX ed the
message `Hello!' appears on the screen).
%\spoint Expansion and spaces in \cs{write} and \cs{message}
\subsection{Expansion and spaces in \cs{write} and \cs{message}}
Both \cs{write} and \cs{message} expand their argument
as if it were the replacement text of an \cs{edef}.
Therefore \begin{verbatim}
\def\a{b}\message{\a}
\end{verbatim}
will
write out~`\n b'.
Unexpandable control sequences are displayed with a trailing
space (and prefixed with the \cs{escapechar}):
\begin{verbatim}
\message{\hbox\vbox!}
\end{verbatim}
will write out
`\verb>\hbox \vbox !>'. Undefined control sequences give an error here.
Expandable control sequences can be written out with some
care:\begin{verbatim}
\message{\noexpand\ifx}
\message{\string\ifx}
{\let\ifx\relax \message{\ifx}}
\end{verbatim}
all write out `\verb>\ifx>'.
Note, however, that spaces after expandable control sequences
are removed in the input processor, which goes into state~$S$
after a control sequence. Therefore
\begin{verbatim}
\def\a{b}\def\c{d}
\message{\a \c}
\end{verbatim}
writes out `\n{bd}'.
Inserting a space can be done as follows:
\begin{verbatim}
\def\space{ } % in plain TeX
\message{\a\space\c}
\end{verbatim}
displays `\n{b d}'.
Note that\begin{verbatim}
\message{\a{ }\c}
\end{verbatim}
does not work: it displays `\verb=b{ }d='
since braces are unexpandable character tokens.
%%%% end of input file [io]
%\InputFile:alloc
%%%% this is input file [alloc]
%\subject[alloc] Allocation
\endofchapter
\chapter{Allocation}\label{alloc}
\TeX\ has registers of a number of types. For some of these,
explicit commands exist to define a synonym for a certain register;
for all of them macros exist in the plain format
to allocate an unused register. This chapter treats
the synonym and allocation commands, and discusses
some guidelines for macro writers regarding allocation.
\begin{inventory}
\item [\cs{countdef}]
Define a synonym for a \cs{count} register.
\item [\cs{dimendef}]
Define a synonym for a \cs{dimen} register.
\item [\cs{muskipdef}]
Define a synonym for a \cs{muskip} register.
\item [\cs{skipdef}]
Define a synonym for a \cs{skip} register.
\item [\cs{toksdef}]
Define a synonym for a \cs{toks} register.
\item [\cs{newbox}]
Allocate an unused \cs{box} register.
\item [\cs{newcount}]
Allocate an unused \cs{count} register.
\item [\cs{newdimen}]
Allocate an unused \cs{dimen} register.
\item [\cs{newfam}]
Allocate an unused math family.
\item [\cs{newinsert}]
Allocate an unused insertion class.
\item [\cs{newlanguage}]
(\TeX3 only)
Allocate a new language number.
\item [\cs{newmuskip}]
Allocate an unused \cs{muskip} register.
\item [\cs{newskip}]
Allocate an unused \cs{skip} register.
\item [\cs{newtoks}]
Allocate an unused \cs{toks} register.
\item [\cs{newread}]
Allocate an unused input stream.
\item [\cs{newwrite}]
Allocate an unused output stream.
\end{inventory}
%\point Allocation commands
\section{Allocation commands}
In plain \TeX, \cs{new...} macros are defined for
allocation of registers.
The registers of \TeX\ fall into two classes that are
\term registers, allocation of\par
allocated in different ways. This is treated below.
The \csidx{newlanguage} macro of plain \TeX\
does not allocate any register. Instead it merely assigns
a number, starting from~0.
\TeX\ (version~3) can have at most 256 different
sets of hyphenation patterns.
The \cs{new...} macros of plain \TeX\ are defined to be
\cs{outer} (see Chapter~\ref{macro} for a precise explanation),
which precludes use of the allocation macros in other macros.
Therefore the \LaTeX\ format redefines these macros
without the \cs{outer} prefix.
%\spoint \cs{count}, \cs{dimen}, \cs{skip}, \cs{muskip}, \cs{toks}
\subsection{\cs{count}, \cs{dimen}, \cs{skip}, \cs{muskip}, \cs{toks}}
For these registers there exists a \gr{registerdef} command,
for instance \cs{countdef}, to couple a specific register
to a control sequence:
\begin{Disp}\gr{registerdef}\gr{control
sequence}\gr{equals}\gr{8-bit number}\end{Disp}
After the definition \begin{verbatim}
\countdef\MyCount=42
\end{verbatim}
the allocated register can be used as
\begin{verbatim}
\MyCount=314
\end{verbatim}
or \begin{verbatim}
\vskip\MyCount\baselineskip
\end{verbatim}
The \gr{registerdef} commands are used in plain \TeX\ macros
\cs{newcount} et cetera that allocate an unused register;
after\begin{verbatim}
\newcount\MyCount
\end{verbatim}
\cs{MyCount} can be used
exactly as in the above two examples.
%\spoint \cs{box}, \cs{fam}, \cs{write}, \cs{read}, \cs{insert}
\subsection{\cs{box}, \cs{fam}, \cs{write}, \cs{read}, \cs{insert}}
For these registers there exists no \gr{registerdef} command in \TeX,
so \cs{chardef} is used to allocate box registers
in the corresponding plain \TeX\ macros \cs{newbox}, for instance.
The fact that \cs{chardef} is used implies that the
defined control sequence does not stand for the register itself,
but only for its number. Thus after \begin{verbatim}
\newbox\MyBox
\end{verbatim}
it is necessary to write \begin{verbatim}
\box\MyBox
\end{verbatim}
Leaving out the \cs{box} means that the character
in the current font with number
\cs{MyBox} is typeset. The \cs{chardef} command
is treated further in Chapter~\ref{char}.
%\point Ground rules for macro writers
\section{Ground rules for macro writers}
The \cs{new...} macros of plain \TeX\ have been designed
to form a foundation for macro packages, such that
several of such packages can operate without collisions
in the same run of \TeX. In appendix~B of \TeXbook\
Knuth formulates some ground rules that macro writers should
adhere to.
\begin{enumerate}
\item The \cs{new...} macros do not allocate registers
with numbers~0--9. These can therefore be used as `scratch'
registers. However, as any macro family can use them,
no assumption can be made about the permanency of their
contents. Results that are to be passed from one call to
another should reside in specifically allocated registers.
Note that count registers 0--9 are used for page identification
in the \n{dvi} file (see Chapter~\ref{TeXcomm}), so no global assignments
to these should be made.
\item \cs{count255}, \cs{dimen255}, and \cs{skip255} are
also available. This is because inserts are
allocated from 254 downward and, together with an insertion box,
a count, dimen, and skip register,
all with the same number, are allocated.
Since \cs{box255} is used by the output routine
(see Chapter~\ref{output}),
the count, dimen, and skip with number~255 are freely available.
\item Assignments to scratch registers~0, 2, 4, 6, 8, and~255
should be local; assignments to registers~1, 3, 5, 7,~9
should be \cs{global} (with the exception of the \cs{count}
registers). This guideline prevents `save
stack build-up' (see Chapter~\ref{error}).
\item Any register can be used inside a group, as \TeX's
grouping mechanism will restore its value outside
the group. There are two conditions on this use of
a register:
no global assignments should be made to it, and
it must not be possible that other macros may be
activated in that group that perform global assignments
to that register.
\item Registers that are used over longer periods of time,
or that have to survive in between calls of different
macros, should be allocated by \cs{new...}.
\end{enumerate}
%%%% end of input file [alloc]
%\InputFile:run
%%%% this is input file [run]
%\subject[run] Running \TeX
\endofchapter
\chapter{Running \TeX}\label{run}
This chapter treats the run modes of \TeX, and some
other commands associated with the job being processed.
\begin{inventory}
\item [\cs{everyjob}]
Token list that is inserted at the start of each new job.
\item [\cs{jobname}]
Name of the main \TeX\ file being processed.
\item [\cs{end}]
Command to finish off a run of \TeX.
\item [\cs{bye}]
Plain \TeX\ macro to force the final output.
\item [\cs{pausing}]
Specify that \TeX\ should pause after each line that is
read from a file.
\item [\cs{errorstopmode}]
\TeX\ will ask for user input on the occurrence of an error.
\item [\cs{scrollmode}]
\TeX\ fixes errors itself,
but will ask the user for missing files.
\item [\cs{nonstopmode}]
\TeX\ fixes errors itself,
and performs an emergency stop on serious errors
such as missing input files.
\item [\cs{batchmode}]
\TeX\ fixes errors itself
and performs an emergency stop on serious errors
such as missing input files,
but no terminal output is generated.
\end{inventory}
%\point Jobs
\section{Jobs}
\TeX\ associates with each run a name for the file
\term job\par
being processed: the \csidx{jobname}. If \TeX\ is run
interactively
\ldash meaning that it has been invoked without a file argument,
and the user types commands \rdash
the jobname is \n{texput}.
The \cs{jobname} can be used to generate
the names of auxiliary files to be read or
written during the run. For instance, for a file \n{story.tex}
the \cs{jobname} is \n{story}, and writing
\begin{verbatim}
\openout\Auxiliary=\jobname.aux
\openout\TableOfContents=\jobname.toc
\end{verbatim}
will create the files \n{story.aux} and \n{story.toc}.
%\spoint Start of the job
\subsection{Start of the job}
\TeX\ starts each job by inserting the \csidx{everyjob} token
list into the command stream.
Setting this variable during a run of \TeX\ has no use,
but a format can use it to identify itself to the user.
If a
format fills the token list, the commands therein are automatically
executed when \TeX\ is run using that format.
%\spoint End of the job
\subsection{End of the job}
A \TeX\ job is terminated by the \csidx{end} command. This
may involve first forcing the output routine to process any
remaining material (see Chapter~\ref{page:break}).
If the end of job occurs inside a group
\TeX\ will give a diagnostic
message. The \cs{end} command is not allowed in internal
vertical mode, because this would be inside a vertical box.
Usually some sugar coating of the \cs{end} command is necessary.
For instance the plain \TeX\ macro \csidx{bye} is defined
as \begin{verbatim}
\def\bye{\par\vfill\supereject\end}
\end{verbatim}
where the \cs{supereject} takes care of any leftover insertions.
%\spoint The log file
\subsection{The log file}
For each run \TeX\ creates a log file. Usually this will be
\term log file\par
a file with as name the value of \cs{jobname}, and the
extension \n{.log}. Other extensions such as \n{.lis}
are used by some implementations.
This log file contains all information that
is displayed on the screen during the run of \TeX, but
it will display some information more elaborately, and it
can contain statistics that are usually not displayed on
the screen. If the parameter \cs{tracingonline}
has a positive value, all the log file information will be
shown on the screen.
Overfull and underfull boxes are reported on the terminal
screen, and they are dumped using the parameters
\cs{showboxdepth} and \cs{showboxbreadth} in the log file
(see Chapter~\ref{trace}). These parameters are also used
for box dumps caused by the \cs{showbox} command, and
for the dump of boxes written by \cs{shipout}
if \cs{tracingoutput} is set to a positive value.
Statistics generated by commands such as \cs{tracingparagraphs}
will be written to the log file; if \cs{tracingonline} is positive
they will also be shown on the screen.
Output operations to a stream that is not open, or to a
stream with a number that is not in the range 0--15,
go to the log file. If the stream number is positive,
they also go to the terminal.
%\point Run modes
\section{Run modes}
By default, \TeX\ goes into \cs{errorstopmode} if an error occurs:
\term run modes\par\cstoidx errorstopmode\par
it stops and asks for input from the user. Some implementations
have a way of forcing \TeX\ into errorstopmode
when the user interrupts \TeX, so that
the internal state of \TeX\ can be inspected (and altered).
See page~\pageref{interaction} for ways to switch the run
mode when \TeX\ has been interrupted.
Often, \TeX\ can
fix an error itself if the user asks \TeX\ just to continue
(usually by hitting the return key),
but sometimes (for instance in alignments)
it may take a while before \TeX\ is on the
right track again (and sometimes it never is).
In such cases the user may want to
turn on \csidx{scrollmode},
which instructs \TeX\ to fix as best it can any
occurring error without confirmation from the user.
This is usually done by typing `s' when \TeX\ asks
for input.
In \cs{scrollmode}, \TeX\ also does not ask for input
after \cs{show...} commands.
\alt
However, some errors, such as a file that could not be
found for \cs{input}, are not so easily remedied, so
the user will still be asked for input.
With \csidx{nonstopmode} \TeX\ will scroll through errors and,
in the case of the kind of error that cannot be recovered from,
it will make an emergency stop, aborting the run.
Also \TeX\ will abort the run if a \cs{read} is attempted
from the terminal.
The \csidx{batchmode} differs only from nonstopmode in that
it gives messages only to the log file, not to the terminal.
\endofchapter
\chapter{\TeX\ and the Outside World}\label{TeXcomm}
This
chapter treats those commands that bear relevance to
\n{dvi} files and formats. It gives some global information
about \IniTeX, font and format files,
Computer Modern typefaces, and \web.
\begin{inventory}
\item [\cs{dump}]
Dump a format file; possible only in \IniTeX,
not allowed inside a group.
\item [\cs{special}]
Write a \gr{balanced text} to the \n{dvi} file.
\item [\cs{mag}]
1000 times the magnification of the document.
\item [\cs{year}]
The year of the current job.
\item [\cs{month}]
The month of the current job.
\item [\cs{day}]
The day of the current job.
\item [\cs{time}]
Number of minutes after midnight that the current job started.
\item [\cs{fmtname}]
Macro containing the name of the format dumped.
\item [\cs{fmtversion}]
Macro containing the version of the format dumped.
\end{inventory}
%\point \TeX, \IniTeX, \VirTeX
\section{\TeX, \IniTeX, \VirTeX}
In the terminology established in {\italic \TeX: the Program},
\cite{Knuth:TeXprogram},
\term\TeX\par\term\IniTeX\par\term\VirTeX\par
\TeX\ programs come in three flavours.
\IniTeX\ is a version of \TeX\ that can generate formats;
\VirTeX\ is a production version without preloaded format,
and \TeX\ is a production version with
preloaded (plain) format. Unfortunately, this terminology is
not adhered to in general. A~lot of systems do not use preloaded
formats (the procedure for making them may be impossible on
some operating systems),
and call the `virgin \TeX' simply \TeX.
This manual also follows that convention.
%\spoint Formats: loading
\subsection{Formats: loading}
A format file (usually with extension~\n{.fmt})
is a compact dump of \TeX's internal structures.
\term format files\par
Loading a format file takes a considerably shorter time than
would be needed for
loading the font information and the macros that
constitute the format.
Both \TeX\ and \IniTeX\ can load a format; the user specifies
this by putting the name on the command line
\begin{verbatim}
% tex &plain
\end{verbatim}
or at the \n{**} prompt
\begin{verbatim}
% tex
This is TeX. Version ....
** &plain
\end{verbatim}
preceded by an ampersand (for UNIX, this should be \verb>\&> on
the command line). An input file name can follow the
format name in both places.
\IniTeX\ does not need a format,
but if no format is specified for (Vir)\TeX, it will try to
load the plain format, and halt if that cannot be found.
%\spoint Formats: dumping
\subsection{Formats: dumping}
\IniTeX\ is the only version of \TeX\
that can dump a format, since it is
the only version of \TeX\ that has
the command~\csidx{dump},
which causes the internal structures
to be dumped as a format.
It is also the only version of \TeX\ that has the command
\cs{patterns}, which
is needed to specify a list of hyphenation
patterns.
Dumping is not allowed inside a group, that is
\begin{verbatim}
{ ... \dump }
\end{verbatim}
is not allowed. This restriction
prevents difficulties with \TeX's save stack.
After the \cs{dump} command \TeX\ gives an elaborate listing of
its internal state, and of the font names associated with
fonts that have been loaded and ends the job.
An interesting possibility arises from the fact that
\IniTeX\ can both load and dump a format.
Suppose you have written a set of macros that build
on top of plain \TeX, \n{superplain.tex}.
You could then call
\begin{verbatim}
% initex &plain superplain
*\dump
\end{verbatim}
and get a format file \n{superplain.fmt} that
has all of plain, and all of your macros.
%\spoint Formats: preloading
\subsection{Formats: preloading}
On some systems it is possible to interrupt a running program,
and save its `core image' such that this can be started as
an independent program.
The executable made from the
core image of a \TeX\ program interrupted after it has loaded
a format is called a \TeX\ program with preloaded format.
The idea behind preloaded formats is
that interrupting \TeX\ after it has loaded a format, and making
this program available to the user,
saves in each run the time for loading the format.
In the good old days when computers were quite a bit slower
this procedure made sense.
Nowadays, it does not seem so necessary.
Besides, dumping a core image may not always be possible.
%\spoint The knowledge of \IniTeX
\subsection{The knowledge of \IniTeX}
If no format has been loaded, \IniTeX\ knows very little.
For instance, it has no open/close group characters.
However, it can not be completely devoid of knowledge
lest there be no way to define anything.
Here is the extent of its knowledge.
\begin{itemize} \mathsurround=1.5pt
%\flushright:no
\item \verb>\catcode`\\=0>, \verb>\escapechar=`\\>
(see page~\pageref{ini:esc}).
\item \verb>\catcode`\^^M=5>, \verb>\endlinechar=`\^^M>
(see page~\pageref{ini:eol}).
\item \verb>\catcode`\ =10>
(see page~\pageref{ini:sp}).
\item \verb>\catcode`\%=14>
(see page~\pageref{ini:comm}).
\item \verb>\catcode`\^^?=15>
(see page~\pageref{ini:invalid}).
\item \cs{catcode}$x$\n{=11} for $x={}$\n{`a..`z,`A..`Z}
(see page~\pageref{ini:let}).
\item \cs{catcode}$x$\n{=12} for all other character codes\nl
(see page~\pageref{ini:other}).
\item \cs{sfcode}$x$=\n{999} for $x={}$\n{`A..`Z},
\cs{sfcode}$x$\n{=1000} for all other characters
(see page~\pageref{ini:sf}).
\item \verb>\lccode`a..`z,`A..`Z=`a..`z>, \verb>\uccode`a..`z,`A..`Z=`A..`Z>,
\cs{lccode}$x$\n{=0}, \cs{uccode}$x$\n{=0} for all other characters
(see page~\pageref{ini:uclc}).
\item \verb>\delcode`.=0>, \cs{delcode}$x$\n{=-1} for all other characters
(see page~\pageref{ini:del}).
\item \cs{mathcode}$x$\n{="!7100}${}+x$ for all lowercase and uppercase
letters, \cs{mathcode}$x$\n{="!7000}${}+x$ for all digits,
\cs{mathcode}$x$\n=$x$ for all other characters
(see page~\pageref{ini:fam}).
\item \cs{tolerance=10000}, \cs{mag=1000},
\cs{maxdeadcycles=25}.
\end{itemize}
%\spoint Memory sizes of \TeX\ and \IniTeX
\subsection{Memory sizes of \TeX\ and \IniTeX}
The main memory size of \TeX\ and \IniTeX\ is controlled by
four constants in the source code:
\n{mem\_bot}, \n{mem\_top}, \n{mem\_min}, and~\n{mem\_max}.
For Ini\TeX's memory \n{mem\_bot${}={}$mem\_min}
and \n{mem\_top${}={}$mem\_max};
for \TeX\ \n{mem\_bot} and \n{mem\_top} record the main memory
size of the Ini\TeX\ used to dump the format.
Thus versions of \TeX\ and \IniTeX\ have to be adapted
to each other in this respect.
\TeX's own main memory can be bigger than that of the
corresponding \IniTeX: in general
\n{mem\_min${}\leq{}$mem\_bot} and \n{mem\_top${}\leq{}$mem\_max}.
For \IniTeX\ a smaller main memory can suffice,
as this program is typically
not meant to do real typesetting.
There may even be a real need for the main memory
to be smaller, because \IniTeX\ needs a lot of auxiliary
storage for initialization and for building the
hyphenation table.
%\point More about formats
\section{More about formats}
%\spoint Compatibility
\subsection{Compatibility}
\TeX\ has a curious error message: `Fatal format error: I'm stymied',
which is given if \TeX\ tries to load a format that was made
with an incompatible version of \IniTeX. See the point
above about memory sizes, and Chapter~\ref{error} for
the hash size (parameters \n{hash\_size} and \n{hash\_prime})
and the hyphenation exception dictionary (parameter \n{hyph\_size}).
%\spoint Preloaded fonts
\subsection{Preloaded fonts}
During a run of \TeX\ the only information needed about fonts
is the data that is found in the \n{tfm} files (see below).
Since a run of \TeX, especially if the input contains math material,
can easily access 30--40 fonts, the disk access for
all the \n{tfm} files can become significant.
Therefore the plain format and \LaTeX\ load these
metrics files in \IniTeX. A~\TeX\ version using such a format
does not need to load any \n{tfm} files.
On the other hand, if a format has the possibility of accessing
a range of typefaces, it may be advantageous to have metrics
files loaded on demand during the actual run of \TeX.
%\spoint The plain format
\subsection{The plain format}
The first format written for \TeX, and the basis for all
later ones,
is the plain format, described in \TeXbook.
It is a mixture of \begin{itemize}
\item definitions and macros one simply cannot live without
such as the initial \cs{catcode} assignments,
all of the math delimiter definitions,
and the \cs{new...} macros;
\item constructs that are useful, but for which \LaTeX\
and other packages use
a different implementation, such as the tabbing environment; and
\item some macros that are insufficient for any but the
simplest applications: \cs{item} and \cs{beginsection}
are in this category.\end{itemize}
It is the first category which Knuth meant to serve as a
foundation for future macro packages, so that they
can live peacefully together (see Chapter~\ref{alloc}).
This idea is reflected in the fact that the name `plain'
is not capitalized: it is the basic set of macros.
%\spoint The \LaTeX\ format
\subsection{The \LaTeX\ format}
The \LaTeX\ format\term\LaTeX\par,
written by Leslie Lamport of Digital Equipment Corporation
and described in~\cite{Lamport:LaTeX},
was released around 1985.
The \LaTeX\ format, using its own version
of \n{plain.tex} (called \n{lplain.tex}),
is not compatible with plain \TeX;
a~number of plain macros are not available. Still, it contains
large parts of the plain format (even when they overlap with
its own constructs).
\LaTeX\ is a powerful format with facilities such as
marginal notes, floating objects, cross referencing,
and automatic table of contents generation.
Its main drawback is that the `style files' which
define the actual layout are quite hard to write
(although \LaTeX\ is in the process of a major revision,
in which this problem will be tackled;
see \cite{Frank} and~\cite{Frank2}).
As a result,
people have had at their disposal mostly the styles
written by Leslie Lamport, the layout of which is
rather idiosyncratic. See~\cite{BEP} for a successful
attempt to replace these styles.
%\spoint Mathematical formats
\subsection{Mathematical formats}
There are two formats with extensive facilities for
mathematics typesetting:
\AmsTeX~\cite{Ams}
(which originated at the American Mathematical Society)
and \LamsTeX~\cite{Lams}.
The first of these includes more facilities than plain \TeX\
or \LaTeX\ for typesetting mathematics, but it lacks
features such as automatic numbering and cross-referencing,
available in \LaTeX, for instance. \LamsTeX, then, is the
synthesis of \AmsTeX\ and \LaTeX. Also it includes
still more features for mathematics, such as complicated
tables and commutative diagrams.
%\spoint Other formats
\subsection{Other formats}
Other formats than the above exist:
for instance, \n{Phyzzx}~\cite{Phyzzx}, \n{TeXsis}~\cite{TeXsis},
Macro \TeX~\cite{Amy}, \n{eplain}~\cite{Berry},
and \n{\TeX T1}~\cite{TeXT1}.
Typically, such formats provide the facilities of \LaTeX, but
try to be more easily adaptable by the user.
Also, in general they
have been written with the intention of being an
add-on product to the plain format.
This book is also written in an `other format':
the \n{lollipop} format.
\term Lollipop\par
This format does not contain user macros, but the
tools with which a style designer can program them; see~\cite{EL}.
%\point The \n{dvi} file
\section{The \n{dvi} file}
The \n{dvi} file (this term stands for `device independent')
\term \n{dvi} file\par
contains the output of a \TeX\ run: it
contains compactly dumped representations of boxes that
have been sent there by \cs{shipout}\gr{box}. The act
of shipping out usually occurs inside the output routine,
but this is not necessarily so.
%\spoint The \n{dvi} file format
\subsection{The \n{dvi} file format}
A \n{dvi} file is a byte-oriented file,
consisting of a preamble, a postamble,
and a list of pages.
Access for subsequent software to a completed \n{dvi} file
is strictly sequential in nature:
the pages are stored as a backwards linked list. This
means that only two ways of accessing are possible:
\begin{itemize} \item given the start of a page, the next can be
found by reading until an end-of-page code is encountered, and
\item starting at the end of the file pages can be read
backwards at higher speed, as each beginning-of-page code
contains the byte position of the previous one.\end{itemize}
The preamble and postamble contain
\begin{itemize}\item the magnification of the document (see below),
\item the unit of measurement used for the document, and
\item possibly a comment string.\end{itemize}
The postamble contains in addition a list of the font definitions
that appear on the pages of the file.
Neither the preamble nor the postamble
of the file contains a
table of byte positions of pages.
The full definition of the \n{dvi} file format can be found
in~\cite{Knuth:TeXprogram}.
%\spoint Page identification
\subsection{Page identification}
Whenever a \cs{shipout} occurs, \TeX\ also writes the
values of counters 0--9 to the \n{dvi} file and the terminal.
Ordinarily, only counter~0, the page number, is used, and the
other counters are zero. Those zeros are not output to the
terminal. The other counters can be used to indicate further
structure in the document. Log output shows the non-zero
counters and the zero counters in between.
%\spoint Magnification
\subsection{Magnification }
Magnification of a document can be indicated by the \gr{integer
parameter}
\term magnification\par\cstoidx mag\par
\cs{mag}, which specifies 1000 times the magnification
ratio.
The \n{dvi} file contains the value of \cs{mag} for the
document in its preamble and postamble.
If no {\tt true} dimensions are used
the \n{dvi} file will look the same as when no magnification
would have been used, except for the \cs{mag} value in the
preamble and the postamble.
Whenever a {\tt true} dimension is used it is divided
by the value of \cs{mag}, so that the final output will have
the dimension as prescribed by the user.
The \cs{mag} parameter cannot be changed after a
\n{true} dimension has been used, or after the first
page has been shipped to the \n{dvi} file.
Plain \TeX\ has the \csidx{magnification} macro for
globally sizing the document, without changing
the physical size of the page:
\begin{verbatim}
\def\magnification{\afterassignment\m@g\count@}
\def\m@g{\mag\count@
\hsize6.5truein\vsize8.9truein\dimen\footins8truein}
\end{verbatim}
The explanation for this is
as follows: the command \cs{m@g} is saved with an \cs{afterassignment}
command, and the magnification value (which is 1000 times the
actual magnification factor) is assigned to \cs{count@}.
After this assignment, the macro \cs{m@g} assigns
the magnification value to \cs{mag}, and the horizontal
and vertical size are reset to
their original values {\tt 6.5truein} and {\tt 8.9truein}.
The \cs{footins} is also reset.
%\point[special] Specials
\section{Specials}
\label{special}
\mdqon
\TeX\ is to a large degree machine"-independent, but it still needs
\mdqoff
\term specials\par
a hook for machine-dependent extensions. This is the
\csidx{special} command, which writes a \gr{balanced text}
to the \n{dvi} file. \TeX\ does not interpret this token list:
it assumes that the printer driver knows what to do with it.
\cs{special} commands are supposed not to change the
$x$ and $y$ position on the page, so that the implementation
of \TeX\ remains independent of the actual device driver
\term device drivers\par
that handles the \cs{special}.
The most popular application of specials is probably the
inclusion of graphic material, written in some
page description language, such as PostScript\term PostScript\par.
The size of the graphics can usually be determined from
the file containing it (in the case of encapsulated
PostScript through
the `bounding box' data), so \TeX\ can leave space for
such material.
%\point Time
\section{Time}
\TeX\ has four parameters, \csidx{year}, \csidx{month}, \csidx{day}, and
\csidx{time}, that tell
\term time\par\term date\par
the time when the current job started.
After this, the parameters are not updated.
The user can change them at any time.
All four parameters are integers; the \cs{time} parameter
gives the number of minutes since midnight that the current
job started.
%\point Fonts
\section{Fonts}
Font information is split in the \TeX\ system into
the metric information (how high, wide, and deep is a character),
and the actual description of the characters in a font.
\TeX, the formatter, needs only the metric information;
printer drivers and screen previewers need the character
descriptions. With this approach it is for instance possible
for \TeX\ to use with relative ease the resident fonts of
a printer.
%\spoint Font metrics
\subsection{Font metrics}
The metric information of \TeX's fonts is stored in \n{tfm}
\term font metrics\par
files, which stands for `\TeX\ font metric' files.
Metrics files contain the following information
(see \cite{Knuth:TeXprogram} for the full definition):
\begin{itemize}\item the design size of a font;
\item the values for the \cs{fontdimen} parameters
(see Chapter~\ref{font});
\item the height, depth, width, and italic correction
of individual characters;
\item kerning tables;
\item ligature tables;
\item information regarding successors and extensions
of math characters (see Chapter~\ref{mathchar}).
\end{itemize}
Metrics files use a packed format, but they can be converted
to and from a readable format by the auxiliary programs
\n{tftopl} and \n{pltotf} (see~\cite{K:Fuchs}).
Here \n{pl} stands for `property list',
a term deriving from the programming language Lisp.
Files in \n{pl} format are just text, so they can easily be edited;
after conversion
they can then again be used as \n{tfm} files.
%\spoint[virtual:fonts] Virtual fonts
\subsection{Virtual fonts}
\label{virtual:fonts}
With `virtual fonts' (see~\cite{K:virt}) it is possible that
\term virtual fonts\par
what looks like one font to \TeX\ resides in more than
one physical font file. Also, virtual fonts can be used
to change in effect the internal organization of font files.
For \TeX\ itself, the
presence of virtual fonts makes no difference: everything
is still based on \n{tfm} files containing metric
information. However, the screen or printer driver that displays
the resulting \n{dvi} file on the screen or on a printer
will search for files with extension \n{.vf} to determine
how characters are to be interpreted.
The \n{vf} file can, for instance, instruct the driver
to interpret a character as a certain position
in a certain font file, to interpret a character as more
than one position (a~way of forming accented characters),
or to include \cs{special} information (for
instance to set gray levels).
Readable variants of \n{vf} files have extension \n{vpl},
analogous to the \n{pl} files for the \n{tfm} files; see above.
Conversion between \n{vf} and \n{vpl} files can be
performed with the \n{vftovp} and \n{vptovf} programs.
However, because virtual fonts are a matter for
\term device drivers\par
device drivers, no more details will be given in this book.
%\spoint Font files
\subsection{Font files}
Character descriptions are stored in three types of files.
\term font files\par
\begin{description} \item [gf]
Generic Font files.
This is the file type that the Metafont program generates.
There are not many previewers or printer drivers that use
this type of file directly.
\item [pxl]
Pixel files. The \n{pxl} format is a pure bitmap format.
Thus it is easy to generate \n{pxl} files from, for instance,
scanner images.
This format should be superseded by the \n{pk} format.
Pixel files can become rather big,
as their size grows quadratically in the size of the characters.
\item [pk]
Packed files. Pixel files can be packed by a form of run-length
encoding: instead of storing the complete bitmap only the
starting positions and lengths of `runs' of black and white
pixels are stored. This makes the size of \n{pk} files
approximately linear in the size of the characters.
However, a previewer or printer driver using a packed font file
has to unpack it before it is able to use it.
\end{description}
The following conversion programs exist:
\n{gftopxl}, \n{gftopk}, \n{pktopxl}, \n{pxltopk}.
%\spoint Computer Modern
\subsection{Computer Modern}
The only family of typefaces that comes with \TeX\
in the standard distribution is the `Computer
\term Computer Modern typefaces\par
Modern' family. This is an adaptation (using the terminology
of~\cite{S}) by Donald Knuth of the Monotype Modern~8A typeface
that was used for the first volume of his {\italic Art of Computer
Programming\/} series. The `modern faces' all derive from the
types that were cut between 1780 and 1800 by Firmin Didot in
France, Giambattista Bodoni in Italy, and Justus Erich Walbaum
in Germany. After the first two, these types are also called
`Didone' types. This name was coined in the Vox classification
of types \cite{Vox}. Ultimately, the inspiration for the Didone
types is the `Romain du Roi', the type that was designed by
Nicolas Jaugeon around 1692 for the French Imprimerie Royale.
Didone types are characterized by a strong vertical orientation,
and thin hairlines. The vertical accent is strengthened by the
fact that the insides of curves are flattened.
The result is a clear and brilliant page, provided that the
printing is done carefully and on good quality paper.
\message{Reference format}
However, they are quite vulnerable; \cite{Up}
compares them to the distinguished but fragile furniture
from the same period, saying one is afraid to use either,
`for both seem in danger of breaking in pieces'.
With the current proliferation of low resolution (around
300 dot per inch) printers, the Computer Modern is
a somewhat unfortunate choice.
Recently, Donald Knuth has developed
a new typeface (or rather, a subfamily of typefaces)
by changing parameters
in the Computer Modern family. The result is a so-called
`Egyptian' typeface: Computer Concrete \cite{K:cc}.
The name derives from the
fact that it was intended primarily for the book {\italic Concrete
Mathematics}. Egyptian typefaces (they fall under the `M\'ecanes'
in the Vox classification, meaning constructed,
not derived from written letters) have a very uniform line width
and square serifs. They do not have anything to do with Egypt;
such types happened to be popular in the first half of the nineteenth
century when Egyptology was developing and popular.
%\point \TeX\ and web
\section{\TeX\ and web}
The \TeX\ program is written in \web, a programming language
\term \web\par\term Pascal\par
that can be considered as a subset of Pascal, augmented with
a preprocessor.
\TeX\ makes no use of some features of Pascal, in order to
facilitate porting to Pascal systems other than the one
it was originally designed for, and even to enable automatic
translation to other programming languages such as~C.
For instance, it does not use the Pascal \n{With} construct.
Also, procedures do not have output parameters; apart from
writing to global variables, the only way
values are returned is through
\n{Function} values.
Actually, \web\ is more than a superset of a subset of Pascal
(and to be more precise, it can also be used with other
programming languages);
it is a `system of structured documentation'. This means that
the \web\ programmer writes pieces of program code,
interspersed with their documentation, in one file.
This idea of `literate programming' was
introduced in~\cite{K:literate};
for more information, see~\cite{Sewell}.
Two auxiliary programs,
Tangle and Weave, can then be used to strip the documentation
and convert \web\ into regular Pascal, or to convert the
\web\ file into a \TeX\ file that will typeset the program
and documentation.
Portability of \web\ programs is achieved by the `change file'
mechanism. A~change file is a list of changes to be made to
the \web\ file; a~bit like a stream editor script.
These changes can comprise both adaptations of the \web\ file
to the particular Pascal compiler that will be used, and
bug fixes to \TeX. Thus the \n{TeX.web} file need never be edited.
%\point The \TeX\ Users Group
\section{The \TeX\ Users Group}
\TeX\ users have joined into several users groups
\term TUG\par
over the last decade. Many national or language
users groups exist, and a lot of them publish newsletters.
The oldest of all \TeX\ users groups is simply called
that: the \TeX\ Users Group, or TUG,
and its journal is called {\italic TUGboat}\term TUGboat\par.
You can reach them at
\begin{disp} \TeX\ Users Group\nl P.O. Box 2311\nl
Portland, OR 97208-2311, USA
\end{disp}
or electronically at \n{office@tug.org} on the Internet.
%%%% end of input file [run]
%\InputFile:trace
%%%% this is input file [trace]
%\subject[trace] Tracing
\endofchapter
\chapter{Tracing}\label{trace}
\TeX's workings are often quite different from what
\term tracing\par\term statistics\par
the programmer expected, so there are ways to discover how \TeX\
arrived at the result it did. The \cs{tracing...}
commands write
all information of a certain kind to the log file
(and to the terminal if \cs{tracingonline} is positive),
and a number of \cs{show...} commands can be used to ask the
current status or value of various items of \TeX.
In the following list, only \cs{show} and \cs{showthe}
display their output on the terminal by default,
other \cs{show...} and \cs{tracing...} commands
write to the log file. They will write in addition to
the terminal if \cs{tracingonline} is positive.
\begin{inventory}
\item [\cs{meaning}]
Give the meaning of a control sequence as a string of characters.
\item [\cs{show}]
Display the meaning of a control sequence.
\item [\cs{showthe}]
Display the result of prefixing a token with \cs{the}.
\item [\cs{showbox}]
Display the contents of a box.
\item [\cs{showlists}]
Display
the contents of the partial lists
currently built in all modes.
This is treated on
page~\pageref{showlists}.
\item [\csidx{tracingcommands}]
If this is~1 \TeX\ displays primitive commands executed;
if this is 2~or more the outcome of conditionals is also recorded.
\item [\csidx{tracingmacros}]
If this is~1, \TeX\ shows expansion of macros
that are performed and the actual values of the arguments;
if this is 2~or more \gr{token parameter}s such as
\cs{output} and \cs{everypar} are also traced.
\item [\cs{tracingoutput}]
If this is positive, the log file shows a dump of boxes
that are shipped to the \n{dvi} file.
\item [\cs{showboxdepth}]
The number of levels of box dump that are shown when
boxes are displayed.
\item [\cs{showboxbreadth}]
Number of successive elements on each level that are shown when
boxes are displayed.
\item [\csidx{tracingonline}]
If this parameter is positive, \TeX\ will write trace
information to the terminal in addition to the log file.
\item [\cs{tracingparagraphs}]
If this parameter is positive, \TeX\ generates
a trace of the line breaking algorithm.
\item [\csidx{tracingpages}]
If this parameter is positive, \TeX\ generates
a trace of the page breaking algorithm.
\item [\csidx{tracinglostchars}]
If this parameter is positive, \TeX\ gives
diagnostic messages whenever a character is accessed that
is not present in a font.
Plain default:~1.
\item [\csidx{tracingrestores}]
If this parameter is positive, \TeX\ will report
all values that are restored when a group ends.
\item [\cs{tracingstats}]
If this parameter is~1, \TeX\ reports at the
end of the job the usage of various internal arrays;
if it is~2, the memory demands are given whenever
a page is shipped out.
\end{inventory}
%\point Meaning and content: \cs{show}, \cs{showthe}, \cs{meaning}
\section{Meaning and content: \protect\cs{show}, \protect\cs{showthe}, \protect\cs{meaning}}
The meaning of control sequences, and the contents of those
that represent internal quantities, can be obtained
by the primitive commands \cs{show}, \cs{showthe},
and~\cs{meaning}.
The control sequences \cs{show} and \cs{meaning} are similar:
\alt
the former will give
\cstoidx show\par\cstoidx meaning\par
output to the log file and the terminal, whereas the latter
will produce the same tokens, but they are placed in \TeX's
input stream.
The meaning of a primitive command of \TeX\ is that command itself:
\begin{verbatim}
\show\baselineskip
\end{verbatim}
gives
\begin{verbatim}
\baselineskip=\baselineskip
\end{verbatim}
The meaning of a defined quantity is its definition:
\begin{verbatim}
\show\pageno
\end{verbatim}
gives
\begin{verbatim}
\pageno=\count0
\end{verbatim}
The meaning of a macro is its parameter text and replacement text:
\begin{verbatim}
\def\foo#1?#2\par{\set{#1!}\set{#2?}}
\show\foo
\end{verbatim}
gives
\begin{verbatim}
\foo=macro:
#1?#2\par ->\set {#1!}\set {#2?}
\end{verbatim}
For macros without parameters the part before the arrow
(the parameter text) is empty.
The \csidx{showthe} command will display on the log file and terminal
the tokens that \cs{the} produces.
After \cs{show}, \cs{showthe}, \cs{showbox}, and \cs{showlists}
\TeX\ asks the user for input; this can be prevented
by specifying \cs{scrollmode}.
Characters generated
by \cs{meaning} and \cs{the} have category~12, except for spaces
(see page~\pageref{cat12});
the value of \cs{escapechar} is used when control sequences
are represented.
%\point Show boxes: \cs{showbox}, \cs{tracingoutput}
\section{Show boxes: \protect\cs{showbox}, \protect\cs{tracingoutput}}
If \cs{tracingoutput} is positive the log file will
\cstoidx tracingoutput\par\cstoidx showbox\par
receive a dumped representation of all boxes that are
written to the \n{dvi} file with \cs{shipout}.
The same representation is used
by the command \cs{showbox}\gr{8-bit number}.
In the first case \TeX\ will report `Completed box being shipped out';
in the second case it will enter \cs{errorstopmode}, and
tell the user `OK. (see the transcript file)'.
If \cs{tracingonline} is positive, the box is also displayed
on the terminal; if \cs{scrollmode} has been specified,
\TeX\ does not stop for input.
The upper bound on the
number of nested boxes that is dumped is \cs{showboxdepth};
\cstoidx showboxdepth\par\cstoidx showboxbreadth\par
each time a level is visited at most \cs{showboxbreadth}
items are shown, the remainder of the list is summarized
with~\n{etc.}
For each box its height, depth, and width
are indicated in that order, and for characters it is
stated from what font they were taken.
\begin{example} After
\begin{verbatim}
\font\tenroman=cmr10 \tenroman
\setbox0=\hbox{g}
\showbox0
\end{verbatim}
the log file will show
\begin{verbatim}
\hbox(4.30554+1.94444)x5.00002
.\tenroman g
\end{verbatim}
indicating that the box was \n{4.30554pt} high,
\n{1.94444pt} deep, and \n{5.00002pt} wide, and that it contained
a character `g' from the font \cs{tenroman}.
Note that the fifth decimal of all sizes may be rounded
because \TeX\ works with multiples of $2^{-16}$\n{pt}.
\message{ifmath: scriptfont fam0 fill!}
\end{example}
The next example has nested boxes,
\begin{verbatim}
\vbox{\hbox{g}\hbox{o}}
\end{verbatim}
and it contains \cs{baselineskip} glue between the boxes.
After a \cs{showbox} command the log file output is:
\begin{verbatim}
\vbox(16.30554+0.0)x5.00002
.\hbox(4.30554+1.94444)x5.00002
..\tenroman g
.\glue(\baselineskip) 5.75002
.\hbox(4.30554+0.0)x5.00002
..\tenroman o
\end{verbatim}
Each time a new level is entered an extra dot is added to
the front of the line. Note that \TeX\ tells explicitly
that the glue is \cs{baselineskip} glue;
it inserts names like this for all automatically inserted glue.
The value of
the baselineskip glue here is such that the baselines of
the boxes are at 12 point distance.
Now let us look at explicit (user) glue. \TeX\ indicates the ratio
by which it is stretched or shrunk.
\begin{example}s
\begin{verbatim}
\hbox to 20pt {\kern10pt \hskip0pt plus 5pt}
\end{verbatim}
gives (indicating that the available stretch has been
multiplied by~\n{2.0}):
\begin{verbatim}
\hbox(0.0+0.0)x20.0, glue set 2.0
.\kern 10.0
.\glue 0.0 plus 5.0
\end{verbatim}
and
\begin{verbatim}
\hbox to 0pt {\kern10pt \hskip0pt minus 20pt}
\end{verbatim}
gives (the shrink has been multiplied by~\n{0.5})
\begin{verbatim}
\hbox(0.0+0.0)x0.0, glue set - 0.5
.\kern 10.0
.\glue 0.0 minus 20.0
\end{verbatim}
respectively.
\end{example}
This is an example with infinitely stretchable or shrinkable
glue:
\begin{verbatim}
\hbox(4.00000+0.14000)x15.0, glue set 9.00000fil
\end{verbatim}
This means that the horizontal box contained \n{fil} glue, and
it was set such that its resulting width was \n{9pt}.
Underfull boxes are dumped like all other boxes, but
the usual `\n{Underfull hbox detected at line...}'
is given. Overfull horizontal boxes contain a vertical rule
of width \cs{overfullrule}:
\begin{verbatim}
\hbox to 5pt {\kern10pt}
\end{verbatim}
gives
\begin{verbatim}
\hbox(0.0+0.0)x5.0
.\kern 10.0
.\rule(*+*)x5.0
\end{verbatim}
Box leaders are not dumped completely:
\begin{verbatim}
.\leaders 40.0
..\hbox(4.77313+0.14581)x15.0, glue set 9.76852fil
...\tenrm a
...\glue 0.0 plus 1.0fil
\end{verbatim}
is the dump for
\begin{verbatim}
\leaders\hbox to 15pt{\tenrm a\hfil}\hskip 40pt
\end{verbatim}
Preceding or trailing glue around the leader
boxes is also not indicated.
%\point Global statistics
\section{Global statistics}
The parameter \csidx{tracingstats} can be used to force \TeX\
to report at the end of the job the global use of resources.
Some production versions of \TeX\ may not have this option.
As an example, here are the statistics for this book:
\begin{verbatim}
Here is how much of TeX's memory you used:
\end{verbatim}
String memory (bounded by `pool size'):
\begin{verbatim}
877 strings out of 4649
9928 string characters out of 61781
\end{verbatim}
Main memory, control sequences, font memory:
\begin{verbatim}
53071 words of memory out of 262141
2528 multiletter control sequences out of 9500
20137 words of font info for 70 fonts,
out of 72000 for 255
\end{verbatim}
Hyphenation:
\begin{verbatim}
14 hyphenation exceptions out of 607
\end{verbatim}
Stacks: input, nest, parameter, buffer, and save stack respectively,
\begin{verbatim}
17i,6n,19p,245b,422s stack positions out of
300i,40n,60p,3000b,4000s
\end{verbatim}
% \begin{comment}
% \endinput
% %\point Line breaking: \cs{tracingparagraphs}
% \section{Line breaking: \cs{tracingparagraphs}}
% If \cs{tracingparagraphs} is positive, \TeX's line breaking
% \cstoidx tracingparagraphs\par
% algorithm will generate trace output. However, on some \TeX\
% implementations this trace mode may have been disabled to get a
% faster running system.
% Consider an example paragraph of \TeX:
% \begin{verbatim}
% \hsize=3in \parindent=0cm \frenchspacing
% \pretolerance=500
% This is a sample paragraph to show the trace output that
% \TeX's line breaking algorithm produces. Some \TeX\ systems
% cannot generate this trace, as the relevant piece of code
% has been commented out for speed optimisation.
% With ever faster computers this won't be necessary any more.
% \end{verbatim}
% \TeX\ first attempts to break the paragraph without
% hyphenation, and it will accept solutions where each
% line has a badness less than \cs{pretolerance}.
% \begin{verbatim}
% @firstpass
% \end{verbatim} Report that the first pass has started;
% \begin{verbatim}
% []\tenrm This is a sample paragraph to show the trace
% \end{verbatim}
% Apparently this is the only way to fill the first line;
% \begin{verbatim}
% @ via @@0 b=263 p=0 d=84529
% \end{verbatim} and doing so
% had badness~263, a zero penalty, and a resulting 84529
% demerit points.
% \begin{verbatim}
% @@1: line 1.0 t=84529 -> @@0
% \end{verbatim} Conclusion:
% breakpoint~1 (\verb>@@1>) occurs on line~1, and it makes the
% line `very loose' (indicated by the~\n{.0}),
% and the total demerits are
% 84529 if the previous breakpoint was number~0.
% The first pass is now aborted.
% \begin{verbatim}
% @secondpass
% []\tenrm This is a sam-ple para-graph to show the trace out-
% @\discretionary via @@0 b=2 p=50 d=2644
% @@1: line 1.2- t=2644 -> @@0
% \end{verbatim}
% With a very small badness of~2, but with 50 penalty points
% for breaking at a hyphen, this line is `decent'
% (indicated by the~\n{.2}), and the total of demerit points
% is~2644.
% The second line is also straighforward:
% \begin{verbatim}
% put that T[]X's line break-ing al-go-rithm pro-duces.
% @ via @@1 b=0 p=0 d=100
% @@2: line 2.2 t=2744 -> @@1
% \end{verbatim}
% The demerits now derive solely from the \cs{linepenalty},
% which is~10. Similarly the third line:
% \begin{verbatim}
% Some T[]X sys-tems can-not gen-er-ate this trace, as
% @ via @@2 b=1 p=0 d=121
% @@3: line 3.2 t=2865 -> @@2
% \end{verbatim}
% For the fourth line two possibilities exist:
% it can be set `loose' with 9409 demerit points
% \begin{verbatim}
% the rel-e-vant piece of code has been com-mented
% @ via @@3 b=87 p=0 d=9409
% @@4: line 4.1 t=12274 -> @@3
% \end{verbatim}
% or, fitting in an extra word, it can be set `tight' with
% 2601 demerit points
% \begin{verbatim}
% out
% @ via @@3 b=41 p=0 d=2601
% @@5: line 4.3 t=5466 -> @@3
% \end{verbatim}
% Line 5 can be set in three ways:
% coming from breakpoint~4 it can be broken as
% \begin{verbatim}
% for speed op-ti-mi-sa-tion. With ever faster com-
% @\discretionary via @@4 b=0 p=50 d=2600
% @@6: line 5.2- t=14874 -> @@4
% \end{verbatim}
% and coming from breakpoint~5 there are two ways:
% \begin{verbatim}
% put-
% @\discretionary via @@5 b=2 p=50 d=2644
% @@7: line 5.2- t=8110 -> @@5
% \end{verbatim}
% and \begin{verbatim}
% ers
% @ via @@5 b=84 p=0 d=8836
% @@8: line 5.3 t=14302 -> @@5
% \end{verbatim}
% Of the three, the last possibility is the only one that
% does not involve hyphenating line~5.
% As line 6 is the last line of the paragraph, coming from
% breakpoints 6 or~7 gives an extra 5000 demerit points
% from the \cs{finalhyphendemerits}.
% \begin{verbatim}
% this won't be nec-es-sary any more.
% @\par via @@6 b=0 p=-10000 d=5100
% @\par via @@7 b=0 p=-10000 d=5100
% @\par via @@8 b=0 p=-10000 d=100
% @@9: line 6.2- t=13210 -> @@7
% \end{verbatim}
% However, coming from breakpoint 7 still gives the least
% demerits.
% \end{comment}
%%%% end of input file [trace]
%\InputFile:errors
%%%% this is input file [errors]
%\subject[error] Errors, Catastrophes, \nl and Help
\endofchapter
\chapter{Errors, Catastrophes, and Help}\label{error}
When \TeX\ is running, various errors can occur.
This chapter treats how errors in the input are displayed,
and what sort of overflow of internal data structures
of \TeX\ can occur.
\begin{inventory}
\item [\cs{errorcontextlines}]
(\TeX3 only)
Number of additional context lines shown in error messages.
\item [\cs{errmessage}]
Report an error, giving the parameter of this command as message.
\item [\cs{errhelp}]
Tokens that will be displayed if the user
asks further help after an \cs{errmessage}.
\end{inventory}
%\point Error messages
\section{Error messages}
When \TeX\ is running in \cs{errorstopmode} (which it usually is;
see Chapter~\ref{run} for the other running modes),
errors occurring are reported on the user terminal, and \TeX\
asks the user for further instructions.
Errors can occur either because of some internal condition
of \TeX, or because a macro has issued an \csidx{errmessage}
command.
If an error occurs \TeX\ shows the input
\term error patching\par
line
on which the error occurred. If the offending command was
not on that line but, for instance, in a macro that was
called \ldash possibly indirectly \rdash from that line,
the line of that command is also shown.
If the offending command was indirectly called,
an additional \csidx{errorcontextlines} number of lines
is shown with the preceding macro calls.
A~value of \cs{errorcontextlines}${}=0$ causes \n{...}
to be printed as the sole indication that there is a context.
Negative values inhibit even this.
For each macro in the sequence that leads to the offending
command,
\TeX\ attempts to display some
preceding and some following tokens.
First one line is displayed ending with
the \ldash indirectly \rdash offending command; then, one line lower
some following tokens are given.
\begin{example}
\begin{verbatim}
This paragraph ends \vship1cm with a skip.
\end{verbatim}
gives
\begin{verbatim}
! Undefined control sequence.
l.1 This paragraph ends \vship
1cm with a skip.
\end{verbatim}
\end{example}
If \TeX\ is not running in some non-stop mode\label{interaction},
the user is given the chance to patch errors or to
ask for further information. In general the following
options are available:\begin{description}\item [\gr{return}]
\TeX\ will continue processing. If the error was something
innocent that \TeX\ could either ignore or patch itself,
this is the easy way out.
\item [\n h]
Give further details about the error.
If the error was caused by an \cs{err\-message} command,
the \csidx{errhelp} tokens will be displayed here.
\item [\n i]
Insert. The user can insert some material. For example,
if a control sequence is misspelled, the correct command can
sometimes be inserted, as \begin{verbatim}
i\vskip
\end{verbatim}
for the above
example. Also, this is an opportunity for inserting
\cs{show} commands to inspect \TeX's internal state.
However, if \TeX\ is in the middle of
scanning something complicated,
such commands will not be executed, or will even
add to the confusion.
\item [\n s]
(\cs{scrollmode})
Scroll further errors, but display the messages.
\TeX\ will patch any further errors.
This is a handy option, for instance if the error occurs
in an alignment, because the number of subsequent errors tends
to be rather large.
\item [\n r]
(\cs{nonstopmode})
Run without stopping. \TeX\ will never stop for user interaction.
\item [\n q]
(\cs{batchmode})
Quiet running. \TeX\ will never stop for user interaction,
and does not give any more terminal output.
\item [\n x]
Exit. Abort this run of \TeX.
\item [\n e]
Edit. This option is not available on all \TeX\ system.
If it is, the run of \TeX\ is aborted, and an editor is
started, opening with the input file, maybe even
on the offending line.
\end{description}
%\point Overflow errors
\section{Overflow errors}
Harsh reality imposes some restrictions on how elaborate
\term overflow errors\par
\TeX's workings can get. Some restrictions are imposed by
compile-time constants, and are therefore fairly loose, but
some depend strongly on the actual computer implementation.
Here follows the list of all categories of overflow that
prompt \TeX\ to report `Capacity exceeded'.
Most bounds involved are (determined by) compile-time
constants; their values given here in parentheses are those
used in the source listing of \TeX\ in~\cite{Knuth:TeXbook}.
Actual values may differ, and probably will. Remember
that \TeX\ was developed in the good old days when even
big computers were fairly small.
%\spoint Buffer size {\rm(500)}
\subsection{Buffer size {\rm(500)}}
Current lines of all files that are open are kept in
\TeX's input buffer, as are control sequence names
that are being built with \verb-\csname...\endcsname-.
%\spoint Exception dictionary {\rm(307)}
\subsection{Exception dictionary {\rm(307)}}
The maximum number of hyphenation exceptions specified
by \cs{hyphenation} must be a prime number.
Two arrays with this many halfwords are allocated.
Changing this number makes formats incompatible;
that is, \TeX\ can only use a format that was made by
an \IniTeX\ with the same value for this constant.
%\spoint Font memory (20$\,$000)
\subsection{Font memory (20$\,$000)}
Information about fonts is stored in an array of
memory words. This is easily overflowed by preloading too
many fonts in \IniTeX.
%\spoint Grouping levels
\subsection{Grouping levels}
The number of open groups should be recordable
in a quarter word. There is no compile-time constant corresponding
to this.
%\spoint Hash size {\rm(2100)}
\subsection{Hash size {\rm(2100)}}
Maximum number of control sequences. It is suggested that
this number should not exceed 10\% of the main memory size.
The values in \TeX\ and \IniTeX\ should agree; also the
\n{hash\_prime} values should agree.
This value is rather low; for macro packages that are more
elaborate than plain \TeX\ a value of about 3000 is more
realistic.
%\spoint Number of strings {\rm(3000)}
\subsection{Number of strings {\rm(3000)}}
The maximum number of strings must be recordable in a half word.
%\spoint Input stack size {\rm(200)}
\subsection{Input stack size {\rm(200)}}
For each input source an item is allocated on the input stack.
Typical input sources are input files (but their simultaneous
number is more limited; see below), and token lists
such as token variables, macro replacement texts, and
alignment templates. A~macro with `runaway recursion'
(for example, \verb>\def\mac{{\mac}}>)
will overflow this stack.
\TeX\ performs some optimization here: before the last call
in a token list all token lists ending with this call are
cleared. This process is
similar to `resolving tail recursion' (see Chapter~\ref{macro}).
%\spoint Main memory size (30$\,$000)
\subsection{Main memory size (30$\,$000)}
Almost all `dynamic' objects of \TeX, such as macro definition
texts and all material on the current page,
are stored in the main memory array.
Formats may already take $20\,000$ words of
main memory for macro definitions, and complicated pages containing
for instance the \LaTeX\ picture environment may easily
overflow this array.
\TeX's main memory is divided in words, and a half word
is supposed to be able to address the whole of the memory.
Thus on current 32-bit computers the most common choice
is to let the main memory size be at most 64K bytes.
A~half word address can then be stored in 16 bits,
half a machine word.
However, so-called `Big \TeX' implementations exist
\term big \TeX\par
that have a main memory larger than 64K words.
Most compilers will then allocate 32-bit words for
addressing this memory, even if (say) 18 bits would
suffice. Big \TeX s therefore become immediately
a lot bigger when they cross the 64K threshold.
Thus they are usually not found on microcomputers,
although virtual memory schemes for these are possible;
see for instance~\cite{Thull}.
\TeX\ can have a bigger main memory than \IniTeX;
see Chapter~\ref{TeXcomm} for further details.
%\spoint Parameter stack size {\rm(60)}
\subsection{Parameter stack size {\rm(60)}}
Macro parameters may contain macro calls with
further parameters. The number of parameters that may occur
nested is bounded by the parameter stack size.
%\spoint Pattern memory {\rm(8000)}
\subsection{Pattern memory {\rm(8000)}}
Hyphenation patterns are stored in a trie array.
The default size of 8000 hyphenation patterns seems sufficient
for English or Italian, for example, but it is not for
Dutch or German.
%\spoint Pattern memory ops per language
\subsection{Pattern memory ops per language}
The number of hyphenation ops (see the literature about
hyphenation: \cite{Liang} and appendix~H of~\cite{Knuth:TeXbook})
should be recordable
in a quarter word. There is no compile-time constant corresponding
to this. \TeX\ version~2 had the same upper bound, but gave no
error message in case of overflow. Again, for languages such
as Dutch and German this bound is too low.
There are versions of \TeX\ that have a higher bound here.
%\spoint Pool size (32$\,$000)
\subsection{Pool size (32$\,$000)}
Strings are error messages and control sequence names.
They are stored using one byte per character.
\TeX\ has initially about $23\,000$ characters worth of
strings.
The pool will overflow if a user defines a large number of
control sequences on top of a substantial macro package.
However, even if the user does not define any new commands
\mdqon
overflow may occur: cross"-referencing schemes also
\mdqoff
work by defining control sequences. For large documents
a pool size of $40\,000$ or $60\,000$ is probably sufficient.
%\spoint Save size {\rm(600)}
\subsection{Save size {\rm(600)}}
Quantities that are assigned to inside a group must be
restored after the end of that group.
The save stack is where the values to be restored are kept;
the size of the
save stack limits the number of values that can be restored.
Alternating global and local assignments to a value
will lead to `save stack build-up': for each local
assignment following a global assignment the
previous value of the variable is saved. Thus an
alternation of such assignments will lead to
an unnecessary proliferation of items on the save stack.
%\spoint Semantic nest size {\rm(40)}
\subsection{Semantic nest size {\rm(40)}}
Each time \TeX\ switches to a mode nested inside another
mode (for instance when processing an \verb-\hbox- inside
a \verb-\vbox-) the current state is pushed on the
semantic nest stack. The semantic nest size is the maximum
number of levels that can be pushed.
%\spoint Text input levels {\rm(6)}
\subsection{Text input levels {\rm(6)}}
The number of nested \verb-\input- files
has to be very limited,
as the current lines are all kept in the input buffer.
%%%% end of input file [errors]
%\InputFile:syntax
%%%% this is input file [syntax]
%\subject[gramm] The Grammar of \TeX
\endofchapter
\chapter{The Grammar of \TeX}\label{gramm}
Many chapters in this book contain pieces of the
grammar that defines the formal syntax of \TeX.
In this chapter the structure of the rewriting rules of the
grammar is explained, and some key notions are presented.
In \TeXbook\ a grammar appears in Chapters~24--27.
An even more rigorous grammar of \TeX\ can be found in~\cite{Appelt}.
The grammar presented in this book is virtually identical
to that of \TeXbook.
%\point Notations
\section{Notations}
Basic to the grammar are \begin{description}\item [grammatical terms]
These are enclosed in angle brackets:\begin{disp}\gr{term}\end{disp}
\item [control sequences]
These are given in typewriter type with a backslash for
the escape character:\begin{disp}\cs{command}\end{disp}
\end{description}
Lastly there are \begin{description}\item [keywords]
Also given in typewriter type\begin{disp}\n{keyword}\end{disp}
This is a limited collection of words that have a special
meaning for \TeX\ in certain contexts; see below.\end{description}
The three elements of the grammar are used in syntax rules:
\begin{disp}\gr{snark} $\longrightarrow$ \n{boojum} $|$ \gr{empty}
\end{disp}
This rule says that the grammatical entity \gr{snark}
is either the keyword \n{boojum}, or the grammatical
entity \gr{empty}.
There are two other notational conventions.
The first is that the double quote
is used to indicate hexadecimal (base~16) notation.
For instance \verb>"ab56> stands for $10\times16^3+11\times16^2
+5\times16^1+6\times16^0$. The second convention
is that subscripts are used to denote category codes.
Thus \n{a}$_{12}$ denotes an `a' of category~12.
%\point[keywords] Keywords
\section{Keywords}
\label{keywords}
A keyword is sequence of characters (or character tokens)
\term keywords\par
of any category code but~13 (active).
Unlike the situation in control sequences, \TeX\ does not
distinguish between lowercase and uppercase characters
in keywords. Uppercase characters in keywords are converted to
lowercase by adding 32 to them; the \cs{lccode} and \cs{uccode}
are not used here. Furthermore, any keyword can be preceded by
optional spaces.
Thus both \n{true cm} and \n{truecm} are legal.
By far the strangest example, however, is provided
by the grammar rule
\begin{disp}\gr{fil unit} $\longrightarrow$ \n{fil} $|$ \gr{fil unit}\n l
\end{disp} which implies that \hbox{\n{fil L l}} is also
a legal \gr{fil dimen}. Strange errors can ensue from this;
see page~\pageref{fil:l:l} for an example.
Here is the full list of all keywords: \n{at}, \n{bp},
\n{by}, \n{cc}, \n{cm}, \n{dd}, \n{depth}, \n{em}, \n{ex},
\n{fil}, \n{height}, \n{in}, \n l, \n{minus}, \n{mm}, \n{mu},
\n{pc}, \n{plus}, \n{pt}, \n{scaled}, \n{sp}, \n{spread},
\n{to}, \n{true}, \n{width}.
%\point Specific grammatical terms
\section{Specific grammatical terms}
Some grammatical terms appear in a lot of rules.
One such term is \gr{optional spaces}. It is probably
\term space, optional\par
clear what is meant, but here is the formal definition:
\begin{disp}\gr{optional spaces} $\longrightarrow$
\gr{empty} $|$ \gr{space token}\gr{optional spaces}
\end{disp}
which amounts to saying that \gr{optional spaces}
is zero or more space tokens.
Other terms may not be so immediately obvious.
Below are some of them.
%\spoint \gr{equals}
\subsection{\gr{equals}}
In assignments the equals sign is optional; therefore there
is a term
\begin{disp}\gr{equals} $\longrightarrow$ \gr{optional spaces}
$|$ \gr{optional spaces}$=_{12}$\end{disp}
in \TeX's grammar.
%% \begin{comment}
%% One assignment exists where the equals sign cannot
%% be left out:
%% \begin{verbatim}
%% \let\spacetoken= %assign a space
%% \end{verbatim}
%% Here the space would have been skipped in \TeX's input processor
%% if the equals sign had been left out.
%% \end{comment}
%\spoint \gr{filler}, \gr{general text}
\subsection{\gr{filler}, \gr{general text}}
More obscure than the \gr{optional spaces} is the combination
of spaces and \cs{relax} tokens that is allowed
in some places, for instance
\begin{verbatim}
\setbox0= \relax\box1
\end{verbatim}
The quantity involved is
\begin{disp}\gr{filler} $\longrightarrow$ \gr{optional spaces}
$|$ \gr{filler}\cs{relax}\gr{optional spaces}\end{disp}
One important occurrence of \gr{filler} is in
\begin{disp}\gr{general text} $\longrightarrow$
\gr{filler}\lb\gr{balanced text}\gr{right brace}
\end{disp}
A \gr{general text} follows such control sequences as
\cs{message}, \cs{uppercase}, or \cs{mark}. The braces around
the \gr{balanced text} are explained in the next point.
%\spoint \lb\rb\ and \gr{left brace}\gr{right brace}
\subsection{\lb\rb\ and \gr{left brace}\gr{right brace}}
The \TeX\ grammar uses a perhaps somewhat unfortunate
convention for braces. First of all \begin{disp}\lb\ and \rb\end{disp}
stand for braces that are either explicit open/close group
characters, or control sequences defined by \cs{let},
such as \begin{verbatim}
\let\bgroup={ \let\egroup=}
\end{verbatim}
The grammatical terms \begin{disp}\gr{left brace} and \gr{right brace}
\end{disp} stand for explicit open/close group characters,
that is, characters of categories 1 and~2 respectively.
Various combinations of these two kinds of braces exist.
Braces around boxes can be implicit:
\begin{disp}\cs{hbox}\gr{box specification}\lb
\gr{horizontal mode material}\rb\end{disp}
Around a macro definition there must be explicit braces:
\begin{disp}\gr{definition text} $\longrightarrow$
\gr{parameter text}\gr{left brace}\gr{balanced text}\gr{right brace}
\end{disp}
Finally, the \gr{general text} that was mentioned above
has to be explicitly closed, but it can be implicitly opened:
\begin{disp}\gr{general text} $\longrightarrow$
\gr{filler}\lb\gr{balanced text}\gr{right brace}
\end{disp}
The closing brace of a \gr{general text} has to be explicit,
since a general text is a token list, which may
contain \cs{egroup} tokens.
\TeX\ performs expansion to find the opening
brace of a \gr{general text}.
%\spoint \gr{math field}
\subsection{\gr{math field}}
In math mode various operations such as subscripting
or applying \cs{underline} take an argument that
is a \gr{math field}: either a single symbol, or
a group. Here is the exact definition.
\begin{disp}\gr{math field} $\longrightarrow$
\gr{math symbol} $|$ \gr{filler}\lb\gr{math mode material}\rb\nl
\gr{math symbol} $\longrightarrow$ \gr{character} $|$
\gr{math character}
\end{disp}
See page~\pageref{character} for \gr{character},
\alt
and page~\pageref{math:character} for \gr{math character}.
%\point[2vs3] Differences between \TeX\ versions 2 and 3
\section{Differences between \TeX\ versions 2 and 3}
\label{2vs3}
In 1989 Knuth released \TeX\ version~3.0, which is
\term \protect\TeX\ version 2\par
the first real change in \TeX\ since version~2.0,
which was released in~1986 (version~0 of \TeX\ was
released in 1982; see~\cite{Knuth:TeXerrors} for more about
the history of \TeX).
All intermediate versions were merely bug fixes.
The main difference between versions 2~and~3 lies
in the fact that 8-bit input has become possible.
Associated with this, various quantities that
used to be 127 or~128 have been raised to 255
or~256 respectively. Here is a short list.
The full description is in~\cite{K:TeX23}.
\begin{itemize}\message{Remove other TeX3 refernces!}
\item All `codes' (\cs{catcode}, \cs{sfcode}, et cetera;
see page~\pageref{codename})
now apply to 256 character codes instead of~128.
\item A character with code \cs{endlinechar}
is appended to the line unless this parameter is negative
or more than~255 (this was~127) (see page~\pageref{append:elc}).
\item No escape character is output by \cs{write} and
other commands if \cs{escapechar} is negative or more than~255
(this was~127) (see page~\pageref{use:escape}).
\item The \verb>^^> replacement mechanism has been extended
(see page~\pageref{hathat}).
\item Parameters \cs{language}, \cs{inputlineno},
\cs{errorcontextlines}, \cs{lefthyphenmin}, \cs{righthyphenmin},
\cs{badness}, \cs{holdinginserts}, \cs{emergencystretch},
and commands \cs{noboundary}, \cs{setlanguage}
have been added.
\item The value of \cs{outputpenalty} is no longer zero
\alt
if the page break was not at a penalty item;
instead it is~$10\,000$ (see page~\pageref{break:penalty}).
\end{itemize}
The plain format has also been updated, mostly
\alt
with default settings for parameters such as
\cs{lefthyphenmin}, but also a few macros have been added.
\endofchapter
%%%% end of input file [syntax]
\chapter{Glossary of \TeX\ Primitives}
\begin{raggedright}
\input glossary
\end{raggedright}
\endofchapter
\chapter{Tables}\label{table}
\pagestyle{plain}
\clearpage
\input tables
\endofchapter
%\chapter{Index}
\printindex
%\input \jobname.ind
%\endofchapter % this winds up on a page of its own.
%\chapter{References}
\mdqon
\bibliography{tex}
\bibliographystyle{plain}
\mdqoff
\endofchapter
%%%% end of input file [tables]
\chapter*{Change log}
\section*{Version 1.1}
Small remark about \cs{afterassignment} after macro definitions.
Trouble with indexing macros fixed, I hope.
Separate letter and a4 versions.
Better intro for the chapter \ref{space} on spacing.
\end{document}
\HasNum:no \ToVerso
\asubject List of Examples\par
\message{set a counter here!}%\SetCounter:point=0
\def\subjectTitle{List of Examples}
\makeatletter\refresh@mark@item{subjectTitle}{LIST OF EXAMPLES}
\makeatother
\LoadExternalFile:todo
\EjectPage
\asubject Index by Command\par
\def\subjectTitle{INDEX BY COMMAND}
\makeatletter\refresh@mark@item{subjectTitle}{INDEX BY COMMAND}
\makeatother
\GutterWidth=1.5pc \NumberOfColumns:3
\LoadExternalFile:index
\EjectPage
\begin{comment}
\message{set a counter here!}%\SetCounter:Page=306 \CounterRepresentation:Page=1
\HasNum:no
\SerifFont %\pointsize:9 \Style:roman
\section{size:9 \Style:roman}
\flushright:no \hyphenpenalty=-50
\asubject Index by Topic\par
\def\subjectTitle{INDEX BY TOPIC}
\makeatletter\refresh@mark@item{subjectTitle}{INDEX BY TOPIC}
\makeatother
\GutterWidth=1.5pc \NumberOfColumns:3
%\GutterWidth=2pc \NumberOfColumns:2
\message{reg: medskips, \string\n{..}, balance}
\SetListIndent:1=2em
\SetListIndent:2=1em
\LoadExternalFile:register
\Stop
|