probsoln v3.01: creating problem sheets optionally with solutions

probsoln v3.01: creating problem sheets optionally with solutions

Nicola L.C. Talbot
School of Computing Sciences
University of East Anglia
Norwich. Norfolk
NR4 7TJ. United Kingdom.
http://theoval.cmp.uea.ac.uk/~nlct/

2011-08-22

Contents

1 Introduction
2 Package Options
3 Showing and Hiding Solutions
4 General Formatting Commands
5 Defining a Problem
6 Using a Problem
7 Loading Problems From External Files
8 Iterating Through Datasets
9 Random Number Generator
10 Compatibility With Versions Prior to 3.0
Index

1 Introduction

The probsoln package is designed for teachers or lecturers who want to create problem sheets for their students. This package was designed with mathematics problems in mind, but can be used for other subjects as well. The idea is to create a file containing a large number of problems with their solutions which can be read in by LATEX, and then select a number of problems to typeset. This means that once the database has been set up, each year you can easily create a new problem sheet that is sufficiently different from the previous year, thus preventing the temptation of current students seeking out the previous year’s students, and checking out their answers. There is also an option that can be passed to the package to determine whether or not the solutions should be printed. In this way, one file can either produce the student’s version or the teacher’s version.

Top

2 Package Options

The following options may be passed to this package:

answers
Show the answers
noanswers
Don’t show the answers (default)
draft
Display the label and dataset name when a problem is used
final
Don’t display label and dataset name when a problem is used
usedefaultargs
Make \thisproblem use the default arguments supplied in the problem definition.
nousedefaultargs
Make \thisproblem prompt for problem arguments (default).

Top

3 Showing and Hiding Solutions

In addition to the answers and noanswers package options, it is also possible to show or suppress the solutions using


\showanswers  \showanswers

and


\hideanswers  \hideanswers

respectively.

The boolean variable showanswers determines whether the answers should be displayed. You can use this value with the ifthen package to specify different text depending on whether the solutions should be displayed. For example:

Assignment 1\ifthenelse{\boolean{showanswers}}{ (Solution Sheet)}{}

Alternatively you can use \ifshowanswers\else\fi:

Assignment 1\ifshowanswers\space (Solution Sheet)\fi

For longer passages, you can use the environments onlyproblem onlyproblem and onlysolution onlysolution. For example:

\begin{onlyproblem}%  
What is the derivative of $f(x) = x^2$?  
\end{onlyproblem}%  
\begin{onlysolution}%  
$f’(x) = 2x$  
\end{onlysolution}

The above will only display the question if showanswers is false and will only display the solution if showanswers is true. If you want the question to appear in the answer sheet as well as the solution, then don’t put the question in the onlyproblem environment:

What is the derivative of $f(x) = x^2$?  
\begin{onlysolution}%  
Solution: $f’(x) = 2x$  
\end{onlysolution}

You can’t use verbatim text in the body of the onlyproblem or onlysolution environments. If you need verbatim-like text, then try packages such as alltt. Remember that you also can’t use verbatim text in command arguments.

If you use onlysolution within the defproblem environment, the problem will be tagged as having a solution and will be added to the list used by \foreachsolution.

Top

4 General Formatting Commands

The commands and environments described in this section are provided to assist formatting problems and their solutions.


solution  \begin{solution}text\end{solution}

By default, this is equivalent to
\par\noindent\textbf{\solutionname}: text
where \solutionname \solutionname defaults to “Solution”. Note that you must place the solution environment inside the onlysolution environment or between \ifshowanswers\fi to ensure that it is suppressed when the solutions are not wanted. (See §3 Showing and Hiding Solutions.)

Note that the probsoln package will only define the solution environment if it is not already defined.


textenum  \begin{textenum}\end{textenum}

The textenum environment is like the enumerate environment but is in-line. It uses the same counter that the enumerate environment would use at that level so the question can be compact but the answer can use enumerate instead. For example:

\begin{onlyproblem}%  
  Differentiate the following:  
  \begin{textenum}  
    \item $f(x)=2^x$; \item $f(x)=\cot(x)$  
  \end{textenum}  
\end{onlyproblem}  
\begin{onlysolution}  
  \begin{enumerate}  
  \item  
    \begin{align*}  
    f(x) &= 2^x = \exp(\ln(x^2)) =\exp(2\ln(x))\\  
    f’(x) &= \exp(2\ln(x))\times \frac{2}{x}\\  
      &= f(x)\frac{2}{x}  
    \end{align*}  
  \item  
    \begin{align*}  
    f(x) &= \cot(x) = (\tan(x))^{-2}\\  
    f’(x) &= -(\tan(x))^{-2}\times\sec^2(x)\\  
    &=-\csc^2x  
    \end{align*}  
  \end{enumerate}  
