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!}
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!}
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}

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).
\newcommand{\filepath}[1]{\protect\path{#1}} which does. Thanks!
– Albert
Jun 07 '13 at 09:26
\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
perhaps the best way is to use
\usepackage{url}
\DeclareUrlCommand\filepath{}

\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.
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 06 '13 at 18:45
\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 06 '13 at 18:52
\section{File \filepath{foo.cpp}}.
– Albert
Jun 06 '13 at 19:04
\path command provided by url Can you please add it to your answer so that I will delete mine :-)
–
Jun 06 '13 at 22:59
\lstinputlisting– Marco Daniel Jun 06 '13 at 18:40