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.
So, your intention is: just one place for the file name. If needed, you only have to make a change at this place. That's very good! However, I would not choose subsection headings within the document body for such places.
I suggest to define macros for file names all at one place in the preamble.
Once you have done this, you could easily use \expandafter
. Further I guess it may be harder to use \expandafter
with referencing commands such as \nameref
or \autoref
. With normal macros it's easy. For example
\newcommand*{\expath}[1]{\expandafter\path\expandafter{#1}}
calls \path
with the expanded file name variable as argument.
Complete minimal example:
\documentclass{article}
\usepackage{url}
\newcommand*{\filecities}{cities.jsp}
\newcommand*{\expath}[1]{\expandafter\path\expandafter{#1}}
\begin{document}
\tableofcontents
\section{Test}
\subsection{\filecities}
The file name is \expath{\filecities}.
\end{document}
If the file name changes, just modify \filecities
.
I've tested the same way of expansion with \nameref
but it didn't work - but in any case I would not depend on definitions in headings within the body, for such work I rely on the preamble.
Best Answer
Let's see what happens
Given the first line, the second is equivalent to
Now, when you say
TeX expands the inner
\myline
completely and this is what it becomes (result of\show\myline
edited to avoid bad line breaks):which is surely not what TeX likes to see when executing the newly defined
\myline
.The problem is that
\noexpand
inhibits expansion once: in the second\edef
, the token\bf
will be expanded. This is a "correct" way to proceed:Note that
\protect
in front of\bfseries
is not needed, as\bfseries
is already a "LaTeX robust command".Another way is to use
etoolbox
:This will make
\bfseries
"protected forever" inside an\edef
.Martin suggests in his answer to use
\appto
, which is quite a good way to build a macro by accretion:In this case we get the same result as
If the accretion has to be global, you can use
\gappto
(which is equivalent to the kernel macro\g@addto@macro
).Another strategy is to use token registers:
Of course the register can be used as many times as one likes, it's necessary to allocate it only once. The kernel provides
\addto@hook
:For using the contents of the register, you say