Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Fifa Earth Cup 2014 Jan 2 '13 at 14:14
1  
@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
2  
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
show 5 more comments

1 Answer

up vote 19 down vote accepted
+150

Add the following load order to your preamble:

\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.

enter image description here

share|improve this answer
    
When I click the link, the browser fails to load the file because of additional 25 inserted between % and 20. Please see these screenshots. –  Fifa Earth Cup 2014 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. –  Fifa Earth Cup 2014 Feb 15 '13 at 17:36
add comment

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.