8

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.

JKreft
  • 470
  • 1
    Somewhat related: http://tex.stackexchange.com/questions/119988/pdftooltip-from-pdfcomment-package-using-latex-code-in-tooltip –  Sep 08 '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 08 '14 at 12:01
  • @1010011010: I did not tested it for a while ;-) Just test it ... it works, apart from the dragging –  Sep 08 '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 08 '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 08 '14 at 13:44
  • Alex: Also, I note that you wrote the \tooltip code I was referring to earlier (Later code than the post Christian linked, a variant with a *-no drag option). I really like the behavior, but can't figure out how to modify your command to get my link to work inside it. – JKreft Sep 08 '14 at 13:56

1 Answers1

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}
AlexG
  • 54,894
  • 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 07: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 07: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 08: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 09: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 05 '16 at 08:46
  • @Peter Ebelsberger --- Could you provide a workaround? – user73438 May 31 '16 at 13:49
  • A quick workaround is to comment these lines: \xdef\@anim@ocg... and \xdef\@anim@offocgs... – user73438 May 31 '16 at 13:54
  • @PeterEbelsberger I have just fixed the code. – AlexG May 31 '16 at 14:49
  • @user73438 I have just fixed the code. – AlexG May 31 '16 at 14:50
  • @AlexG --- Thanks. I works quite well: the tooltip is always shown, and hide some text. And I don't know how to fix that. I tested in Acrobat DC. – user73438 May 31 '16 at 15:42
  • @AlexG — What I mean is it doesn't appear when I place mouse over, it is always over. – user73438 May 31 '16 at 19:50
  • @AlexG — And what I didn't say but appreciated is your quick answer. Thanks. – user73438 May 31 '16 at 19:51
  • @user73438 : I tried with AR-XI on Windows and it works as advertised: moving the mouse pointer over the link opens the tip-box, moving it off hides the tip-box. When clicking the link, the viewer jumps to the link target (the next page, in the present case). – AlexG Jun 01 '16 at 07:31
  • @AlexG — Interesting. On Windows 8.1, with AR-DC, it doesn't. I had other packages which could have interfere… I will try again (answer in 1 month). Thanks anyway. – user73438 Jun 01 '16 at 08:00
  • @user73438 : Now at home, successfully tested with AR-DC. Are your installed packages up-to-date? – AlexG Jun 01 '16 at 18:00
  • @AlexG —It seems that's the problem. I will update and try with no other superfluous package. I will report here (in 1 month or so). – user73438 Jun 01 '16 at 23:51
  • @AlexG, nice piece of code! In a script I wrote I use the \hyperref tag to use an image as an internal link. Does your code work on image links as well? I tried but I got a sequence error. – rdatasculptor Feb 11 '18 at 16:24
  • @rdatasculptor The following example works for me: \hyperlinkWithTip{test}{\includegraphics[width=2cm]{example-image}}{To be explained on the next page} – AlexG Feb 11 '18 at 20:01
  • @AlexG, thanks for the quick response. I still get sequence errors. Maybe it's related to the fact that I use LaTeX within rmarkdown, I am not sure. I will make an issue when I find some time. – rdatasculptor Feb 12 '18 at 10:00
  • @AlexG I hope you are not bothered me asking, but do you think this https://tex.stackexchange.com/q/415127/95444 can be easily solved? – rdatasculptor Feb 14 '18 at 09:35
  • Hm. I don't use rmarkdown nor pdfcomment pkg. Could you turn the code into LaTeX code that could directly be processed (with (pdf)latex)? Maybe this would raise the chances for getting an answer. (The pdfcomment developer quite regularly drops in here.) – AlexG Feb 14 '18 at 09:44
  • @AlexG I added the tex code rmarkdown produces. Does that help? Many thanks in advance! – rdatasculptor Feb 14 '18 at 10:54
  • @AlexG on topic again. My code throws the error: pdfbase.sty not found. Is it possible to alter the hyperlinkwithtip code to not needing pdfbase package? – rdatasculptor Feb 14 '18 at 13:44
  • It is part of media9 pkg. It seems that your TeX installation is outdated. You should update it (TeXLive 2017 or MiKTeX). – AlexG Feb 14 '18 at 14:24
  • @AlexG thanks for pointing that out. I never had problems with that installation before so updating it didn't come to my mind. After updating it another peculiar problem appeared. You were mentioned in the comments as the one who could solve it... :-) https://tex.stackexchange.com/q/416615/95444 – rdatasculptor Feb 23 '18 at 19:38