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
|
<head>
<title>UK TeX FAQ -- question label actinarg</title>
</head><body>
<h3>Active characters in command arguments</h3>
<p/>Occasionally, it’s nice to make one or two characters active in the
argument of a command, to make it easier for authors to code the
arguments.
<p/>Active characters <em>can</em> be used safely in such situations; but
care is needed.
<p/>An example arose while this answer was being considered: an aspirant
macro writer posted to <i>comp.text.tex</i> asking for help to
make <code>#</code> and <code>b</code> produce musical sharp and flat signs, respectively,
in a macro for specifying chords.
<p/>The first problem is that both <code>#</code> and <code>b</code> have rather important uses
elsewhere in TeX (to say the least!), so that the characters can
only be made active while the command is executing.
<p/>Using the techniques discussed in
“<a href="FAQ-activechars.html">characters as commands</a>”,
we can define:
<blockquote>
<pre>
\begingroup
\catcode`\#=\active
\gdef#{$\sharp$}
\endgroup
</pre>
</blockquote><p>
and:
<blockquote>
<pre>
\begingroup
\lccode`\~=`\b
\lowercase{\endgroup
\def~{$\flat$}%
}
</pre>
</blockquote><p>
The second problem is one of timing: the command has to make each
character active <em>before</em> its arguments are read: this means that
the command can’t actually “have” arguments itself, but must be
split in two. So we write:
<blockquote>
<pre>
\def\chord{%
\begingroup
\catcode`\#=\active
\catcode`\b=\active
\Xchord
}
\def\Xchord#1{%
\chordfont#1%
\endgroup
}
</pre>
</blockquote><p>
and we can use the command as <code>\</code><code>chord{F#}</code> or
<code>\</code><code>chord{Bb minor}</code>.
<p/>Two features of the coding are important:
<ul>
<li> <code>\</code><code>begingroup</code> in <code>\</code><code>chord</code> opens a group that is closed by
<code>\</code><code>endgroup</code> in <code>\</code><code>Xchord</code>; this group limits the change of
category codes, which is the <em>raison d’être</em> of the whole
exercise.
<li> Although <code>#</code> is active while <code>\</code><code>Xchord</code> is executed, it’s
<em>not</em> active when it’s being defined, so that the use of <code>#1</code>
doesn’t require any special attention.
</ul>
<p/>Note that the technique used in such macros as <code>\</code><code>chord</code>, here, is
analogous to that used in such commands as <code>\</code><code>verb</code>; and, in just the
same way as <code>\</code><code>verb</code> (see
“<a href="FAQ-verbwithin.html"><code>\</code><code>verb</code> doesn’t work in arguments</a>”),
<code>\</code><code>chord</code> won’t work inside the argument of another command (the
error messages, if they appear at all, will probably be rather odd).
<p/><p/><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=actinarg">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=actinarg</a>
</body>
|