The \abbr command

Hyperlinked abbreviations

The following command can be used to define commands for words that should be abbreviated in a document.

Code for the \abbr command
\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
The \abbr command is used to define a new abbreviation. It takes four mandatory arguments:
  • the abbreviation,
  • the full form of the abbreviation,
  • the abbreviation in the plural form, and
  • the full form of the abbreviation in the plural form.
As an example, the following code introduces the abbrevation ANN for artificial neural network:
Example of the \abbr command
\abbr{ANN}{artificial neural network}{ANNs}{artificial neural networks}
This defines two commands, \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:
Example of the \abbr command with optional arguments
\abbr{ANN}{artificial neural network}[ann]{artificial neural networks}{ANNabbr}[anns]  % Defines commands \ann and \anns
The defined commands also take an optional argument that can be used to override the output of the full form of the abbreviation (in case this is the first use of the command; in subsequent uses the optional argument is ignored). This can be useful, e.g., if the abbreviation appears at the start of a sentence and should be capitalized:
Example of the \abbr command with optional arguments
\ANNs[Artificial neural networks]  % Outputs "Artificial neural networks (ANNs)"

Unless otherwise noted, all LaTeX snippets on this site are released under the Zero-Clause BSD (0BSD) license. You may copy them into your documents without attribution or including any license. No warranty.