[Tex/LaTex] Using \nameref within \path

expansionpathsurls

Background

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.

Problem

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:

\path{cities.jsp}

What would be ideal is:

\path{\nameref{sub:cities.jsp}}

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.

Question

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:

\filename{sub:cities.jsp}

And have it become:

\path{cities.jsp}

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

Related

Best Answer

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}

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.