I wrote the following code
\documentclass{article}
\makeatletter
\newtoks\a@toks
\newtoks\b@toks
\newcounter{a@counter}%
\newcounter{b@counter}%
\setcounter{a@counter}{0}%
\setcounter{b@counter}{0}%
\newcommand{\aAdd}[1]{%
\ifnum\thea@counter>0\a@toks=\expandafter{\the\a@toks {#1}}%
\else\a@toks=\expandafter{\the\a@toks {#1}}%
\fi
\stepcounter{a@counter}%
}
\newcommand{\reset}{%
\setcounter{a@counter}{0}%
\a@toks={}%
}
\newcommand{\bexp}{%
\ifnum\theb@counter>0\b@toks=\expandafter{\the\b@toks, (\the\a@toks)}%
\else\b@toks=\expandafter{\the\b@toks (\the\a@toks) }%
\fi
\stepcounter{b@counter}%
\setcounter{a@counter}{0}%
\a@toks=\noexpand{}%
}
\newcommand{\print}{%
\the\b@toks%
}
\makeatother
\begin{document}
Hello World!\\[3cm]
\aAdd{a}
\aAdd{b}
\bexp
\aAdd{c}
\print
\end{document}
What I'm trying to do is the following: with \aAdd
I add some element to a list, specifically in this example after
\aAdd{a}
\aAdd{b}
I expect \a@toks
to be equal to ab
. Then I flush this into another token, so I expect \b@toks
to be equal to ab
. The problem is when the command
\a@toks={}
is executed, this resets even \b@toks
that continues to follow \a@toks
, so that, when I execute
\aAdd{c}
\b@toks
has the value c
.
I would like, once set \b@toks = \a@toks
to set only the value of \b@toks
, so that, when I redefine \a@toks
, \b@toks
continues to have the preceding value (in this case ab
).
Can anyone help me?
\aAdd
the register\a@toks
is{a}{b}
notab
do you not want the braces? and after\bexp
the register\b@toks
is(\the \a@toks )
but (I think?) you want the content of a@toks not a reference?\a@toks
, but what is stored in\b@toks
. I want that after I reset\a@toks={}
in\b@toks
continues to be{a}{b}
. Have I explained it better?\ifnum\theb@counter>0\b@toks
which will try to expand\b@toks
to terminate the number before doing the test, it works here asb@toks
is not expandable but safer to leave a space after0
or better use\z@
\b@toks=\expandafter{\the\a@toks}
for the first assignment?ab
which it does not, so the whole question is very confusing. b@toks is never set to {a}{b} in the above code.