# Please explain \Newassociation and provide an overview of how the package answers works

I've found the answers package a little difficult to use and the documentation isn't as accessible as it could be to a novice like me. I'm not sure but it often seems at times like multiple files are involved and in particular, I do not understand \Newassociation, and talk of a "symbolic file handle". It sounds like programming jargon and I don't understand it. Could someone please explain the function of \Newassociation in different words to the manual? Please also give a simple overview of how the package answers works.

The alternative packages for the job, exercise and probsoln, aren't especially easy to follow either either. But if you feel either of those packages will be easier to learn, please let me know and I will consider using the easier alternative.

Aside: Unfortunately, the packages' documentations don't include an actual description or visual examples of the output so it leaves me unsure what formatting is applied, especially while I can't seem to get it working. I think it's always best if documentation includes a description of the output, especially visual. I also greatly appreciate the philosophy of trying to lower the entry barrier into the LaTeX world, so perhaps the package authors would kindly consider this feedback and potentially put it to effect. I think it is often good practice to include a beginner's edition of documentation when it is bound to get complicated, as some packages in the LaTeX world have.

-

I agree that the documentation of the answers package is a bit on the terse side. I don't find the exercise documentation to be very difficult; it can do a lot of things, but is quite well documented, I think. I've never used probsoln so I won't comment further on that.

Since you ask about the answers package specifically, here is my attempt at explaining the package, and clarifying at least the simple example given in the documentation.

First, the basic command \Newassociation{xxx}{yyy}{zzz} does the following things:

1. It creates an environment {xxx} to be used in your document for the solutions within the problems themselves. This environment will generally not be displayed unless the [nosolutionfiles] option chosen when the package is loaded.
2. It creates an environment {yyy} which is the environment that will be used to display the solutions. These solutions are written to a separate file (or files) and then can be input into your document at a later place (e.g. at the end of a chapter, or at the end of the entire book.)
3. It creates a file naming schema for the solution file(s) based on zzz. Either a single file is used (in which case it will be zzz.tex) or multiple files are used, in which case zzz acts as a kind of label that identifies the type of solution it is, and the file names are given explicitly.

Because xxx, yyy and zzz are used as part of environment/macro names, they must only contain letters, since these are the only valid characters for multi-character macro names in TeX. As with other macro names, they are case-sensitive (Xxxxxx). Furthermore, you should make sure that they do not conflict with existing environments that you may have in your document.

In the documentation, there are three sample documents (ansexam{1|2|3}.tex) which show how this works in practice. I've taken the simple example here (ansexam1.tex), and annotated it with some comments. (On TeXLive distributions, these files can be found in /usr/local/texlive/2010/texmf-dist/doc/latex/answers/).

\documentclass[12pt,a4paper]{article}
% First we load the package.
% Now we set up a solution set.
% The environment that marks solutions within exercises is {sol}.
% The environment that marks solutions to be displayed is {Solution}
% The solution file type is "ans".
\Newassociation{sol}{Solution}{ans}

% Now we set up a theorem-like environment for exercises (this is)
% standard LaTeX; you could also use any of the more sophisticated
% theorem packages to do this.
\newtheorem{ex}{Exercise}

\begin{document}

% The next command opens a file to store the solutions. Without the second
% argument, the file would be called ans.tex.  With the second argument
% the file is called ans1.tex and it is of the "ans" type.
\Opensolutionfile{ans}[ans1]

% Now we write our problems, and for each problem we write its solution into the
% "sol" environment.  The solutions will not be displayed here.
\section{Problems}
\begin{ex}
First exercise
\begin{sol}
First solution.
\end{sol}
\end{ex}
\begin{ex}
Second exercise
\begin{sol}
Second solution.
\end{sol}
\end{ex}

% Now that we are done with our exercises and our solutions,
% we close the solution file
%
\Closesolutionfile{ans}

% Now our document has created a file called "ans1.tex" which contains a bunch of
% "Solution" environments, one for each "sol" environment that we put into the
% exercises. This file can now be \input back into another place in the document,
% here in the section called "Solutions".

\section{Solutions}
\input{ans1}
\end{document}


If you run this document, you will find that it has created a file ans1.tex which looks like the following:

\begin{Solution}{1}
First solution.

\end{Solution}
\begin{Solution}{2}
Second solution.

\end{Solution}


The Solution environment is set up by the \Newassociation command. It has some basic formatting, which you can change as needed, either with hooks provided by the package, or by using \renewenvironment after you have issued the \Newassociation command.

For example, there is a hook \solutionstyle which formats the label of each solution. This is initially set to \bfseries, so the solution environment produces:

1 First solution

2 Second solution

You could redefine it as follows:

\renewcommand{\solutionstyle}[1]{\bfseries Answer to Exercise #1 }


And now your solutions would appear as:

Answer to Exercise 1 First solution

Answer to Exercise 2 Second solution

More complicated formatting is possible for any of the environments created using the yyy argument of the \Newassociation command. There are two hooks, \preyyy and \postyyy (in this example, this would be \preSolution and \postSolution.) These must be created using \newcommand by you, and will be prepended or appended to the output solution as needed. For example, if you wanted each solution to end with a bullet, you could use:

\newcommand\postSolution{\hfill\textbullet}


Which would result in a right aligned bullet on its own line (ugly, but just an example.)

If you want your solutions to be more fancily formatted, you would use \renewenvironment to define the environment the way you like.

-
Wow, thanks. Your annotated approach to explaining this package helps tremendously. The documentation itself was somewhat cryptic to me, but with your annotations, I think I should be much better placed to figure it out now. Again, many thanks for your detailed answer! :) I will now proceed to try out the package. –  ptrcao Jul 5 '11 at 4:45
By the way, for my own education can anybody explain what a "symbolic file handle" is? –  ptrcao Jul 5 '11 at 4:50
I did my best :-). When used by itself, it forms the basename of the .tex file that stores the solutions. When used with the alternate syntax in the example I explained, it's basically the type of the file. You can think of it similarly to the file extension .tex for TeX files, .sty for packages, .cls for class files etc. (Even though in this context it is not implemented as a file extension.) –  Alan Munn Jul 5 '11 at 4:58