I want to use hyperlinks in math mode without changing the spacing or kerning. I.e., I want to define a command \mathhyperref similar to Correct kerning of hyperlinks in math mode but more flexible (I don't want to use different commands depending on if there is a subscript or not and so on). I managed to deal with subscripts (based on \colorlet{saved}{.} inside caption produces error by David Carlisle), but I am still struggeling with \tilde{}:
MWE
\documentclass{article}
\usepackage{amsthm,xcolor,mathtools}
\usepackage[colorlinks]{hyperref}
\newtheorem{definition}{Definition}
\colorlet{linkcolor}{red!70}
\hypersetup{linkcolor=linkcolor}
\newcommand{\mathlinkcolor}[1]{\mathcolor{linkcolor}{#1}}
\newcommand{\mathhyperref}[2]{\mathrlap{\hyperref[#1]{\phantom{\ensuremath{#2}}}}\mathlinkcolor{#2}}
\newcommand{\linkedtheta}{\mathhyperref{def:theta}{\theta}}
%\newcommand{\linkedtheta}{\mathrlap{\hyperref[def:theta]{\phantom{\ensuremath{\theta}}}}\mathlinkcolor{\theta}} % I would like the command to work as similar to theata in terms of spacing as possible, but it should be linked and colored
\begin{document}
$\tilde{\theta}$ %correct
$\tilde{\linkedtheta}$ %wrong (the tilde is too far left)
$M_{\theta}$ % correct
$M_{\linkedtheta}$ % correct I think
$\linkedtheta_a^b$ % correct I think
$\theta_a^b$ % correct
\begin{definition}\label{def:theta}
$\linkedtheta$
\end{definition}
\end{document}
Do you have any idea how to fix this?


\skew{3}\tilde{\linkedtheta}? It might be possible to patch\tildebut I don't think it would be worth it... – campa Nov 16 '23 at 15:50