# Defining and expanding macros In loops

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 { ...|?
\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}

• Off-topic:\tt is a TeX command which shouldn't be used with LaTeX. Use \texttt instead please
– user31729
Sep 27, 2015 at 18:16
• You want something like \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 expand S before \csname) Sep 27, 2015 at 18:48
• Thanks, Clemens, can you explain the individual steps to me? Or provide useful links? Sep 28, 2015 at 13:37
• @clemens An answer? May 29, 2016 at 14:20

You want something like

\edef\CurrentSubStage{%
\unexpanded\expandafter\expandafter\expandafter
{\csname SubStage\stage\endcsname}%
}


That is, you need to expand to the value in \csname SubStage\stage\endcsname. \edef is a TeX command which completely expands everything in the macro definition before assigning it the the macro it defines. In your example actually

\edef\CurrentSubStage{\csname SubStage\stage\endcsname}


would suffice but the first version is a bit safer. How does it work? Expanding \unexpanded starts expanding what comes next up to the first opening brace. The first \expandafter expands the third \expandafter which expands \csname. So after one expansion cycle we get:

\unexpanded\expandafter{\SubStageOne}


Now the remaining \expandafter expands \SubStageOne and we get:

\unexpanded{2,3,4,6}


The \unexpanded then prevents further expansion in the \edef so \CurrentSubStage becomes 2,3,4,6. (Analoguous for the second step of the loop.)

Remarks:

• \tt is not really recommended in LaTeX. Better is to use \ttfamiy or in this case \texttt{...}, see "Correct" way to bold/italicize text? for reference.
• the \expandafter in \expandafter\csname SubStage\stage \endcsname in your code is completely useless: it tries to expand S before \csname but S is not expandable.

Complete code:

\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 {
\edef\CurrentSubStage{%
\unexpanded\expandafter\expandafter\expandafter
{\csname SubStage\stage\endcsname}%
}%
The Macro replacement of \texttt{CurrentSubStage} looks the same as Macro
replacement of \texttt{SubStage\stage}:
\begin{center}
\CurrentSubStage\ is the same as \csname SubStage\stage \endcsname .
\end{center}
But really they are different: Consider first the output from looping over
\texttt{CurrentSubStage}:
\begin{itemize}
\foreach [count=\ii] \substage in \CurrentSubStage {
\item Substage number \ii\ is \substage
}
\end{itemize}
and then this output from looping over \texttt{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}