Are mouse over definitions of acronyms possible?
Here is an example using the previously defined \tooltip
command. Some user macros from the Acro
package needed to be redefined to insert the tooltips only after first use:
For Acronyms in black and non-draggable tip boxes use
\def\ac#1{\acifused{#1}{\tooltip**[black]{\acs{#1}}{\acl{#1}}}{\acorig{#1}}\relax}
\def\acp#1{\acifused{#1}{\tooltip**[black]{\acsp{#1}}{\aclp{#1}}}{\acporig{#1}}\relax}
instead of
\def\ac#1{\acifused{#1}{\tooltip{\acs{#1}}{\acl{#1}}}{\acorig{#1}}\relax}
\def\acp#1{\acifused{#1}{\tooltip{\acsp{#1}}{\aclp{#1}}}{\acporig{#1}}\relax}
in the code below.
The complete code:
\documentclass[a6paper,12pt]{scrbook}
%\documentclass[12pt]{scrbook}
\usepackage{acro}
% some acronyms
\DeclareAcronym{cd}{ short={CD}, long={Compact Disc}, short-plural={s}, long-plural={s} }
\DeclareAcronym{mc}{ short={MC}, long={Musicassette}, short-plural={s}, long-plural={s} }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The re-defining of the acronym entering commands \ac for the normal form,
% \acp for the plural form of the acronym, \acl, \aclp
\let\acorig\ac
\let\acporig\acp
\def\ac#1{\acifused{#1}{\tooltip{\acs{#1}}{\acl{#1}}}{\acorig{#1}}\relax}
\def\acp#1{\acifused{#1}{\tooltip{\acsp{#1}}{\aclp{#1}}}{\acporig{#1}}\relax}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% tooltips with LaTeX v. 2018/01/15
%
% \tooltip[*[*[*[*]]]]
% [<link colour>]{<link text>}
% [<tip box colour>]{<tip text>}
% [<x-offset>,<y-offset>]
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \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,linegoal}
\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:
\let\cListSet\clist_set:Nn\let\cListItem\clist_item:Nn
\ExplSyntaxOff
\makeatletter
\NewDocumentCommand{\tooltip}{%
ssssO{\ifdefined\@linkcolor\@linkcolor\else blue\fi}mO{yellow!20}mO{0pt,0pt}%
}{{%
\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}%
}%
\cListSet\tpOffsets{#9}%
\edef\twd{\the\wd\tiptext}%
\edef\tht{\the\ht\tiptext}%
\edef\tdp{\the\dp\tiptext}%
\tipshift=0pt%
\IfBooleanTF{#2}{%
%OCG-based (that is, all non-draggable) boxes should not extend beyond the
%current column as they may get overlaid by text in the neighbouring column
\setlength\whatsleft{\linegoal}%
}{%
\measureremainder{\whatsleft}%
}%
\ifdim\whatsleft<\dimexpr\twd+\cListItem\tpOffsets{1}\relax%
\setlength\tipshift{\whatsleft-\twd-\cListItem\tpOffsets{1}}\fi%
\IfBooleanF{#2}{\tpPdfXform{\tiptext}}%
\raisebox{\heightof{#6}+\tdp+\cListItem\tpOffsets{2}}[0pt][0pt]{%
\makebox[0pt][l]{\hspace{\dimexpr\tipshift+\cListItem\tpOffsets{1}\relax}%
\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{document}
\acp{mc} and \acp{cd} are media for recording music. The \ac{mc} appeared about 15 years earlier than the \ac{cd}.
\end{document}
Update 2015-09-08:
As of version 2.1 acro
supports tooltips directly. They can be activated in two ways:
- simply set the option
\acsetup{tooltip=true}
. This will load thepdfcomment
package and use\pdftooltip
as command for creating tooltips. - set the option
\acsetup{tooltip-cmd = <macro>}
to a tooltip-creating macro of your liking.
As a general rule tooltips are only ever inserted for short forms of acronyms and never for the long form. Nesting is supported in the following way: tooltips of the “inner” acronyms are disabled and the one of the “outer” acronym is used.
The insertion can be checked visually by defining a macro
\providecommand\tooltip[2]{[PDF:#1][tooltip:#2]}
and use it via \acsetup{tooltip-cmd = \tooltip}
.
\documentclass{article}
\usepackage{acro}
\providecommand\tooltip[2]{[PDF:#1][tooltip:#2]}
\acsetup{
tooltip-cmd = \tooltip ,
}
\DeclareAcronym{m}{
short = {m},
long = {milli},
short-plural = {},
long-plural = {},
class = {SI-prefix},
tooltip = milli
}
\DeclareAcronym{g}{
short = {g},
long = {gramm},
short-plural = {},
long-plural = {},
class = {SI-unit-derived},
tooltip = gramm
}
\DeclareAcronym{mg}{
short = {\acs{m}\acs{g}},
long = {\Acl{m}\acl{g}},
short-plural = {},
long-plural = {},
class = {SI-unit-derived},
}
\begin{document}
\ac{g}\par
\ac{g}\par
\Ac{g}
\bigskip
\ac{m}\par
\acp{m}\par
\Acp{m}
\bigskip
\ac{mg}\par
\ac{mg}\par
\acf{mg}
\end{document}
Original answer – for versions up to 1.6:
The code below adds a tooltip
property to acronyms that allows to specify the hover text for an acronym if it should differ from the long form. If not specified the long form is used. The code takes care that the tooltips are inserted for the short forms and the alternative forms but not on first use and not in the list of acronyms. This is done by redefining the corresponding internal commands.
The code uses the pdfcomment
package and its \pdftooltip
macro for generating the tooltips as an example. AlexG's \tooltip
macro can just as easily be used, though, which means the integration into acro
is independent from a tooltip generating package/code. Below the macro that needs to be adjusted is shown.
(I'm thinking of adding something like this to acro
... but I'd like some feedback first. So far the feedback isn't overly enthusiastic...).
General usage
\documentclass{article}
\usepackage{pdfcomment}
\usepackage{acro}
\ExplSyntaxOn
...
\ExplSyntaxOff
% long form used as tooltip text:
\DeclareAcronym{cd}{
short = {CD},
long = {Compact Disc}
}
% custom tooltip text:
\DeclareAcronym{mc}{
short = {MC},
long = {Music Cassette},
tooltip = {my mouse-over text}
}
\begin{document}
\ac{cd} \ac{mc}\par\vspace{1cm}
\ac{cd} \ac{mc}
\end{document}
If another package/code for tooltips should be used then the definition in the code below
% #1: printed text
% #2: tool tip text
\cs_new_protected:Npn \acro_tooltip:nn #1#2
{ \pdftooltip {#1} {#2} }
must be adjusted accordingly.
Here's the complete code
\documentclass{article}
\usepackage{pdfcomment}
\usepackage{acro}
\ExplSyntaxOn
\prop_new:N \l__acro_tooltip_prop
\cs_new_protected:Npn \__acro_declare_tooltip:nn #1#2
{
\__acro_key_check:nn { #1 } { tooltip }
\prop_put:Nnn \l__acro_tooltip_prop { #1 } { #2 }
}
\cs_generate_variant:Nn \__acro_declare_tooltip:nn { V }
\keys_define:nn { acro / declare-acronym }
{
tooltip .code:n =
\__acro_declare_tooltip:Vn \l__acro_current_key_tl { #1 } ,
}
\cs_new_protected:Npn \acro_write_tooltip:nnn #1#2#3
{
\prop_get:NnNTF \l__acro_tooltip_prop { #1 } \l__acro_tmpc_tl
{ \acro_tooltip:nn { #2 } { \l__acro_tmpc_tl } }
{ \acro_tooltip:nn { #2 } { #3 } }
}
\cs_generate_variant:Nn \acro_write_tooltip:nnn { nnV }
\cs_set_protected:Npn \acro_use:n #1
{
% get the acronym and the plural settings:
\acro_get:n { #1 }
\acro_is_used:nTF { #1 }
{
% this is not the first time
\__acro_indefinite:nn { #1 } { short }
\__acro_acc_supp:nn
{ #1 }
{
\acro_write_tooltip:nnn
{ #1 }
{ \__acro_write_short:V \l__acro_short_tl }
{ \l__acro_long_tl }
}
\acro_after:n { #1 }
}
{
% this is the first time
\bool_gset_true:c { g__acro_#1_first_use_bool }
\__acro_is_single:nTF { #1 }
{
\__acro_indefinite:nn { #1 } { long }
\__acro_write_long:NV \l__acro_long_format_tl \l__acro_long_tl
}
{
\UseInstance { acro-first } { \l__acro_first_instance_tl }
{ #1 }
{ \l__acro_short_tl }
{ \l__acro_long_tl }
}
}
}
\cs_set_protected:Npn \acro_short:n #1
{
\acro_get:n { #1 }
\acro_is_used:n { #1 }
\__acro_indefinite:nn { #1 } { short }
\__acro_is_single:nT { #1 }
{ \cs_set_eq:NN \acro_hyper_link:nn \use_ii:nn }
\__acro_acc_supp:nn
{ #1 }
{
\acro_write_tooltip:nnn
{ #1 }
{ \__acro_write_short:V \l__acro_short_tl }
{ \l__acro_long_tl }
}
\acro_after:n { #1 }
}
\cs_set_protected:Npn \acro_alt:n #1
{
\acro_get:n { #1 }
\acro_is_used:n { #1 }
\__acro_indefinite:nn { #1 } { alt }
\__acro_is_single:nT { #1 }
{ \cs_set_eq:NN \acro_hyper_link:nn \use_ii:nn }
\__acro_acc_supp:nn
{ #1 }
{
\acro_write_tooltip:nnn
{ #1 }
{ \__acro_write_short:V \l__acro_alt_tl }
{ \l__acro_long_tl }
}
\acro_after:n { #1 }
}
% use whatever command you like here:
% #1: printed text
% #2: tool tip text
\cs_new_protected:Npn \acro_tooltip:nn #1#2
{ \pdftooltip {#1} {#2} }
\ExplSyntaxOff
\DeclareAcronym{cd}{
short = {CD},
long = {Compact Disc}
}
\DeclareAcronym{mc}{
short = {MC},
long = {Music Cassette},
tooltip = {my mouse-over text}
}
\begin{document}
\ac{cd} \ac{mc}\par\vspace{1cm}
\ac{cd} \ac{mc}
\aca{cd} \aca{mc}
\acs{cd} \acs{mc}
\end{document}
According to the Bitbucket issue tracker, for versions 2.0b or greater the code of clemens' answer reduces to:
\documentclass{article}
\usepackage{pdfcomment}
\usepackage{acro}
\ExplSyntaxOn
\acro_declare_simple_key:n {tooltip}
\cs_new_protected:Npn \acro_write_tooltip:nnn #1#2#3
{
\prop_get:NnNTF \l__acro_tooltip_prop {#1} \l__acro_tmpa_tl
{ \acro_tooltip:nV {#2} \l__acro_tmpa_tl }
{ \acro_tooltip:nn {#2} {#3} }
}
\cs_generate_variant:Nn \acro_write_tooltip:nnn { nnV }
% use whatever command you like for creating tooltips here:
% #1: printed text
% #2: tool tip text
\cs_new_protected:Npn \acro_tooltip:nn #1#2
{ \pdftooltip {#1} {#2} }
\cs_generate_variant:Nn \acro_tooltip:nn { nV }
\cs_set_protected:Npn \acro_write_compact:nn #1#2
{
\acro_get_ending_form:nn {#1} {#2}
\acro_acc_supp:nn
{#1}
{
\acro_write_tooltip:nnn
{#1}
{
\use:c {acro_write_#2:nv} {#1} {l__acro_#2_tl}
\acro_endings:nn {#1} {#2}
}
{ \l__acro_long_tl }
}
}
\ExplSyntaxOff
\DeclareAcronym{cd}{
short = {CD},
long = {Compact Disc}
}
\DeclareAcronym{mc}{
short = {MC},
long = {Music Cassette},
tooltip = {my mouse-over text}
}
\begin{document}
\ac{cd} \ac{mc}\par\vspace{1cm}
\ac{cd} \ac{mc}
\aca{cd} \aca{mc}
\acs{cd} \acs{mc}
\end{document}