Context
In my document, I attach a tooltip to the middle part of a word. To demonstrate this behavior, try mousing over just the letter V in the word AVA.
Here is the implementation and usage of my \tooltip command.
%% This tooltip command
\newcommand\tooltip[2]{%
\special{pdf:bann<</Type/Annot /Subtype/Widget /FT/Btn /Ff 65536 /H/N /TU(#2)>>}%
#1%
\special{pdf:eann}%
}
%% is used like so
A\tooltip{V}{Tooltip text here.}A
%% which expands like so.
A\special{…}V\special{…}A
The specifics of making the tooltips are not directly relevant the question, but I have included it for context.
Problem
Separating the parts of the word by interspersing \special{}, i.e. A\special{}V\special{}A causes the kerning between parts to no longer apply.¹ (Note: \special is not special — this behavior also occurs in A{V}A and A{}V{}A, unless one uses XeLaTeX or LuaLaTeX.²)
Partial solution
I was able to restore kerning in the latter two parts using the following command, adapted from this answer, which uses \futurelet to do the trick.
\newcommand\kernright[1]{\def\hltext{#1}\futurelet\hlnext\hldokern}
\def\hldokern{%
\sbox0{\mbox\hltext\mbox\hlnext}\sbox2{\hltext\hlnext}\kern\dimexpr\wd2-\wd0\relax%
}
(Although that question specifically concerns kerning between different styles, the solution still works. Another more generic question has no satisfactory solution.)
Here are three wrapper commands for \tooltip.
\newcommand\tooltipA[2]{#1}
\newcommand\tooltipB[2]{\tooltip{#1}{#2}}
\newcommand\tooltipC[2]{\tooltip{#1}{#2}\kernright{#1}}
Below is a comparison table and the corresponding rendered image.
 A\tooltipX{V}{}A expands to AV kerned? VA kerned?
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ ‾‾‾‾‾‾‾‾‾‾ ‾‾‾‾‾‾‾‾‾‾
A. Normal kerned text AVA yes yes
B. Interrupted text A\special{}V\special{}A no no
C. Partial solution A\special{}V\special{}\kernright{V}A no yes
However, I have not been able to come up with a way to write a \tooltipD command that would also restore kerning between A and V, as there does not appear to be a command that does the “opposite” of \futurelet.
Question
How do I restore the kerning between A and V?
I would expect to insert an additional \kernleft command somewhere in the body of the \tooltipC command to yield an output equivalent to the top part of the image.
MWE
(Tooltips should work in XeLaTeX.³)
\documentclass[varwidth=true]{standalone}
\newcommand\tooltip[2]{%
\special{pdf:bann<</Type/Annot /Subtype/Widget /FT/Btn /Ff 65536 /H/N /TU(#2)>>}%
#1%
\special{pdf:eann}%
}
\newcommand\kernright[1]{\def\hltext{#1}\futurelet\hlnext\hldokern}
\def\hldokern{%
\sbox0{\mbox\hltext\mbox\hlnext}\sbox2{\hltext\hlnext}\kern\dimexpr\wd2-\wd0\relax%
}
% A-C are from original question; D is adapted from Steven's answer
\newcommand\tooltipA[2]{#1}
\newcommand\tooltipB[2]{\tooltip{#1}{#2}}
\newcommand\tooltipC[2]{\tooltip{#1}{#2}\kernright{#1}}
\newcommand\tooltipD[2]{%
#1%
\setbox0=\hbox{#1}\kern-\wd0%
\tooltip{#1}{#2}%
\kern-\wd0#1%
}
\begin{document}
\tooltip{Foo}{Bar}
A\tooltipA{V}{Tooltip text}A
A\tooltipB{V}{Tooltip text}A
A\tooltipC{V}{Tooltip text}A
A\tooltipD{V}{Tooltip text}A
\end{document}



\specialtargeted for? – David Carlisle Mar 07 '14 at 08:34f\special{}fito use an ffi ligature. (Solutions involving typesetting the original string might be easier if you do ligatures as well as kerns) – David Carlisle Mar 07 '14 at 09:27