diff options
Diffstat (limited to 'Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-actinarg.html')
-rw-r--r-- | Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-actinarg.html | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-actinarg.html b/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-actinarg.html new file mode 100644 index 00000000000..7d024d8dd5c --- /dev/null +++ b/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-actinarg.html @@ -0,0 +1,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> |