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


\resetanimalnumis the culprit. If you put\nextanimalinto the next line, it works – Jan 14 '16 at 19:13\def\resetanimalnum{\animalnum=0\relax}– touhami Jan 14 '16 at 19:13\fi% \ifnum\animalnum > 2with\else– touhami Jan 14 '16 at 19:14\relax, but I don't know why this must be this way – Jan 14 '16 at 19:15\elseis better)! But why does TeX not reset the counter without the\relax(or a space)? – musarithmia Jan 14 '16 at 19:17\relaxwill stop the parsing of\animalnum=0. – barbara beeton Jan 14 '16 at 19:26\relaxafter\ifnum…\ficonstruction – Werner Jan 14 '16 at 19:38