# How to typeset a file path?

I would like to properly typeset a file path, e.g.

C:\Program Files\Some program\bin\executable.exe


The first problem I encountered was that LaTeX tries to treat \Program as a command. I tried escaping it with another backslash which created a linebreak.

Then I found this package menukeys but I am not really happy because it typesets system-specific delimiters (i.e. \ for Windows and / for Unix-like) as some weird arrows.

Which leads to my question: what is the preferred way of typesetting a file path in LaTeX?

• @Mico Oh, thanks, did it like in the wikibook. Did not occur to me at firt that url will be the responsible package but now it works. You still have to escape the \ though. – alisianoi Mar 8 '14 at 18:54
• Sorry, I meant to write \url{C:\Program Files\Some program\bin\executable.exe}. – Mico Mar 8 '14 at 18:58

You could load the package url with the option obeyspaces and encase the fully qualified file name in a \url instruction:

\documentclass{article}
\usepackage[obeyspaces]{url}
\begin{document}
\path{C:\Program Files\Some program\bin\executable.exe} % equivalent to \url{...}, but more semantic
\end{document}


Addendum: If you need or wish to allow line breaks at the spaces, choose the spaces option as well:

\usepackage[obeyspaces,spaces]{url}

• Package url also defines the command \path{<some\thing>}. It is doing just the same, but it seems semantically better. – Johannes_B Mar 8 '14 at 22:16
• @Masi - The Option clash ... message means that the url package has already been loaded, but without either the spaces or the obeyspaces option. Check your preamble and the packages that are loaded by your preamble. – Mico Sep 11 '15 at 9:54
• Thank you for your answer! I got confused by another problem, which I opened here tex.stackexchange.com/q/267061/13173 Running your command in MacTeX 2015 gives apparently wrong output. – Léo Léopold Hertz 준영 Sep 11 '15 at 10:22
• @Johannes_B - The \path macro may indeed seem "semantically better", at first. However, it's not entirely identical to \url: If the hyperref package is loaded, using \url will create a hyperlink, whereas using \path does not. – Mico Sep 11 '15 at 12:54
• @EL_DON - Indeed, it’s the pointlessness of providing a URL to a nonaccessible place on someone’s hard disk that explains best why the output value of \path is not made into a URL. – Mico Jul 19 '18 at 21:32

I'm quite fond of menukeys, and I think you should still consider using it.

Unfortunately, menukeys currently doesn't provide its users a way of customising the output path separator (the "weird arrow" you're referring to). However, I'm sure Tobias "Tobi" Weh (the author of menukeys) would add that feature in a future release if he sees this question.

In the meantime, here is a way of customising the path separator.

\documentclass{article}

\makeatletter

% --- macro for changing path sep ---
\newcommand\setnewpathsep[1]
{%
\tw@declare@style@simple*{paths}{%
}[%
#1%
]{blacknwhite}
}

% --- reset the path separator (macro expands to original style def) ---
\newcommand\resetpathsep
{%
\tw@declare@style@simple*{paths}{%
}[%
\hspace{0.2em plus 0.1em}%
\raisebox{0.08ex}{%
-- (0,1ex) -- cycle;}%
}%
\hspace{0.2em plus 0.1em}%
]{blacknwhite}
}

\makeatother

\begin{document}
% original style
\directory{C:/Program Files/Some program/bin/executable.exe}

% Windows style
\setnewpathsep{\textbackslash}
\directory{C:/Program Files/Some program/bin/executable.exe}

% Unix style
\setnewpathsep{/}
\directory{C:/Program Files/Some program/bin/executable.exe}

% back to original style
\resetpathsep
\directory{C:/Program Files/Some program/bin/executable.exe}
\end{document}

• There is a way to change the output sep, but it’s not the good documented ;-) so thanks for your answer so far. I plan a new version, since the user interface isn't that good an the manual neither … but I can't tell when :-/ – Tobi Mar 8 '14 at 22:39
• @Tobi No problem. Looking forward to the next release, but no pressure... – jub0bs Mar 8 '14 at 23:23
• :-) You may like to follow on GitHub, I think I need some Beta-Testers ;-) An there you can add feature request too … – Tobi Mar 9 '14 at 2:11
• @Tobi I'll do that. – jub0bs Mar 25 '14 at 11:08
• You can even use backslashes in your path (copy-paste path) with the bslash option like so: \directory[bslash]{X:\DIR\SUBDIR}. Unfortunately, your \setnewpathsep{\textbackslash} command removes line-breaks :-\ You'll need to change your makro to something like this:  \makeatletter % --- macro for changing path sep --- \newcommand\setnewpathsep[1] {% \tw@declare@style@simple*{paths}{% {\ttfamily\CurrentMenuElement}% }[% \hspace{0.1em plus 0.1em}% #1 \hspace{0.1em plus 0.1em}% ]{blacknwhite} } \makeatother – gilu Mar 3 '16 at 6:40