# Textbook package compatible with examdoc

I really like the examdoc class. I almost always use it to create exams, tests, ... However...
I'd like to make a textbook. In it should be several exercises, with (preferably) their solutions in an appendix.
I encounter following problems:

1. I'm using \documentclass{book} for the textbook. I'd like to be able to copy-paste questions and solutions to a \documentclass{exam}. I already tried to make my own .sty file:

\ProvidesPackage{oefeningen}

\newenvironment{questions}{\begin{enumerate}}{\end{enumerate}}
\newcommand{\question}{\item}
\newenvironment{parts}{%
\let\oldpart=\part
\renewcommand{\part}{\item}
\begin{enumerate}}{\end{enumerate}\renewcommand{\part}{\oldpart}}


but that doesn't seem to do the trick... (it doesn't compile and I still don't have a clue how to cope with the solution-environment)

2. I don't know how to display the solutions in a seperate appendix, instead of directly after each question...

I'm quite a newbie on writing classes, packages, or macros but any help will be appreciated. I browsed a lot of forums, too, but imho other answers don't provide the functionality I'd like. (but I'd happily be corrected)

sort of mwe:

i have this exam document:

\documentclass[12pt,a4paper,answers]{exam}

\begin{document}
\begin{questions}
\question Main question goes here
\begin{parts}
\part $1+1=$
\begin{solution}
2
\end{solution}
\end{parts}
\end{questions}
\end{document}


what i'd like to do (but what doesn't work!) is something like this:

\documentclass[12pt,a4paper]{book}
\usepackage{exsheets}

\begin{document}

\section{foo}

\question Main question goes here
\begin{parts}
\part $1+1=$
\begin{solution}
2
\end{solution}
\end{parts}

\section{solutions}
%command to display list of solutions, optimally organised by section/chapter

\end{document}


resulting in something like this

This way I'd be able to copy-paste questions to/from textbook to re-use them on exams...

-
Welcome to TeX.sx! A tip: You can use backticks  to mark your inline code as I did in my edit. –  Kurt Oct 20 '12 at 21:37
By browsing more, i found the package exsheets, that almost does the trick. i could easily adapt the examdoc class from \question to \begin{question}\end{question} by using \newevironment, so that works out. But unfortunately, exsheets doesn't provide \begin{parts}... –  long tom Oct 21 '12 at 12:53
i added something mwe-like... –  long tom Oct 22 '12 at 11:08
This is not going to work with exsheets the way you want. The way exsheets is designed one can only add a solution to a whole question. –  cgnieder Oct 22 '12 at 11:10
Of course the easiest way would be to use exsheets both for the textbook and the exams. –  cgnieder Oct 23 '12 at 14:10

### The Goal

Writing a textbook using the exsheets package and be able to copy & paste the exercises and solutions as they are in a document using the exam class for creating exams.

### Requirements

• The exsheets package has no parts environment. It needs to be defined so it can be used with the same syntax as required by exam.
• a solution environment should be available to be used once or more for a question. For this exsheets' own solution environment needs to be redefined. This also requires a suitable replacement for the environment so exsheets can still save solutions to print them later in the textbook.
• adapt exsheets' question environment in the exam document.

### The Definitions

• The parts Environment: Since in the textbook no points need to be assigned to questions a simple wrapper for enumerate will do. We will use enumitem because it's comfortable.

\usepackage{enumitem}
\newenvironment{parts}{%
\renewcommand*\part[1][]{\item}%
\begin{enumerate}[label=(\alph*)]
}{\end{enumerate}}

• Redefinition of the solution Environment: The first thing we do is create a new question/solution pair for exsheets. This will provide a replacement for the original solution. This requires exsheets version 0.3 which should be on CTAN in a week or so. The beta version can be downloaded here.

\NewQuSolPair{Question}{Solution}


The following is a little bit longer. The goal will be to define solution in a way that will store the solution for exsheets. The manual way would be to write the solution a second time in the newly defined Solution environment:

\begin{Question}
Main question goes here
\begin{parts}
\part $1+1=$
\begin{solution}
$2$
\end{solution}
\end{parts}
\end{Question}
% the following should be unnecessary:
\begin{Solution}
\begin{parts}
\part $1+1=$
\end{parts}
\end{Solution}


Since I don't have a perfect solution for the automization I'll present the basic solution that requires the double input first.

\usepackage{environ}
\makeatletter
% no idea why these are necessary:
\def\env@solution@save@env{}
\def\env@solution@process{}
% the actual redefinition into a dummy environment
% that gobbles its body:
\RenewEnviron{solution}{}
\makeatother


### The Documents

Now the textbook can look like this:

% this is textbook.tex
\documentclass[12pt,a4paper]{book}
\usepackage{exsheets}

\usepackage[inline]{enumitem}
\newenvironment{parts}{%
\renewcommand*\part[1][]{\item}%
\begin{enumerate}[label=(\alph*)]
}{%
\end{enumerate}%
}

\NewQuSolPair{Question}{Solution}

\usepackage{environ}
\makeatletter
\def\env@solution@save@env{}
\def\env@solution@process{}
\RenewEnviron{solution}{}
\makeatother
\begin{document}
\chapter{foo}
\section{questions}\exlabel{sec:questions:foo}

