I am trying to build up tabular data using the solution at How do I use the ampersand (&) inside a foreach or conditional (or other group/environment) when building tables?, but not having any success.
There is some expansion issue that I am not able to figure out. I have counters which are declared based on the content of a list and I need to output the value of these counters in the output and hence trying to use \eAddTableTokens
. The MWE below yields:
Undefined control sequence.
\\ ->\let \reserved@e
\relax \let \reserved@f \relax \@ifstar {\let \reserv...
l.47 ^^I}
Code:
\documentclass{article}
\usepackage{tikz}
%% This is based on:
%% https://tex.stackexchange.com/questions/165126
%%
\newcommand*{\MyTempTableTokens}{}%
\makeatletter
\newtoks\@tabtoks
%%% assignments to \@tabtoks must be global, because they are done in \foreach
\newcommand\AddTableTokens[1]{\global\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand\eAddTableTokens[1]{%
\edef\MyTempTableTokens{#1}%
\expandafter\AddTableTokens\expandafter{\MyTempTableTokens}%
}
%%% variable should always be operated on always locally or always globally
\newcommand*\ResetTableTokens{\global\@tabtoks{}}
\newcommand*\PrintTableTokens{\the\@tabtoks}
\makeatother
\newcommand*{\MyList}{ABC, DE, FG}%
\begin{document}
% Confirm basic functionality
\ResetTableTokens%
\foreach \Entry in \MyList {%
\AddTableTokens{22 & 44 \\}%
}%
\begin{tabular}{ll}
\multicolumn{2}{c}{Basic Testing} \\
\PrintTableTokens
\end{tabular}%
%%% Processing:
%%% Counters get declared and appropriate values are computed,
%%% but for MWE these are all just a fixed number.
\foreach \Entry in \MyList {%
\newcounter{Count\Entry}%
\setcounter{Count\Entry}{77}%
}%
%%%
%%% Printing
\ResetTableTokens%
\foreach \Entry in \MyList {%
\edef\TempCountExpanded{\arabic{Count\Entry}}%
\eAddTableTokens{\Entry & \TempCountExpanded \\}%
}%
\begin{tabular}{ll}
\multicolumn{2}{c}{Actual Use Case} \\
\PrintTableTokens
\end{tabular}%
\end{document}
\\
through an\edef
.