[XeTeX] forcing counter evaluation

Joseph Wright joseph.wright at morningstar2.co.uk
Thu Oct 8 15:16:21 CEST 2009


Michiel Kamermans wrote:
> I'm running into the problem of a counter not being expanded to its
> actual value, leading xetex to complain that I'm using something that
> isn't a number where a number is required.
> 
> In a nutshell, I am writing a new package that's fiddling with xetex'
> interchartoks, and has the following macro command:
> 
> \usepackage{fontspec}
> \newcommand{\lowestclass}{74}
> \newcommand{\highestclass} {240}
> ...
> \newcommand{\setUTransitionTo}[2]{
>    \newcounter{iclass}
>    \forloop{iclass}{\lowestclass}{\value{iclass} < #1}
>    {
>        \XeTeXinterchartoks #1 \value{iclass}={#2}
>    }
>    \forloop{iclass}{#1+1}{\value{iclass} <= \highestclass}}
>    {
>        \XeTeXinterchartoks #1 \value{iclass}={#2}
>    }
> }
> ...
> \setUTransitionTo{228}{\fontspec{Arial}}
> 
> problematically, the "\value{iclass}" command inside the for loops does
> not yield the number stored in the counter, but yields the text string
> "\c at iclass" instead, which is very much not what xetex expects to find
> in a \XeTeXinterchartoks rule, so it dies.
> 
> I tried looking for ways to force counter evaluation, but the only
> things I can find relating to forcing expansion are actually the "not
> forcing expansion" commands (\expandafter, \noexpand, \the). Is there a
> way to specify that \value needs to absolutely definitely give a number,
> rather than the counter's macro? (I'm reading through TeX by Topic at
> the moment, but it's not the easiest material to get through ^_^;;)
> 
> - Mike

Usually in this situation you \edef something:

\newcommand{\setUTransitionTo}[2]{
   \newcounter{iclass}
   \forloop{iclass}{\lowestclass}{\value{iclass} < #1}
   {
      \begingroup
      \edef\x{\endgroup
        \noexpand\XeTeXinterchartoks #1 \value{iclass} = {#2}
      }%
      \x
   }

etc.

The trick here is that \x is only defined inside a group, so we don't
worry about trashing some other definition. \value{iclass} expands to
the primitive result (a number), and then executing \x closes the group
and does the original command.
-- 
Joseph Wright


More information about the XeTeX mailing list