linebreak in url with Bibtex and hyperref package
Asked Answered
T

12

23

Why is this item not shown properly in my bibliography?

@misc{ann,
    abstract = {ANN is an implbmentation of nearest neighbor search.},
    author = {David M. Mount and Sunil Arya},
    howpublished = {\url{http://www.cs.umd.edu/~mount/ANN/}},
    keywords = {knn},
    posted-at = {2010-04-08 00:05:04},
    priority = {2},
    title = {ANN.},
    url = "http://www.cs.umd.edu/~mount/ANN/",
    year = {2008}
}

@misc{Nilsson96introductionto,  
    author = {Nilsson, Nils J.},  
    citeulike-article-id = {6995464},  
    howpublished = {\url{http://robotics.stanford.edu/people/nilsson/mlbook.html}},  
    keywords = {*file-import-10-04-11},  
    posted-at = {2010-04-11 06:52:28},  
    priority = {2},  
    title = {Introduction to Machine Learning: An Early Draft of a Proposed Textbook.},  
    year = {1996}  
}  

alt text


EDIT:

I am using

\usepackage{hyperref}

not

\usepackage{url}

. It produces error when using url package together with it. So can the two not work together?

I would like to use hyper links inside pdf file, so I want to use hyperref package instead of url package. I googled a bit, and try

\usepackage[hyperindex,breaklinks]{hyperref}

but there is still no line break just as before. How can I do it?


EDIT:

When using url and hyperref together, if it is just

\usepackage{hyperref}  
\usepackage{url}

the compilation by latex is fine, but the link is still hyperlink and has still no linebreak. If I do not use hyperref package, the link has linebreak, but I lose hyper links. Since \url can be used in both hyperref and url packages, how can I specify which package's \url is being used?

If it is

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

the compilation by latex command will report clash with url:

! LaTex Error: Option clash for package url.

So I wonder how I should do?

Taurus answered 12/4, 2010 at 21:51 Comment(0)
O
11

If you mean the too-wide spacing, that's because the URLs seems to not allow line-breaks. Why this happens is another question, and the answer depends on your preamble (the packages you use etc.). In principle, if properly used, the url package should allow line breaks.

EDIT This problem (and its solution) is described here (sending you to a Google cache since the site is offline at the moment). Bottom line: either use the breakurl package, or PDFLaTeX, or both.

Oxpecker answered 12/4, 2010 at 22:41 Comment(1)
Thanks, AVB! I would like to use hyper links inside pdf file, so I still want to use hyperref package instead of url package. I googled a bit, and try \usepackage[hyperindex,breaklinks]{hyperref}, but there is still no line break just as before. How can I do it?Taurus
T
22

You should use them in this order:

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

You get this error when you use them the other way around.

! LaTex Error: Option clash for package url.

since the hyperref package loads the url package somewhere internally without that hypens option, and then you want to load it with the option, so it clashes.

Turning the order around does what you want and does not give this error (since the package is already loaded hyperref won't load it again with different options)

edit: this was with pdftex, I did not test with other tools.

edit2:

or as mentioned by PatrickT in a comment: \PassOptionsToPackage{hyphens}{url} if you're using a class that already loads the package, e.g. beamer.

Tsarism answered 29/7, 2011 at 13:47 Comment(2)
This worked for me! (url included before hyperref, and didn't need to use breakurl).Athlete
or \PassOptionsToPackage{hyphens}{url} if you're using a class that already loads the package, e.g. beamer.Guarantee
E
14

I just ran across almost the same problem and found it solved by putting

\PassOptionsToPackage{hyphens}{url}

before the

\usepackage[...]{hyperref}
Evolutionist answered 13/4, 2012 at 16:32 Comment(1)
this would be needed with the beamer class, which already loads the hyperref and url packages.Guarantee
O
11

If you mean the too-wide spacing, that's because the URLs seems to not allow line-breaks. Why this happens is another question, and the answer depends on your preamble (the packages you use etc.). In principle, if properly used, the url package should allow line breaks.

EDIT This problem (and its solution) is described here (sending you to a Google cache since the site is offline at the moment). Bottom line: either use the breakurl package, or PDFLaTeX, or both.

Oxpecker answered 12/4, 2010 at 22:41 Comment(1)
Thanks, AVB! I would like to use hyper links inside pdf file, so I still want to use hyperref package instead of url package. I googled a bit, and try \usepackage[hyperindex,breaklinks]{hyperref}, but there is still no line break just as before. How can I do it?Taurus
L
6

Weird, I load the url package (with no option) and hyphenation is done at the slashes / if needed. Anyway, simple workaround:

howpublished = {\url{http://www.cs.umd.edu/}\url{~mount/ANN/}},

Manual job, but it splits up the URL yes or yes.

Lanalanae answered 1/2, 2013 at 12:6 Comment(2)
This worked when I put a space between the two urls as well, like so howpublished = {url\{...} \url{...}}.Grisons
The manual fix is the only one that worked for me. You can also add a manual linebreak, e.g. \newline\url{}, if you'd like the url to start on a new line.Guarantee
B
5

for me only this worked:

\usepackage[hyphens]{url}
\usepackage{hyperref}
...
\usepackage{biblatex}
\setcounter{biburlnumpenalty}{100}
\setcounter{biburlucpenalty}{100}
\setcounter{biburllcpenalty}{100}

Taken from: https://tex.stackexchange.com/questions/22854/url-line-breaks-with-biblatex

Balneal answered 28/12, 2012 at 13:35 Comment(0)
T
5

If url package doesn't help, try:

\usepackage{xurl}
\usepackage{hyperref}

Package xurl is an expanded version of url, which allows line breaking at every point in the url. Call xurl before hyperref.

Source: Does the hyperref breaklinks option have any effect?

Thickleaf answered 25/2, 2022 at 4:43 Comment(0)
P
3

Normally URLs are hyphenated, but in your bibliography they seem not to be. When the URLs don't fit on one line, they are moved to the next line, and the text before them is stretched to fill out the preceding lines. Since there is only one place to break the line, it's not very likely that the preceding text will fit nicely into a whole number of lines, and so you get all the extra space.

EDIT: When you changed your bib entry, you happened to change it in such a way that the text did fit nicely. This is just a coincidence, you didn't fix your problem.

I suspect that putting

\usepackage{url}

in your preamble will solve it.

Painter answered 12/4, 2010 at 22:40 Comment(3)
Thanks, ptomato! I would like to use hyper links inside pdf file, so I still want to use hyperref package instead of url package. I googled a bit, and try \usepackage[hyperindex,breaklinks]{hyperref}, but there is still no line break just as before. How can I do it?Taurus
Use them both. There is nothing to stop you using url and hyperref.Desirous
Using them both either still does not work or reports clash with url package.Taurus
J
2

I add this package:

\usepackage[hyphens]{url}

and in the bibtex I used:

howpublished = {**\url{**http://www.......**}**}

and it works out.

Jotting answered 18/2, 2015 at 10:45 Comment(0)
W
0

This simple solution worked for me:

\usepackage{hyperref} \usepackage{breakurl}

The URLs are now perfect.

Wycoff answered 8/12, 2014 at 3:32 Comment(0)
E
0

Even for website references, my Zotero didn't export a url = ... line for .bib files, but just howpublished = {https://...}. This caused some URLs in the bibliography (I had to use multibib, not bibtex) to cause overful hboxes, and the URLs shoot out of the document without a line break. Apparently, behind the hood, there was no \url, but (I think?) a \href from the hyperref package, which has different behavior.

Out of the MANY options I tried, I ended up manually replacing the howpublished = {https://...} with a howpublished = {\url{https://...}} for the broken links. The URLs might look ugly then, this can be fixed with \urlstyle{rm} after \usepackage[hyphens]{url}.

Edik answered 23/6, 2023 at 17:8 Comment(0)
C
-1

At the preamble, just put \usepackage{breaker} somewhere after \usepackage{hyper ref}. The \burl command is defined and, by default, the package also turns the \url command into a synonym of \burl.

Corregidor answered 15/6, 2012 at 3:40 Comment(0)
P
-1

This fixed everything in one move for me: \usepackage{xurl}

Perdita answered 25/8, 2023 at 18:0 Comment(2)
How is this different from @wolfram77's answer?Tacklind
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Hudson

© 2022 - 2024 — McMap. All rights reserved.