# Possible incompatibility between biblatex and hyperref's pdfmark?

I am trying to use biblatex's \citetitle inside a \pdfmark so that the title of a citation pops up as a tooltip in PDFs.

Code looks like this:

%% A tooltip macro using pdfmark

\newcounter{tooltip}
% Creates a tooltip text.
%  arg #1 : the text to display
%  arg #2 : the text to show in the tooltip
\newcommand\tooltip[2]{%
\pdfmark[#1]{%
pdfmark=/ANN,%
Subtype=/Widget,%
Raw={%
/TU (#2)/T (tooltip \thetooltip)%
/FT/Btn/Ff 65536/H/N%
}%
}


Now I can use the tooltip macro with regular text and with commands I create, for example:

% will show "some text" with "tooltip text" as tooltip on mouseover
\tooltip{some text}{tooltip text}

% will show "Tooltip tooltip" as tooltip
\newcommand{\test}[1]{Tooltip #1}
\tooltip{some text}{\test{tooltip}}


However, if I use \citetitle, it does not expand:

% will show "\citetitle{key}" as tooltip
\tooltip{some text}{\citetitle{key}}


I tried adding some \expandafter at some places to no avail.

Also, inside my tooltip macro \citetitle gets expanded (e.g., if I use #2 outside \pdfmark). It seems that only inside the RAW argument it does not get expanded. On the other hand, my simple command gets expanded also in the RAW.

Since I do not know much about TeX, I was not able to debug the problem further, but it seems like some incompatibility between RAW and the way biblatex defines its macros. Any help?

-

The /TU argument of Raw expects unformatted text. Any citation command is devised to format bibliographic data, so the problem isn't really an issue with biblatex. You can access unformatted data using the biblatex commands \thefield, \thename and \thelist. The latter two return name and literal list fields, respectively. Unfortunately they don't give output suitable for printing.

Below we define the citation commands \tooltipcitefield and \tooltipcitename, which make use of \thefield and \thename. These macros have three arguments to specify the display text, field name and entry keys. If the specified field isn't labelnumber, the fields are prefixed with the label number wrapped in brackets.

\documentclass{article}
\usepackage[american]{babel}
\usepackage{csquotes}
\usepackage[maxcitenames=2]{biblatex}
\usepackage[dvips]{hyperref}

\newcounter{tooltip}
\newcommand*{\tooltip}[2]{%
\pdfmark[#1]{
pdfmark=/ANN,Subtype=/Widget,
Raw={/TU (#2 )                % annotation text
/T (tooltip \thetooltip) % annotation title
/FT /Btn                 % button field
/Ff 65536                % push button field flag
/H /N                    % no highlighting
}
}%
\stepcounter{tooltip}}

\makeatletter

\newrobustcmd*{\cbx@displaytext}{}

\newrobustcmd*{\mkbibtooltip}[1]{%
#1\cbx@displaytext%
\settowidth{\@tempdima}{\cbx@displaytext}%
\nobreak\hskip-\@tempdima%
\tooltip{\hskip\@tempdima}{\csuse{cbx@tooltiptext}}%
\csgdef{cbx@tooltiptext}{}}

\DeclareCiteCommand{\cbx@tooltipcitefield}[\mkbibtooltip]
{\iffieldundef{prenote}{}{\BibliographyWarning{Ignoring prenote argument}}}
{\ifcsstring{cbx@field}{labelnumber}
{}
{\csxappto{cbx@tooltiptext}{[\thefield{labelnumber}] }}%
\csxappto{cbx@tooltiptext}{\thefield{\csuse{cbx@field}}}}
{\csgappto{cbx@tooltiptext}{; }}
{\iffieldundef{postnote}{}{\BibliographyWarning{Ignoring postnote argument}}}

\newrobustcmd*{\tooltipcitefield}[2]{%
\renewrobustcmd*{\cbx@displaytext}{#1}%
\csgdef{cbx@field}{#2}%
\cbx@tooltipcitefield}

\DeclareIndexNameFormat{thename}{%
\ifnumgreater{\value{listcount}}{\value{liststart}}
{\ifboolexpr{ test {\ifnumless{\value{listcount}}{\value{liststop}}}
or test \ifmorenames }
{\csgappto{cbx@tooltiptext}{, }}
{\ifnumgreater{\value{liststop}}{2}{\csgappto{cbx@tooltiptext}{,}}{}%
\csgappto{cbx@tooltiptext}{ and }}}
{}%
\ifuseprefix{\ifblank{#5}{}{\csgappto{cbx@tooltiptext}{#5 }}}{}%
\csgappto{cbx@tooltiptext}{#1}%
\ifboolexpr{ test {\ifnumequal{\value{listcount}}{\value{liststop}}}
and test \ifmorenames }
{\ifnumgreater{\value{liststop}}{1}{\csgappto{cbx@tooltiptext}{,}}{}%
\csgappto{cbx@tooltiptext}{ et al.}}
{}}

\DeclareCiteCommand{\cbx@tooltipcitename}[\mkbibtooltip]
{\iffieldundef{prenote}{}{\BibliographyWarning{Ignoring prenote argument}}}
{\csxappto{cbx@tooltiptext}{[\thefield{labelnumber}] }%
\indexnames[thename]{\csuse{cbx@name}}}
{\csgappto{cbx@tooltiptext}{; }}
{\iffieldundef{postnote}{}{\BibliographyWarning{Ignoring postnote argument}}}

\newrobustcmd*{\tooltipcitename}[2]{%
\renewrobustcmd*{\cbx@displaytext}{#1}%
\csgdef{cbx@name}{#2}%
\cbx@tooltipcitename}

\makeatother

\begin{document}
Filler text \tooltipcitefield{\cite[e.g.][]{bertram,cicero}}{labeltitle}{bertram,cicero}.
Filler text \tooltipcitename{\cites[10--15]{bertram}[10]{cicero}}{labelname}{bertram,cicero}.
\tooltipcitefield{Tooltip titles}{labeltitle}{bertram,companion,cicero}.
\tooltipcitename{Tooltip names}{labelname}{companion,bertram,vangennep}.
\tooltipcitefield{\emph{Tooltip numbers}}{}{cicero,companion}.
\printbibliography
\end{document}


Here's an image of the document taken from Adobe Reader after compiling with latex, bibtex, dvips and ps2pdf.

This solution has a few open problems.

• Linebreaks. When the display text breaks across a line, the tooltip is applied only to the text after the linebreak. See the above document for an example.

• Hyperlinks and tooltips. These don't combine well. As a workaround the citation commands apply the tooltip to the baseline of the display text. This area is about 3pt high. It can be made higher and be displayed on mouse rollover. Some details on the \pdfmark options can be found on D.P. Story's page.

• Citations as display text. With the existing commands you have to specify the entry keys twice. Moreover the tooltip area includes pre- and postnotes and only one tooltip is assigned to citation lists. All of these problems probably can't be addressed with the workaround described above.

-
Thanks Audrey, that works exactly like I asked! Just forgot to add something to the question, perhaps you can help. If I do \tooltipcite{\cite{ctan}}{labeltitle}{ctan} then I get the tooltip, but the hyperref link does not work in the pdf. Is there a simple workaround? – ttonelli Mar 13 '12 at 19:23
@ttonelli I think so, but I'll need to take a look. – Audrey Mar 13 '12 at 19:44
Thanks! I should note that I am also using dvips and ps2pdf. Using Okular I can see tooltips in the dvi, unless the base text is \cite{} (then the tooltip says "go to page X"); in the pdf I cannot see the tooltips. Now, in Acrobat Reader (which is my goal), I can see the tooltip always, even if I use \cite{} for the base text. But then, the link does not work in Acrobat... – ttonelli Mar 13 '12 at 20:06
@ttonelli The links work, but you have to click outside the tooltip area, toward the top of the link box. This isn't very user-friendly. The issue has been discussed at comp.text.tex. I think a workaround could be found if tooltip area can be made smaller. I'll look into this and make an edit to this answer when I find some time. If it's any consolation, I found a nice way to format name lists. So the edit will at least give you that. – Audrey Mar 13 '12 at 20:27
Yes, I can confirm that I can follow the link by positioning the mouse at the top. Thanks again for the help! – ttonelli Mar 13 '12 at 20:35