\end{onlysolution}

In this example, the items in the question are brief, so an enumerate environment would result in a lot of unnecessary white space, but the answers require more space, so an enumerate environment is more appropriate. Since the textenum environment uses the same counters as the enumerate environment, the question and answer sheets use consistent labelling. Note that there are other packages available on CTAN that you can use to create in-line lists. Check the TeX Catalogue for further details.

\correctitem \incorrectitem 


\correctitem
\incorrectitem

You can use the commands \correctitem and \incorrectitem in place of \item. If the solutions are suppressed, these commands behave in the same way as \item, otherwise they format the item label using one of the commands: \correctitemformat \incorrectitemformat 


\correctitemformat{label}
\incorrectitemformat{label}

For example:

Under which of the following functions does $S=\{a_1,a_2\}$  
become a probability space?  
\begin{enumerate}  
\incorrectitem $P(a_1)=\frac{1}{3}$, $P(a_2)=\frac{1}{2}$  
\correctitem $P(a_1)=\frac{3}{4}$, $P(a_2)=\frac{1}{4}$  
\correctitem $P(a_1)=1$, $P(a_2)=0$  
\incorrectitem $P(a_1)=\frac{5}{4}$, $P(a_2)=-\frac{1}{4}$  
\end{enumerate}

The default definition of \correctitemformat puts a frame around the label.

Top

5 Defining a Problem

It is possible to construct a problem sheet with solutions using the commands described in the previous sections, however it is also possible to define a set of problems for later use. In this way you can create an external file containing many problems some or all of which can be loaded and used in a document. The probsoln package has a default data set labelled “default” in which you can store problems. Alternatively, you can create multiple data sets. You can then iterate through each problem in a problem set. You can use a previously defined problem more than once, which means that by judicious use of onlyproblem, onlysolution or the showanswers boolean variable in conjunction with \showanswers and \hideanswers, you can print the solutions in a different location to the questions (for example in an appendix).


defproblem  \begin{defproblem}[n][default args]{label}
definition
\end{defproblem}

This defines the problem whose label is given by label. The label must be unique for a given data set and should not contain active characters or a comma. (Active characters include the special characters such as $ and &, but some packages may make other symbols active, such as the colon (:) character. For example, the ngerman and babel packages make certain punctuation active. Check the relevant package documentation for details.)

If defproblem occurs in the document or is included via \input or \include, then the problem will be added to the default data set. If defproblem occurs in an external file that is loaded using one of the commands defined in §7 Loading Problems From External Files then the problem will be added to the specified data set.

The contents of the defproblem environment should be the text that defines the problem. This may include any of the commands defined in §3 Showing and Hiding Solutions and §4 General Formatting Commands.

The problem may optionally take narguments (where nis from 0 to 9). The arguments can be referenced in the definition via #1,…,#9. If nis omitted then the problem doesn’t take any arguments. The following example defines a problem with one argument:

