-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathappendixD.tex
executable file
·2357 lines (2263 loc) · 112 KB
/
appendixD.tex
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
% -*- coding: utf-8 -*-
\input macros
%\beginchapter Appendix D. Dirty Tricks
\beginchapter Appendix D. 诡计多端
\TeX\ was designed to do the ordinary tasks of typesetting: to make paragraphs
and pages. But the underlying mechanisms that facilitate ordinary
typesetting---e.g., boxes, glue, penalties, and macros---are extremely
versatile; hence people have discovered sneaky ways to coerce \TeX\ into
doing tricks quite different from what its author originally had in mind.
Such clever constructions are not generally regarded as examples of
``high \TeX''; but many of them have turned out to be useful and
instructive, worthy of being known (at least by a few wizards). The purpose
of this appendix is to introduce crafty and/or courageous readers
to the nether world of \TeX arcana.
\ninepoint\medskip
\setbox0=\hbox spread-6\fontdimen4\font % that removes all the shrinkability
{\strut Please don't read this material until you've}
\setbox1=\hbox to\wd0{\strut had plenty of experience with plain \TeX.}
\line{\leaders\hbox{\dbend\kern2pt}\hfil\vtop{\box0\box1}}
\nointerlineskip
\noindent\strut After you have read and understood the secrets below, you'll
know all sorts of devious combinations of \TeX\ commands,
and you will often be tempted to write inscrutable macros. Always remember,
however, that there's usually a simpler and better way to do something
than the first way that pops into your head. You may not have to
resort to any subterfuge at all, since \TeX\ is able to do lots of things
in a straightforward way. Try for simple solutions first.
%\subsection Macro madness. If you need to write complicated ^{macros}, you'll
%need to be familiar with the fine points in Chapter~20. \TeX's control
%sequences are divided into two main categories, ``expandable'' and
%``unexpandable''; the former category includes all macros and |\if...\fi|
%tests, as well as special operations like |\the| and |\input|, while the
%latter category includes the primitive commands listed in Chapter~24.
%The expansion of expandable tokens takes place in \TeX's ``^{mouth},''
%but primitive commands (including assignments) are done in \TeX's
%``^{stomach}.'' One important consequence of this structure is that
%it is impossible to redefine a control sequence or to advance a register
%while \TeX\ is expanding the token list of, say, a |\message| or |\write|
%command; assignment operations are done only when \TeX\ is building a
%vertical or horizontal or math list.
\subsection 疯狂的宏. If you need to write complicated ^{macros}, you'll
need to be familiar with the fine points in Chapter~20. \TeX's control
sequences are divided into two main categories, ``expandable'' and
``unexpandable''; the former category includes all macros and |\if...\fi|
tests, as well as special operations like |\the| and |\input|, while the
latter category includes the primitive commands listed in Chapter~24.
The expansion of expandable tokens takes place in \TeX's ``^{mouth},''
but primitive commands (including assignments) are done in \TeX's
``^{stomach}.'' One important consequence of this structure is that
it is impossible to redefine a control sequence or to advance a register
while \TeX\ is expanding the token list of, say, a |\message| or |\write|
command; assignment operations are done only when \TeX\ is building a
vertical or horizontal or math list.
For example, it's possible to put |\n| asterisks into a paragraph,
by saying simply `|{|^|\loop||\ifnum\n>0 *\advance\n-1 \repeat}|'.
But it's much more difficult to define a
control sequence |\asts| to consist of exactly |\n| consecutive asterisks.
If\/ |\n| were known to be at most~5, say, it would be possible to write
\begintt
\edef\asts{\ifcase\n\or*\or**\or***\or****\or*****\else\bad\fi}
\endtt
since \TeX\ handles |\ifcase| in its mouth. But for general |\n| it would
be impossible to use a construction like
`|\edef\asts{\loop\ifnum\n>0 *\advance\n-1 \repeat}|', since |\n| doesn't change
during an ^|\edef|. A more elaborate program is needed; e.g.,
\begintt
{\xdef\asts{}
\loop\ifnum\n>0 \xdef\asts{\asts*}\advance\n-1 \repeat}
\endtt
And here's another solution (which is faster, because ^{token list registers}
can be expanded more quickly than macros, using ^|\the|):
\begintt
\newcount\m \newtoks\t \m=\n \t={}
\loop \ifnum\m>0 \t=\expandafter{\the\t *} \advance\m-1 \repeat
\edef\asts{\the\t}
\endtt
However, both of these solutions have a running time proportional to the
square of\/~|\n|. There's a much quicker way to do the job:
\begintt
\begingroup\aftergroup\edef\aftergroup\asts\aftergroup{
\loop \ifnum\n>0 \aftergroup*\advance\n-1 \repeat
\aftergroup}\endgroup
\endtt
Get it? The ^|\aftergroup| commands cause a whole list of other tokens to be
saved up for after the group! This method has only one flaw, namely
that it takes up |\n| cells of space on \TeX's ^{input stack} and |\n|
more on \TeX's ^{save stack}; hence a special version of \TeX\ may be
required when |\n| is larger than 150 or~so.
(Incidentally, there's a completely different way to put |\n| asterisks
into a paragraph, namely to say `|\setbox0=\hbox{*}|%
^|\cleaders|^|\copy||0\hskip\n\wd0|'. This may seem to be the fastest
solution of all; but actually it is not so fast, when all things are
considered, since it generates four bytes of ^|dvi| output per asterisk,
compared to only one byte per asterisk in the other methods. Input/output
time takes longer than computation time, both in \TeX\ itself and in
the later stages of the printing process.)
The problem just solved may seem like a rather special application; after
all, who needs a control sequence that contains a variable number of
asterisks? But the same principles apply in other similar cases,
e.g., when you want to construct a variable-length ^|\parshape|
specification. Similarly, many of the ``toy problems'' solved below are
meant to illustrate paradigms that can be used in real-life situations.
The precise rules for expansion are explained in Chapter~20; and the
best way to get familiar with \TeX's expansion mechanism is to watch it
in action, looking at the log file when |\tracingmacros=2| and
|\tracingcommands=2|. One of the important ways to change the normal
order of expansion is to use ^|\expandafter|; the construction
\begintt
\expandafter\a\b
\endtt
causes |\b| to be expanded first, then~|\a|.
And since |\expandafter| is itself expandable, the construction
\begintt
\expandafter\expandafter\expandafter\a\expandafter\b\c
\endtt
causes |\c| to be expanded first, then |\b|, then |\a|. \ (The next step,
\begintt
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\a
\expandafter\expandafter\expandafter\b\expandafter\c\d
\endtt
is probably too lengthy to be of any use.)
It's possible to make good use of\/ |\expandafter\a\b| even when |\a|
isn't expandable. For example, the token list assignment
`|\t=\expandafter{\the\t|~|*}|' in the example on the previous page
was able to invade territory where expansion is normally suppressed,
by expanding after a left brace. Similarly,
\begintt
\t=\expandafter{\expandafter*\the\t}
\endtt
would have worked; and ^^|\uppercase|
\begintt
\uppercase\expandafter{\romannumeral\n}
\endtt
yields the value of register |\n| in ^{uppercase roman numerals}.
^^{roman numerals, uppercase}
Here's a more interesting example: Recall that ^|\fontdimen||1| is the
amount of ``^{slant} per point'' of a font; hence, for example,
`^|\the||\fontdimen1\tenit|' expands to `{\tt\the\fontdimen1\tenit}',
where the characters `|pt|' are of category~12. After the macro definitions
\begintt
{\catcode`p=12 \catcode`t=12 \gdef\\#1pt{#1}}
\let\getfactor=\\
\def\kslant#1{\kern\expandafter\getfactor\the\fontdimen1#1\ht0}
\endtt
one can write, e.g., `|\kslant\tenit|' and this will expand to
`|\kern0.25\ht0|'. If the boundary of\/ |\box0| is considered to be
slanted by 0.25 horizontal units per vertical unit, this kern measures
the horizontal distance by which the top edge of the box is skewed with
respect to an edge at the baseline. All of the computation of\/
|\kslant| is done in \TeX's mouth; thus, the mouth can do some rather
complicated things even though it cannot assign new values. \ (Incidentally,
an indirect method was used here to define the control sequence ^|\getfactor|
when the character~|t| had category~12, since control words normally consist
only of letters. The alternative construction
\begintt
{\catcode`p=12 \catcode`t=12
\csname expandafter\endcsname\gdef
\csname getfactor\endcsname#1pt{#1}}
\endtt
would also have worked, since ^|\csname| and ^|\endcsname| don't
contain `|p|' or `|t|'!)
The mechanism by which \TeX\ determines the ^{arguments} of a macro
can be applied in unexpected ways. Suppose, for example, that |\t|~is
a token list register that contains some text; we wish to determine if
at least one asterisk (|*|$_{12}$) appears in that text. Here's
one way to do it:
\begintt
\newif\ifresult % for the result of a computed test
\def\atest#1{\expandafter\a\the#1*\atest\a}
\long\def\a#1*#2#3\a{\ifx\atest#2\resultfalse\else\resulttrue\fi}
\endtt
^^|\long|^^|\newif|
Now after `|\atest\t|', the control sequence |\ifresult|
will be |\iftrue| or |\iffalse|, depending on whether or not |\t|~contains
an asterisk. \ (Do you see why?) \ And here's a slightly more elegant way
to do the same thing, using ^|\futurelet| to look ahead:
\begintt
\def\btest#1{\expandafter\b\the#1*\bb}
\long\def\b#1*{\futurelet\next\bb}
\long\def\bb#1\bb{\ifx\bb\next\resultfalse\else\resulttrue\fi}
\endtt
In both cases the solution works if\/ |\t| contains control sequence tokens
as well as character tokens, provided that the special control sequences
|\atest|, |\a|, and |\bb| don't appear. Notice, however, that an asterisk
is ``hidden'' if it appears within a ^{group}~|{...}|; the test is limited
to asterisks at nesting level zero. A token list register is always balanced
with respect to grouping, so there is no danger of the test leading
to error messages concerning missing braces or extra braces.
We can apply the ideas in the preceding paragraph to solve a problem
related to generalized math formatting: ^^{displays, non-centered}
The goal is to set \TeX\ up so that the respective constructions
`\thinspace|$$|$\,\alpha\,$|$$|\thinspace', ^^{dollar dollar}
`\thinspace|$$|$\,\alpha\,$^|\eqno|$\,\beta\,$|$$|\thinspace', and
`\thinspace|$$|$\,\alpha\,$^|\leqno|$\,\beta\,$|$$|\thinspace' will cause
a macro |$$\generaldisplay$$| to be invoked, with |\eq| defined to be $\alpha$;
^^{communication between macros}
furthermore, the test |\ifeqno| should be true when an equation
number~$\beta$ is present, and |\ifleqno| should be true in the case
of\/ |\leqno|. When $\beta$ is present, it should be stored in~|\eqn|.
Here $\alpha$~and~$\beta$ are arbitrary balanced token lists that don't
contain either |\eqno| or |\leqno| at nesting level zero. The
following macros do the required maneuvers:
\begintt
\newif\ifeqno \newif\ifleqno \everydisplay{\displaysetup}
\def\displaysetup#1$${\displaytest#1\eqno\eqno\displaytest}
\def\displaytest#1\eqno#2\eqno#3\displaytest{%
\if!#3!\ldisplaytest#1\leqno\leqno\ldisplaytest
\else\eqnotrue\leqnofalse\def\eqn{#2}\def\eq{#1}\fi
\generaldisplay$$}
\def\ldisplaytest#1\leqno#2\leqno#3\ldisplaytest{\def\eq{#1}%
\if!#3!\eqnofalse\else\eqnotrue\leqnotrue\def\eqn{#2}\fi}
\endtt
An examination of the three cases
|$$|$\,\alpha\,$|$$|,
|$$|$\,\alpha$|\eqno|$\,\beta\,$|$$|,
|$$|$\,\alpha$|\leqno|$\,\beta\,$|$$| shows that the correct actions will
ensue. Parameter |#3| in the tests `|\if!#3!|' will be either empty or
|\eqno| or |\leqno|; thus, the condition will be false (and the second
`|!|' will be skipped) unless |#3| is empty.
Returning to the problem of |*|'s in |\t|, suppose that it's
necessary to consider |*|'s at all levels of nesting. Then a slower
routine must be used:
\begintt
\def\ctest#1{\resultfalse\expandafter\c\the#1\ctest}
\def\c{\afterassignment\cc\let\next= }
\def\cc{\ifx\next\ctest \let\next\relax
\else\ifx\next*\resulttrue\fi\let\next\c\fi \next}
\endtt
Here ^|\afterassignment| has been used to retain control after a
non-future ^|\let|; the `|= |' ensures that exactly one token is ^^{equals}
swallowed per use of\/~|\c|. This routine could be modified in an obvious
way to count the total number of |*|'s and/or tokens in~|\t|. Notice the
`|\let\next|' instructions in~|\cc|; it should be clear why the alternative
\begintt
\def\cc{\ifx\next\ctest\else\ifx\next*\resulttrue\fi\c\fi}
\endtt
would not work. \ (The latter |\c| would always swallow a `|\fi|'.)
^{Space tokens} are sometimes anomalous, so they deserve special care. The
following macro ^|\futurenonspacelet| behaves essentially like
|\futurelet| except that it discards any implicit or explicit space tokens
that intervene before a nonspace is scanned:
\begintt
\def\futurenonspacelet#1{\def\cs{#1}%
\afterassignment\stepone\let\nexttoken= }
\def\\{\let\stoken= } \\ % now \stoken is a space token
\def\stepone{\expandafter\futurelet\cs\steptwo}
\def\steptwo{\expandafter\ifx\cs\stoken\let\next=\stepthree
\else\let\next=\nexttoken\fi \next}
\def\stepthree{\afterassignment\stepone\let\next= }
\endtt
An operation like |\futurenonspacelet| is useful, for example,
when implementing macros that have a variable number of arguments.
Notice that `|\def\stepthree#1{\stepone}|' would not work here, because of
\TeX's rule that a \]$_{10}$ token is bypassed if it would otherwise be treated
as an ^{undelimited} ^{argument}. Because of this rule it is
difficult to distinguish explicit space tokens from implicit ones.
The situation is surprisingly complex, because it's possible to
use ^|\uppercase| to create ``^{funny space}'' tokens like |*|$_{10}$;
^^|\uccode| for example, the commands
\begintt
\uccode` =`* \uppercase{\uppercase{\def\fspace{ }\let\ftoken= } }
\endtt
make |\fspace| a macro that expands to a funny space, and they make
|\ftoken| an implicit funny space. \ (The tests ^|\if||\fspace*|,
|\if\ftoken*|, ^|\ifcat||\fspace\stoken|, and |\ifcat\ftoken\stoken|
will all be true, assuming that |*| has category~12; but if |*| has
category~10, |\if\fspace*| will be false, because \TeX\ normalizes
all newly created space tokens to \]$_{10}$, as explained in Chapter~8.) \
Since the various forms of space tokens are almost identical in
^^|\noexpand|^^|\escapechar|^^|\string|
behavior, there's no point in dwelling on the details.\footnote\dag{The
following little program is for \TeX\ exegetes who insist on
learning the whole story: Macro |\stest| decides whether or not
the first token of a given token list register
is a \<space token> as defined in Chapter~24. If so, the macro
decides whether or not the token is ``funny,'' i.e., whether or not
the character code is different from an ASCII \<space>; the macro
also decides whether a space token is explicit or implicit.
\begintt|global|displayindent=0pt|global|displaywidth=|hsize
\newif\ifspace \newif\iffunny \newif\ifexplicit
\def\stest#1{\expandafter\s\the#1! \stest}
\def\s{\funnyfalse \global\explicitfalse \futurelet\next\ss}
\def\ss{\ifcat\noexpand\next\stoken \spacetrue
\ifx\next\stoken \let\next=\sss \else\let\next=\ssss \fi
\else \let\next=\sssss \fi \next}
\long\def\sss#1 #2\stest{\def\next{#1}%
\ifx\next\empty \global\explicittrue \fi}
\long\def\ssss#1#2\stest{\funnytrue {\uccode`#1=`~
\uppercase{\ifcat\noexpand#1}\noexpand~% active funny space
\else \escapechar=\if*#1`?\else`*\fi
\if#1\string#1\global\explicittrue\fi \fi}}
\long\def\sssss#1\stest{\spacefalse}
\endtt}
\smallbreak
The argument to ^|\write| is expanded when a |\shipout| occurs, but
sometimes expansion isn't desired. Here's a macro (suggested by Todd
^{Allen}) that suppresses all expansion, by inserting ^|\noexpand| before
each control sequence or ^{active character}. The macro assumes that
|~|~is an active character, and that the tokens being written
do not include implicit spaces or braces. Funny spaces are changed
to ordinary ones.
\begintt
\long\def\unexpandedwrite#1#2{\def\finwrite{\write#1}%
{\aftergroup\finwrite\aftergroup{\sanitize#2\endsanity}}}
\def\sanitize{\futurelet\next\sanswitch}
\def\sanswitch{\ifx\next\endsanity
\else\ifcat\noexpand\next\stoken\aftergroup\space\let\next=\eat
\else\ifcat\noexpand\next\bgroup\aftergroup{\let\next=\eat
\else\ifcat\noexpand\next\egroup\aftergroup}\let\next=\eat
\else\let\next=\copytoken\fi\fi\fi\fi \next}
\def\eat{\afterassignment\sanitize \let\next= }
\long\def\copytoken#1{\ifcat\noexpand#1\relax\aftergroup\noexpand
\else\ifcat\noexpand#1\noexpand~\aftergroup\noexpand\fi\fi
\aftergroup#1\sanitize}
\def\endsanity\endsanity{}
\endtt
As before, the heavy use of\/ ^|\aftergroup| in |\unexpandedwrite| means
that parameter |#2| should not include more than about 150~tokens.
%\subsection List macros. The next several macros we shall discuss can be
%used to maintain lists of information in the form ^^{backslash backslash}
%\begindisplay
%|\\{|\<item$_1$>|}\\{|\<item$_2$>|}| $\ldots$ |\\{|\<item$_n$>|}|
%\enddisplay
%where each \<item> is a balanced list of tokens. A parameterless control
%sequence whose replacement text has this form may be called a {\sl ^{list
%macro}}. The empty list macro has $n=0$ and it is called ^|\empty|.
\subsection 列表宏. The next several macros we shall discuss can be
used to maintain lists of information in the form ^^{backslash backslash}
\begindisplay
|\\{|\<item$_1$>|}\\{|\<item$_2$>|}| $\ldots$ |\\{|\<item$_n$>|}|
\enddisplay
where each \<item> is a balanced list of tokens. A parameterless control
sequence whose replacement text has this form may be called a {\sl ^{list
macro}}. The empty list macro has $n=0$ and it is called ^|\empty|.
It's easy to add new items at either end of a list macro, and to
concatenate list macros, for example as follows: ^^|\toksdef| ^^|\long|
\begintt
\toksdef\ta=0 \toksdef\tb=2 % token list registers for temp use
\long\def\leftappenditem#1\to#2{\ta={\\{#1}}\tb=\expandafter{#2}%
\edef#2{\the\ta\the\tb}}
\long\def\rightappenditem#1\to#2{\ta={\\{#1}}\tb=\expandafter{#2}%
\edef#2{\the\tb\the\ta}}
\def\concatenate#1=#2{\ta=\expandafter{#2}\tb=\expandafter{#3}%
\edef#1{\the\ta\the\tb}}
\endtt
Conversely, the left item of a list can be removed and placed in a
control sequence by the |\lop| macro defined in the following curious way:
\begintt
\def\lop#1\to#2{\expandafter\lopoff#1\lopoff#1#2}
\long\def\lopoff\\#1#2\lopoff#3#4{\def#4{#1}\def#3{#2}}
\endtt
For example, if\/ |\l| expands to the list `|\\{a\b}\\{c}\\{{d}}|', the
macro invocation |\lop\l\to\z| makes |\l| expand to `|\\{c}\\{{d}}|'
and |\z|~expand to `|a\b|'. The |\lop| operation should be used only when
|\l|~is nonempty, otherwise an error will occur; to test if\/ |\l|~is
empty, one simply says `|\ifx\l\empty|'.
The programming details of the |\lop| macro indicate why individual
items have been enclosed in |{...}| ^{groups}. A simpler kind of list,
in which grouping is omitted and an extra |\\| appears at the end,
suffices for many purposes; one could define, for instance,
\begintt
\long\def\lopoff\\#1\\#2\lopoff#3#4{\def#4{#1}\def#3{\\#2}}
\endtt
and the results would be almost the same as before. In this case an empty
list macro expands to `|\\|'. However, the new |\lop| resulting from this
new |\lopoff| macro also removes a pair
of braces, if the leftmost item happens to be a group; extra braces are
included in our general scheme to prevent such anomalies.
So far the examples we've considered haven't revealed why the |\\|'s
appear in the general scheme; it appears that grouping by itself
should be enough. But in fact, the |\\| separators are enormously
useful, because we can define |\\| to be any desired one-argument macro,
and then we can {\sl execute\/} the list! For example, here's a
way to count the number of items: ^^{hash hash}
\begintt
\def\cardinality#1\to#2{#2=0 \long\def\\##1{\advance#2 by1 }#1}
\endtt
(Parameter |#2| is supposed to be the name of a count register.) \
And here's a way to take a list macro and center all its items
on individual lines within a |\vbox|:
\begintt
\def\centerlist#1{\def\\##1{\relax##1\cr}%
\vbox{\halign{\hfil##\hfil\cr#1}}}
\endtt
A particular item can be selected by its position number from the left:
\begintt
\def\select#1\of#2\to#3{\def#3{\outofrange}%
\long\def\\##1{\advance#1-1 \ifnum#1=0 \def#3{##1}\fi}#2}
\endtt
(Here |#1| is a count register, |#2| is a list macro, and |#3| is a
control sequence.) \ And so on; hundreds of other applications can
be imagined.\footnote\dag{The concept of a list macro is strongly related
to the concept of a list procedure in a programming language;
see {\sl Communications of the ACM\/ \bf7} (1964), 280.}
\TeX\ does all of the preceding operations efficiently, in the sense
that the running time will be proportional to the length of the list
macro involved. It's natural to ask if the rightmost item can be
removed with equal efficiency, since the final item of a list is
somewhat hard to isolate. There is apparently no way to delete the
$n$th item of an $n$-item list in order~$n$ steps, maintaining
complete generality, unless the ^|\aftergroup| trick (by which
we created a macro that expands to $n$~asterisks) is used; and the
|\aftergroup| trick is somewhat unattractive in the list application,
because the list might be quite long.\footnote\ddag{The interested reader may
enjoy constructing a macro that removes the $k$th item of an $n$-item list
macro~|\l| in $O(n\log n)$ steps, given $k$ and~|\l|,
without using |\aftergroup|.} However, if we restrict list items to
unexpandable tokens, it turns out to be possible to remove the
^^|\xyzzy| rightmost item quite efficiently:
\begintt
\def\deleterightmost#1{\edef#1{\expandafter\xyzzy#1\xyzzy}}
\long\def\xyzzy\\#1#2{\ifx#2\xyzzy\yzzyx
\else\noexpand\\{#1}\fi\xyzzy#2}
\long\def\yzzyx#1\xyzzy\xyzzy{\fi}
\endtt
Careful study of this example shows that \TeX's ^{mouth} is capable of
doing ^{recursive} operations, given sufficiently tricky macros.
The contents of a ^|\count| register can easily be converted to decimal
and stored in a control sequence; for example, if\/ |\n| is a register,
`|\edef\csn{\the\n}|' puts its value into~|\csn|. Conversely, a value
from |\csn| can be put back into |\n| by saying simply `|\n=\csn|'.
There's usually no point in doing this transformation just to minimize
the usage of\/ |\count| registers, since \TeX\ has 256 of them; but a
decimal representation like the expansion of\/ |\csn| can be stored in a
list macro, and that might be useful in some applications. Incidentally,
there's a neat way to test if such a control-sequence-number is zero:
`^|\if||0\csn|\<true text>|\else|\<false text>|\fi|' works because
extra digits of a nonzero number will be ignored with the \<true text>.
A technique something like list macros can be used to maintain
unordered sets of control sequences. In this case it's
convenient to leave off the braces; for example,
\begintt
\def\l{\\\alpha\\\beta\\\gamma}
\endtt
defines a ``^{set macro}'' |\l| that represents the control
sequences $\{\,\hbox{|\alpha|},\hbox{|\beta|},\hbox{|\gamma|}\,\}$.
A straightforward construction tests whether a given control sequence
is in the set:
\begintt
\def\ismember#1\of#2{\resultfalse\def\given{#1}%
\def\\##1{\def\next{##1}\ifx\next\given\resulttrue\fi}#2}
\endtt
And an efficient but not-so-straightforward construction removes all
occurrences of control sequences that are
|\ifx|-equivalent to a given control sequence:
\begintt
\def\remequivalent#1\from#2{\let\given=#1%
\ifx#2\empty\else\edef#2{\expandafter\plugh#2\plugh}\fi}
\def\plugh\\#1#2{\ifx#1\given\else\noexpand\\\noexpand#1\fi
\ifx#2\plugh\hgulp\fi\plugh#2}
\def\hgulp\fi\plugh\plugh{\fi}
\endtt
%\subsection Verbatim listing. ^^{Verbatim}
%Plain \TeX\ includes a macro called
%^|\dospecials| that is essentially a set macro, representing the set
%of all characters that have a special category code. \ (The control
%sequence ^|\do| plays the r\^ole of\/ |\\| in the discussion above.) \
%Therefore it's easy to change all of the special characters to
%category~12~(other):
%\begintt
%\def\uncatcodespecials{\def\do##1{\catcode`##1=12 }\dospecials}
%\endtt
%This works even when the set of special characters has been changed,
%provided that |\dospecials| has been updated to represent the current set.
\subsection 逐字呈现. ^^{Verbatim}
Plain \TeX\ includes a macro called
^|\dospecials| that is essentially a set macro, representing the set
of all characters that have a special category code. \ (The control
sequence ^|\do| plays the r\^ole of\/ |\\| in the discussion above.) \
Therefore it's easy to change all of the special characters to
category~12~(other):
\begintt
\def\uncatcodespecials{\def\do##1{\catcode`##1=12 }\dospecials}
\endtt
This works even when the set of special characters has been changed,
provided that |\dospecials| has been updated to represent the current set.
The operation |\uncatcodespecials| just defined is important, of course,
when \TeX's automatic features need to be temporarily disabled. Let's
suppose that we want to create a listing of some computer file,
reproducing the characters and the spacing exactly as they appear in the
file. To make the problem more interesting, let's also print line
numbers in front of each line, as in the listing of |story.tex| on
page~\storypage. To make the problem simpler, let's assume that the file
contains only standard ASCII printing characters: no tab marks or
form feeds or such things. Our goal is to devise a |\listing| macro
such that, e.g., `|\listing{story}|' will insert a listing of
the |story.tex| file into a manuscript, after which \TeX's normal
conventions will be restored. The listing should be in ^|\tt| type.
A macro of the following form meets the desired specifications:
\begintt
\def\listing#1{\par\begingroup\setupverbatim\input#1 \endgroup}
\endtt
Notice that the ^|\endgroup| command here will nicely ``turn off'' all
the weird things that ^|\setupverbatim| turns on. Notice also that the
commands `|\input#1 \endgroup|' will not be listed verbatim, even though
they follow |\setupverbatim|, since they entered \TeX's reading mechanism
when the |\listing| macro was expanded (i.e., before the verbatim business
was actually set up).
But what should |\setupverbatim| do? Well, it ought to include
^|\obeylines|, since this automatically inserts a ^|\par| at the end of
each line that is input; it ought to include ^|\uncatcodespecials|, so
that special characters print as themselves; and it ought to include
^|\obeyspaces|, so that each space counts. But we need to look carefully
at each of these things to see exactly what they do: \
(1)~Plain \TeX's |\obeylines| macro changes the ^|\catcode| of |^^M|
to |\active|, and then it says `|\let^^M=\par|'. Since |^^M| is placed
at the end of each line, this effectively ends each line with |\par|;
however, |\obeylines| doesn't say `|\def^^M{\par}|', so we must make
any desired changes to |\par| before invoking |\obeylines|. \
(2)~The |\uncatcodespecials| operation changes a space to category~12;
but the |\tt| font has the character `\]' in the \<space> position,
so we don't really want \]$_{12}$. \
(3)~The |\obeyspaces| macro in Appendix~B merely changes the \<space>
character to category~13; ^{active character} \]$_{13}$ has been
defined to be the same as ^|\space|, a macro that expands to \]$_{10}$.
This is usually what is desired; for example, it means that spaces in
constructions like `|\hbox to 10 pt {...}|' won't cause any trouble.
But in our application it has an undesirable effect, because it
produces spaces that are affected by the ^{space factor}. To defeat this
feature, it's necessary either to say ^|\frenchspacing| or to
redefine \]$_{13}$ to be the same as |\|\]. ^^{control space}
The latter alternative is better, because the former will discard spaces
at the beginning of each line.
The |\setupverbatim| macro should also take care of putting a line
number into the position of the paragraph indentation. We can take care
of this by introducing a counter variable and using ^|\everypar|, as follows:
\begintt
\newcount\lineno % the number of file lines listed
\def\setupverbatim{\tt \lineno=0
\obeylines \uncatcodespecials \obeyspaces
\everypar{\advance\lineno by1 \llap{\sevenrm\the\lineno\ \ }}}
{\obeyspaces\global\let =\ } % let active space = control space
\endtt
^^|\llap|
In theory, this seems like it ought to work; but in practice, it fails
in two ways. One rather obvious failure---at least, it becomes obvious
when the macro is tested---is that all the empty lines of the file
are omitted. The reason is that the |\par| command at the end of an empty
line doesn't start up a new paragraph, because it occurs in vertical mode.
The other failure is not as obvious, because it occurs much less often:
The |\tt| fonts contain ^{ligatures} for Spanish punctuation, so the
sequences |?||`| and~|!||`| will be printed as {\tt?`} and~{\tt!`}
respectively. Both of these defects can be cured by inserting
\begintt
\def\par{\leavevmode\endgraf} \catcode`\`=\active
\endtt
before |\obeylines| in the |\setupverbatim| macro, and by defining |`|$_{13}$
as follows:
\begintt
{\catcode`\`=\active \gdef`{\relax\lq}}
\endtt
A similar scheme could be used to produce verbatim listings in other fonts;
but more characters would have to be made active, in order to break
ligatures and to compensate for ASCII characters that aren't present.
\def\uncatcodespecials{\def\do##1{\catcode`##1=12 }\dospecials}
\def\verbatim{\begingroup\tt\uncatcodespecials\obeyspaces\doverbatim}
\newcount\balance
{\catcode`<=1 \catcode`>=2 \catcode`\{=12 \catcode`\}=12
\gdef\doverbatim{<\balance=1\verbatimloop>
\gdef\verbatimloop#1<\def\next<#1\verbatimloop>%
\if#1{\advance\balance by1
\else\if#1}\advance\balance by-1
\ifnum\balance=0\let\next=\endgroup\fi\fi\fi\next>>
Instead of listing a file verbatim, you might want to define a
|\verbatim| macro such that `|\verbatim{$this$|{\tt\ is }|{\it!}}|'
yields `\verbatim{$this$ is {\it!}}'. It's somewhat dangerous to change
^{category codes}, because \TeX\ stamps the category on each character
when that character is first read from a file. Thus, if\/ |\verbatim|
were defined by a construction of the form
|\long\def\verbatim#1{|\<something>|}|, argument~|#1| would already be
converted to a list of tokens when \<something> starts; |\catcode| changes
would not affect the argument. The alternative is to change category
codes before scanning the argument to |\verbatim|:
\begintt
\def\verbatim{\begingroup\tt\uncatcodespecials
\obeyspaces\doverbatim}
\newcount\balance
{\catcode`<=1 \catcode`>=2 \catcode`\{=12 \catcode`\}=12
\gdef\doverbatim{<\balance=1\verbatimloop>
\gdef\verbatimloop#1<\def\next<#1\verbatimloop>%
\if#1{\advance\balance by1
\else\if#1}\advance\balance by-1
\ifnum\balance=0\let\next=\endgroup\fi\fi\fi\next>>
\endtt
This works; but it's slow, and it allows verbatim setting only of text
that has balanced braces. It would not be suitable for typesetting
the examples in a book like {\sl The \TeX book}. \ (Appendix~E contains
the verbatim macros that were actually used.) \ Note also that if this
|\verbatim{...}| macro appears in the argument to another macro
like |\centerline|, it will fail because the category codes can no longer be
changed. The ^|\footnote| macro in Appendix~B is careful to avoid
scanning its argument prematurely; it uses ^|\bgroup| and ^|\egroup| in a
somewhat tricky way, so that category code changes are permitted inside
plain \TeX\ footnotes.
On the other hand, there is a fairly fast way to convert a token list
to an almost-verbatim transcript:
\begintt
\long\def\verbatim#1{\def\next{#1}%
{\tt\frenchspacing\expandafter\strip\meaning\next}}
\def\strip#1>{}
\endtt
Tokens are stripped off in this construction since, for example,
^|\meaning||\next| might be `\def\next{$this$ is {\it!}}%
\expandafter|\meaning\next|'. Notice that a space will be inserted after
the control word |\it|, but no space might actually have occurred there in
the argument to |\verbatim|; such information has been irretrievably lost.
One of the problems with verbatim mode is that it's hard to stop;
if we turn off all of \TeX's normal control capabilities, we end up
``painting ourselves into a corner'' and reaching a point of no return.
The |\listing| macro was able to solve this problem because the end of
a file brings an old token list back to life. Another solution would be
to specify a certain number of lines, after which verbatim mode should
end. Otherwise it's necessary to put some constraint on the text,
i.e., to make certain texts unprintable in verbatim mode. For example,
here's an approach that typesets everything between |\beginverbatim| and
|\endverbatim|, assuming only that the control sequence |\endverbatim|
does not need to be set:
\begintt
\def\beginverbatim{\par\begingroup\setupverbatim\doverbatim}
{\catcode`\||=0 \catcode`\\=12 % || is temporary escape character
||obeylines||gdef||doverbatim^^M#1\endverbatim{#1||endgroup}}
\endtt
This construction assumes that |\beginverbatim| appears at the end of a
line in the manuscript file. Argument |#1| will be read entirely into
\TeX's memory before anything happens, so the total amount of verbatim
material had better not be too voluminous. Incidentally, it isn't
necessary to say that this macro is ^|\long|, because the |\par|'s
inserted by ^|\obeylines| are really |^^M|'s.
Another approach is to keep one character untouchable. For example,
it's possible to define things so that `|\verbatim|\<char>\<text>\<char>'
will typeset the \<text> verbatim, where the \<text> is not supposed
to contain any occurrences of the repeated delimiter \<char>:
\begintt
\def\verbatim{\begingroup\setupverbatim\doverbatim}
\def\doverbatim#1{\def\next##1#1{##1\endgroup}\next}
\endtt
%\subsection Selective loading of macros. Some interesting problems arise
%when a computer system acquires a large library of macro files. For
%example, suppose that a file |macs.tex| contains the lines
%\begintt
%\let\italcorr=\/
%\def\/{\unskip\italcorr}
%\endtt
%because somebody thought it would be nice to allow an optional space
%before \TeX's primitive ^|\/| command. That's fine, except if |macs.tex|
%is input twice; for example, two other macro files might both say
%^|\input|~|macs|. When those lines are processed the second time,
%|\italcorr| will be |\let| equal to a macro that expands to
%`|\unskip\italcorr|',
%and you can guess what will happen: \TeX\ will get into an infinite loop,
%stoppable only by ^{interrupt}ing the program manually.
%^^{recursion, infinite}
\subsection 可选的宏载入. Some interesting problems arise
when a computer system acquires a large library of macro files. For
example, suppose that a file |macs.tex| contains the lines
\begintt
\let\italcorr=\/
\def\/{\unskip\italcorr}
\endtt
because somebody thought it would be nice to allow an optional space
before \TeX's primitive ^|\/| command. That's fine, except if |macs.tex|
is input twice; for example, two other macro files might both say
^|\input|~|macs|. When those lines are processed the second time,
|\italcorr| will be |\let| equal to a macro that expands to
`|\unskip\italcorr|',
and you can guess what will happen: \TeX\ will get into an infinite loop,
stoppable only by ^{interrupt}ing the program manually.
^^{recursion, infinite}
Fortunately there's an easy way to prevent this problem, by placing
a suitable interlock near the beginning of every macro file that
might introduce such anomalies:
\begintt
\ifx\macsisloaded\relax\endinput\else\let\macsisloaded=\relax\fi
\endtt
Then |\macsisloaded| will be undefined at the time of the first~|\ifx|,
but the file will not be read twice. A different control sequence
should, of course, be used for each file.
Another difficulty with large sets of macros is that they take up space.
It would be nice to preload every macro that every \TeX\ user has ever
dreamed up; but there might not be enough room,
because \TeX's memory capacity is finite. You might find it necessary
to hold back and to load only the macros that are really needed.
How much memory ^{space} ^^{efficiency} does a macro require?
Well, there are four kinds of memory involved: token memory,
name memory, string memory, and character memory. \ (If any of these
becomes too full, it will be necessary to increase what \TeX\ calls the
macro memory size, the hash size, the number of strings, and/or the
pool size, respectively; see Chapter~27.) \ The token memory is
most important; a macro takes one cell of token memory for each token
in its definition, including the `|{|' and the `|}|'. For example,
the comparatively short definition
\begintt
\def\example#1\two{\four}
\endtt
takes five tokens: |#1|, \cstok{two}, |{|$_1$, \cstok{four}, and |}|$_2$.
Each control sequence also takes up one cell of name memory, one cell
of string memory, and as many cells of character memory as there are
characters in the name (seven in the case of\/ |\example|). Character
memory is comparatively cheap; four characters, or in some cases five,
will fit in the same number of bits as a single cell of token memory,
inside the machine. Therefore you don't save much by choosing short
macro names.
\TeX\ will tell you how close you come to exceeding its current
memory capacity if you say ^|\tracingstats||=1|. For example, one of the
runs that the author made while testing galley proofs of this appendix
reported the following statistics:
\begintt
Here is how much of TeX's memory you used:
209 strings out of 1685
1659 string characters out of 17636
27618 words of memory out of 52821
1172 multiletter control sequences out of 2500
\endtt
Consequently there was plenty of room for more macros: $52821-27618=
25203$ unused cells of main memory, $2500-1172=1328$ of name memory,
$1685-209=1476$ of string memory, and $17636-1659=15977$ of character memory.
But a fairly large \TeX\ was being used, and only the macros of
Appendices B and~E were loaded; in other circumstances it might have
been necessary to conserve space.
One obvious way to keep from loading too many macros is to keep
the macro files short and to |\input| only the ones that you need.
But short files can be a nuisance; sometimes there's a better way.
For example, let's suppose that a file contains five optional classes
of macros called |A|, |B|, |C|, |D|,~|E|, and that a typical user
will probably want only at most two or three of these five; let's
design a |\load| macro so that, for example,
`|\load{macs}{AC}|'
will load file |macs.tex| including options |A| and |C| but not options
|B|, |D|, or |E|. The following |\load| macro converts its second argument
into a set~macro called |\options|:
\begintt
\def\load#1#2{\let\options=\empty \addoptions#2\end \input#1 }
\def\addoptions#1{\ifx#1\end \let\next=\relax
\else\let\\=\relax\edef\options{\options\\#1}%
\let\next=\addoptions \fi \next}
\endtt
Inside the file |macs.tex|, a portion of code that should be loaded only
under option~|B|, say, can be enclosed by `|\ifoption B ... \fi|', where
|\ifoption| is defined thus:
\begintt
\def\ifoption#1{\def\\##1{\if##1#1\resulttrue\fi}%
\resultfalse \options \ifresult}
\endtt
(This is a simple application of ideas presented earlier in this appendix.)
However, the |\ifoption...\fi| scheme isn't very robust, because it requires
all of the macros in the optional part to be well nested with
respect to |\if...| and |\fi|; a macro like |\ifoption| itself couldn't
easily be defined in such a place! There's a better scheme that also
runs faster, based on ^{category code} changes. This idea (due to
Max ^{D\'\i az}) requires that the leftmost nonblank character on each
line be either `|\|' or~`|{|'; it's usually easy to arrange this.
Furthermore, one other symbol, say |~|, is reserved. Then the text
material that is to be loaded only under option~|B| is preceded by
the line `|\beginoption|~|B|' and followed by a line that says
`|~endoptionalcode|'. The ^|\catcode| for~|~| is set to~14 (comment
character), hence the |~endoptionalcode| line will have no effect if
code is not being skipped. The |\beginoption| macro works like this:
\begintt
\def\beginoption#1{\ifoption#1\else\begingroup\swapcategories\fi}
\def\swapcategories{\catcode`\\=14 \catcode`\{=14 \catcode`\~=0 }
\let\endoptionalcode=\endgroup
\catcode`\~=14
\endtt
Once the categories have been swapped, all lines will be skipped at high speed
until the control sequence |~endoptionalcode| is encountered; then
everything will be restored to its former state. Under this scheme,
material that should be loaded only under both options |B| and~|D|
can be prefaced by both `|\beginoption|~|B|' and `|\beginoption|~|D|';
material that should be loaded under either option~|B| or option~|D|
(or both) can be prefaced by
\begintt
\beginoption B
~oroption D
\endtt
if we define |\oroption#1| to be an abbreviation for
`|\ifoption#1\endgroup\fi|'.
Another kind of selective loading is sometimes appropriate, based on
whether or not a particular control sequence is defined. In this
scheme, if the control sequence is undefined, it should remain
undefined and it should take up no space whatever in \TeX's memory.
There's a slick way to do this, namely to say
\begintt
\ifx\cs\undefined ... \fi
\endtt
(assuming that ^|\undefined| has never been defined). \TeX\ does not
put undefined control sequences into its internal tables if they follow
^|\ifx| or if they are encountered while skipping ^{conditional text}.
You can use this idea, for example, to prepare a bibliography for a paper,
by reading a suitably arranged bibliography file; only the entries
that correspond to defined control sequences will be loaded.
%\subsection Brace hacks. Several of \TeX's operations depend on
%^{grouping}, and you'll want to know exactly what this means if
%you try to do certain tricky things. For example, plain \TeX's
%control sequences ^|\bgroup| and ^|\egroup| are ``^{implicit braces}''
%because they have been defined by
%\begintt
%\let\bgroup={ \let\egroup=}
%\endtt
%This means that you can include them in the replacement texts of
%definitions without worrying about how they nest; for example, the
%macros
%\begintt
%\def\beginbox{\setbox0=\hbox\bgroup}
%\def\endbox{\egroup\copy0 }
%\endtt
%allow you to make a box between |\beginbox| and |\endbox|; the behavior
%is almost the same as
%\begintt
%\def\beginbox#1\endbox{\setbox0=\hbox{#1}\copy0 }
%\endtt
%but different in three important ways: (1)~The first alternative allows
%category codes to change inside the box. (2)~The first alternative
%is faster, because it doesn't need to scan the box contents both as an
%argument and as a sequence of actual commands. (3)~The first alternative
%takes less memory space, because no argument needs to be stored.
%Thus, the first alternative is usually superior.
\subsection 花括号技巧. Several of \TeX's operations depend on
^{grouping}, and you'll want to know exactly what this means if
you try to do certain tricky things. For example, plain \TeX's
control sequences ^|\bgroup| and ^|\egroup| are ``^{implicit braces}''
because they have been defined by
\begintt
\let\bgroup={ \let\egroup=}
\endtt
This means that you can include them in the replacement texts of
definitions without worrying about how they nest; for example, the
macros
\begintt
\def\beginbox{\setbox0=\hbox\bgroup}
\def\endbox{\egroup\copy0 }
\endtt
allow you to make a box between |\beginbox| and |\endbox|; the behavior
is almost the same as
\begintt
\def\beginbox#1\endbox{\setbox0=\hbox{#1}\copy0 }
\endtt
but different in three important ways: (1)~The first alternative allows
category codes to change inside the box. (2)~The first alternative
is faster, because it doesn't need to scan the box contents both as an
argument and as a sequence of actual commands. (3)~The first alternative
takes less memory space, because no argument needs to be stored.
Thus, the first alternative is usually superior.
For the purposes of this discussion we shall assume that only `|{|'
has category~1 and that only `|}|' has category~2, although any
characters can actually be used as group delimiters. ^^{braces}
Group nesting is crucial during two of \TeX's main activities:
(a)~when \TeX\ is scanning a ^\<balanced text>, e.g., when \TeX\ is
forming the replacement text of a macro, a parameter, or a token list
variable; (b)~when \TeX\ must determine whether the token
|&|~or ^|\span| ^^{ampersand}
or ^|\cr| or~^|\crcr| is the end of an entry within an ^{alignment}.
\TeX's mouth has two internal counting mechanisms to deal with nesting:
The ``master counter'' goes up by~1 for each |{|$_1$ scanned by
\TeX, and down by~1 for each~|}|$_2$; the ``balance counter'' is
similar, but it is affected only by explicit |{|$_1$ and |}|$_2$ tokens
that are actually contributed to a token list that is being formed.
The master counter decreases by~1 when \TeX\ evaluates the
^{alphabetic constant} |`{|, and it increases by~1 when \TeX\ evaluates
|`}|, hence the net change is zero when such constants are evaluated.
As a consequence of these rules, certain constructions produce
the following effects:
$$\halign{\indent\hfil#\hfil&\quad\hfil#\hfil&
\quad\hfil#\hfil&\qquad\hfil#\hfil&\quad\hfil#\hfil\cr
&\it Master counter change\span\omit&\it Balance counter change\span\omit\cr
\it Input&\it expanded&\it unexpanded&\it expanded&\it unexpanded\cr
|{|&1&1&1&1\cr
|\bgroup|&0&0&0&0\cr
|\iffalse{\fi|&1&1&0&1\cr
|\ifnum0=`{\fi|&0&1&0&1\cr}$$
The last two cases produce no begin-group tokens when expanded, but they
do affect the master counter as shown. Thus, for example,
\begintt
\def\eegroup{\ifnum0=`{\fi}}
\endtt
makes |\eegroup| behave rather like |\egroup|, but the expansion of\/
|\eegroup| also decreases the master counter.
Alignment processing uses only the master counter, not the balance counter.
An alignment entry ends with the first |&|~or |\span| or |\cr| or~|\crcr|
that appears when the master counter has the value that was present
in the counter at the beginning of the entry. Thus, for example,
the curious construction
\begintt
\halign{\show\par#\relax\cr
\global\let\par=\cr
{\global\let\par=\cr}\cr
\par}
\endtt
causes \TeX\ to perform three |\show| instructions, in which the
respective values of\/ |\par| shown are |\par|, |\relax|, and~|\cr|.
Similarly, each template in the preamble to an alignment ends with
the first |&| or |\cr| or |\crcr| that appears at the master counter level
that was in effect at the beginning of the entry; hence |&|~and |\cr| and
|\crcr| tokens can appear within a template of an alignment, if they
are hidden by braces (e.g., if they appear in a definition).
These facts allow us to draw two somewhat surprising conclusions: (1)~If
an alignment entry has the form `$\,\alpha\,$^|\iffalse||{\fi|$\,\beta\,$%
|\iffalse}\fi|$\,\gamma\,$', it's possible for $\beta$ to include
|&|~and~|\cr| tokens that aren't local to a group.\footnote*{The
token list $\alpha$ should not be empty, however, because \TeX\ expands
the first token of an alignment entry before looking at the template,
in order to see if the entry begins with |\noalign| or |\omit|. The master
counter value that is considered to be present at the beginning of an
entry is the value in the counter just after the ``$u$~part'' of the
template has been entirely read.} (2)~The construction
\begintt
{\span\iffalse}\fi
\endtt
appearing in a preamble contributes `|{|' to the template without
any net change to the master counter; thus, it's very much like
|\bgroup|, except that it produces |{|$_1$ explicitly.
If you understand (1) and~(2), you'll agree that the present appendix
deserves its name.
%\def\pmb#1{\setbox0=\hbox{#1}%
% \kern-.025em\copy0\kern-\wd0\kern.05em\copy0
% \kern-\wd0\kern-.025em\raise.0433em\box0 }
%\subsection Box maneuvers. ^^{Box maneuvers}
%Let's turn now from syntax to semantics,
%i.e., from \TeX's mouth to its gastro-intestinal tract. Sometimes an odd
%symbol is needed in boldface type, but it's available only in a normal
%weight. In such cases you can sometimes get by with ``^{poor man's bold},''
%obtained by overprinting the normal weight symbol with slight offsets.
%The following macro typesets its argument three times in three
%slightly different places, equidistant from each other; but the result
%takes up just as much space as if\/ |\pmb| had been simply |\hbox|:
%\begintt
%\def\pmb#1{\setbox0=\hbox{#1}%
% \kern-.025em\copy0\kern-\wd0
% \kern.05em\copy0\kern-\wd0
% \kern-.025em\raise.0433em\box0 }
%\endtt
%For example, `|\pmb{$\infty$}|' yields `\pmb{$\infty$}'. The results
%are somewhat \pmb{fuzzy}, and they certainly are no match for the
%real thing if it's available; but poor man's bold is better than nothing,
%and once in a~while you can get away with it.
\def\pmb#1{\setbox0=\hbox{#1}%
\kern-.025em\copy0\kern-\wd0\kern.05em\copy0
\kern-\wd0\kern-.025em\raise.0433em\box0 }
\subsection 操纵盒子. ^^{Box maneuvers}
Let's turn now from syntax to semantics,
i.e., from \TeX's mouth to its gastro-intestinal tract. Sometimes an odd
symbol is needed in boldface type, but it's available only in a normal
weight. In such cases you can sometimes get by with ``^{poor man's bold},''
obtained by overprinting the normal weight symbol with slight offsets.
The following macro typesets its argument three times in three
slightly different places, equidistant from each other; but the result
takes up just as much space as if\/ |\pmb| had been simply |\hbox|:
\begintt
\def\pmb#1{\setbox0=\hbox{#1}%
\kern-.025em\copy0\kern-\wd0
\kern.05em\copy0\kern-\wd0
\kern-.025em\raise.0433em\box0 }
\endtt