1

My environment defined by \NewEnviron macro contains a macro \mymacro. How can to delete the macro \mymacro in the \BODY macro, when it executes?

\documentclass{article}
\NewEnviron{myenviron}{\newcommand\mymacro[2]{\def\textone{##1}}{\def\texttwo{##2}}} \gdef\result{\BODY}}
\begin{document}
\begin{myenviron}
Some text here
\mymacro{Textone}{Texttwo}
\end{myenviron}
\end{document}

How can I delete the \mymacro in the \result macro? I have try by many ways, but I cannot!

Now I want to add more detail my problem so that the experts can help me.

I have a lot of multiple choice questions in of the following structure:

\begin{myenviron}
Question
\choice{\True First answer}{Second answer}{Third answer}{Fourth answer}
\sulution{The detail solution.}
\end{myenviron}

Here \True means that is the correct answer.

I want to convert to the multi environment of the moodle package to upload to my moodle website.

I have tried to do as follows:

\documentclass[12pt,a4paper]{article}
\usepackage[final]{moodle}
\usepackage{ifthen}
\usepackage{etoolbox}
\usepackage{environ}
\usepackage{xstring}
\moodleregisternewcommands

\makeatletter \newcounter{c@ex}% \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

\NewEnviron{myenviron}{% \stepcounter{c@ex} \setcounter{ans@wer}{1} }% \AtEndEnvironment{myenviron}{% \newcommand{\choice}[4]{ \newcommand{\ansone}{#1}\hide@text{\begin@ans #1} \newcommand{\anstwo}{#2}\hide@text{\begin@ans #2} \newcommand{\ansthree}{#3}\hide@text{\begin@ans #3} \newcommand{\ansfour}{#4}\hide@text{\begin@ans #4}}% \newcommand{\solution}[1]{\newcommand{\feed@back}{#1}}% \hide@text{\xa\global\xa\def\xa\result\xa{@body}} \ifnum\the\value{correct@answer}=1% \begin{multi}[numbering=ABCD,penalty=0,feedback={\feed@back}]{\exname\thec@ex}
@body \item* \ansone% \item \anstwo% \item \ansthree% \item \ansfour% \end{multi} \else \ifnum\the\value{correct@answer}=2% \begin{multi}[numbering=ABCD,penalty=0,feedback={\feed@back}]{\exname\thec@ex} @body \item \ansone% \item* \anstwo% \item \ansthree% \item \ansfour% \end{multi} \else \ifnum\the\value{correct@answer}=3 \begin{multi}[numbering=ABCD,penalty=0,feedback={\feed@back}]{\exname\thec@ex} @body \item \ansone% \item \anstwo% \item* \ansthree% \item \ansfour% \end{multi} \else \ifnum\the\value{correct@answer}=4 \begin{multi}[numbering=ABCD,penalty=0,feedback={\feed@back}]{\exname\thec@ex} @body \item \ansone% \item \anstwo% \item \ansthree% \item* \ansfour%
\end{multi} \else \relax \fi \fi \fi \fi }% \makeatother

\begin{document} \begin{quiz}{My first test} \begin{myenviron} This is my question.

\choice {\True Answer one} {Answer two} {Answer three} {Answer four} \solution{This is a detail answer.} \end{myenviron}

\end{quiz} \end{document}

It work fine and output correctly in pdf file. However, in .xml file, the body contains the \choice and \solution macros. This is the body containing in .xml file:

<text><![CDATA[<p>This is my question. <BR/> \newcommand {Answer one}{Answer one}\hide@text {\begin@ans Answer one} \newcommand {Answer two}{Answer two}\hide@text {\begin@ans Answer two} \newcommand {Answer three}{Answer three}\hide@text {\begin@ans Answer three} \newcommand {Answer four}{Answer four}\hide@text {\begin@ans Answer four} \newcommand {This is a detail answer.}{This is a detail answer.}</p>]]></text>

while I need only This is my question.

phchon
  • 51
  • 1
    Please extend your example to a proper document (there is no document class) – daleif Aug 29 '23 at 14:09
  • 1
    Whatdo you want to delete? just \mymacro or all of \mymacro{Textone}{Texttwo} ? do you want \textone and \texttwo to be defined? (they will not be if you delete \mymacro?) – David Carlisle Aug 29 '23 at 14:12
  • I'm not sure why you want to delete a macro that's about to become undefined anyway. How do you know it isn't being deleted? – Teepeemm Aug 29 '23 at 14:41
  • 1
    @Teepeemm I guess the OP wants to use \result after the environment but it's not at all clear what that definition should be – David Carlisle Aug 29 '23 at 14:43
  • 1
    First of all, you need \xdef\result{\BODY} (plus removing some extra braces). \BODY is undefined outside the environment. Note that \textone and \texttwo are local and wil be lost. – John Kormylo Aug 29 '23 at 14:48
  • I have a data base with many data in the structure of myenviron. I want to convert them to another environment or write to a .tex file in subtable structure. So I want the Some text here of the environment, because it is easy to catch the \textone and \texttwo by set the global macros. – phchon Aug 29 '23 at 15:00
  • So you want \result to contain only that? What should the output of the actual environment be and what are the other definitions supposed to do? – cfr Aug 29 '23 at 15:06
  • What does your database look like? There are packages that specialize in working with csv files. It's still not clear what you're wanting to happen. – Teepeemm Aug 29 '23 at 15:09
  • I have a database of the multiple choice questions that the question is of the structure \begin{myenviron} Question.... \choice{first answer}{\True second answer}{third answer}{fourth answer} \solution{The detail solution} \end{myenviron}. I want to convert them to the structure of multi environment of moodle package. – phchon Aug 29 '23 at 15:12
  • See also https://tex.stackexchange.com/questions/51239/verbatim-in-environ-s-body – John Kormylo Aug 29 '23 at 15:15
  • 2
    Can you edit your question to show, more specifically to your application, what myenviron looks like to begin with and what you would like it to have looked like, given the appropriate processing? – Steven B. Segletes Aug 29 '23 at 15:39

0 Answers0