# Wrap a command with two arguments inside an environment

I know this question looks like this one but the solutions given there won't work for me.

I'm trying to replace the \element{foo}{bar} macro from automultiplechoice to get something like

\begin{qcm}{foo}
bar
\end{qcm}


I have tried the environ package, the document compiles but the question defined this way is ignored when using \restitutegroup. Here is my MWE:

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{environ}
\usepackage{automultiplechoice}
\usepackage{amsmath}
\usepackage{amsfonts}

\NewEnviron{qcm}[1]{\element{#1}{\BODY}}
\begin{document}

\begin{qcm}{test}
\begin{question}{foo}
foo
\end{question}
\end{qcm}

\element{test}{
\begin{question}{bar}
bar
\end{question}
}

\begin{examcopy}[2]
\insertgroup{test}
\clearpage
\end{examcopy}
\end{document}


Only the question "bar" is restituted.

Any clue would be greatly appreciated !

Within an environment, the scope of definitions are local unless explicitly made \global. So, any definitions made within qcm may be void if not handled appropriately. This is the case with \element - it creates a token list whose definition (or contents) doesn't survive the group.

The example below makes the definition \global, as well as ensuring that \BODY survives the end of qcm.

\documentclass{article}

\usepackage{environ}
% https://github.com/bdemeshev/pr201/blob/master/prob_exams_collection/automultiplechoice.sty
\usepackage{automultiplechoice}

\NewEnviron{qcm}[1]{%
\def\arg{#1}%
\expandafter\element\expandafter\arg\expandafter{\BODY}}

\makeatletter
\renewcommand{\element}[2]{%
\AMC@prepare@element{#1}%
\global\csname #1@\romannumeral\AMCtok@k\endcsname={#2}%
}
\makeatother

\begin{document}

\begin{qcm}{test}
\begin{question}{foo}
foo
\end{question}
\end{qcm}

\element{test}{%
\begin{question}{bar}
bar
\end{question}
}

\begin{examcopy}[2]
\insertgroup{test}
\clearpage
\end{examcopy}

\end{document}