# Automatically count of glossary entries

I want to count all glossary entries of a document globally and automatically. Can I expand the macro \glossaryentry to do that and in such a way that I don't introduce a newly defined macro? I have heard about something like \newcommandx or similar. The counter must persist to the .aux so that I can use it in the body of the document.

-

The number of glossaries is remembered in the reference numglossaries. Package zref is used here, because it provides \zref@wrapper@immediate. \label uses deferred writing at the next page shipout. At the end of the document there is no further page, therefore the writing must be done immediately.

The numbering itself depends on the glossary package you are using. Here as example I have patched \@wrglossary that writes the actual glossary entry into the .glo file.

\documentclass{article}
\usepackage{zref-base}

\makeatletter
\newcounter{numglossaries}
\newcommand*{\numglossaries}{%
\zref@extract{numglossaries}{default}%
\zref@refused{numglossaries}%
}
\newcommand*{\write@numglossaries}{%
\begingroup
\zref@setcurrent{default}{\the\value{numglossaries}}%
\zref@wrapper@immediate{%
\zref@labelbyprops{numglossaries}{default}%
}%
\endgroup
}
\AtEndDocument{\write@numglossaries}

% The counting depends on the package that is used for glossaries.
\@ifdefinable{org@wrglossary}{%
\let\org@wrglossary\@wrglossary
\renewcommand*{\@wrglossary}{%
\stepcounter{numglossaries}%
\org@wrglossary
}%
}
\makeatother

\makeglossary

\begin{document}

The number of glossaries is \numglossaries.

\glossary{A}
\glossary{B}
\glossary{C}

\end{document}


## Extended version

Also other elements can be counted this way:

\documentclass{article}
\usepackage{zref-base}
\usepackage{etoolbox}

\makeatletter
\newcommand*{\numberelement}[1]{%
\newcounter{num#1}
\expandafter\newcommand\expandafter*\csname num#1\endcsname{%
\zref@extract{num#1}{default}%
\zref@refused{num#1}%
}%
\expandafter
\newcommand\expandafter*\csname write@num#1\endcsname{%
\begingroup
\zref@setcurrent{default}{\the\value{num#1}}%
\zref@wrapper@immediate{%
\zref@labelbyprops{num#1}{default}%
}%
\endgroup
}%
\expandafter\AtEndDocument\csname write@num#1\endcsname
}

\numberelement{glossaries}
\numberelement{figures}
\numberelement{tables}

\pretocmd\@wrglossary{\stepcounter{numglossaries}}{}{}
\pretocmd\figure{\stepcounter{numfigures}}{}{}
\pretocmd\table{\stepcounter{numtables}}{}{}

\makeatother

\iffalse
% The counting depends on the package that is used for glossaries.
\@ifdefinable{org@wrglossary}{%
\let\org@wrglossary\@wrglossary
\renewcommand*{\@wrglossary}{%
\stepcounter{numglossaries}%
\org@wrglossary
}%
}
\fi
\makeatother

\makeglossary

\begin{document}

\begin{table}
\centering
\begin{tabular}{ll}
Glossary entries: & \numglossaries\\
Figures: & \numfigures\\
Tables: & \numtables
\end{tabular}
\caption{Statistics}
\end{table}

\glossary{A}
\glossary{B}
\glossary{C}

\end{document}


-
Yes, phantastic! Can I use generally this technique to persist all kinds of counters such as number of tables, figures diagrams etc.? –  Dr. Heiko Voss Sep 28 '12 at 14:14
Extended version added. –  Heiko Oberdiek Sep 28 '12 at 14:35