# Left-aligning URL with line breaks in bibliography

URLs break fine in my bibliography. My problem is that they are justified which creates a gap between "URL:" and the URL itself (see below). Is there a way to left align only the URL? I am using natbib.

Here is a minimum working example:

\documentclass[11pt, oneside]{report}
\usepackage[hyphens]{url}
\usepackage[round]{natbib}
\begin{document}

\cite{Shaffer2004}

\bibliographystyle{dcu}
\bibliography{references}

\end{document}


This is the references.bib file:

@article{Shaffer2004,
author = {Shaffer, David Williamson and Squire, Kurt R and Gee, James P},
journal = {Phi Delta Kappan},
pages = {104--111},
number = {2},
title = {{Video games and the future of learning}},
url = {http://website.education.wisc.edu/kdsquire/tenure-files/23-pdk-VideoGamesAndFutureOfLearning.pdf},
volume = {87},
year = {2004}
}

• – Henri Menke Dec 1 '13 at 1:20
• You can put raggedright before \bibliography{references} if that is allowed. I think this is better. – user11232 Dec 1 '13 at 1:23
• Thanks. Putting raggedright before \bibliography, however, left aligns the whole entry. But I want to left align only the URL and keep the other parts justified. – kysosid Dec 1 '13 at 2:14

The url's in the bibliography are typeset with \harvardurl which is defined as

\newcommand\harvardurl[1]{\textbf{URL:} \textit{#1}}


As far as I can tell these are always preceeded by a \newline too. So one redefinition would be to box this up as a raggedright paragraph. I prefer to use the \RaggedRight command from the ragged2e package for this:

\documentclass[11pt, oneside]{report}

\usepackage[hyphens]{url}
\usepackage[round]{natbib}
\usepackage{ragged2e}

\renewcommand\harvardurl[1]{\parbox{\dimexpr\textwidth-\leftmargin}{\RaggedRight\textbf{URL:} \textit{#1}\par}}

\begin{document}

\cite{Shaffer2004}

\bibliographystyle{dcu}
\bibliography{references}

\end{document}


I would seriously consider replacing the \textit in the definition with \url and perhaps changing the url font in the bibliography to italics, for example as follows:

\documentclass[11pt, oneside]{report}

\usepackage[hyphens]{url}
\usepackage[round]{natbib}
\usepackage{ragged2e}
\usepackage{etoolbox}

\AtBeginEnvironment{thebibliography}{\def\UrlFont{\normalfont\itshape}}

\renewcommand\harvardurl[1]{\parbox{\dimexrp\textwidth-\leftmargin}{\RaggedRight\textbf{URL:}
\url{#1}\par}}

\begin{document}

\cite{Shaffer2004} and a document url \url{http://tex.stackexchange.com}

\bibliographystyle{dcu}
\bibliography{references}

\end{document}