# URLs in bibliography: LaTeX not breaking line as expected

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}

\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},
)


I'm getting this, for example:

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}
\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},
)

@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 ?

-
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 '13 at 5:19
Welcome to TeX.SX! You may have a look on our starter guide. –  texenthusiast May 23 '13 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 –  personne3000 May 23 '13 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 '13 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. –  personne3000 May 23 '13 at 7:34

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:

\documentclass[paper]{ieice}

\usepackage[hyphens]{url}
\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 \riggedright before your \bibliography command. Better ragged right formatting is provided by the ragged2e package. Unfortunately your class defines Center environemnt 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.

-
The "\Urlmuskip=0mu plus 1mu" command finally did the trick; thank you very much for this answer, I finally solved this problem. –  personne3000 May 24 '13 at 3:13
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. –  personne3000 May 24 '13 at 3:28