[Tex/LaTex] URLs in bibliography: LaTeX not breaking line as expected

bibliographiesbibtexline-breakingurls

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 ?

Best Answer

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.

Related Question