Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Ulrich Schwarz Dec 11 '10 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. –  Dave Jarvis Dec 11 '10 at 21:57
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
The point of using the \path (at least from the previous question) was to allow c:\foo paths. –  TH. Dec 12 '10 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 Dec 12 '10 at 0:57
    
It should be mentioned that this solution only works for “neat” filenames not containing “_”, “%” etc. –  mhp Sep 22 '11 at 7:44
add comment

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

\documentclass{article}

\usepackage{hyperref}

\urldef\citiesjsp\path{cities.jsp}

\begin{document}

\tableofcontents

\section{A Section}

\subsection{\citiesjsp}
\label{sub:citiesjsp}

The filename is \nameref{sub:citiesjsp}.

\end{document}

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:

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.