3

I want to define a command \filepath so that I can use \filepath{foo_bar.cpp} and it is printed in monospaced font.

Also, why does this not work? I get strange errors:

\newcommand{\filepath}[1]{\verb!#1!}
  • You can use the listings package with \lstinputlisting – Marco Daniel Jun 6 '13 at 18:40
  • How? It should not print the content but the filename. Like I'm trying to do in the title of the question. – Albert Jun 6 '13 at 18:42
  • 1
    possible duplicate of How do I define a custom verbatim command? – Marco Daniel Jun 6 '13 at 18:52
  • @MarcoDaniel: The answer here is better than in the other question. For example, there is always a possible character where it would fail but I don't want that. – Albert Jun 6 '13 at 18:57
  • @MarcoDaniel: Which one? I just checked url.sty and it seems they have found a (complicated) way to read the argument verbatim (search "reading the argument verbatim"). – Albert Jun 6 '13 at 19:01
10

I think you are re-inventing the wheel. The package url provides a command \path.

\documentclass[obeyspaces]{article}
\usepackage{hyperref}
%\usepackage{url}
\begin{document}

\url{aa/bb_cc/dd.foo}

\path{aa/bb_cc/dd.foo}

\path{C:\Program Files\Mozilla Firefox}
\end{document}

enter image description here

The option obeyspaces is passed to url package (which is loaded by hyperref) so that spaces in the path are respected (see the third line in image).

  • That does not work in a title. But I'm using now \newcommand{\filepath}[1]{\protect\path{#1}} which does. Thanks! – Albert Jun 7 '13 at 9:26
  • @Albert Fragile commands need to be \protected in moving arguments. Or you may use the optional arguments for titles: \section[short title]{{File \path{foo.cpp}} – user11232 Jun 7 '13 at 13:33
9

perhaps the best way is to use

\usepackage{url}
\DeclareUrlCommand\filepath{}

enter image description here

\documentclass{article}

\usepackage{hyperref}

%\usepackage{url}
\DeclareUrlCommand\filepath{}

\begin{document}

\url{aa/bb_cc/dd.foo}

\filepath{aa/bb_cc/dd.foo}

\end{document}

Note \url becomes clickable but not \filepath in the hyperref version.

  • I already have hyperref included and I use \url for URLs. It's almost like I want it to be for \filepath, except that it is clickable. – Albert Jun 6 '13 at 18:45
  • @David: This time your miracle failed ;-) – Marco Daniel Jun 6 '13 at 18:48
  • @Albert Ok so you just want the original url.sty version without the hyper support, I will just check the incantation required... – David Carlisle Jun 6 '13 at 18:51
  • I just tested and it seems that the version from \DeclareUrlCommand is not clickable but other \urls are, so it's already like I want it. Whereby I would not have expected it to not be clickable. – Albert Jun 6 '13 at 18:52
  • @Albert it works fro me, see update – David Carlisle Jun 6 '13 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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