TransWikia.com

Hyperref + url: long URL with dashes won't break

TeX - LaTeX Asked by texnic on July 23, 2021

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?

2 Answers

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.

Correct answer by mhp on July 23, 2021

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!)

Answered by Ralph on July 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP