\documentstyle[ans,11pt]{article}
% \input smalbold
\pagestyle{empty}
\makeanswers\artsec
\makesolutions
\emergencystretch10pt
\newcommand{\setpage}[1]{\setcounter{page}{#1}}
 
% The following arcane code defines bkslsh to be a textmode
% backslash.  In \LaTeX, \backslash defines backslash for mathmode
% but it doesn't look right in text mode.
% This definition gives the right size backslash for any Computer
% Modern size.
{\catcode`|=0\catcode`\\=\other
|def|bsl{{|tt\}}
|catcode`\=0 \global\let\bkslsh=|bsl}

\def\cmd#1{{\tt\bkslsh #1}}

\title{Exercises and answers for \LaTeX
\footnote{Copyright \copyright\number\year\ by Charles Frederick
Wells}\\
\normalsize Revision 25 February 1994}
\author{Charles Wells \\ Department of Mathematics \\
Case Western Reserve University \\ University Circle \\
Cleveland, OH 44106-7058 \\ USA\\ {\tt cfw2@po.cwru.edu}}

\begin{document}
\maketitle

\section{Description}\label{dessec}

{\tt Ans.sty} is a \LaTeX\ style file.  
It allows you to write exercises and their answers together in a
\LaTeX\ document.  When composed, the answers will appear in a
separate chapter or section elsewhere in the document (you
determine where they appear). 

{\tt Ans.sty} allows several variations.  In the current
release, you can use {\tt article.sty} or style files derived
from {\tt article.sty} and have exercises in a subsection at the end
of each section; or you can use {\tt book.sty} and have
exercises at the end of each section or at the end of each
chapter.  I hope to have a version that allows exercises
interspersed throughout the document (as in The \TeX book) in a
later version.

This document is written using {\tt article.sty}.  Below are
some exercises.  The ones that are marked with a dot have
answers on page~\pageref{ansec}.   Some of those not marked with
a dot have answers in the booklet called ``Solutions to
exercises not answered in text'' that is generated with
this article.
Later in the document
you will find more elaborate exercises along with their source.

\exercises

\exera This the first exercise of section~\ref{dessec}.
\answer{This is the answer to the first exercise of
section~\ref{dessec}.}

\exera This is the second exercise of section~\ref{dessec}.
\answer This is the answer to the second exercise of
section~\ref{dessec}.

\exer This has an answer in the Solution Manual.
\solution This is an answer in the Solution Manual.


\eexer

\section{Using ans.sty}\label{seca}

\subsection{At the beginning of the document}

The beginning of your document should look like this:

\begin{verbatim}
\documentstyle[ans]{article}
\makeanswers\artsec %% Use \bkchap or \bksec in book style
%% Other preamble commands you might want here
\begin{document}
\end{verbatim}
If you want a booklet printed with answers to exercises not
answered in the article, you should also put \cmd{makesolutions}
in the preamble.

\subsubsection{Options}
One of the options commands 
must come after \cmd{makeanswers}.  The options commands are
\begin{description}
\item[\cmd{artsec}]  This is used in article style.  It allows
(no more than one) subsection of exercises in each section.
\item[\cmd{artscattered}] {\bf Not currently implemented.}
For article style.  Allows exercises
to be interspersed throughout the document, as in The \TeX book.
\item[\cmd{once}] 
This must be used in article style and allows
just one section of exercises for the whole document.  
See~\ref{once} below.  You cannot make a solution manual in this
style.  The file {\tt onceexam.tex} contains an example of its
use.
\item[\cmd{bkchap}] Used with {\tt book.sty}, allowing one
section of exercises per chapter.
\item[\cmd{bksec}] Used with {\tt book.sty}, allowing one
section of exercises per section.
\item[\cmd{bkscattered}] {\bf Not currently implemented.}
Like \cmd{artscattered} for 
{\tt book.sty}.  
\end{description}

If you omit the \cmd{makeanswers}
command, the document will
compose without complaint but the answers won't appear at the
end.  This may be useful for draft purposes.  

If you include \cmd{makeanswers}, the option command is
mandatory.
If you omit it, you will get an error message the
first time you include an exercise.

\subsubsection{Using \protect\bkslsh once}\label{once}
This option is for documents with just one section of exercises.
It may be useful for class handouts whose answers are printed
separately to be handed out later.

It is used a little differently from the others.
In the other options, \cmd{exercises} puts a section or
subsection heading on each
section of exercises and writes the appropriate heading for the
answers to that set to the answer file.  In this option,
\cmd{exercises} does neither.  You must write the title of the
exercise section if you want one, and you must write the title
of the answer section, putting in a page break and setting the
page number to 1 if that is what you want.  The document
``onceexam.tex'' illustrates the use of the \cmd{once} option.

% \subsubsection{Scattered exercises} I have no current plans to
% implement \cmd{artscattered} and \cmd{bkscattered} because I
% don't particularly like that style.  I have some thoughts on it
% for anyone who wants to try it:  You could have an exercise
% environment based on \cmd{newtheorem} (page 58 of Lamport's book
% on \LaTeX) or you could make each exercise a subsection in book
% style or a subsubsection in article style.  

\subsection{To print the answers}

At the place in the document where you want the answers printed,
put 

\begin{verbatim}
\doanswers
\end{verbatim}

You can put this any place after the last
batch of exercises.
This will produce an unnumbered section
titled ``Solutions'',
followed by the solutions to the problems
in each section.  (The \cmd{once} option will not produce a
section title, although it will print the answers.)
When using {\tt book.sty}, the section of answers will be
formatted as an unnumbered chapter, therefore starting on a new
page. In {\tt article.sty},
the answer section will not start on a new page.
If you want it to, type 
\cmd{newpage} before \cmd{doanswers}.  If you want the page
numbers to start over, type 

\begin{verbatim}
\newpage
\setcounter{page}{1}
\end{verbatim}

You could then bind the answer section as a separate document.

The command \cmd{dosolutions} prints the answers you wrote to
exercises that are not marked as having answers.  Normally, you
would set the page counter to 1 as above
before invoking \cmd{dosolutions}. 
This article does this, as you can see by examining the source
code.


\subsection{Entering the exercises}

\subsubsection{The exercise header}
A subsection with exercises begins with the command 
\cmd{exercises} and ends with \cmd{eexer}.  If the subsection
has only one exercise, you can use \cmd{exercise}.
The word \cmd{exercises} produces a subsection in the same
format as other subsections, headed with the word ``Exercises''.
{\it There should be at most one exercise subsection for each
section of the document}.

\subsubsection{The exercises}
Each exercise is preceded by the word \cmd{exer} if you are not
going to answer it, and by \cmd{exera} if you are going to
answer it.  You can mix answered and unanswered exercises in any
way you wish; the numbering will be correct in the answer
section.  Exercises with answers will be marked like
this\answermark.  You can change the answer mark by putting a
command such as
\cmd{\renewcommand\answermark{\mbox{$\mymark$}}}
in the preamble.

\subsubsection{The answers}\label{anss}
{\bf Warning: The answers must be entered in exactly the way
spelled out here.  This aspect of {\tt ans.sty} is very
user-unfriendly.  Deviation will result in answers appearing in
the text or part of the next problem appearing in the answer.}  

The answer should start immediately after the exercise without a
blank line intervening, preceded
by the word \cmd{answer}.  You can type anything you want in the
answer, subject to the following three
requirements:{\bf\begin{itemize} 
\item The answer should start on the same line as the command
\cmd{answer}.
\item The answer should contain no
blank lines.  
\item The answer should be followed by at least
one blank line.  
\end{itemize}} If you want more than one
paragraph in an answer, insert the command \cmd{par} at the end
of each paragraph except the last.

The reason for these requirements is that the program uses the
blank line to delimit the answers (using an idea of Donald
Knuth's).  

\subsubsection{Subexercises}

To write an exercise with subexercises, start the exercise with
\cmd{exer} or \cmd{exera}, and then introduce each subexercise
with \cmd{subexer} or \cmd{subexera}.  You can mix answered and
nonanswered subexercises as you wish.  

If the exercise begins with a subexercise without any preceding
text, it should begin
``\cmd{exer}\cmd{immsubexer}'' (either may be followed by an
``a'').   
The remaining subexercises are labeled \cmd{subexer} or
\cmd{subexera} as before.
The answer to a subexercise beginning with
\cmd{immsubexer} should be labeled \cmd{immsubanswer}. 
The use of \cmd{immsubexer(a)} and \cmd{immsubanswer} keeps the
spacing straight (although the current version, \today, 
still doesn't get
the spacing right when the exercise has a double-digit number).

I apologize for the necessity of using \cmd{immsubexer} and
\cmd{immsubanswer}, but it seems quite complicated to create a
version that knows whether the exercise has any beginning text
or not.

\subsubsection{Problems with special marks} To mark an exercise 
hard, type

\begin{verbatim}
\exer\annot{hard}
\end{verbatim}

In general, you can annotate a problem with any sort of remark
in this way.

\subsection{Error messages}
If you type \cmd{exera} for an exercise but don't give it an
answer, you will get an error message.
Likewise if you give an
exercise an answer but headed it with \cmd{exer}.  Neither error
stops the document from composing, and if you have given an
exercise headed \cmd{exer} an answer, the answer will be printed
in the correct place with the correct number, but the spacing in
the exercise section may be wrong.

If you use \cmd{exer(a)} when you should have used
\cmd{immsubexer(a)} you will not get an error message; the
spacing will merely be wrong.

\subsection{Sample exercises}
This section ends with a set of exercises illustrating all these
points.  The next section has a few, too, so you can see how the
answer section handled the answers to different sections.

The answers to this section were entered as illustrated below.
If you removed the percent signs from
in front of the exercises that are commented out and compose
this document again, you can see how the error messages work.

This example also illustrates the use of the \cmd{exhead}
command, which allows you to insert instructions for doing the
next several exercises.

%%%RECOPY THIS!!!
\begin{verbatim}

\exercises
\exera This the first exercise of section~\ref{seca}.
\answer{This is the answer to the first exercise of
section~\ref{seca}.}

\exera This is the second exercise of section~\ref{seca}.
\answer This is the answer to the second exercise of
section~\ref{seca}.

\exer This exercise has no answer.

% \exera This exercise tests an error message.  
% It is marked to
% have an answer, but does not.

% \exer This exercise tests an error message too.
% \answer This should not be here.

\exhead{This header applies to Exercises~\ref{firste}
through~\ref{laste}.}

\exera\annot{hard}\label{firste} Yet another 
exercise with an answer.
\answer This is yet another answer. 
It is longer than the
others, and illustrates how you can 
have more than one
paragraph.\par
This is the second paragraph.

\exer\annot{hard} This is an exercise with subexercises.
\subexera First subexercise.
\subanswer Answer to first subexercise.

\subexer Second subexercise.

\subexera Third subexercise.
\subanswer Answer to third subexercise.

\exer\label{laste}\immsubexera This 
is the first subexercise of an exercise
that has no text before the subexercises.
\immsubanswer This is an answer.

\subexera Another subexercise.
\subanswer Another answer.

\exer\label{solans} This exercise has an answer 
in the Solution Manual.
\solution Answer to Exercise~\ref{solans} on
page~\pageref{solans}.

\eexer
\end{verbatim}



\exercises
\exera This the first exercise of section\ref{seca}.
\answer{This is the answer to the first exercise of
section~\ref{seca}.}

\exera This is the second exercise of section~\ref{seca}.
\answer This is the answer to the second exercise of
section~\ref{seca}.

\exer This exercise has no answer.

% \exera This exercise tests an error message.  
% It is marked to
% have an answer, but does not.

% \exer This exercise tests an error message too.
% \answer This should not be here.

\exhead{This header applies to Exercises~\ref{firste}
through~\ref{laste}.}

\exera\annot{hard}\label{firste} Yet another 
exercise with an answer.
\answer This is yet another answer. 
It is longer than the
others, and illustrates how you can 
have more than one
paragraph.\par
This is the second paragraph.

\exer\annot{hard} This is an exercise with subexercises.
\subexera First subexercise.
\subanswer Answer to first subexercise.

\subexer Second subexercise.

\subexera Third subexercise.
\subanswer Answer to third subexercise.

\exer\label{laste}\immsubexera This 
is the first subexercise of an exercise
that has no text before the subexercises.
\immsubanswer This is an answer.

\subexera Another subexercise.
\subanswer Another answer.

\exer\label{solans} This exercise has an answer 
in the Solution Manual.
\solution Answer to Exercise~\ref{solans} on
page~\pageref{solans}.

\eexer

\section{Bugs}\label{probsec}

\subsection{Answers appearing in the text\protect\\ or text appearing in
the answers}
If you enter an exercise and answer like this
\begin{verbatim}
\exera This is an exercise.
\answer
I did this wrong.
\end{verbatim}
the answer will
appear in the text instead of in the answer section.  The point
is that the answer must start on the same line as the word
\cmd{answer}.

If you do this
\begin{verbatim}
\exera This is an exercise.
\answer I did this wrong too.
\exera This exercise will disappear.
\answer Here is an answer.
\end{verbatim}
the second exercise will wind up as part of the answer to the
first exercise, because a blank line must follow the answer.  
Reread Section~\ref{anss} carefully.


\subsection{Spaces between carriage returns} 
Most text editors allow you to type return
(``enter'' on some computers), one or more spaces, then return. 
\TeX\ {\it will not interpret the result as a blank line} for
the purposes of ending answers.  The file has to contain two
returns in a row without any intervening spaces.  If in your
printout you discover than an answer has swallowed the following
exercise (or more), this may be the reason.  Blanks are inserted
between returns by some word processors or editors, and
sometimes (apparently) by e-mail systems.

Many text editors allow you to detect blanks because the cursor
changes shape on a blank.  Your editor may allow you to search 
for the sequence carriage return, blank(s), carriage return, and
change such sequences globally to carriage return, carriage
return.

\section{Customizing} If you look in the file {\tt
ans.sty},
you will discover numerous hints for customizing the file.

\subsection{Headings}
The headings put on the answer section and on the solutions
booklet are determined by the option, \cmd{artsec},
\cmd{bkchap}, etc. and can easily be changed.

\subsection{Other style files}
{\tt Ans.sty} will very likely work with many other style files
besides the standard {\tt art\-icle.sty}, but of course I can't
guarantee this.   Since \cmd{exersection} uses \cmd{subsection},
a different style file with a different definition of subsection
will place the exercise header in the same style as the other
subsections.  

\exercises

\exera Prove that for any integer $n>2$ there are no nonzero
integers $x$, $y$ and $z$ for which $x^n+y^n=z^n$.
\answer If I knew how to do \cmd{marginpar} in \LaTeX\ I would
have written it in the margin.  But that's the way the cookie
crumbles.

\exer An exercise without an answer.

\exera The very last exercise.
\answer The very last answer.

\doanswers

\setpage{1}
\dosolutions

\end{document}