forked from CTeX-org/tex-by-topic-cn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter12.tex
2052 lines (1930 loc) · 69.8 KB
/
chapter12.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 -*-
\documentclass{book}
\input{preamble}
\setcounter{chapter}{11}
\begin{document}
%\chapter{Expansion}\label{expand}
\chapter{Expansion}\label{expand}
%\emph{Expansion}\index{expansion} in \TeX\ is rather different from
%procedure calls in most programming languages. This chapter treats the
%commands connected with expansion, and gives a number of (non-trivial)
%examples.
\emph{Expansion}\index{expansion} in \TeX\ is rather different from
procedure calls in most programming languages. This chapter treats the
commands connected with expansion, and gives a number of (non-trivial)
examples.
%\label{cschap:relax}\label{cschap:expandafter}\label{cschap:noexpand}\label{cschap:afterassignment}\label{cschap:the}
%\begin{inventory}
%\item [\cs{relax}]
% Do nothing.
\label{cschap:relax}\label{cschap:expandafter}\label{cschap:noexpand}\label{cschap:afterassignment}\label{cschap:the}
\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{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{noexpand}]
Do not expand the next token.
%\item [\cs{edef}]
% Start a macro definition;
% the replacement text is expanded at definition time.
\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{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{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.
\item [\cs{the}]
Expand the value of various quantities in \TeX\ into a string
of character tokens.
%\end{inventory}
\end{inventory}
%%\point Introduction
%\section{Introduction}
%\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.
\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}
%\point Ordinary expansion
\section{Ordinary expansion}
%The following list gives those constructs
%that are expanded, unless
%expansion is inhibited:
%\index{expansion!expandable constructs}
%\begin{itemize}
%\item macros\label{expand:lijst}
%\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}
The following list gives those constructs
that are expanded, unless
expansion is inhibited:
\index{expansion!expandable constructs}
\begin{itemize}
\item macros\label{expand:lijst}
\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}
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}
%\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.
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}}
%\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}
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}
%\subsection{Total expansion: \protect\cs{edef}}
%\label{expand: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.
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.
\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}
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}
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.
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}
%\spoint \cs{afterassignment}
\subsection{\protect\cs{afterassignment}}
\label{sec:afterassignment}
%The \csterm 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}).
The \csterm 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.
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}.
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.
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.
%\subsection{\protect\cs{aftergroup}}
%\label{sec:aftergroup}
\subsection{\protect\cs{aftergroup}}
\label{sec: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}.
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}
\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.
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.
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}
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}
%\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.
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\index{category!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}.
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\index{category!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}.
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}}
%\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.
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.
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}.
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}
%\spoint \cs{noexpand} and active characters
\subsection{\protect\cs{noexpand} and active characters}
%The combination \cs{noexpand}\gr{token}
%is\index{character!active, and \cs{noexpand}}
%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.
The combination \cs{noexpand}\gr{token}
is\index{character!active, and \cs{noexpand}}
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.
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}}
%\point \cs{relax}
\section{\protect\cs{relax}}
%The control sequence \csidx{relax} cannot be expanded, but
%when it is executed nothing happens.
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.
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}
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}
%\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}).
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}.
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}.
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}