Looking to apply a consistent style to file and directory names. The book's appendix has a subsection for every file referenced by the book. Every subsection has a label whose value is that of the subsection. In this fashion, changing the label will change all the references throughout the book, accordingly.


The \path{} command in the URL package takes an argument and applies its style while taking into consideration backslashes. For example, the following works as expected:


What would be ideal is:


This will output \nameref{sub:cities.jsp} into the document, rather than the desired value of cities.jsp.

The purpose is so that if I change my mind about the file name, all I need to do is change the label "sub:cities.jsp" to "sub:city.jsp", for example, and the entire document now references "city.jsp". I use a subsection so that hyperlinks are automatically applied.


How can a parameter be added to \path{} that first expands \nameref before passing the argument to \path{}? That is, how can I write the following:


And have it become:


I have tried a number of variations, but I do not understand how to use expandafter with parameters (and even the simplest expandafter examples that I can find are Latin to me). I also want to keep the \path command unchanged (as I use it for directory names, not file names) and add a new command \filename that references the files by their corresponding subsection. Examples from the preamble:

Attempt #1

% \newcommand{\filename}[1]{\path{#1}}
% \expandafter\filename\path
\expandafter\def\filename#1{\nameref{#1}} \path

The first line gives me the \filename command, but does not expand the parameter until \path looks at it, which is not surprising.

The second line fails because the parameter is not taken into consideration, nor \nameref.

The third line generates "Undefined control sequence" errors.

Attempt #2

\def \filename #1{\nameref{#1}}
\expandafter\def\filename \path


  • Not an answer, but avoiding the problem: have you considered using \texttt{...} instead of \path? It depends on what kind of characters you expect to occur or not occur in your filenames. Commented Dec 11, 2010 at 21:17
  • @Ulrich: I have not considered that. I would like to use \path so that, when time comes, I can use the URL package to change the font of all paths everywhere. If I use a mix of \path and \texttt, then I have to change two things. Also, \texttt is formatting, it does not describe the content. An interesting, solution, though, if I cannot figure this out. Commented Dec 11, 2010 at 21:57

2 Answers 2


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


calls \path with the expanded file name variable as argument.

Complete minimal example:

The file name is \expath{\filecities}.

alt text

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.

  • The point of using the \path (at least from the previous question) was to allow c:\foo paths.
    – TH.
    Commented Dec 12, 2010 at 0:32
  • @TH.: This \expath (for the file name) works also with c:\foo. Besides that: Dave may still use \path but the point here is a \filename macro. I expect, unless stated otherwise, a file name doesn't contain a complete path. However, also this would be no problem.
    – Stefan Kottwitz
    Commented Dec 12, 2010 at 0:57
  • It should be mentioned that this solution only works for “neat” filenames not containing “_”, “%” etc.
    – mhp
    Commented Sep 22, 2011 at 7:44

I would take a more robust approach, using the \urldef macro of the url package to define filename macros:






\section{A Section}


The filename is \nameref{sub:citiesjsp}.


Note that macros defined with \urldef are robust. As a consequence, they can smoothly be used in moving arguments (e.g. in the argument of \subsection). Moreover, I think that \nameref is quite a good choice for referencing filenames in the current context since you get not only filenames, but also hyperlinks to the corresponding subsection headings.

The resulting output is:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .