1

I came along Clemens’ solutions from 161521 for adding mouse over effects to a PDF.

It works pretty fine, except when I got nested acronyms. As a MWE [Edit: no errors/warnings in Acro 1.6b [Edit: With Acro 2.0, the code is not working anymore.]]:

\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{B}{
  short        = {B},
  long         = {byte},
  short-plural = {},
  long-plural  = {},
  class        = {non-SI-unit},
}
\DeclareAcronym{g}{
  short        = {g},
  long         = {gramm},
  short-plural = {},
  long-plural  = {},
  class        = {SI-unit-derived},
}
\DeclareAcronym{k}{
  short        = {k},
  long         = {kilo},
  short-plural = {},
  long-plural  = {},
  class        = {SI-prefix},
}
\DeclareAcronym{kB}{
  short        = {\acs{k}\acs{B}},
  long         = {\Acl{k}\acl{B}},
  short-plural = {},
  long-plural  = {},
  class        = {non-SI-unit-derived},
}
\DeclareAcronym{kg}{
  short        = {\acs{k}\acs{g}},
  long         = {\Acl{k}\acl{g}},
  short-plural = {},
  long-plural  = {},
  class        = {SI-unit},
}
\DeclareAcronym{m}{
  short        = {m},
  long         = {milli},
  short-plural = {},
  long-plural  = {},
  class        = {SI-prefix},
}
\DeclareAcronym{mg}{
  short        = {\acs{m}\acs{g}},
  long         = {\Acl{m}\acl{g}},
  short-plural = {},
  long-plural  = {},
  class        = {SI-unit-derived},
}


\begin{document}

1~\ac{mg}
1~\ac{kg}
1~\ac{kB}

2~\acp{mg}
2~\acp{kg}
2~\acp{kB}

\end{document}

(For this example siunitx or the like might help, but that is not the point here.)

The text of the PDF looks fine, but the mouse over shows only the last part (gramm/byte). [Edit: with Adobe Reader 9.4.2 on Linux there are actually mouse overs for the prefixes in the first call, albeit lower case. Still none in the second call. [Edit: Same for Adobe Acrobat Reader DC on Windows 7. Somehow overlooked this.] Seems like Poppler is almost the only alternative on Linux. No support for tooltips as far as I guess.]

Honestly, Clemens’ code is overwhelming me, so I just use it as is.

3

As of version 2.1 acro supports tooltips directly. They can be activated in two ways:

  1. simply set the option \acsetup{tooltip=true}. This will load the pdfcomment package and use \pdftooltip as command for creating tooltips.
  2. 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}.

enter image description here

\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}
1

As of version 2.1, code can be reduced to

\documentclass{article}

\usepackage[tooltip]{acro}

\DeclareAcronym{B}{
  short        = {B},
  long         = {byte},
  short-plural = {},
  long-plural  = {},
  class        = {non-SI-unit},
}
\DeclareAcronym{g}{
  short        = {g},
  long         = {gramm},
  short-plural = {},
  long-plural  = {},
  class        = {SI-unit-derived},
}
\DeclareAcronym{k}{
  short        = {k},
  long         = {kilo},
  short-plural = {},
  long-plural  = {},
  class        = {SI-prefix},
}
\DeclareAcronym{kB}{
  short        = {\acs{k}\acs{B}},
  long         = {\Acl{k}\acl{B}},
  short-plural = {},
  long-plural  = {},
  class        = {non-SI-unit-derived},
}
\DeclareAcronym{kg}{
  short        = {\acs{k}\acs{g}},
  long         = {\Acl{k}\acl{g}},
  short-plural = {},
  long-plural  = {},
  class        = {SI-unit},
}
\DeclareAcronym{m}{
  short        = {m},
  long         = {milli},
  short-plural = {},
  long-plural  = {},
  class        = {SI-prefix},
}
\DeclareAcronym{mg}{
  short        = {\acs{m}\acs{g}},
  long         = {\Acl{m}\acl{g}},
  short-plural = {},
  long-plural  = {},
  class        = {SI-unit-derived},
}


\begin{document}

 1~\Ac{g}
 1~\Ac{B}

 1~\ac{mg}
 1~\ac{kg}
 1~\ac{kB}

 2~\Acp{g}
 2~\Acp{B}

 2~\acp{mg}
 2~\acp{kg}
 2~\acp{kB}

\end{document}

Now I get mouse-overs (in Acrobat Reader 15.008.20082 on Windows 7), but they show the short-form (mg, kg, kB).

It works in the non-nested case (gramm, byte).

  • You still seem to have mutliple accounts… – clemens Sep 8 '15 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.