3

I'm trying to implement a foreach loop in LaTeX limited by the value of a totcount counter, but this doesn't work at all and I don't understand why.

Can this be done at all, and if yes, how?

Here's a minimal example of failure:

\documentclass{scrartcl}
\usepackage{totcount}
\usepackage{pgffor}

\begin{document}
% dummy totcount counter
\newtotcounter{dummycounter}
\addtocounter{dummycounter}{10}
Total of dummy counter = \total{dummycounter}

% for loop with explicit number
\foreach\index in {1,...,10}{repeat }

% for loop with number taken from totcount counter
\foreach\index in {1,...,\total{dummycounter}}{repeat}

\end{document}

The resulting output looks like:

Total of dummy counter = 10
repeat repeat repeat repeat repeat repeat repeat repeat repeat repeat
repeat10pt¿1pt10pt

and the error message on the console looks like:

! Missing number, treated as zero.
<to be read again> 
                   \def 
l.15 ...ex in {1,...,\total{dummycounter}}{repeat}

so it appears that in the context of \foreach, the output of \total is not interpreted as a number. This is very puzzling, as by definition, it is a number.

Clarification of the packages:

  • The goal of using totcount is to add some values during a first run, which then get written to the .aux file and can be used on a second run (I already use it succesfully elsewhere)
  • pgffor is a possible way to implement loops, as found in answers to other questions in this site; I did try others, without any more success for this particular question, but pgffor seems to be simpler to implement in general
0

2 Answers 2

3

\total is not expandable which probably is the problem here. However,

\foreach\index in {1,...,\totvalue{dummycounter}}{repeat}

works, because \totvalue is expandable and expands to a number (which is –1 on the first run) that can be tested.

\documentclass{scrartcl}
\usepackage{totcount}
\usepackage{pgffor}

\begin{document}
% dummy totcount counter
\newtotcounter{dummycounter}
\addtocounter{dummycounter}{10}
Total of dummy counter = \total{dummycounter}

% for loop with explicit number
\foreach\index in {1,...,10}{repeat }

% for loop with number taken from totcount counter
\foreach\index in {1,...,\totvalue{dummycounter}}{repeat}

\end{document}

enter image description here

1

As clemens explained, \total is not expandable since it uses an internal \def, which prevents expansion -- in the very end, it can't be used in an numerical expansion context which is necessary in the \if condition in order to check for the end of a loop.

I also apply \totvalue here, but in a traditional TeX \loop:

Side note: counters should be defined preferably in the preamble, not somewhere in the document body.

\documentclass{article}
\usepackage{totcount}


\newcounter{loopcntr}


\newtotcounter{dummycounter}
\begin{document}
\addtocounter{dummycounter}{10}
Total of dummy counter = \total{dummycounter}

\loop\unless\ifnum\value{loopcntr}>\numexpr\totvalue{dummycounter}-1%
\stepcounter{loopcntr}
\theloopcntr\ repeat

\repeat


\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .