48

I've discovered a rather weird behaviour of PdfLaTeX. In this example

\documentclass{article}
\usepackage[breaklinks]{hyperref}
\begin{document}
\url{http://www.emarketer.com/blog/index.php/quick-stat-smartphone-users-account-38-mobile-phone-users/}
\end{document}

PdfLaTeX won't break the URL and create a too long line. However, if I replace \url with \href like this:

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\href{http://www.emarketer.com/blog/index.php/quick-stat-smartphone-users-account-38-mobile-phone-users/}{http://www.emarketer.com/blog/index.php/quick-stat-smartphone-users-account-38-mobile-phone-users/}
\end{document}

I get the line broken, but the font of the link is not right any more of course (normal instead of texttt). Why doesn't URL break the line at dashes and what could I do to get a nicely typeset line-broken link?

3 Answers 3

76

Load the url package explicitly with the hyphens option:

\usepackage[hyphens]{url}
\usepackage{hyperref}

By default, the url package does not consider hyphens as legal break points.

6
  • 3
    Incidentally, there's a good reason for the default: if a URI gets broken at a hyphen, the user may not know whether the hyphen is a part of the URI or an artifact of an intrusive H&J routine. Obviously that doesn't apply in the present case of a news story where the URI has lots of hyphens, but I would caution against using the [hyphens] option as a matter of course. Oct 5, 2015 at 20:46
  • @PeterFlynn H&J = HTML & Javascript? For printing, purposes this wouldn't matter then?
    – mxmlnkn
    Apr 10, 2018 at 6:43
  • 2
    Hyphenation and Justification, sorry, I should have explained. May 1, 2018 at 21:48
  • my cls already had \RequirePackage{hyperref}, so right before that i added \RequirePackage[hyphens]{url}
    – Michael
    Jun 29, 2018 at 14:14
  • 7
    @Michael - For users who aren't allowed to modify the .cls file, a useful recommendation would be to execute \RequirePackage[hyphens]{url} before \documentclass.
    – Mico
    Dec 30, 2018 at 11:07
32

The above answer by mhp may not provide a solution when PDFLaTeX complains with "Option clash for package url."

The cause of this may be because of the package hyperref, which also loads the package. If hyperref is loaded before the url package it gives this error, because the url package is then loaded twice with different options: one without and one with hyphens option (this always causes a clash).

If you must load url after hyperref, you can first specify the hyphens option with the PassOptionsToPackage command:

\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}
\usepackage{url}

Note that you do not need to provide the hyphens option a second time when loading the url package (or load url explicitly at all).

I noticed that the biblatex package apparently also clashes with url. Therefore, the same goes for biblatex as for hyperref: load url before it or use PassOptionsToPackage.

To moewe and Heilo; I know, I'm sorry and I would have, but as you said, I cannot make comments until a I have a certain 'reputation'. So I will edit my 'answer' to turn it into a real answer. Maybe I shall now collect enough reputation ;-)

Strange, my other post is deleted, and it still advised me to edit that one rather than making a new one! (It also was still visible, in red!)

5
  • 7
    This did not work for me I received a different error though: Undefined control sequence. \loadpackage{hyperref}
    – Whitecat
    Mar 9, 2016 at 21:42
  • 2
    This no longer works. Hyperref complains about the clash of options with url while it is loading (immediately after loading hyperref.cfg) so the document will never get to the \loadpackage{url} anyway. Jan 18, 2018 at 22:55
  • Did you mean to write \usepackage instead of \loadpackage?
    – Mico
    Dec 30, 2018 at 11:05
  • 1
    Aside: It's worth pointing out that the error message "Option clash for package url" does not arise if @mhp's solution is implemented properly. This error message arises only if \usepackage[hyphens]{url} is run after \usepackage{hyperref} -- which is clearly not what mhp recommended.
    – Mico
    Dec 30, 2018 at 11:11
  • A reason for hyperref being already loaded without being obviously notices (and erring out with the option clash message: beamer loads it at \documentclass time! Use the \PassOptionsToPackage command before that and you are fine.
    – stefanct
    Jul 11, 2019 at 14:08
2

The [hyphens] option for the url package doesn't seem to always work. But now, there is the xurl package that does exactly what you want.

It seems you can load it wherever, but it has interplay with biblatex.

See this post for more information.

1
  • IMO, the best option! Line-breaks of URLs do not depend on special characters anymore, just on the length, which gives them a much simpler read as technical identifier instead of a floating text.
    – matheburg
    Jun 7, 2023 at 7:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .