summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/probsoln/probsoln.dtx
blob: d555fc87f5e8953790047a2b8ffb98f8d39a3e8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
%\iffalse
% probsoln.dtx generated using makedtx version 0.91b (c) Nicola Talbot
% Command line args:
%   -src "(.+)\.(sty)=>\1.\2"
%   -doc "manual.tex"
%   -author "Nicola Talbot"
%   -dir "source"
%   probsoln
% Created on 2007/7/18 20:23
%\fi
%\iffalse
%<*package>
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%</package>
%\fi
% \iffalse
% Doc-Source file to use with LaTeX2e
% Copyright (C) 2007 Nicola Talbot, all rights reserved.
% \fi
% \iffalse
%<*driver>
\documentclass{ltxdoc}
\usepackage[colorlinks,
            bookmarks,
            hyperindex=false,
            pdfauthor=Nicola Talbot,
            pdftitle={probsoln.sty : a LaTeX2e package to help create problem sheets}]{hyperref}



\newcommand{\sty}[1]{\textsf{#1}}\newcommand{\env}[1]{\textsf{#1}}
\newcommand{\cmddef}[1]{\hypertarget{#1}{\cmdname{#1}}}
\newcommand{\cmdlink}[1]{\hyperlink{#1}{\cmdname{#1}}}
\newcommand{\problabel}[2]{\hypertarget{#1}{\texttt{#2}}}
\newcommand{\probref}[1]{\hyperlink{#1}{\texttt{#1}}}
\newcommand{\cmdname}[1]{\texttt{\symbol{92}#1}}
 %hyperindex conflicts with doc
 %(end up with |usage|hyperpage in indexentry)
\renewcommand{\usage}[1]{\textit{\hyperpage{#1}}}
\renewcommand{\main}[1]{\hyperpage{#1}}
\newcommand{\see}[2]{\emph{see} #1}
\makeatletter
\def\index@prologue{\section*{Index}}
\makeatother


\OnlyDescription
\PageIndex

\begin{document} 
\DocInput{probsoln.dtx}
\end{document}
%</driver>
%\fi
%
%
% \title{probsoln.sty v2.03: \LaTeXe\ Package to help create problem sheets}
% \author{Nicola L.C. Talbot}
% \date{20th June 2006}
% \maketitle
% \tableofcontents
%
% \section{Introduction}
%The \sty{probsoln} package is designed for teachers or
%lecturers who want to create problem sheets for their 
%students.  This package was designed with specifically 
%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.
%
%\section{Package Options}
%The following options may be passed to this package:
%\begin{center}
%\begin{tabular}{@{\ttfamily}ll}
%answers & Show the answers\\
%noanswers & Don't show the answers (default)
%\end{tabular}
%\end{center}
%
%\section{Creating a New Problem}
%\DescribeMacro{\newproblem}
%\cmddef{newproblem}\verb|[|\meta{nargs}\verb/]{/\meta{label}\verb!}{!\meta{problem}\verb+}{+\meta{solution}\verb=}=\\[10pt]
%A new problem is defined using the command 
%\cmdname{newproblem}.  This does not print anything,
%but merely stores the problem. As from version 2.03, there is also
%a starred version of this command for problems without a solution
%(for example, essay style questions):\\[10pt]
%\DescribeMacro{\newproblem*}
%\cmddef{newproblem*}\verb|[|\meta{nargs}\verb/]{/\meta{label}\verb!}{!\meta{problem}\verb+}+\\[10pt]
%The argument \meta{label} is
%a unique string that is assigned to this problem so that it
%can be used later.  The argument \meta{problem} is normal 
%\LaTeX\ code that should be used to typeset the problem.  
%The argument \meta{solution} is normal \LaTeX\ code that
%should be used to typeset the solution, if required.  For
%example, the following defines a problem with the label 
%\problabel{quaddiff}{quaddiff}:
%\begin{verbatim}
% \newproblem{quaddiff}{%
% %This is the problem
% \begin{displaymath}
% f(x) = x^2 + 3x + 4
% \end{displaymath}
% }{%
% %This is the solution
% \begin{displaymath}
% f'(x) = 2x + 3
% \end{displaymath}
% }
%\end{verbatim}
%The following is an essay question, so doesn't come with a solution:
%\begin{verbatim}
%\newproblem*{inheritance}{Describe what is meant by the term 
%\emph{inheritance} in object-oriented programming. Use examples.}
%\end{verbatim}
%
%The optional argument \meta{nargs} specifies the number of
%parameters this problem will take.  By default this value is
%0, but any value from 1 to 9 may be used.  Each parameter is
%referred to by \verb|#1|, \verb/#2/, \ldots, \verb!#9!.
%For example, the following problem, labelled \problabel{sindiff}{sindiff}, 
%takes one parameter:
%\begin{verbatim}
%\newproblem[1]{sindiff}{%
%\(f(x) = \sin(#1x)\)
%}{%
%\(f'(x) = #1\cos(#1x)\)
%}
%\end{verbatim}
%
%The \probref{quaddiff} problem shown above can be made more
%generic by using parameters\problabel{diff:quad}{}:
%\begin{verbatim}
%\newcount\ctr
%\newproblem[3]{diff:quad}{%
%\(f(x) = 
%\ifnum#1=0
%\else
%\ifnum#1=1\else#1\fi x^2 
%\fi
%\ifnum#2=0
%\else
%\ifnum#2>0 \ifnum#1=0 \else + \fi \fi
%\ifnum#2=1\else#2\fi x 
%\fi
%\ifnum#3=0
%\else
%\ifnum#3>0 \ifnum#2=0 \ifnum#1=0 \else + \fi \else + \fi\fi
%#3
%\fi\)
%}{%
%\(f'(x) = 
%\ifnum#1=0
%\else
%\ctr=2
%\multiply\ctr by #1
%\the\ctr x 
%\fi
%\ifnum#2=0
%\else
%\ifnum#2>0 \ifnum#1=0 \else + \fi \fi
%#2
%\fi
% % print 0 if both #1 and #2 are 0
%\ifnum#1=0 \ifnum#2=0 0 \fi\fi 
%\)
%}
%\end{verbatim}
%The three parameters correspond to the coefficients.  Note
%that they must all be integers since \TeX\ only performs
%integer arithmetic.
%
%\section{Creating a Database}
%
%To generate a database, simply create a \texttt{.tex} file
%where all the problems are defined using \cmdlink{newproblem} or
%\cmdlink{newproblem*},
%and either \cmdname{input} it at the start of your document if
%you want to use specific problems (see Section~\ref{sec:useprob}), or pass it to 
%\cmdname{selectrandomly} (see Section~\ref{sec:selran}). This package
%comes with some sample databases, along with some sample documents
%that use these databases.
%
%\section{Displaying a Problem}
%\label{sec:useprob}
% \DescribeMacro{\useproblem}
%\cmddef{useproblem}\verb|{|\meta{label}\verb/}/\\[10pt]
%Once a problem has been defined using \cmdlink{newproblem}, it 
%can be typeset using the command \cmdname{useproblem}.  If the
%problem was defined to take arguments, the arguments to the
%problem should come after the label.  In the case of the
%\probref{sindiff} example above, the command 
%\verb|\useproblem{sindiff}{2}| would produce the following:\\[10pt]
% $f(x) = \sin(2x)$ \paragraph{Solution:} $f'(x) = 2\cos(2x)$\\[10pt]whereas the command \verb|\useproblem{diff:quad}{3}{0}{-2}| would produce:\\[10pt]
% $f(x) = 3x^2 - 2$ \paragraph{Solution:} $f'(x) = 6x$\vspace{10pt}
%
%Suppose all the above problems are defined in the file 
%\texttt{probs.tex}, then the following code will create a
%problem sheet with four questions in it:
%\begin{verbatim}
%\documentclass{article}
%
%\usepackage{probsoln}
%\input{probs}
%
%\begin{document}
%Differentiate the following functions with respect to $x$:
%\begin{enumerate}
%\item \useproblem{quaddiff}
%\item \useproblem{sindiff}{4}
%\item \useproblem{diff:quad}{2}{3}{1}
%\item \useproblem{diff:quad}{0}{1}{2}
%\end{enumerate}
%\end{document}
%\end{verbatim}
%The answer sheet can then be generated by passing the option
%\texttt{answers} to the \texttt{probsoln} package.
%
%\section{Selecting All Problems}
%
%To select all problems defined in a database, in the order in
%which they were defined, use the command:
%\DescribeMacro{\selectallproblems}
%\cmddef{selectallproblems}\verb|{|\meta{filename}\verb/}/
%For example, suppose the problems are defined in the file
%\texttt{easy.tex}, then the following will create a problem sheet 
%which uses all these problems:
%\begin{verbatim}
%\documentclass[a4paper]{article}
%
%\usepackage{probsoln}
%
%\begin{document}
%Differentiate the following functions:
%\begin{enumerate}
%\selectallproblems{easy}
%\end{enumerate}
%\end{document}
%\end{verbatim}
%
%\section{Selecting Problems at Random}
%\label{sec:selran}
% \DescribeMacro{\selectrandomly}
%\cmddef{selectrandomly}\verb|{|\meta{filename}\verb/}{/\meta{n}\verb!}!\\[10pt]
%The command \cmdname{selectrandomly} will select \meta{n}
%problems that are defined in the file \meta{filename}.  Each
%problem is proceeded by 
%\DescribeMacro{\PSNitem}\cmddef{PSitem} which is
%defined to be \cmdname{item}, so the command 
%\cmdname{selectrandomly} should occur within one of the
%list-like environments, such as \texttt{enumerate}.  
%For example:
%\begin{verbatim}
%\begin{enumerate}
%\selectrandomly{easy.tex}{4}
%\end{enumerate}
%\end{verbatim}
%will result in four numbered problems, selected at random
%from the file \texttt{easy.tex}. (The \texttt{.tex} extension
%may be omitted.) Each problem is followed by the command
%\cmddef{endPSNitem} which by default does nothing.
%
%Multiple \cmdname{selectrandomly} commands may be used, however a
%different file must be used each time.  For
%example:
%\begin{verbatim}
%\begin{enumerate}
%\item Differentiate the following functions with respect to $x$:
%
%\begin{enumerate}
%\selectrandomly{samples/easy.tex}{3}
%\selectrandomly{samples/args.tex}{1}
%\end{enumerate}
%
%\selectrandomly{samples/implicit.tex}{1}
%\selectrandomly{samples/1stprncpl.tex}{1}
%
%\end{enumerate}
%\end{verbatim}
%This will result in a total of 6 problems, numbered 1(a),
%1(b), 1(c), 1(d), 2 and 3.
%
%If a randomly selected problem requires arguments, a message
%similar to the following will be displayed:
%\begin{verbatim}
%Problem diff:quad requires 3 argument(s), please specify (e.g. {5}{3}):
%\end{verbatim}
%Enter the required arguments, where each argument is
%enclosed in braces (\{ \}).
%
%\section{Other Commands}
%\DescribeMacro{\PSNrandseed}
%The command \cmddef{PSNrandseed}\verb|{|\meta{n}\verb/}/ specifies 
%the seed for the random number generator.  For example,
%if you are using \cmdlink{selectrandomly},
%\verb|\PSNrandseed{\year}| will produce a different set of
%problems each year, whereas \verb|\PSNrandseed{\time}|
%will produce a different set of problems each time you 
%\LaTeX\ the problem sheet (as long as you leave at least a
%minute between runs.)
%
%\DescribeMacro{\random}
%The command \cmddef{random}\verb|{|\meta{counter}\verb/}{/\meta{a}\verb!}{!\meta{b}\verb-}-
%generates a random number from \meta{a} to \meta{b} and
%stores it in the \LaTeX\ counter \meta{counter}. For example,
%to select 2, 3 or 4 problems from the file
%\texttt{implicit.tex}:
%\begin{verbatim}
%\newcounter{numproblems}
%\begin{enumerate}
%\random{numproblems}{2}{4}
%\selectrandomly{implicit.tex}{\value{numproblems}}
%\end{enumerate}
%\end{verbatim}
%(Note the use of \cmdname{value}.)
%
%This command can also be used to generate random values for
%problems that take arguments. Consider the problem \probref{diff:quad}
%defined earlier. Three counters can be defined to represent the
%three coefficients:
%\begin{verbatim}
%\newcounter{A}
%\newcounter{B}
%\newcounter{C}
%\end{verbatim}
%Random values can now be assigned to these counters:
%\begin{verbatim}
%\random{A}{-5}{5}
%\random{B}{-5}{5}
%\random{C}{-5}{5}
%\end{verbatim}
%Finally, the problem can be used (note the use of \cmdname{arabic}):
%\begin{verbatim}
%\useproblem{diff:quad}{\arabic{A}}{\arabic{B}}{\arabic{C}}
%\end{verbatim} 
%
%\DescribeMacro{\doforrandN}%
%The command 
%\cmddef{doforrandN}\verb|{|\meta{n}\verb/}{/\meta{cmd}\verb!}{!\meta{list}\verb+}{+\meta{text}\verb-}- will apply
%\meta{text} for a random selection of \meta{n} items in the
%comma separated \meta{list}. In each iteration the list
%item is denoted by \meta{cmd}. For example, suppose you 
%have three files called \texttt{file1.tex}, 
%\texttt{file2.tex} and \texttt{file3.tex}, and you want to
%select 1 problem from two of the three files, then you can
%do:
%\begin{verbatim}
%\doforrandN{2}{\file}{file1,file2,file3}{\selectrandomly{\file}{1}}
%\end{verbatim}
%Note that it is also possible to do
%\begin{verbatim}
%\newcounter{numproblems}
%\random{numproblems}{1}{3}
%\selectrandomly{file\arabic{numproblems}}{1}
%\random{numproblems}{1}{3}
%\selectrandomly{file\arabic{numproblems}}{1}
%\end{verbatim}
%however there is a possibility that the same file may be
%selected twice which will cause an error.
%
%\DescribeMacro{\showanswers}
%The command \cmddef{showanswers} will show the solutions from
%that point on.  May be localised by placing within a group.
%
%\DescribeMacro{\hideanswers}
%The command \cmddef{hideanswers} will hide the solutions from
%that point on.  May be localised by placing within a group.
%
%The solution is placed inside the \DescribeEnv{solution}\env{solution} 
%environment. By default this environment simply does
%\verb|\par\noindent\textbf{\solutionname:} | at the start, where 
%\DescribeMacro{\solutionname}\cmddef{solutionname}
%has the value: Solution. See the file \texttt{sample3.tex}, which
%comes with this package, for an illustration of how to 
%customise the way in which the randomly selected problems are
%displayed. This sample file randomly selects multiple choice
%problems stored in the file \texttt{tabmchoice.tex}, and displays them
%in a longtable \env{environment}. As from version 2.03, the package 
%will not define this environment if another package or class file has
%already defined it.
%
%\DescribeMacro{showanswers}
%The boolean variable \texttt{showanswers} is defined to be
%true if the answers are shown and false otherwise.  You can
%therefore do something like:
%\begin{verbatim}
%\ifthenelse{\boolean{showanswers}}{\textbf{Solution Sheet}}{}
%\end{verbatim}
%and \textbf{Solution Sheet} will be printed only if the
%answers are displayed.  (For more information on 
%\cmdname{ifthenelse} and \cmdname{boolean} see the 
%documentation for the \texttt{ifthen} package by 
%David Carlisle.)
%
%The \texttt{showanswers} switch can also be used within the definition
%of a problem, if you want the question to appear differently if the
%solution is displayed. For example:
%\begin{verbatim}
%\newproblem{mc:prod}{%
%Which of the following is the derivative of $x\sin(x)$?
%(Circle the correct answer.)
%\ifthenelse{\boolean{showanswers}}{}%
%{\begin{description}
%\item[A] $\sin(x)$
%\item[B] $x\cos(x)$
%\item[C] $\sin(x) + x\cos(x)$
%\end{description}
%}}{%
%\begin{description}
%\item[A] $\sin(x)$
%\item[B] $x\cos(x)$
%\item[\textcircled{C}] $\sin(x) + x\cos(x)$ (product rule).
%\end{description}
%}
%\end{verbatim}
%If the solutions are not displayed, the question will appear
%as
%\begin{description}
%\item Which of the following is the derivative of 
%$x\sin(x)$? (Circle the correct answer.)
%\begin{description}
%\item[A] $\sin(x)$\item[B] $x\cos(x)$\item[C] $\sin(x) + x\cos(x)$\end{description}
%\end{description}
%otherwise it will appear as:
%\begin{description}
%\item Which of the following is the derivative of 
%$x\sin(x)$? (Circle the correct answer.)
%\begin{description}
%\item[A] $\sin(x)$\item[B] $x\cos(x)$\item[\textcircled{C}] $\sin(x) + x\cos(x)$ (product rule)\end{description}
%\end{description}
%
%\section{Troubleshooting}
%\subsection{Unexpected Output}
%\begin{itemize}
%\item I have lots of blank space before the first problem when
%using \cmdlink{selectrandomly}.
%
%This is probably because you have lots of extraneous white space
%in your database. \cmdlink{selectrandomly} will input the entire
%file, so any extra space will be included. Try commenting out the
%extra space using \%.
%\end{itemize}
%
%\subsection{Error Messages}
%\begin{itemize}
%\item
%\begin{ttfamily}
%! Package probsoln Error: Label \ldots\ already used.
%\end{ttfamily}
%
%Each label identifier used in \cmdlink{newproblem} must be
%unique.  Check to make sure you haven't used the same label 
%more than once.  Also check to make sure you haven't 
%\cmdname{input}ed or randomly selected from the same file more
%than once. (Or \cmdname{input}ed and randomly selected from
%the same file.)
%
%\item
%\begin{ttfamily}
%! Package probsoln Error: Label \ldots\ undefined.
%\end{ttfamily}
%
%You need to define a problem before you can use it.  Check to
%make sure you haven't mis-spelt it, and check to make sure you
%have \cmdname{input}ed the file in which it is defined.
%
%\item
%\begin{ttfamily}
%! Package probsoln Error: Requested number too large.
%\end{ttfamily}
%
%You have asked for more problems than are defined within the
%specified file.  All problems in that file will be selected.
%
%\item
%\begin{ttfamily}
%! Package probsoln Error: Can't randomly select \meta{n}
%item(s)
%\end{ttfamily}
%
%You have asked to randomly select \meta{n} items from a list
%that has less than \meta{n} elements. For example, the
%following will generate this error:
%\begin{verbatim}
%\doforrandN{10}{\file}{file1,file2,file3}{%
%\selectrandomly{\file}{1}}
%\end{verbatim}
%In this case the list, \texttt{file1,file2,file3} has only 3
%elements, but the user has asked for 10 elements. If you type
%`h' at the \LaTeX\ prompt it will tell you how many items it thinks
%there are in the list. Remember that each item must be separated by a
%comma.
%
%\item
%\begin{ttfamily}
%! LaTeX Error: Lonely \cmdname{item}--perhaps missing list
%environment.
%\end{ttfamily}
%
%Each problem selected using \cmdlink{selectrandomly} is
%proceeded by \cmdlink{PSNitem} which by default is defined
%as \cmdname{item}, and should therefore be 
%placed in one of the list environments, such as 
%\texttt{enumerate}. Alternatively, redefine \cmdlink{PSNitem}.
%
%\item I get an error when I put a command definition in my database
%when using \cmdname{selectrandomly}.
%
%\cmdlink{selectrandomly} inputs the database twice, so any 
%command definition will be read twice, causing an error. You
%can use the switch \cmdname{iffirstpass} to prevent the error. For
%example:
%\begin{verbatim}
%\iffirstpass
%\newcommand{\mycmd}{}
%\fi
%\end{verbatim}
%It is generally not a good idea to put anything other than
%\cmdlink{newproblem} commands within the database.
%
%\end{itemize}
%
%\section{Contact Details}
%Dr Nicola Talbot\\
%School of Computing Sciences\\
%University of East Anglia\\
%Norwich. NR4 7TJ\\
%\url{http://theoval.cmp.uea.ac.uk/~nlct/}
%
%\StopEventually{\PrintIndex}
%
%\section{The Code}
%\iffalse
%    \begin{macrocode}
%<*probsoln.sty>
%    \end{macrocode}
%\fi
% At the start of the package, specify that the \LaTeXe\ format is
% required.  And specify the package provided.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{probsoln}[2006/06/20 v2.03 Problems and their Solutions Package (NLCT)]
\RequirePackage{ifthen}
%    \end{macrocode}
% Define newif to determine whether or not to show the answers.
% (Changed to |\provideboolean| 21 Mar 2004.)
%    \begin{macrocode}
\provideboolean{showanswers}
\newcommand{\showanswers}{\showanswerstrue}
\newcommand{\hideanswers}{\showanswersfalse}
%    \end{macrocode}
% Now define the package options
%    \begin{macrocode}
\DeclareOption{answers}{\showanswerstrue}
\DeclareOption{noanswers}{\showanswersfalse}
\ExecuteOptions{noanswers}
\ProcessOptions
%    \end{macrocode}
% Create newifs for selecting randomly and parsing through file
%    \begin{macrocode}
\newif\ifselectrandom \selectrandomfalse
\newif\iffirstpass
\newif\ifselected
%    \end{macrocode}
% Define the command that creates a new problem. As from version
% 2.03 this command now has a starred version for problems without
% solutions.
%    \begin{macrocode}
\newcommand{\newproblem}{\@ifstar\@snewproblem\@newproblem}
%    \end{macrocode}
% The unstarred version.
%    \begin{macrocode}
\newcommand{\@newproblem}[4][0]{%
\ifselectrandom
\iffirstpass
% on the first pass, just store the label, and increment \@probN
\global\advance\@probN by 1
\expandafter\gdef\csname @problabel\romannumeral\@probN\endcsname{#2}%
\else
% check to see if this problem has been selected
\checkselected{#2}%
\ifselected
\@ifundefined{@prob@#2}{%
\ifnum#1=0
% this problem has no arguments
\expandafter\gdef\csname @prob@#2\endcsname{%
#3 \ifshowanswers\begin{solution}#4\end{solution}\fi}%
\expandafter\gdef\csname @prob@#2@arg\endcsname{\relax}%
\else
\message{Problem #2 requires #1 argument(s), please specify (e.g. {5}{3}):}%
\read-1to\@tmp
\expandafter\xdef\csname @prob@#2@arg\endcsname{\@tmp}%
\expandafter\global\expandafter\newcommand\csname @prob@#2\endcsname[#1]{%
#3 \ifshowanswers\begin{solution}#4\end{solution}\fi}%
\fi
}{%
\PackageError{probsoln}{Label #2 already used}%
{Each problem must have a unique label identifier}%
}%
\fi
\fi
\else
\@ifundefined{@prob@#2}{%
\expandafter\global\expandafter\newcommand\csname @prob@#2\endcsname[#1]{%
#3 \ifshowanswers\begin{solution}#4\end{solution}\fi}%
}{%
\PackageError{probsoln}{Label #2 already used}%
{Each problem must have a unique label identifier}%
}
\fi
}
%    \end{macrocode}
% The starred version (new to version 2.03). Note that there is one 
% less argument, because the problem doesn't have a solution.
%    \begin{macrocode}
\newcommand{\@snewproblem}[3][0]{%
\ifselectrandom
\iffirstpass
% on the first pass, just store the label, and increment \@probN
\global\advance\@probN by 1
\expandafter\gdef\csname @problabel\romannumeral\@probN\endcsname{#2}%
\else
% check to see if this problem has been selected
\checkselected{#2}%
\ifselected
\@ifundefined{@prob@#2}{%
\ifnum#1=0
% this problem has no arguments
\expandafter\gdef\csname @prob@#2\endcsname{#3}%
\expandafter\gdef\csname @prob@#2@arg\endcsname{\relax}%
\else
\message{Problem #2 requires #1 argument(s), please specify (e.g. {5}{3}):}%
\read-1to\@tmp
\expandafter\xdef\csname @prob@#2@arg\endcsname{\@tmp}%
\expandafter\global\expandafter\newcommand\csname @prob@#2\endcsname[#1]{#3}%
\fi
}{%
\PackageError{probsoln}{Label #2 already used}%
{Each problem must have a unique label identifier}%
}%
\fi
\fi
\else
\@ifundefined{@prob@#2}{%
\expandafter\global\expandafter\newcommand\csname @prob@#2\endcsname[#1]{#3}%
}{%
\PackageError{probsoln}{Label #2 already used}%
{Each problem must have a unique label identifier}%
}
\fi
}
%    \end{macrocode}
% Define the command that uses a problem
%    \begin{macrocode}
\newcommand{\useproblem}[1]{%
\@ifundefined{@prob@#1}{%
\PackageError{probsoln}{Label #1 undefined}%
{Can't find problem defined with label identifier '#1'}%
}{\csname @prob@#1\endcsname}%
}
%    \end{macrocode}
% Now deal with the stuff for random selection.  
% First define some registers for later use.
%    \begin{macrocode}
\newcount\@probN \newcount\@probselN \newcount\@rndselctr
\newcount\r@ndcur
\newcount\@ps@randtmp
\r@ndcur=1\relax
%    \end{macrocode}
% Set the random generator seed
%    \begin{macrocode}
\newcommand{\PSNrandseed}[1]{%
\ifnum#1=0\relax
\PackageWarning{probsoln}{Can't have 0 as random seed, changing to 1}%
\r@ndcur=1\relax
\else
\r@ndcur=#1\relax
\fi
\PackageInfo{probsoln}{Random Seed = \the\r@ndcur}%
}
%    \end{macrocode}
% Generate a random integer.
%    \begin{macrocode}
\newcommand{\PSNrand}{%
\@ps@randtmp=\r@ndcur
\multiply\@ps@randtmp by 16807\relax
\r@ndcur=\@ps@randtmp
\divide\r@ndcur by 120001\relax
\multiply\r@ndcur by 120001\relax
\advance\@ps@randtmp by -\r@ndcur
\r@ndcur = \@ps@randtmp
\ifnum\r@ndcur=0\relax\r@ndcur=1\fi
}
%    \end{macrocode}
% |\PSNrandom{|\meta{count}|}{|\meta{n}|}| stores a random number 
% from 1 to \meta{n}
% in the TeX count register \meta{count}
%    \begin{macrocode}
\newcommand{\PSNrandom}[2]{%
% generate new random number.
\PSNrand
#1=\r@ndcur
\@ps@randtmp=\r@ndcur
% now (#1 mod #2) + 1
\divide\@ps@randtmp by #2\relax
\multiply\@ps@randtmp by #2\relax
\advance#1 by -\@ps@randtmp
\advance#1 by 1\relax
}
%    \end{macrocode}
% |\random{|\meta{counter}|}{|\meta{a}|}{|\meta{b}|}|:
% Generate a random number in the range $[a,b]$, and store this
% number in the \LaTeX\ counter \meta{counter}.
%    \begin{macrocode}
\newcommand{\random}[3]{%
\ifnum#2=1\relax
\PSNrandom{\value{#1}}{#3}%
\else
\@rndselctr=#3%
\advance\@rndselctr by -#2\relax
\advance\@rndselctr by 1\relax
\PSNrandom{\value{#1}}{\@rndselctr}%
\addtocounter{#1}{#2}%
\addtocounter{#1}{-1}%
\fi
}
%    \end{macrocode}
% Define command to use all problems from a given file.
% Syntax: |\selectallproblems{|\meta{filename}|}|.
%    \begin{macrocode}
\newcommand*{\selectallproblems}[1]{%
\global\@probN=0\relax
\selectrandomtrue
\firstpasstrue
\input{#1}%
\firstpassfalse
\selectrandomfalse
\input{#1}%
\global\@rndselctr=1\relax
\whiledo{\@rndselctr < \@probN \TE@or \@rndselctr = \@probN}{%
\edef\@tmp{\csname @problabel\romannumeral\@rndselctr\endcsname}%
\edef\@probargs{{\@tmp}\csname @prob@\@tmp @arg\endcsname}%
\PSNitem \expandafter\useproblem\@probargs \endPSNitem
\global\advance\@rndselctr by 1\relax
}}
%    \end{macrocode}
% Now define command to randomly select \meta{n} problems from
% a given file.
% Syntax: |\selectrandomly{|\meta{filename}|}{|\meta{n}|}|.
%    \begin{macrocode}
\newcommand{\selectrandomly}[2]{%
\global\@probselN=#2\relax
\global\@probN=0\relax
\selectrandomtrue
\firstpasstrue
\input{#1}%
\ifnum\@probselN>\@probN
\PackageError{probsoln}{Requested number too large}%
{You have asked for \the\@probselN \space problems, but there are only
\the\@probN \space problems defined in the file #1.  I will only select \the\@probN \space
problems.}%
\global\@probselN=\@probN
\fi
\shuffle{@problabel}{\@probN}%
\firstpassfalse
\input{#1}%
\selectrandomfalse
\global\@rndselctr=1\relax
\whiledo{\@rndselctr < \@probselN \TE@or \@rndselctr = \@probselN}{%
\edef\@tmp{\csname @problabel\romannumeral\@rndselctr\endcsname}%
\edef\@probargs{{\@tmp}\csname @prob@\@tmp @arg\endcsname}%
\PSNitem \expandafter\useproblem\@probargs \endPSNitem
\global\advance\@rndselctr by 1\relax
}%
}
%    \end{macrocode}
% What to do at start and end of each randomly selected item:
% (I may turn this into a normal environment at a later date. At the
% moment, I have kept \verb|\PSNitem| for compatibility with earlier
% versions.)
%    \begin{macrocode}
\newcommand{\PSNitem}{\item}
\@ifundefined{endPSNitem}{\def\endPSNitem{}}{%
\PackageError{probsoln}{\string\endPSitem\ already defined}{}}
%    \end{macrocode}
% How to display each solution
%    \begin{macrocode}
\providecommand{\solutionname}{Solution}
\@ifundefined{solution}{%
\newenvironment{solution}{\par\noindent\textbf{\solutionname:} }{}}{%
}
%    \end{macrocode}
% Check to see if label has been selected
%    \begin{macrocode}
\newcount\@ckselctr
\newcommand{\checkselected}[1]{%
\selectedfalse
\@ckselctr=1\relax
\whiledo{\@ckselctr < \@probselN \TE@or \@ckselctr = \@probselN}{%
\ifthenelse{\equal{#1}{\csname @problabel\romannumeral\@ckselctr\endcsname}}%
{\global\selectedtrue \global\@ckselctr=\@probselN}{}%
\advance\@ckselctr by 1\relax
}%
}
%    \end{macrocode}
% Shuffle contents of pseudo-array. For example, suppose you have
% the following definitions: |\def\fooi{A}|, |\def\fooii{B}| and
% |\def\fooiii{C}|, then |\shuffle{foo}{3}| will shuffle the
% definitions, so you may end up with, e.g.\ |\def\fooi{C}|, 
% |\def\fooii{A}|, |\def\fooiii{B}|, or some other variation.
%    \begin{macrocode}
\newcount\@shfctr \newcount\@shfA \newcount\@shfB
\newcommand{\shuffle}[2]{%
\@shfctr=1\relax
\whiledo{\@shfctr < 101}{%
\PSNrandom{\@shfA}{#2}\PSNrandom{\@shfB}{#2}%
\ifnum\@shfA=\@shfB
\else
\edef\@@tmpA{\csname#1\romannumeral\@shfA\endcsname}%
\let\@tmpA=\@@tmpA
\edef\@@tmpB{\csname#1\romannumeral\@shfB\endcsname}%
\let\@tmpB=\@@tmpB
\expandafter\xdef\csname#1\romannumeral\@shfA\endcsname{\@tmpB}%
\expandafter\xdef\csname#1\romannumeral\@shfB\endcsname{\@tmpA}%
\fi
\advance\@shfctr by 1\relax
}%
}
%    \end{macrocode}
% Syntax: |\doforrandN{|\meta{n}|}{|\meta{cmd}|}{|\meta{list}|}{|\meta{text}|}|.
% A bit like |\@for| but only for a random subset of the given list. 
% For example,
%\begin{verbatim}
%\doforrandN{2}{\tmp}{file1,file2,file3}{%
%\selectrandomly{\tmp}{1}}
%\end{verbatim}
%This will select one problem each from two out of the three listed
%files.
%    \begin{macrocode}
\newcount\@ps@forrand
\newcommand{\doforrandN}[4]{%
{\@ps@forrand=0\relax
\@for#2:=#3\do{%
\advance\@ps@forrand by 1\relax
\expandafter\edef\csname @doforrandN@\romannumeral\@ps@forrand\endcsname{#2}}%
\ifnum\@ps@forrand<#1\relax
\PackageError{probsoln}{Can't randomly select \number#1 item(s)}{You 
have requested \number#1 item(s), but there
are only \number\@ps@forrand item(s) in the list}%
\else
\shuffle{@doforrandN@}{\@ps@forrand}%
\ifnum#1>0\relax
\@ps@forrand=0\relax
\loop
\advance\@ps@forrand by 1\relax
\edef#2{\csname @doforrandN@\romannumeral\@ps@forrand\endcsname}%
#4%
\ifnum\@ps@forrand<#1\relax
\repeat
\fi
\fi
}}
%    \end{macrocode}
%\iffalse
%    \begin{macrocode}
%</probsoln.sty>
%    \end{macrocode}
%\fi
%\Finale
\endinput