我在 TikZ foreach 循环中遇到了宏定义列表的问题。宏由\csname
...构建\endcsname
。我尝试过使用多个\expandafter
或 ##1 代替 #1,但没有成功:元素列表不被视为多个元素,而被视为完整的字符串。
这是我的 MWE:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
% Macro to read in configuration parameters
\newcommand*\ReadCfg[3]{%
\expandafter\xdef\csname#1#2list\endcsname{#3}%
}
% Provide list for upper positions (this macro will be looped by \foreach)
% \jobname is 'AUT'
\providecommand\ListUpper{%
\expandafter\csname\jobname upperlist\endcsname%
}
% This configuration will be later provided by external file
\ReadCfg{AUT}{upper}{Elem1,Elem2,Elem3}
\begin{document}
\begin{tikzpicture}
\foreach \e in \ListUpper {
\typeout{***\space\e} % \e is interpreted as "Elem1,Elem2,Elem3" instead of single elements
}
\end{tikzpicture}
\end{document}
那么,我如何循环遍历元素列表?
谢谢你!
答案1
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
% Macro to read in configuration parameters
\newcommand*\ReadCfg[3]{%
\expandafter\xdef\csname#1#2list\endcsname{#3}%
}
% Provide list for upper positions (this macro will be looped by \foreach)
% \jobname is 'AUT'
\providecommand\ListUpper{%
\csname\jobname upperlist\endcsname%
}
% This configuration will be later provided by external file
\ReadCfg{AUT}{upper}{Elem1,Elem2,Elem3}
\begin{document}
\begin{tikzpicture}
\edef\tmpA{\ListUpper}
\foreach \e in \tmpA {
\typeout{***\space\e} % \e is interpreted as "Elem1,Elem2,Elem3" instead of single elements
}
\end{tikzpicture}
\end{document}
来自AUT.log: