summaryrefslogtreecommitdiffstats
path: root/doc/groff.html.node/Punning-Names.html
blob: dde1a20f6d3109338f4620cad8a47ac65b8c1aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.0.3, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- This manual documents GNU troff version 1.23.0.

Copyright � 1994-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License". -->
<title>Punning Names (The GNU Troff Manual)</title>

<meta name="description" content="Punning Names (The GNU Troff Manual)">
<meta name="keywords" content="Punning Names (The GNU Troff Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Request-Index.html" rel="index" title="Request Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="GNU-troff-Reference.html" rel="up" title="GNU troff Reference">
<link href="Environments.html" rel="next" title="Environments">
<link href="Diversions.html" rel="prev" title="Diversions">
<style type="text/css">
<!--
div.example {margin-left: 3.2em}
kbd.key {font-style: normal}
-->
</style>


</head>

<body lang="en">
<div class="section-level-extent" id="Punning-Names">
<div class="nav-panel">
<p>
Next: <a href="Environments.html" accesskey="n" rel="next">Environments</a>, Previous: <a href="Diversions.html" accesskey="p" rel="prev">Diversions</a>, Up: <a href="GNU-troff-Reference.html" accesskey="u" rel="up">GNU <code class="code">troff</code> Reference</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Request-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h3 class="section" id="Punning-Names-1">5.30 Punning Names</h3>
<a class="index-entry-id" id="index-diversions-1"></a>

<p>Macros, strings, and diversions share a name space; recall
<a class="ref" href="Identifiers.html">Identifiers</a>.  Internally, the same mechanism is used to store
them.  You can thus call a macro with string interpolation syntax and
vice versa.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.de subject
Typesetting
..
.de predicate
rewards attention to detail
..
\*[subject] \*[predicate].
Truly.
    &rArr; Typesetting
    &rArr;  rewards attention to detail Truly.
</pre></div></div>

<p>What went wrong?  Strings don&rsquo;t contain newlines, but macros do.  String
interpolation placed a newline at the end of &lsquo;<samp class="samp">\*[subject]</samp>&rsquo;, and the
next thing on the input was a space.  Then when &lsquo;<samp class="samp">\*[predicate]</samp>&rsquo; was
interpolated, it was followed by the empty request &lsquo;<samp class="samp">.</samp>&rsquo; on a line by
itself.  If we want to use macros as strings, we must take interpolation
behavior into account.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.de subject
Typesetting\\
..
.de predicate
rewards attention to detail\\
..
\*[subject] \*[predicate].
Truly.
    &rArr; Typesetting rewards attention to detail.  Truly.
</pre></div></div>

<p>By ending each text line of the macros with an escaped
<code class="code">\<kbd class="key">RET</kbd></code>, we get the desired effect (see <a class="pxref" href="Line-Continuation.html">Line Continuation</a>).<a class="footnote" id="DOCF114" href="groff.html_fot.html#FOOT114"><sup>114</sup></a>
What would have happened if we had used only one backslash at a time
instead?
</p>
<p>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
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.xx \\$@
</pre></div></div>

<p>is
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">\\*[xx]\\
</pre></div></div>

<p>The latter calling syntax doesn&rsquo;t change the value of <code class="code">\$0</code>, which
is then inherited from the calling macro (see <a class="pxref" href="Parameters.html">Parameters</a>).
</p>
<p>Diversions can be also called with string syntax.  It is sometimes
convenient to copy one-line diversions to a string.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.di xx
the
.ft I
interpolation system
.ft
.br
.di
.ds yy This is a test of \*(xx\c
\*(yy.
    &rArr; This is a test of the <i class="i">interpolation system</i>.
</pre></div></div>

<p>As the previous example shows, it is possible to store formatted output
in strings.  The <code class="code">\c</code> escape sequence prevents the subsequent
newline from being interpreted as a break (again,
see <a class="pxref" href="Line-Continuation.html">Line Continuation</a>).
</p>
<p>Copying multi-output line diversions produces unexpected results.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.di xxx
a funny
.br
test
.br
.di
.ds yyy This is \*[xxx]\c
\*[yyy].
    &rArr; test This is a funny.
</pre></div></div>

<p>Usually, it is not predictable whether a diversion contains one or more
output lines, so this mechanism should be avoided.  With <abbr class="acronym">AT&amp;T</abbr>
<code class="code">troff</code>, 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.
</p>
<a class="index-entry-id" id="index-stripping-final-newline-in-diversions"></a>
<a class="index-entry-id" id="index-diversion_002c-stripping-final-newline"></a>
<a class="index-entry-id" id="index-final-newline_002c-stripping-in-diversions"></a>
<a class="index-entry-id" id="index-newline_002c-final_002c-stripping-in-diversions"></a>
<a class="index-entry-id" id="index-horizontal-space_002c-unformatting"></a>
<a class="index-entry-id" id="index-space_002c-horizontal_002c-unformatting"></a>
<a class="index-entry-id" id="index-unformatting-horizontal-space"></a>
<p>A clean solution to this problem is available in GNU <code class="code">troff</code>, using
the requests <code class="code">chop</code> to remove the final newline of a diversion, and
<code class="code">unformat</code> to make the horizontal spaces adjustable again.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">.box xxx
a funny
.br
test
.br
.box
.chop xxx
.unformat xxx
This is \*[xxx].
    &rArr; This is a funny test.
</pre></div></div>

<p>See <a class="xref" href="Gtroff-Internals.html"><code class="code">gtroff</code> Internals</a>.
</p>

</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Environments.html">Environments</a>, Previous: <a href="Diversions.html">Diversions</a>, Up: <a href="GNU-troff-Reference.html">GNU <code class="code">troff</code> Reference</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Request-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>