85

I am trying to include long URLs in a bibliography, but those often overflow into the margin of the document.

I have seen a number of questions about breaking URLs in bibliography, but after trying every solution, I have not found anything that completely solves my problem. There are apparently a number of ways to allow LaTeX to break line at different places in URLs. But in my case, even though LaTeX breaks line at some places, I still get overflow for some URLs.

Here is the code I am using:

\usepackage[hyphens]{url}
\usepackage{hyperref}
\hypersetup{breaklinks=true}

\urlstyle{same}
\usepackage{cite}

and with these bibliography entries:

@misc(webrtc,
    organization={W3C WebRTC Working Group},
    title={WebRTC 1.0: Real-time Communication Between Browsers},
    howpublished={\url{http://www.w3.org/TR/2012/WD-webrtc-20120821/}}
)

@misc(android_javascriptinterface,
    organization={Android Open Source project},
    title={Android WebView addJavascriptInterface reference},
    howpublished={\url{http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)}}
)

I'm getting this, for example:

enter image description here

The entry about Android overflows in the border: my column should end where you can read "browsers." on the first line or "http:" on the third line.

Edit: when trying to create a minimal example for my problem, I noticed that this only occurs when using the IEICE template file. You can find it here: http://www.ieice.org/ftp/tex/ieice/LaTeX2e/, but if you do not want to have to extract the archive and locate the correct file, I have put it there also: http://pastebin.com/BKi2cyMQ

Here is my minimal example:

.tex file:

\documentclass[paper]{ieice}

\usepackage[hyphens]{url}
\usepackage[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\urlstyle{same}
\usepackage{cite}

\title{Test document}
\begin{document}

this\cite{upnp_spec} is a test\cite{android_javascriptinterface}

\bibliographystyle{plain}
\bibliography{refs_min}

\end{document}

and refs_min.bib file:

@misc(android_javascriptinterface,
    organization={Android Open Source project},
    title={Android WebView addJavascriptInterface reference},
    howpublished={\url{http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)}}
)

@misc(upnp_spec,
    organization = {UPnP Forum},
    title = {UPnP Device Architecture 1.1},
    howpublished = {\url{http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf}}
)

Is there any way I can fix this ?

6
  • These URLs break for me; it's not pretty, but they break. There must be something you're not telling us. Please provide a complete minimal example.
    – jon
    May 23, 2013 at 5:19
  • Welcome to TeX.SX! You may have a look on our starter guide. May 23, 2013 at 5:21
  • @jon Indeed they break as in the screenshot I posted, but my problem is as you said that they are not pretty: they sometimes break "too late", and overflow into the margin. Do you mean that it does not overflow for you ? There might be a difference with the bibliography style May 23, 2013 at 5:46
  • Yes, they break without sticking into the margin. As for why we are observing different things, the best solution is to post a minimal example that others can reproduce for themselves.
    – jon
    May 23, 2013 at 5:54
  • @jon Apparently this problem only occurs with the class file I'm using. I added a minimal example; you can see the updated question. May 23, 2013 at 7:34

5 Answers 5

59

The url package provides an interesting extra piece of flexibility, via the construct \Urlmuskip. This specifies the spacing around the breakable characters. By default it is 0mu however you can set it some glue specification:

\Urlmuskip=0mu plus 1mu

(Unfortunately the example in the url manual is wrong. It says \Urlmuskip=0pt plus 1mu. Being a mathematical skip expression the only units allowed are mu.)

Anyway in your example this gives:

Sample output

\documentclass[paper]{ieice}

\usepackage[hyphens]{url}
\usepackage[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\urlstyle{same}
\usepackage{cite}

\title{Test document}
\begin{document}

this\cite{upnp_spec} is a test\cite{android_javascriptinterface} \cite{Author:Title}

\Urlmuskip=0mu plus 1mu\relax
\bibliographystyle{plain}
\bibliography{refs_min}

\end{document}

with an extra entry in your bib file.

Other things one might try to help in general include making the bibliography ragged right. The easiest way to this is just to write \raggedright before your \bibliography command. Better ragged right formatting is provided by the ragged2e package. Unfortunately your class defines the Center environment that clashes with the ragged2e, so in this case you would have to load it as follows:

\let\clsCenter\Center\let\clsendCenter\endCenter
\let\Center\undefined\let\endCenter\undefined
\usepackage{ragged2e}
\let\Center\clsCenter
\let\endCenter\clsendCenter

You could then issue \RaggedRight just before your \bibliography command. If there is text after the bibliography, you can turn on normal formatting again with the \justifying command.

5
  • Actually, in some cases, I had to use "\Urlmuskip=0mu plus 2mu" because apparently "1mu" was not enough. But the URL ends up very "stretched" on the line, so the \RaggedRight command is probably also a good option. May 24, 2013 at 3:28
  • where exactly do i put the "\Urlmuskip=0mu plus 1mu\relax" ? also in the bib file or just in the doc-body?
    – kuuhkuuh
    Aug 4, 2016 at 22:21
  • You want it to take effect just before the bibliography, so you can place it as I have in the main file before the \bibliography command. It can also be place in the .bib file in a @Preamble, but that it is not really the correct thing do, the presentation should depend on the formatting document. Aug 5, 2016 at 7:57
  • 1
    Works like a charm. I am new to LaTeX, so this is absolutely black magic to me. Nov 30, 2020 at 2:00
  • I don't like stretched-out URL's. Andreas's solution below worked perfectly for me.
    – user93292
    Aug 19, 2022 at 14:34
56

https://norwied.wordpress.com/2012/07/10/how-to-break-long-urls-in-bibtex/

\usepackage{url}
\def\UrlBreaks{\do\/\do-}
\usepackage{breakurl}
\usepackage[breaklinks]{hyperref}
3
  • 17
    actually, if you have - in your url, then just the 2nd line is enough to break it. Or you can pass the option hyphens to the url package: \usepackage[hyphens]{url} from this answer Can you please include the longer description from your blog, as well? It would make the answer well rounded. Thank you! Feb 6, 2019 at 9:44
  • works for me w/o the last line (\usepackage[breaklinks]{hyperref}) and with the last line causes a package clash error. thanks! Mar 7, 2020 at 19:22
  • Added \do_ to the \def to get a long URL with lots of underscores to break before going into the right margin. When I removed the breaklinks option and \usepackage{breakurl} nothing changed so I left them out.
    – user93292
    Aug 19, 2022 at 14:27
26

When using biblatex, this can also be achieved with the block=ragged option, see §3.1.2.1 in http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/biblatex/doc/biblatex.pdf i.e. \usepackage[block=ragged]{biblatex} will do the trick

3
  • 4
    This answers the question. Works fine!
    – tatojo
    Jun 20, 2017 at 13:14
  • 7
    It also has an undesired effect on the rest of the bibliographic entries.
    – hola
    May 31, 2020 at 10:26
  • 3
    Your answer is just like a virus, it ruins everything even when I remove it! Apr 17, 2021 at 13:22
8
\usepackage{url}
\def\UrlBreaks{\do\/\do-}
1
6

I tried all of these solutions, but none of them worked. The thing that finally worked for me was adding the \sloppy command before the \printbibliography.

\begingroup
\sloppy
\printbibliography
\endgroup

(Credit for this solution goes to Suresh and lurking_quietly)

You must log in to answer this question.

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