#
and b
produce musical sharp and flat signs, respectively,
in a macro for specifying chords.
The first problem is that both #
and b
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.
Using the techniques discussed in
“characters as commands”,
we can define:
\begingroup \catcode`\#=\active \gdef#{$\sharp$} \endgroup
and:
\begingroup \lccode`\~=`\b \lowercase{\endgroup \def~{$\flat$}% }
The second problem is one of timing: the command has to make each character active before its arguments are read: this means that the command can’t actually “have” arguments itself, but must be split in two. So we write:
\def\chord{% \begingroup \catcode`\#=\active \catcode`\b=\active \Xchord } \def\Xchord#1{% \chordfont#1% \endgroup }
and we can use the command as \
chord{F#}
or
\
chord{Bb minor}
.
\
begingroup
in \
chord
opens a group that is closed by
\
endgroup
in \
Xchord
; this group limits the change of
category codes, which is the raison d’être of the whole
exercise.
#
is active while \
Xchord
is executed, it’s
not active when it’s being defined, so that the use of #1
doesn’t require any special attention.
\
chord
, here, is
analogous to that used in such commands as \
verb
; and, in just the
same way as \
verb
(see
“\
verb
doesn’t work in arguments”),
\
chord
won’t work inside the argument of another command (the
error messages, if they appear at all, will probably be rather odd).
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=actinarg