xref: /freebsd/share/doc/usd/22.trofftut/tt11 (revision e1e636193db45630c7881246d25902e57c43d24e)
This module is believed to contain source code proprietary to AT&T.
Use and redistribution is subject to the Berkeley Software License
Agreement and your Software Agreement with AT&T (Western Electric).
Copyright (C) Caldera International Inc. 2001-2002. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code and documentation must retain the above
copyright notice, this list of conditions and the following
disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

All advertising materials mentioning features or use of this software
must display the following acknowledgement:

This product includes software developed or owned by Caldera
International, Inc. Neither the name of Caldera International, Inc.
nor the names of other contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.

USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


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.