Am having difficulty determining the correct syntax for using
\csuse from
the etoolbox package
with a \foreach.
I have a list, and for each element in this list I need to create a new list. To simplify the MWE, I used \IfStrEq to define the new list using \csgdef.
The problem in in the second \foreach where I have an additional curly brace:
{\csuse{ContentFor\x}}
which should not be needed as per TikZ \foreach loop with macro-defined list.
If I leave out the {} in the second \foreach I get:
Argument of \csuse has an extra }
With the {} it compiles and I get:

However, the desired output is:

Code:
\documentclass{article}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage{pgffor}
\newcommand*{\MyList}{ABC,XYZ}%
\begin{document}
%% For each element of \MyList I need to create a new list.
\foreach \x in \MyList {%
% In my actual use case the list that gets defined for each element of \MyList
% is based on timestamps of files, but as that is not relevant to this
% particular issue I left out that complexity, to make this test case easier to read.
\IfStrEq{\x}{ABC}{%
\csgdef{ContentFor\x}{FileA,FileB,FileC}%
}{%
\csgdef{ContentFor\x}{FileX,FileY,FileZ}%
}%
}%
% Now, only after I have ALL the lists created, I want to print EACH list
% for each element of \MyList.
\foreach \x in \MyList {%
\par\x=
\foreach \y in {\csuse{ContentFor\x}} {% <--- The {} around \csuse{} should NOT be necessary.
\par\hspace*{1.0cm}\y
}%
}%
%%% This is the expected output:
% \par ABC=
% \foreach \y in {FileA,FileB,FileC} {%
% \par\hspace*{1.0cm}\y
% }%
% \par XYZ=
% \foreach \y in {FileX,FileY,FileZ} {%
% \par\hspace*{1.0cm}\y
% }%
\end{document}

\edef\ContentForThisX{\csuse{ContentFor\x}}followed by\foreach \y in \ContentForThisXworks just fine. Is there an inherent problem in this for the case of no "dangerous" content? – Peter Grill May 05 '14 at 00:48\ContentForABC\fi, not\ContentForABC. ■ (it's not recommended to rely on "undocumented behavior" (i.e. the macro requires exactly 2 expansion step) anyway. Could use csname or expl3's:cargument type) ■ (see the comment above for the edef, it's much cleaner, although I don't know if it's really less efficient or not -- I suspect not) ■ The problem is not just that the single expansion does not give the desired result, but that the initial content is not a single token. For example\def\f #1{#1,2}\foreach \x in \f{1}{\x}won't work – user202729 Jun 18 '22 at 00:51