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!}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
Albert
  • 2,707

2 Answers2

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 07 '13 at 09: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}} –  Jun 07 '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.

David Carlisle
  • 757,742