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
\newcommand*{\leadingcolumns}{
\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}
\leadingcolumns
\charactercolumn
\numbercolumn
\printtable
\end{tabular}
characters only:
\begin{tabular}{lll}
\leadingcolumns
\charactercolumn
\printtable
\end{tabular}
numbers only:
\begin{tabular}{lll}
\leadingcolumns
\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?
%
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.