\documentclass{memoir} \usepackage{comment} \chapterstyle{bianchi} \newoutputstream{backansout} \newenvironment{Exercises} {\openoutputfile{\jobname.backans}{backansout}} {\closeoutputstream{backansout}} \newcounter{exercise}[section] \renewcommand{\theexercise}{\arabic{exercise}} \newenvironment{exercise}% {\refstepcounter{exercise} \par\noindent\textbf{\theexercise}.} {\par} \newcommand{\printbackanswers}{% \subsection{Backanswers} \input{\jobname.backans}} \newenvironment{backanswer}% {\ifodd\theexercise{% \addtostream{backansout}{\protect\begin{oddexercise}{\theexercise}}% }\else{% \addtostream{backansout}{\protect\begin{evenexercise}{\theexercise}}% }\fi \begin{writeverbatim}{backansout}} {\ifodd\theexercise{% \addtostream{backansout}{\protect\end{oddexercise}}% }\else{% \addtostream{backansout}{\protect\end{evenexercise}}% }\fi} \newenvironment{oddexercise}[1]{\par\noindent\textbf{#1.}}{} \newenvironment{evenexercise}[1]{\par\noindent\textbf{#1.}}{} %\includecomment{oddexercise} \excludecomment{evenexercise} \begin{document} \chapter{The Whole Numbers} \section{Introduction to the Whole Numbers} Now is the time for all good men to come to the aid of their country. Now is the time for all good men to come to the aid of their country. Now is the time for all good men to come to the aid of their country. Now is the time for all good men to come to the aid of their country. Now is the time for all good men to come to the aid of their country. Now is the time for all good men to come to the aid of their country. \subsection{Exercises} \begin{Exercises} \begin{exercise} What is the sum of $-2$ and $-15$? \begin{backanswer} $-17$ \end{writeverbatim} \end{backanswer} \end{exercise} \begin{exercise} What is the sum of 2 and 15? \begin{backanswer} $17$ \end{writeverbatim} \end{backanswer} \end{exercise} \begin{exercise} What is the sum of $-2$ and 15? \begin{backanswer} $13$ \end{writeverbatim} \end{backanswer} \end{exercise} \begin{exercise} What is the sum of $-2$ and $-15$? \begin{backanswer} $-17$ \end{writeverbatim} \end{backanswer} \end{exercise} \begin{exercise} What is the sum of 2 and 15? \begin{backanswer} $17$ \end{writeverbatim} \end{backanswer} \end{exercise} \begin{exercise} What is the sum of $-2$ and 15? \begin{backanswer} $13$ \end{writeverbatim} \end{backanswer} \end{exercise} \begin{exercise} What is the sum of $-2$ and $-15$? \begin{backanswer} $-17$ \end{writeverbatim} \end{backanswer} \end{exercise} \begin{exercise} What is the sum of 2 and 15? \begin{backanswer} $17$ \end{writeverbatim} \end{backanswer} \end{exercise} \begin{exercise} What is the sum of $-2$ and 15? \begin{backanswer} $13$ \end{writeverbatim} \end{backanswer} \end{exercise} \end{Exercises} \subsection{Answers} \input{\jobname.backans} \end{document}