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
|
<head>
<title>UK TeX FAQ -- question label ltxhash</title>
</head><body>
<h3>Defining LaTeX commands within other commands</h3>
<p>LaTeX command definition is significantly different from the TeX
primitive form discussed in an
<a href="FAQ-hash.html">earlier question</a> about definitions within
macros.
<p>In most ways, the LaTeX situation is simpler (at least in part
because it imposes more restrictions on the user); however, defining a
command within a command still requires some care.
<p>The earlier question said you have to double the <code>#</code> signs in command
definitions: in fact, the same rule holds, except that LaTeX
already takes care of some of the issues, by generating argument lists
for you.
<p>The basic problem is that:
<blockquote>
<pre>
\newcommand{\abc}[1]{joy, oh #1!%
\newcommand{\ghi}[1]{gloom, oh #1!}%
}
</pre>
</blockquote><p>
followed by a call:
<blockquote>
<pre>
\cmdinvoke{abc}{joy}
</pre>
</blockquote><p>
typesets “joy, oh joy!”, but defines a command <code>\</code><code>ghi</code> that takes
one parameter, which it ignores; <code>\</code><code>ghi{gloom}</code> will expand to
“gloom, oh joy!”, which is presumably not what was expected.
<p>And (as you will probably guess, if you’ve read the earlier question)
the definition:
<blockquote>
<pre>
\newcommand{\abc}[1]{joy, oh #1!%
\newcommand{\ghi}[1]{gloom, oh ##1!}%
}
</pre>
</blockquote><p>
does what is required, and <code>\</code><code>ghi{gloom}</code> will expand to
“gloom, oh gloom!”, whatever the argument to <code>\</code><code>abc</code>.
<p>The doubling is needed whether or not the enclosing command has an
argument, so:
<blockquote>
<pre>
\newcommand{\abc}{joy, oh joy!%
\newcommand{\ghi}[1]{gloom, oh ##1!}%
}
</pre>
</blockquote><p>
is needed to produce a replica of the <code>\</code><code>ghi</code> we defined earlier.
<p><p><p><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=ltxhash">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=ltxhash</a>
</body>
|