TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
@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. – all3fox 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
up vote 21 down vote accepted

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

enter image description here

\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}
share|improve this answer
7  
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
1  
@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. – Masi Sep 11 '15 at 10:22
1  
@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

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.

enter image description here

\documentclass{article}

\usepackage{menukeys}

\makeatletter

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

% --- reset the path separator (macro expands to original style def) ---
\newcommand\resetpathsep
{%
     \tw@declare@style@simple*{paths}{%
       {\ttfamily\CurrentMenuElement}%
    }[%
       \hspace{0.2em plus 0.1em}%
       \raisebox{0.08ex}{%
          \tikz{\fill[\usemenucolor{txt}] (0,0) -- (0.5ex,0.5ex)%
                    -- (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}
share|improve this answer
3  
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... – Jubobs Mar 8 '14 at 23:23
2  
:-) 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. – Jubobs 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 at 6:40

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.