4

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.

enter image description here

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}
}
kysosid
  • 41
  • Related: http://tex.stackexchange.com/questions/50777/how-to-emulate-url-hyphenating-without-using-the-url-package – Henri Menke Dec 01 '13 at 01:20
  • You can put raggedright before \bibliography{references} if that is allowed. I think this is better. –  Dec 01 '13 at 01: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 01 '13 at 02:14

1 Answers1

1

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:

Sample output

\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}
Andrew Swann
  • 95,762