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. So
\expandafter\def\csname an-awkward-name\endcsname
will expand \csname
before \def
. So after one expansion the above turns into
\def\an-awkward-name
which 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). So
\def\examplea{more stuff}
\edef\exampleb{Some stuff \csname examplea\endcsname}
will 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 read
\def\examplea{more stuff}
\edef\exampleb{Some stuff \expandafter\noexpand\csname examplea\endcsname}
then what will happen is that the \edef
will execute the \expandafter
, which will turn the above effectively into
\def\examplea{more stuff}
\edef\exampleb{Some stuff \noexpand\examplea}
Now 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 like
\csname name\expandafter\endcsname\token
which 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 like
\edef\cohtheory{%
\noexpand\newcommand\expandafter\noexpand\csname foofunc\endcsname[1][*]{%
\noexpand\MakeUppercase{foo}^{##1}}%
}
What 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
\begingroup
\edef\temp{%
\endgroup
\noexpand\command
{\unexpanded\expandafter{\argone}}%
{\unexpanded\expandafter{\argtwo}}%
}
\temp
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.
Change \noexpand
to \expandafter
, that's what you need to "skip" that \fi
. As well, as egreg points out, \if\null
won't work. Either use \ifx
, or change \null
to \relax
and hope it is not contained in your text. The reason why \relax
will work is that it is unexpandable and \if
takes it, instead of expanding. For the reason that \relax
or \null
might be used by someone else, a much safer option is to use a command that doesn't exist, like \thisIStoheczSdelimiter
:
\def\Macro#1{\ifx\thisIStoheczSdelimiter#1 . \else ,\expandafter\Macro \fi}
\Macro foogg\thisIStoheczSdelimiter
As for working foreach
cycles, look into pgffor
package. Your code, IIRC, could be rewritten as
\foreach \x in {foo, bar, baz}{ var = \x, }
Best Answer
Searching for
\edef
or\xdef
(global variant) with command to be defined and argument in TeX Live 2015 and filtering out possible false positives (when the command is constructed with\csname
):Result are 219 lines.
I do not want to discuss all findings, thus I restrict myself to few examples.
Example
\TextOrMath
of the LaTeX kernel (latex.ltx
)Recently, macro
\TextOrMath
was added to the LaTeX kernel:Here, the
\edef
has the purpose to construct a macro name with a space in it:\TextOrMath␣
. The definition would simpler, if the space would be a letter:But, this is not possible with the usual category code of the space. An alternative with
\def
and lots of\expandafter
is more cumbersome:Therefore,
\edef
works on the non-argument part of the definition text. To make it clear, arguments are not expanded during the definition with\edef
,#1
to#9
are non-expandable tokens.Example
\split@err
of packageamsmath
Another example can be found in package
amsmath
:Here, the argument is not used at all, it is ignored as part of the error recovery.
Example
\mleftright@Def
of packagemleftright
Package
mleftright
has an example, where the argument is used three times:The purpose of the expansion is
\mleftright@Def
inside the definition. Previously, it was defined as\protected\def
,\DeclareRobustCommand*
or\def
, depending, which method is available (e-TeX, LaTeX, or neither of them).Arguments
#1
, ...At definition time, the argument place holders (
#1
,#2
, ...,#9
) are just two non-expandable tokens, the hash character#
and the digit1
to9
. Therefore, there isn't a difference between\edef
or\def
, because\edef
cannot expand non-expandable tokens.The argument place holders get replaced with the actual arguments, when the macro is used, that is after the definition of the macro.