BSD 3 development
[unix-history] / usr / doc / trofftut / tt11
CommitLineData
8340f87c
BJ
1.NH
2Macros with arguments
3.PP
4The next step is to define macros that can change from one
5use to the next
6according to parameters supplied as arguments.
7To make this work, we need two things:
8first, when we define the macro, we have to indicate that some
9parts of it will be provided as arguments when the macro is called.
10Then when the macro is
11called
12we have to provide actual arguments
13to be plugged into the definition.
14.PP
15Let us illustrate by defining a macro
16.BD .SM
17that will print its argument two points
18smaller than the surrounding text.
19That is, the macro call
20.P1
21^SM TROFF
22.P2
23will produce
24.UC TROFF .
25.PP
26The definition of
27.BD .SM
28is
29.P1
30^de SM
31\es\-2\e\e$1\es+2
32^^
33.P2
34Within a macro definition,
35the symbol
36.BD \e\e$n
37refers to the
38.BD n th
39argument
40that the macro was called with.
41Thus
42.BD \e\e$1
43is the string to be placed in a smaller point
44size when
45.BD .SM
46is called.
47.PP
48As a slightly more complicated version, the following definition of
49.BD .SM
50permits optional second and third arguments
51that will be printed in the normal size:
52.P1
53^de SM
54\e\e$3\es\-2\e\e$1\es+2\e\e$2
55^^
56.P2
57Arguments not provided when the macro is called are treated
58as empty,
59so
60.P1
61^SM TROFF ),
62.P2
63produces
64.UC TROFF ),
65while
66.P1
67^SM TROFF ). (
68.P2
69produces
70.UC TROFF ). (
71It is convenient to reverse
72the order of arguments because trailing punctuation
73is much more common than leading.
74.PP
75By the way, the number of arguments that a macro was called with
76is available in number register
77.BD .$ .
78.PP
79The following macro
80.BD ^BD
81is the one used to make the
82`bold roman' we have been using for
83.UL troff
84command names in text.
85It combines horizontal motions, width computations,
86and argument rearrangement.
87.P1 2
88\&.de BD
89\e&\e\e$3\ef1\e\e$1\eh'\-\ew'\e\e$1'u+1u'\e\e$1\efP\e\e$2
90\&..
91.P2
92The
93.BD \eh
94and
95.BD \ew
96commands need no extra backslash, as we discussed above.
97The
98.BD \e&
99is there in case the argument begins with a period.
100.WS
101.PP
102Two backslashes are needed with the
103.BD \e\e$n
104commands, though,
105to protect one of them when the macro is
106being defined.
107Perhaps a second example will make this clearer.
108Consider a macro called
109.BD .SH
110which
111produces section headings rather like those in this paper,
112with the sections numbered automatically,
113and the title in bold in a smaller size.
114The use is
115.P1
116^SH "Section title ..."
117.P2
118(If the argument to a macro is to contain blanks,
119then it must be
120.ul
121surrounded
122by double quotes,
123unlike a string, where only one leading quote is permitted.)
124.PP
125Here is the definition of the
126.BD .SH
127macro:
128.P1
129.ta .75i 1.15i
130^nr SH 0 \e" initialize section number
131^de SH
132^sp 0.3i
133^ft B
134^nr SH \e\en(SH+1 \e" increment number
135^ps \e\en(PS\-1 \e" decrease PS
136\e\en(SH. \e\e$1 \e" number. title
137^ps \e\en(PS \e" restore PS
138^sp 0.3i
139^ft R
140^^
141.P2
142The section number is kept in number register SH, which is incremented each
143time just before it is used.
144(A number register may have the same name as a macro
145without conflict but a string may not.)
146.PP
147We used
148.BD \e\en(SH
149instead of
150.BD \en(SH
151and
152.BD \e\en(PS
153instead of
154.BD \en(PS .
155If we had used
156.BD \en(SH ,
157we would get the value of the register at the time the macro was
158.ul
159defined,
160not at the time it was
161.ul
162used.
163If that's what you want, fine,
164but not here.
165Similarly,
166by using
167.BD \e\en(PS ,
168we get the point size at the time the macro is called.
169.WS
170.PP
171As an example that does not involve numbers,
172recall our
173.BD .NP
174macro which had a
175.P1
176^tl 'left'center'right'
177.P2
178We could make these into parameters by using instead
179.P1
180^tl '\e\e*(LT'\e\e*(CT'\e\e*(RT'
181.P2
182so the title comes from three strings called LT, CT and RT.
183If these are empty, then the title will be a blank line.
184Normally CT would be set with something like
185.P1
186\&^ds CT - % -
187.P2
188to give just the page number between hyphens (as on the top of this page),
189but a user could supply private definitions for
190any of the strings.