39

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/}}
Nikos Alexandris
  • 1,051
  • 1
  • 15
  • 43
  • 9
    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

5 Answers5

41

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}​
Sicco
  • 103
Werner
  • 603,163
8

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.

enter image description here

\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} 
Mico
  • 506,678
  • 1
    I tried to use \urldef like this, but it does not work. :( I am using \usepackage{hyperref} and \PassOptionsToPackage{hyphens}{url}. – Magne Nov 21 '19 at 09:08
  • @Magne - Please elaborate on the "does not work" observation. (a) What exactly have you tried -- do please show the URL that you're trying to get typeset in a footnote, plus the \urldef instruction. (b) What is the precise nature of the does-not-work problem: Do you get an error message, a warning message, the URL doesn't get displayed, the footnote doesn't even show up, or something else? – Mico Nov 21 '19 at 09:44
  • I tried \urldef{\footurl}\url{https://www.washingtonpost.com/world/asia_pacific/chinas-plan-to-organize-its-whole-society-around-big-data-a-rating-for-everyone/2016/10/20/1cd0dd9c-9516-11e6-ae9d-0030ac1899cd_story.html} with \footnote{\footurl} as in your example. No errors or warnings, footnote shows as usual, but without the intended linebreak. – Magne Nov 21 '19 at 10:51
  • @Magne - I have some doubts that \PassOptionsToPackage{hyphens}{url} works flawlessly. For sure, if I use the setup shown in the answer above, i.e., if I type \usepackage[hyphens,spaces,obeyspaces]{url} before loading hyperref, the URL is typeset across three lines, without further ado. – Mico Nov 21 '19 at 11:17
  • If you load url with \usepackage instead of \PassOptionsToPackage, won't that give you a LaTeX Error: Option clash for package url. since hyperref also loads the url package? – Magne Nov 21 '19 at 14:39
  • @Magne - The minimal working example I posted above explicitly loads first url and second hyperref. For sure, it does not generate any "option clash" errors. I'm not surprised that errors crop up if one insists on a pursuing a different setup. – Mico Nov 21 '19 at 14:59
  • @Magne -- When loading both the url (or the recent xurl) package and the hyperref package, it's essential to load url before hyperref. Nothing could will come from ignoring this rule. – Mico Nov 21 '19 at 15:42
  • I was actually always loading url before hyperref, just like in your example. Might be something else that was tripping it up, I don't know ¯_(ツ)_/¯ anyways, thanks for your time. I went with @ffl's solution for the time being. – Magne Nov 21 '19 at 15:54
7

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.

ffl
  • 71
1

My solution simply defines a new command which is capable of separating the URL at any position. I make use of this solution, by formating every character as texttt and appending \hskip 0pt plus 1pt. The following example code defines the command urlx:

\documentclass{article}

\usepackage{hyperref}

% TT every character and hyphenate after it
\def\hyphenateAndTtWholeString #1{\xHyphenate#1$\wholeString\unskip}

\def\xHyphenate#1#2\wholeString {\if#1$%
    \else\transform{#1}%
    \takeTheRest#2\ofTheString\fi}

\def\takeTheRest#1\ofTheString\fi
{\fi \xHyphenate#1\wholeString}

\def\transform#1{\url{#1}\hskip 0pt plus 1pt}

% Define the \urlx command which works like \url, but with line brakes
\def\urlx #1{\href{#1}{\hyphenateAndTtWholeString{#1}}}

\begin{document}

Test\footnote{Test: \urlx{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.de/}}

\end{document}

Produces: Output of urlx command

Explanation: By typing e.g. \urlx{www.tt.net}, the command produces actually: \href{www.tt.net}{\texttt{w}\hskip 0pt plus 1pt\texttt{w}\hskip 0pt plus 1pt\texttt{w}\hskip 0pt plus 1pt\texttt{.}\hskip 0pt plus 1pt\texttt{t}\hskip 0pt plus 1pt\texttt{t}\hskip 0pt plus 1pt\texttt{.}\hskip 0pt plus 1pt\texttt{n}\hskip 0pt plus 1pt\texttt{e}\hskip 0pt plus 1pt\texttt{t}\hskip 0pt plus 1pt}

Tik0
  • 163
1

Try using the following package:

\usepackage{xurl}
chsk
  • 3,667
Guest1
  • 11
  • 2
    Might want to add a little explanation as to why. But I agree, nowadays xurl is probably better – daleif Mar 30 '21 at 09:38