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