![计算列表中元素的数量](https://linux22.com/image/312094/%E8%AE%A1%E7%AE%97%E5%88%97%E8%A1%A8%E4%B8%AD%E5%85%83%E7%B4%A0%E7%9A%84%E6%95%B0%E9%87%8F.png)
这个问题有点类似于计算列表中每种大小的元素数量。它实际上应该更简单,但是我仍然无法弄清楚如何在不使用与其他问题相同的代码的情况下做到这一点,然后将所有值加在一起(我认为这是过度的)。
包含问题的代码
\documentclass{article}
\def\valueA{35762}
\def\valueB{62525}
\def\valueC{67102}
\def\valueD{38990}
\def\valueE{1993}
\def\valueF{42762}
\def\valueG{22098}
\def\valueH{28900}
\def\valueI{45596}
\def\valueJ{38495}
\def\valueK{196292}
\begin{document}
\noindent The elements have the values\\[0.5\baselineskip]
\valueA\\
\valueB\\
\valueC\\
\valueD\\
\valueE\\
\valueF\\
\valueG\\
\valueH\\
\valueI\\
\valueJ\\
\valueK\\[0.5\baselineskip]
but how do I make \LaTeX{} automatically count the number of elements above? (The answer is $11$ in this case.)
\end{document}
请注意,所有元素都是通过 定义的\def
。
答案1
虐待\prevgraf
:
\documentclass{article}
\newenvironment{listandcount}
{\trivlist\item\relax}
{\par\xdef\lastcount{\the\prevgraf}\endtrivlist}
\def\valueA{35762}
\def\valueB{62525}
\def\valueC{67102}
\def\valueD{38990}
\def\valueE{1993}
\def\valueF{42762}
\def\valueG{22098}
\def\valueH{28900}
\def\valueI{45596}
\def\valueJ{38495}
\def\valueK{196292}
\begin{document}
\noindent The elements have the values
\begin{listandcount}
\valueA\\
\valueB\\
\valueC\\
\valueD\\
\valueE\\
\valueF\\
\valueG\\
\valueH\\
\valueI\\
\valueJ\\
\valueK
\end{listandcount}
and the list has \lastcount{} elements.
\end{document}
如果你只想计算一组元素
\documentclass{article}
\makeatletter
\newcommand{\countlist}[1]{%
\count@=\z@
\@for\next:=#1\do{\advance\count@\@ne}%
\the\count@
}
\makeatother
\def\valueA{35762}
\def\valueB{62525}
\def\valueC{67102}
\def\valueD{38990}
\def\valueE{1993}
\def\valueF{42762}
\def\valueG{22098}
\def\valueH{28900}
\def\valueI{45596}
\def\valueJ{38495}
\def\valueK{196292}
\begin{document}
There are
\countlist{\valueA,\valueB,\valueC,\valueD,\valueE,
\valueF,\valueG,\valueH,\valueI,\valueJ,\valueK}
elements.
\end{document}
相同之处expl3
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\countlist}{m}
{
\clist_count:n { #1 }
}
\ExplSyntaxOff
\def\valueA{35762}
\def\valueB{62525}
\def\valueC{67102}
\def\valueD{38990}
\def\valueE{1993}
\def\valueF{42762}
\def\valueG{22098}
\def\valueH{28900}
\def\valueI{45596}
\def\valueJ{38495}
\def\valueK{196292}
\begin{document}
There are
\countlist{\valueA,\valueB,\valueC,\valueD,\valueE,
\valueF,\valueG,\valueH,\valueI,\valueJ,\valueK}
elements.
\end{document}
答案2
以下是列表计数etoolbox
执行:
共有 11 个元素。
\documentclass{article}
\usepackage{etoolbox}
\newcounter{listcount}
\newcommand{\countlist}[1]{%
\setcounter{listcount}{0}% Reset listcount counter
\renewcommand*{\do}[1]{\stepcounter{listcount}}% At each item, step listcount
\docsvlist{#1}% Process list
}
\newcommand{\printlistcount}{\thelistcount}%
\def\valueA{35762}
\def\valueB{62525}
\def\valueC{67102}
\def\valueD{38990}
\def\valueE{1993}
\def\valueF{42762}
\def\valueG{22098}
\def\valueH{28900}
\def\valueI{45596}
\def\valueJ{38495}
\def\valueK{196292}
\begin{document}
There are
\countlist{\valueA,\valueB,\valueC,\valueD,\valueE,
\valueF,\valueG,\valueH,\valueI,\valueJ,\valueK}\printlistcount{}
elements.
\end{document}