% \iffalse meta-comment
%
% Copyright (C) 2019-2025 by Jan Hajer
% -----------------------------------
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in:
%
% http://www.latex-project.org/lppl.txt
%
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% \fi
%
% \iffalse
%
%<package>\NeedsTeXFormat{LaTeX2e}[2021-11-15]
%<package>\ProvidesPackage{hep-acronym}[2026/08/01 v1.5 An acronym extension for glossaries]
%<documentation>\ProvidesFile{hep-acronym-documentation.tex}[2026/08/01 v1.5 HEP-acronym documentation]
%
%<*documentation>
\RequirePackage[l2tabu, orthodox]{nag}
\documentclass{ltxdoc}

\renewcommand*\theCodelineNo{\rmfamily\tstyle\footnotesize\arabic{CodelineNo}}
\AtBeginEnvironment{macrocode}{\renewcommand*{\ttdefault}{clmt}}
\renewcommand*{\MacroFont}{\codestyle}
\AtBeginDocument{\DeleteShortVerb{\|}}
\AtBeginDocument{\MakeShortVerb{\"}}
\EnableCrossrefs
\CodelineIndex
\RecordChanges

\usepackage{hologo}

\usepackage[parskip,oldstyle,font=10pt]{hep-paper}
\bibliography{bibliography}
\acronym{PDF}{portable document format}
%</documentation>

%<*driver>
\expandafter\newif\csname ifshort\endcsname
\shortfalse
\begin{document}
\DocInput{hep-acronym-implementation.dtx}
\end{document}
%</driver>
%
% \fi
%
% \CheckSum{627}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \changes{v1.0}{2021/09/01}{Created the dedicated acronym package with glossaries-extra based acronym declarations, sentence-aware use, short and long variants, reset and dummy helpers, and acronym groups.}
% \changes{v1.1}{2022/11/01}{Added \cs{sentence}, forced long-form acronym commands, conditional PDF-string disabling without hyperref, safer option passing, and title-case tests.}
% \changes{v1.2}{2023/07/01}{Added the \option{hyper} option, alternative long-form acronyms, math-mode acronym atom wrapping, and refreshed documentation and README formatting.}
% \changes{v1.3}{2024/11/01}{Renamed the sentence-state helper, applied starred command definitions, corrected release dates, and disabled the table/list acronym reset patch.}
% \changes{v1.4}{2025/09/01}{Updated release metadata.}
% \changes{v1.5}{2026/08/01}{Raised the LaTeX format requirement, bumped release metadata, and expanded the abstract, user documentation, README, and examples.}
%
% \ifshort
%<*documentation>
% \fi
%
\GetFileInfo{hep-acronym.sty}

\title{The \software{hep-acronym} package\thanks{This document corresponds to \software{hep-acronym}~\fileversion.}}
\subtitle{An acronym extension for glossaries}
\author{Jan Hajer \email{jan.hajer@tecnico.ulisboa.pt}}
\date{\filedate}

% \ifshort
\begin{document}
% \fi

\newgeometry{vscale=.8, vmarginratio=3:4, includeheadfoot, left=11em, marginparwidth=4.6cm, marginparsep=3mm, right=7em}

\maketitle

\begin{abstract}
The \software{hep-acronym} package provides sentence-aware acronym helpers based on \software{glossaries-extra}.
It defines declaration commands for regular, always-short, always-long, and alternative long-form acronyms, optional links back to the first use, reset and dummy-use helpers, and support for several acronym groups.
\end{abstract}

Acronyms are implemented using the \software{glossaries-extra} package \cite{glossaries-extra} which is an extension of the \software{glossaries} package \cite{glossaries} and must be loaded after the \software{hyperref} package \cite{hyperref}.
It can be loaded using "\usepackage{hep-acronym}".

\DescribeMacro{hyper}
The "hyper" option controls whether later acronym uses link back to their first appearance.
It is enabled by default and can be deactivated with "hyper=false".

\DescribeMacro{\acronym}
The "\acronym"\meta{*}\oarg{typeset abbreviation}\marg{abbreviation}\meta{*}\marg{definition}\oarg{plural definition} macro generates the singular "\"\meta{abbreviation} and plural "\"\meta{abbreviation}"s" macros.

The first star prevents the addition of an \enquote{s} to the abbreviation plural.
The second star restores the \hologo{TeX} default of swallowing subsequent white space.
The long form is only shown at the first appearance of these macros, later appearances generate the abbreviation with a hyperlink to the long form.
Capitalisation at the beginning of paragraphs and sentences is (mostly) ensured.
The long form is never used in math mode which can be exploited to enforce the short form.
In order to enforce the long form use "\"\meta{abbreviation}"long".
As there can be no fixed rule whether to use the long form or the short form in section headers the user is left to their own devices \eg "\glsdesc"\marg{abbreviation}, "\Glsdesc"\marg{abbreviation}.
\DescribeMacro{\sentence}
The "\sentence" macro ensures that the directly following abbreviation is capitalised.


\DescribeMacro{\shortacronym}
\DescribeMacro{\longacronym}
The "\shortacronym" and "\longacronym" macros are drop-in replacements of the "\acronym" macro showing only the short or long form of their acronym.

\DescribeMacro{\acronymalternative}
The "\acronymalternative"\marg{acronym}\marg{name}\meta{*}\marg{alternative long form} macro defines an additional acronym macro that shares the use counter and short form of \meta{acronym} but prints the supplied long form at first use.

\DescribeMacro{\resetacronym}
The first use form of the acronym can be enforced by resetting the acronym counter with "\resetacronym"\marg{key}.

\DescribeMacro{\dummyacronym}
If the acronym counter equals one at the end of the document the short form of the acronym is not introduced.
Placing a "\dummyacronym"\marg{key} at the end of the document ensures that the short form is introduced.

\DescribeMacro{\acronyms}
The "\acronyms"\oarg{key}\marg{title} macro creates a new acronym glossary and switches subsequent declarations to it.
Without the optional argument the mandatory argument is used both as glossary key and printed title.

\DescribeMacro{warning}
In order to reduce the number of potentially conflicting packages the \software{glossaries} package is loaded without any glossary style.
In the case that the glossary should be printed additional packages must be loaded via \eg "\usepackage{glossary-list}".

% \ifshort
\printbibliography

\end{document}
%
%</documentation>
% \fi
%
% \StopEventually{
% \printbibliography
% \PrintChanges
% }
%
% \appendix
%
% \section{Implementation}
%
%<*package>
%
% Define a hepacronym namespace for the options using the \software{kvoptions} package \cite{kvoptions}.
%    \begin{macrocode}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{
  family=hepacronym,
  prefix=hepacronym@
}
%    \end{macrocode}
%
% \begin{macro}{hyper}
% Define the "hyper" option controlling the hyperlink to the first appearance of the acronyms.
%    \begin{macrocode}
\DeclareBoolOption[true]{hyper}
%    \end{macrocode}
% \end{macro}
%
% Process options.
%    \begin{macrocode}
\ProcessKeyvalOptions*
%    \end{macrocode}
%
% Load the base package \cite{glossaries, glossaries-extra} and set the abbreviation style.
%    \begin{macrocode}
\PassOptionsToPackage{nostyles}{glossaries-extra}
\RequirePackage{glossaries-extra}
% \setabbreviationstyle{long-hyphen-short-hyphen}
%    \end{macrocode}
% Use the entry count feature.
%    \begin{macrocode}
\glsenableentrycount
\glssetcategoryattribute{abbreviation}{entrycount}{1}
%    \end{macrocode}
% Provide macros for older "glossaries-extra" installations.
%    \begin{macrocode}
\AtBeginDocument{
  \@ifpackageloaded{hyperref}{
    \providecommand*{\glsxtrusefield}[2]{\@gls@entry@field{#1}{#2}}
    \providecommand*{\glsxtrsetfieldifexists}[3]{\glsdoifexists{#1}{#3}}
    \providecommand*{\gGlsXtrSetField}[3]{%
      \glsxtrsetfieldifexists{#1}{#2}{%
        \csgdef{glo@\glsdetoklabel{#1}@#2}{#3}%
      }%
    }
%    \end{macrocode}
% Set hyperlinks from the abbreviation to their definition in the text.
%    \begin{macrocode}
    \glssetcategoryattribute{abbreviation}{nohyperfirst}{true}
    \renewcommand*{\glsdonohyperlink}[2]{{%
      \glsxtrprotectlinks\edef\fieldvalue{%
        \glsxtrusefield{\glslabel}{hastarget}%
      }%
      \ifdefstring\fieldvalue{true}{#2}{%
        \gGlsXtrSetField{\glslabel}{hastarget}{true}%
        \glsdohypertarget{#1}{#2}%
      }%
    }}
  }{}
}
%    \end{macrocode}
% Provide for the case that hyperref is not loaded.
%    \begin{macrocode}
\newcommand*{\hep@disable}[1]{%
  \@ifpackageloaded{hyperref}{\pdfstringdefDisableCommands{#1}}{}%
}
% Ignore only hyperref's "math shift" PDF-string warning.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{hyperref}{%
    \let\hep@HyPsd@CatcodeWarning\HyPsd@CatcodeWarning
    \renewcommand*{\HyPsd@CatcodeWarning}[1]{%
      \ifstrequal{#1}{math shift}{}{%
        \hep@HyPsd@CatcodeWarning{#1}%
      }%
    }%
  }{}%
}
%    \end{macrocode}
% Deactivate hyperrefs if requested.
%    \begin{macrocode}
\ifhepacronym@hyper\else
  \glsdisablehyper
\fi
%    \end{macrocode}
%
% Load the \software{titlesec} package \cite{titlesec} in order to ensure that section headers are treated as sentences.
%    \begin{macrocode}
\RequirePackage{titlesec}
%    \end{macrocode}
%
% \begin{macro}{\begin@sentence}
% Mark the beginning of a paragraph as if it would follow a full stop using the \software{everyhook} package \cite{everyhook}.
%    \begin{macrocode}
\PassOptionsToPackage{excludeor}{everyhook}
\RequirePackage{everyhook}
\newcommand*{\begin@sentence}{1001}
\newcommand*{\sentence}{\spacefactor=\begin@sentence}
\hep@disable{\renewcommand*\sentence{}}
\PushPostHook{par}{{\sentence}}
\gdef\ttl@case{\leavevmode\sentence}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\frenchspacing}
% Adjust the "\frenchspacing" macro to be compatibel with this idea.
%    \begin{macrocode}
\def\frenchspacing{%
  \sfcode`\.\begin@sentence \sfcode`\?\begin@sentence
  \sfcode`\!\begin@sentence \sfcode`\:\begin@sentence
  \sfcode`\;\@m \sfcode`\,\@m
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hep@if@begin@of@sentence}
% Provide a macro checking for the beginning of a sentence by examining the length of the preceeding space.
%    \begin{macrocode}
\newcommand*{\hep@if@begin@of@sentence}[2]{\leavevmode\protecting{%
  \ifboolexpr{ test {\ifnumcomp{\spacefactor}{=}{3000}} or%
               test {\ifnumcomp{\spacefactor}{=}{2000}} or%
               test {\ifnumcomp{\spacefactor}{=}{\begin@sentence}}%
  }{#1}{#2}%
}}
%    \end{macrocode}
% \end{macro}
%
% Fix the table of contents.
%    \begin{macrocode}
\let\hep@table@of@contents\tableofcontents
\renewcommand*{\tableofcontents}{%
  \begingroup
    \glsunsetall
    \let\hep@contents@line\contentsline
    \let\hep@number@line\numberline
    \renewcommand*{\contentsline}[4]{%
      \hep@contents@line{##1}{\sentence ##2}{##3}{##4}%
    }%
    \renewcommand*{\numberline}[1]{\hep@number@line{##1}\sentence}%
    \hep@table@of@contents
  \endgroup
  \glsresetall
}
%    \end{macrocode}
%
% \begin{macro}{\hep@item@sentence}
% \begin{macro}{\descriptionsitem}
% Ensure that acronyms in description items get capitalised using the \software{enumitem} package \cite{enumitem}.
%    \begin{macrocode}
\RequirePackage{enumitem}
\newcommand*\hep@item@sentence{\leavevmode\sentence}
\newcommand*\descriptionsitem{%
  \let\hep@descripton@item\item
  \RenewDocumentCommand{\item}{o}{%
    \IfNoValueTF{##1}{%
      \hep@descripton@item\hep@item@sentence
    }{%
      \hep@descripton@item[\hep@item@sentence##1]\hep@item@sentence
    }%
  }%
}
\setlist[description]{before=\descriptionsitem}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mathdef}
% Provide a macro that allows to extend a macro definition into math mode using the \software{xparse} package \cite{xparse}.
%    \begin{macrocode}
\RequirePackage{xparse}
\ProvideDocumentCommand{\mathdef}{mO{0}m}{%
  \expandafter\let\csname hep@text\string#1\endcsname=#1
  \expandafter\newcommand\expandafter*%
    \csname hep@math\string#1\endcsname[#2]{#3}
  \DeclareRobustCommand#1{%
    \ifmmode
      \expandafter\let\expandafter\next\csname%
      hep@math\string#1\endcsname%
    \else
      \expandafter\let\expandafter\next\csname%
      hep@text\string#1\endcsname%
    \fi
    \next
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\acronym}
% Define the "\acronym"\meta{*}\oarg{typeset abbreviation}\marg{abbreviation}\meta{*}\marg{definition}\oarg{plural\linebreak[4] definition} macro using the \software{xspace} \cite{xspace} and \software{amstext} \cite{amstext} packages.
% \begin{enumerate}[nosep, label=\#\arabic*]
% \item star for omitting the \enquote{s} in the short plural
% \item optional typeset abbreviation
% \item mandatory abbreviation
% \item star for restoring the \hologo{TeX} default for space after text macros
% \item mandatory long form
% \item optional plural long form
% \end{enumerate}
%    \begin{macrocode}
\RequirePackage{xspace}
\RequirePackage{amstext}
\NewDocumentCommand{\acronym}{somsmo}{
  \newabbreviation[
    type=\acronymtype,
    sort=#3,
    \glsshortpluralkey=\IfBooleanTF{#1}{#3}{\IfNoValueTF{#2}{#3s}{#2s}},
    longplural=\IfNoValueTF{#6}{#5s}{#6}
  ]{#3}{\IfNoValueTF{#2}{#3}{#2}}{#5}
%    \end{macrocode}
% Provide the singular acronym macro.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3\endcsname[1][]{%
    \hep@if@begin@of@sentence{%
      \ifglsused{#3}{\cgls{#3}[##1]}{\cGls{#3}[##1]}%
    }{\cgls{#3}[##1]}%
    \ifnum\glsentrycurrcount{#3}>1\relax
      \IfBooleanTF{#4}{}{\@\xspace}%
    \else\@\xspace\fi
  }
%    \end{macrocode}
% Expand the singular acronym macro in \PDF labels.
%    \begin{macrocode}
  \hep@disable{\expandafter\def\csname#3\endcsname{%
    \IfNoValueTF{#2}{#3}{#2} }%
  }
%    \end{macrocode}
% Provide the singular acronym macro in math mode.
%    \begin{macrocode}
  \expandafter\mathdef\csname#3\endcsname{%
    \mathinner{\text{\glsxtrshort{#3}}}\@gls@increment@currcount{#3}%
  }
%    \end{macrocode}
% Provide the plural acronym macro.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3s\endcsname[1][]{%
    \hep@if@begin@of@sentence{\cGlspl{#3}[##1]}{\cglspl{#3}[##1]}%
    \IfBooleanTF{#4}{}{\@\xspace}%
  }
%    \end{macrocode}
% Expand the plural acronym macro in \PDF labels.
%    \begin{macrocode}
  \hep@disable{\expandafter\def\csname#3s\endcsname{%
    \IfBooleanTF{#1}{#3}{\IfNoValueTF{#2}{#3s}{#2s}} }%
  }
%    \end{macrocode}
% Provide the plural acronym macro in math mode.
%    \begin{macrocode}
  \expandafter\mathdef\csname#3s\endcsname{%
    \mathinner{\text{\glsxtrshortpl{#3}}}\@gls@increment@currcount{#3}%
  }
%    \end{macrocode}
% Provide an enforced long form.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3long\endcsname[1][]{%
    \hep@if@begin@of@sentence{\Glsdesc*{#3}[##1]}{\glsdesc*{#3}[##1]}%
    \IfBooleanTF{#4}{}{\@\xspace}%
  }
%    \end{macrocode}
% Ensure that it works in \PDFs.
%    \begin{macrocode}
  \hep@disable{\expandafter\def\csname#3long\endcsname{#5 }}
%    \end{macrocode}
% Provide an enforced long plural form.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3slong\endcsname[1][]{%
    \hep@if@begin@of@sentence{%
      \Glsxtrlongpl*{#3}[##1]}{\glsxtrlongpl*{#3}[##1]%
    }%
    \IfBooleanTF{#4}{}{\@\xspace}%
  }
%    \end{macrocode}
% Ensure that it works in \PDFs.
%    \begin{macrocode}
  \hep@disable{\expandafter\def\csname#3slong\endcsname{%
    \IfNoValueTF{#6}{#5s}{#6} }%
  }
%    \end{macrocode}
% Provide an enforced short form.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3short\endcsname[1][]{%
    \glsxtrshort{#3}\@gls@increment@currcount{#3}%
    \IfBooleanTF{#4}{}{\@\xspace}%
  }
%    \end{macrocode}
% Provide an enforced first form.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3first\endcsname[1][]{%
    \glsreset{#3}
    \hep@if@begin@of@sentence{%
      \ifglsused{#3}{\cgls{#3}[##1]}{\cGls{#3}[##1]}%
    }{\cgls{#3}[##1]}%
    \ifnum\glsentrycurrcount{#3}>1\relax
      \IfBooleanTF{#4}{}{\@\xspace}%
    \else\@\xspace\fi
  }
%    \end{macrocode}
% End of "\acronym".
%    \begin{macrocode}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\shortacronym}
% The "\shortacronym" never expands into the long form.
%    \begin{macrocode}
\NewDocumentCommand{\shortacronym}{somsmo}{
%    \end{macrocode}
% Provide the singular acronym macro.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3\endcsname[1][]{%
    \IfNoValueTF{#2}{#3}{#2}\IfBooleanTF{#4}{}{\@\xspace}##1%
  }
%    \end{macrocode}
% Expand the singular acronym macro in \PDF labels.
%    \begin{macrocode}
  \hep@disable{\expandafter\def\csname#3\endcsname{%
    \IfNoValueTF{#2}{#3}{#2} }%
  }
%    \end{macrocode}
% Provide the singular acronym macro in math mode.
%    \begin{macrocode}
  \expandafter\mathdef\csname#3\endcsname{%
    \mathinner{\text{\IfNoValueTF{#2}{#3}{#2}}}%
  }
%    \end{macrocode}
% Provide the plural acronym macro.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3s\endcsname[1][]{%
    \IfBooleanTF{#1}{#3}{\IfNoValueTF{#2}{#3s}{#2s}}%
    \IfBooleanTF{#4}{}{\@\xspace}##1%
  }
%    \end{macrocode}
% Expand the plural acronym macro in \PDF labels.
%    \begin{macrocode}
  \hep@disable{\expandafter\def\csname#3s\endcsname{%
    \IfBooleanTF{#1}{#3}{\IfNoValueTF{#2}{#3s}{#2s}} }%
  }
%    \end{macrocode}
% Provide the plural acronym macro in math mode.
%    \begin{macrocode}
  \expandafter\mathdef\csname#3s\endcsname{%
    \mathinner{\text{\IfBooleanTF{#1}{#3}{\IfNoValueTF{#2}{#3s}{#2s}}}}%
  }%
%    \end{macrocode}
% Ensure that long form also exists.
%    \begin{macrocode}
  \expandafter\let\csname#3long\expandafter\endcsname\csname#3\endcsname
  \expandafter\let\csname#3slong\expandafter\endcsname\csname#3s\endcsname
%    \end{macrocode}
% End of "\shoracronym".
%    \begin{macrocode}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\longacronym}
% The "\longacronym" never shows the abbreviated form.
%    \begin{macrocode}
\NewDocumentCommand{\longacronym}{somsmo}{
%    \end{macrocode}
% Provide the singular acronym macro.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3\endcsname[1][]{%
    \hep@if@begin@of@sentence{\MakeUppercase#5}{#5}%
    \IfBooleanTF{#4}{}{\@\xspace}##1%
  }
%    \end{macrocode}
% Expand the singular acronym macro in \PDF labels.
%    \begin{macrocode}
  \hep@disable{\expandafter\def\csname#3\endcsname{#5 }}
%    \end{macrocode}
% Provide the plural acronym macro.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#3s\endcsname[1][]{%
    \hep@if@begin@of@sentence{%
      \IfNoValueTF{#6}{\MakeUppercase#5s}{\MakeUppercase#6}%
    }{%
      \IfNoValueTF{#6}{#5s}{#6}}\IfBooleanTF{#4}{}{\@\xspace}##1%
  }
%    \end{macrocode}
% Expand the plural acronym macro in \PDF labels.
%    \begin{macrocode}
  \hep@disable{\expandafter\def\csname#3s\endcsname{%
    \IfNoValueTF{#6}{#5s}{#6} }%
  }
%    \end{macrocode}
% Ensure that long form also exists.
%    \begin{macrocode}
  \expandafter\let\csname#3long\expandafter\endcsname\csname#3\endcsname
  \expandafter\let\csname#3slong\expandafter\endcsname\csname#3s\endcsname
%    \end{macrocode}
% End of "\longacronym".
%    \begin{macrocode}
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\acronymalternative}
% Allow for alternative long text in abbreviation.
%    \begin{macrocode}
\NewDocumentCommand{\acronymalternative}{mmsm}{%
%    \end{macrocode}
% Store it in the "useri" field.
%    \begin{macrocode}
  \GlsXtrSetField{#1}{useri}{#4}%
%    \end{macrocode}
% Define the acronym
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#2\endcsname[1][]{%
%    \end{macrocode}
% Redirect the relevant macros to the useri field.
%    \begin{macrocode}
  \let\hep@glsaccesslong\glsaccesslong%
  \let\hep@Glsaccesslong\Glsaccesslong%
  \let\glsaccesslong\glsuseri%
  \let\Glsaccesslong\Glsuseri%
%    \end{macrocode}
% Execute abbreviation macros.
%    \begin{macrocode}
    \hep@if@begin@of@sentence{%
      \ifglsused{#1}{\cgls{#1}[##1]}{\cGls{#1}[##1]}%
    }{\cgls{#1}[##1]}%
%    \end{macrocode}
% Undo the redirection.
%    \begin{macrocode}
  \let\glsaccesslong\hep@glsaccesslong%
  \let\Glsaccesslong\hep@Glsaccesslong%
%    \end{macrocode}
% Spacing code.
%    \begin{macrocode}
    \ifnum\glsentrycurrcount{#1}>1\relax%
      \IfBooleanTF{#3}{}{\@\xspace}%
    \else\@\xspace\fi%
  }
%    \end{macrocode}
% Redirect \PDF code to original acronym.
%    \begin{macrocode}
  \hep@disable{\expandafter\def\csname#2\endcsname{%
    \csname#1\endcsname }%
  }
%    \end{macrocode}
% Redirect math macro to original acronym.
%    \begin{macrocode}
  \expandafter\mathdef\csname#2\endcsname{%
    \mathinner{\text{\glsxtrshort{#1}}}\@gls@increment@currcount{#1}%
  }
%    \end{macrocode}
% Provie long version of acronym.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter*\csname#2long\endcsname[1][]{%
%    \end{macrocode}
% Redirect relevant macros.
%    \begin{macrocode}
    \let\hep@glsentrydesc\glsentrydesc%
    \let\hep@Glsentrydesc\Glsentrydesc%
    \renewrobustcmd*{\glsentrydesc}[1]{\@gls@entry@field{#1}{useri}}%
    \renewrobustcmd*{\Glsentrydesc}[1]{\@Gls@entry@field{#1}{useri}}%
%    \end{macrocode}
% Execute relevant macro.
%    \begin{macrocode}
    \hep@if@begin@of@sentence{\Glsdesc*{#1}[##1]}{\glsdesc*{#1}[##1]}%
    \IfBooleanTF{#3}{}{\@\xspace}%
%    \end{macrocode}
% Undo redefinition.
%    \begin{macrocode}
    \let\glsentrydesc\hep@glsentrydesc%
    \let\Glsentrydesc\hep@Glsentrydesc%
  }
%    \end{macrocode}
% Provie \PDF code.
%    \begin{macrocode}
  \hep@disable{%
    \expandafter\def\csname#2long\endcsname{%
      \@Gls@entry@field{#1}{useri} %
    }%
  }%
}
%    \end{macrocode}
% \end{macro}
% Silence warning if no acronyms are defined.
%    \begin{macrocode}
\renewcommand*{\@gls@write@entrycounts}{%
  \immediate\write\@auxout{%
    \string\providecommand*{\string\@gls@entry@count}[2]{}
  }%
  \count@=0\relax
  \forallglsentries{\@glsentry}{%
    \glshasattribute{\@glsentry}{entrycount}{%
      \ifglsused{\@glsentry}{%
        \immediate\write\@auxout{%
          \string\@gls@entry@count{\@glsentry}{%
            \glsentrycurrcount{\@glsentry}%
          }
        }%
      }{}\advance\count@ by \@ne
    }{}%
  }%
}
%    \end{macrocode}
% \begin{macro}{\resetacronym}
% \begin{macro}{\dummyacronym}
% Add two macros for acronym management.
%    \begin{macrocode}
\newcommand*{\resetacronym}[1]{\protect\glsreset{#1}}
\newcommand*{\dummyacronym}[1]{\protect\glsunset{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{abstract}
% Adjust the "abstract" environment to reset all acronym counters.
%    \begin{macrocode}
\@ifundefined{endabstract}{}{%
  \let\end@hep@abstract\endabstract%
  \renewcommand*\endabstract{\glsresetall\end@hep@abstract}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tableofcontents}
% \begin{macro}{\listoffigures}
% \begin{macro}{\listoftables}
% Adjust the "\tableofcontents" macro to never show the long form of acronyms.
%    \begin{macrocode}
% \let\hep@table@of@contents\tableofcontents
% \renewcommand*\tableofcontents{%
%   \glsunsetall\hep@table@of@contents\glsresetall%
% }
% \let\hep@list@of@figures\listoffigures
% \renewcommand*\listoffigures{%
%   \glsunsetall\hep@list@of@figures\glsresetall%
% }
% \let\hep@list@of@tables\listoftables
% \renewcommand*\listoftables{%
%   \glsunsetall\hep@list@of@tables\glsresetall%
% }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\acronyms}
% Add a possibility to have different groups of acronyms.
%    \begin{macrocode}
\NewDocumentCommand{\acronyms}{om}{%
  \IfNoValueTF{#1}{
    \newglossary{#2}{#2.in}{#2.out}{#2}%
    \renewcommand*{\acronymtype}{#2}%
  }{
    \newglossary{#1}{#1.in}{#1.out}{#2}%
    \renewcommand*{\acronymtype}{#1}%
  }
}
%    \end{macrocode}
% \end{macro}
%
%</package>
%
% \section{Test}
%
%<*test>
%    \begin{macrocode}
\documentclass{article}

\usepackage{hyperref}
\usepackage{hep-acronym}
\usepackage{titlesec}

\acronym{PDF}{portable document format}
\acronym{URL}{uniform resource locator}
\acronym{CM}{computer modern}
\acronym{LM}{latin modern}

\begin{document}

\PDF \emph{(Capitalised at beginning of paragraph)}
\PDF \emph{(Second appearance is shortened)}.
\URLs \emph{(Capitalised plural form at beginning of sentence)}
$\URL$ \emph{(Second appearance is forced to be always short)}
\LM \emph{(Not capitalised in middle of sentence)}
\LM \emph{(Second appearance is shorted)}.
$\CM$ \emph{(Enforce short form)}
\CM \emph{(then extend second appearance)}
\URLlong \emph{(enforce long form)}

\section{\PDFlong table of contents is not broken}

\end{document}
%    \end{macrocode}
%
%</test>
%
% \section{Readme}
%
%<*readme>
%
%    \begin{macrocode}
# The `hep-acronym` package

An acronym extension for glossaries

## Introduction

The `hep-acronym` package provides an acronym macro based on the
`glossaries-extra` package. It defines declaration commands for regular,
always-short, always-long, and alternative long-form acronyms, keeps the
first-use counters sentence-aware, optionally links later uses back to the
first occurrence, and provides helpers for resetting, dummy-using, and
grouping acronyms.

It can be loaded using `\usepackage{hep-acronym}`.

## Author

Jan Hajer

## License

This file may be distributed and/or modified under the conditions of the
`LaTeX` Project Public License, either version 1.3c of this license or
(at your option) any later version. The latest version of this license is
in `http://www.latex-project.org/lppl.txt` and version 1.3c or later is
part of all distributions of LaTeX version 2005/12/01 or later.
%    \end{macrocode}
%
%</readme>
%
% \Finale

\endinput

% \PrintIndex
% makeindex -s gglo.ist -o hep-acronym-implementation.gls hep-acronym-implementation.glo
% makeindex -s gglo.ist -o hep-acronym-implementation.ind hep-acronym-implementation.idx
