I have asked a question at How to delete a command in the \BODY macro of environment defined by \NewEnviron about the problem when I try to convert my ex environment to the multi environment of moodle package.
I have tried to solve my problem since last weekend, and now, I have solved almost everything. It works fine and give output .pdf file and .xml file correct result.
However, it takes errors with display math mode $$...$$ and \[...\] and \begin{center}...\end{center} environment.
Here is my code
\documentclass[12pt,a4paper]{article}
\usepackage[tikz]{moodle}
\usepackage{ifthen}
\usepackage{etoolbox}
\usepackage{environ}
\usepackage{xstring}
\usepackage{amsmath,amssymb,yhmath,mathrsfs,fontawesome}
\usepackage{fancyhdr,enumerate,tkz-euclide,tikz-3dplot,tikz,tkz-tab}
\moodleregisternewcommands
\makeatletter
\newcounter{c@ex}%
\setcounter{c@ex}{0}
\newcounter{ans@wer}%
\newcounter{correct@answer}%
\newcommand{\True}{}%
\newcommand\exname{MC}
%% Define the command to hide the text body of the environment.
\newlength{\trashlength}%
\def\hide@text#1{%
\settowidth{\trashlength}{#1}
}
%% Define the command to find the correct answer.
\def\begin@ans{@ifnextchar\True
{\setcounter{correct@answer}{\theans@wer}}{\stepcounter{ans@wer}}%
}
\environbodyname@body
\htmlregister@body
\htmlregister\final@result
\htmlregister\ans@one
\htmlregister\ans@two
\htmlregister\ans@three
\htmlregister\ans@four
\htmlregister\feed@back
\htmlregister\envbody
\newcommand{\choice}[4]{
\gdef\ans@one{#1}
\gdef\ans@two{#2}
\gdef\ans@three{#3}
\gdef\ans@four{#4}
\gdef\del@choice{\choice
{#1}
{#2}
{#3}
{#4}
}%
}%
\newcommand{\loigiai}[1]{
\gdef\feed@back{#1}
\gdef\del@feedback{\loigiai{#1}}
}%
\NewEnviron{ex}{%
}%
\AtEndEnvironment{ex}{%
\stepcounter{c@ex}
\hide@text{@body}
%% Delete the macro \choice and \loigiai in the body of environment
\hide@text{
\global\let@result@body
\expandarg
\StrDel{@result}{\del@choice}[\re@sult]
\StrDel{\re@sult}{\del@feedback}[\re@@sult]
\global\let\final@result\re@@sult
}
% \let\envbody\noexpand{\funal@result}
%% Finding the correct answer
\setcounter{ans@wer}{1}
\hide@text{\expandafter\begin@ans\ans@one}
\hide@text{\expandafter\begin@ans\ans@two}
\hide@text{\expandafter\begin@ans\ans@three}
\hide@text{\expandafter\begin@ans\ans@four}
%% Write the data in `milti' environment
\def\ex@name{\exname\thec@ex}
\ifnum\the\value{correct@answer}=1%
\begin{multi}[numbering=ABCD,penalty=0,feedback={\feed@back}]{\ex@name}
\final@result
\item* \ans@one%
\item \ans@two%
\item \ans@three%
\item \ans@four%
\end{multi}
\else
\ifnum\the\value{correct@answer}=2%
\begin{multi}[numbering=ABCD,penalty=0,feedback={\feed@back}]{\exname\thec@ex}
\final@result
\item \ans@one%
\item* \ans@two%
\item \ans@three%
\item \ans@four%
\end{multi}
\else
\ifnum\the\value{correct@answer}=3
\begin{multi}[numbering=ABCD,penalty=0,feedback={\feed@back}]{\exname\thec@ex}
\final@result
\item \ans@one%
\item \ans@two%
\item* \ans@three%
\item \ans@four%
\end{multi}
\else
\ifnum\the\value{correct@answer}=4
\begin{multi}[numbering=ABCD,penalty=0,feedback={\feed@back}]{\exname\thec@ex}
\final@result
\item \ans@one%
\item \ans@two%
\item \ans@three%
\item* \ans@four%
\end{multi}
\else
\relax
\fi
\fi
\fi
\fi
}%
\makeatother
\begin{document}
\begin{quiz}{My first test}
\begin{ex}
This is my question. $\dfrac{a}{b}$
\choice
{\True Answer one}
{Answer two}
{Answer three}
{Answer four}
\loigiai{This is a detail answer.}
\end{ex}
\end{quiz}
\end{document}
I don't know where is wrong, when I use display math mode in the body of my question I get errors (of course it is used right syntax)
! Missing $ inserted.
<inserted text>
$
In addition, when I use \begin{center}...\end{center} I get the error
! LaTeX Error: Something's wrong--perhaps a missing \item.
Please help me to fix them!

\[..\](and$$...$$) and instead use$...$. Use\centeringinstead ofcenterenvironment. Thus,\centering $...$will give a centered equation. – Steven B. Segletes Sep 02 '23 at 14:58%% Define the command to hide the text body of the environment.that is not what\hide@textdoes, it typesets a space, and sets a length register, and gives errors on vertical material like a paragraph or display math. why do you need to measure the width???? – David Carlisle Sep 03 '23 at 09:20c@exas a counter name is not exactly wrong but rather confusing as latex usesc@as a prefix for count registers, so\newcounter{ex}allocates\c@exand\newcounter{c@ex}allocates\c@c@ex– David Carlisle Sep 03 '23 at 09:26