Next: , Previous: , Up: GNU troff Reference   [Contents][Index]


5.31 Punning Names

Macros, strings, and diversions share a name space; recall Identifiers. Internally, the same mechanism is used to store them. You can thus call a macro with string interpolation syntax and vice versa.

.de subject
Typesetting
..
.de predicate
rewards attention to detail
..
\*[subject] \*[predicate].
Truly.
    ⇒ Typesetting
    ⇒  rewards attention to detail Truly.

What went wrong? Strings don’t contain newlines, but macros do. String interpolation placed a newline at the end of ‘\*[subject]’, and the next thing on the input was a space. Then when ‘\*[predicate]’ was interpolated, it was followed by the empty request ‘.’ on a line by itself. If we want to use macros as strings, we must take interpolation behavior into account.

.de subject
Typesetting\\
..
.de predicate
rewards attention to detail\\
..
\*[subject] \*[predicate].
Truly.
    ⇒ Typesetting rewards attention to detail.  Truly.

By ending each text line of the macros with an escaped RET, we get the desired effect; recall Line Continuation.174 What would have happened if we had used only one backslash in each case?

Interpolating a string does not hide existing macro arguments. We can also place the escaped newline outside the string interpolation instead of within the string definition. Thus, in a macro, a more efficient way of doing

.xx \\$@

is

\\*[xx]\\

The latter calling syntax doesn’t change the value of \$0, which is then inherited from the calling macro; recall Parameters.

It is sometimes convenient to copy a single-line diversion to a string, which can then be interpolated with \*.

.di xx
the
.ft I
interpolation system
.ft
.br
.di
.ds yy This is a test of \*(xx\c
\*(yy.
    ⇒ This is a test of the interpolation system.

In the foregoing, we see that formatted output can thus be stored in a string. The \c escape sequence prevents the subsequent newline from being interpreted as a break; again, recall Line Continuation.

Copying multi-output-line diversions produces unexpected results.

.di xxx
a funny
.br
test
.br
.di
.ds yyy This is \*[xxx]\c
\*[yyy].
    ⇒ test This is a funny.

Usually, it is not predictable whether a diversion contains one or more output lines, so this mechanism should be avoided. With AT&T troff, this was the only solution to strip off a final newline from a diversion. Another disadvantage is that the spaces in the copied string are already formatted, preventing their adjustment. This can cause ugly results.

A clean solution to this problem is available in GNU troff, using the requests chop to remove the final newline of a diversion, and unformat to make the horizontal spaces adjustable again.

.box xxx
a funny
.br
test
.br
.box
.chop xxx
.unformat xxx
This is \*[xxx].
    ⇒ This is a funny test.

See GNU troff Internals.


Next: , Previous: , Up: GNU troff Reference   [Contents][Index]