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.
(Further updated answer, with a look deep down the Lion's Mouth in the last section.)
Joseph gave an answer (“No!”) to your actual question; I'll try to get your (and my) head around the “temporarily”. Let's start from TeX-by-Topic, bottom of page 129:
The \noexpand
command is expandable, and its expansion is the following token. The meaning of that token is made temporarily equal to \relax
, so that it cannot be expanded further.
At first I dismissed this as a colourful description, but one can easily see that TeX-by-Topic is right: The following short plain TeX file
\def\foo{bar}
\expandafter\show\noexpand\foo
\bye
prints
> \foo=\relax.
<recently read> \notexpanded: \foo
to the terminal, so \foo
is indeed “made temporarily equal to \relax
”. What I find very interesting is the \notexpanded: \foo
; this \notexpanded
isn't mentioned in the TeXbook or in TeX-by-Topic, and I only found one occurrence in Knuth's torture test for TeX. (Another test is to put \tracingcommands=2 \noexpand\foo
into your TeX file; then you'll find a \relax
in your log file.)
In the TeXbook there's a description that I find easier chewable than the one from TeX-by-Topic (since it doesn't contain “temporarily”): On page 214 it says
\noexpand
⟨token⟩. The expansion is the token itself; but that token is interpreted as if its meaning were ‘\relax
’ if it is a control sequence that would ordinarily be expanded by TeX’s expansion rules.
So what does “temporarily” mean?
In practise, “temporarily equal to \relax
” means the following: When TeX expands \noexpand\foo
, the result is \foo
with the temporary meaning \relax
, and as soon as TeX continues expanding, \foo
gets back its original meaning. A typical use case of \noexpand
, from Tex-by-Topic (page 130):
\edef\one{\def\noexpand\two{\the\prevdepth}}
Inside the \edef
, TeX doesn't expand \two
but continues with the next token {
(which is also not expandable). The \noexpand
is needed since otherwise TeX would try to expand \two
. This would cause an error if \two
is undefined, and it would cause much more trouble if \two
has been defined before. (Before \def
one doesn't need a \noexpand
since \def
is not expandable.) If you now \show
the \one
, then you see that the real \two
, not a \relax
ed one is inside \one
:
> \one=macro:
->\def \two {-1000.0pt}.
What's happening behind the scenes?
Let me first say this in the picture of TeX's mouth and stomach. In general, TeX continues chewing on a token list in its mouth as long as the first token is expandable. When the first token is unexpandable, it's swallowed for further processing in the stomach. Now \noexpand
indeed wraps the token following it in some protective coating. However, this coating is chewable; figure that it's made of sugar. Thus, when TeX chews on such a coated token, the coating is removed (and the token is expandable again), but TeX gets a sensory flash and thinks “Wow, this tastes like \relax
, I want to swallow it.” And sure enough, if the now uncoated token is the first in TeX's mouth (think of “first” as “closest to the throat”), then it is swallowed.
Two examples (the 2nd one being rather academic):
You have, e.g.,
\edef\bar{\noexpand\foo\anothercs}
Then inside the \edef
, \noexpand\foo
expands to a \relax
ed \foo
. This is converted back to \foo
, immediately swallowed, and TeX continues with expanding the next token \anothercs
.
You expand \noexpand\foo
twice, e.g. with
\def\foo{bar}
\expandafter\expandafter\expandafter\show\noexpand\foo
\bye
Then on the terminal you get
> \foo=macro:
->bar.
Thus, expanding \noexpand\foo
once gives a \relax
ed \foo
(as seen above), and the expansion of the \relax
ed \foo
is again the original \foo
. And this is not swallowed since it's not the first token!
Disclaimer: The above I found out by observation; it might be that some details are not entirely correct.
Some final remarks
Your attempt at defining \noexpand
as macro fails for several reasons. One technical point is that \let#1\relax
is not a good idea if #1
isn't a control sequence. Another point is that it's not really helpful to do \let#1\relax #1
; this has the same effect as \relax
as far as I can tell. (And as pointed out in the other answers, \let
is not expandable.)
Best Answer
When TeX is skipping tokens in a false path of a conditional it looks at every token without expanding it and modifies an if-counter very similar to the brace counter. If the token is a primitive
\if...
token (e.g.\let
to any of TeX's original conditionals like\if
,\ifx
,\ifnum
, ...) the counter is increased and if it's identical to\fi
(i.e.\fi
or any macro let to it) the counter is decreased. The name of the macro is not important. Conditionals inside other macros are never seen.To answer your questions in detail:
\let\ifabc=\ifx
(so, to be clear, what happens in\iffalse ... \ifabc ... \fi
, does the\ifabc
count as an\if
?A: Yes,
\ifabc
counts as an if.\def\ifabc{\ifx}
A: No,
\ifabc
isn't aif
. It will work in the true path, because it is expanded there, but not in the false path.\edef\ifabc{\ifx}
A: This wont work, because
\ifx
is expanded and therefore awaits its two input tokens and the final\fi
. Even so, primitives can't be expanded to their underlying meaning, otherwise they wouldn't be primitives.\let\ifabc=\SomePreviouslyDefinedCommandThatIsntAnIf
A: As I've said above , the name doesn't matter; this is not an
if
.\let\abc=\ifx
A: The name doesn't matter; this is an
if
.For further information about the if-switches see The TeXBook in "Chapter 20: Definitions (also called Macros)", page 207 ff.