88

I'm using the url package. Now, I have some some url with a percent sign in it and want to place it in a footnote. When I let the % where it is, I get a comment, and the whole thing fails to compile because of some unmatched brackets. When it's at the end of a line, it doesn't show up at all in the result. When I write \% instead, this gets literally into the output—but I want the url to be displayed correctly.

What could I do?

yo'
  • 51,322
  • 2
    What are you using to create URLs? \url{foo%.com} works fine for me with the url package – Seamus Feb 27 '11 at 16:56
  • pdfLaTeX, on miktex –  Feb 27 '11 at 16:59
  • 1
    @phg I meant, are you using the url package (\usepackage{url} in your preamble) or some other package that defines a \url{} command? – Seamus Feb 27 '11 at 17:01
  • Yeah, I was using \usepackage{url}, but I just saw I screwed it up somewhere else, because I was using it inside a footnote and then the error was kept somehow. Im sorry, problem solved... but thanks for really fast help. –  Feb 27 '11 at 17:06
  • 1
    @Seamus: Could you post this as an answer. – Caramdir Feb 27 '11 at 17:35
  • Or should this be simply closed? – Caramdir Feb 27 '11 at 17:36
  • @Caramdir post what as an answer? I didn't answer anything! I think we could close this... – Seamus Feb 27 '11 at 17:48
  • @Seamus: That it should “just work”. But Martin has a better answer already. So just ignore me :) – Caramdir Feb 27 '11 at 17:50
  • You can also use some of online websites that provide a new short form of link address to your original link address, they usually don't include % sign etc, for example bitly.com or other similar ones, then use the new link address. But the new form may be not informative since it doesn't show the mother domain, for example the link maybe a page in the website of a university, so the original link gives some extra information other than just a link to click on. – AmirHosein Sadeghimanesh Oct 21 '19 at 13:28

2 Answers2

41

For URLs in footnotes or inside other macro arguments use \urldef to define it first as a macro:

\documentclass{article}
\usepackage{url}
\urldef\myurl\url{foo%.com}
\begin{document}
text\footnote{WWW: \myurl}
\end{document}
Martin Scharrer
  • 262,582
  • 2
    This doesn't work for long URLs - https://svn.ggy.bris.ac.uk/websvn/listing.php?repname=genie&path=%2Fbranches%2Fgreg-s_branch%2F&rev=6324 will print correctly in the document, but the link doesn't work as it's only half complete. –  Oct 27 '11 at 12:32
  • 3
    @user8875 This method does work for long url's too, just make sure you don't break it in the definition. To get an active link, load the hyperref package. – Andrew Swann Oct 16 '13 at 13:33
23

The easiest way is to simply escape it.

\documentclass{article}
\usepackage{hyperref}
\begin{document}
    text\footnote{%
        \href{http://example.com/name\%27s-page}{http://example.com/name's-page}%
    }
\end{document}
Fleshgrinder
  • 859
  • 8
  • 14
  • 4
    Yes, I found that adding a "" in front of the "%" in the URL produces a working URL. – Kurt Peek Mar 25 '16 at 17:52
  • 12
    But then the backslash appears in the URL, you don't want that. – yannis Aug 16 '16 at 11:35
  • 11
    It is actually not necessary to escape percent signs in \href{}{} using the hyperref package. The percent sign might confuse text highlighting in your text editor though (for example Overleaf). – Paul Rougieux Aug 29 '18 at 14:23
  • 3
    You also have to escape the # and & signs with a backslash in urls. – erik Oct 16 '19 at 20:22
  • @erik Considering @Paul Rougieux Do you have to escape # and &, or don't you? I mean you don't have to escape %... – U. Windl Nov 29 '19 at 01:33
  • Best answer by far. – VirtuallyRealistic Jan 02 '22 at 08:00
  • I did need to escape it and it did not result in it appearing in the URL. This is actually the only thing that worked for me. – Akaisteph7 Jan 17 '23 at 21:36
  • Other than the precent sign needing escaping % -> \% , the # and & signs wont show error in tex file but will give error after compiling (wont impact the generated file though).. to even remove the compilation error, we need to escape # -> \# and & -> \& – Abhishek Shah Feb 16 '24 at 20:33