summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/exam-randomizechoices/exam-randomizechoices-doc.tex
blob: a8b00f9b10655bebc0cd8d2044edf368265cb402 (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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
%%--------------------------------------------------------------------
%%--------------------------------------------------------------------
%% exam-randomizechoices-doc.tex
%%
%% This is the user's guide for the exam-randomizechoices package, 
%% by Jesse op den Brouw
%%
%% The package itself is in the file exam-randomizechoices.sty.


%% Copyright (c)2019, Jesse E. J. op den Brouw
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX 
%% version 2003/12/01 or later.
%%
%% This work consists of the files exam-randomizechoices.sty,
%% exam-randomizechoices.tex and exam-randomizechoices-doc.tex

%% This software is provided 'as is', without warranty of any kind,
%% either expressed or implied, including, but not limited to, the
%% implied warranties of merchantability and fitness for a
%% particular purpose.

%% Jesse op den Brouw
%% Department of Electrical Engineering
%% The Hague University of Applied Sciences
%% Rotterdamseweg 137, 2628 AL, Delft
%% Netherlands
%% J.E.J.opdenBrouw@hhs.nl

%% The newest version of this package should always be available
%% from BitBucket: https://bitbucket.org/jesseopdenbrouw/exam-randomizechoices/src/master/

%--------------------------------------------------------------------
%--------------------------------------------------------------------

\documentclass[12pt,a4paper]{exam}

\newcommand{\styversion}{\fileversion}
\newcommand{\stydate}{\filedate}
\newcommand{\docdate}{January 9, 2019}
%\newcommand{\docdate}{Draft: \today}

\usepackage{mathtools}
%\usepackage{amssymb}

%% Set input encoding to UTF-8
\usepackage[utf8]{inputenc}
%% Use T1 output font encoding
\usepackage[T1]{fontenc}

%% Set page layout
\usepackage[a4paper,left=1.0in,right=1.0in,top=1.0in,bottom=1.4in,footskip=0.5in]{geometry}

%% Use package enumitem
\usepackage{enumitem}
                   
%% Load English spelling
\usepackage[english]{babel}

%% Nice tables
\usepackage{tabu}

%% Colors
\usepackage[x11names]{xcolor}

%% Use the Charter font + math symbols
\usepackage[scaled=0.9]{nimbusmono}
\usepackage[bitstream-charter]{mathdesign}
%% Use microtype
\usepackage[stretch=10]{microtype}

\usepackage{listings}
\usepackage{textcomp}

\lstset{ %
  language=[AlLaTeX]TeX,
  basicstyle=\ttfamily,
  numbers=left,
  numberstyle=\tiny\color{gray},
  stepnumber=1,                           
  numbersep=8pt,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  frame=lines,
  rulecolor=\color{gray},
  tabsize=4,
  captionpos=b,
  breaklines=true,
  breakatwhitespace=true,
  title=\lstname,
  upquote=true,
  aboveskip=0.3\baselineskip,
  belowskip=-.5\baselineskip,
  escapeinside={(*}{*)}
}

\usepackage[colorlinks,linkcolor=blue]{hyperref}

\usepackage{titlesec}
\usepackage{titletoc}
\titleformat{\section}{\fontfamily{phv}\selectfont\large\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}{\fontfamily{phv}\selectfont\bfseries}{\thesubsection}{1em}{}
\titlespacing*{\section}{0pt}{\baselineskip}{\aftersubsection}
\titlespacing*{\paragraph}{0pt}{1.0ex plus 1ex minus .2ex}{1.5em}
\newlength{\aftersubtitle}
\setlength{\aftersubtitle}{1.2\baselineskip}
\newlength{\aftersubsection}
\setlength{\aftersubsection}{\aftersubtitle}
\addtolength{\aftersubsection}{-\baselineskip}
\titlespacing*{\subsection}{0pt}{.8\baselineskip}{\aftersubsection}
\titlespacing*{\subsubsection}{0pt}{.6\baselineskip}{0pt}

%% parskip et al.                                            
\usepackage{parskip}

%% The randomization package
\usepackage[randomize,nokeeplast]{exam-randomizechoices}

%% Set seed
\setrandomizerseed{1}

% In case we're not using hyperref.sty:
\providecommand{\texorpdfstring}[2]{#1}
% The following can be used in \section commands
% without generating pdf warnings:
\newcommand{\bs}{\texorpdfstring{\char`\\}{}}


%---------------------------------------------------------------------
%---------------------------------------------------------------------
%---------------------------------------------------------------------
%---------------------------------------------------------------------

\begin{document}
\raggedbottom

\cfoot[]{Page \thepage\ of \numpages}

\title{The \texttt{exam-randomizechoices} package\\[2ex]\normalsize LaTeX package for creating random placed choices in multiple choice \\environments using the \texttt{exam} document class}

\author{Jesse op den Brouw\\
  Department of Electrical Engineering\\
  The Hague University of Applied Sciences\\
  Delft, Netherlands\\
  \href{mailto:J.E.J.opdenBrouw@hhs.nl}{J.E.J.opdenBrouw@hhs.nl}\\[\bigskipamount]
  Copyright \copyright \the\year\ Jesse op den Brouw\\
  All rights reserved}

\date{\docdate}

\maketitle

\thispagestyle{empty}

%% We like the metric system...
\vspace*{2cm}

\begin{center}
  \small
  This is the user's guide for version~\styversion{} [\stydate] of the
  \verb|exam-randomizechoices| package. 
\end{center}

\clearpage
\tableofcontents

\clearpage


\section{Introduction}
This document describes the \LaTeX\ \texttt{exam-randomizechoices} package.
The package provides the user with four new multiple choice typesetting
environments which place the content in a random order. It can (only) be used
in combination with the \texttt{exam} document class. It can only
randomize the placement of choices in multiple choice questions. The questions
themselves can't be randomized with this package.

Furthermore, the package provides a simple answer key table typesetter and has
a command for writing the answer keys to an external file.

\subsection{License and warranty}
This work may be distributed and/or modified under the
conditions of the \LaTeX{} Project Public
License, either version~1.3 of this
license or (at your option) any later version. The latest version
of this license is in \url{http://www.latex-project.org/lppl.txt}
and version 1.3 or later is part of all distributions of \LaTeX{}
version 2003/12/01 or later.

This work has the LPPL maintenance status ``author-maintained''.

This work consists of the files \texttt{exam-randomizechoices.sty},
\texttt{exam-randomizechoices.tex} and \texttt{exam-randomizechoices-doc.tex}

This software is provided `as is', without warranty of any kind,
either expressed or implied, including, but not limited to, the
implied warranties of merchantability and fitness for a
particular purpose.

\subsection{Where to find}
The development version of this package is available on BitBucket. See

\url{https://bitbucket.org/jesseopdenbrouw/exam-randomizechoices/}

\subsection{Acknowledgements}
The author wishes to thank the developers of the \verb|exam| document
class and the \verb|mcexam|, \verb|environ|, \verb|etoolbox| and \verb|pgffor|
packages.

\subsection{Warning}
This package is experimental, so it could possibly break \LaTeX{}
compilation. Use this package with care. Please report any problems
to the author.

Please use a recent version of the \texttt{exam} document class. This
package is tested with version 2.603 which is available in most distributions.
Testing with version 2.605\$\bs beta\$ is planned.

\subsection{A word about reading this document}
This document uses terminology which is described below:

\begin{itemize}
\item If you encounter the term ``the standard multiple choice environments'',
you should read this as ``the \texttt{choices}, \texttt{oneparchoices},
\texttt{checkboxes} and \texttt{oneparcheckboxes} environments''.

\item If you encounter the term ``the new multiple choice environments'',
you should read this as ``the \texttt{randomizechoices}, \texttt{randomizeoneparchoices},
\texttt{randomizecheckboxes} and \texttt{randomizeoneparcheckboxes} environments''.

\item If you encounter the term ``\texttt{*choices}'', you should read this as
``\texttt{choices}, \texttt{oneparchoices}, \texttt{randomizechoices} and \texttt{randomizeoneparchoices}''.

\item If you encounter the term ``\texttt{*checkboxes}'', you should read this as
``\texttt{checkboxes}, \texttt{oneparcheckboxes}, \texttt{randomizecheckboxes} and
\texttt{randomizeoneparcheckboxes}''.

\item If you encounter the term ``command'' in the context of a backslash followed
by a series of characters, you can read this as ``macro''. The official \TeX-name
is ``control sequence''.
\end{itemize}


\section{Using the \texttt{exam} class}
This section provides a limited introduction to the \texttt{exam} document
class. As a novice user, please read on. If you are a experienced user,
you may skip this section.

The \verb|exam| document class is a powerful class to create exams
with \LaTeX{}. Both open questions and multiple choice questions are
supported. For multiple choice questions we can differentiate between
enumerated lists or checkbox lists. The class documentation states%
\footnote{See \url{https://ctan.org/tex-archive/macros/latex/contrib/exam}.}:

\begin{quote}
The file \verb"exam.cls" provides the \verb"exam" document class,
which attempts to make it easy for even a \LaTeX{} novice to prepare
exams.  Specifically, \verb"exam.cls" sets the page layout so that
there are one inch margins\index{margins} all around (no matter what
size paper you're using) and provides commands that make it easy to
format questions, create flexible headers and footers, change the
margins, and create grading tables.  In more detail:
\begin{itemize}
\item The class will automatically format and number the questions,
  parts of questions, subparts of parts, and subsubparts of subparts.
\item You can include the point value\index{points} of each question
  (or part, or subpart, or subsubpart), with your choice of having the
  point values printed at the beginning of the text of the question,
  opposite that in the left margin, opposite that in the right margin,
  or in the right margin opposite the end of the question.
\item The class will add up the total points for each question (and
  all of its parts, subparts, and subsubparts) and the total
  points\index{points!total} on each page, and make those totals
  available in macros.
\item You can have the class print a grading table, indexed either by
  question number or by page number.
\item You specify the header in three parts: One part to be left
  justified, one part to be centered, and one part to be right
  justified, and one or all of these can be omitted.
\item The footer is also specified in three parts: Left justified,
  centered, and right justified.
\item The header and footer for the first page can be different from
  the ones used on other pages.
\item Both headers and footers can contain more than one line. To
  accommodate headers and footers with several lines, simple commands
  are provided to enlarge the part of the page devoted to the header
  and/or footer, and these commands can give one amount of space on
  the first page and a different amount of space on all other pages.
\item Macros are defined to enable you to state the total number of
  pages in the exam and to change the
  header and/or footer that appears on the last page of the exam .
\item Macros are defined so that the headers and footers can vary
  depending on whether the current page begins a new question or
  continues a question that started on an earlier page (and, if one
  continues onto the current page, to say what the number of that
  question is).  Macros are also defined so that the headers and
  footers can vary depending on whether a question is complete on the
  current page or continues on to the next page (and, if one
  continues, to say what the number of that question is).
\item You can have a horizontal rule at the base of the header and/or
  at the top of the footer.
\item The exam can begin with one or more cover pages, which are
  numbered separately from the main pages of the exam and which can
  have headers and footers different from the ones in the main pages
  of the exam.
\item You can include solutions in your \LaTeX{} file and have these
  solutions either printed or ignored (or replaced automatically by
  space in which the students can write their answers) depending on a
  single command.
\end{itemize}
\end{quote}

Furthermore, not stated in this excerpt, you can typeset bonus questions
with bonus points and grading tables with bonus points.

You can load the \texttt{exam} document class the usual way. An example
might be:

\begin{lstlisting}
\documentclass[a4paper,12pt,addpoints]{exam}
\end{lstlisting}

which loads the \texttt{exam} document class. The paper size is set to
A4 (297 mm $\times$ 210 mm, 11.7~in $\times$ 8.3 in), the document is
typeset with a 12 points font and the question points are calculated.


\subsection{Typesetting multiple choice questions}
Then, within the document body and between \texttt{\bs begin\{questions\}} and
\texttt{\bs end\{questions\}}, you enter the questions. Only multiple choice
questions are considered here. The \texttt{exam} document class provides four types
of multiple choice question environments:

\begin{description}[labelindent=2ex]
\item[\texttt{choices}] The given choices are typeset in a linear, vertical list.
Each given choice is prepended with a label name which can be set to uppercase
letter, lowercase letter, Roman numerals (uppercase and lowercase) and the Greek
alphabet\footnote{Provided by the \texttt{exam} document class.}.

\item[\texttt{oneparchoices}] The given choices are typeset in a linear, horizontal list.
Long lists are split over multiple lines.
Each given choice is prepended with a label name which can be set to uppercase
letter, lowercase letter, Roman numerals (uppercase and lowercase) and the Greek
alphabet.

\item[\texttt{checkboxes}] The given choices are typeset in a linear, vertical list.
Each given choice is prepended with a checkbox, which defaults to a big circle. 

\item[\texttt{oneparcheckboxes}] The given choices are typeset in a linear, horizontal
list. Long lists are split over multiple lines.
Each given choice is prepended with a checkbox, which defaults to a big circle.

\end{description}

 Within the environments, the
commands \texttt{\bs choice} and \texttt{\bs CorrectChoice} designate the
typesetting material. The difference between the two commands is discussed
in Section~\ref{sec:solutions}.

Examples of the four environments are given below.

\subsection{The \texttt{choices} environment}

An example of a question with the \texttt{choices} environment is:

\begin{lstlisting}
\question[5] What is the result of $1+1$?

\begin{choices}
\choice 1
\CorrectChoice 2
\choice 3
\choice 4
\end{choices}
\end{lstlisting}

which is typeset as:
\enlargethispage{\baselineskip}
\begin{questions}
\setcounter{question}{0}
\question[5] What is the result of $1+1$?

\begin{choices}
\choice 1
\CorrectChoice 2
\choice 3
\choice 4
\end{choices}
\end{questions}

\subsection{The \texttt{oneparchoices} environment}

An example of a question with the \texttt{oneparchoices} environment is:

\begin{lstlisting}
\question[5] What is the result of $2+2$?

\begin{oneparchoices}
\choice 1
\choice 2
\choice 3
\CorrectChoice 4
\end{oneparchoices}
\end{lstlisting}

which is typeset as:

\begin{questions}
\setcounter{question}{1}
\question[5] What is the result of $2+2$?

\begin{oneparchoices}
\choice 1
\choice 2
\choice 3
\CorrectChoice 4
\end{oneparchoices}
\end{questions}

Furthermore, the \texttt{exam} document class provides two ways of typesetting
checkbox questions.

\subsection{The \texttt{checkboxes} environment}

An example of a vertically aligned checkbox environment is:

\begin{lstlisting}
\question[5] What is the result of $1+2$?

\begin{checkboxes}
\choice 1
\choice 2
\CorrectChoice 3
\choice 4
\end{checkboxes}
\end{lstlisting}

which typesets to:

\begin{questions}
\setcounter{question}{2}
\question[5] What is the result of $1+2$?

\begin{checkboxes}
\choice 1
\choice 2
\CorrectChoice 3
\choice 4
\end{checkboxes}
\end{questions}

\subsection{The \texttt{onecheckboxes} environment}

The \texttt{oneparcheckboxes} environment typesets the choices
in al linear, horizontal way. An example is given below:

\begin{lstlisting}
\question[5] What is the result of $1+2$?

\begin{oneparcheckboxes}
\choice 1
\choice 2
\CorrectChoice 3
\choice 4
\end{oneparcheckboxes}
\end{lstlisting}

which typesets to:

\begin{questions}
\setcounter{question}{3}
\question[5] What is the result of $1+2$?

\begin{oneparcheckboxes}
\choice 1
\choice 2
\CorrectChoice 3
\choice 4
\end{oneparcheckboxes}
\end{questions}

Other types of questions are not considered here.

\subsection{Typesetting solutions}
\label{sec:solutions}
Please note the use of the \texttt{\bs choice} and \texttt{\bs CorrectChoice}
commands. A \texttt{\bs choice} typesets as an item in the list with no special markup.
A \texttt{\bs CorrectChoice} typesets an item in the list with special markup if
the \texttt{exam} document class option \texttt{answers} is given, otherwise it
typesets the same way a \texttt{\bs choice}. This special
markup defaults to boldface for the \texttt{choices} and \texttt{oneparchoices}
environments:

\begin{lstlisting}
\question[5] What is the result of $2+2$?

\begin{choices}
\choice 1
\choice 2
\choice 3
\CorrectChoice 4
\end{choices}
\end{lstlisting}

which is typesets as:

\printanswers

\begin{questions}
\setcounter{question}{4}
\question[5] What is the result of $2+2$?

\begin{choices}
\choice 1
\choice 2
\choice 3
\CorrectChoice 4
\end{choices}
\end{questions}

Note that item D is typeset in boldface. The \texttt{checkboxes} and \texttt{oneparcheckboxes}
environments use a \emph{surd}\footnote{A surd is a simplistic form of a square root sign.}:

\begin{lstlisting}
\question[5] What is the result of $1+2$?

\begin{oneparcheckboxes}
\choice 1
\choice 2
\CorrectChoice 3
\choice 4
\end{oneparcheckboxes}
\end{lstlisting}

which typesets to:

\begin{questions}
\setcounter{question}{5}
\question[5] What is the result of $1+2$?

\begin{oneparcheckboxes}
\choice 1
\choice 2
\CorrectChoice 3
\choice 4
\end{oneparcheckboxes}
\end{questions}

\noprintanswers


\section{Using the package \texttt{exam-randomizechoices}}
Although the \texttt{exam} document class is a very powerful tool to
create exams, it does not provide options to typeset the content of the
standard multiple choice environments in a random order\footnote{It
also doesn't provides options to randomize questions.}. This package
addresses this situation.

\subsection{New multiple choice environments}
Basically, this package provides the user with four new multiple choice
environments:

\begin{description}[labelindent=2ex]
\item[\texttt{randomizechoices}] This is the randomizing counterpart of the
\texttt{choices} environment. It typesets the given items in a random order.

\item[\texttt{randomizeoneparchoices}] This is the randomizing counterpart of the
\texttt{oneparchoices} environment. It typesets the given items in a random order.

\item[\texttt{randomizecheckboxes}] This is the randomizing counterpart of the
\texttt{checkboxes} environment. It typesets the given items in a random order.

\item[\texttt{randomizeoneparcheckboxes}] This is the randomizing counterpart of the \linebreak
\texttt{oneparcheckboxes} environment. It typesets the given items in a random order.

\end{description}


\subsection{Using the new multiple choice environments}
We will discuss the \texttt{randomizechoices} environment only. The other
environment work alike.

You can use the new multiple choice environments in the same way as the non-randomizing counterparts.
So an example of the \texttt{randomizechoices} environment might be:

\begin{lstlisting}
\question[5] What is the result of $1+1$?

\begin{randomizechoices}
\choice 1
\CorrectChoice 2
\choice 3
\choice 4
\end{randomizechoices}
\end{lstlisting}

which \emph{possibly} is typeset as:

\begin{questions}
\setcounter{question}{6}
\question[5] What is the result of $1+1$?

\begin{randomizechoices}
\choice 1
\CorrectChoice 2
\choice 3
\choice 4
\end{randomizechoices}
\end{questions}

Here we can see that the resulting output is typeset in a different order
then the choices are given. We say \emph{possibly} because the output
depends on the state of the pseudo random generator (see Section~\ref{sec:seeding}).

\subsection{Arguments to the new multiple choice environments}
The new multiple choice environments accept (a combination of) the following optional arguments
which are local to the environment currently being typeset:

\begin{description}[labelindent=2ex]
\item[randomize] The typesetting material is randomized. This is the default
behaviour of the package.
\item[norandomize] Randomization is turned off.
Useful if you wish to see the typesetting in the given order.
\item[keeplast] The last given item in the entered order is not part of the
randomization process. This way you can keep the last item always the last item.
\item[nokeeplast] The last given item is used in the randomization process. This
is the default behaviour of the package.

\end{description}

Sometimes you want the last given item to stick on its place. This is useful
if you want to use an choice item if none of the other choices are correct:

\begin{lstlisting}
\question[5] What is the result of $2+5$?

\begin{randomizechoices}[keeplast]
\choice 1
\choice 2
\choice 3
\CorrectChoice None of the above answers is correct.
\end{randomizechoices}
\end{lstlisting}

which possibly is typeset as:

\begin{questions}
\setcounter{question}{7}
\question[5] What is the result of $1+1$?

\begin{randomizechoices}[keeplast]
\choice 1
\choice 2
\choice 3
\CorrectChoice None of the above answers are correct.
\end{randomizechoices}
\end{questions}

Note that the last item can also be a \texttt{\bs choice} command. Also note
that if randomization is turned off, the \texttt{keeplast} option has no effect.

\subsection{Loading the package}
The package is loaded using the well-known \texttt{\bs{usepackage}} command:

\begin{lstlisting}
\usepackage[(* \emph{\textrm{option list}} *)]{exam-randomizechoices}
\end{lstlisting}

The package depends on the \texttt{exam} document class being loaded beforehand. If this
is not the case, the package will throw an error and stops the compilation
immediately.


\subsection{Package options}

The options in \emph{\textrm{option list}} can be any combination of:

\begin{description}[labelindent=2ex]
\item[randomize] This option globally turns on the randomizing of the choices given for all
available typesetting environments. Randomization is turned on by default.

\item[norandomize] This option globally turns off the randomizing of the choices for all
available typesetting environments. This option is useful for inspecting the resulting
PDF output file with typesetting the choices in the order they were entered.

\item[keeplast] This option globally turns on the preservation of the last entered
item in the new environments.

\item[nokeeplast] This option globally turns off the preservation of the last entered
item in the new environments. This is the default behaviour.

\item[overload] This option makes the standard multiple choice environments behave
the same as the new environment counterparts, i.e.\@ the the standard multiple choice
environments are overloaded (or redefined). This is useful if you wish to use an old
exam and randomize the choices of the questions.

\item[nooverload] This option suppresses the overloading of the standard multiple choice
environments so you have to use the new multiple choice environments if you want to
randomize the choices to the questions. Overloading is turned off by default.

\item[\texttt{debug}] This option causes the package to emit a lot of debug messages.
The messages are written to the log file by the \texttt{\bs{PackageWarning}}
command. Most IDE's, such as TeXMaker, will display the messages in the transcript pane.
Debug is turned off by default. There is no \texttt{nodebug} option.

\end{description}

If you load the package with no options, it behaves as:

\begin{lstlisting}
\usepackage[randomize,nokeeplast,nooverload]{exam-randomizechoices}
\end{lstlisting}


\subsection{Overloading the standard multiple choice environments}
If the package option \texttt{overload} is in effect, the standard multiple choice
environments are overloaded (or redefined) by the new multiple choice environments,
i.e. the standard multiple choice environment behave the same as their
randomizing counterparts. This way the user can typeset old exams or when creating
an exam from a repository of (old) questions. Please note that the overloaded
multiple choice environments now accept arguments just as their randomizing
counterparts do.


\subsection{Seeding rhe pseudo random generator}
\label{sec:seeding}
To get a consistent randomization, you must seed the pseudo random generator
with the same seed every time you compile your document. You can set the
seed using the \texttt{\bs setrandomizerseed} macro. The macro has a mandatory
argument that is an integer between 0 and $2^{31}-1$, \TeX's largest integer.
Internally, the PGF macro \texttt{\bs pgfmathsetseed} is called, and it is
flagged that you applied a seed. If you fail to do so, the seeding value is
\texttt{\bs time}$\times$\texttt{\bs year} as stated by the PFG manual%
\footnote{Version 3.0.1a, page 940.}. \LaTeX\ compilers keep track of
time by an integer that holds the minutes counted since midnight. The integer is
incremented every time the time passes a minute boundary. So the scenario
can be that you compile your document a couple of times with no apparent
differences between runs. But if the time passes a minute boundary, the
next time you compile your document you'll see that the environment items
have been rearranged.

\subsection{Printing the key table}
The package provides the typesetting of a basic key table in vertical direction.
Please note that only the environments \texttt{randomizechoices},
\texttt{randomizeoneparchoices}, \texttt{choices} (if overloaded) and
\texttt{oneparchoices} (if overloaded) can have valid keys in the key table.
The \texttt{*checkboxes} environments can't 
have keys because of the typesetting regime used by the \texttt{exam}
document class. The \texttt{*choices} environments use an internal counter
to keep track of the choice currently being typeset. Using this counter, a
label can be provided with a \texttt{\bs label} command.
The \texttt{*checkboxes} environments don't use an internal counter so 
labelling them would lead to a reference to the current question (or part,
or sub part or sub-sub part). Labelling the correct choices (with the
\texttt{\bs CorrectChoice} command) is automatically handled by the
package.

At the end of the exam, issue the commands:

\begin{lstlisting}
\ifprintanswers
   \printkeytable
\fi
\end{lstlisting}

to print the key table. The \texttt{\bs ifprintansers} command is only true if the
\texttt{exam} document class \texttt{ansers} option is set, thereby preventing
accidental typesetting the key table. If an correct choice can't be labelled, the
table entry will contain \textbf{??}, otherwise it will contain the used typesetting scheme
(which is \texttt{\bs Alph} by default).
The key table is typeset using the \texttt{tabular}
environment, so it can be wrapped in a \texttt{table} environment.

An example of a key table is presented below. Note the \textbf{??} in the
Answer column. This is the result of using the standard multiple choice
environments (they are not overloaded, so no label is applied).

\printkeytable

The text in the table header row can be changed using two command: the command
\texttt{\bs keylistquestionname} sets the text above the question numbers, which
defaults to ``Question'', the command \texttt{\bs keylistkeyname} sets the text
above the keys, which defaults to ``Key''. A typical use could be:

\begin{lstlisting}
\keylistquestionname{Exercise}
\keylistkeyname{Answer Key}
\end{lstlisting}

\subsection{Writing the keys to a file}
The package provides the writing of the keys to the file.
Please note that only the environments \texttt{randomizechoices},
\texttt{randomizeoneparchoices}, \texttt{choices} (if overloaded) and
\texttt{oneparchoices} (if overloaded) can have valid keys in the key list.

Writing a key list file is started by the command:

\begin{lstlisting}
\writekeylist[(*\textsl{\textrm{filename}}*)]{(*\textsl{\textrm{command name}}*)}
\end{lstlisting}

The optional parameter \emph{filename} is the name of the file. If you don't
provide a filename, \texttt{\bs jobname.keylist} will be used,
where \texttt{\bs jobname} is the name of your \LaTeX{} file you are compiling.
The \emph{command name} is used in the key list file. An example of a key list file
is presented below. In essence, the file contains a \texttt{\bs gdef} command
that assigns the key list to the supplied command name (in this case
\texttt{\bs mykeylist}).

\writekeylist{\mykeylist}
\vspace*{\baselineskip}
\lstinputlisting[caption=]{\jobname.keylist}

\vspace*{1.5\baselineskip}
The key list itself is constructed as a comma-separated list. The question
number and answer keys are separated with a `/'. This makes it easy to
parse the list with PGF's \texttt{\bs foreach} command:

\begin{lstlisting}
\input{\jobname.keylist}

\foreach \num/\key in \mykeylist {
	\textcolor{red!\num0!blue} {question \num\ has key \key} \\
}
\end{lstlisting}

Executing this code results in:

\input{\jobname.keylist}

\foreach \num/\key in \mykeylist {
	\textcolor{red!\num0!blue} {question \num\ has key \key} \\
}

The key list file can safely be deleted. It is generated each time the
\texttt{\bs writekeylist} command is executed. Edits to the file are lost.

\subsection{There should be only one correct answer}
As stated in the title of this section, each multiple choice question
should have one, and only one correct answer. The packages issues a
warning if a question has zero or more than one correct answer, but it
doesn't stop compilation. If a question has no correct answer there
will be a \textbf{??} in the printed key table and a ? in the key list
file. If a question has two or more correct answers, the last correct
answer being typeset will be printed in the key table and is written to
the key list file.

Also note that using the \textsl{parts} environment to typeset multiple
multiple choice questions is not supported because the automatic
labelling mechanism supports only at the question level.

\subsection{Verbatim environments}
Verbatim environments such as \texttt{verbatim} and \texttt{lstlistings}
are currently \emph{not} supported in the new and (overloaded) standard multiple
choice environments. This is due to the way the contents of these environments
are collected. The \texttt{\bs verb} command is also not supported by the package
but \texttt{\bs lstinline} is. An example, translated to English from a Dutch exam
on the ATmega32 microcontroller chip:

\begin{lstlisting}
\question[5]
The individual pins of Port D can be configured as input or output.
The single bits of Port D are called PD7, PD6, PD5, PD4, PD3, PD2,
PD1 and PD0. Which of the following alternatives configures PD0, PD1,
PD6 and PD7 as output and the remaining pins as input?

\begin{randomizechoices}
\choice \lstinline|out DDRD,0x3c|
\CorrectChoice \lstinline|ldi r20,0xc3| \par \lstinline|out DDRD,r20|
\choice \lstinline|out PORTD,0x3c|
\choice \lstinline|ldi r20,195| \par \lstinline|PORTD,r20|
\end{randomizechoices}
\end{lstlisting}

typesets possibly to:

\begin{questions}
\setlength{\parskip}{0pt}
\setcounter{question}{8}
\question[5]
The individual pins of Port D can be configured as input or output. The single bits
of Port D are called PD7, PD6, PD5, PD4, PD3, PD2, PD1 and PD0. Which of the
following alternatives configures PD0, PD1, PD6 and PD7 as output and the remaining
pins as input?

%%Original Dutch text
%%De individuele pinnen van Port D kunnen als ingang of uitgang geconfigureerd worden. De
%%enkelvoudige bits van Port D worden PD7, PD6, PD5, PD4, PD3, PD2, PD1, PD0 genoemd.
%%Welk van de volgende alternatieven configureert PD0, PD1, PD6 en PD7 als uitgang en de
%%overige pinnen als ingang?
%
%\hspace*{\baselineskip}

\begin{randomizechoices}[norandomize]
	\choice \lstinline|out DDRD,0x3c|
	\CorrectChoice \lstinline|ldi r20,0xc3| \par \lstinline|out DDRD,r20|
    \choice \lstinline|out PORTD,0x3c|
	\choice \lstinline|ldi r20,195| \par \lstinline|PORTD,r20|
\end{randomizechoices}
\end{questions}

\subsection{Accessing the internal labels}
Each \texttt{\bs question} command is accompanied with a label of the form
\texttt{question@}$x$ as provided by the \texttt{exam} document class, where
$x$ is the decimal number of the question. A \texttt{\bs CorrectChoice}
command is provided with a label (by this package) of the form \linebreak \texttt{question@}$x$%
\texttt{@correctchoice} where $x$ is again the decimal number of the question.
The user can access these labels by using the \texttt{\bs ref} command as shown
below:

\begin{lstlisting}
The question number is~\ref{question@9} and the correct answer
is~\ref{question@9@correctchoice}.
\end{lstlisting}

which typesets to:

The question number is~\ref{question@9} and the correct answer
is~\ref{question@9@correctchoice}.

There is no need to change the cat code of `@'.

\subsection{Error messages}
The package will throw you some error messages in case something is wrong. The
message below is printed only at start up.

\begin{description}[labelindent=2ex]
\item[\texttt{The exam class is not loaded. Emergency stop!}] --- You didn't load
the \texttt{exam} document class prior to loading the package. As a result of that,
the package can't continue and stops compiling your document. Note that a document
class can be based on the \texttt{exam} document class and will load it for you.

\end{description}

The next messages will only be printed if the new multiple choice environments
are used \emph{or} if the standard multiple choice environments are used when
overloaded: 

\begin{description}
\item[\texttt{You should NOT define \string\inaccessible. Emergency stop!}] ---
The packages relies on the fact that \texttt{\bs inaccessible} is not defined. It is
used by the list parser to designate \texttt{\bs CorrectChoice} and
\texttt{\bs correctchoice}. Normally, a user doesn't define this command.
As a result of that, the package can't continue and stops compiling your document.

%% Next two are turned into warningz
%%%\item[\texttt{You need exactly one \string\CorrectChoice, I found} $x$
%%%      \texttt{in question} $y$] --- You should provide only one correct
%%%answer in a multiple choice question. Compilation does continue.
%%%
%%%\item[\texttt{You need at least two choices in question} $x$] --- You didn't
%%%provide two or more choices, so there's nothing to choose from. Compilation
%%%does continue.     

\item[\texttt{Something's wrong, perhaps a missing \string\choice\ or \string\CorrectChoice\ or ...}] --- You used text before an initial \texttt{\bs choice}, \texttt{\bs CorrectChoice}
or \texttt{\bs correctchoice}. Analogue to the famous missing \texttt{\bs item} messages.
Compilation does continue.
 
\item[\texttt{Cannot write key list file}] --- Writing of files is disabled so the key list file can't be generated. Compilation does continue.
 
\end{description}



\section{Some internal details of the package}
This section provides some internal details on the operation of the package.

\subsection{Used packages}
The package loads the following packages:

\begin{description}[labelindent=2ex]
\item[\texttt{environ}] This package is used for defining the new environments. It provides
the powerful \texttt{NewEnviron} and \texttt{RenewEnviron} commands.
\item[\texttt{etoolbox}] This package provides some useful commands, notably on the parsing of lists.
\item[\texttt{pgffor}] This package provides the powerful \texttt{\bs foreach} loop
construct. In turn, this package loads the \texttt{pgfmath} package which supplies
the \texttt{\bs pgfmathsetseed} and \linebreak \texttt{\bs pgfmathrandominteger} commands.
\end{description}

\subsection{Defining the \texttt{randomize*} environments}
Defining the \texttt{randomize*} environments is done with the \texttt{NewEnviron}
and \texttt{RenewEnviron} commands. As an example we discuss the
\texttt{randomizechoices} environment.

We define this environment as follows (not overloaded):

\begin{lstlisting}
\NewEnviron{randomizechoices}[1][]{
  %%
  %% Create a random list
  \erc@createrandomlist[#1]%
  %%
  %% Start the choices environment
  \begin{choices}%
    % Execute the list
    \erc@typesetchoices%
  \end{choices}%
}
\end{lstlisting}

Now the \texttt{NewEnviron} command has a very useful property in that
when expanded it places its content in the command \texttt{\bs BODY}.
So when the environment is used as in:

\begin{lstlisting}
\begin{randomizechoices}
\choice one
\choice two
\CorrectChoice three
\choice four
\end{randomizechoices
\end{lstlisting}

the \texttt{\bs BODY} command now contains (comments and newlines are stripped):

\begin{lstlisting}
\choice one \choice two \CorrectChoice three \choice four
\end{lstlisting}

Using the command as in:

\begin{lstlisting}
\BODY
\end{lstlisting}

simply expands the command. Now all the \texttt{randomize*} environments
are defined this way, so we need a generic command that parses \texttt{\bs BODY} and
provides a command that contains the randomized version of \texttt{\bs BODY}. This is
handled by the command \texttt{\bs erc@createrandomlist}. What \texttt{\bs erc@createrandomlist}
basically does is:

\begin{enumerate}
\item Parses the supplied options and sets internal flags for later use.
\item Replaces every occurrence of \texttt{\bs CorrectChoice} and
      \texttt{\bs correctchoice} in \texttt{\bs BODY} 
      with \texttt{\bs choice \bs inaccessible}.
\item Disassembles \texttt{\bs BODY} into a set of commands by splitting on the list
      separator \texttt{\bs choice}. \texttt{\bs choice} is removed.
\item Randomizes the order of the set of commands.
\item Assembles the set of commands to the new command \texttt{\bs erc@typesetchoices},
      thereby appending a \texttt{\bs label} to the correct choice. The ``command''
      \texttt{\bs inaccessible} is replaced by \texttt{\bs CorrectChoice}. The
      other commands are prepended with \texttt{\bs choice}.
\end{enumerate}

Of course this behaviour can be altered by options. For example, if the option
\texttt{norandomize} is passed, \texttt{\bs BODY} is not randomized but labelling still
takes place.

Please note: We need to replace \texttt{\bs CorrectChoice} with \texttt{\bs choice \bs inaccessible}
because the list parser can only handle one list separator at a time. The very misunderstood command
\texttt{\bs inaccessible}\footnote{This sequence of characters sometimes appear in error messages
generated by \LaTeX.} is likely not to be entered by the user but if the user enters \texttt{\bs inaccessible}
directly after a \texttt{\bs choice} command, this is converted to \texttt{\bs CorrectChoice}
in step 5. Of course this is not a bug, but a feature.

After \texttt{\bs erc@createrandomlist} has done it's job, the command \texttt{\bs erc@typesetchoices}
is simply expanded within a \texttt{choices} environment.

Now if the global \texttt{overload} option is in effect, some more trickery is needed. First, a copy
of the \texttt{choices} environment is created using \texttt{\bs let}:

\begin{lstlisting}
%% Save choices environment
\let\@oldchoices\choices
\let\end@oldchoices\endchoices
\end{lstlisting}

Using \texttt{\bs let} doesn't create problems, because the \texttt{choices} environment
doesn't support options. See \url{https://tex.stackexchange.com/questions/116670/}.
Next, the \texttt{\bs choices} environment is redefined:

\begin{lstlisting}
%% Renew the choices environment
\RenewEnviron{choices}[1][]{

  %% Create a random list
  \erc@createrandomlist[#1]

  %% Start the choices environment
  \begin{@oldchoices}
     % Execute the list
     \erc@typesetchoices
  \end{@oldchoices}
}
\end{lstlisting}

Note that now the \texttt{\bs choices} environment can handle the same options as the
\texttt{randomizechoices} environment.

\subsection{Note on the key table}
The code of the key table is first collected using a series of \texttt{\bs gappto} and
\texttt{\bs xappto} commands. Using these commands is far more easy then typesetting it
directly from the package. See \url{https://tex.stackexchange.com/questions/367979/}
why. Notably the use of \texttt{\bs begin}, \texttt{\bs foreach} and \texttt{\bs hline}
is problematic.

\subsection{Verbatim environments}
Verbatim environments are currently not supported. This is due to the way \texttt{\bs NewEviron}
collects its contents. A \texttt{\bs NewEnviron} is not really an environment at all, but a
command. One solution is to write the contents to a file and then include the file. This is
being investigated. See
% \url{https://tex.stackexchange.com/questions/51239/verbatim-in-environ-s-body}.
\url{https://tex.stackexchange.com/questions/51239/}.

Also the \texttt{\bs verb} command is not supported, but \texttt{\bs lstinline} is.

\section{A personal note}
I've been using the \texttt{exam} document class for five years now. What I like most
is the consistent typesetting of my exams. I don't use sub parts and sub-sub parts
because in my opinion this not the right way to prepare an exam. I've written a
department consistent class that provides the department's cover pages and some
other typesetting trickery. This class is used by a small core \TeX\ users. Regrettably,
the majority of my colleagues use Word *cough*.

\end{document}