[texhax] Howto Count tokens

Dr. R. Hasson R.Hasson at open.ac.uk
Wed Sep 19 09:24:56 CEST 2007


Tim Dijkstra wrote:
> Hi,
> 
> I'm pretty new to TeX programming, so my understanding of it is pretty
> shallow. I'm trying to write a latex class at the moment and hit the
> following problem.
> 
> I have a register created with \newtoks\list, it's content is roughly
> 
> 	{AA \mysep{BB \mysep CC \mysep DD} \mysep{EE} }
> 
> Now the problem is, how do I count the number of \mysep-s? I first
> tried to replace all of them with a step in the counter (idea from tex
> by topic)
> 
> 	\def\mysep#1{\advance\mycount1\relax}\the\list
> 
> And now the counter has a number.
> But that obviously doesn't take into account the nesting. Now I could
> just redefine it to:
> 
> 	\def\mysep#1{\advance\mycount1\relax #1}\the\list
> 
> Now I get the correct number, but now it also outputs AA BB CC DD EE.
> In other words I need a way to expand the list but discard the output,
> I only need the `by product'.

Why not put the output in a box (and then forget about it)? For example:

\def\mysep#1{\global\advance\mycount1#1}\setbox0\hbox{\the\list}

(The global is needed to advance the counter despite the nesting.)

Robert.















More information about the texhax mailing list