我正在尝试使用以下解决方案建立表格数据构建表时,如何在 foreach 或条件(或其他组/环境)中使用与号 (&)?,但没有任何成功。
有一些我无法解决的扩展问题。我有一些基于列表内容声明的计数器,我需要在输出中输出这些计数器的值,因此尝试使用\eAddTableTokens
。下面的 MWE 得出:
Undefined control sequence.
\\ ->\let \reserved@e
\relax \let \reserved@f \relax \@ifstar {\let \reserv...
l.47 ^^I}
代码:
\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}
答案1
诸如\\
或 之类的命令\textbf
无法继续存在\edef
;您需要\protected@edef
:
\newcommand\eAddTableTokens[1]{%
\protected@edef\MyTempTableTokens{#1}%
\expandafter\AddTableTokens\expandafter{\MyTempTableTokens}%
}