53

I'm trying to make a URL with a tilde character (~) in it:

\usepackage{url}
...
\url{http://lyle.smu.edu/~tspell/jaws}

It looks right in the resulting PDF, but when clicking on it the browser goes to a wrong URL: http://lyle.smu.edu/%18tspell/jaws

If I type in the browser %7e (escape code for '~') instead of %18 the browser then goes to the correct URL.

Any idea how to get a correct link in the PDF?

2

4 Answers 4

48

You should use the hyperref package (which uses url internally but improves on it). It will not only format the link but also place a real hyperlink into the PDF. With url alone the link is only normal text, but is probably recognized as hyperlink by your PDF viewer, but with the wrong code for ~. LaTeX tends to substitute the code of these special characters depending on the font you use instead of taking the ASCII code.

This works fine with Acrobat Reader:

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\url{http://lyle.smu.edu/~tspell/jaws}
\end{document}

A funny thing is that %18 is the ASCII code for "CAN (cancel)", which doesn't make much sense and \char"18 isn't the tilde in the tt font. I can't follow how this specific code was produced.

8
  • 1
    Wow, this is one great package! (and it solved the problem too - thanks). Feb 11, 2011 at 18:20
  • 3
    @Hagai Cibulski Be careful with hyperref: it is temperamental, as a search of this site will reveal. It likes being the last package loaded (apart from packages that need loading after it)...
    – Seamus
    Feb 15, 2011 at 12:36
  • I think that the problem is the hyperref changes the definition of things, (like chapter and section and toc and cite entries) by adding a link to them, this will fail if you load another package that changes the way these command are defined. Feb 15, 2011 at 15:43
  • 2
    @Hagai: in this case use hyperref s \nolinkurl macro, and disable the PDF bookmarks. Feb 16, 2011 at 8:49
  • 1
    @MartinScharrer I took your MWE, compiled it with MiKTeX 2.9, opened the pdf with Adobe Reader 11, clicked the link, which was opened in FF 19, but it tried to open http://lyle.smu.edu/%257Etspell/jaws (%257E is ~ encoded twice, i.e. % in %7E (~) encoded into %25). SumatraPDF and PDF-X Change Viewer opened the correct link in FF, so it might even be a bug / undesirable behavior on the side of Adobe Reader? Any idea if we can work around this?
    – doncherry
    Mar 7, 2013 at 22:49
11

The ~ character will work fine if used inside \url{} or \hyperref{} within your text because they automatically disable all the TeX parsing; as described by Martin Scharrer. The same is true for other characters like _ and for the \path{} command provided by the url package.

But if you have a URL within your bibliography’s .bib file it won’t work. The solution to this problem is to URL encode the character. The tilde would be encoded as %7E. The bibliography entry would look like this:

@article{Spell.2009,
  author = {Spell, Brett},
  date   = {2009-12-24},
  title  = {Java API for WordNet Searching (JAWS)},
  url    = {http://lyle.smu.edu/%7Etspell/jaws/},
}

This will compile correctly and the URL is clickable in e.g. generated PDF. You can even just copy that URL into your browser, it will load as expected.

1
  • In my experience ~ works fine in the url field in .bib files. Ultimately it will depend on the style you are using, but most styles that support a url field usually use hyperref/url to typeset the URLs and then ~ is supported out of the box. Some styles use fallback definitions in case url/hyperref commands are not loaded (not available), in that case ~ may not come out as expected, though I would expect %7E even worse in those situations.
    – moewe
    Mar 26, 2020 at 21:34
11

This did it for me (plus: the link works!)

\string~

From: https://latex.org/forum/viewtopic.php?t=3663

1
  • This worked inside author footnote, i.e. \author{\thanks{\url{~}}}. Other answers did not.
    – Dean
    Feb 15 at 7:28
7

uses \%7E to replace ~, do not forget \ before %7E.

url    = {http://lyle.smu.edu/\%7Etspell/jaws/},

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .