Research V7 development
[unix-history] / usr / doc / trofftut / tt12
CommitLineData
54b544da
BK
1.NH
2Conditionals
3.PP
4Suppose we want the
5.BD .SH
6macro to leave two extra inches of space just before section 1,
7but nowhere else.
8The cleanest way to do that is to test inside the
9.BD .SH
10macro
11whether
12the section number is 1,
13and add some space if it is.
14The
15.BD .if
16command provides the conditional test
17that we can add
18just before the heading line is output:
19.P1 4
20^if \e\en(SH=1 ^sp 2i \e" first section only
21.P2
22.PP
23The condition after the
24.BD .if
25can be any arithmetic or logical expression.
26If the condition is logically true, or arithmetically greater than zero,
27the rest of the line is treated as if
28it were text _
29here a command.
30If the condition is false, or zero or negative,
31the rest of the line is skipped.
32.PP
33It is possible to do more than one command if a condition is true.
34Suppose several operations are to be done before section 1.
35One possibility is to define a macro
36.BD .S1
37and invoke it
38if we are about to do section 1
39(as determined by an
40.BD .if ).
41.P1
42^de S1
43--- processing for section 1 ---
44^^
45^de SH
46^^^
47^if \e\en(SH=1 ^S1
48^^^
49^^
50.P2
51.PP
52An alternate way is to use the
53extended form of the
54.BD .if ,
55like this:
56.P1
57^if \e\en(SH=1 \e{--- processing
58for section 1 ----\e}
59.P2
60The braces
61.BD \e{
62and
63.BD \e}
64must occur in the positions shown
65or you will get unexpected extra lines in your output.
66.UL troff
67also provides
68an `if-else' construction,
69which we will not go into here.
70.PP
71A condition can be negated by preceding it with
72.BD ! ;
73we get the same effect as above (but less clearly) by using
74.P1
75^if !\e\en(SH>1 ^S1
76.P2
77.PP
78There are a handful of
79other conditions that can be tested with
80.BD .if .
81For example, is the current page even or odd?
82.P1
83^if e ^tl ''even page title''
84^if o ^tl ''odd page title''
85.P2
86gives facing pages different titles
87when used inside an appropriate new page macro.
88.PP
89Two other conditions
90are
91.BD t
92and
93.BD n ,
94which tell you whether the formatter is
95.UL troff
96or
97.UL nroff .
98.P1
99^if t troff stuff ...
100^if n nroff stuff ...
101.P2
102.PP
103Finally, string comparisons may be made in an
104.BD .if :
105.P1
106^if 'string1'string2' stuff
107.P2
108does `stuff' if
109.ul
110string1
111is the same as
112.ul
113string2.
114The character separating the strings can be anything
115reasonable that is
116not contained in either string.
117The strings themselves can reference strings with
118.BD \e* ,
119arguments with
120.BD \e$ ,
121and so on.