How do I put a "#" into a token list without getting two?
When TeX shows #
, it doubles it up. So in your
\newtoks\t
\t={#}
\showthe\t
\bye
example,
> ##.
l.3 \showthe\t
represents one #
inside the token register. You can convince TeX to show this using \string
:
\newtoks\t
\t={#}
\expandafter\string\the\t
\bye
(which of course only works because there is a single token in \t
!)
For the definition of the macro, you'd need something like
\newtoks\t
\t={You gave '#1'}
\expandafter\def\expandafter\test
\expandafter#\expandafter1\expandafter{\the\t}
\test{things}
\bye
Joseph answered the reason why # shows up double, as the standard behaviour of show etc.
Since you want to build the body of a macro definition in a token list the only way to incorporate #1 etc. (IMHO) is to put the to put it via a macro.
\newtoks\t
\t={\def\z#1{\bf #1}}
\def\macro#1{\the\t \z{B} \z{#1}}
\macro{test}
\bye
The only difference from what was proposed by the OP, is that to put the body definition in a token list, you need to pass it to the macro by using \the\t
and a \helpermacro{#1}
.
Following what Yiannis wrote, you can do something like this:
% Put the replacement text into \t, using any computation like this doubling loop
\newtoks\t
\t{#1}
\count255 0
\loop\ifnum\count255<10
\t\expandafter\expandafter\expandafter{\expandafter\the\expandafter\t\the\t}
\advance\count255 1
\repeat
% Define a helper that when executed will perform the real definition
\edef\helper{\noexpand\def\noexpand\X##1{\the\t}}
\helper
% Now the real macro \X can be used.
\X{[X] }
\bye