summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/randexam/randexam.tex
blob: feb1af2e14559c93b0e74e1316d03c1af00164e7 (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
% -*- coding: utf-8 -*-

\documentclass[12pt,plain,most]{randexam}
\geometry{b5paper,margin=2cm}

\newcommand*{\myversion}{2024C}
\newcommand*{\mydate}{\the\year-\mylpad\month-\mylpad\day}
\newcommand*{\mylpad}[1]{\ifnum#1<10 0\the#1\else\the#1\fi}

\cfoot{\small\thepage}

\setlength{\parindent}{0pt}
\setlength{\parskip}{7pt plus 1pt minus 1pt}

\usepackage{arevtext}
\usepackage{iftex}
\usepackage{ninecolors}
\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  urlcolor=blue3,
  linkcolor=blue3,
}

\renewcommand\familydefault{\sfdefault}

\renewcommand{\baselinestretch}{1}
\renewcommand{\arraystretch}{1.3}

\usepackage{tabularx}

\newcommand{\fillbox}[1]{\ulinefill{#1}\underline{#1}\ulinefill{#1}}

\usepackage{fancyvrb}

\DefineVerbatimEnvironment{code}{Verbatim}{%
  formatcom=\color{blue!50!red}%
}

\usepackage{codehigh}

\NewDocumentCommand\mypkg{m}{\textcolor{blue3}{\mbox{#1}}}
\NewDocumentCommand\myopt{m}{\textcolor{yellow3}{\mbox{#1}}}
\NewDocumentCommand\mycmd{m}{\textcolor{green3}{\ttfamily\fakeverb{#1}}}
\NewDocumentCommand\myenv{m}{\textcolor{green3}{\ttfamily#1}}
\NewDocumentCommand\myfile{m}{\textcolor{purple3}{\mbox{#1}}}
\NewDocumentCommand\mykey{m}{\textcolor{brown3}{\mbox{\fakeverb{#1}}}}
\NewDocumentCommand\myval{m}{\textcolor{azure3}{\mbox{\fakeverb{#1}}}}
\NewDocumentCommand\myelem{m}{\textcolor{violet3}{\mbox{#1}}}

\begin{document}

{%
  \renewcommand{\arraystretch}{2}%
  \noindent\Large
  \begin{tabularx}{\linewidth}{|X|}
    \hline
    Title: \fillbox{\color{blue3}The randexam class for LaTeX}\\
    Author: \fillbox{Jianrui Lyu (tolvjr@163.com)}\\
    Version: \fillbox{\myversion{} (\mydate)}\\
    Code: \fillbox{\url{https://github.com/lvjr/randexam}}\\
    \hline
  \end{tabularx}%
}

\tableofcontents

\section{Introduction}

Document class \mypkg{randexam} is an exam class for LaTeX.
With this class you could easily make an exam paper and its randomized variants.

The class mainly focuses on making math exam papers, but you could use it to make other exam papers.

The latest release of this package can be downloaded from here: \newline
\textcolor{blue}{\href{https://ctan.org/pkg/randexam}{\ttfamily https://ctan.org/pkg/randexam}}.

\section{Basic Structure}

\subsection{A randexam document}

The following is the basic structure of a \mypkg{randexam} document:
\begin{code}
\documentclass{randexam}
% document preamble
\begin{document}
% document body
\end{document}
\end{code}
In document preamble you could set some options for the exam.
In document body you write the contents of the exam.

\subsection{Document preamble}

In document preamble you normally set some class options with \mycmd{\SetExamOption} command.
For example, you could change the random seed with \myopt{seed} key. The seed is used only when
you add class option \myopt{random} (see Subsection \ref{opt:random}):
\begin{code}
\SetExamOption{
  seed = 19061116, % random seed
}
\end{code}

\subsection{Document body}

In document body you normally write an \mycmd{\examtitle},
multiple \mycmd{\exampart}, and an optional \mycmd{\examdata} commands:

\begin{code}
\examtitle{name=Math Final Exam,date=2018-06-28,version=A}
......
\exampart{Fill in the blanks.}{3 points for each.}
......
\exampart{Select one answer.}{3 points for each.}
......
\exampart{Work out math calculations.}{8 points for each.}
......
\examdata{Some data may be used in the exam}
......
\end{code}

\subsection{Exam title}

In calling \mycmd{\examtitle} command,
you need to provide some basic informations of the exam:

\begin{code}
\examtitle{name=Math Final Exam,date=2018-06-28,version=A}
\end{code}

If the \mykey{date} key is missing, it has the default value \myval{\today}.
When class option \myopt{random} is passed, the value of \mykey{version} key
will be modified from \myval{A} to \myval{B}.

\subsection{Exam parts}

The questions in an exam could be separated into several parts:

\begin{code}
\exampart{Fill in the blanks.}{3 points for each.}
......
\exampart{Select one answer.}{3 points for each.}
......
\exampart{Work out math calculations.}{8 points for each.}
......
\end{code}

\subsection{Exam data}

At the end of the exam, you could add some appendix data with \mycmd{\examdata} command:

\begin{code}
\examdata{Some data may be used in the exam}
......
\end{code}

You must put appendix data after \mycmd{\examdata} command,
or the exam variants will be incorrect.

\section{Types of questions}

\subsection{True-or-false questions}

\begin{code}
\exampart{True-or-false questions}{3 points for each.}

\begin{question}
The first true-or-false question. \tickout{T}
\end{question}

\begin{question}
The second true-or-false question. \tickout{F}
\end{question}
\end{code}

\begin{question}
The first true-or-false question. \tickout{T}
\end{question}

\begin{question}
The second true-or-false question. \tickout{F}
\end{question}

With \mycmd{\tickout{T}} and \mycmd{\tickout{F}}, you get \textcolor{blue}{\textsf{T}}
and \textcolor{blue}{\textsf{F}}; with \mycmd{\tickout{t}} and \mycmd{\tickout{f}},
you get \textcolor{blue}{$\checkmark$} and \textcolor{blue}{\large$\times$}.

You must put answers inside \mycmd{\tickout} command,
so as to make \mypkg{randexam} hide them in generating blank exam papers.

\subsection{Fill-in-the-blank questions}

\begin{code}
\exampart{Fill in the blanks.}{3 points for each.}

\begin{question}
The first fill-in-the-blank question \fillout{answer}.
\end{question}

\begin{question}
The second fill-in-the-blank question \fillout{answer}.
\end{question}
\end{code}

\begin{question}
The first fill-in-the-blank question \fillout{answer}.
\end{question}

\begin{question}
The second fill-in-the-blank question \fillout{answer}.
\end{question}

With \mycmd{\fillout} command, the underline will fill the whole line;
with \mycmd{\fillin} command, the underline will be minimal.

You must put answers inside \mycmd{\fillout} or \mycmd{\fillin} command,
so as to make \mypkg{randexam} hide them in generating blank exam papers.

\subsection{Multiple-choice questions}

\begin{code}
\exampart{Select one answer.}{3 points for each.}

\begin{question}
The first multiple-choice questions \pickout{A}.
\begin{abcd}
  \item First
  \item Second
  \item Third
  \item Fourth
\end{abcd}
\end{question}

\begin{question}
The second multiple-choice questions \pickout{C}.
\begin{abcd}
  \item First choice
  \item Second choice
  \item Third choice
  \item Fourth choice
\end{abcd}
\end{question}
\end{code}

\begin{question}
The first multiple-choice questions \pickout{A}.
\begin{abcd}
  \item First
  \item Second
  \item Third
  \item Fourth
\end{abcd}
\end{question}

\begin{question}
The second multiple-choice questions \pickout{C}.
\begin{abcd}
  \item First choice
  \item Second choice
  \item Third choice
  \item Fourth choice
\end{abcd}
\end{question}

With \mycmd{\pickout} command, the answer will be printed on the right edge of the line;
with \mycmd{\pickin} command, the answer will be printed on current position.

You must put answers inside \mycmd{\pickout} or \mycmd{\pickin} command,
so as to make \mypkg{randexam} hide them in generating blank exam papers.

The four choices of multiple-choice questions could be typeset with \myenv{abcd} environment.
And \myenv{abcd} environment will put them in one, two, or four rows
according to the lengths of the choices.

\subsection{Subjective questions}

For subjective questions, you could put answers inside \myenv{solution} environment.

\begin{code}
\exampart{Work out math calculations.}{8 points for each.}

\begin{question}
The first math calculation question.
\end{question}

\begin{solution}
Answer to the first question.
\end{solution}

\begin{question}
The second math calculation question.
\end{question}

\begin{solution}
Answer to the second question.
\end{solution}
\end{code}

\begin{question}
The first math calculation question.
\end{question}

\begin{solution}
Answer to the first question.
\end{solution}

\begin{question}
The second math calculation question.
\end{question}

\begin{solution}
Answer to the second question.
\end{solution}

\subsection{Other questions}

You can write other types of questions. For example:

\begin{code}
\exampart{Some question type}{4 points for each.}

\begin{question}
First question text. \answer{Answer text.}
\end{question}

\begin{question}
Second question text. \answer{Answer text.}
\end{question}
\end{code}

\begin{question}
First question text. \answer{Answer text.}
\end{question}

\begin{question}
Second question text. \answer{Answer text.}
\end{question}

You must put answer text inside \mycmd{\answer} command,
so as to make \mypkg{randexam} hide them in generating blank exam papers.

\section{Other components}

\subsection{Grade tables}

Normally you need a grade table after exam title.
You can get it by using \mycmd{\gradetable} command.

\begin{code}
\gradetable[total=6,strut=2em]
\end{code}

\vspace{-1.5em}\gradetable[total=6,strut=2em]

The meanings of keys in the \verb!\gradetable! command are:
\begin{description}
  \item[total] total number of parts in this exam.
  \item[strut] strut height of the score row; its default value is \verb!2.5em!.
\end{description}

\subsection{Answer tables}

Before true-or-false, fill-in-the-blank, or multiple-choice questions,
you may use \mycmd{\answertable} to generate an blank answer table:

\begin{code}
\answertable[total=6,column=3,strut=3em]
\end{code}

\answertable[total=6,column=3,strut=3em]

The meanings of keys in the \verb!\answertable! command are:
\begin{description}
  \item[total] total number of questions in this exam part.
  \item[column] number of questions in each row.
  \item[strut] strut height of the answer rows; its default value is \verb!1em!.
  \item[notice] notice text before the answer table.
\end{description}

\subsection{Vertical space}

You could leave some vertical space after a \myenv{question} or \myenv{solution} environment.
At this time \mypkg{randexam} class supports the following commands for adding vertical space:\par

\renewcommand{\arraystretch}{1.3}%
\begin{tabularx}{\linewidth}{l<{\qquad}X}
  \hline
  \texttt{\string\smallskip} & Add small vertical space\\
  \hline
  \texttt{\string\medskip}   & Add medium vertical space\\
  \hline
  \texttt{\string\bigskip}   & Add big vertical space\\
  \hline
  \texttt{\string\vfill}     & Fill vertical space available\\
  \hline
\end{tabularx}\par

Of course, you could use multiple commands in the above tables.

In the exam body, you could use \mycmd{\newpage} to make a page break,
but you should \underline{NOT} use other page breaking commands, such as \mycmd{\clearpage},
or the exam variants may be wrong.

\section{Writing questions}

\subsection{points key}

The \myenv{question} environment accepts a \mykey{points} key:

\begin{code}
\begin{question}[points=4]
This is a question.
\end{question}
\end{code}

\begin{question}[points=4]
This is a question.
\end{question}

\section{Writing solutions}

\subsection{Solution name}

If you would like to change the name of \myenv{solution} environment,
you could modify the translation of \mykey{solution-Solution} keyword
(see Subsection \ref{subsect:translation}).
The following example changes it from "Solution" to "Proof":

\begin{code}
\DeclareExamTranslation{current}{solution-Solution=Proof}
\end{code}

\subsection{Points command}

Inside \myenv{solution} environment, you could use \mycmd{\points} to give points for each step.
For example:

\begin{code}
\begin{solution}
$1+1=2$ \points{4}
$2+2=4$ \points{8}
\end{solution}
\end{code}

\begin{solution}
$1+1=2$ \points{4}
$2+2=4$ \points{8}
\end{solution}

You can also use \mycmd{\points} command inside displayed formulas or \myenv{align*} environment.
And the point text will be printed at the right edge of the line.

\subsection{Alignment commands}

With class option \myopt{many}, \mypkg{randexam} will load \mypkg{freealign} package.
and \mypkg{freealign} package provides several commands for aligning math formulas in different lines.

Here is the first example:

\begin{code}
We have $(a+b)^2 \? = (a+b)(a+b)$ \\
                 \+$= a^2+2ab+b^2$ \points{2}
\end{code}

\hrule
We have $(a+b)^2 \? = (a+b)(a+b)$ \\
                 \+$= a^2+2ab+b^2$ \points{2}
\hrule\vskip0.5em

The \mycmd{\?} command \underline{inside} the first formula saves current horizontal position,
and the \mycmd{\+} command \underline{before} the second formula jumps to previously saved position.

Here is another example:

\begin{code}
We have \? $(a+b)^2 = (a+b)(a+b)$ \\
        \< $= a^2+2ab+b^2$ \points{2}
\end{code}

\hrule
We have \? $(a+b)^2 = (a+b)(a+b)$ \\
        \< $= a^2+2ab+b^2$ \points{2}
\hrule\vskip0.5em

The \mycmd{\?} command \underline{before} the first formula saves current horizontal position,
and the \mycmd{\<} command \underline{before} the second formula jumps to the left
of previously saved position by the width of $=$.

Because \mypkg{freealign} package uses \mypkg{zref} package to save positions,
you need two compilations to get correct results.

\section{Class options}

\subsection{Blank exam papers}

Assume \myfile{exam-a-answer.tex} is an exam paper with answers.
You can easily get an blank exam paper with answers removed,
by creating an \myfile{exam-a-blank.tex} file with the following lines:

\begin{code}
\PassOptionsToClass{noanswer}{randexam}
\input{exam-a-answer}
\end{code}

That is to say, when adding \myopt{noanswer} option to \mypkg{randexam} class,
The answers will be hidden in the compiled exam paper.

\subsection{Randomized variants}\label{opt:random}

Assume \myfile{exam-a-answer.tex} is an exam paper.
You can get an randomized variant with all questions in the same part shuffled,
by creating an \myfile{exam-b-answer.tex} file with the following lines:

\begin{code}
\PassOptionsToClass{random}{randexam}
\input{exam-a-answer}
\end{code}

That is to say, when adding \myopt{random} option to \mypkg{randexam} class,
The questions in the same part will be shuffled in the compiled exam paper.
Furthermore, four choices in an \myenv{abcd} environment will be shuffled too.

\subsection{Two column exam papers}

Assume \myfile{exam-a-blank.tex} is the TeX file of an exam paper of A4 size.
You could get an exam paper of A3 size,
by creating a new TeX file with the following lines:

\begin{code}
\PassOptionsToClass{a3paper}{randexam}
\input{exam-a-blank}
\end{code}

That is to say, when adding \myopt{a3paper} option to \mypkg{randexam} class,
The result paper will be a two column document in A3 size.

Assume \myfile{exam-a-blank.pdf} is the PDF file of an exam paper of A4 size.
You could get an exam paper of A3 size,
by creating a new TeX file with the following lines:
\begin{code}

\documentclass[a3input]{randexam}
\begin{document}
\includepdf[pages=-,nup=2x1]{exam-a-blank}
\end{document}
\end{code}

That is to say, you can make an exam of A3 size from an exam of A4 size,
even if you have only the PDF file.

\section{Customizations}

\subsection{Translations of keywords}\label{subsect:translation}

With \mycmd{\DeclareExamTranslation} you can define the translaitons of the keywords
in an \mypkg{randexam} paper. At this time only English keywords are defined:

\begin{code}
\DeclareExamTranslation{english}{
   answertable-Answer   = Answer
  ,answertable-Number   = Number
  ,examdata-Appendix    = Appendix
  ,exampart-Part        = Part
  ,examtitle-Name       = Name
  ,examtitle-Solutions  = Solutions
  ,gradetable-Evaluator = Evaluator
  ,gradetable-Part      = Part
  ,gradetable-Score     = Score
  ,gradetable-Total     = Total
  ,headfoot-Name        = Name
  ,headfoot-of          = of
  ,headfoot-Page        = Page
  ,headfoot-Solutions   = Solutions
  ,headfoot-Version     = Version
  ,points-point         = point
  ,points-points        = points
  ,question-Question    = Question
  ,solution-Solution    = Solution
}
\end{code}

You could translate them for another language and enable them with \mycmd{\SelectExamTranslation} command:

\begin{code}
\DeclareExamTranslation{somelang}{
   question-Question    = Questioooooon
  ,solution-Solution    = Soooooolution
}
\SelectExamTranslation{somelang}
\end{code}
\DeclareExamTranslation{somelang}{
   question-Question    = Questioooooon
  ,solution-Solution    = Soooooolution
}
\SelectExamTranslation{somelang}

After this, you could load current translation of some keywords with \mycmd{\UseExamTranslation} command:

\begin{code}
\UseExamTranslation{question-Question}
\UseExamTranslation{solution-Solution}
\end{code}
\hrule
\UseExamTranslation{question-Question}
\UseExamTranslation{solution-Solution}
\vspace{0.5em}
\hrule
\vspace{0.5em}

\SelectExamTranslation{english}

This command is useful in defining new templates for the exam (see Subsection \ref{subsect:template}).

\subsection{Saving and reading key values}

With \mycmd{\DeclareExamValue} and \mycmd{\UseExamValue} you could save and read
the value of a key, respectively.

\begin{code}
\DeclareExamValue{somemodule}{somekey}{SomeValue}
\UseExamValue{somemodule}{somekey}
\end{code}
\hrule
\DeclareExamValue{somemodule}{somekey}{SomeValue}
\UseExamValue{somemodule}{somekey}
\vspace{0.5em}
\hrule
\vspace{0.5em}

These two commands are useful in defining new templates
for the exam (see Subsection \ref{subsect:template}).

\subsection{Templates of elements}\label{subsect:template}

With \mycmd{\DeclareExamTemplate} you could define a new template for some exam element,
which could be set as default template with \mycmd{\SelectExamTemplate}.
And with \mycmd{\UseExamTemplate} you could use the default template of the element.

To customize \myelem{examtitle} element for your school,
you can write the following code in document preamble:
\begin{code}
\DeclareExamTemplate{examtitle}{myschool}{%
  \fbox{%
    \UseExamValue{examtitle}{name}\quad%
    \UseExamValue{examtitle}{date}%
  }%
}%
\SelectExamTemplate{examtitle}{myschool}
\end{code}
\DeclareExamTemplate{examtitle}{myschool}{%
  \fbox{%
    \UseExamValue{examtitle}{name}\quad%
    \UseExamValue{examtitle}{date}%
  }%
}%
\SelectExamTemplate{examtitle}{myschool}

Then the \mycmd{\examtitle} command in document body will produce different result:
\begin{code}
\examtitle{name=Final Exam in My School,date=2024-01-01}
\end{code}
\vspace{-1em}
\examtitle{name=Final Exam in My School,date=2024-01-01}

Normally \mycmd{\examtitle} will call \mycmd{\UseExamTemplate{examtitle}{default}}.

All elements that could be customized with templates in an exam paper
are listed in the following table:

\begingroup\renewcommand\arraystretch{1.5}\noindent
\begin{tabularx}{\linewidth}{ll}
\hline
  \myelem{examtitle}     & for customizing \mycmd{\examtitle} command \\
\hline
  \myelem{exampart}      & for customizing \mycmd{\exampart} command \\
\hline
  \myelem{examdata}      & for customizing \mycmd{\examdata} command \\
\hline
  \myelem{gradetable}    & for customizing \mycmd{\gradetable} command \\
\hline
  \myelem{answertable}   & for customizing \mycmd{\answertable} command \\
\hline
  \myelem{questionbegin} & for customizing \myenv{question} environment \\
\hline
  \myelem{questionend}   & for customizing \myenv{question} environment \\
\hline
  \myelem{solutionbegin} & for customizing \myenv{solution} environment \\
\hline
  \myelem{solutionend}   & for customizing \myenv{solution} environment \\
\hline
\end{tabularx}\par
\endgroup

\end{document}