# Is it possible to use LetterSpace (fontspec) with urls (hyperref)?

Using fontspec (with xelatex in this case), LetterSpace in an option for fonts that allows for adjustment of the spacing between glyphs.

However, the hyperref package seems to override this, as shown in the following MWE:

\documentclass[preview,convert]{standalone}

\usepackage{fontspec}
\setmainfont[Scale=MatchUppercase]{Libertinus Serif}
\setmonofont[Scale=MatchLowercase,LetterSpace=-10]{Liberation Mono}
\newfontfamily\regularmono[Scale=MatchLowercase]{Liberation Mono}

\usepackage{hyperref}

\begin{document}

http://www.example.com                 % main font

\texttt{http://www.example.com}        % uses monofont
% respects LetterSpace

{\regularmono http://www.example.com}  % without LetterSpace

\url{http://www.example.com}           % uses monofont
% does not respect LetterSpace

\end{document}


Output:

In this example, the monospace text with \texttt{} uses the LetterSpace setting, whereas the monospace text with \url{}, while clearly using the same font, does not appear to respect the LetterSpace setting, despite it being set for all occurrences of the font (or so I thought I understood).

My question is what options there are for getting past this apparent limitation. In other words, how can one adjust the spacing between glyphs in hyperref urls?

Edit: a related question was asked before (I looked several times for related questions but somehow missed this one).

• A workaround: \href{http://www.example.com}{\texttt{http://www.example.com}} (does not handle special characters in the url). May 29 '20 at 6:25
• This is the best answer so far. If you'd type it up and add a line to my MWE with this solution and show the output, I'd be happy to accept it. If you'd prefer not to, I can write it up as an answer and accept it. Also, which "special symbols" aren't handled? Punctuation characters seem to work as expected. May 31 '20 at 16:14

Here is a workaround: \href{http://www.example.com}{\texttt{http://www.example.com}}

But you can't use special characters (as ~, % or #...) directly in the URL printed by \texttt.

\documentclass[preview]{standalone}

\usepackage{fontspec}
\setmainfont[Scale=MatchUppercase]{Libertinus Serif}
\setmonofont[Scale=MatchLowercase,LetterSpace=-10]{Liberation Mono}
\newfontfamily\regularmono[Scale=MatchLowercase]{Liberation Mono}
\usepackage{hyperref}

\begin{document}

http://www.example.com                 % main font

\texttt{http://www.example.com}        % uses monofont
% respects LetterSpace

{\regularmono http://www.example.com}  % without LetterSpace

\url{http://www.example.com}           % uses monofont
% does not respect LetterSpace

\href{http://www.example.com}{\texttt{http://www.example.com}}
% uses monofont
% does respect LetterSpace

\href{http://www.example.com/%special#characters~}{\texttt{http://www.example.com/\%special\#characters\~{}}}

\url{http://www.example.com/%special#characters~}

\end{document}


When compiled with lualatex:

urls are typeset in math mode and math mode handles spacing differently. With xelatex imho your only option it to typeset the url in text mode as suggested by Paul in a comment. With lualatex which implements letter spacing differently your example works:

• The spacing around the punctuation characters in your lualatex example seem to behave differently in \texttt{} and \url{}. Specifically, the punctuation characters in \url{} seem to not respect LetterSpace. May 29 '20 at 20:02
• You need to change \Urlmuskip for this spaces. See e.g tex.stackexchange.com/questions/326809/… May 29 '20 at 20:12
• Interesting. What is the correspondence between \Urlmuskip values and LetterSpace values? That is, if I have one set to a given value, how can I calculate what to set the other one to? May 29 '20 at 20:44