# Setting the border of links in hypersetup always at the same height

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}


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{
}

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

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


• 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

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}


• 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