完美运行的文档中有错误

完美运行的文档中有错误

这是我之前的一个问题的后续:考试课堂上编写题目

我最终成功地创建了一些可以自动完成我想要做的事情的东西。

首先,我将课堂考试归纳为

\ProvidesClass{examFull}[2016/11/09 v1.0]

\LoadClassWithOptions{exam}
\RequirePackage{etex}

\newcommand\insertQuestions{
\input{\jobname.exm}
\newwrite\examfile
\immediate\openout\examfile=\jobname.exm}

\AtEndDocument{
\immediate\closeout\examfile}

\newcommand\create@environment[1]{
\newenvironment{my#1}{%
\begin{#1}
\immediate\write\examfile{\string\begin{#1}}
}{%
\immediate\write\examfile{\string\end{#1}}
\end{#1}
}
}



\newcommand\create@item[1]{
\expandafter\newcommand\csname my#1\endcsname[2]{%
\csname #1\endcsname[##1] ##2
\immediate\write\examfile{\string\csname \space #1\string\endcsname[##1] \unexpanded##2}%
}
}

\create@environment{questions}
\create@environment{parts}
\create@environment{subparts}
\create@environment{subsubparts}
\create@item{question}
\create@item{part}
\create@item{subpart}
\create@item{subsubpart}

此类通过在开头添加 my 来定义新的类似考试的命令。这些命令排版其参数并将其存储在 \jobname.exm 中。

使用 \unexpanded 的主要原因是,数学模式命令可以在参数中使用,而无需在文件插入前进行扩展。该类仍需要一些改进,例如检查不存在的文件。

以下是其工作原理的 MWE:

\documentclass{examFull}
\usepackage{amsmath, amssymb}

\begin{document}

 \insertQuestions
 \clearpage
 \begin{myquestions}
  \myquestion{2}{This is a question $3\in\mathbb{R}$}

  \begin{solutionbox}{3in}
   This is a solution
  \end{solutionbox}

  \begin{myparts}
   \mypart{2}{This is a part}

   \begin{solutionbox}{3in}
   This is a solution
  \end{solutionbox}
  \end{myparts}
 \end{myquestions}
\end{document}

问题来了。运行两次将得到正确的文档: 输出

但是,我在 \myquestion 和 \mypart 行中收到“Missing {”错误,并且在这些行中也收到“Forbidden control serial”错误。有人知道出了什么问题吗?我该如何修复它?

答案1

\unexpanded##2 

应该

\unexpanded{##2}

相关内容