@(#)tt11 8.2 (Berkeley) 06/01/94
Macros with arguments
The next step is to define macros that can change from one use to the next according to parameters supplied as arguments. To make this work, we need two things: first, when we define the macro, we have to indicate that some parts of it will be provided as arguments when the macro is called. Then when the macro is called we have to provide actual arguments to be plugged into the definition.
Let us illustrate by defining a macro .SM that will print its argument two points smaller than the surrounding text. That is, the macro call
1 ^SM TROFF
2 will produce C TROFF .
The definition of .SM is
1 ^de SM \es-2\e\e$1\es+2 ^^
2 Within a macro definition, the symbol \e\e$n refers to the n th argument that the macro was called with. Thus \e\e$1 is the string to be placed in a smaller point size when .SM is called.
As a slightly more complicated version, the following definition of .SM permits optional second and third arguments that will be printed in the normal size:
1 ^de SM \e\e$3\es-2\e\e$1\es+2\e\e$2 ^^
2 Arguments not provided when the macro is called are treated as empty, so
1 ^SM TROFF ),
2 produces C TROFF ), while
1 ^SM TROFF ). (
2 produces C TROFF ). ( It is convenient to reverse the order of arguments because trailing punctuation is much more common than leading.
By the way, the number of arguments that a macro was called with is available in number register .$ .
The following macro ^BD is the one used to make the `bold roman' we have been using for troff command names in text. It combines horizontal motions, width computations, and argument rearrangement.
1 2 .de BD \e&\e\e$3\ef1\e\e$1\eh'-\ew'\e\e$1'u+1u'\e\e$1\efP\e\e$2 ..
2 The \eh and \ew commands need no extra backslash, as we discussed above. The \e& is there in case the argument begins with a period. .WS
Two backslashes are needed with the \e\e$n commands, though, to protect one of them when the macro is being defined. Perhaps a second example will make this clearer. Consider a macro called .SH which produces section headings rather like those in this paper, with the sections numbered automatically, and the title in bold in a smaller size. The use is
1 ^SH "Section title ..."
2 (If the argument to a macro is to contain blanks, then it must be .ul surrounded by double quotes, unlike a string, where only one leading quote is permitted.)
Here is the definition of the .SH macro:
1 ^nr SH 0 \e" initialize section number ^de SH ^sp 0.3i ^ft B ^nr SH \e\en(SH+1 \e" increment number ^ps \e\en(PS-1 \e" decrease PS \e\en(SH. \e\e$1 \e" number. title ^ps \e\en(PS \e" restore PS ^sp 0.3i ^ft R ^^
2 The section number is kept in number register SH, which is incremented each time just before it is used. (A number register may have the same name as a macro without conflict but a string may not.)
We used \e\en(SH instead of \en(SH and \e\en(PS instead of \en(PS . If we had used \en(SH , we would get the value of the register at the time the macro was .ul defined, not at the time it was .ul used. If that's what you want, fine, but not here. Similarly, by using \e\en(PS , we get the point size at the time the macro is called. .WS
As an example that does not involve numbers, recall our .NP macro which had a
1 ^tl 'left'center'right'
2 We could make these into parameters by using instead
1 ^tl '\e\e*(LT'\e\e*(CT'\e\e*(RT'
2 so the title comes from three strings called LT, CT and RT. If these are empty, then the title will be a blank line. Normally CT would be set with something like
1 ^ds CT - % -
2 to give just the page number between hyphens (as on the top of this page), but a user could supply private definitions for any of the strings.