-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathchapter13.tex
1637 lines (1511 loc) · 58.6 KB
/
chapter13.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}{12}
\begin{document}
%\chapter{Conditionals}\label{if}
\chapter{Conditionals}\label{if}
%\emph{Conditionals}\index{conditionals}
%are an indispensible tool for powerful macros.
%\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.
\emph{Conditionals}\index{conditionals}
are an indispensible tool for powerful macros.
\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.
%\label{cschap:if}\label{cschap:ifcat}\label{cschap:ifx}\label{cschap:ifcase}\label{cschap:ifnum}\label{cschap:ifodd}\label{cschap:ifhmode}\label{cschap:ifvmode}\label{cschap:ifmmode}\label{cschap:ifinner}\label{cschap:ifdim}\label{cschap:ifvoid}\label{cschap:ifhbox2}\label{cschap:ifvbox2}\label{cschap:iftrue}\label{cschap:iffalse}\label{cschap:fi}\label{cschap:else}\label{cschap:or}\label{cschap:newif}
%\begin{inventory}
%\item [\cs{if}]
% Test equality of character codes.
\label{cschap:if}\label{cschap:ifcat}\label{cschap:ifx}\label{cschap:ifcase}\label{cschap:ifnum}\label{cschap:ifodd}\label{cschap:ifhmode}\label{cschap:ifvmode}\label{cschap:ifmmode}\label{cschap:ifinner}\label{cschap:ifdim}\label{cschap:ifvoid}\label{cschap:ifhbox2}\label{cschap:ifvbox2}\label{cschap:iftrue}\label{cschap:iffalse}\label{cschap:fi}\label{cschap:else}\label{cschap:or}\label{cschap:newif}
\begin{inventory}
\item [\cs{if}]
Test equality of character codes.
%\item [\cs{ifcat}]
% Test equality of category 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{ifx}]
Test equality of macro expansion, or equality of character code and
category code.
%\item [\cs{ifcase}]
% Enumerated case statement.
\item [\cs{ifcase}]
Enumerated case statement.
%\item [\cs{ifnum}]
% Test relations between numbers.
\item [\cs{ifnum}]
Test relations between numbers.
%\item [\cs{ifodd}]
% Test whether a number is odd.
\item [\cs{ifodd}]
Test whether a number is odd.
%\item [\cs{ifhmode}]
% Test whether the current mode is (possibly restricted) horizontal mode.
\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{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{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{ifinner}]
Test whether the current mode is an internal mode.
%\item [\cs{ifdim}]
% Compare two dimensions.
\item [\cs{ifdim}]
Compare two dimensions.
%\item [\cs{ifvoid}]
% Test whether a box register is empty.
\item [\cs{ifvoid}]
Test whether a box register is empty.
%\item [\cs{ifhbox}]
% Test whether a box register contains a horizontal box.
\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{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{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{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{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{or}]
Separator for entries of an \cs{ifcase}.
%\item [\cs{newif}]
% Create a new test.
\item [\cs{newif}]
Create a new test.
%\end{inventory}
\end{inventory}
%%\point The shape of conditionals
%\section{The shape of conditionals}
%\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.
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.
The exact process of how \TeX\ expands conditionals is treated
below.
%\section{Character and control sequence tests}
%\label{sec:charactertests}
\section{Character and control sequence tests}
\label{sec:charactertests}
%Three tests exist for testing character tokens and
%control sequence tokens.
Three tests exist for testing character tokens and
control sequence tokens.
%\subsection{\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).
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).
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}.
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}
\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}}
%\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}
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\index{category!16}
%(ordinarily, category codes are in the range~0--15), which makes them
%all equal to each other, and different from all character
%tokens.
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\index{category!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}}
%\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}
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}
\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.
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.
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}.
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}.
%\section{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.
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}
\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.
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.
%See also chapter~\ref{chap:hvmode}.
See also chapter~\ref{chap:hvmode}.
%\section{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\index{category!12}.
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\index{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.
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.
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}
%\point Other tests
\section{Other tests}
%%\spoint Dimension testing
%\subsection{Dimension testing}
%\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}.
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}
%\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.
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}
%\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}.
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}
%\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.
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}
%\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.
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.
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.
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}
%\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 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 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}.
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\index{category!12}.
%Ordinarily, these characters have category code~11\index{category!11}.
%In effect this
%macro then eats these two characters, and \TeX\ complains if
%they are not present.
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\index{category!12}.
Ordinarily, these characters have category code~11\index{category!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}>.
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@>
%\mdqon
%to expand \cs{string} first. The part \cs{string}""\cs{iffoo}
%\mdqoff
%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}
Let us examine the call \verb>\@if\iffoo{true}>.
\begin{itemize}\item The \cs{expandafter} reaches over the \verb>\if@>
\mdqon
to expand \cs{string} first. The part \cs{string}""\cs{iffoo}
\mdqoff
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}
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}
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}
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}
%\section{Evaluation of conditionals}
\section{Evaluation of conditionals}
%\TeX's conditionals behave differently from those
%in ordinary programming languages. In many instances
%one may not notice the difference, but in certain contexts
%it is important to know precisely the \indextermbus{evaluation of}{conditionals} proceeds.
\TeX's conditionals behave differently from those
in ordinary programming languages. In many instances
one may not notice the difference, but in certain contexts
it is important to know precisely the \indextermbus{evaluation of}{conditionals} proceeds.
%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.
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.
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.
\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.
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}
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}
%\point Assorted remarks
\section{Assorted remarks}
%%\spoint The test gobbles up tokens
%\subsection{The test gobbles up tokens}
%\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.
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}}
%\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.
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},