I'm quite happy hacking TeX macros and cobbling together bits and pieces from different style files to suit my own ends, but I have a suspicion that my resulting hacks are not quite as elegant as they could be. In particular, with regard to when to expand and when not to expand macros.
A common occurrence for me is defining a meta-command that defines a whole slew of sub-commands. So the names of the sub-commands will contain parameters depending on the parameter passed to the meta-command, and the contents of the sub-commands will also vary a little depending on what the meta-command got. Often it won't be a direct substitution but rather a "if #1
is a
do \this
else do \that
", but \this
and \that
need expansion at define time, not call time.
Here's a very simple example just involving direct substitution:
\def\cohtheory#1{
\expandafter\newcommand\expandafter{\csname #1func\endcsname}[1][*]{%
\MakeUppercase{#1}^{##1}}
}
Sometimes I worry that my commands are more complicated than they need be. For example, if I want to call a command with two arguments and the arguments expand before the command, here's how I've coded it:
\expandafter
\expandafter
\expandafter
\command\expandafter
\expandafter
\expandafter
{\expandafter
\argone
\expandafter
}\expandafter
{\argtwo}
So I'm looking for guidance on when and how to control expansion in defining macros. I strongly suspect that such cannot be given in a simple answer, so to make this a focussed question, let me phrase it thus:
Where's a good reference for writing TeX macros that includes advice on how to best deal with how to handle expansions?
Of course, if anyone can formulate some advice in short answer, I'd be only to happy to read it.
(Note: this was partially motivated by juannavarroperez's adaptation of my answer to this question where over 20 \expandafter
s got condensed down to just 1!)
Best Answer
Expansion is a complicated area of TeX programming. I'll try to explain the key primitives involved first, then try to come up with some examples.
The
\expandafter
primitive expands the token after the next one. Sowill expand
\csname
before\def
. So after one expansion the above turns intowhich will then do its thing. Life becomes more complex when you want to step further ahead, and it soon becomes very hard to track what is going on.
The
\edef
> primitive does a full expansion of what is given as its argument (in contrast to\def
, which simply stores the input). Sowill expand the
\csname name\endcsname
to\examplea
, then expand that to leave a final definition of\exampleb
as 'Some stuff more stuff'.Now,
\noexpand
comes in by preventing\edef
from doing an expansion of the next token. So if I modify my above example to readthen what will happen is that the
\edef
will execute the\expandafter
, which will turn the above effectively intoNow the
\noexpand
will operate (disappearing in the process), leaving the definition of\exampleb
as 'Some stuff \examplea'.We can use this ability to cut down on
\expandafter
use, but there are a couple of other things to know. First, e-TeX includes an additional primitive\unexpanded
, which will prevent expansion of multiple tokens. Secondly, there are various special cases where you don't need quite so many\expandafter
statements. A classic example is from within\csname
, as this will do expansion anyway. So you'll see things likewhich will expand
\token
before\name
.Back to your example. In the first one, there isn't much to do: as the entire point is to have a dynamic name (
#1
), doing an\edef
at point-of-definition doesn't really make sense. The closest one can get is something likeWhat will happen here is that
\newcommand
and\MakeUppercase
will be protected from expansion, and the\csname
will only expand once. (Tokens which don't have an expansion don't need protection, which is why things like '[1]' are simply included as is.) Of course, this is something of a 'toy' as all it does is create a fixed\foofunc
.For your second example, you could instead to
I'm using a couple of extra ideas here. First, the group is used so that
\temp
is not altered anywhere other than where I'm using it. The\endgroup
primitive will do nothing inside the\edef
, and so will still be there to close the group when\temp
is used. Secondly,\unexpanded
works like a toks, and so will respect the\expandafter
after it but before the{
. This cuts down on an unnecessary\expandafter
.There are more wrinkles to this, and often there are several equally-efficient and clear methods. You are best off posting specific examples, and seeking advice on how they might be achieved.