\begin{defproblem}[1]{diffsin}  
Differentiate $f(x)=\sin(#1x)$.  
\begin{onlysolution}%  
  \begin{solution}  
    $f’(x) = #1\cos(#1x)$  
  \end{solution}  
\end{onlysolution}  
\end{defproblem}

The second optional argument default argssupplies default problem arguments that will automatically be used within \thisproblem when used in \foreachproblem in conjunction with the package option usedefaultargs. (See §8 Iterating Through Datasets.) For example:

\begin{defproblem}[1][{2}]{diffsin}  
Differentiate $f(x)=\sin(#1x)$.  
\begin{onlysolution}%  
  \begin{solution}  
    $f’(x) = #1\cos(#1x)$  
  \end{solution}  
\end{onlysolution}  
\end{defproblem}


\newproblem  \newproblem[n][default args]{label}{problem}{solution}

This is a shortcut command for: \begin{defproblem}[n][default args]{label}%
problem%
\begin{onlysolution}%
\begin{solution}%
solution%
\end{solution}%
\end{onlysolution}%
\end{defproblem}

For example:

\newproblem[1]{diffsin}{%  
  \(f(x) = \sin(#1x)\)  
}%  
{%  
  \(f’(x) = #1\cos(#1x)\)  
}

is equivalent to

\begin{defproblem}[1]{diffcos}%  
  \(f(x) = \cos(#1x)\)  
\begin{onlysolution}%  
  \begin{solution}%  
    \(f’(x) = -#1\sin(#1x)\)  
  \end{solution}%  
\end{onlysolution}%  
\end{defproblem}

(In this example, the argument will need to be a positive number to avoid a double minus in the answer. If you want to perform floating point arithmetic on the arguments, then try the fp or pgfmath packages.)

Alternatively, if you want to supply default arguments to use when iterating through problems with \foreachproblem:

\newproblem[1][{3}]{diffsin}{%  
  \(f(x) = \sin(#1x)\)  
}%  
{%  
  \(f’(x) = #1\cos(#1x)\)  
}


\newproblem*  \newproblem*[n][default args]{label}{definition}

This is a shortcut for: \begin{defproblem}[n][default args]{label}%
definition%
\end{defproblem}

Verbatim text must not be used in the contents of defproblem or in any of the arguments to \newproblem or \newproblem*. If you need verbatim-like text consider using \texttt or the alltt package.

Top

6 Using a Problem

Once you have defined a problem using defproblem or \newproblem (see §5 Defining a Problem), you can later display the problem using:


\useproblem  \useproblem[data set]{label}{arg1}…{argN}

where data setis the name of the data set that contains the problem (the default data set is used if omitted), labelis the label identifying the required problem and arg1, …, argNare the arguments to pass to the problem, if the problem was defined to have arguments (where N is the number of arguments specified when the problem was defined).

For example, in the previous section the problem diffcos was defined to have one argument, so it can be used as follows:

\useproblem{diffcos}{3}

This will be equivalent to:

\(f(x) = \cos(3x)\)  
\begin{onlysolution}%  
\begin{solution}%  
\(f’(x) = -3\sin(3x)\)  
\end{solution}%  
\end{onlysolution}%

Top

7 Loading Problems From External Files

You can store all your problem definitions (see §5 Defining a Problem) in an external file. These problems can all be appended to the default data set by including the file via \input or they can be appended to other data sets using one of the commands described below. Once you have loaded all the required problems, you can iterate through the data sets using the commands described in §8 Iterating Through Datasets. Note that the commands below will create a new data set, if the named data set doesn’t exist.


\loadallproblems  \loadallproblems[data set]{filename}

This will load all problems defined in filenameand append them to the specified data set, in the order in which they are defined in the file. If data setis omitted, the default data set will be used. If data setdoesn’t exist, it will be created.


\loadselectedproblems  \loadselectedproblems[data set]{labels}{filename}

This is like \loadallproblems, but only those problems whose label is listed in the comma-separated list labelsare loaded. For example, if I have some problems defined in the file derivatives.tex, then

\loadselectedproblems{diffsin,diffcos}{derivatives}

will only load the problems whose labels are diffsin and diffcos, respectively. All the other problems in the file will remain undefined.


\loadexceptproblems  \loadexceptproblems[data set]{exception list}{filename}

This is the reverse of \loadselectedproblems. This loads all problems except those whose labels are listed in exception list.


\loadrandomproblems  \loadrandomproblems[data set]{n}{filename}

This randomly loads nproblems from filenameand adds them to the given data set. If data setis omitted, the default data set is assumed. Note that the problems will be added to the data set in a random order, not in the order in which they were defined. There must be at least nproblems defined in filename.


\loadrandomexcept  \loadrandomexcept[data set]{n}{filename}{exception list}

This is similar to \loadrandomproblems except that it won’t load those problems whose labels are listed in exception list.

Note that the random number generator has been modified in version 3.01 in order to fix a bug. If you want to ensure that your random numbers are compatible with earlier versions, you can switch to the old generator using


\PSNuseoldrandom  \PSNuseoldrandom

It is generally not a good idea to place anything in filenamethat is not inside the body of defproblem or in the arguments to \newproblem or \newproblem*. All the commands in this section input the external file within a local scope, so command definitions would need to be made global to have any effect. In addition, \loadrandomproblems has to load each file twice, which means that anything outside a problem definition will be parsed twice.

Top

8 Iterating Through Datasets

Once you have defined all your problems for a given data set, you can use an individual problem with \useproblem (see §6 Using a Problem) but it is more likely that you will want to iterate through all the problems so that you don’t need to remember the labels of all the problems you have defined.


\foreachproblem  \foreachproblem[data set]{body}

This does bodyfor each problem in the given data set. If data setis omitted, the default data set is used. Within bodyyou can use


\thisproblem  \thisproblem

to use the current problem and


\thisproblemlabel  \thisproblemlabel

to access the current label. If the problem requires arguments, and no default arguments were supplied in the problem definition or the package option usedefaultargs was not used, then you will be prompted for arguments, so if you want to use this approach you will need to use LATEX in interactive mode. If you do provide arguments, they will be stored in the event that you need to iterate through the data set again. The arguments will be included in \thisproblem, so you only need to use \thisproblem without having to specify \useproblem.

For example, to iterate through all problems in the default data set:

\begin{enumerate}  
\foreachproblem{\item\thisproblem}  
\end{enumerate}


\foreachsolution  \foreachsolution[data set]{body}

This is equivalent to \foreachsolution, but only iterates through problems that contain the onlysolution environment. Note that you still need to use \showanswers or the answers package option for the contents of the onlysolution environment to appear.


\foreachdataset  \foreachdataset{cmd}{body}

This does bodyfor each of the defined data sets. Within body, cmdwill be set to the name of the current data set. For example, to display all problems in all data sets:

\begin{enumerate}  
\foreachdataset{\thisdataset}{%  
\foreachproblem[\thisdataset]{\item\thisproblem}}  
\end{enumerate}

Suppose I have two external files called derivatives.tex and probspaces.tex which define problems using both onlyproblem and onlysolution for example:

\begin{defproblem}{cosxsqsinx}%  
\begin{onlyproblem}%  
$y = \cos(x^2)\sin x$.%  
\end{onlyproblem}%  
\begin{onlysolution}%  
\[\frac{dy}{dx} = -\sin(x^2)2x\sin x + \cos(x^2)\cos x\]  
\end{onlysolution}%  
\end{defproblem}

I can write a document that creates two data sets, one for the derivative problems and one for the problems about probability spaces. I can then use \hideanswers and iterate through the require data set to produce the problems. Later, I can use \showanswers and iterate over all problems defined in both data sets to produce the chapter containing all the answers. When displaying the questions, I have taken advantage of the fact that I can cross-reference items within an enumerate environment, and redefined \theenumi to label the questions according to the chapter. The cross-reference label is constructed from the problem label and is referenced in the answer section to ensure that the answers have the same label as the questions.

\documentclass{report}  
\usepackage{probsoln}  
\begin{document}  
\hideanswers  
\chapter{Differentiation}  
 randomly select 25 problems from derivatives.tex and add to  
 the data set called ’deriv’  
\loadrandomproblems[deriv]{25}{derivatives}  
 Display the problems  
\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}  
\begin{enumerate}  
\foreachproblem[deriv]{\item\label{prob:\thisproblemlabel}\thisproblem}  
\end{enumerate}  
 You may need to change \theenumi back here  
\chapter{Probability Spaces}  
 randomly select 25 problems from probspaces.tex and add to  
 the data set called ’spaces’  
\loadrandomproblems[spaces]{25}{probspaces}  
 Display the problems  
\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}  
\begin{enumerate}  
\foreachproblem[spaces]{\item\label{prob:\thisproblemlabel}\thisproblem}  
\end{enumerate}  
 You may need to change \theenumi back here  
\appendix  
\chapter{Solutions}  
\showanswers  
\begin{itemize}  
\foreachdataset{\thisdataset}{%  
\foreachproblem[\thisdataset]{\item[\ref{prob:\thisproblemlabel}]\thisproblem}  
}  
\end{itemize}  
\end{document}

Top

9 Random Number Generator

This package provides a pseudo-random number generator that is used by \loadrandomproblems. As noted earlier the random number generator has been modified in version 3.01 in order to fix a bug. If you want to ensure that your random numbers are compatible with earlier versions, you can switch to the old generator using


\PSNuseoldrandom  \PSNuseoldrandom


\PSNrandseed  \PSNrandseed{n}

This sets the seed to nwhich must be a non-zero integer. For example, to generate a different set of random numbers every time you LATEX your document,1 put the following in your preamble:

\PSNrandseed{\time}

or to generate a different set of random numbers every year you LATEX your document:

\PSNrandseed{\year}


\PSNgetrandseed  \PSNgetrandseed{register}

This stores the current seed in the count register specified by register. For example:

\newcount\myseed  
\PSNgetrandseed{\myseed}


\PSNrandom  \PSNrandom{register}{n}

Generates a random integer from 1 to nand stores in the count register specified by register. For example, the following generates an integer from 1 to 10 and stores it in the register \myreg:

\newcount\myreg  
\PSNrandom{\myreg}{10}


\random  \random{counter}{min}{max}

Generates a random integer from minto maxand stores in the given counter. For example, the following generates a random number between 3 and 8 (inclusive) and stores it in the counter myrand.

\newcounter{myrand}  
\random{myrand}{3}{8}


\doforrandN  \doforrandN{n}{cmd}{list}{text}

Randomly selects nvalues from the comma-separated list given by listand iterates through this subset. On each iteration it sets cmdto the current value and does text. For example, the following will load a randomly selected problem from two of the listed files (where file1.tex, file2.tex and file3.tex are files containing at least one problem):

\doforrandN{2}{\thisfile}{file1,file2,file3}{%  
\loadrandomproblems{1}{\thisfile}}

Top

10 Compatibility With Versions Prior to 3.0

Version 3.0 of the probsoln package completely changed the structure of the package, but the commands described in this section have been provided to maintain compatibility with earlier versions. The only problems that are likely to occur are those where commands are contained within groups. This will effect any commands that are contained in external files that are outside of the arguments to \newproblem and \newproblem*. However, since the external files had to be parsed twice in order to load the problems, this shouldn’t be an issue as adding anything other than problem definitions in those files would be problematic anyway.

The other likely difference is where the random generator is used in a group. This includes commands such as \selectrandomly. For example, if your document contained something like:

\begin{enumerate}  
\selectrandomly{file1}{8}  
\item Solve the following:  
\begin{enumerate}  
\selectrandomly{file2}{4}  
\end{enumerate}  
\selectrandomly{file3}{2}  
\end{enumerate}

Then using versions prior to v3.0 will produce a different set of random numbers since the second \selectrandomly is in a different level of grouping. If you want to ensure that the document produces exactly the same random set with the new version as with the old version, you will need to get and set the random number seed. For example, the above would need to be modified so that it becomes:

\begin{enumerate}  
\selectrandomly{file1}{8}  
\item Solve the following:  
\newcount\oldseed  
\PSNgetrandseed{\oldseed}  
\begin{enumerate}  
\selectrandomly{file2}{4}  
\end{enumerate}  
\PSNrandseed{\oldseed}  
\selectrandomly{file3}{2}  
\end{enumerate}


\selectrandomly  \selectrandomly{filename}{n}

This is now equivalent to: {\loadrandomproblems[filename]{n}{filename}}%
\foreachproblem[filename]{\PSNitem\thisproblem\endPSNitem}


\selectallproblems  \selectallproblems{filename}

This is now equivalent to: {\loadallproblems[filename]{filename}}%
\foreachproblem[filename]{\PSNitem\thisproblem\endPSNitem}

Note that in both the above cases, a new data set is created with the same name as the file name.

Top

Index

A

alltt package  1, 2

B babel package  3

C \correctitem  4
\correctitemformat  5

D defproblem (environment)  6, 7, 8, 9, 10, 11, 12
\doforrandN  13

E enumerate (environment)  14, 15, 16
environments:
    defproblem  17, 18, 19, 20, 21, 22, 23
    enumerate  24, 25, 26
    onlyproblem  27, 28, 29, 30, 31
    onlysolution  32, 33, 34, 35, 36, 37
    solution  38, 39, 40
    textenum  41

F \foreachdataset  42
\foreachproblem  43, 44, 45
\foreachsolution  46, 47
fp package  48

H \hideanswers  49, 50, 51

I \ifshowanswers  52, 53
ifthen package  54
\include  55
\incorrectitem  56
\incorrectitemformat  57
\input  58, 59
\item  60

L \loadallproblems  61
\loadexceptproblems  62
\loadrandomexcept  63
\loadrandomproblems  64, 65
\loadselectedproblems  66

N \newproblem  67, 68, 69, 70
\newproblem*  71, 72, 73
ngerman package  74

O onlyproblem (environment)  75, 76, 77, 78, 79
onlysolution (environment)  80, 81, 82, 83, 84, 85

P package options:
    answers  86, 87, 88
    draft  89
    final  90
    noanswers  91, 92
    nousedefaultargs  93
    usedefaultargs  94, 95, 96
pgfmath package  97
probsoln package  98
\PSNgetrandseed  99
\PSNrandom  100
\PSNrandseed  101
\PSNuseoldrandom  102, 103

R \random  104

S \selectallproblems  105
\selectrandomly  106, 107
\showanswers  108, 109, 110, 111
showanswers boolean variable  112, 113, 114, 115
solution (environment)  116, 117, 118
\solutionname  119

T textenum (environment)  120
\theenumi  121
\thisproblem  122, 123, 124, 125
\thisproblemlabel  126

U \useproblem  127, 128, 129

1assuming you leave at least a minute between runs.