\begin{Question}
Main question goes here
\begin{parts}
\part $1+1=$
\begin{solution}
$2$
\end{solution}
\part $8-3=$
\begin{solution}
$5$
\end{solution}
\end{parts}
\end{Question}
\begin{Solution}
\begin{enumerate*}[label=(\alph*)]
\item $2$
\item $5$
\end{enumerate*}
\end{Solution}

\begin{Question}
Another question.
\begin{solution}
\end{solution}
\end{Question}
\begin{Solution}
\end{Solution}

\section{solutions}
\printsolutions[section=\S{sec:questions:foo}]

\end{document}


The corresponding exam:

% this is exam.tex

\newenvironment{Question}{\question}{}

\begin{document}

\begin{questions}
\begin{Question}
Main question goes here
\begin{parts}
\part $1+1=$
\begin{solution}
$2$
\end{solution}
\part $8-3=$
\begin{solution}
$5$
\end{solution}
\end{parts}
\end{Question}

\begin{Question}
Another question.
\begin{solution}
\end{solution}
\end{Question}
\end{questions}

\end{document}


### Preliminary Attempt to avoid Double Input of Solutions

The following definition of the solution environment tries to automate things but has some obvious shortcomings. Flexibility in creating the solutions is mainly lost.

Every call of solution within a question will save its body and at the end of the Question environment a Solution environment will automatically called if the solution env has been used. In order to do this comfortably the etoolbox package is used:

\usepackage{etoolbox,environ}
\makeatletter
\def\env@solution@save@env{}
\def\env@solution@process{}
\RenewEnviron{solution}{%
\ifdefvoid{\BODY}{}
{%
{%
\ifnum\value{enumi}>0\theenumi\space\fi
\expandonce\BODY\space
}%
}}
\AtEndEnvironment{Question}{%
}


With this the textbook.tex now looks as follows:

% this is textbook.tex
\documentclass[12pt,a4paper]{book}
\usepackage{exsheets}

\usepackage{enumitem}
\newenvironment{parts}{%
\renewcommand*\part[1][]{\item}%
\begin{enumerate}[label=(\alph*)]
}{%
\end{enumerate}%
}

\NewQuSolPair{Question}{Solution}

\usepackage{etoolbox,environ}
\makeatletter
\def\env@solution@save@env{}
\def\env@solution@process{}
\RenewEnviron{solution}{%
\ifdefvoid{\BODY}{}
{%
{%
\ifnum\value{enumi}>0\theenumi\space\fi
\expandonce\BODY\space
}%
}}
\AtEndEnvironment{Question}{%
}
\makeatother
\begin{document}
\chapter{foo}
\section{questions}\exlabel{sec:questions:foo}

\begin{Question}
Main question goes here
\begin{parts}
\part $1+1=$
\begin{solution}
$2$
\end{solution}
\part $8-3=$
\begin{solution}
$5$
\end{solution}
\end{parts}
\end{Question}

\begin{Question}
Another question.
\begin{solution}
\end{solution}
\end{Question}

\section{solutions}
\printsolutions[section=\S{sec:questions:foo}]

\end{document}


The result looks the same as above.

You are saying in the comments that exsheets would do the trick for you if it had a parts environment. If this really is the only thing you're missing: it could easily be defined.

I'm assuming that in the textbook you do not want to assign points to the exercises so the only thing needed is a simple wrapper for an enumerate environment. I'm using enumitem to format the labels. The redefinition of \part should be kept local since you might want to divide the textbook into parts.

\documentclass{scrartcl}
\usepackage{exsheets}
\usepackage{enumitem}

\newenvironment{parts}{%
\renewcommand*\part[1][]{\item}%
\begin{enumerate}[label=(\alph*)]
}{%
\end{enumerate}%
}

\begin{document}
\part{A demonstration}
\begin{question}
Why is there air?
\end{question}

\begin{question}
What if there were no air?
\begin{parts}
\part Describe the effect on the balloon industry.
\part Describe the effect on the aircraft industry.
\end{parts}
\end{question}

\end{document}


The corresponding exam document:

\documentclass{exam}

\begin{document}

\begin{questions}

% the following part is the same as in the other document:
\begin{question}
Why is there air?
\end{question}

\begin{question}
What if there were no air?
\begin{parts}
\part Describe the effect on the balloon industry.
\part Describe the effect on the aircraft industry.
\end{parts}
\end{question}

\end{questions}

\end{document}


-
This works fine concerning the missing parts environment. Thanks a lot, textbook finally compiles... Alas, this doesn't handle the solutions to the parts of a question. or at least i don't think they do? –  long tom Oct 22 '12 at 10:54
Why not? The parts environment can be used inside the solution environment as well. –  cgnieder Oct 22 '12 at 11:07
but having solutions inside that parts environment wouldn't be a good idea? –  long tom Oct 22 '12 at 11:23
This won't work the way exsheets is designed. I'm sorry. There may be only one solution environment per question` environment and it should be placed after the question. –  cgnieder Oct 22 '12 at 11:27
I might have a workaround... This'll has to wait until tonight, though. –  cgnieder Oct 22 '12 at 11:38