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.
David has explained this one way, I will take a slightly different tack.
First, what is going on? When e-TeX finds a protected macro, it will not expand it inside an \edef
, \write
and some similar circumstances, which usually exhaustively expand everything. That is to ensure you can see the protected token in the result. For example,
\protected\def\a{}
\typeout{%
\a%
b%
}
will show \a b
, even though there is no space between \a
and b
as far as TeX is concerned. That's because the alternative is \ab
, which as we can't see the tokens would be misleading (does it mean \a
followed by b
or a different macro \ab
?).
You can 'force' expansion of a protected macro by using the fact that they do respect \expandafter
:
\protected\def\a{b}
\edef\test{\expandafter\empty\a}
\show\a
using \empty
as something to expand after which will not result in anything remaining behind.
Second, why do you need protection? Some operations in TeX simply will not work within an \edef
as they use TeX primitives which are not 'expandable'. The classic ones here are assignments (\def
, \let
and so on). If you try
\let\a\undefined
\def\b{a}
\edef\test{\let\a\b}
you will not find that \a
ends up defined to give a
. Instead, you'll get an error: in this case 'Undefined control sequence
'. That is because \let
is not expandable. So TeX simply 'leaves it alone' inside the \edef
, and tries to expand \a
. That's not possible, and so an error arises. Thus in general it is not a good idea to try to expand protected macros.
Best Answer
You can't use
\edef
for commands accepting optional arguments; or, at least, not in this way. The paragraphs are stored in commands calledand so on. You can emulate your
\edef
byThen
\unpacklipsum{1}{2}{\mycommand}
will do what you need: this will store in\mycommand
the paragraphs from 1 to 2. Paragraph number 42 can be stored in\mycommand
with