2

I would like to underline links in my document, using the hypersetup command. However, this underlines links with a descender (e.g. a p, q, g, j, y) on the descender line, but links without a descender on the baseline.

In essence, I have the problem illustrated here: https://alexwlchan.net/2017/10/latex-underlines/

Is there any way I can adapt that author's solution to underlines added with a hypersetup border, or just define a set distance from the baseline at which the border will be set?

MWE:

\documentclass[a4paper, 12pt]{article}
\usepackage{hyperref}
\usepackage{xcolor}

\hypersetup{%
    pdfborderstyle={/S/U/W 0.5}%
}

\begin{document}
\url{http://www.example.com} \\

\url{www.hhhhh.com}
\end{document}
3

You can inject a \strut like this:

\documentclass[a4paper, 12pt]{article}
\usepackage{hyperref}
\usepackage{xcolor}

\hypersetup{%
    pdfborderstyle={/S/U/W 0.5}%
}
\makeatletter
\usepackage{xpatch}
\AtBeginDocument{
\apptocmd\Hy@colorlink{\strut}{}{\fail}
}

\begin{document}
\url{http://www.example.com} \\

\url{www.hhhhh.com}
\end{document}

enter image description here

  • Probably some kind of general 'evenboxes' setting would be good ... – Joseph Wright May 14 at 14:37
  • @JosephWright the question is if one wants the \strut really always, e.g. what if the content is a picture? But I will try to think about it. – Ulrike Fischer May 14 at 14:41
  • Should there also be a \makeatother? – LokiRagnarok May 14 at 14:56
  • your document won't break if not but yes it would be more orderly to add it too. – Ulrike Fischer May 14 at 18:34
2

Welcome to TeX.SX! You could redefine the original command to include a strut, which is an invisible typesetting element of a minimum height that includes ascender and descender heights. However, your \url command that omits http:// will not link anywhere. If you don't want to show that part I suggest you use \href. I have modified both to include a strut below. Note you will need to escape special chars, like \# or \%.

\documentclass[a4paper, 12pt]{article}
\usepackage{hyperref}
\usepackage{xcolor}

\let\oldurl\url
\renewcommand{\url}[1]{\oldurl{#1}\strut}

\let\oldhref\href
\renewcommand{\href}[2]{\oldhref{#1}{#2}\strut}

\hypersetup{%
    pdfborderstyle={/S/U/W 0.5}%
}

\begin{document}
\oldurl{http://www.example.com} \\

\url{www.hhhhh.com}\\

\href{https://www.overleaf.com}{www.overleaf.com}    
\end{document}

enter image description here

  • 1
    This will break if the url contains special chars like # or %. – Ulrike Fischer May 14 at 14:24
  • I am aware of the issue with leaving out http, thanks. I just had to leave it out for demonstration purposes because every link with http contains a p and therefore has a descender. – LokiRagnarok May 14 at 14:39
  • Is there any way to adjust this answer for other types of links besides manually set \href and \url? (say, links set by a \footnote, links from the table of contents to the respective page, links from the table of figures to the respective figure, and so on?) – LokiRagnarok May 14 at 14:42
  • @KonstantinKotenko Does Ulrike's answer address the other links? – erik May 14 at 17:43
  • @erik As far as I can tell, yes. – LokiRagnarok May 16 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.