8.  Introduction to Macros

      Before we can go much further in troff, we need to learn a bit about the macro facility. In its simplest form, a macro is just a shorthand notation quite similar to a string. Suppose we want every paragraph to start in exactly the same way _ with a space and a temporary indent of two ems: ^sp
^ti +2m
Then to save typing, we would like to collapse these into
one shorthand line,
a
troff
`command' like
^PP
that would be treated by
troff
exactly as
^sp
^ti +2m
.PPis called a
macro.
The way we tell
troff
what
.PPmeans is to
define
it with the
.decommand:
^de PP
^sp
^ti +2m
^^
The first line names the macro
(we used
`.PPfor `paragraph',
and upper case so it wouldn't conflict with
any name that
troff
might
already know about).
The last line
..marks the end of the definition.
In between is the text,
which is simply inserted whenever
troff
sees the `command'
or macro call
^PP
A macro
can contain any mixture of text and formatting commands.

      The definition of .PPhas to precede its first use; undefined macros are simply ignored. Names are restricted to one or two characters.

      Using macros for commonly occurring sequences of commands is critically important. Not only does it save typing, but it makes later changes much easier. Suppose we decide that the paragraph indent is too small, the vertical space is much too big, and roman font should be forced. Instead of changing the whole document, we need only change the definition of .PPto something like ^de PP \" paragraph macro
^sp 2p
^ti +3m
^ft R
^^
and the change takes
effect everywhere we used
.PP

      \"is a troff command that causes the rest of the line to be ignored. We use it here to add comments to the macro definition (a wise idea once definitions get complicated).

      As another example of macros, consider these two which start and end a block of offset, unfilled text, like most of the examples in this paper: ^de BS \" start indented block
^sp
^nf
^in +0.3i
^^
^de BE \" end indented block
^sp
^fi
^in -0.3i
^^
Now we can surround text like
Copy to
John Doe
Richard Roberts
Stanley Smith
by the commands
.BSand
.BEand it will come out as it did above.
Notice that we indented by
.in +0.3iinstead of
.in 0.3iThis way we can nest our uses of
.BSand
BEto get blocks within blocks.

      If later on we decide that the indent should be 0.5i, then it is only necessary to change the definitions of .BSand .BEnot the whole paper.