20

I'm looking for a way to get popup references when hovering with the mouse cursor over the references in my papers. I looked around on this forum and found this question and hpesoj626's answer very useful:

Interactive PDF, Latex and Article of the Future

However, it's imperative that I can use opentype/truetype fonts in my document, including CJK. Previously, I've used XeLaTeX. I've realized that XeLaTeX is not an option for this kind of thing, since it doesn't produce PDFs in the same way. However, there is LuaLaTeX, which, as far as I understand, works similarly to pdflatex?

For my own purposes (unicode, CJK) I could just as well use LuaLaTeX (although I haven't tried this yet).

What I cannot use is simple pdflatex, which is what is used in the Perl script provided in hpesoj626's answer to the question I linked to above.

Is there a way to make this script work with unicode and opentype fonts?

Mårten
  • 2,194
  • 2
    I would try first changing my $pdflatex="pdflatex"; into my $pdflatex="lualatex"; in the Perl script. – egreg May 02 '13 at 08:02
  • @egreg: Switching pdflatex to luatex can be done with command line argument now (the development version at https://bitbucket.org/robert.marik/fancytooltips), for example perl ~/path/fancy-preview filename --pdflatex="/path/lualatex" – robert.marik.cz Aug 11 '13 at 20:10

1 Answers1

28

Here is a non-fancytooltips solution that supports any driver, including xelatex, allowing you to use OpenType and TrueType fonts. It makes use of the macro

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

for generating draggable boxes in the PDF display of AdobeReader.

The macro for creating an inline citation pop-up is:

\citeTip{<bib key>}

Inside the macro, after inserting \cite{<bib key>}, a phantom "|" is passed as <link text> to the first \tooltip argument, and \fullcite{<bib key>} as <tip text> to the second \tooltip argument.

The example uses biblatex/biber. The workflow is latex->biber->latex, run on the base-name of the document source file.

Any flavour of latex can be used. Of course, the fontspec- related lines must be commented out for other drivers than xelatex or lualatex.

enter image description here

\documentclass[
 a4paper,12pt,
% dvipdfmx      %uncomment this for latex->dvipdfmx
]{article}
\usepackage[sorting=none]{biblatex}

%\usepackage{fontspec}
%\setmainfont{Times New Roman}

\usepackage[T1]{fontenc} %standard fonts
\usepackage{lmodern}

\usepackage{hyperref}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% usage: \citeTip{<bib key>}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\citeTip}[1]{%
  \cite{#1}%
  \makebox[0pt][l]{%
    \tooltip*[black!0]{|}{\parbox[b]{\textwidth}{\fullcite{#1}}}%
  }%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% tooltips with LaTeX v. 2017/11/28
%
% \tooltip[*[*[*[*]]]][<link colour>]{<link text>}[<tip box colour>]{<tip text>}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   \tooltip     --> draggable tip, visible on mouse-over, hidden on mouse-out
%               
%   \tooltip*    --> draggable tip, toggle visiblity on mouse-over
%               
%   \tooltip**   --> NON-draggable tip, visible on mouse-over, hidden on mouse-out
%              
%   \tooltip***  --> NON-draggable tip, toggle visiblity on mouse-over
%               
%   \tooltip**** --> NON-draggable tip, toggle visiblity on mouse-click (Evince!)
%
% Default link colour can be set with
%
%   \usepackage[linkcolor=<colour>]{hyperref}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{pdfbase}[2017/03/16]
\usepackage{xparse,ocgbase}
\usepackage{xcolor,calc}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}

\ExplSyntaxOn
\let\tpPdfLink\pbs_pdflink:nn
\let\tpPdfAnnot\pbs_pdfannot:nnnn\let\tpPdfLastAnn\pbs_pdflastann:
\let\tpAppendToFields\pbs_appendtofields:n
\def\tpPdfXform{\pbs_pdfxform:nnnnn{1}{1}{}{}}
\let\tpPdfLastXform\pbs_pdflastxform:
\ExplSyntaxOff

\makeatletter
\NewDocumentCommand{\tooltip}{%
  ssssO{\ifdefined\@linkcolor\@linkcolor\else blue\fi}mO{yellow!20}m%
}{{%
  \leavevmode%
  \IfBooleanT{#2}{%
    %for variants with two and more stars, put tip box on a PDF Layer (OCG)
    \ocgbase@new@ocg{tipOCG.\thetcnt}{%
      /Print<</PrintState/OFF>>/Export<</ExportState/OFF>>%
    }{false}%
    \xdef\tpTipOcg{\ocgbase@last@ocg}%
    %prevent simultaneous visibility of multiple non-draggable tooltips
    \ocgbase@add@ocg@to@radiobtn@grp{tool@tips}{\ocgbase@last@ocg}%
  }%
  \tpPdfLink{%
    \IfBooleanTF{#4}{%
      /Subtype/Link/Border[0 0 0]/A <</S/SetOCGState/State [/Toggle \tpTipOcg]>>
    }{%
      /Subtype/Screen%
      /AA<<%
        \IfBooleanTF{#3}{%
          /E<</S/SetOCGState/State [/Toggle \tpTipOcg]>>%
        }{%  
          \IfBooleanTF{#2}{%
            /E<</S/SetOCGState/State [/ON \tpTipOcg]>>%
            /X<</S/SetOCGState/State [/OFF \tpTipOcg]>>%
          }{
            \IfBooleanTF{#1}{%
              /E<</S/JavaScript/JS(%
                var fd=this.getField('tip.\thetcnt');%
                if(typeof(click\thetcnt)=='undefined'){%
                  var click\thetcnt=false;%
                  var fdor\thetcnt=fd.rect;var dragging\thetcnt=false;%
                }%
                if(fd.display==display.hidden){%
                  fd.delay=true;fd.display=display.visible;fd.delay=false;%
                }else{%
                  if(!click\thetcnt&&!dragging\thetcnt){fd.display=display.hidden;}%
                  if(!dragging\thetcnt){click\thetcnt=false;}%
                }%
                this.dirty=false;%
              )>>%
            }{%
              /E<</S/JavaScript/JS(%
                var fd=this.getField('tip.\thetcnt');%
                if(typeof(click\thetcnt)=='undefined'){%
                  var click\thetcnt=false;%
                  var fdor\thetcnt=fd.rect;var dragging\thetcnt=false;%
                }%
                if(fd.display==display.hidden){%
                  fd.delay=true;fd.display=display.visible;fd.delay=false;%
                }%
               this.dirty=false;%
              )>>%
              /X<</S/JavaScript/JS(%
                if(!click\thetcnt&&!dragging\thetcnt){fd.display=display.hidden;}%
                if(!dragging\thetcnt){click\thetcnt=false;}%
                this.dirty=false;%
              )>>%
            }%  
            /U<</S/JavaScript/JS(click\thetcnt=true;this.dirty=false;)>>%
            /PC<</S/JavaScript/JS (%
              var fd=this.getField('tip.\thetcnt');%
              try{fd.rect=fdor\thetcnt;}catch(e){}%
              fd.display=display.hidden;this.dirty=false;%
            )>>%
            /PO<</S/JavaScript/JS(this.dirty=false;)>>%
          }%
        }%
      >>%
    }%
  }{{\color{#5}#6}}%
  \sbox\tiptext{%
    \IfBooleanT{#2}{%
      \ocgbase@oc@bdc{\tpTipOcg}\ocgbase@open@stack@push{\tpTipOcg}}%
    \fcolorbox{black}{#7}{#8}%
    \IfBooleanT{#2}{\ocgbase@oc@emc\ocgbase@open@stack@pop\tpNull}%
  }%
  \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%
  \IfBooleanF{#2}{\tpPdfXform{\tiptext}}%
  \raisebox{\heightof{#6}+\tdp}[0pt][0pt]{\makebox[0pt][l]{\hspace{\tipshift}%
    \IfBooleanTF{#2}{\usebox{\tiptext}}{%
      \tpPdfAnnot{\twd}{\tht}{\tdp}{%
        /Subtype/Widget/FT/Btn/T (tip.\thetcnt)%
        /AP<</N \tpPdfLastXform>>%
        /MK<</TP 1/I \tpPdfLastXform/IF<</S/A/FB true/A [0.0 0.0]>>>>%
        /Ff 65536/F 3%
        /AA <<%
          /U <<%
            /S/JavaScript/JS(%
              var fd=event.target;%
              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 fdr=fd.rect;%
                fdr[0]+=dX;fdr[1]+=dY;fdr[2]+=dX;fdr[3]+=dY;%
                fd.rect=fdr;mX=nX;mY=nY;%
              };%
              if(!dragging\thetcnt){%
                dragging\thetcnt=true;Int=app.setInterval("drag()",1);%
              }%
              else{app.clearInterval(Int);dragging\thetcnt=false;}%
              this.dirty=false;%
            )%
          >>%
        >>%
      }%
      \tpAppendToFields{\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{filecontents*}{my.bib}
@article{einstein05,
    author={Albert Einstein},
    title="Zur Elektrodynamik bewegter K{\"o}rper",
    journal={Annalen der Physik und Chemie},
    volume={17},
    year={1905},
    pages={891--921}
}

@article{einstein15,
    author={Albert Einstein},
    title="Die Grundlage der allgemeinen Relativit{\"a}tstheorie",
    journal={Annalen der Physik},
    volume={354},
    year={1916},
    pages={769--822}
}
\end{filecontents*}

\addbibresource{my.bib}
\begin{document}
The principles of the Special Relativity Theory were first published in
1905\citeTip{einstein05}. Einstein coined the term `Special Relativity
Theory' while working on the General Relativity Theory, published in
1916\citeTip{einstein15}.
\printbibliography
\end{document}
AlexG
  • 54,894
  • That works beautifully and it's exactly what I wanted. I just have two questions/problems: 1) When I open the document in a reader other than Adobe Acrobat/Reader, that lack support for popups, all the pop-ups are displayed by default, as opposed to being concealed by default. This makes the document unreadable. Is there any way to make them concealed by default instead? My second question is 2) is it possible to make all the biblatex citation commands (e.g. \cite, \parencite, \headlesscite etc) function like \citeTip by default? This would also enable me to use a different citation style. – Mårten May 02 '13 at 13:49
  • 2
    @Mårten: Currently only AdobeReader supports this feature. I could simplify the code to not use JavaScript. But still AR is required. – AlexG May 02 '13 at 14:23
  • Yes I understand that, but when I used the example that was linked to in my original question, I could read the document without the popups in other readers, whereas with this one, the popups appear as open in the document when read in other readers, meaning it cannot be read using those readers at all (as opposed to simply being read as traditional PDFs with no popups). Do you see what I mean? – Mårten May 02 '13 at 14:25
  • 1
    @Mårten: For the other citing commands, copy the code of the \newcommand\citeTip... command, and replace \cite by \parencite etc. – AlexG May 02 '13 at 14:25
  • 1
    @Mårten: Which other PDF viewers did you try? – AlexG May 02 '13 at 14:27
  • Preview; the reader included in the editor TeXPad; and Skim (all three on Mac OS). Thanks for the clarification re the citation commands, I will try them out now! – Mårten May 02 '13 at 14:30
  • @Mårten: After the edit, pop-ups remain hidden in alternative PDF viewers. – AlexG May 03 '13 at 11:08
  • The result is still the same for me, I don't know what's wrong (tried the same 3 readers as before) :/ – Mårten May 03 '13 at 14:36
  • @Mårten: Would you please repost a link to a working PDF? – AlexG May 03 '13 at 15:04
  • https://dl.dropboxusercontent.com/u/402161/paper2.pdf Will this work? – Mårten May 03 '13 at 16:12
  • 1
    @Mårten. Sorry, I meant a pdf that works as you expect (not showing boxes in alternative reader). – AlexG May 03 '13 at 16:34
  • Oh sorry. Here: https://dl.dropboxusercontent.com/u/402161/paper2-fancy.pdf – Mårten May 03 '13 at 16:44
  • 1
    @Mårten: Done. Boxes shouldn't clutter the display anymore in other viewers. – AlexG May 06 '13 at 15:15
  • Great! I can now open the file in other viewers as well. However, the boxes aren't behaving ideally in Adobe Reader 11.0.02 on Mac OS. Once they are opened they cannot again be closed, just moved around. And if I close the document and saves it (Reader considers it modified as soon as it's opened), the boxes will appear at the place where I dragged them before saving and closing. – Mårten May 07 '13 at 00:07
  • 1
    @Mårten: Weird. In Linux AR-9 and Windows AR-10, I can close the tips by wiping across the links again. And they don't ask for saving nor do they tacitly save. At least saving can be suppressed. See my edit. – AlexG May 07 '13 at 07:04
  • I tried the code with lualatex and adobe reader dc. The boxes look great but they are not draggable. – Vivek Sharma Nov 11 '21 at 03:06
  • @AlexG Thank you for a great solution. I am using this with genealogytree package to create a clickable node directed to a biblatex file to give more information about the node. The tooltip appears ok but the text is white and cannot be read, where can one specify the font color of the text in the tooltip – Vivek Sharma Nov 11 '21 at 04:39
  • Hard to tell what might be the reason. Could you please post a new question with a complete compilable example? – AlexG Nov 11 '21 at 07:42
  • I tried your code. I ran biber paper and pdflatex paper. I opened the pdf on Foxit Reader 9.7 on Windows 10. The tooltips show up, but if I click on one, Foxit Reader crashes. If I don't click them, it works OK. In Acrobat Reader DC: the tooltip shows up, I can move it by clicking on it (not click+hold) and clicking again to release it. But sometimes the tooltips disappear. Overall, sadly not impressed with how these PDF readers handle the tooltips. – MD004 Sep 03 '22 at 19:21