Clickable, breakable, colored & underlined URLs in LaTeX

Requirements

Suppose the goal is to simultaneously achieve all of the following for typesetting URLs with the \url command:1

  1. URLs are clickable.
  2. URLs are (line-)breakable.
  3. URLs are colored.
  4. URLs are underlined.2

Many StackOverflow questions and answers cover a subset of these, but none that I’ve found does all four. Moreover, such answers usually don’t combine to get all of the above.

A rant

The esteemed typographer Robert Bringhurst states in his “The Elements of Typographic Style”:

3.5.1 Change one parameter at a time.

Moreover, underlining is shunned in general, e.g. by the TeX FAQ and the soul-ori package documentation (with references to other typographic texts).

Hence, requiring URLs in texts to be underlined (and colored) is madness, but this is what happens when Word users make the rules…

LuaLaTeX

\usepackage{hyperref} % provides clickable \url command
\hypersetup{
    breaklinks, % allow line breaks in links
    colorlinks, % allow colors for links
    allcolors=black, % disable obnoxious colors for \ref, \cite, etc. (optional)
    urlcolor=blue, % choose color for \url (default)
}
\usepackage{xurl} % allows arbitrary line breaks in \url (optional)

\usepackage{lua-ul} % provides underlining for LuaLaTeX
\makeatletter % allow accessing \@underLine below
\DeclareUrlCommand{\Hurl}{% redefine hyperref's internal \Hurl instead of \url to preserve clickability and color
    \def\UrlLeft##1\UrlRight{\@underLine##1} % underline \url, use internal command instead of \underLine to preserve breakability
}
\makeatother

pdfLaTeX

Currently I am not aware of a way to simultaneously achieve breakability and underlining with pdfLaTeX. Neither the soul nor the ulem package for underlining with pdfLaTeX provides a command like \@underLine which can be used to underline without forcing the argument into an unbreakable box. Both of these packages and the url package (loaded by hyperref) for the \url command do some low-level \catcode trickery with their arguments, but they don’t seem to play well together. The href-ul package outright seems to do nothing.

Comment below if you have solution for pdfLaTeX!


  1. It has to be the standard \url command from the url package, not an alternative custom command. The latter will lead to inconsistencies, e.g. URLs in BibLaTeX bibliography would not use the custom command. 

  2. pdfborderstyle from the hyperref package is not an appropriate and portable way to achieve this.