Skip to content

Instantly share code, notes, and snippets.

@Sharpie
Created January 14, 2011 21:18
Show Gist options
  • Save Sharpie/780258 to your computer and use it in GitHub Desktop.
Save Sharpie/780258 to your computer and use it in GitHub Desktop.
TeX Code for making tooltips
% Decoding the PDF primitives used by these functions is somewhat possible with
% the help of the PDF spec:
%
% http://www.adobe.com/devnet/pdf/pdf_reference.html
%
% Attempt #1
% Adapted from a post by Herbert Voss on comp.text.tex:
%
% https://groups.google.com/d/msg/comp.text.tex/hjYT89EzN_E/MWXw1eksMhkJ
%
\newcounter{tooltip}
\def\tooltiptarget{\phantom{\rule{1mm}{1mm}}} % Size of the mouse over area that will trigger the tooltip
\newcommand\tooltip[1]{%
\leavevmode\pdfstartlink user{%
/Subtype /Widget % Defines the object as type 'Widget'
/TU (#1) % Contents for the tooltip
/T (tooltip \thetooltip) % Tooltip number, required for Acrobat to display it
/FT/Btn % Formtype of button, activates a change in the pointer and the tooltip
/Ff 65536 % Form flags, no idea what they mean exactly, but keeps the PDF viewer from asking "do you want to save?"
/H/N % Highlighting mode: N for no highlighting when the user interacts with the button
}%
\tooltiptarget%
\pdfendlink%
\stepcounter{tooltip}%
}
% Pros: Works. Is clean.
% Cons: Don't understand everything. Longish delay between mouseover and tooltip display and no idea on how to change that.
% Relevant pages in the spec:
% Section 12.5: Annotations (page 389)
% Section 12.5.6.19: Widget Annotations (page 416)
% Section 12.7: Interactive Forms (page 439)
% Attempt #2
% Adapted from a post by Mycroft on comp.text.tex:
%
% https://groups.google.com/d/msg/comp.text.tex/yTZx-SWGAhA/ywnM4_NgSeUJ
%
\def\tooltiptarget{\phantom{\rule{1mm}{1mm}}}
\newcommand\tooltip[1]{%
\leavevmode\pdfstartlink user{%
/Subtype /Text
/CA 0 % Icon opacity. Preview ignores this.
/Name /Comment % Sets the icon to be a comment balloon
/Contents (#1)
}%
\tooltiptarget%
\pdfendlink%
}
% Pros: Works. Concise. Very snappy under Acrobat Reader. Relevent sections of the spec are easier to understand.
% Cons: Preview completely ignores /CA 0 which means "set the ugly icon to be transparent plz!"
% Relevant pages in the spec:
% Section 12.5: Annotations (page 389)
% Section 12.5.6.4: Text Annotations (page 416)
% Attempt #3
% Obtained by blending attempt #2 with some code from the cooltips package:
%
% http://tug.ctan.org/cgi-bin/ctanPackageInformation.py?id=cooltooltips
%
\def\tooltiptarget{\phantom{\rule{1mm}{1mm}}}
\newbox\tempboxa
\setbox\tempboxa=\hbox{}
\immediate\pdfxform\tempboxa
\edef\emptyicon{\the\pdflastxform}
\newcommand\tooltip[1]{%
\leavevmode\pdfstartlink user{%
/Subtype /Text
/Contents (#1)
/AP <<
/N \emptyicon\space 0 R % Defines an empty icon for the tooltip
>>
/Open false
}%
\tooltiptarget%
\pdfendlink%
}
% Pros: Gets rid of icons in preview.
% Cons: Preview still has a large target area to the lower left of the point that should not be there.
% Relevant pages in the spec:
% Section 12.5: Annotations (page 389)
% Section 12.5.5: Appearance Streams (page 396)
% Section 12.5.6.4: Text Annotations (page 416)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment