I'm building an exercise database. The exercises are stored in the form

```
\begin{Exer}
 Text of the exercice
  \item first answer
  \item* second answer
 \end{Exer}

```

where the \* denotes the correct answer

And from this I need to define macros \text, \firstanswer, \secondanswer, \correctanswer. For this I found packages environ, and listofitems, thank to [this answer](https://tex.stackexchange.com/questions/540232/how-to-use-getitems-latex-package/540235?noredirect=1#540235). But now, I need to do it with a counter conting, namely I need to define thing as "\textN", depending on N.

I think that my problem is an expansion problem, indeed my macro \correctanswerN works, while the very same defined \textN gives me errors. I guess that the fact is that \correctanswer is basically a number, while \text is a macro. (and if I use gdef instead of edef, I get no errors, but the output is wrong as things are not "expanded" so successive calls of Exer erase my data)

I tried to put some \expandafter\noexpand around (as I read in the listofitem doc that "items need a double expansion") but without succes.

Anyway, here my attempt (you may go directly to lines where \text\roman{\num} is defined)

```
\documentclass{amsart}
\usepackage{listofitems,environ}
\newcounter{num}
\setcounter{num}{0}

\NewEnviron{Exer}{%
  \stepcounter{num}% 
  \setsepchar{\item/*}%
  \greadlist*\myitem{\BODY}%
  \gdef\correct{0}%
  \foreachitem\z\in\myitem[]{%
    \ifnum\listlen\myitem[\zcnt]>1\relax
    \xdef\correct{\the\numexpr\zcnt-1}\fi
}%      
\expandafter\xdef\csname text\roman{num}\endcsname{\myitem[1]}%
%
\expandafter\xdef\csname firstanswer\roman{num}\endcsname{\edef\tmp{\listlen\myitem[2]}%
  \myitem[2,\tmp]}%
%
\expandafter\xdef\csname secondanswer\roman{num}\endcsname{\edef\tmp{\listlen\myitem[3]}%
  \myitem[3,\tmp]}%
%
\expandafter\xdef\csname correctanswer\roman{num}\endcsname{\correct}%
}

\begin{document}
\begin{Exer}
First exercise
\item First answer
\item* Second answer
\end{Exer}

\begin{Exer}
SECOND exercise
\item* A
\item B
\end{Exer}

The first exercise:

 \texti

 possibilities: 1) \firstansweri, 2) \secondansweri

 The correct answer is  \correctansweri

\

 Second exercise

 \textii

 Possibilities: 1) \firstanswerii, 2) \secondanswerii

 The correct answer is \correctanswerii

\end{document}

```