词汇表:超链接到第一个条目,而不是首字母缩略词列表

词汇表:超链接到第一个条目,而不是首字母缩略词列表

是否可以将创建的超链接指向\gls{term}正文中第一次使用该术语的地方而不是词汇表?

我正在写一篇论文,其中使用了很多缩写,但我不想在文档中包含词汇表。相反,我想在第一次使用时定义每个缩写,但在所有后续使用该缩写时提供指向第一次使用的超链接。这可以吗?

以下是我当前用于设置glossarieshyperref包以及定义术语的代码示例:

\documentclass{report}
\usepackage[acronym,hyperfirst=false,nonumberlist,nowarn]{glossaries}
\usepackage[hidelinks]{hyperref}
...
\newacronym{FEA}{FEA}{Finite Element Analysis}
...
\begin{document}
    ...this includes a \gls{FEA} tool to...    % first use of term
    ...the \gls{FEA} tool...                   % subsequent uses of term, want to hyperlink to first use above within PDF
\end{document}

答案1

我建议你升级到最新版本glossaries(撰写本文时为 v4.01)并尝试以下操作:

\documentclass{report}

\usepackage[colorlinks]{hyperref}
\usepackage[acronym,nowarn,hyperfirst=false]{glossaries}

% Switch off hyperlinks for all uses of \gls etc.
% Hyperlinks will be inserted manually in the custom display style
\setkeys{glslink}{hyper=false}

\renewcommand*{\CustomAcronymFields}{%
  name={\the\glsshorttok},%
  description={\the\glslongtok},%
}

\renewcommand*{\SetCustomDisplayStyle}[1]{%
  \defglsentryfmt[#1]{%
    \ifdefempty\glscustomtext
    {%
      \ifglsused\glslabel
      {% subsequent use
       % Assuming all acronyms are written in upper case, so
       % not bother to check for case changes.
        \glsifplural
        {% subsequent use, plural
          \glshyperlink[\glsentryshortpl{\glslabel}]{\glslabel}%
        }%
        {% subsequent use, singular
          \glshyperlink[\glsentryshort{\glslabel}]{\glslabel}%
        }%
      }%
      {% first use
        \glsifplural
        {% first use, plural
          \glscapscase
          {% no case change
            \glstarget{\glslabel}{\glsentrylongpl{\glslabel}\glsinsert}%
            \space(\glsentryshortpl{\glslabel})%
          }%
          {% first letter upper case
            \glstarget{\glslabel}{\Glsentrylongpl{\glslabel}\glsinsert}%
            \space(\glsentryshortpl{\glslabel})%
          }%
          {% all caps
            \glstarget{\glslabel}{\MakeTextUppercase{%
              \glsentrylongpl{\glslabel}\glsinsert}}%
            \MakeTextUppercase{\space(\glsentryshortpl{\glslabel})}%
          }%
        }%
        {% first use, singular
          \glscapscase
          {% no case change
            \glstarget{\glslabel}{\glsentrylong{\glslabel}\glsinsert}%
            \space(\glsentryshort{\glslabel})%
          }%
          {% first letter upper case
            \glstarget{\glslabel}{\Glsentrylong{\glslabel}\glsinsert}%
            \space(\glsentryshort{\glslabel})%
          }%
          {% all caps
            \glstarget{\glslabel}{\MakeTextUppercase{%
              \glsentrylong{\glslabel}\glsinsert}}%
            \MakeTextUppercase{\space(\glsentryshort{\glslabel})}%
          }%
        }%
      }%
    }%
    {% \glsdisp used
      \ifglsused\glslabel
      {% subsequent use
        \glshyperlink[\glscustomtext]{\glslabel}%
      }%
      {% first use
        \glstarget{\glslabel}{\glscustomtext}%
      }%
    }%
  }%
}

\SetCustomStyle

\newacronym{FEA}{FEA}{Finite Element Analysis}

\begin{document}

First use: \gls{FEA}.

Next use: \gls{FEA}.

\end{document}

我使用了该colorlinks选项,以便您可以看到结果中的链接位置:

生成的文档的图像

链接目标是第一个用途。注意不要重置首字母缩略词(通过\glsreset等),否则您最终会得到多个定义的目标。

编辑:使用扩展包有一个更简单的方法glossaries-extra

\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage{glossaries-extra}

\setabbreviationstyle[acronym]{long-short}

\glssetcategoryattribute{acronym}{nohyperfirst}{true}

