Create token registers dynamically

  • Follow


Hi,

I need to create token registers on purpose. One should be able to
pass an argument and a token register wit this name should be created.
Unfortunately it seems as I can't use \newtoks inside of \def. I tried
something like (the name of the new token register is saved in
\temp@@def@name):

\def\temp@def{%
  %% Test if the name is already registered
  \expandafter\ifcsname\temp@@def@name\endcsname
    \errmessage{"\temp@@def@name" is already defined!}
  \else
    %% Create new token registers named like the
    %% expansion of \temp@@def@name
    \global\expandafter\newtoks\csname\temp@@def@name\endcsname
  \fi
}

But this yields in errors on using it. Is there something wrong or a
is there indeed a limitation that I can't use \newtoks inside \def? If
so, is there a work-around?

Thanks,
Tom
0
Reply 2voodoo (4) 3/23/2010 1:01:05 PM

On 23 Mrz., 14:01, Tom Kazimiers <2voo...@gmx.de> wrote:
> Hi,
>
> I need to create token registers on purpose. One should be able to
> pass an argument and a token register wit this name should be created.
> Unfortunately it seems as I can't use \newtoks inside of \def. I tried
> something like (the name of the new token register is saved in
> \temp@@def@name):
>
> \def\temp@def{%
> =A0 %% Test if the name is already registered
> =A0 \expandafter\ifcsname\temp@@def@name\endcsname
> =A0 =A0 \errmessage{"\temp@@def@name" is already defined!}
> =A0 \else
> =A0 =A0 %% Create new token registers named like the
> =A0 =A0 %% expansion of \temp@@def@name
> =A0 =A0 \global\expandafter\newtoks\csname\temp@@def@name\endcsname
> =A0 \fi
>
> }
>
> But this yields in errors on using it. Is there something wrong or a
> is there indeed a limitation that I can't use \newtoks inside \def? If
> so, is there a work-around?
>
> Thanks,
> Tom

It seems as I found a solution within musixTex package (http://
www.cam.ctan.org/tex-archive/macros/generic/fenixpar/fenixtok.sty). I
define the following macro:
\def\tok@newtoks{\csname newtoks\endcsname}
Then I could use it instead of \newtoks.

This seems like a hack. Is there something wrong with it? Or the other
way round: Why is \newtoks prohibited in \def?

Regards,
Tom
0
Reply Tom 3/23/2010 1:09:43 PM


Tom Kazimiers <2voodoo@gmx.de> wrote:

> On 23 Mrz., 14:01, Tom Kazimiers <2voo...@gmx.de> wrote:
> > Hi,
> >
> > I need to create token registers on purpose. One should be able to
> > pass an argument and a token register wit this name should be created.
> > Unfortunately it seems as I can't use \newtoks inside of \def. I tried
> > something like (the name of the new token register is saved in
> > \temp@@def@name):
> >
> > \def\temp@def{%
> > � %% Test if the name is already registered
> > � \expandafter\ifcsname\temp@@def@name\endcsname
> > � � \errmessage{"\temp@@def@name" is already defined!}
> > � \else
> > � � %% Create new token registers named like the
> > � � %% expansion of \temp@@def@name
> > � � \global\expandafter\newtoks\csname\temp@@def@name\endcsname
> > � \fi
> >
> > }
> >
> > But this yields in errors on using it. Is there something wrong or a
> > is there indeed a limitation that I can't use \newtoks inside \def? If
> > so, is there a work-around?
> >
> > Thanks,
> > Tom
> 
> It seems as I found a solution within musixTex package (http://
> www.cam.ctan.org/tex-archive/macros/generic/fenixpar/fenixtok.sty). I
> define the following macro:
> \def\tok@newtoks{\csname newtoks\endcsname}
> Then I could use it instead of \newtoks.
> 
> This seems like a hack. Is there something wrong with it? Or the other
> way round: Why is \newtoks prohibited in \def?

In Plain TeX \newtoks and the other \new... macros are "outer",
so they are not allowed inside the replacement text of a macro.
In LaTeX they are not outer, however.

The workaround is good. You can also avoid the \global, since
all \new... macros act globally; defining a new macro is actually
not required:

  \csname newtoks\expandafter\endcsname
    \csname\temp@@def@name\endcsname

should work just as well. Another method for getting a non-outer
version of \newtoks is

  \toks@=\expandafter{\newtoks}
  \edef\tok@newtoks{\the\toks@}

This avoids expansion issues that may happen with the previous
definition.

Ciao
Enrico
0
Reply Enrico 3/23/2010 1:24:57 PM

Tom Kazimiers <2voodoo@gmx.de> wrote:

> It seems as I found a solution within musixTex package (http://
> www.cam.ctan.org/tex-archive/macros/generic/fenixpar/fenixtok.sty). I
> define the following macro:
> \def\tok@newtoks{\csname newtoks\endcsname}
> Then I could use it instead of \newtoks.
> 
> This seems like a hack. Is there something wrong with it?

No.

> Or the other way round: Why is \newtoks prohibited in \def?

In plain-TeX \newtoks is an \outer definition and therefore prohibited
in \def or inside \if constructs. Generating the command token
on the fly using \csname is the usual way to call \outer macros.

-- 
Heiko Oberdiek
0
Reply Heiko 3/23/2010 1:26:41 PM

Thanks for the explanations.

Cheers,
Tom
0
Reply Tom 3/23/2010 1:31:07 PM

4 Replies
172 Views

(page loaded in 0.168 seconds)

Similiar Articles:






7/21/2012 8:06:53 PM


Reply: