7

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}
3

1 Answer 1

8

Commands such as \\ or \textbf don't survive \edef; you need \protected@edef:

\newcommand\eAddTableTokens[1]{%
  \protected@edef\MyTempTableTokens{#1}%
  \expandafter\AddTableTokens\expandafter{\MyTempTableTokens}%
}
1
  • Well, this certainly fixed it for the MWE. But, in my actual use case I had to eliminate the \\ in the call to \eAddTableTokens and instead add the \\ to definition of \eAddTableTokens. But, I am unable to determine what the actual difference between my actual use case and MWE is that could be causing this. May 8, 2014 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.