The \abbr command
Hyperlinked abbreviations
The following command can be used to define commands for words that should be abbreviated in a document.
\makeatletter
\ExplSyntaxOn
\seq_new:N \g_abbrs
\prop_new:N \g_abbr_counts
\tl_new:N \l_abbr_count_tl
\NewDocumentCommand{\abbr}{m m O{#1} m m O{#4}}{
\expandafter\newcommand\csname#3\endcsname[1][]{
\seq_if_in:NnTF \g_abbrs {#1} {
\prop_get:NnN \g_abbr_counts {#1} \l_abbr_count_tl
\prop_gput:Nnx \g_abbr_counts {#1} {\int_eval:n {\l_abbr_count_tl + 1}}
\hyperref[#1]{#1}
} {
\seq_gput_left:Nn \g_abbrs {#1}
\prop_gput:Nnn \g_abbr_counts {#1} {1}
\expandafter\gdef\csname#1@def\endcsname{#2}
\phantomsection\label{#1}
\str_if_eq:nnTF{##1}{}{\emph{#2}}{##1}~(\hyperref[#1]{#1})
}
}
\expandafter\newcommand\csname#6\endcsname[1][]{
\seq_if_in:NnTF \g_abbrs {#1} {
\prop_get:NnN \g_abbr_counts {#1} \l_abbr_count_tl
\prop_gput:Nnx \g_abbr_counts {#1} {\int_eval:n {\l_abbr_count_tl + 1}}
\hyperref[#1]{#4}
} {
\expandafter\gdef\csname#1@def\endcsname{#5}
\seq_gput_left:Nn \g_abbrs {#1}
\prop_gput:Nnn \g_abbr_counts {#1} {1}
\phantomsection\label{#1}
\str_if_eq:nnTF{##1}{}{\emph{#5}}{##1}~(\hyperref[#1]{#4})
}
}
}
\ExplSyntaxOff
\makeatother\abbr command is used to define a new abbreviation. It takes four mandatory arguments:
\abbr{ANN}{artificial neural network}{ANNs}{artificial neural networks}\ANN and \ANNs. The first time either of
these two commands is used, it will output the full form of the abbreviation with the abbreviation
in parentheses. Subsequent uses of the command will only output the abbreviation with a hyperlink
to the first use of the command.
The \abbr command also takes two optional arguments that specify the
name of the commands, in case the default of having them be the same as the
abbreviation is not desired:
\abbr{ANN}{artificial neural network}[ann]{artificial neural networks}{ANNabbr}[anns] % Defines commands \ann and \anns\ANNs[Artificial neural networks] % Outputs "Artificial neural networks (ANNs)"