使用 Acro 和 Mouse-over 嵌套缩写

使用 Acro 和 Mouse-over 嵌套缩写

我来了克莱门斯来自的解决方案161521用于向 PDF 添加鼠标悬停效果。

它工作得很好,除了我得到嵌套的首字母缩略词时。作为 MWE [编辑:Acro 1.6b 中没有错误/警告 [编辑:使用 Acro 2.0,代码不再起作用。]]:

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

(对于这个例子,siunitx 或类似的东西可能会有帮助,但这不是这里的重点。)

PDF 文本看起来不错,但鼠标悬停时只显示最后一部分(gramm/byte)。[编辑:在 Linux 上使用 Adob​​e Reader 9.4.2 时,第一次调用时鼠标悬停在前缀上,尽管是小写。第二次调用时仍然没有。[编辑:Windows 7 上的 Adob​​e Acrobat Reader DC 也是如此。不知何故忽略了这一点。] 似乎波普勒几乎是 Linux 上唯一的替代方案。据我猜测不支持工具提示。

老实说,克莱门斯的代码让我难以理解,所以我只是按原样使用它。

答案1

从 2.1 版开始acro直接支持工具提示。它们可以通过两种方式激活:

  1. 只需设置选项\acsetup{tooltip=true}。这将加载pdfcomment包并用作\pdftooltip创建工具提示的命令。
  2. 将选项设置\acsetup{tooltip-cmd = <macro>}为您喜欢的工具提示创建宏。

作为一般规则,工具提示仅用于短的缩写形式,而长形式则从不。嵌套支持方式如下:禁用“内部”缩写的工具提示,并使用“外部”缩写的工具提示。

可以通过定义宏来直观地检查插入情况

\providecommand\tooltip[2]{[PDF:#1][tooltip:#2]}

并通过 使用它\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}

答案2

从 2.1 版开始,代码可以简化为

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

现在我得到了鼠标悬停(在 Windows 7 上的 Acrobat Reader 15.008.20082 中),但它们显示的是缩写形式(mg、kg、kB)。

它在非嵌套情况下有效(语法、字节)。

相关内容