# Correct kerning of hyperlinks in math mode

I use hyperlinks in math mode to if a reader does not read my thesis sequentially and finds a very arcane symbol somewhere, he may just click it and go to where it is defined. However, adding links changes the kerning making subscripts and superscripts more spaced out and other ugly things.

Consider this MWE:

\documentclass{article}

\usepackage{amsmath, amssymb, fontspec, unicode-math, tikz}

\begin{document}

Let \hypertarget{a}{$X\colon[0,1]\times\Omega\to\mathbb{R}$} be a stochastic
process.

\tikz[overlay, baseline, anchor=base west, inner sep=0, blue]
\node {${X}_t$};%
\tikz[overlay, baseline, anchor=base west, inner sep=0, red, opacity=0.5]
\node {$\hyperlink{a}{X}_t$};

\end{document}


The output is

I'm using LuaLaTeX and texlive 2013. Is there any way to correct the kerning?

• Try not to put the \hyperlink in math mode: \hyperlink{a}{${X}_t$}. – Pedro Nov 1 '13 at 14:42
• The thing is that most times I want the hyperlink in the middle of a complicated equation with a bunch of symbols. More than one symbol can have links and some will point to different targets. – Dimas Nov 1 '13 at 15:44
• If you define a command \newcommand{\mathhyper}[2]{\text{\hyperlink{#1}{$#2$}}}, you can use it inside equation environments as \mathhyper{a}{X_t}. – Pedro Nov 1 '13 at 15:58
• It still gives the bad kerning. I think I'll just put the subscript inside the link. – Dimas Nov 1 '13 at 16:12
• Do you know if there is a constant amount of space inserted after the hyperlink, or if it varies depending on what follows? I have to sort some packages out (no tikz on this system) before I can experiment,but I'm wondering if a constant negative space could be used. – Chris H Nov 1 '13 at 16:19

Edit A real solution uses this answer:

\documentclass{article}
\usepackage{amsmath, amssymb, tikz}

\begin{document}
\tikz[overlay, baseline, anchor=base west, inner sep=0, blue]
\node {${X}_t$};%
\tikz[overlay, baseline, anchor=base west, inner sep=0, red, opacity=0.5]
\node {$\hypersub{a}{X}_t$};
\end{document}


end edit

This is only a workaround for use when you want to link the primary term excluding the subscript. Defining a specific hyperlink for when a subscript follows as:

\newcommand\hypersub[2]{\hyperlink{#1}{#2}\hspace{-.08em}}


allows:

\tikz[overlay, baseline, anchor=base west, inner sep=0, blue]
\node {${X}_t$};%
\tikz[overlay, baseline, anchor=base west, inner sep=0, red, opacity=0.5]
\node {$\hypersub{a}{X}_t$};


to produce:

The mismatch is around 2% of the stroke width of the t.

The explanation is the the text of a hyperlink has to be surrounded by \special commands. These are technically called "whatsits". These essentially force TeX to wrap up its processing of the text. In math mode, this includes appending an italic correction. Since the hyperlink text is a math italic X, the italic correction is nonzero. You get the same shifting of the subscript if you put any other whatsit between The X and its subscript. Examples are \write commands and color changes. Try, for example, $X\write16{test}_t$ or \$\color{red}X\color{black}_t.

• Just out of curiosity, do you know if a \latelua command is also a whatsit node? – Dimas Nov 4 '13 at 16:48
• @Dimas I'm afraid LuaTeX is far outside of my expertise; I don't know what \latelua does. But, generally, if some feature requires precise positioning in the page before or after shipout, then a whatsit (or whatsit-like) node is produced, at least for non-luatex engines. – Dan Nov 6 '13 at 19:30