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?


\aAddthe register\a@toksis{a}{b}notabdo you not want the braces? and after\bexpthe register\b@toksis(\the \a@toks )but (I think?) you want the content of a@toks not a reference? – David Carlisle May 04 '16 at 19:30\a@toks, but what is stored in\b@toks. I want that after I reset\a@toks={}in\b@tokscontinues to be{a}{b}. Have I explained it better? – MaPo May 04 '16 at 19:33\ifnum\theb@counter>0\b@tokswhich will try to expand\b@toksto terminate the number before doing the test, it works here asb@toksis not expandable but safer to leave a space after0or better use\z@– David Carlisle May 04 '16 at 19:34\b@toks=\expandafter{\the\a@toks}for the first assignment? – May 04 '16 at 19:35abwhich it does not, so the whole question is very confusing. b@toks is never set to {a}{b} in the above code. – David Carlisle May 04 '16 at 19:35abwith{a}{b}, let I will perform the following substitution in the questionab→{a}{b}. When\b@toks=\expandafter{\the\b@toks (\the\a@toks)} what is it stored in\b@toks? I would like to maintain this value even when, after I set\a@toks={}– MaPo May 04 '16 at 19:40(\the \a@toks )as you have stored exactly that. not the expansion of a@toks – David Carlisle May 04 '16 at 19:42