# Move-around box in PDF display

I tried using the fancytooltips package and like it a lot. One thing that I could imagine would be nice is to have the entry showing up as a movable box instead of just a tooltip. (Sometimes the tooltip obscures other text.) Is this possible at all to implement? I mean it could take a lot of work, but if it's possible at all I'd like to look into it.

The examples and screenshots of fancytooltips can be found in this post. To quote from that post:

Both Gonzalo Medina and Audrey posted an excellent solution. But the things are simpler now. See the fancy-preview webpage - based on the new version of fancytooltips (May 2012), preview.sty and some bash scripts you get tooltips for bibliographic entries, theorems, definitions, displayed equations etc. automatically.

Small example and screenshots are attached.

\documentclass{article}
\usepackage{amsmath,amsthm}
\newtheorem{lemma}{Lemma}
\usepackage{hyperref} % Important!
\begin{document}

\begin{lemma}[Lemma from \cite{M}]\label{lemma}
If $k<0$, then
$$\label{eq:1} x^2+k<x^2$$
for every real number $x$.
\end{lemma}

Inequality \eqref{eq:1} in Lema \ref{lemma} can be proved easily. Is
more general than \cite[Theorem 3.4]{K}.

\begin{thebibliography}{9}
\bibitem[M]{M} Me: My book related to the problem (2006), 145 p.
% The new line is important!

\bibitem{K} Karl: Karl's paper published in some minor proceedings,
a local conferrence organized by his university (2005), 23--25.
% The new line is important!

\end{thebibliography}
\end{document}


The file in Adobe Reader

Ref M

Ref 1

Equation (1)

Lemma 1.

-
Just because many users (like me) might not be familiar with fancytooltips, could you add a MWE, perhaps even demonstrating the problematic overlap, and a picture of the output? –  doncherry Apr 6 '13 at 17:00
@doncherry Good point, I've just edited to add that! –  Mika H. Apr 6 '13 at 17:10
Thanks! Perhaps you could even include (reproduce) it in this question? We kind of like to have everything in one place. –  doncherry Apr 6 '13 at 17:20
@doncherry Done! –  Mika H. Apr 6 '13 at 21:35

## 1 Answer

This would require hacking the fancytooltips package.

Since fancytooltips depends on packages which are not in TeXLive, I tried something on my own.

Edit:

In addition, the tool-tip command defined below supports all common output drivers (LaTeX workflows):

• latex->dvips->ps2pdf (dvips must not be called with option -Ppdf!)
• latex->dvipdfmx
• xelatex, lualatex, pdflatex

The command

\tooltip[<link text colour>]{<link text>}[<tip box colour>]{<tip text>}


creates a tooltip that can be dragged around.

The colour arguments are optional and default to blue and yellow!20. Predefined colour names and colour expressions according to the colour-model of the xcolor package can be used.

<link text> is breakable across line endings, except for the latex->dvips->ps2pdf workflow.

Multi-line <tip text> should be put into a \parbox or minipage.

