2

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

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

Thank you!

2
  • 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. Commented Mar 4, 2021 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
    Commented Mar 4, 2021 at 14:04

1 Answer 1

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}

From AUT.log:

enter image description here

3
  • 1
    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. Commented Mar 4, 2021 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
    Commented Mar 4, 2021 at 14:11
  • Thank you @AndrewStacey. I have revised the answer based on your comment. Commented Mar 4, 2021 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .