# Glossaries: introduce short name only if used more than once within section/chapter/part

I would like to obtain the following behavior: If an acronym is only used once within a section/chapter/part, at this occurrence only the long name should be used. If however the acronym is used more than one, I want the long-short behavior.

I have the following non working MWE:

\documentclass{book}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\fpcompare}{ m m m }
{
% #1 = test to perform
% #2 = text for the true case
% #3 = text for the false case
\fp_compare:nTF { #1 } { #2 } { #3 }
}
\ExplSyntaxOff

\usepackage[acronym,symbols,nomain,toc,sanitizesort,nogroupskip,xindy,numberedsection=autolabel]{glossaries-extra}
\setglossarystyle{super}
\setabbreviationstyle[acronym]{long-short}

\GlsXtrEnableEntryUnitCounting{acronym}{1}{section}
%\renewcommand*{\gls}{\cgls}%
%\renewcommand*{\Gls}{\cGls}%
%\renewcommand*{\glspl}{\cglspl}%
%\renewcommand*{\Glspl}{\cGlspl}%
%\renewcommand*{\GLS}{\cGLS}%
%\renewcommand*{\GLSpl}{\cGLSpl}%

\makenoidxglossaries
\newacronym{wn}{WN}{wireless network}

\glsxtrnewsymbol[description={pi},type=symbols]{symb:pi}{\ensuremath{\pi}}%

\usepackage{xstring}
\renewcommand*{\gls}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%to apply this to acronym only (and not symbols as well)
\fpcompare{\glsentrycurrcount{#1} > 1}{\glsentrydesc{#1}}{\cgls{#1}}% fpcompare to detect how often used
}{%
\cgls{#1}%
}%
}%
\renewcommand*{\Gls}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\Glsentrydesc{#1}}{\cgls{#1}}%
}{%
\cgls{#1}%
}%
}%
\renewcommand*{\glspl}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\glsentryplural{#1}}{\cglspl{#1}}%
}{%
\cglspl{#1}%
}%
}%
\renewcommand*{\Glspl}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\Glsentryplural{#1}}{\cglspl{#1}}%
}{%
\cglspl{#1}%
}%
}%
\renewcommand*{\GLS}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\GLSentrydesc{#1}}{\cgls{#1}}%
}{%
\cgls{#1}%
}%
}%
\renewcommand*{\GLSpl}[1]{%
\IfEq{\glscategory{#1}}{acronym}{%
\fpcompare{\glsentrycurrcount{#1} > 1}{\GLSentryplural{#1}}{\cglspl{#1}}%
}{%
\cglspl{#1}%
}%
}%

\begin{document}
\section{A}
\gls{wn}\\
\glspl{wn}\\
\glspl{wn}\\
\glspl{wn}\\
count: \glsentrycurrcount{wn}\\
\gls{symb:pi}
\section{B}

\glspl{wn}\\
count: \glsentrycurrcount{wn}

\section{C}
\Glspl{wn}\\
\gls{wn}\\
count: \glsentrycurrcount{wn}
\end{document}


which produces:

I however, would have expected/wanted a count of 4 in section A, and in section C, that at first the full description is printed instead of WNs

How to make this work reliably?

The solution was given by Nicola here in this example:

% arara: pdflatex
% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{article}

\usepackage{glossaries-extra}

\makeglossaries

\GlsXtrEnableEntryCounting
{abbreviation}% list of categories to use entry counting
{2}% trigger value

\newabbreviation{html}{HTML}{hypertext markup language}
\newabbreviation{xml}{XML}{extensible markup language}

\newglossaryentry{sample}{name={sample},description={sample}}

\begin{document}
This is a sample document that uses entry counting. The entry counting
has been enabled on the \texttt{abbreviation} category.
This means that abbreviations will only be added to the glossary
if they have been used more than $n$ times, where in this
document $n$ has been set to
\glsgetcategoryattribute{abbreviation}{entrycount}.
Entries in other categories behave as normal.

Used once: \gls{html}.

Used twice: \gls{xml} and \gls{xml}.

Used three times: \gls{css} and \gls{css} and \gls{css}.

Used once but this entry is in the general'' category
which doesn't have the entrycount'' attribute set:
\gls{sample}.

\printglossaries

\end{document}



To automatically reset the count e.g. for chapters use this code (copied from manual). Note the \GlsXtrEnableEntryUnitCounting command.

\documentclass{report}
\usepackage{glossaries-extra}
\GlsXtrEnableEntryUnitCounting{abbreviation}{2}{chapter}
\makeglossaries
\newabbreviation{html}{HTML}{hypertext markup language}
\newglossaryentry{sample}{name={sample},description={sample}}
\begin{document}
\chapter{Sample}
Used once: \gls{html}.
Used three times: \gls{css} and \gls{css} and \gls{css}.
Used once: \gls{sample}.
\chapter{Another Sample}
Used once: \gls{css}.
Used twice: \gls{html} and \gls{html}.
\printglossaries
\end{document}



I am just pasting this here for the sake of completeness, please upvote the answer the in the other thread.

• How does this take care of the "more than once within section/chapter/part"-bit of your question? It seems to work when I compile it, but which part of the code defines within which strucutre the counting should be? – sheß Mar 13 '19 at 8:34
• Oh, I see, it's the \GlsXtrEnableEntryCounting{abbreviation}{2} – sheß Mar 13 '19 at 8:35

You could make acronyms section-specific by giving them section-specific names. Of course you could easily automate this:

\documentclass{book}
\usepackage[xindy,style=long,numberline,savewrites=true,acronym,nomain]{glossaries}
\usepackage{pgffor}

\makeglossaries
\glsenableentrycount

%create a separate acrony for each section
\let\oldnewacronym\newacronym   \renewcommand{\newacronym}[3]{\foreach \n in {0,...,10}{\oldnewacronym{c\n-#1}{#2}{#3}}}

%%patch all the commands to use the within-section version of the acronym
\let\oldcgls\cgls \renewcommand{\cgls}[1]{\oldcgls{s\arabic{section}-#1}}
\let\oldcglspl\cglspl \renewcommand{\cglspl}[1]{\oldcglspl{s\arabic{section}-#1}}
\let\oldcGlspl\cGlspl \renewcommand{\cGlspl}[1]{\oldcGlspl{s\arabic{section}-#1}}

\newacronym{wn}{WN}{wireless network}

\begin{document}
\section{A}
\cgls{wn}\\
\cglspl{wn}\\
\cglspl{wn}\\
\cglspl{wn}\\
count: \glsentrycurrcount{s1-wn}\\

\section{B}
\cglspl{wn}\\
count: \glsentrycurrcount{s2-wn}

\section{C}
\cGlspl{wn}\\
\cgls{wn}\\
count: \glsentrycurrcount{s3-wn}
\end{document}


Basically, when the acronym wn is defined, it creates separate acronyms for each section, naming them s1-wn and so on. Now when the acronym is called within section X, it "redirects" and uses sX-wn. The result:

Of course this would need some more tweaking:

1. So far it only works for sections, not chapters, but that should be straighforward
2. It is hard-wired to work for up to 10 sections. You could increase that number, introduce a new counter, or do some .aux-file-magic to automatize this to work for any number of chapters/section/subsections
3. It also needs some further tweaking to deal with symbols and so on.
4. I haven't tried it with glossaries-extra so far, because I don't have that installed.
5. I don't now how you want your list of abbreviations to appear. So for now my proposal ignores this, as did your question.

Credits: Part of my solution is taken from here