# TikZ \foreach loop with \csname macro list

I have a problem with a macro-defined list in a TikZ foreach loop. The macro is built by \csname...\endcsname. I've played around with multiple \expandafter or ##1 instead of #1, but with no success: The list of elements is not considered as of multiple elements then as a complete string.

Here is my MWE:

\documentclass[border=10pt]{standalone}

\usepackage{tikz}

% Macro to read in configuration parameters
\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

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


So, how can I loop throu the list of elements?

Thank you!

• When using a macro in a list then it is expanded just once to get to the actual list. Yours needs expanding several times (I think three: \ListUpper -> \expandafter\csname ... -> \documentupperlist -> Elem1,Elem2,Elem3). So you'll need to do a bit of pre-processing to shorten that sequence. Will your list contain macros itself or will it be just strings? If the latter, simplest might be to do a \edef\ListUpper{...} just before the \foreach. It depends slightly on how automated you want it to be. – Andrew Stacey Mar 4 at 12:58
• @AndrewStacey: the list contains no macros, just strings. I'm using the solution from Steven B. Segletes below, which works fine for me. – GerritKi Mar 4 at 14:04

\documentclass[border=10pt]{standalone}

\usepackage{tikz}

% Macro to read in configuration parameters
\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

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


From AUT.log:

• Once you have the \edef\tmpA{\ListUpper} then you don't need the other \def\tmpB stuff, you can go with \foreach \e in \tmpA. \foreach will do one expansion of a macro to get a list. – Andrew Stacey Mar 4 at 14:04
• I just saw your solution, and I'm going with that. I've putted a TikZ node inside the foreach loop and it works. Thanks a lot. – GerritKi Mar 4 at 14:11
• Thank you @AndrewStacey. I have revised the answer based on your comment. – Steven B. Segletes Mar 4 at 14:57