2

This MWE works perfectly when the I comment out equation in the teacher environment. With that equation in place I get this error:

(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}

The code:

\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}

Some history. A previous problem with the answers package led me to this question: Incomplete \iffalse error using answers package

In fact that was the first error I saw when I encountered this problem. That error message was replaced by the one above when I made my example minimal.

2

You can substitute the \protected@iwrite mechanism with a stronger one that has the defect that it expands nothing. So if you want to add titles to the teacher's notes you have to work in two steps.

\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}

enter image description here

2

The problem here is that \BODY gets expanded before being written to the file. The result isn't plain text, and disaster follows. Protecting doesn't help, because the meaning of \BODY is lost at \end{teacher}. One way around this is to store the bodies of the environments using globally defined macros called \tchI, \tchII, \tchIII, etc. These can be protected, and written to the file.

\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}
  • Both your answer and @egreg s work. I've upvoted both. I accepted his because it seems just a little less magical to me. – Ethan Bolker Jun 11 '15 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.