[Tex/LaTex] Mouseover with Hyperref Internal Links

hyperref

I have a PDF that is heavily internally linked. (I use terms and link them to the appendix definition with \term{termname}{displaytext}, which uses \hyperlink from the hyperref package.) I'm trying to create mouseover text to be part of that command.

(1) Is there a way inside hyperref to have a mouseover for \hyperlink?

I have tried cooltooltips, but it creates URL hyperlinks, not internal reference links, so it won't work with what I want to do.

(2) Is there a way to make cooltooltips create an internal link instead of a URL link?

I've also examined the tooltip code published on several answers (but not as a package) by a user, where he creates a \tooltip command. Unfortunately, his command nulls out my links. (I've tried \tooltop{\term{name}{display}} and \term{name}{\tooltip{display}}. The internal links don't work.)

(3) Is there a way to modify that code so that it accepts an internal reference argument?

TLDR version: I want to create a command \term{internal link}{display}{mouseover}, and none of the existing packages seem to allow this.


Added code example:

\documentclass[12pt,oneside]{book}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage[log-declarations=false]{xparse}%To enable better command creation
\usepackage[%ALWAYS LOAD THIS LAST
        colorlinks=false,
        pdfborder={0 0 0},
%        pdftitle=\booktitle,
%        pdfauthor=\theauthor,
        pdfpagemode=UseNone,
        bookmarksopen=true,
        bookmarks=true
     ]{hyperref}


\colorlet{Conds}{Black}
\colorlet{Terms}{Green}

\newenvironment{terms}{\small\bfseries\color{Terms}}{}
\newenvironment{conds}{\ttfamily\color{Conds}}{}

\newcommand\envtype{}
\DeclareDocumentCommand \Link {o m O{}}{%
    \begin{\envtype}%
    \IfNoValueTF{#1}{\lowercase{\hyperlink{\envtype:#2}}{#2#3}}{\lowercase{\hyperlink{\envtype:#1}}{#2#3}}%
    \end{\envtype}%
    }


%%%%Specific Links:
\newcommand{\term}{\renewcommand{\envtype}{terms}\Link}
\newcommand{\cond}{\renewcommand{\envtype}{conds}\Link}
%%(Many other link types cut)

\begin{document}
Lots of text. \term{Test} this if you can. Trying it with cooltooltips \cond{foo}[bars] everything up.

\newpage
\hypertarget{terms:test}{Here's my link: Test is a term.}

\hypertarget{conds:foo}{Here's another link: Foo is a condition.}
\end{document}

The reason for the complicated \Link command is two-fold: First, it handles the \lowercase problem correctly, allowing me to always reference the lowercased version for the link. Secondly, I have 5 types of keywords, each with their own formatting, all of which use \Link, and coding it once makes the document work consistently.

Best Answer

Here is a generic solution, implementing the command

\hyperlinkWithTip{<destination>}[<linkcolour>]{<linktext>}[<tipboxcolour>]{<tiptext>}

It is similar to hyperref's

\hyperlink{<destination>}{<linktext>}

command, except that the new command has an additional 3rd mandatory argument {<tiptext>} which provides the tip text to be shown when the mouse hovers over the link.

Optional arguments [<linkcolour>] and [<tipboxcolour>] do what they are supposed to do.

The example contains two hyperlinks with tip text. One of them makes use of \parbox to produce multiline tip text.


\documentclass[twoside,a5paper]{book}

\usepackage{hyperref}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% usage:
%
% \hyperlinkWithTip{<destination>}[<linkcolour>]{<linktext>}[<tipboxcolour>]{<tiptext>}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{pdfbase,ocgbase,xparse}
\usepackage{xcolor}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{calc}

\ExplSyntaxOn
\let\tpPdfLink\pbs_pdflink:nn
\let\tpPdfAnnot\pbs_pdfannot:nnnn\let\tpPdfLastAnn\pbs_pdflastann:
\def\tpPdfXform{\pbs_pdfxform:nnnnn{1}{1}{}{}}\let\tpPdfLastXform\pbs_pdflastxform:
\let\tpPdfObj\pbs_pdfobj:nnn\let\tpPdfLastObj\pbs_pdflastobj:
\let\tpPdfAppendToFields\pbs_appendtofields:n
\ExplSyntaxOff

\makeatletter
\NewDocumentCommand{\hyperlinkWithTip}{mO{blue}mO{yellow!20}m}{{%
  \leavevmode%
  \ocgbase@new@ocg{tipOCG.\thetcnt}{/Print<</PrintState/OFF>>/Export<</ExportState/OFF>>}{off}%
  \tpPdfLink{%
    /Subtype/Screen%
    /AA<<%
      /E<</S/SetOCGState/State [/ON \ocgbase@last@ocg]>>%
      /X<</S/SetOCGState/State [/OFF \ocgbase@last@ocg]>>%
      /U<</S/SetOCGState/State [/OFF \ocgbase@last@ocg]/Next<</S/GoTo/D (#1)>>>>%
    >>%
  }{{\color{#2}#3}}%
  \sbox\tiptext{\fcolorbox{black}{#4}{#5}}%
  \edef\twd{\the\wd\tiptext}%
  \edef\tht{\the\ht\tiptext}%
  \edef\tdp{\the\dp\tiptext}%
  \measureremainder{\whatsleft}\tipshift=0pt%
  \ifdim\whatsleft<\twd\setlength\tipshift{\whatsleft-\twd}\fi%
  \tpPdfXform{\tiptext}%
  \raisebox{\heightof{#3}+\tdp}[0pt][0pt]{\makebox[0pt][l]{\hspace{\tipshift}%
    \tpPdfAnnot{\twd}{\tht}{\tdp}{%
      /Subtype/Widget/FT/Btn/Ff 65537/T (tip:\thetcnt)%
      /AP<</N \tpPdfLastXform>>%
      /MK<</TP 1/I \tpPdfLastXform/IF<</S/A/FB true/A [0.0 0.0]>>>>%
      /OC \ocgbase@last@ocg
    }%
    \tpPdfAppendToFields{\tpPdfLastAnn}%
  }}%
  \stepcounter{tcnt}%
}}
\makeatother
\newsavebox\tiptext\newcounter{tcnt}
\newlength{\whatsleft}\newlength{\tipshift}
\newcommand{\measureremainder}[1]{%
  \begin{tikzpicture}[overlay,remember picture]
    \path let \p0 = (0,0), \p1 = (current page.east) in
      [/utils/exec={\pgfmathsetlength#1{\x1-\x0}\global#1=#1}];
  \end{tikzpicture}%
} 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

Lots of text. \hyperlinkWithTip{test}{Test}{To be explained on the next page} this if you can.

Lots of text. Another \hyperlinkWithTip{testB}{test}{\parbox[b]{0.5\linewidth}{This is a longer text. Don't miss the explanation given on the next page.}} if you wish.
\newpage

\hypertarget{test}{Here's my link destination.}

\hypertarget{testB}{Yet another link destination.}

\end{document}
Related Question