Joachim Breitner

Nicer URL formatting in LaTeX

Published 2011-08-08 in sections English, Digital World.

To format URLs in LaTeX, one usually uses the url package (either directly, or indirectly via hyperref). By default, it typesets the URL in a mono-spaced font. Maybe this was a good idea some years ago, when URL were not ubiquitous. Today, I do not think they need neither the emphasis nor the explictit technical association that is caused by changing the font. And last but not least, mono-spaced fonts are very wide.

So url offers the command \urlstyle. I prefer to set it to sf, because URLs tend to look better without serif. In the picture above (also available as PDF), that is the second line. But this still has issues: The double slash at the beginning is spaced out more than it should, given that, at least to most readers, http:// is a single entitiy. In addition, the underscore is far to wide (this only happens if you use the T1 font encoding).

Hence I modified the typesetting of both the slash and the underscore to achieve the appearance in the third line. The code is derived from the one given on this blog post by Nei, with slightly less kerning of the slashes and with extending instead of redefining \UrlSpecials and the underscore handling added. Here is the full code for the picture given above, to use it you need to copy the part between and including \makeatletter and \makeatother into the preamble of your document:

\usepackage[top=3pt, bottom=3pt, left=3pt, right=3pt, paperwidth=6.7cm, paperheight=1.6cm]{geometry}
% Inspired by
% but slightly less kern and shorter underscore
\def\Url@underscore{\nfss@text{\leavevmode \kern.06em\vbox{\hrule\@width.3em}}}


Nice trick! I must try that!
#1 Mark am 2011-08-08
When I try copying the URL off of the PDF, the underscore is missing...
#2 lumi am 2011-08-08
True... and more interestingly, this is also the case in the second line, if you do not use T1 encoding, so it is not really caused by my code. It would still be nice to know how to prevent that, though.
#3 Joachim Breitner (Homepage) am 2011-08-08
One way to fix this is to load the accsupp package in the latest version (0.3) and use the following definition:

\def\Url@underscore{\nfss@text{\BeginAccSupp{ActualText=_,space}\leavevmode \kern.06em\vbox{\hrule\@width.3em}\EndAccSupp{}}}
#4 Joachim Breitner (Homepage) am 2011-08-08
Usually I use the font package "times" in combination with T1 font encoding (I think that font is better in PDFs). With this font, the second alternative looks best.
Anyway, thank you very much for the hint!
#5 Stephan am 2011-08-08
I agree that the times font does not need any adjustment to the slashes. But the underscore is still too wide for my taste.
#6 Joachim Breitner (Homepage) am 2011-08-08
thanks. just put that into my thesis. looks nice.
#7 ssam am 2011-08-08
Thanks a lot! This helped my a lot!
#8 stefan am 2012-07-30
Thanks a lot for this. The url package allows for line breaks after underscores. However, this does not work with the replacement you suggest. I managed to fix this by adding \linebreak[1] in the definition of \Url@underscore as follows:
#9 RL am 2016-03-04

Have something to say? You can post a comment by sending an e-Mail to me at <>, and I will include it here.