答案包 + 环境包 + 方程式无法编译

答案包 + 环境包 + 方程式无法编译

当我在教师环境中注释掉时,此 MWE 可以完美运行equation。有了这个等式,我得到了这个错误:

(C:\Users\Ethan.Bolker\AppData\Roaming\MiKTeX\2.9\tex\latex\trimspaces\trimspac
es.sty)) 
Output from handle answersout going to answersout.tex
! Missing \endcsname inserted.
<to be read again> 
                   \csname\endcsname
l.30 \end{teacher}

代码:

\documentclass{article}

\usepackage{answers}
\usepackage{amsmath}
\usepackage{environ}

\newcommand{\answersout}{answersout}

\NewEnviron{teacher}{%
\Writetofile{\answersout}{\BODY }
}

\begin{document}
\Opensolutionfile{\answersout}

An equation in the master file:
%
\begin{equation}
2+2 = 4
\end{equation}

\begin{teacher}
In answersout: expand a macro: \LaTeX.
Try an equation:
% comment out the next three lines and the document compiles
\begin{equation}
2+2 = 4
\end{equation}
\end{teacher}

\Closesolutionfile{\answersout}
\input{\answersout}
\end{document}

一些历史。之前软件包的一个问题answers让我想到了这个问题: 使用答案包时出现不完整的 \iffalse 错误

事实上,这是我遇到此问题时看到的第一个错误。当我将示例简化后,该错误消息被上面的错误消息所取代。

答案1

你可以用\protected@iwrite一个更强大的机制来代替它,但它的缺陷是它会膨胀没有什么。因此,如果您想在教师笔记中添加标题,您必须分两个步骤进行。

\documentclass{article}

\usepackage{answers}
\usepackage{amsmath}
\usepackage{environ}

\newcommand{\answersout}{answersout}

\newcounter{teacher}

\makeatletter
\NewEnviron{teacher}{%
  \stepcounter{teacher}%
  (See teacher note \theteacher)
  \Writetofile\answersout{%
    \protect\subsection*{Teacher's note \theteacher}%
  }
  \begingroup
  \def\protected@iwrite##1##2##3{\immediate\write##1{##3}}%
  \Writetofile\answersout{\unexpanded\expandafter{\BODY}}%
  \endgroup
}
\makeatother

\begin{document}
\Opensolutionfile{\answersout}

An equation in the master file:
\begin{equation}
2+2 = 4
\end{equation}

\begin{teacher}
In answersout: expand a macro: \LaTeX.
Try an equation:
\begin{equation}
2+2 = 4
\end{equation}
\end{teacher}

\Closesolutionfile{\answersout}

\section*{Teacher's notes}
\input{\answersout}
\end{document}

在此处输入图片描述

答案2

这里的问题是\BODY在写入文件之前会进行扩展。结果不是纯文本,灾难随之而来。保护无济于事,因为\BODY在处丢失了的含义。解决此问题的一种方法是使用全局定义的宏(称为、、等\end{teacher})存储环境的主体。这些可以受到保护,并写入文件。\tchI\tchII\tchIII

\documentclass{article}

\usepackage{answers}
\usepackage{environ}

\newcommand{\answersout}{answersout}

\newcounter{tchcount} 
\setcounter{tchcount}{0}

\NewEnviron{teacher}{%
\stepcounter{tchcount}
\global\expandafter\let\csname tch\Roman{tchcount}\endcsname\BODY
\Writetofile{\answersout}{\expandafter\protect\csname tch\Roman{tchcount}\endcsname}
}

\begin{document}
\Opensolutionfile{\answersout}

\begin{teacher}
Try an equation:
\begin{equation}
2+2 = 4
\end{equation}
\end{teacher}

\begin{teacher}
And another
\begin{equation}
1+1 = 2
\end{equation}
\end{teacher}

\Closesolutionfile{\answersout}
\input{\answersout}
\end{document}

相关内容