59

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.

ahorn
  • 673
Barth
  • 815
  • 1
    The problem with all the answers, I think, provided to this question is that (when a certain URL is broken across multiple lines) every PDF viewer which I tried will only highlight that line of the URL which one is clicking on (and not the complete URL), which is confusing. Anybody knows how to fix it? – O0123 Apr 16 '16 at 22:12

5 Answers5

69

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

Mico
  • 506,678
  • 9
    @Barth You can use \begin{sloppypar}...\end{sloppypar} to turn \sloppy on for just a part of the document. – Ethan Bolker May 08 '12 at 16:58
  • When I click the URLs, they are not forwarded to correct addresses. Any part in the second lines are missing. For Item 1, 2012 is missing. Even worse, For Item 1, 'svn co' is added to the address. Any way to fix it? – Chang Jul 03 '15 at 19:21
  • 1
    @FEQ - The web address svn.xxx.ch that the OP provided (and which I re-used in my answer) isn't active -- and, as far as I can tell, never was. Furthermore, the code in my answer doesn't load the hyperref package; hence, any hyperlinks that may exist are provided by the pdf file browser you use rather than by LaTeX and friends. Please clarify what you're trying to achieve. – Mico Jul 03 '15 at 23:30
  • 1
    @Mico Thanks! You already gave me an answer! Yes the hyperlink was from my PDF viewer and when I loaded hyperref, it went all okay. – Chang Jul 04 '15 at 03:19
  • how can I use this if I use pdflatex by default? the urlbreak package states that it will do nothing then. (I could just use latex and dvi->pdf, but I would like a solution for pdflatex) – Garonenur Oct 30 '15 at 09:07
  • 1
    @Garonenur - If you use pdflatex and compile directly to pdf, you don't need the breakurl package at all. A combination of \usepackage[hyphens]{url} in the preamble and \sloppy directives (as needed) should do the job. – Mico Oct 30 '15 at 09:14
  • 1
    @Mico - yes and no, it breaks the links, but doesn't do a very good job, I still get overfull boxes for a link that spans more than one line length. (it can't be shortened in that case.) I just would like to be able to define more breaking chars. – Garonenur Oct 31 '15 at 14:32
  • 1
    @Mico I was about to ask my own question and found the answer right below :) \makeatletter \g@addto@macro{\UrlBreaks}{\do\=\do\$} \makeatother did it for me. – Garonenur Oct 31 '15 at 15:11
  • @Mico How to get color in url? I tried \usepackage[hyphenbreaks]{breakurl} \usepackage[hyphens]{url} \hypersetup{colorlinks=true, linkcolor=black,urlcolor=blue} I can not get result. – minhthien_2016 Jan 13 '22 at 00:41
  • 1
    @minhthien_2016 - I believe the breakurl package is obsolete by now. When I wrote this answer, back in May 2015, thexurl package (so named, presumably, because it's an extension of the url package) hadn't been created yet. I think \usepackage{xurl} \usepackage[colorlinks=true,allcolors=blue]{hyperref} should work. If it does not, then please report any error and warning messages you get. (By the way, a statement such as "I can not get the result" is not actionable.) – Mico Jan 13 '22 at 03:14
  • 1
    @Mico Thank you very much for your update. – minhthien_2016 Jan 13 '22 at 07:45
45

None of the answers helped me out. My long url was placed in a footnote. After searching around I found a working solution here.

In my preamble:

\usepackage{hyperref}
\def\UrlBreaks{\do\/\do-}

The last line instructs the url-package which is loaded with the hyperref-package to additionally break at the characters / and -. The option breaklinks=true in hyperref can be invoked too but for me it had no effect.

Karl
  • 623
  • 3
  • 1
    The original post requested line breaks in document text, not \footnotes. – Werner Jul 10 '15 at 18:52
  • 1
    I´m sorry, guys for the not perfectly matching answer. As a justification and a mapping to the real world where search phrases most often not match an exact question title (often because a user doesn´t know the terms yet), a user ends up at not perfectly matching threads. So there is always a chance a non-perfectly-matching answer offers the correct solution to a problem. This is my experience so far :) – Karl Jul 12 '15 at 21:38
  • 6
    it should read \def\UrlBreaks{\do\/\do\-} I needed it to break at = and it would only work with the \ in front of the breaking char. even better would be \makeatletter \g@addto@macro{\UrlBreaks}{\do\/\do\-} \makeatother so you would not overwrite the original \UrlBreaks symbols – Garonenur Oct 31 '15 at 15:05
  • I'm not sure if this is the best answer for this specific answer, or even if it is a very elegant away of doing things, but it fixed my problem with a couple of very long URLs that I had on my .bib files. Thank you very much for the tip. – petersaints Mar 21 '17 at 14:16
14

The problem with breakurl package is that it seems incompatible with PDFLaTeX, and XeLaTeX. My colleague Dag Langmyhr pointed out that the url package is compatible with hyperref if it is loaded before the latter, and consequently

\usepackage[T1,hyphens]{url}
\usepackage[colorlinks,urlcolor=blue]{hyperref}

does the job more elegantly (breaklinks=true did also not work for me). This approach allows the normal \url command to be used when inserting the URLs.

Geir
  • 331
3

I think a more current answer is the usage of the package xurl as mentioned in this post.

To my understanding the usage of this package frees one from using the command \sloopy (since the line break could be almost everywhere).

LeO
  • 1,451
2

If the other solutions won't work, this was the only solution that worked for me (from here):

% If you want to break on URL numbers
\setcounter{biburlnumpenalty}{9000}
% If you want to break on URL lower case letters
\setcounter{biburllcpenalty}{9000}
% If you want to break on URL UPPER CASE letters
\setcounter{biburlucpenalty}{9000}