| 1 |
%\iffalse |
| 2 |
% makeindex -s gglo.ist -o ran_toks.gls ran_toks.glo |
| 3 |
% makeindex -s gind.ist -o ran_toks.ind ran_toks.idx |
| 4 |
%<*copyright> |
| 5 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6 |
%% ran_toks package, %% |
| 7 |
%% Copyright (C) 1999-2021 D. P. Story %% |
| 8 |
%% [email protected], [email protected] %% |
| 9 |
%% %% |
| 10 |
%% This program can redistributed and/or modified under %% |
| 11 |
%% the terms of the LaTeX Project Public License %% |
| 12 |
%% Distributed from CTAN archives in directory %% |
| 13 |
%% macros/latex/base/lppl.txt; either version 1 of the %% |
| 14 |
%% License, or (at your option) any later version. %% |
| 15 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 16 |
%</copyright> |
| 17 |
%<package>\NeedsTeXFormat{LaTeX2e}[1997/12/01] |
| 18 |
%<package>\ProvidesPackage{ran_toks} |
| 19 |
%<package> [2021/06/06 v1.4 Randomizing tokens (dps)] |
| 20 |
%<*driver> |
| 21 |
\documentclass{ltxdoc} |
| 22 |
\usepackage[colorlinks,hyperindex=false]{hyperref} |
| 23 |
\usepackage{fancyvrb} |
| 24 |
%\def\texorpdfstring#1#2{#1} |
| 25 |
%\pdfstringdefDisableCommands{\let\\\textbackslash} |
| 26 |
\OnlyDescription % comment out for implementation details |
| 27 |
\EnableCrossrefs |
| 28 |
\CodelineIndex |
| 29 |
\RecordChanges |
| 30 |
\bgroup\ttfamily |
| 31 |
\gdef\brpr#1{\char123\relax#1\char125\relax}\egroup |
| 32 |
\let\darg\brpr |
| 33 |
\let\env\texttt |
| 34 |
\let\opt\texttt |
| 35 |
\let\app\textsf |
| 36 |
\def\visispace{\symbol{32}} |
| 37 |
\def\ameta#1{\ensuremath{\langle\textit{\texttt{#1}}\rangle}} |
| 38 |
\def\meta#1{\textsl{\texttt{#1}}} |
| 39 |
\def\SUB#1{\ensuremath{{}_{\mbox{\scriptsize\ttfamily#1}}}} |
| 40 |
\def\cs#1{\texttt{\bslash#1}} |
| 41 |
\makeatletter |
| 42 |
\let\@latex@warning@no@line\@gobble |
| 43 |
\makeatother |
| 44 |
\InputIfFileExists{aebdocfmt.def}{\PackageInfo{ran_toks}{Inputting aebdocfmt.def}} |
| 45 |
{\def\IndexOpt{\DescribeMacro}\def\IndexKey{\DescribeMacro}\let\setupFullwidth\relax |
| 46 |
\PackageInfo{ran_toks}{aebdocfmt.def cannot be found}} |
| 47 |
\begin{document} |
| 48 |
\def\CMD#1{\textbackslash#1} |
| 49 |
\let\pkg\textsf |
| 50 |
\let\opt\texttt |
| 51 |
\addtolength{\marginparwidth}{3pt} |
| 52 |
\GetFileInfo{ran_toks.sty} |
| 53 |
\title{The \textsf{ran\_toks} Package} |
| 54 |
\author{D. P. Story\\ |
| 55 |
Email: \texttt{[email protected]}} |
| 56 |
\date{processed \today} |
| 57 |
\maketitle |
| 58 |
\tableofcontents |
| 59 |
\DocInput{ran_toks.dtx} |
| 60 |
\IfFileExists{\jobname.ind}{\newpage\setupFullwidth\par\PrintIndex}{\paragraph*{Index} The index goes here.\\Execute |
| 61 |
\texttt{makeindex -s gind.ist -o ran\_toks.ind ran\_toks.idx} on the command line and recompile |
| 62 |
\texttt{ran\_toks.dtx}.} |
| 63 |
\IfFileExists{\jobname.gls}{\PrintChanges}{\paragraph*{Change History} The list of changes goes here.\\Execute |
| 64 |
\texttt{makeindex -s gglo.ist -o ran\_toks.gls ran\_toks.glo} on the command line and recompile |
| 65 |
\texttt{ran\_toks.dtx}.} |
| 66 |
\end{document} |
| 67 |
%</driver> |
| 68 |
% \fi |
| 69 |
% \MakeShortVerb{|} |
| 70 |
% \InputIfFileExists{aebdonotindex.def}{\PackageInfo{ran_toks}{Inputting aebdonotindex.def}} |
| 71 |
% {\PackageInfo{ran_toks}{cannot find aebdonotindex.def}} |
| 72 |
% |
| 73 |
% \begin{macrocode} |
| 74 |
%<*package> |
| 75 |
% \end{macrocode} |
| 76 |
% \paragraph*{Description.} |
| 77 |
% This short package randomizes a list of tokens. The command, \cs{ranToks}, |
| 78 |
% takes one argument, which is a list of tokens: |
| 79 |
%\begin{Verbatim}[xleftmargin=\parindent,fontsize=\small, |
| 80 |
%commandchars={!()},codes={\catcode`\%=9}] |
| 81 |
%\ranToks{!ameta(name)}{!% |
| 82 |
%!quad{!ameta(tok!SUB1)}{!ameta(tok!SUB2)}...{!ameta(tok!SUB(n))} |
| 83 |
%} |
| 84 |
%\end{Verbatim} |
| 85 |
% The command defines a series of $n$ internal commands, one for each of the tokens. |
| 86 |
% The definitions are essentially randomized. The randomized tokens are accessed through |
| 87 |
% the command \cs{useRanTok}. For example, |
| 88 |
%\begin{Verbatim}[xleftmargin=\parindent,fontsize=\small, |
| 89 |
%commandchars={!()},codes={\catcode`\%=9}] |
| 90 |
%\useRanTok{1}, \useRanTok{2},..., \useRanTok{n} |
| 91 |
%\end{Verbatim} |
| 92 |
% gives a random listing of the $n$ tokens. These can be arranged on the page |
| 93 |
% as desired. |
| 94 |
% |
| 95 |
% There is a second construct, designed for more elaborate randomization. |
| 96 |
%\begin{Verbatim}[xleftmargin=\parindent,fontsize=\small, |
| 97 |
%commandchars={!()},codes={\catcode`\%=9}] |
| 98 |
%\bRTVToks{!ameta(name)} |
| 99 |
%\begin{rtVW} |
| 100 |
%!quad!ameta(some content) |
| 101 |
%\end{rtVW} |
| 102 |
%... |
| 103 |
%... |
| 104 |
%\begin{rtVW} |
| 105 |
%!quad!ameta(some content) |
| 106 |
%\end{rtVW} |
| 107 |
%\eRTVToks |
| 108 |
%\end{Verbatim} |
| 109 |
% The contents of each of the \texttt{rtVW} environments are written to the computers hard drive, then input |
| 110 |
% back in random order, using \cs{useRanTok}, eg, |
| 111 |
%\begin{Verbatim}[xleftmargin=\parindent,fontsize=\small, |
| 112 |
%commandchars={!()},codes={\catcode`\%=9}] |
| 113 |
%\useRanTok{1}, \useRanTok{2},..., \useRanTok{n} |
| 114 |
%\end{Verbatim} |
| 115 |
% Other details are left to the readers' imagination. |
| 116 |
% \changes{v1.4}{2021/06/06}{Rounded up v1.3.3 to v1.4 for publication} |
| 117 |
% \paragraph{Requirements.} As of this writing, we require only the \texttt{verbatim} package |
| 118 |
% and \texttt{random.tex}, the package was written by Donald Arseneau. |
| 119 |
% \begin{macrocode} |
| 120 |
\RequirePackage{verbatim} |
| 121 |
% \end{macrocode} |
| 122 |
% \paragraph*{Input \texttt{random.tex}.} Input \texttt{random.tex} if not already input. |
| 123 |
%\changes{v1.0d}{2013/08/03}{Added conditional input of random.tex} |
| 124 |
% \begin{macrocode} |
| 125 |
\@ifundefined{nextrandom}{\input{random.tex}}{} |
| 126 |
% \end{macrocode} |
| 127 |
% We redefine \cs{nextrandom} from \texttt{random.tex} to save the initializing seed. |
| 128 |
% \begin{macrocode} |
| 129 |
\def\nextrandom{\begingroup |
| 130 |
\ifnum\randomi<\@ne % then initialize with time |
| 131 |
\global\randomi\time |
| 132 |
\global\multiply\randomi388 \global\advance\randomi\year |
| 133 |
\global\multiply\randomi31 \global\advance\randomi\day |
| 134 |
\global\multiply\randomi97 \global\advance\randomi\month |
| 135 |
\message{Randomizer initialized to \the\randomi.}% |
| 136 |
\nextrandom \nextrandom \nextrandom |
| 137 |
% \end{macrocode} |
| 138 |
% Save the initial seed value to \cs{rtInitSeedValue}. |
| 139 |
% \changes{v1.0c}{2013/08/03}{Save the initial seed value to \string\cs{rtInitSeedValue}.} |
| 140 |
% \begin{macrocode} |
| 141 |
\xdef\InitSeedValue{\the\randomi}% |
| 142 |
\fi |
| 143 |
\count@ii\randomi |
| 144 |
\divide\count@ii 127773 % modulus = multiplier * 127773 + 2836 |
| 145 |
\count@\count@ii |
| 146 |
\multiply\count@ii 127773 |
| 147 |
\global\advance\randomi-\count@ii % random mod 127773 |
| 148 |
\global\multiply\randomi 16807 |
| 149 |
\multiply\count@ 2836 |
| 150 |
\global\advance\randomi-\count@ |
| 151 |
\ifnum\randomi<\z@ \global\advance\randomi 2147483647\relax\fi |
| 152 |
\endgroup |
| 153 |
} |
| 154 |
% \end{macrocode} |
| 155 |
% The code for this package was taken from the \textsf{dps} package, and modified |
| 156 |
% suitably. We use several token registers and count registers. This can probably |
| 157 |
% be optimized. |
| 158 |
% \begin{macrocode} |
| 159 |
\newtoks\rt@listIn \rt@listIn={} |
| 160 |
\newtoks\rt@newListIn \rt@newListIn={} |
| 161 |
\newtoks\rt@listOut \rt@listOut={} |
| 162 |
\newcount\rt@nMax |
| 163 |
\newcount\rt@nCnt |
| 164 |
\newcount\rt@getRanNum |
| 165 |
\newif\ifrtdebug \rtdebugfalse |
| 166 |
\newif\ifwerandomize \werandomizetrue |
| 167 |
\newif\ifsaveseed\saveseedtrue |
| 168 |
\newif\ifrt@InputUsedIDs\rt@InputUsedIDsfalse |
| 169 |
\newwrite\rt@Verb@write |
| 170 |
% \end{macrocode} |
| 171 |
% Convenience commands. |
| 172 |
% \begin{macrocode} |
| 173 |
\def\rtcsarg#1#2{\expandafter#1\csname#2\endcsname} |
| 174 |
\def\rt@nameedef#1{\expandafter\edef\csname #1\endcsname} |
| 175 |
% \end{macrocode} |
| 176 |
% \leavevmode\IndexOpt{usedbapp}^^A |
| 177 |
% The code to support a DB application has grown, so much so, it desirves |
| 178 |
% a option so as to include the code only if needed. |
| 179 |
% \changes{v1.3}{2021/01/14}{Added \string\opt{usedbapp} option} |
| 180 |
% \begin{macrocode} |
| 181 |
\DeclareOption{usedbapp}{\let\rtPkgInpt\rt@PkgInpt} |
| 182 |
\def\rt@PkgInpt{\InputIfFileExists{rt-dbapp.def} |
| 183 |
{\PackageInfo{ran_toks}{Inputting rt-dbapp.def}} |
| 184 |
{\PackageInfo{ran_toks}{Cannot find rt-dbapp.def}} |
| 185 |
} |
| 186 |
\let\rtPkgInpt\relax |
| 187 |
\AtEndOfPackage{\rtPkgInpt} |
| 188 |
\ProcessOptions\relax |
| 189 |
% \end{macrocode} |
| 190 |
% \begin{macrocode} |
| 191 |
%</package> |
| 192 |
%<*altpkgname> |
| 193 |
% \end{macrocode} |
| 194 |
% |
| 195 |
% \section{Alternate package name: \texorpdfstring{\protect\pkg{ran-toks}}{ran-toks}} |
| 196 |
% CTAN lists this package (\pkg{ran\_toks}) as \pkg{ran-toks}, so we'll create |
| 197 |
% a dummy package by that name. |
| 198 |
% \changes{v1.2}{2019/12/28}{Added dummy package \string\pkg{ran-toks}} |
| 199 |
% \begin{macrocode} |
| 200 |
\NeedsTeXFormat{LaTeX2e} |
| 201 |
\ProvidesPackage{ran-toks} |
| 202 |
[2019/12/28 v1.0 ran-toks Alt-name (dps)] |
| 203 |
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{ran_toks}} |
| 204 |
\ProcessOptions |
| 205 |
\RequirePackage{ran_toks}[2019/12/28] |
| 206 |
% \end{macrocode} |
| 207 |
% \begin{macrocode} |
| 208 |
%</altpkgname> |
| 209 |
%<*package> |
| 210 |
% \end{macrocode} |
| 211 |
% \section{Commands for controlling the process} |
| 212 |
% |
| 213 |
% \DescribeMacro{\ranToksOn}\DescribeMacro{\ranToksOff} These two turn on and turn off |
| 214 |
% randomization. |
| 215 |
% \begin{macrocode} |
| 216 |
\def\ranToksOn{\werandomizetrue} |
| 217 |
\def\ranToksOff{\werandomizefalse} |
| 218 |
% \end{macrocode} |
| 219 |
% \DescribeMacro{\useThisSeed} initializes the random number generator. Use this to reproduce |
| 220 |
% the same sequence of pseudo-random numbers from an earlier run. We also set |
| 221 |
% \cs{saveseedfalse} so we do not write the initial seed to the disk. |
| 222 |
% \begin{macrocode} |
| 223 |
\def\useThisSeed#1{\saveseedfalse\randomi=#1} |
| 224 |
\@onlypreamble\useThisSeed |
| 225 |
% \end{macrocode} |
| 226 |
% \DescribeMacro{\useLastAsSeed} initializes the random number generator using the |
| 227 |
% last random seed. If the file \cs{jobname\_rt.sav} does not exist, the generator will be |
| 228 |
% initialized using time and date data. |
| 229 |
% \begin{macrocode} |
| 230 |
\def\useLastAsSeed{\rt@useLastAsSeed} |
| 231 |
\@onlypreamble\useLastAsSeed |
| 232 |
\def\rt@useLastAsSeed{% |
| 233 |
\IfFileExists{\jobname_rt.sav}{% |
| 234 |
\PackageInfo{ran_toks}{Inputting \jobname_rt.sav}% |
| 235 |
\@ifundefined{readsavfile}{\newread\readsavfile}{}% |
| 236 |
\openin\readsavfile=\jobname_rt.sav |
| 237 |
\read\readsavfile to \InitSeedValue |
| 238 |
\read\readsavfile to \lastRandomNum |
| 239 |
\closein\readsavfile |
| 240 |
\randomi=\lastRandomNum |
| 241 |
% \end{macrocode} |
| 242 |
% When \cs{useLastAsSeed}, the last becomes the first. |
| 243 |
% \begin{macrocode} |
| 244 |
\xdef\InitSeedValue{\the\randomi} |
| 245 |
}{% |
| 246 |
\PackageInfo{ran_toks}{\jobname_rt.sav cannot |
| 247 |
be found, \MessageBreak |
| 248 |
using the random initializer}% |
| 249 |
}% |
| 250 |
} |
| 251 |
\@ifundefined{aeb@randomizeChoices}{% |
| 252 |
\let\inputRandomSeed\useLastAsSeed |
| 253 |
\let\useRandomSeed\useThisSeed}{} |
| 254 |
% \end{macrocode} |
| 255 |
% |
| 256 |
% \section{Utility commands} |
| 257 |
% |
| 258 |
% A standard \cs{verbatim} write used in exerquiz and other package in the AeB family. |
| 259 |
% \begin{macrocode} |
| 260 |
\def\verbatimwrite{\@bsphack |
| 261 |
\let\do\@makeother\dospecials |
| 262 |
\catcode`\^^M\active \catcode`\^^I=12 |
| 263 |
\def\verbatim@processline{% |
| 264 |
\immediate\write\verbatim@out |
| 265 |
{\the\verbatim@line}}% |
| 266 |
\verbatim@start} |
| 267 |
\def\endverbatimwrite{\@esphack} |
| 268 |
\def\rt@IWVO{\immediate\write\verbatim@out} |
| 269 |
% \end{macrocode} |
| 270 |
% We write only if \cs{ifsaveseed} is true. |
| 271 |
% \begin{macrocode} |
| 272 |
\def\InitSeedValue{\the\randomi} |
| 273 |
\def\rt@writeSeedData{\ifsaveseed |
| 274 |
\@ifundefined{saveseedinfo}{\newwrite\saveseedinfo}{} |
| 275 |
\immediate\openout \saveseedinfo \jobname_rt.sav |
| 276 |
\let\verbatim@out\saveseedinfo |
| 277 |
\def\rt@msgi{initializing seed value}% |
| 278 |
\def\rt@msgii{last random number used}% |
| 279 |
\uccode`c=`\%\uppercase{% |
| 280 |
\rt@IWVO{\InitSeedValue\space c \rt@msgi}% |
| 281 |
\rt@IWVO{\the\randomi\space c \rt@msgii}}\immediate |
| 282 |
\closeout\saveseedinfo\fi} |
| 283 |
% \end{macrocode} |
| 284 |
% Save the initial seed value to hard drive. |
| 285 |
% \begin{macrocode} |
| 286 |
\AtEndDocument{\rt@writeSeedData}% |
| 287 |
% \end{macrocode} |
| 288 |
% \DescribeMacro{\rt@populateList}\hskip-\marginparsep|{|\ameta{n}|}| is a utility command, |
| 289 |
% its argument \ameta{n} is a positive integer, |
| 290 |
% and it generates a list of the form \verb!\\{1}\\{2}...\\{n}! and is held in the |
| 291 |
% token register \cs{rt@listIn} This listing is later |
| 292 |
% randomly permuted by \cs{rt@RandomizeList}. |
| 293 |
% \begin{macrocode} |
| 294 |
\def\rt@populateList#1{\rt@listIn={}\rt@nCnt\z@ |
| 295 |
\@whilenum\rt@nCnt<#1\do{\advance\rt@nCnt\@ne |
| 296 |
\edef\rt@listInHold{\the\rt@listIn\noexpand\\{\the\rt@nCnt}}% |
| 297 |
\rt@listIn=\expandafter{\rt@listInHold}}} |
| 298 |
% \end{macrocode} |
| 299 |
% \DescribeMacro{\rt@RandomizeList}\hskip-\marginparsep\thinspace\texttt{\darg{\ameta{n}}} |
| 300 |
% is the command that gets the process of randomizing |
| 301 |
% the input list going. The argument is the number \ameta{n} of tokens. If |
| 302 |
% \cs{werandomize} is false, it just returns the input list; otherwise, it |
| 303 |
% calls \cs{rt@randomizeList} to actually do the work. |
| 304 |
% \begin{macrocode} |
| 305 |
\def\rt@RandomizeList#1{\global |
| 306 |
\rt@listIn={}\global\rt@newListIn={}\global\rt@listOut={}% |
| 307 |
\rt@nMax=#1\relax\rt@populateList{\the\rt@nMax}% |
| 308 |
\ifwerandomize |
| 309 |
\expandafter\rt@randomizeList\else |
| 310 |
\global\rt@listOut=\expandafter{\the\rt@listIn}\fi |
| 311 |
% \end{macrocode} |
| 312 |
% Save the list out as \cs{rt@BaseName-List} for later retrieval. This is the randomized |
| 313 |
% list of integers for this base name. |
| 314 |
% \changes{v1.1}{2017/05/04}{Save out list for later use} |
| 315 |
% \begin{macrocode} |
| 316 |
\global\rt@nameedef{\rt@BaseName-List}{\the\rt@listOut}} |
| 317 |
% \end{macrocode} |
| 318 |
% \DescribeMacro{\rt@randomizeList} randomizes the list of consecutive integers, and leaves the |
| 319 |
% results, |
| 320 |
%\begin{verbatim} |
| 321 |
% \\{k_1}\\{k_2}...\\{k_n} |
| 322 |
%\end{verbatim} |
| 323 |
% in the token register \cs{rt@listOut}. |
| 324 |
% \cs{rt@randomizeList} is a loop, looping between itself and \cs{rt@loopTest}. |
| 325 |
% \begin{macrocode} |
| 326 |
\def\rt@randomizeList{\let\\=\rt@processi |
| 327 |
\setrannum{\rt@getRanNum}{1}{\the\rt@nMax}% |
| 328 |
\ifrtdebug\typeout{\string\rt@getRanNum=\the\rt@getRanNum}\fi |
| 329 |
\rt@nCnt\z@ |
| 330 |
\ifrtdebug\typeout{LISTING: \the\rt@listIn}\fi |
| 331 |
\the\rt@listIn |
| 332 |
\rt@loopTest |
| 333 |
} |
| 334 |
\def\rt@loopTest{\advance\rt@nMax\m@ne |
| 335 |
\ifnum\rt@nMax>\z@ |
| 336 |
\def\rt@next{% |
| 337 |
\rt@listIn=\expandafter{\the\rt@newListIn}% |
| 338 |
\rt@newListIn={}\rt@randomizeList}% |
| 339 |
\else |
| 340 |
\let\rt@next\relax |
| 341 |
\global\rt@listOut=\expandafter{\the\rt@listOut}% |
| 342 |
\ifrtdebug |
| 343 |
\typeout{Final Result: \string\rt@listOut=\the\rt@listOut}\fi |
| 344 |
\fi\rt@next |
| 345 |
} |
| 346 |
% \end{macrocode} |
| 347 |
% In \cs{rt@randomizeList}, we \verb~\let\\=\rt@processi~ before dumping the |
| 348 |
% contents of \cs{rt@listIn}. We then go into a loop \cs{rt@loopTest}. \cs{rt@getRanNum} |
| 349 |
% is the random integer between 1 and \cs{rt@nMax}. |
| 350 |
% \begin{macrocode} |
| 351 |
\def\rt@processi#1{\advance\rt@nCnt\@ne |
| 352 |
\ifnum\rt@nCnt=\rt@getRanNum |
| 353 |
\edef\rt@listOutHold{\the\rt@listOut}% |
| 354 |
\global\rt@listOut=\expandafter{\rt@listOutHold\\{#1}}% |
| 355 |
\ifrtdebug\typeout{Found it: \string\\{#1}}% |
| 356 |
\typeout{New \string\rt@listOut: \the\rt@listOut}\fi |
| 357 |
\else |
| 358 |
\edef\rt@listInHold{\the\rt@newListIn}% |
| 359 |
\rt@newListIn=\expandafter{\rt@listInHold\\{#1}}% |
| 360 |
\ifrtdebug\typeout{\string\rt@newListIn: \the\rt@newListIn}\fi |
| 361 |
\fi |
| 362 |
} |
| 363 |
% \end{macrocode} |
| 364 |
% We perform modular arithmetic when the index of \cs{useRanTok} is too large. |
| 365 |
% \DescribeMacro{\rt@modarith}\cs{rt@modarith} performs modular arithmetic on its arguments (\texttt{\#1 mod \#2}) |
| 366 |
% and returns the result in the macro \cs{rt@mod}. |
| 367 |
% \begin{macrocode} |
| 368 |
\def\rt@modarith#1#2{\count\z@=#1\relax\count\tw@=#1\relax |
| 369 |
% \end{macrocode} |
| 370 |
% This macro uses \cs{dimen0} and |
| 371 |
% \cs{dimen2}, so it should be called within a group. |
| 372 |
% \begin{macrocode} |
| 373 |
\advance\count\z@\m@ne\divide\count\z@ #2\relax |
| 374 |
\multiply\count\z@ #2\relax |
| 375 |
\advance\count\tw@-\count\z@ |
| 376 |
\edef\rt@mod{\the\count\tw@}} |
| 377 |
% \end{macrocode} |
| 378 |
% Warning messages, these are \DescribeMacro{\rt@badIndex}\cs{rt@badIndex} and |
| 379 |
% \cs{rt@badTokName}\DescribeMacro{\rt@badTokName}. |
| 380 |
% \begin{macrocode} |
| 381 |
\def\rt@badIndex#1#2{\PackageWarningNoLine{ran_toks} |
| 382 |
{The argument of \string\useRanTok{#1} on line |
| 383 |
\the\inputlineno\space is\MessageBreak |
| 384 |
greater than \string\nToksFor{#2} (\nToksFor{#2}), |
| 385 |
instead will use\MessageBreak |
| 386 |
\string\useRanTok{\rt@mod}, obtained from modular |
| 387 |
arithmetic.\MessageBreak |
| 388 |
You might want to fix this}% |
| 389 |
} |
| 390 |
\def\rt@badTokName#1{% |
| 391 |
\PackageWarningNoLine{ran_toks} |
| 392 |
{The token list `#1' on line \the\inputlineno\space |
| 393 |
is undefined,\MessageBreak |
| 394 |
possibly simply mispelled; check spelling.\MessageBreak |
| 395 |
If undefined, use \string\ranToks\space or \string\bRTVToks/% |
| 396 |
\string\eRTVToks\space\MessageBreak |
| 397 |
to define a list with the name `#1'}% |
| 398 |
} |
| 399 |
\def\rt@warnTokName#1{% |
| 400 |
\PackageWarningNoLine{ran_toks} |
| 401 |
{The token list `#1' on line \the\inputlineno\space |
| 402 |
is already defined,\MessageBreak |
| 403 |
will overwrite this list}% |
| 404 |
} |
| 405 |
% \end{macrocode} |
| 406 |
% \section{The main commands} |
| 407 |
% \begin{macro}{\ranToks}\hskip-\marginparsep\,\texttt{\darg{\ameta{token-list}}} |
| 408 |
% takes one argument, \texttt{\darg{\ameta{token-list}}}, a list of tokens. It randomizes them. |
| 409 |
% The randomized listing can be accessed using \cs{useRanTok}. |
| 410 |
% \begin{macrocode} |
| 411 |
\def\ranToks#1{\begingroup |
| 412 |
\useRTName{#1}% |
| 413 |
\r@nToks |
| 414 |
} |
| 415 |
\long\def\r@nToks#1{\rt@nMax\z@\r@ndToks#1\rt@NIL} |
| 416 |
\def\rt@NIL{@nil} |
| 417 |
% \end{macrocode} |
| 418 |
% \end{macro} |
| 419 |
% \begin{macro}{\useRTName}\hskip-\marginparsep\,\texttt{\darg{\ameta{name}}} |
| 420 |
% sets the base name (use prior to the use of \cs{useRanTok}). |
| 421 |
% \begin{macrocode} |
| 422 |
\newcommand{\useRTName}[1]{\gdef\rt@BaseName{#1}}% |
| 423 |
\let\rt@BaseName\@empty |
| 424 |
% \end{macrocode} |
| 425 |
% \end{macro} |
| 426 |
% \begin{macro}{\bRTVToks}\hskip-\marginparsep\,\texttt{\darg{\ameta{name}}} |
| 427 |
% \cs{bRTVToks} and \cs{eRTVToks} enclose a series |
| 428 |
% of \texttt{rtVW} environments. The single argument is the name of this set of |
| 429 |
% verbatim write ``tokens''. |
| 430 |
% \begin{macrocode} |
| 431 |
\newcommand{\bRTVToks}[1]{\rt@nCnt\z@\useRTName{#1}} |
| 432 |
% \end{macrocode} |
| 433 |
% \end{macro} |
| 434 |
% \begin{macro}{\eRTVToks} |
| 435 |
% At the end of the \texttt{rtVW} environments, initiated by \cs{bRTVToks}, the |
| 436 |
% \cs{eRTVToks} command saves the number of tokens counted, and randomizes the |
| 437 |
% access to the contents of the \texttt{rtVW} environments, this done by |
| 438 |
% \cs{r@nVToks}. |
| 439 |
% \begin{macrocode} |
| 440 |
\newcommand{\eRTVToks}{% |
| 441 |
\global\rt@nameedef{\rt@BaseName Cnt}{\the\rt@nCnt}\expandafter |
| 442 |
\r@nVToks\expandafter{\rt@BaseName}} |
| 443 |
% \end{macrocode} |
| 444 |
% \end{macro} |
| 445 |
% \begin{environment}{rtVW} |
| 446 |
% \cs{rtVW} is a verbatim write environment. It saves its contents to the file |
| 447 |
% \verb!\jobname_\rt@BaseName-\the\[email protected]!. The file is later input back into |
| 448 |
% the source file in a random way. |
| 449 |
% \begin{macrocode} |
| 450 |
\def\reVerbEnd{\ifhmode\unskip\fi} |
| 451 |
% \end{macrocode} |
| 452 |
% Insert the hook \cs{rtVWHook} prior to writing the verbatim content. |
| 453 |
% The default is \cs{relax}. |
| 454 |
% \changes{v1.2}{2019/12/28}{Defined \string\cs{rtVWHook}} |
| 455 |
% \begin{macrocode} |
| 456 |
\def\rtVWHook#1{\def\@rgi{#1}\ifx\@rgi\@empty |
| 457 |
\let\RTVWHook\relax\else\def\RTVWHook{#1}\fi} |
| 458 |
\rtVWHook{} |
| 459 |
\newwrite\wrtprobids |
| 460 |
\newif\ifviewIDs\viewIDsfalse |
| 461 |
\newif\ifxDBUnique\xDBUniquefalse |
| 462 |
\def\wrtProbIds#1{\immediate\write\wrtprobids{\string |
| 463 |
\rtcsarg\string\gdef{#1}{used}}} |
| 464 |
\def\rtVWId#1{\ifviewIDs\noindent#1\fi |
| 465 |
\ifxDBUnique\ifrt@InputUsedIDs\wrtProbIds{#1}\fi\fi |
| 466 |
} |
| 467 |
% \end{macrocode} |
| 468 |
% \begin{macrocode} |
| 469 |
\newenvironment{rtVW}{\global\advance\rt@nCnt\@ne |
| 470 |
\immediate\openout\rt@Verb@write |
| 471 |
\jobname_\rt@BaseName-\the\[email protected] |
| 472 |
\let\verbatim@out\rt@Verb@write |
| 473 |
\rt@IWVO{\string\RTVWHook}% |
| 474 |
\rt@IWVO{\string |
| 475 |
\rtVWId{\rt@BaseName-\the\rt@nCnt}\string\relax}% |
| 476 |
\verbatimwrite |
| 477 |
}{% |
| 478 |
\endverbatimwrite |
| 479 |
\immediate\write\rt@Verb@write{\string\reVerbEnd}% |
| 480 |
\immediate\closeout\rt@Verb@write |
| 481 |
} |
| 482 |
% \end{macrocode} |
| 483 |
% (2021/05/29) In support of nested |\bRTVToks/\eRTVToks| constructs, we \cs{let} |
| 484 |
% \cs{rtVWi} and \cs{rtVWii} to \cs{rtVW}, and \cs{endrtVWi} and \cs{endrtVWii} to \cs{endrtVW}. |
| 485 |
% \changes{v1.3.3}{2021/05/29}{Assign \string\cs{rtVWi} and \string\cs{endrtVWi}} |
| 486 |
% \begin{macrocode} |
| 487 |
\let\rtVWi\rtVW % dps5-29 |
| 488 |
\let\endrtVWi\endrtVW |
| 489 |
\let\rtVWii\rtVW % dps5-29 |
| 490 |
\let\endrtVWii\endrtVW |
| 491 |
% \end{macrocode} |
| 492 |
% \end{environment} |
| 493 |
% \DescribeMacro{\r@nVToks} randomizes the contents of the \texttt{rtVW} |
| 494 |
% environment. |
| 495 |
% \begin{macrocode} |
| 496 |
\def\r@nVToks#1{\begingroup |
| 497 |
\gdef\rt@BaseName{#1}% |
| 498 |
\expandafter\rt@nMax\@nameuse{#1Cnt}% |
| 499 |
\rt@listIn={}\rt@nCnt=0\relax\let\rt@listInHold\@empty |
| 500 |
\@whilenum\rt@nCnt<\rt@nMax\do{\advance\rt@nCnt\@ne |
| 501 |
\edef\rt@listInHold{% |
| 502 |
\the\rt@listIn{\noexpand\rt@inputVerb{#1-\the\rt@nCnt}}}% J14 |
| 503 |
\rt@listIn=\expandafter{\rt@listInHold}}\ifrtdebug |
| 504 |
\typeout{\string\r@nVToks: \the\rt@listIn}\fi |
| 505 |
\expandafter\r@nToks\expandafter{\the\rt@listIn}} |
| 506 |
% \end{macrocode} |
| 507 |
% \DescribeMacro\rt@inputVerb\hskip-\marginparsep\texttt{\darg{\ameta{db-name}-\ameta{num}}} This |
| 508 |
% is the command that inputs a DB Test problem, it inputs it from the file named |
| 509 |
% \cs{jobname\_\darg{\ameta{db-name}-\ameta{num}}}. As we input, we make a record of the |
| 510 |
% problem we input by expanding \cs{rt@recordAsUsed\darg{\ameta{db-name}-\ameta{num}}}, which |
| 511 |
% itself expands to \cs{rtcsarg\string\gdef\darg{\ameta{db-name}-\ameta{num}}\darg{used}}. This is necessary when we are |
| 512 |
% choosing more than one item from a given DB Test file; it must be recorded immediately so that later |
| 513 |
% it cannot be used again, if possible. |
| 514 |
% \begin{macrocode} |
| 515 |
\def\rt@inputVerb#1{\rt@recordAsUsed{#1}\input{\jobname_#1.cut}} |
| 516 |
%\def\rt@recordAsUsed#1{\ifxDBUnique\rtcsarg\gdef{#1}{used}\fi} |
| 517 |
\def\rt@recordAsUsed#1{\rtcsarg\gdef{#1}{used}} |
| 518 |
% \end{macrocode} |
| 519 |
% \DescribeMacro{\r@ndToks} is main looping command for \cs{ranToks} |
| 520 |
% and \cs{eRTVToks} (through \cs{r@nVToks}). If the ending token \cs{rt@NIL} is detected, we |
| 521 |
% break off and go to \cs{rt@endToks}. |
| 522 |
% \changes{v1.0e}{2016/02/06}{Fixed a bug, when the first two tokens \string\texttt{\#1} are the same, |
| 523 |
% we get an incorrect decision} |
| 524 |
% \begin{macrocode} |
| 525 |
\def\rt@PAR{\par} |
| 526 |
\long\def\r@ndToks#1{\def\rt@rgi{#1}% |
| 527 |
% \end{macrocode} |
| 528 |
% If the current argument is \cs{par}, we skip it |
| 529 |
% \begin{macrocode} |
| 530 |
\ifx\rt@rgi\rt@PAR\def\rt@next{\r@ndToks}\else |
| 531 |
\advance\rt@nMax\@ne |
| 532 |
\global\@namedef{rtTok\the\rt@nMax\rt@BaseName}{#1}% |
| 533 |
\def\rt@next{\@ifnextchar\rt@NIL |
| 534 |
{\rt@endToks\@gobble}{\r@ndToks}}\fi\rt@next} |
| 535 |
% \end{macrocode} |
| 536 |
% \hskip-\marginparsep|{|\ameta{n}|}| The \DescribeMacro{\rt@performRanDefns}\cs{rt@performRanDefns} performs code that |
| 537 |
% is repeated in several other macros: \cs{rt@endToks}, \cs{reorderRanToks}, and |
| 538 |
% \cs{copyRanToks}. It randomizes the list {\cs{rt@RandomizeList}, then assignments |
| 539 |
% the randomized list to the definitions. |
| 540 |
% \begin{macrocode} |
| 541 |
\def\rt@performRanDefns#1{% |
| 542 |
% \end{macrocode} |
| 543 |
% Now we randomize the order of the integers 1, 2,\dots \texttt{\#1}. |
| 544 |
% \begin{macrocode} |
| 545 |
\rt@RandomizeList{#1}\rt@nCnt\z@ |
| 546 |
% \end{macrocode} |
| 547 |
% Now we randomize the definitions. We \verb!\let\\=\rt@ssign!, then |
| 548 |
% let loose the tokens! |
| 549 |
% \begin{macrocode} |
| 550 |
\let\\\rt@ssign\the\rt@listOut} |
| 551 |
% \end{macrocode} |
| 552 |
% The final destination for \DescribeMacro{\rt@endToks}\cs{r@ndToks}. |
| 553 |
% \begin{macrocode} |
| 554 |
\def\rt@endToks{\global |
| 555 |
% \end{macrocode} |
| 556 |
% Save the number of tokens counted |
| 557 |
% \begin{macrocode} |
| 558 |
\rt@nameedef{nMax4\rt@BaseName}{\the\rt@nMax}% |
| 559 |
\rt@performRanDefns{\the\rt@nMax}\endgroup} |
| 560 |
% \end{macrocode} |
| 561 |
% \begin{macro}{\reorderRanToks}\hskip-\marginparsep\,\texttt{\darg{\ameta{name}}} |
| 562 |
% The \cs{reorderRanToks} command reorders (or re-indexes) |
| 563 |
% the family with name \ameta{name} (\texttt{\#1}). |
| 564 |
% \begin{macrocode} |
| 565 |
\def\reorderRanToks#1{\begingroup\useRTName{#1}\expandafter |
| 566 |
\ifx\csname nMax4#1\endcsname\relax |
| 567 |
% \end{macrocode} |
| 568 |
% Document author has not run \cs{ranToks} yet for this basename (\texttt{\#1}) |
| 569 |
% \begin{macrocode} |
| 570 |
\rt@badTokName{#1}\else |
| 571 |
% \end{macrocode} |
| 572 |
% Good to go. We reorder this list. |
| 573 |
% \begin{macrocode} |
| 574 |
\rt@performRanDefns{\@nameuse{nMax4#1}}\fi |
| 575 |
\endgroup} |
| 576 |
% \end{macrocode} |
| 577 |
% \end{macro} |
| 578 |
% \begin{macro}{\copyRanToks}\hskip-\marginparsep |
| 579 |
% \texttt{\darg{\ameta{name1}}\darg{\ameta{name2}}} |
| 580 |
% Use this command to copy \ameta{name1} to \ameta{name2}. This gives |
| 581 |
% a randomization of the same list, without affecting the original order of \ameta{name1}. |
| 582 |
% \begin{macrocode} |
| 583 |
\newcommand\copyRanToks[2]{\begingroup |
| 584 |
\expandafter |
| 585 |
\ifx\csname nMax4#1\endcsname\relax |
| 586 |
% \end{macrocode} |
| 587 |
% Source list is not defined |
| 588 |
% \begin{macrocode} |
| 589 |
\rt@badTokName{#1}% |
| 590 |
\else |
| 591 |
% \end{macrocode} |
| 592 |
% Source list is defined |
| 593 |
% \begin{macrocode} |
| 594 |
\expandafter |
| 595 |
\ifx\csname nMax4#2\endcsname\relax |
| 596 |
% \end{macrocode} |
| 597 |
% Destination list is not defined, which is good in this instance. This is |
| 598 |
% the case we copy the list. |
| 599 |
% \begin{macrocode} |
| 600 |
\useRTName{#2}\global |
| 601 |
\rt@nameedef{nMax4#2}{\@nameuse{nMax4#1}}% |
| 602 |
\rt@nCnt=\csname nMax4#2\endcsname\relax |
| 603 |
\@whilenum\rt@nCnt>\z@\do{\global |
| 604 |
\rt@nameedef{rtTok\the\rt@nCnt#2}% |
| 605 |
{\noexpand\@nameuse{rtTok\the\rt@nCnt#1}}% |
| 606 |
\advance\rt@nCnt\m@ne |
| 607 |
}\rt@performRanDefns{\@nameuse{nMax4#2}}% |
| 608 |
\else |
| 609 |
% \end{macrocode} |
| 610 |
% Destination list is defined already, warn the user. |
| 611 |
% \begin{macrocode} |
| 612 |
\rt@warnTokName{#2}% |
| 613 |
\fi |
| 614 |
\fi |
| 615 |
\endgroup |
| 616 |
} |
| 617 |
% \end{macrocode} |
| 618 |
% \end{macro} |
| 619 |
%\DescribeMacro{\rt@ssign}\hskip-\marginparsep\texttt{\darg{\ameta{name}}} makes the assignments that are expanded by \cs{useRanTok}. |
| 620 |
% We \cs{let} the assignment \verb!\let\\=\rt@ssign! in \cs{rt@endToks}, just before we dump out the |
| 621 |
% contents of \verb!\the\rt@listOut!. |
| 622 |
% \begin{macrocode} |
| 623 |
\def\rt@ssign#1{\advance\rt@nCnt\@ne\global |
| 624 |
\rt@nameedef{rtRanTok\the\rt@nCnt\rt@BaseName}{\noexpand |
| 625 |
\@nameuse{rtTok#1\rt@BaseName}}} |
| 626 |
% \end{macrocode} |
| 627 |
% \subsection{Additional user access commands} |
| 628 |
% \DescribeMacro{\nToksFor}\hskip-\marginparsep\texttt{\darg{\ameta{name}}} |
| 629 |
% expands the the number of tokens whose name is \ameta{name} (\texttt{\#1}). |
| 630 |
% \begin{macrocode} |
| 631 |
\newcommand{\nToksFor}[1]{\expandafter |
| 632 |
\ifx\csname nMax4#1\endcsname\relax |
| 633 |
\textbf{??}\rt@badTokName{#1}\else |
| 634 |
\@nameuse{nMax4#1}\fi |
| 635 |
} |
| 636 |
% \end{macrocode} |
| 637 |
% \DescribeMacro{\rtTokByNum}\hskip-\marginparsep\texttt{[\ameta{name}]\darg{\ameta{num}}} is an internal macro, but it can be used publicly. |
| 638 |
% The argument of it is an integer, eg, \verb!\rtTokByNum{3}! is the third token, as listed in the order |
| 639 |
% given in the argument of \cs{ranToks}. |
| 640 |
% \begin{macrocode} |
| 641 |
\newcommand{\rtTokByNum}[2][\rt@BaseName]{\expandafter |
| 642 |
\ifx\csname nMax4#1\endcsname\relax |
| 643 |
\textbf{??}\rt@badTokName{#1}\else |
| 644 |
\@nameuse{rtTok#2#1}\expandafter\ignorespaces |
| 645 |
\fi |
| 646 |
} |
| 647 |
% \end{macrocode} |
| 648 |
%\DescribeMacro{\useRanTok}\hskip-\marginparsep\texttt{[\ameta{name}]\darg{\ameta{num}}} |
| 649 |
% After \cs{ranToks} has been executed, the user has access to the |
| 650 |
% randomized tokens through \cs{useRanTok}. The argument \ameta{num} is an integer 1 through max. |
| 651 |
% \begin{macrocode} |
| 652 |
% \end{macrocode} |
| 653 |
% We provide two commands to control the feature of try to select unique |
| 654 |
% choices across multiple renditions of the same source file. |
| 655 |
% \DescribeMacro\uniqueXDBChoicesOn\cs{uniqueXDBChoicesOn}, turns on this feature; |
| 656 |
% the default is \DescribeMacro\uniqueXDBChoicesOff\cs{uniqueXDBChoicesOff} make |
| 657 |
% no changes to how \cs{useRanTok} operates. One other command we define here is |
| 658 |
% \DescribeMacro\makeInfoAWarning\cs{makeInfoAWarning}; this command applies only |
| 659 |
% when \cs{uniqueXDBChoicesOn} is expanded. In the macro \cs{xdb@unique} which is |
| 660 |
% expanded when \cs{uniqueXDBChoicesOn} is expanded first, there is one line that |
| 661 |
% reports information to the log as \cs{PackageInfo}. By expanding \cs{makeInfoAWarning} |
| 662 |
% we change \cs{PackageInfo} to \cs{PackageWarning}, which gives it greater |
| 663 |
% visibility in the log and the log report. |
| 664 |
% \changes{v1.3.1}{2021/01/19}{Added \string\cs{ifrt@recording}} |
| 665 |
% \begin{macrocode} |
| 666 |
\newcommand{\uniqueXDBChoicesOn}{\xDBUniquefalse |
| 667 |
\PackageWarning{ran_toks} |
| 668 |
{The \string\uniqueXDBChoicesOn\space requires the\MessageBreak |
| 669 |
\texttt{usedbapp} option}} |
| 670 |
\newcommand{\uniqueXDBChoicesOff}{\let\xdbunique\relax\xDBUniquefalse} |
| 671 |
\let\xdbunique\relax |
| 672 |
\newcommand{\makeInfoAWarning}{\def\pkgNotifType{\PackageWarning}} |
| 673 |
\def\pkgNotifType{\PackageInfo} |
| 674 |
\newif\ifrt@recording \rt@recordingtrue % dps |
| 675 |
% \end{macrocode} |
| 676 |
% Now for the definition of \cs{useRanTok}. |
| 677 |
% \begin{macrocode} |
| 678 |
\newcommand{\useRanTok}[2][\rt@BaseName]{\bgroup\expandafter |
| 679 |
\ifx\csname nMax4#1\endcsname\relax |
| 680 |
\rt@badTokName{#1}\global\let\rt@next\relax |
| 681 |
\else |
| 682 |
\ifnum#2>\@nameuse{nMax4#1}% |
| 683 |
\rt@modarith{#2}{\@nameuse{nMax4#1}}% |
| 684 |
\rt@badIndex{#2}{#1}\edef\Indx{\rt@mod}% |
| 685 |
\else |
| 686 |
\edef\Indx{#2}% |
| 687 |
\fi |
| 688 |
\xdef\rt@orig@Indx{\Indx}% |
| 689 |
% \end{macrocode} |
| 690 |
% If \cs{xdbunique} is \cs{relax}, \cs{useRanTok} executes as it did in the past |
| 691 |
% (no change in behavior); otherwise, we expand \cs{xdb@unique} which attempts |
| 692 |
% to avoid duplicate choices based on the DBs input by \cs{useProbDBs}. |
| 693 |
% \begin{macrocode} |
| 694 |
\ifx\xdbunique\relax |
| 695 |
\ifrt@recording\rt@recordAsUsed{#1-\Indx}\fi |
| 696 |
\xdef\rt@next{\noexpand\@nameuse{rtRanTok\Indx#1}}% |
| 697 |
\else |
| 698 |
\xdb@unique{#1}% |
| 699 |
\fi |
| 700 |
\fi |
| 701 |
\egroup |
| 702 |
\rt@next |
| 703 |
} |
| 704 |
% \end{macrocode} |
| 705 |
% \DescribeMacro{\displayListRandomly}\hskip-\marginparsep\texttt{[\ameta{prior}][\ameta{post}]\darg{\ameta{name}}} |
| 706 |
% lists all items in the list as passed |
| 707 |
% by the required argument. For expanding in a list environment, use \cs{item} as |
| 708 |
% the optional argument. Designed for listing all question in an eqexam |
| 709 |
% document in random order. |
| 710 |
%\changes{v1.0b}{2013/07/29}{Added \string\cs{displayListRandomly}} |
| 711 |
%\changes{v1.0e}{2016/02/06}{Added optional argument to \string\cs{displayListRandomly}} |
| 712 |
%\changes{v1.1}{2017/05/04}{Added second optional argument to \string\cs{displayListRandomly}} |
| 713 |
%\changes{v1.3.3}{2021/05/29}{In support of nested \string\cs{bRTVToks}/\string\cs{eRTVToks}\space |
| 714 |
% constructs, replace the counter \string\cs{rt@nCnt} with \string\cs{@tempcntb}} |
| 715 |
% \begin{macrocode} |
| 716 |
\newcommand{\displayListRandomly}[1][]{\begingroup |
| 717 |
\def\rt@prior{#1}\displ@yListRandomly |
| 718 |
} |
| 719 |
\newcommand{\displ@yListRandomly}[2][]{\@tempcntb\z@ % dps5-29 |
| 720 |
\expandafter\ifx\csname nMax4#2\endcsname\relax |
| 721 |
\rt@rgi\space\textbf{??}\rt@badTokName{#2}#1% |
| 722 |
\else |
| 723 |
\rt@recordingfalse |
| 724 |
% \end{macrocode} |
| 725 |
% Within the optional arguments, we define \DescribeMacro{\i}\cs{i}, |
| 726 |
% \DescribeMacro{\first}\cs{first}, \DescribeMacro{\last}\cs{last}, and |
| 727 |
% \DescribeMacro{\lessone}\cs{lessone} to do some logic on the arguments. |
| 728 |
% These four macro are defined locally and not available outside the command |
| 729 |
% \cs{displayListRandomly}. |
| 730 |
% \begin{macrocode} |
| 731 |
\def\rt@post{#1}\useRTName{#2}\let\i\@tempcntb |
| 732 |
\def\first{1}\edef\last{\@nameuse{nMax4#2}}\@tempcnta\last |
| 733 |
\advance\@tempcnta\m@ne |
| 734 |
\edef\lessone{\the\@tempcnta}\@whilenum\@tempcntb<\last |
| 735 |
\advance\@tempcntb\@ne |
| 736 |
% \end{macrocode} |
| 737 |
% There is one example of this command getting confused and printing the wrong |
| 738 |
% number of items. Here, we pass the optional argument of \cs{useRanTok} and that |
| 739 |
% cleared up the problem. |
| 740 |
% \changes{v1.3.2}{2021/05/25}{Insert optional argument for \string\cs{useRanTok} |
| 741 |
% within \string\cs{displ@yListRandomly}} |
| 742 |
% \begin{macrocode} |
| 743 |
\do{\rt@prior\useRanTok[#2]{\the\@tempcntb}\rt@post |
| 744 |
}\fi |
| 745 |
\endgroup |
| 746 |
} |
| 747 |
%</package> |
| 748 |
%<*dbapp> |
| 749 |
% \end{macrocode} |
| 750 |
% \section{A DB application} |
| 751 |
% This (optional) section supports an application of \pkg{ran\_toks} to the \pkg{eqexam} package; though, |
| 752 |
% conceptually, the commands of this section may be applied in other settings. In this application, |
| 753 |
% the document author has a series of DB test files (TEX files), each file contains \cs{bRTVToks}/\allowbreak |
| 754 |
% \cs{eRTVToks} constructs, which contain a series of \env{rtVW} environments of verbatim content. In this application, |
| 755 |
% the verbatim content are \env{problem}/\env{problem*} environments of \pkg{eqexam}. |
| 756 |
% |
| 757 |
% The following verbatim listing is taken from the preamble of \texttt{mc-db.tex}, which illustrates the layout |
| 758 |
% of how to apply the commands of this section. |
| 759 |
%\begin{Verbatim}[xleftmargin=\parindent,fontsize=\small, |
| 760 |
%commandchars={!()},codes={\catcode`\%=9}] |
| 761 |
%\examNum{1} |
| 762 |
%\numVersions{4} |
| 763 |
%\forVersion{a} |
| 764 |
%!%!textsf( initial seeds for each of the four versions of this document) |
| 765 |
%\vA{\useThisSeed{54356}} |
| 766 |
%\vB{\useThisSeed{577867}} |
| 767 |
%\vC{\useThisSeed{6746788}} |
| 768 |
%\vD{\useThisSeed{856785}} |
| 769 |
% |
| 770 |
%\uniqueXDBChoicesOn !%!textsf( Try to avoid duplicate questions in multi-version doc) |
| 771 |
%\InputUsedIDs !%!textsf( Input history of previous versions to current version) |
| 772 |
%\viewIDstrue !%!textsf( To view the IDs of problems used) |
| 773 |
%... |
| 774 |
%\useTheseDBs{db1,db2,db3,db4} |
| 775 |
%\end{Verbatim} |
| 776 |
% \changes{v1.3}{2021/01/14}{Added several commands and macro to continue to support |
| 777 |
% a DB application.} |
| 778 |
% If \cs{ifxDBUnique} is true and if \pkg{eqexam} is loaded, we |
| 779 |
% open \cs{wrtprobids} which is used to write the problem IDs of the problems |
| 780 |
% already chosen in earlier version of this source file. The name of this file |
| 781 |
% is \cs{jobname-ver\string\selVersion.cut}; eg, \texttt{mc-db-verA.cut}, |
| 782 |
% \texttt{mc-db-verB.cut}, etc. |
| 783 |
% \begin{macrocode} |
| 784 |
\def\rt@OpenProbIds{\@ifpackageloaded{eqexam} |
| 785 |
{\immediate\openout\wrtprobids\jobname-ver\selVersion.cut}{}} |
| 786 |
% \end{macrocode} |
| 787 |
% We open the file \cs{jobname-ver\cs{selVersion}.cut} when \cs{InputUsedIDs} is |
| 788 |
% expanded in the preamble. |
| 789 |
% \begin{macrocode} |
| 790 |
%\def\rt@ABD{\ifxDBUnique\expandafter\rt@OpenProbIds\fi} |
| 791 |
\def\rt@ABD{\@ifundefined{eq@nVersions}{} |
| 792 |
{\ifnum\eq@nVersions>\@ne\expandafter\rt@OpenProbIds\fi}} |
| 793 |
% \end{macrocode} |
| 794 |
% We begin with some utility commands to help parse the argument of \cs{useProbDBs}. |
| 795 |
% \begin{macrocode} |
| 796 |
\def\rt@gettonil#1\@nil{\def\to@nilarg{#1}} |
| 797 |
\def\rt@ifspc{\ifx\@let@token\@sptoken |
| 798 |
\let\rt@next\rt@xifspc\else |
| 799 |
\let\rt@next\rt@gettonil\fi\rt@next |
| 800 |
} |
| 801 |
\begingroup |
| 802 |
\def\:{\rt@xifspc}\expandafter |
| 803 |
\gdef\: {\futurelet\@let@token\rt@ifspc} |
| 804 |
\endgroup |
| 805 |
\def\rt@strpspcs{\futurelet\@let@token\rt@ifspc} |
| 806 |
% \end{macrocode} |
| 807 |
% \begin{macro}{\useTheseDBs}\hskip-\marginparsep\thinspace\texttt{\darg{\ameta{list}}} |
| 808 |
% Inputs any files included in the comma-delimited list. The base names need only be listed, |
| 809 |
% as the extension is assumed to be \texttt{.tex}. The command \cs{useProbDBs} can only be used |
| 810 |
% in the preamble. Refer to the demo file \texttt{mc\_db.tex} for an illustration of its intended use. |
| 811 |
% \begin{macrocode} |
| 812 |
\def\ProbDBWarningMsg#1{\filename@parse{#1} |
| 813 |
\PackageWarning{ran_toks} |
| 814 |
{The file \filename@area\filename@base.\ifx\filename@ext\relax |
| 815 |
tex\else\filename@ext\fi\space cannot be found}} |
| 816 |
\def\useTheseDBs#1{\def\rt@dblist{#1}\ifx\rt@dblist\@empty\else |
| 817 |
\let\rt@DB@List\@empty |
| 818 |
\edef\temp@expand{\noexpand\@for\noexpand\@@tmp:=\rt@dblist}% |
| 819 |
\temp@expand\do{\ifx\@@tmp\@empty\else |
| 820 |
\expandafter\rt@strpspcs\@@tmp\@nil\edef\@@tmp{\to@nilarg}% |
| 821 |
\edef\rt@nextDB{\noexpand |
| 822 |
\InputIfFileExists{\@@tmp}{}{\noexpand |
| 823 |
\ProbDBWarningMsg{\@@tmp}}}% |
| 824 |
\toks\tw@=\expandafter{\rt@DB@List}% |
| 825 |
\toks@=\expandafter{\rt@nextDB}% |
| 826 |
\edef\rt@DB@List{\the\toks\tw@\space\the\toks@}\fi |
| 827 |
}\expandafter\rt@DB@List\fi} |
| 828 |
% \end{macrocode} |
| 829 |
% \end{macro} |
| 830 |
% \begin{macro}{\useProbDBs}\hskip-\marginparsep\thinspace\texttt{\darg{\ameta{list}}} Is an alias of |
| 831 |
% \cs{useTheseDBs}. |
| 832 |
% \begin{macrocode} |
| 833 |
\let\useProbDBs\useTheseDBs |
| 834 |
% \end{macrocode} |
| 835 |
% \end{macro} |
| 836 |
% \begin{macro}{\viewDB}\hskip-\marginparsep\thinspace\texttt{\darg{\ameta{name}}} |
| 837 |
% Typeset the entire contents of a DB Test file. The argument \ameta{name} is the name |
| 838 |
% of the DB Test file (as in |\bRTVToks{DB1}|, here \texttt{DB1} is the \ameta{name}. |
| 839 |
% The DB test files should be input using \cs{useProbDBs}. |
| 840 |
% \begin{macrocode} |
| 841 |
\def\viewDB#1{\useRTName{#1}\rt@nCnt\z@ |
| 842 |
\edef\nSTOP{\@nameuse{nMax4\rt@BaseName}}% |
| 843 |
\loop\advance\rt@nCnt\@ne |
| 844 |
\rtTokByNum{\the\rt@nCnt}% |
| 845 |
\ifnum\rt@nCnt<\nSTOP\repeat |
| 846 |
} |
| 847 |
% \end{macrocode} |
| 848 |
% \end{macro} |
| 849 |
% The macro \DescribeMacro\getR@nIndx\cs{getR@nIndx} executes with each |
| 850 |
% entry of |\@nameuse{#1-List}|. |
| 851 |
% For an index value of \cs{Indx}, the macro goes through the |
| 852 |
% arguments to the \cs{Indx}'th argument and reads |
| 853 |
% the value of the argument at that point. It |
| 854 |
% returns the argument of the \cs{Indx}'th as \cs{ranIndx}; |
| 855 |
% eg, if |\Indx=1|, then |\ranIndx=3|, for the above example. |
| 856 |
% \begin{macrocode} |
| 857 |
%% uses \@tempcnta and \Indx |
| 858 |
\def\getR@nIndx#1{\def\argi{#1}% |
| 859 |
\ifx\argi\rt@STOP |
| 860 |
% no match, something is wrong |
| 861 |
\edef\ranIndex{-1}\else |
| 862 |
\advance\@tempcnta\@ne |
| 863 |
\ifnum\Indx=\@tempcnta |
| 864 |
\def\ranIndx{#1}\fi |
| 865 |
\fi |
| 866 |
} |
| 867 |
% \end{macrocode} |
| 868 |
% \DescribeMacro\xdb@unique\hskip-\marginparsep\texttt{\darg{\ameta{name}}} |
| 869 |
% An add-on command to \cs{useRanTok}. The command attempts |
| 870 |
% to create a unique choice of a problem over several versions of the same document. This may not |
| 871 |
% be possible if there are not enough choices to satisfy the number of declared versions. |
| 872 |
% |
| 873 |
% The \cs{xdb@unique} seems to work when the \pkg{eqexam} document has multiple parts (more then one |
| 874 |
% \env{exam} environments). The id files for the parts are all combined; ideally, for multiple part |
| 875 |
% exams, the second part draws form a set of DB test files different from the first part, as long as there are |
| 876 |
% enough problems to choose from. |
| 877 |
% |
| 878 |
% Note that, if there is not a unique choice for a question from the designated DB test file, |
| 879 |
% \cs{xdb@unique} reverts to the original choice so there may be duplicates across versions of the document. |
| 880 |
% \begin{macrocode} |
| 881 |
\def\rt@NoAltChoice#1#2{\PackageWarning{ran_toks} |
| 882 |
{Cannot find an alternative to #1-#2,\MessageBreak |
| 883 |
will use it but it may be a duplicate\MessageBreak |
| 884 |
question}} |
| 885 |
\def\xdb@unique#1{\@tempcnta\z@ |
| 886 |
\def\rt@STOP{\relax}% |
| 887 |
% \end{macrocode} |
| 888 |
%We use the randomized list for the \ameta{name} |
| 889 |
\begin{Verbatim}[xleftmargin=\parindent,fontsize=\small, |
| 890 |
%commandchars={!()},codes={\catcode`\%=9}] |
| 891 |
%\@nameuse{#1-List} is the randomized list: eg, |
| 892 |
%\\{3}\\{2}\\{4}\\{5}\\{1} |
| 893 |
%\end{Verbatim} |
| 894 |
% \begin{macrocode} |
| 895 |
\let\\\relax\edef\x{\@nameuse{#1-List}}% |
| 896 |
\toks@=\expandafter{\x}\let\\\getR@nIndx |
| 897 |
\the\toks@\\\rt@STOP |
| 898 |
% \end{macrocode} |
| 899 |
% We take as the default choice the original choice |
| 900 |
% \begin{macrocode} |
| 901 |
\xdef\rt@next{\noexpand |
| 902 |
\@nameuse{rtRanTok\rt@orig@Indx#1}}% |
| 903 |
% \end{macrocode} |
| 904 |
% Begin to look at the results of |\the\toks@\\\rt@STOP|, |
| 905 |
% \begin{macrocode} |
| 906 |
\ifnum\ranIndx>\m@ne |
| 907 |
% \end{macrocode} |
| 908 |
% If \cs{ranIndx} is -1, we use the original index. |
| 909 |
% \begin{macrocode} |
| 910 |
\edef\rt@orig@ranIndx{\ranIndx}% |
| 911 |
\expandafter |
| 912 |
\ifx\csname#1-\ranIndx\endcsname\relax |
| 913 |
% \end{macrocode} |
| 914 |
% This question has not been chosen earlier, so we'll use it. |
| 915 |
% \begin{macrocode} |
| 916 |
\xdef\rt@next{\noexpand |
| 917 |
\@nameuse{rtRanTok\Indx#1}}% |
| 918 |
\else |
| 919 |
% \end{macrocode} |
| 920 |
% The question has been chosen in an earlier version of the document. |
| 921 |
% Find the next higher unused one (cycle search). |
| 922 |
% \begin{macrocode} |
| 923 |
\@tempcntb\z@ |
| 924 |
\rt@nCnt\rt@orig@Indx\relax |
| 925 |
% \end{macrocode} |
| 926 |
% As we move into the \cs{@whilenum} loop, we take as the default |
| 927 |
% the original index. The loop may overwrite the definition of |
| 928 |
% \cs{rt@next}. |
| 929 |
% \begin{macrocode} |
| 930 |
\xdef\rt@next{\noexpand\rt@NoAltChoice{#1}{\rt@orig@Indx}\noexpand |
| 931 |
\@nameuse{rtRanTok\rt@orig@Indx#1}}% |
| 932 |
\@whilenum\@tempcntb<\@nameuse{nMax4#1}\do{% |
| 933 |
\advance\@tempcntb\@ne |
| 934 |
\advance\rt@nCnt\@ne |
| 935 |
% \end{macrocode} |
| 936 |
% If the count is at the \texttt{nMax4} value, we start over from |
| 937 |
% the beginning. |
| 938 |
% \begin{macrocode} |
| 939 |
\ifnum\rt@nCnt>\@nameuse{nMax4#1}\rt@nCnt\@ne\fi |
| 940 |
% \end{macrocode} |
| 941 |
% We search through \cs{toks@} again, so we have the initialize |
| 942 |
% the dependent variables: \cs{Indx}, the index to search for; \cs{@tempcnta} |
| 943 |
% the counter that is used by \cs{getR@nIndx}; nothing has changed |
| 944 |
% |\let\\\getR@nIndx| should still be in effect. |
| 945 |
% \begin{macrocode} |
| 946 |
\edef\Indx{\the\rt@nCnt}\@tempcnta\z@ |
| 947 |
\the\toks@\\\rt@STOP |
| 948 |
% \end{macrocode} |
| 949 |
% If \cs{ranIndx} is -1, we use the original index. |
| 950 |
% \begin{macrocode} |
| 951 |
\ifnum\ranIndx>\m@ne |
| 952 |
\expandafter |
| 953 |
\ifx\csname#1-\ranIndx\endcsname\relax |
| 954 |
\pkgNotifType{ran_toks}{#1-\rt@orig@ranIndx\space |
| 955 |
has already been used,\MessageBreak |
| 956 |
will use #1-\ranIndx}% |
| 957 |
% exit the \@whilenum loop |
| 958 |
\@tempcntb\@nameuse{nMax4#1}% |
| 959 |
\advance\@tempcntb\@ne |
| 960 |
\fi |
| 961 |
\fi |
| 962 |
\xdef\rt@next{\noexpand\@nameuse{rtRanTok\Indx#1}}% |
| 963 |
}% do |
| 964 |
\ifnum\@tempcntb=\@nameuse{nMax4#1}\relax |
| 965 |
\xdef\rt@next{\noexpand |
| 966 |
\rt@NoAltChoice{#1}{\rt@orig@ranIndx}\noexpand |
| 967 |
\@nameuse{rtRanTok\rt@orig@Indx#1}}% |
| 968 |
\fi |
| 969 |
\fi |
| 970 |
\fi |
| 971 |
} |
| 972 |
% \end{macrocode} |
| 973 |
% Here is the operational definition of \DescribeMacro\uniqueXDBChoicesOn |
| 974 |
% \cs{uniqueXDBChoicesOn}; when executed in the preamble, an attempt is made |
| 975 |
% the select only problems that have not already been chosen in any prior |
| 976 |
% renditions of the same source file. |
| 977 |
% \begin{macrocode} |
| 978 |
\renewcommand{\uniqueXDBChoicesOn}{\xDBUniquetrue |
| 979 |
\let\xdbunique\xdb@unique} |
| 980 |
% \end{macrocode} |
| 981 |
% \begin{macro}{\InputUsedIDs} Input the user ID (CUT) files. These are files |
| 982 |
% that document which questions were used for the various versions of the exam. |
| 983 |
% \begin{macrocode} |
| 984 |
\newif\ifrt@InputUsedIDs\rt@InputUsedIDsfalse |
| 985 |
\def\InputUsedIDs{\rt@InputUsedIDstrue |
| 986 |
\bgroup |
| 987 |
\setcounter{eq@count}{0}% |
| 988 |
\let\rt@InputUsedIDs\@empty |
| 989 |
\let\rt@InputUsedIDsFIs\@empty |
| 990 |
\@whilenum \value{eq@count}<\eq@nVersions\relax\do |
| 991 |
{% |
| 992 |
\stepcounter{eq@count}% |
| 993 |
\g@addto@macro\rt@InputUsedIDs{\if\selVersion}% |
| 994 |
\g@addto@macro\rt@InputUsedIDsFIs{\fi}% |
| 995 |
\edef\x{\Alph{eq@count}}% |
| 996 |
\edef\y{\noexpand\g@addto@macro\noexpand |
| 997 |
\rt@InputUsedIDs{\x\expandafter\noexpand |
| 998 |
\csname else\endcsname\noexpand\rt@IIFE}}\y |
| 999 |
\edef\x{{\x}}\expandafter |
| 1000 |
\g@addto@macro\expandafter\rt@InputUsedIDs\expandafter{\x}% |
| 1001 |
}% do |
| 1002 |
\expandafter\g@addto@macro\expandafter |
| 1003 |
\rt@InputUsedIDs\expandafter{\rt@InputUsedIDsFIs}% |
| 1004 |
\egroup |
| 1005 |
\rt@InputUsedIDs |
| 1006 |
\AtBeginDocument{\rt@ABD}% |
| 1007 |
} |
| 1008 |
\@onlypreamble\InputUsedIDs |
| 1009 |
% \end{macrocode} |
| 1010 |
% \end{macro} |
| 1011 |
% A convenience command used by \cs{InputUsedIDs}. |
| 1012 |
% \begin{macrocode} |
| 1013 |
\def\rt@IIFE#1{\InputIfFileExists{\jobname-ver#1.cut} |
| 1014 |
{\PackageInfo{ran_toks}{Inputting \jobname-ver#1.cut}} |
| 1015 |
{\PackageInfo{ran_toks}{Cannot find \jobname-ver#1.cut}}} |
| 1016 |
% \end{macrocode} |
| 1017 |
% \begin{macrocode} |
| 1018 |
%</dbapp> |
| 1019 |
%<*package> |
| 1020 |
%</package> |
| 1021 |
% \end{macrocode} |
| 1022 |
% \Finale |
| 1023 |
\endinput |