Swirly Mein Kopf

Monday, August 8. 2011

Nicer URL formatting in LaTeX

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 http://anti.teamidiot.de/nei/2009/09/latex_url_slash_spacingkerning/
% but slightly less kern and shorter underscore
\def\Url@underscore{\nfss@text{\leavevmode \kern.06em\vbox{\hrule\@width.3em}}}


No Trackbacks


Display comments as (Linear | Threaded)

*Nice trick! I must try that!
#1 Mark on 2011-08-08 12:04 (Reply)
*When I try copying the URL off of the PDF, the underscore is missing...
#2 lumi on 2011-08-08 13:02 (Reply)
*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.
#2.1 Joachim Breitner (Homepage) on 2011-08-08 14:28 (Reply)
*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{}}}
#2.1.1 Joachim Breitner (Homepage) on 2011-08-08 14:57 (Reply)
*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!
#3 Stephan on 2011-08-08 13:16 (Reply)
*I agree that the times font does not need any adjustment to the slashes. But the underscore is still too wide for my taste.
#3.1 Joachim Breitner (Homepage) on 2011-08-08 14:27 (Reply)
*thanks. just put that into my thesis. looks nice.
#4 ssam on 2011-08-08 14:20 (Reply)
*Thanks a lot! This helped my a lot!
#5 stefan on 2012-07-30 12:24 (Reply)

Add Comment

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.

Gravatar, Favatar, Identica author images supported.
What is the first name of the owner of this blog? / Wie heißt der Betreiber dieses Blogs mit Vornamen?
Nach oben