7

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.

  • 1
    Somewhat related: tex.stackexchange.com/questions/119988/… – user31729 Sep 8 '14 at 11:55
  • @ChristianHupfer Knew you'd come up with that one. ;-) Didn't work on my TeX Distro which is fully up to date. That's not version-dependent of JS, right? – 1010011010 Sep 8 '14 at 12:01
  • @1010011010: I did not tested it for a while ;-) Just test it ... it works, apart from the dragging – user31729 Sep 8 '14 at 12:01
  • @JKreft Could you post some compilable code example using your \term macro? Maybe there is a way (some kind of workaround or solution) to achieve what you are after. – AlexG Sep 8 '14 at 13:25
  • Alex: Here's the code I use right now (stripped down to essentials). I would be open to rewriting \Link which drives my links, if I can find a way to get it to work with both tooltips and internal links in the document. – JKreft Sep 8 '14 at 13:44
5

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}
  • Thanks a bunch, AlexG. This is what I needed. In case I get too creative, what are the limitations of what I should put in a tooltip? Short text seems to work fine, long text appears in one long line. Can I force it to auto-wrap? If not, I can work with short text instead. – JKreft Sep 11 '14 at 7:32
  • @JKreft: longer <tiptext> should go into a \parbox of predefined length. Of course, the parbox shouldn't be wider than linewidth of the main text. Apart from this, there is no limitation. You can even use \includegraphics as tip content. – AlexG Sep 11 '14 at 7:38
  • @JKreft I improved the implementation, removing the need for JavaScript, and added a multiline tip text example (\parbox). However, still AR required for viewing. – AlexG Sep 11 '14 at 8:16
  • Thank you! Letting people mouseover keywords in my rulebook for a definition is going to improve readability so much. – JKreft Sep 11 '14 at 9:25
  • This answer is not working any more. It seems to me, that I am responsible for this, because I initiaded changes to ocgx2....:((( – Peter Ebelsberger Mar 5 '16 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.