I want to define and expand macros in a \foreach
loop. The names of the macros depend on the iteration step. The macros I define are interpreted as strings rather than sets of numbers that I actually want them to be. Here is a minimal example that illustrates the issues more precisely.
\documentclass[english]{article}
\usepackage{verbatim}
\usepackage{tikz}
\newcommand\Stage{One,Two}
\newcommand\SubStageOne{2,3,4,6}
\newcommand\SubStageTwo{6,7,8}
\newcommand{\CurrentSubStage}{} % will be defined in each stage loop
\begin{document}
%
\foreach \stage in \Stage {
%
\renewcommand{\CurrentSubStage}{ \expandafter\csname SubStage\stage \endcsname }
%
The Macro replacement of {\tt CurrentSubStage} looks the same as Macro replacement of {\tt SubStage\stage}:
%
\begin{center}
\CurrentSubStage\ is the same as \expandafter\csname SubStage\stage \endcsname .
\end{center}
%
But really they are different: Consider first the output from looping over {\tt CurrentSubStage}:
\begin{itemize}
\foreach [count=\ii] \substage in \CurrentSubStage {
\item Substage number \ii\ is \substage \\
}
\end{itemize}
%
and then this output from looping over {\tt SubStageOne}:
%
\begin{itemize}
\foreach [count=\ii] \substage in \SubStageOne {
\item Substage number \ii\ is \substage
}
\end{itemize}
}
%
%
In the second \verb|\stage| iteration, i.e. ``Two'', I want to loop over \verb|\SubStageTwo| as in the \verb|\SubStageOne| that creates four rather than one item. But I do not know how to to call \verb|\Substage\stage| (This is why I created \verb|\CurrentSubStage|). My questions:
\begin{enumerate}
\item How can I define macro \verb|\CurrentSubStage| such that it does not treat the set of numbers as a single string so that the corresponding \verb|\substage| loop gives an iteration for each number?
\item Is there a possibility to do something like:\\ \verb|\foreach [count=\ii] \substage in \SubStage\stage { ...|?
\item I need to define an ``output'' macro at the end of each \verb|\stage| iteration: Something like \verb|\newcommand{\SubStageOutputOne}| in iteration One and \verb|\newcommand{\SubStageTwoOutput}| in iteration Two. (In my application, this output is a permutation of the set |\CurrentSubStage|. I want to ``save'' this permutation in a macro and call it later, at the end of the document.) In short, how can I include something like \verb|\newcommand{\SubStageOutput\stage}| at the end of each \verb|\stage| loop?
\end{enumerate}
%
[Final note: For my actual purpose, the set \verb|\Stage| contains 6 elements and each \verb|\SubStage| set contains between 3 and 10 elements each.]
%
\end{document}
\tt
is a TeX command which shouldn't be used with LaTeX. Use\texttt
instead please\edef\CurrentSubStage{\unexpanded\expandafter\expandafter\expandafter{\csname SubStage\stage\endcsname}}
(BTW: the\expandafter
in\expandafter\csname SubStage\stage \endcsname
is completely useless: it tries to expandS
before\csname
)