2

Basically, what I want is exactly asked in Showing Solutions of the Questions "separately", except that I'm using amsbook. So, I want to write a series of questions, by writing something like

\begin{question}

\question 1+1=?

\begin{answer}
1+1=2
\end{answer}

\question What is 1+2=?

\begin{answer}
1+2=3
\end{answer}

\end{question}

The code above should display the questions only:

Question 1: 1+1=?

Question 2: 1+2=?

Then the command

\showanswers

displays the answers:

Answer 1: 1+1=2

Answer 2: 1+2=3

I also want the enumeration style "Question 1, Question 2, ..." to be customizable.

greg
  • 21
  • I think this might help. http://tex.stackexchange.com/questions/60771/cleanest-way-to-part-answers-from-questions – R. Schumacher Jul 09 '12 at 14:31
  • I don't seem to find the answer to my question. – greg Jul 09 '12 at 14:36
  • Another option, use the exercise.sty style file. It will give you both an exercise and and answer environment. You have the options of either one, both, or none. I use this to put questions into beamer presentations with or without answers. – R. Schumacher Jul 09 '12 at 14:40

1 Answers1

2

Here's a solution using the answers package.

You can toggle the answers appearing or not by using

\usepackage{answers}    

to write the answers to a file (answers.tex in the code below) or

\usepackage[nosolutionfiles]{answers}

to show the answers in the document.

I've used the enumitem package to create a list-type environment called questions, and defined the \question command as \item (although it doesn't take an optional argument- not sure if this matters or not here though).

\documentclass{amsbook}

\usepackage{enumitem}   % for custom lists
\usepackage{answers}    % for separating solutions
%\usepackage[nosolutionfiles]{answers}

\newlist{questions}{enumerate}{5}
\setlist[questions]{label*=Question \arabic*.}
\newcommand{\question}{\item}

% open the answer file
\Opensolutionfile{answers}
\Newassociation{answer}{ansWER}{answers}

\begin{document}

\begin{questions}
\question $1+1=?$
\begin{answer}
    $1+1=2$
\end{answer}

\question What is $1+2=?$
\begin{answer}
$1+2=3$
\end{answer}  
\end{questions}

% close the answer file
\Closesolutionfile{answers}

% use this line if you'd like to include the answers separately
\clearpage
\IfFileExists{answers.tex}{\input{answers.tex}}{no file!}
\end{document}

You can do a lot with the answers package, particularly if you combine it with the hyperref, as shown in a few answers on this site, for example in Adding another answer hyperlinked to the question itself

cmhughes
  • 100,947