How to prevent \url from removing the spaces in a path?

The following code

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{"a b c.tex"}
one
two
\end{filecontents*}

\usepackage{listings}
\usepackage{hyperref}

\begin{document}
\lstinputlisting[caption={\url{a b c.tex}}]{"a b c.tex"}
\end{document}


produces a hyperlink with suppressed spaces given in the path.

How to prevent \url from removing the spaces in a path?

• I'd encode the spaces anyway, as %20. It's annoying, and ugly, but clearer for someone who may be copying your URL, as users do not expect URLs to contain spaces. – TRiG Oct 13 '12 at 23:22
• Are you not happy with the answer you received to this question? It's usually a good idea to specify what's missing from existing answers when offering a bounty on a question, so people know how they can help you better. – Jake Jan 2 '13 at 14:11
• @Jake: I am very happy with the existing answer. – kiss my armpit Jan 2 '13 at 14:14
• @AdorableCreature: Ah, okay. A better/cheaper way to emphasise the relation between the two questions might be to post a link to this question as a comment on the other question, which will create an entry in the "Linked" list on the right-hand side. Also, you could vote to close as a duplicate, which (if successful) creates a very prominent link at the top of the other question. – Jake Jan 2 '13 at 14:34
• URL (or URI, URN...) can't contain space character: it's an unsafe character! A space must be encoded (%20). See RFC 1738. – Paul Gaborit Jan 2 '13 at 22:33

\usepackage[obeyspaces]{url}% http://ctan.org/pkg/url
\usepackage{hyperref}% http://ctan.org/pkg/hyperref


See the UK TeX FAQ entry Typesetting URLs. Or, since hyperref loads url by default, perhaps with other package options, use:

\PassOptionsToPackage{obeyspaces}{url}% ~ \usepackage[...,obeyspaces]{url}
\usepackage{hyperref}% http://ctan.org/pkg/hyperref


See Option clash for package, also from the UK TeX FAQ.

• When I click the link, the browser fails to load the file because of additional 25 inserted between % and 20. Please see these screenshots. – kiss my armpit Feb 15 '13 at 8:26
• @GarbageCollector: I'm having no such problems, even when setting Google Chrome to my default application to open .tex files. The file opens as expected. – Werner Feb 15 '13 at 17:31
• OK. Thanks. I have to try the earlier version of Adobe Reader. – kiss my armpit Feb 15 '13 at 17:36

Building on @Werner's answer I found the following was necessary in the preamble ...

\usepackage[obeyspaces]{url}
\PassOptionsToPackage{obeyspaces}{url}

% Other packages which might use the url package.

\usepackage{hyperref}


That is, in practice one needs to use \PassOptionsToPackage just after the package you want to protect, rather than just before the conflicting package you suspect is causing the problem (in this example: hyperref). In other words, I had to move ....

\usepackage[obeyspaces]{url}
\PassOptionsToPackage{obeyspaces}{url}


... toward the top of my preamble, keeping \usepackage{hyperref} toward the bottom, in order to handle other packages that might be calling url or hyperref

• \PassOptionsToPackage after the package is already loaded, does not make much sense. hyperref loads package url without any option. Either the package url is loaded before with the option: \usepackage[obeyspaces]{url}. Or \PassOptionsToPackage before loading of url and hyperref can be used: \PassOptionsToPackage{obeyspaces}{url}\usepackage{hyperref}. – Heiko Oberdiek May 18 '16 at 4:42