\toks register expansion issue - or is it?

I am trying to assemble a table that occurs multiple times throughout a document. It always consists of some fixed columns, followed by a set of columns that appear in one table but are not needed in another table. I was told it would be a good idea to use toks registers for this. Never having heard of these, I tried to figure out how I could put them to use in my case. (BTW, is there a good online documentation of this stuff available?) From various examples, I've pieced together this code:

\documentclass{article}

\newtoks\toksA
\newtoks\toksB
\newtoks\toksC
\newtoks\toksD

\toksA={ foo & bar }
\toksB={ Foo & Bar }
\toksC={ fOo & bAr }
\toksD={ foO & baR }
}

\newcommand*{\numbercolumn}{
\toksA=\expandafter{\the\toksA & 1 }
\toksB=\expandafter{\the\toksB & 2 }
\toksC=\expandafter{\the\toksC & 3 }
\toksD=\expandafter{\the\toksD & 4 }
}

\newcommand*{\charactercolumn}{
\toksA=\expandafter{\the\toksA & W }
\toksB=\expandafter{\the\toksB & X }
\toksC=\expandafter{\the\toksC & Y }
\toksD=\expandafter{\the\toksD & Z }
}

\newcommand{\printtable}{
\the\toksA \tabularnewline
\the\toksB \tabularnewline
\the\toksC \tabularnewline
\the\toksD \tabularnewline
}

\begin{document}

all columns:

\begin{tabular}{llll}
\charactercolumn
\numbercolumn
\printtable
\end{tabular}

characters only:

\begin{tabular}{lll}
\charactercolumn
\printtable
\end{tabular}

numbers only:

\begin{tabular}{lll}
\numbercolumn
\printtable
\end{tabular}

\end{document}


While this basically does what I want it to do, it only prints out the first line of each table. What am I doing wrong?

• as a side remark you have many space tokens in your macros and this will show as extra blanks in the tabular; it would be safer to add suitable % to avoid them; you can also prefix all your assignments with \global, for example \toksA=\expandafter{\the\toksA & 1 } becomes \global\toksA\expandafter{\the\toksA & 1 }. The = sign is optional and can be removed. With the \global it works. – user4686 Nov 8 '13 at 23:04

You are assigning the registers in the first cell of the table which is a local group. Using registers here doesn't really gain you anything over using macros, but if you want to do it you need to assign them before the table (just as you would if you used macros):

\documentclass{article}

\newtoks\toksA
\newtoks\toksB
\newtoks\toksC
\newtoks\toksD

\toksA={ foo & bar }
\toksB={ Foo & Bar }
\toksC={ fOo & bAr }
\toksD={ foO & baR }
}

\newcommand*{\numbercolumn}{
\toksA=\expandafter{\the\toksA & 1 }
\toksB=\expandafter{\the\toksB & 2 }
\toksC=\expandafter{\the\toksC & 3 }
\toksD=\expandafter{\the\toksD & 4 }
}

\newcommand*{\charactercolumn}{
\toksA=\expandafter{\the\toksA & W }
\toksB=\expandafter{\the\toksB & X }
\toksC=\expandafter{\the\toksC & Y }
\toksD=\expandafter{\the\toksD & Z }
}

\newcommand{\printtable}{
\the\toksA \tabularnewline
\the\toksB \tabularnewline
\the\toksC \tabularnewline
\the\toksD \tabularnewline
}

\begin{document}

all columns:

\charactercolumn
\numbercolumn
\begin{tabular}{llll}
\printtable
\end{tabular}

characters only:

\charactercolumn
\begin{tabular}{lll}
\printtable
\end{tabular}

numbers only: