BSD 3 development
[unix-history] / usr / doc / trofftut / tt08
CommitLineData
8340f87c
BJ
1.NH
2Introduction to Macros
3.PP
4Before we can go much further in
5.UL troff ,
6we need to learn a bit about the
7macro
8facility.
9In its simplest form, a macro is just a shorthand notation
10quite similar to a string.
11Suppose we want every paragraph to start
12in exactly the same way _
13with a space and a temporary indent of two ems:
14.P1
15^sp
16^ti +2m
17.P2
18Then to save typing, we would like to collapse these into
19one shorthand line,
20a
21.UL troff
22`command' like
23.P1
24^PP
25.P2
26that would be treated by
27.UL troff
28exactly as
29.P1
30^sp
31^ti +2m
32.P2
33.BD .PP
34is called a
35.ul
36macro.
37The way we tell
38.UL troff
39what
40.BD .PP
41means is to
42.ul
43define
44it with the
45.BD .de
46command:
47.P1
48^de PP
49^sp
50^ti +2m
51^^
52.P2
53The first line names the macro
54(we used
55.BD .PP ' `
56for `paragraph',
57and upper case so it wouldn't conflict with
58any name that
59.UL troff
60might
61already know about).
62The last line
63.BD ..
64marks the end of the definition.
65In between is the text,
66which is simply inserted whenever
67.UL troff
68sees the `command'
69or macro call
70.P1
71^PP
72.P2
73A macro
74can contain any mixture of text and formatting commands.
75.PP
76The definition of
77.BD .PP
78has to precede its first use;
79undefined macros are simply ignored.
80Names are restricted to one or two characters.
81.PP
82Using macros for commonly occurring sequences of commands
83is critically important.
84Not only does it save typing,
85but it makes later changes much easier.
86Suppose we decide that the paragraph indent is too small,
87the vertical space is much too big,
88and roman font should be forced.
89Instead of changing the whole document,
90we need only change the definition of
91.BD .PP
92to
93something like
94.P1
95^de PP \e" paragraph macro
96^sp 2p
97^ti +3m
98^ft R
99^^
100.P2
101and the change takes
102effect everywhere we used
103.BD .PP .
104.PP
105.BD \e"
106is a
107.UL troff
108command that causes the rest of the line to be ignored.
109We use it here to add comments to the macro
110definition
111(a wise idea once definitions get complicated).
112.PP
113As another example of macros,
114consider these two which start and end a block of offset,
115unfilled text, like most of the examples in this paper:
116.P1
117^de BS \e" start indented block
118^sp
119^nf
120^in +0.3i
121^^
122^de BE \e" end indented block
123^sp
124^fi
125^in \(mi0.3i
126^^
127.P2
128Now we can surround text like
129.P1
130Copy to
131John Doe
132Richard Roberts
133Stanley Smith
134.P2
135by the commands
136.BD .BS
137and
138.BD .BE ,
139and it will come out as it did above.
140Notice that we indented by
141.BD .in\ +0.3i
142instead of
143.BD .in\ 0.3i .
144This way we can nest our uses of
145.BD .BS
146and
147.BD BE
148to get blocks within blocks.
149.PP
150If later on we decide that the indent
151should be 0.5i, then it is only necessary to
152change the definitions of
153.BD .BS
154and
155.BD .BE ,
156not the whole paper.