[Tex/LaTex] How to break a long URL

hyperrefline-breakingurls

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

\usepackage[hyphenbreaks]{breakurl}
\usepackage[hyphens]{url}

Instead, I would just run

\usepackage{xurl}

Quoting from the introductory section of the user guide of the xurl package:

Package xurl loads package url by default and defines possible [line breaks in URLs] for all alphanumerical characters and = / . : * - ~ ' "

All arguments which are valid for [the] url [package] can be used. It will be passed to package url. xurl itself has no special optional argument. For more information read the documentation of package url.

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 load xurl with the options spaces and obeyspaces.


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 the breakurl package nor \url of the url 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 the breakurl package with the hyphenbreaks option or, equivalently, load the url package with the hyphens 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 and url are loaded with the options hyphenbreaks and hyphens, 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.)

\documentclass{article}
\usepackage{lipsum} % load paragraphs of filler text
\usepackage[margin=3cm]{geometry}
\usepackage[hyphenbreaks]{breakurl}
\usepackage[hyphens]{url}
\begin{document}

\lipsum[2] % generate some filler text (to show width of text block)

\begin{enumerate}
\item
svn co \burl{https://svn.xxx.ch/reps/yyyyyyyyyy/Publications/ABC-paper-2012/trunk ABC-paper-2012}
\item 
svn co \url{https://svn.xxx.ch/reps/yyyyyyyyyy/Publications/ABC-paper-2012/trunk ABC-paper-2012}
\item
\sloppy
svn co \burl{https://svn.xxx.ch/reps/yyyyyyyyyy/Publications/ABC-paper-2012/trunk ABC-paper-2012}
\item 
svn co \url{https://svn.xxx.ch/reps/yyyyyyyyyy/Publications/ABC-paper-2012/trunk ABC-paper-2012}
\end{enumerate}
\end{document}

enter image description here