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