Home General LaTeX tips and tricks → URL formatting [Previous Page] [Question index] [Next Page]

14. How do you get nicely formatted URLs in the bibliography?


Answer

Use the url package by Donald Arseneau.


Example usage

In the preamble:


\usepackage{url}

%% Define a new 'leo' style for the package that will use a smaller font.
\makeatletter
\def\url@leostyle{%
  \@ifundefined{selectfont}{\def\UrlFont{\sf}}{\def\UrlFont{\small\ttfamily}}}
\makeatother
%% Now actually use the newly defined style.
\urlstyle{leo}

In a BibTeX entry:


@misc{
    c.elmohamed,
    author = "Saleh Elmohamed",
    title = "Examples in {H}igh {P}erformance {F}ortran",
    howpublished = "Website",
    year = {1996},
    note = {\url{http://www.npac.syr.edu/projects/
                    cpsedu/summer98summary/ examples/hpf/hpf.html}}
}


Result, before

'Before' screenshot

Result, after

'After' screenshot

Things to watch out for

I tweak the default url style to use a smaller version of the typewriter font because I otherwise find that the Adobe Courier font I use (see also the Using Courier tip) displays a bit too large for my tastes, but also because the larger font still causes more spacing ugliness (in terms of underfull hboxes), as illustrated by the following screenshot:

'After'
screenshot

If strange ‘%’ signs start appearing in the list of references in the bibliography section, this is because BibTeX inserts those when it encounters a line it feels is too long:

'After'
screenshot

This is purely a BibTeX thing: inside a URL being handled by the url package, the line won't actually be too long — after all the whole idea is for url to be responsible for splitting it. The solution, as shown in the listing given earlier, is to add some stray whitespace to the long url. BibTeX will then no longer think the line is too long, and \url{} ignores all whitespace anyway, so it will not appear in the output.

The biggest problem with the url package is that you do not use it in your LaTeX source file, but in your BibTeX source files, thus making your bibliographies non-portable: once you start using \url{} in your BibTeX entries, you will be forced to include the url package in all your LaTeX files that use that bibliography file.


[Previous page] [Question index] [Next page]

Leo Breebaart (leo@lspace.org)
Last updated: 2 June 2012