\documentclass[
a4paper,
%dvipdfmx     %uncomment this for latex->dvipdfmx
]{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% usage: \tooltip[<link colour>]{<link text>}[<tip box colour>]{<tip text>}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{media9}
\usepackage{xcolor}
\usepackage{calc}
\usepackage{ifxetex}

\ExplSyntaxOn
\let\tpPdfLink\mix_pdflink:nn\let\tpPdfAnnot\mix_pdfannot:nnnn
\let\tpPdfXform\mix_pdfxform:n\def\tpPdfLastXform{\g_mix_pdflastxform_tl}
\ExplSyntaxOff

\newif\ifdvipdfmx
\DeclareOption{dvipdfmx}{\dvipdfmxtrue}
\ProcessOptions*\relax
\ifxetex\dvipdfmxtrue\fi

\ifpdf
\pdfcatalog{/AcroForm << /NeedAppearances true /Fields []>>}
\else
\ifdvipdfmx %dvipdfmx, xelatex
\special{pdf:put
@catalog <</AcroForm << /NeedAppearances true /Fields []>>>>
}
\else %dvips
\special{ps:
mark {Catalog} <</AcroForm << /NeedAppearances true /Fields []>>>> /PUT pdfmark
}
\fi
\fi

\NewDocumentCommand{\tooltip}{O{blue}mO{yellow!20}m}{{%
\leavevmode%
\tpPdfLink{%
/Subtype/Screen%
/AA<<%
/E<</S/JavaScript/JS (%
var fd=this.getField('tip.\thetcnt');%
if(fd.display==display.hidden){%
fd.delay=true;fd.display=display.visible;fd.delay=false;%
}else{fd.display=display.hidden;}%
)>>%
/PO<</S/JavaScript/JS (this.getField('tip.\thetcnt').display=display.hidden;)>>%
>>%
}{{\color{#1}#2}}%
\sbox\tiptext{\fcolorbox{black}{#3}{#4}}%
\edef\twd{\the\wd\tiptext}%
\edef\tht{\the\ht\tiptext}%
\edef\tdp{\the\dp\tiptext}%
\tpPdfXform{\tiptext}%
\raisebox{\heightof{#2}}[0pt][0pt]{\makebox[0pt][l]{%
\tpPdfAnnot{\twd}{\tht}{\tdp}{%
/Subtype/Widget/FT/Btn/Ff 65536/T (tip.\thetcnt)/F 6%
/MK<</TP 1/I \tpPdfLastXform/IF<</S/A/FB true/A [0.0 0.0]>>>>%
/AA <<%
/U <<%
/S/JavaScript/JS(%
var mX=this.mouseX;var mY=this.mouseY;%
var drag=function(){%
var nX=this.mouseX;var nY=this.mouseY;%
var dX=nX-mX;var dY=nY-mY;%
var tf=this.getField('tip.\thetcnt');%
var tfr=tf.rect;%
tfr[0]+=dX;tfr[1]+=dY;%
tfr[2]+=dX;tfr[3]+=dY;%
tf.rect=tfr;%
mX=nX;mY=nY;%
};%
if(typeof(dragging)=='undefined'||!dragging){%
dragging=true;Int=app.setInterval("drag()",1);%
}%
else{app.clearInterval(Int);dragging=false;}%
)%
>>%
>>%
}%
}}%
\stepcounter{tcnt}%
}}
\newsavebox\tiptext\newcounter{tcnt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\parindent=0pt
\begin{document}\Huge
Einstein's \tooltip{formula}{$E=m c^2$} is well known.
Another famous formula is due to \tooltip{Pythagoras}{$a^2+b^2=c^2$}.
\end{document}


How it works:

Two PDF-Annotations (mouse-sensitive regions) are created: (1) A link around the blue link text, which is breakable across line endings. (2) An interactive (/Ff 65536) button (/FT/Btn) widget (/Subtype/Widget) for the tip text.

Visibility of the tip box is controlled via a JavaScript action that is bound to the mouse-enter event of the link. In order to show/hide the tip it is sufficient to repeatedly hover the mouse pointer over the link text.

The tip text may not be directly embedded into the page content; it couldn't be moved around otherwise. Rather, it is typeset into a box (\sbox) and subsequently distilled into a so-called Form-XObject, which is a self-contained PDF entity with graphical and/or textual content. This Form-XObject is bound to the button widget appearance via the /MK entry.

The dragging is implemented as a JavaScript action, which is triggered by the mouse-up event of the tip-text button. The mouse movement is tracked by the function drag() which is repeatedly executed using a JavaScript timer (app.setInterval). The deltas of the mouse position thus determined are continuously added to the coordinates (rect property) of the JavaScript Field object that represents the tip-text button.

-
That looks great, AlexG! Could you please add some comment on what each part of your code is doing, so that I (and others) can understand it more easily? Thanks! –  Mika H. Apr 16 '13 at 15:33
Currently I am a bit short of time. Will do that later. –  AlexG Apr 17 '13 at 6:33
@MikaH: ...Done. Plus some code fixes. –  AlexG Apr 19 '13 at 11:57
@AlexG Mother of God! img.mu.cdn.li/Ao/xqefEH.jpg Excellent work! Questions: Could it be made so that (in your MWE) "formula" could be replaced by \eqref{}, if you know what I mean? Also, does the end-user require anything other than a PDF viewer? –  User 17670 Apr 19 '13 at 14:52
@User17670: First, we would need to redefine the equation environment such that it puts the equation in an \sbox in addition to typesetting it onto the page. Then, \eqref cannot be used as it only inserts the equation number (we do not really want to change this!). Instead, we need a new macro, say, \eqrepeat{...}, to be used as <tip text>, which inserts the previously typeset equation using \usebox. As for requirements, pdflatex is used for typesetting; the end-user only needs AdobeReader. I could extend this code to different drivers (dvips->ps2pdf, XeLaTeX, ...), if needed. –  AlexG Apr 19 '13 at 15:14