URL linebreak in footnote

I use a footnote with a too long url, so that I'd like to see a linebreak. The url package does linebreaks automatically as far as i know, but in the footnote it won't do it. Anyone have a solution to this?

\footnote{\url{http://www.this-is-a-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-long.url/}}


migrated from stackoverflow.comJul 18 '11 at 14:40

This question came from our site for professional and enthusiast programmers.

• In general \url does a good job in breaking the URL; this particular example can't be broken, because the hyphen is not a permissible point. Can you show the real URL? – egreg Jul 18 '11 at 14:49
• Welcome to TeX.sx! Your question was migrated here from another stackexchange site. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. – Martin Scharrer Jul 18 '11 at 16:14

3 Answers

The url package documentation suggests the following:

Ordinarily, breaks are not allowed after "-" characters because this leads to confusion. (Is the "-" part of the address or just a hyphen?) The package option [hyphens] allows breaks after explicit hyphen characters. The \url command will never ever hyphenate words.

If your original example included a URL that has that specific structure, then \usepackage[hyphens]{url} will solve your problem.

\documentclass{article}
\usepackage[hyphens]{url}
\begin{document}
This is text\footnote{\url{http://www.this-is-a-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-long.url/}}.
\end{document}​


Or – not fancy but effective – simply replace the hyphens in the URL by "\%2D". It doesn't look as nice as hyphens, but this way it breaks correctly and still works fine as a link.

E.g.

\url{http://www.whysomanyhyphens.com/content/this-article-has-its-whole-title-as-the-url-that-will-never-fit-in-your-puny-footnote}


becomes

\url{http://www.whysomanyhyphens.com/content/this\%2Darticle\%2Dhas\%2Dits\%2Dwhole\%2Dtitle\%2Das\%2Dthe\%2Durl\%2Dthat\%2Dwill\%2Dnever\%2Dfit\%2Din\%2Dyour\%2Dpuny\%2Dfootnote}


Especially when the url is long and cryptic (=ugly) anyway, I just do that.

• Can you please add an MWE? – Mensch Nov 11 '15 at 18:36
• \%2D does not work for me, just displays as "\%2D" – Sander Heinsalu Feb 13 '18 at 22:45

The url package does linebreaks automatically [in long URL strings], but in the footnote it won't do it.

The main issue -- not touched on by the previously-posted answers -- is that the \url macro is "fragile" (in the LaTeX-specific sense of the word). That's why typesetting URL strings -- especially long URL strings -- in footnotes can be tricky.

For just such cases, the user guide of the url package recommends the "defined-URL" approach, as it is "robust" -- again, the LaTeX-specific sense of the word. Use the \urldef macro to create a defined URL.

\documentclass{article}
\usepackage[hyphens,spaces,obeyspaces]{url}
\urldef{\footurl}\url{http://www.this-is-a-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-long.url/}
\usepackage[colorlinks]{hyperref} % optional

\begin{document}
\footnote{\footurl} % use the "defined URL" in the argument of "\footnote"
\end{document}