![自动统计词汇表条目数](https://linux22.com/image/256165/%E8%87%AA%E5%8A%A8%E7%BB%9F%E8%AE%A1%E8%AF%8D%E6%B1%87%E8%A1%A8%E6%9D%A1%E7%9B%AE%E6%95%B0.png)
我想全局自动地对文档的所有词汇表条目进行计数。我可以扩展宏\glossaryentry
来执行此操作,而无需引入新定义的宏吗?我听说过类似\newcommandx
或类似的方法。计数器必须持续存在,.aux
以便我可以在文档正文中使用它。
答案1
词汇表的数量在参考中被记住。这里使用numglossaries
包,因为它提供了。在下一页发货时使用延迟写入。在文档末尾没有其他页面,因此必须立即进行写入。zref
\zref@wrapper@immediate
\label
编号本身取决于您使用的词汇表包。这里作为示例,我已修补\@wrglossary
将实际词汇表条目写入.glo
文件中。
\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}
扩大的视野
其他元素也可以按以下方式计算:
\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}