I have a long URL. I use the package breakurl
, and it works fine outside of an enumerate
environment, but not when it is on an \item
.
\usepackage[margin=3cm]{geometry}
\usepackage{hyperref}
\usepackage{breakurl}
\begin{enumerate}
\item svn co \burl{https://svn.xxx.ch/reps/yyyyyyyyyy/Publications/ABC-paper-2012/trunk ABC-paper-2012}
\end{enumerate}
How can I give the URL a line break?
Edit: If I remove margin=3cm
, it works properly. It seems to be an interaction between geometry
and breakurl
.
Best Answer
Update Jan 2022: Back when I originally wrote this answer, the xurl package -- so named, presumably, because it greatly extends the capabilities of the url package -- hadn't been written yet. However, it exists now, and that's why I would no longer recommend the answer shown below, i.e., running
Instead, I would just run
Quoting from the introductory section of the user guide of the
xurl
package:Note that it is not necessary to specify the package option
hyphens
to allow line breaks to occur at the-
(hyphen) character. If you want to allow line breaks to occur at spaces in a URL string, while not suppressing spaces elsewhere in the URL string, be sure to loadxurl
with the optionsspaces
andobeyspaces
.It should be noted up front that the long-URL issues you are encountering can arise regardless of whether the URL occurs inside an
enumerate
environment or not.By default, if a URL string contains a hyphen character, neither
\burl
of thebreakurl
package nor\url
of theurl
package will insert a linebreak after a hyphen character. (This setting is chosen to avoid ambiguities over whether the hyphen character is a part of the URL string or not.) To override the default setting, load thebreakurl
package with thehyphenbreaks
option or, equivalently, load theurl
package with thehyphens
option set (and use the command\url
instead of\burl
, of course).However, even with the
hyphenbreaks
/hyphens
options set, overfull lines can still occur. An additional measure you may have to take is to issue the command\sloppy
. This directive lets TeX expand the amount of interword whitespace (almost) arbitrarily to support its efforts to avoid overfull lines.The following, modified form of your MWE shows how this works. The packages
breakurl
andurl
are loaded with the optionshyphenbreaks
andhyphens
, respectively. Even so, the first two items in the enumeration, which use the\burl
and\url
commands, produce overfull lines. In contrast, the third and fourth items, for which the directive\sloppy
is in effect, do not produce overfull lines. (By the way, in this MWE the scope of the\sloppy
directive ends at the\end{enumerate}
statement.)