\renewcommand*{\glsdonohyperlink}[2]{%
 {\glsxtrprotectlinks \glsdohypertarget{#1}{#2}}}

\newacronym{FEA}{FEA}{Finite Element Analysis}

\begin{document}

First use: \gls{FEA}.

Next use: \gls{FEA}.

\end{document}

当超链接被抑制时,这将创建一个目标。如果您重置首次使用标志或想要使用带星号的版本,则可能会导致问题\gls*。稍作修改即可跟踪目标是否已设置:

\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage{glossaries-extra}

\setabbreviationstyle[acronym]{long-short}

\glssetcategoryattribute{acronym}{nohyperfirst}{true}

\renewcommand*{\glsdonohyperlink}[2]{%
 {\glsxtrprotectlinks 
   \edef\fieldvalue{\glsxtrusefield{\glslabel}{hastarget}}%
   \ifdefstring\fieldvalue{true}
   {%
     #2%
   }%
   {%
     \gGlsXtrSetField{\glslabel}{hastarget}{true}\glsdohypertarget{#1}{#2}%
   }%
 }%
}

\newacronym{FEA}{FEA}{Finite Element Analysis}

\begin{document}

First use: \gls{FEA}.

Next use: \gls{FEA}.

No hyperlink: \gls*{FEA}.

Another use: \gls{FEA}.

\end{document}

答案2

这不是一个答案,更多的是一个错误报告,但它不适合评论,抱歉......

\setacronymstyle在我的文档中,我在文本中多次使用,因此我可以将首字母缩略词 first use 输出从 long-short 更改为 short-long。请注意,使用时有一个非常微妙的错误@Nicola Talbot的答案是:

一旦使用\setacronymstyle,首先 .glsdefs 文件中的“名称”条目会发生变化:

$ grep name test.glsdefs
name={JAA1},%
name={\acronymfont {\glsentryshort {JAA2}}},%
name={\acronymfont {\glsentryshort {JAA3}}},%
name={\acronymfont {\glsentryshort {JAA4}}},%

...但这似乎不是什么大问题——事实上,使用该命令后,格式命令从包含和\gls@acronym@entryfmt的“独立”代码变为最终调用例如,这是类似的“独立”代码,但不使用超链接目标(显然是因为)。据我所知,在使用时尝试解决这个问题是可能的——也就是说,可以恢复超链接,但在这种情况下不会有任何效果(即长-短不会更改为短-长)。\glshyperlink\glstarget\GlsUseAcrEntryDispStyle {long-short}\glsgenacfmt\setkeys{glslink}{hyper=false}\setacronymstyle\setacronymstyle

编辑:实际上,修复方法似乎是在下面 MWE 中使用:

% \let\glsgenacfmt\oldglsgenacfmt % this allows that the short-long effect is back; but unfortunately there are no hyperlink targets in it
\def\glsgenacfmt{\glstarget{\glslabel}{\oldglsgenacfmt}} % this makes link targets work!

...也就是说,临时做出一个新定义,然后您将手动将其包装在链接目标中。

下面是一个证明这一点的 MWE(参见里面的评论):

\documentclass[a4paper]{book}

\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{hyperref}
\colorlet{mycolr}{red}
\hypersetup{colorlinks=true, citecolor=mycolr, filecolor=mycolr, linkcolor=mycolr, urlcolor=mycolr}

\usepackage[acronym,hyperfirst=false,nomain]{glossaries}
% nomain "This suppresses the creation of the main glossary and associated .glo file, if unrequired."
% hyperfirst "This is a boolean option that specifies whether each term has a hyperlink on first use."
% acronym "This creates a new glossary with the label acronym."
\setkeys{glslink}{hyper=true}
\setacronymstyle{long-short}
\let\oldglsgenacfmt\glsgenacfmt

% https://tex.stackexchange.com/questions/140666/glossaries-hyperlink-to-first-entry-not-list-of-acronyms
% Switch off hyperlinks for all uses of \gls etc.
% Hyperlinks will be inserted manually in the custom display style
\setkeys{glslink}{hyper=false}

\renewcommand*{\CustomAcronymFields}{%
  name={\the\glsshorttok},%
  description={\the\glslongtok},%
}

\renewcommand*{\SetCustomDisplayStyle}[1]{%
  \defglsentryfmt[#1]{%
    \ifdefempty\glscustomtext
    {%
      \ifglsused\glslabel
      {% subsequent use
       % Assuming all acronyms are written in upper case, so
       % not bother to check for case changes.
        \glsifplural
        {% subsequent use, plural
          \glshyperlink[\glsentryshortpl{\glslabel}]{\glslabel}%
        }%
        {% subsequent use, singular
          \glshyperlink[\glsentryshort{\glslabel}]{\glslabel}%
        }%
      }%
      {% first use
        \glsifplural
        {% first use, plural
          \glscapscase
          {% no case change
            \glstarget{\glslabel}{\glsentrylongpl{\glslabel}\glsinsert}%
            \space(\glsentryshortpl{\glslabel})%
          }%
          {% first letter upper case
            \glstarget{\glslabel}{\Glsentrylongpl{\glslabel}\glsinsert}%
            \space(\glsentryshortpl{\glslabel})%
          }%
          {% all caps
            \glstarget{\glslabel}{\MakeTextUppercase{%
              \glsentrylongpl{\glslabel}\glsinsert}}%
            \MakeTextUppercase{\space(\glsentryshortpl{\glslabel})}%
          }%
        }%
        {% first use, singular
          \glscapscase
          {% no case change
            \glstarget{\glslabel}{\glsentrylong{\glslabel}\glsinsert}%
            \space(\glsentryshort{\glslabel})%
          }%
          {% first letter upper case
            \glstarget{\glslabel}{\Glsentrylong{\glslabel}\glsinsert}%
            \space(\glsentryshort{\glslabel})%
          }%
          {% all caps
            \glstarget{\glslabel}{\MakeTextUppercase{%
              \glsentrylong{\glslabel}\glsinsert}}%
            \MakeTextUppercase{\space(\glsentryshort{\glslabel})}%
          }%
        }%
      }%
    }%
    {% \glsdisp used
      \ifglsused\glslabel
      {% subsequent use
        \glshyperlink[\glscustomtext]{\glslabel}%
      }%
      {% first use
        \glstarget{\glslabel}{\glscustomtext}%
      }%
    }%
  }%
}

\SetCustomStyle

\makeatletter
\def\printDefs#1{%
  \typeout{\space\space\space
    newacronym #1 is:
    \meaning\newacronym
  }%
  \typeout{\space\space\space
    \\newacronym #1 is:
    \expandafter\meaning\csname\string\newacronym\endcsname
  }%
  \typeout{\space\space\space
    gls #1 is:
    \meaning\gls
  }%
  \typeout{\space\space\space
    \\@gls #1 is:
    \expandafter\meaning\csname\string\@gls\endcsname
  }%
  \typeout{\space\space\space
    gls@acronym@entryfmt #1 is:
    \meaning\gls@acronym@entryfmt
  }%
  \typeout{\space\space\space
    glsgenacfmt #1 is:
    \meaning\glsgenacfmt
  }%
  \typeout{\space\space\space
    glsgenentryfmt #1 is:
    \meaning\glsgenentryfmt
  }%
  \typeout{\space\space\space}
}
\makeatother
% \GlsUseAcrEntryDispStyle: macro:#1->\csuse {@glsacr@dispstyle@#1}
% \@glsacr@dispstyle@long-short: macro:->\ifglshaslong {\glslabel }{\glsgenacfmt }{\glsgenentryfmt }
% default: gls@acronym@entryfmt

\begin{document}

\section{Testing}

% \let\oldnewacronym\newacronym% no can do; doesn't actually change (protected)
\makeatletter
\let\oldacronymentryfmt\gls@acronym@entryfmt
\makeatother

Testing first:
\newacronym{JAA1}{JAA1}{just another acronym One}%
\gls{JAA1}%
\printDefs{1}

\setacronymstyle{long-short} % this changes things!!! kills all the hyperlinks, even if there is a previously correct .glsdefs file (generated if this line is commented)!

Testing second:
\newacronym{JAA2}{JAA2}{just another acronym Two}%
\gls{JAA2}%
\printDefs{2}

% \let\oldglsgenacfmt\glsgenacfmt % now in preamble
\let\glsgenacfmt\oldacronymentryfmt % this restores all hyperlinks again, but the second one will be broken!
Testing third:
\newacronym{JAA3}{JAA3}{just another acronym Three}%
\gls{JAA3}%
\printDefs{3}

\setkeys{glslink}{hyper=true}% no effect
\setacronymstyle{short-long} % now does not kill links, but has no effect
\let\glsgenacfmt\oldglsgenacfmt % this allows that the short-long effect is back; but unfortunately there are no hyperlink targets in it
Testing fourth:
\newacronym{JAA4}{JAA4}{just another acronym Four}%
\gls{JAA4}%
\setkeys{glslink}{hyper=false}
\let\glsgenacfmt\oldacronymentryfmt % again this, to restore links for below commands


Testing refs: \gls{JAA1} and \gls{JAA2} and \gls{JAA3} and \gls{JAA4} % here 1 and 3 work, 2 and 4 are broken

\end{document}

相关内容