I am not able to reset a counter to zero using a macro in Plain TeX.
This code uses \ifcase
and the counter animalnum
to output one of a list of animal names, incrementing the counter each time it is called so that next call will produce the subsequent name in the list. If it has been called more than a maximum number, it outputs an error message.
The command \resetanimalnum
is supposed to set the counter back to zero and start the list over, but for some reason it doesn't work as expected.
Why does the counter behave this way and I how can I fix it?
\newcount\animalnum
\def\nextanimal{%
\ifcase\animalnum%
\animalI\or
\animalII\or
\animalIII%
\fi%
\ifnum\animalnum > 2
\exclaim%
\fi%
\advance\animalnum by 1%
}
\def\resetanimalnum{\animalnum=0}
\def\animalI {Lions}
\def\animalII {Tigers}
\def\animalIII {Bears}
\def\exclaim {Oh my!}
\nextanimal\ \nextanimal\ \nextanimal\ \nextanimal
\resetanimalnum\nextanimal\ \nextanimal
\bye
\resetanimalnum
is the culprit. If you put\nextanimal
into the next line, it works\def\resetanimalnum{\animalnum=0\relax}
\fi% \ifnum\animalnum > 2
with\else
\relax
will stop the parsing of\animalnum=0
.\relax
after\ifnum
…\fi
construction