0

I would like to be able to know the number of abbreviations used in a document before the list of abbreviations is eventually output. (The reason is to guess if the list will fit to a single page, and there might be a better way to do that but for the sake of argument let's ignore that).

The following code almost works but is probably far from a working solutions... I'll explain it below.

\documentclass{article}

\usepackage[nomain,acronym]{glossaries}

\makeglossaries

\newacronym{abc}{ABC}{a contrived acronym}\glsunset{abc}
\newacronym{foo}{FOO}{a contrived acronym}
\newacronym{bar}{BAR}{a contrived acronym}
\newacronym{baz}{BAZ}{a contrived acronym}

\usepackage{totcount}
\newtotcounter{acronum}
\renewcommand*{\glslinkpostsetkeys}{%
  \ifglsused{\glslabel}{%
  }{%
    \typeout{acronum: counting \glslabel (\glstype)}%
    \stepcounter{acronum}%
  }%
}


\begin{document}

\gls{abc}
\gls{foo}
\gls{bar}
\gls{foo}
\gls{foo}

\vspace{1cm}
\textbf{acronum: is \theacronum}

\printacronyms

\end{document}

If it wasn't for that \glsunset{abc} this code would be fine. However, I have a number of abbreviations that I would like to have in the list but not written out fully in the text - thus I use \glsunset for them like above. With it the output of this example is 2 instead of 3.

I have tried looking at glossaries' source for some way or hidden variable or hook but it beat me ;) How to count the true number of abbreviations in the list?

1 Answer 1

1

I would just define a glossary style that actually counts the number of entries in the glossary:

\documentclass{article}

\usepackage[nomain,acronym]{glossaries}

\makeglossaries

\makeatletter
  \newcounter{acronyms}
  \newglossarystyle{countentries}{%
    \setcounter{acronyms}{0}
    \renewenvironment{theglossary}{}{}%
    \let\glossaryheader\@empty
    \let\glsgroupheading\@gobble
    \let\glsgrouptitle\@gobble
    \let\glsnavhypertarget\@gobbletwo
    \let\glsnavigation\@empty
    \let\glsgroupskip\@empty
    \let\glsentryitem\@gobble
    \let\glsentrycounterlabel\@empty
    \let\glstarget\@gobbletwo
    \let\glossaryentrynumbers\@gobble
    \let\subglossentry\@gobblethree
    \let\glssubentryitem\@gobble
    \let\glssubentrycounterlabel\@empty
    \let\currentglossary\@empty
    \renewcommand\glossarysection[2][]{}%
    \let\glossarypreamble\@empty
    \let\glossarypostamble\@empty
    \let\glsresetentrylist\@empty
    \renewcommand\glossentry[2]{\stepcounter{acronyms}}%
  }
  \AtBeginDocument{\printacronyms[style=countentries]}
\makeatother

\newacronym{abc}{ABC}{a contrived acronym}\glsunset{abc}
\newacronym{foo}{FOO}{a contrived acronym}
\newacronym{bar}{BAR}{a contrived acronym}
\newacronym{baz}{BAZ}{a contrived acronym}

\begin{document}

\gls{abc}
\gls{foo}
\gls{bar}
\gls{foo}
\gls{foo}

\vspace{1cm}
\textbf{I counted \arabic{acronyms} acronyms.}

\printacronyms

\end{document}

Most of the definitions in the glossary style just make sure that nothing is printed when you use it (I'm not sure if all of them are necessary, but better safe than sorry). The actual counting happens only in \glossentry.

One advantage of this method is that you can count the number of acronyms anywhere in the document (I did it \AtBeginDocument).

3
  • I would have hoped for a more concise and elegant solution. Maybe @NicolaTalbot can come up with one later. ;) For now I'll accept this answer.
    – stefanct
    Jul 11, 2019 at 13:09
  • One problem I have noticed with this solution is that \printacronyms prints a \null if the respective acr file(?) is not found which happens (at least in my current setup) if there are no acronyms used at all. This is a problem when used with beamer because it creates an empty page at the beginning.
    – stefanct
    Jan 29, 2020 at 19:55
  • Correction: the acr file actually contains \null already... this is intended behavior of makeglossaries, see my followup question: tex.stackexchange.com/questions/526238
    – stefanct
    Jan 30, 2020 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.