% Checks for current environment.

\def\AMCcurrentenv{document}
\def\ifenv#1{
    \def\AMC@tempenv{#1}%
    \ifx\AMC@tempenv\AMCcurrentenv
        \expandafter\@firstoftwo
    \else
        \expandafter\@secondoftwo
    \fi
}

% Error to display if \explain command is displayed outside question like environments

\def\AMC@error@explain{\PackageError{automultiplechoice}{
    Command \protect\explain\space can only be used inside\MessageBreak question like environments}{Something's gone wrong.\MessageBreak \space \space Try typing \space <return>\space to proceed, ignoring \protect\explain.
}}

\renewenvironment{question}[2][]{%
  \def\AMCcurrentenv{question}% New addition to question environment
  \global\def\AMCid@name{#2}\AMC@affecte{#2}{\AMCid@quest}%
  \ifAMC@calibration\AMCmessage{Q=\the\AMCid@quest}\fi%
  \AMCtype@multifalse\ifAMC@qbloc\noindent\begin{minipage}{\linewidth}\fi%
  \ifAMC@affichekeys\index{\texttt{#2}}\fi%
  \AMCbeginQuestion{\ifAMC@affichekeys\ifAMC@ensemble\AMC@qaff\ \fi[\texttt{#2}]\else\AMC@qaff\fi}{#1}%
  \AMCformulaire@dedansfalse\setcounter{AMC@ncase}{0}%
  \AMCmem@ireQ{\arabic{AMCquestionaff}}}%
{\ifAMC@qbloc\end{minipage}\vspace{\AMCinterBquest}\else\vspace{\AMCinterIquest}\fi\AMCmessage{FQ}}

% Localization text for Explanation

\def\AMC@loc@explain{\textit{\textbf{Explanation: }}}

% Definition of \explain command

\ifAMC@correchead
    \newcommand{\explain}[1]{
        \ifenv{question}{\par\noindent{\AMC@loc@explain #1}}{\AMC@error@explain}\vspace{1ex}
        }
\else \newcommand{\explain}[1]{\ifenv{question}{}{\AMC@error@explain}}
\fi
