3

This question is somewhat similar to Count the number of elements of each size in a list. It should actually be simpler but I nevertheless can't figure out how to do it without using the same code as in the other question and then add all the values together (which is overkill, I assume).

Code with question incorporated

\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}

output

Notice that all the elements are defined via \def.

2
  • Should one be able to count only the items listed? That is, ``\valueA \\ \valueB` should return 2? What about a different interface that is more representative of a list? – Werner May 25 '15 at 23:54
  • @Werner For what I had in mind, only the items listed, but a more comprehensive counting mechanism is of course very acceptable too. :) – Svend Tveskæg May 25 '15 at 23:58
3

Abuse \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}

enter image description here

If you just want to count a set of elements

\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}

enter image description here

The same with 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}
3
  • Really nice. I might have made the question a bit confusing (sorry!); I really only want the number of elements, and no printing of the elements themselves. How can I do that? :) – Svend Tveskæg May 25 '15 at 23:53
  • @SvendTveskæg You have to tell TeX to count them. :) – egreg May 26 '15 at 0:00
  • That's a good point. :) Thank you very much, egreg. (I'll wait and see if Werner comes up with code which is abel to count other things than numbers before acceping an answer.) Btw. don't you mean xparse instead of expl3? – Svend Tveskæg May 26 '15 at 0:02
1

Here is a list-counting etoolbox implementation:

There are 11 elements.

\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}
0

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.