| 1 |
% \iffalse |
| 2 |
% |
| 3 |
%% randomlist.dtx |
| 4 |
%% Copyleft 2013-2017 J.-C. Charpentier & C. Tellechea |
| 5 |
% |
| 6 |
%% Packages `randomlist' to use with (La)eTeX |
| 7 |
%% Copyleft (L) 2013-2017 Jean-C\^ome Charpentier & Christian Tellechea. |
| 8 |
% \fi |
| 9 |
% |
| 10 |
%% \CharacterTable |
| 11 |
%% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z |
| 12 |
%% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z |
| 13 |
%% Digits \0\1\2\3\4\5\6\7\8\9 |
| 14 |
%% Exclamation \! Double quote \" Hash (number) \# |
| 15 |
%% Dollar \$ Percent \% Ampersand \& |
| 16 |
%% Acute accent \' Left paren \( Right paren \) |
| 17 |
%% Asterisk \* Plus \+ Comma \, |
| 18 |
%% Minus \- Point \. Solidus \/ |
| 19 |
%% Colon \: Semicolon \; Less than \< |
| 20 |
%% Equals \= Greater than \> Question mark \? |
| 21 |
%% Commercial at \@ Left bracket \[ Backslash \\ |
| 22 |
%% Right bracket \] Circumflex \^ Underscore \_ |
| 23 |
%% Grave accent \` Left brace \{ Vertical bar \| |
| 24 |
%% Right brace \} Tilde \~} |
| 25 |
%% |
| 26 |
% \iffalse |
| 27 |
%<*driver> |
| 28 |
\documentclass[a4paper, 12pt]{ltxdoc} |
| 29 |
\errorcontextlines=10 |
| 30 |
|
| 31 |
\makeatletter |
| 32 |
\renewenvironment{theglossary}{% |
| 33 |
\glossary@prologue |
| 34 |
\GlossaryParms \let\item\@idxitem \ignorespaces}% |
| 35 |
{} |
| 36 |
\makeatother |
| 37 |
\GlossaryPrologue{% |
| 38 |
\newpage |
| 39 |
\section*{Change history} |
| 40 |
\markboth{{Change history}}{{Change history}}% |
| 41 |
} |
| 42 |
\DisableCrossrefs |
| 43 |
%\EnableCrossrefs |
| 44 |
\CodelineIndex |
| 45 |
\RecordChanges |
| 46 |
%\OnlyDescription |
| 47 |
\usepackage[T1]{fontenc} |
| 48 |
\usepackage[utf8]{inputenc} |
| 49 |
\usepackage{kpfonts} |
| 50 |
\usepackage[a4paper, left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry} |
| 51 |
\usepackage{array} |
| 52 |
\usepackage{fvrb-ex} |
| 53 |
\fvset{frame=lines,xrightmargin=0.5\linewidth} |
| 54 |
\usepackage{makeidx} |
| 55 |
\usepackage{randomlist} |
| 56 |
\usepackage[USenglish]{babel} |
| 57 |
\usepackage{hyperref} |
| 58 |
|
| 59 |
\pagestyle{plain} |
| 60 |
\makeindex |
| 61 |
\title{% |
| 62 |
Package \package{randomlist}\\Tools for data base, table and random |
| 63 |
writting-reading} |
| 64 |
\author{% |
| 65 |
Jean-Côme Charpentier\thanks{\ttfamily |
| 66 |
[email protected]} |
| 67 |
\and |
| 68 |
Christian Tellechea\thanks{\ttfamily [email protected]} |
| 69 |
} |
| 70 |
\newcommand*\pgm[1]{\texttt{#1}} |
| 71 |
\newcommand*\file[1]{\texttt{#1}} |
| 72 |
\newcommand*\package[1]{\texttt{#1}} |
| 73 |
\newcommand*\class[1]{\texttt{#1}} |
| 74 |
\newcommand*\option[1]{\texttt{#1}} |
| 75 |
\newcommand*\key[1]{\texttt{#1}} |
| 76 |
\newcommand*\Key[1]{\texttt{#1}\index{#1=\texttt{#1}}} |
| 77 |
\newcommand*\environ[1]{\texttt{#1}} |
| 78 |
\newcommand*\Environ[1]{\texttt{#1}\index{#1=\texttt{#1}}} |
| 79 |
\makeatletter |
| 80 |
\edef\quotechar{\string!} |
| 81 |
\edef\actualchar{\string=} |
| 82 |
\edef\verbatimchar{\string+} |
| 83 |
\def\SpecialPageIndex#1{% |
| 84 |
\immediate\write\@indexfile{% |
| 85 |
\string\indexentry{\expandafter\@gobble\string#1\actualchar |
| 86 |
\string\verb\quotechar*\verbatimchar\string#1\verbatimchar|hyperpage}% |
| 87 |
{\number\c@page}% |
| 88 |
}% |
| 89 |
} |
| 90 |
\makeatother |
| 91 |
\newcommand*\Cmd[1]{\texttt{\string#1}\SpecialPageIndex{#1}} |
| 92 |
|
| 93 |
\begin{document} |
| 94 |
\maketitle |
| 95 |
\newpage |
| 96 |
\setcounter{tocdepth}{3} |
| 97 |
\tableofcontents |
| 98 |
\newpage |
| 99 |
\section{Overview} |
| 100 |
The main aim of package \package{randomlist} is to work on list, |
| 101 |
especially with random operation. The hidden aim is to build |
| 102 |
personnal collection of exercices with different data for each |
| 103 |
pupils. In order to build such exercices, some features about |
| 104 |
databases are necessary. |
| 105 |
|
| 106 |
In ``randomlist'', the word ``List'' must be understound with two |
| 107 |
meanings: |
| 108 |
\begin{itemize} |
| 109 |
\item itemize and enumerate \LaTeX{} environments; |
| 110 |
\item list as in computer science. |
| 111 |
\end{itemize} |
| 112 |
In fact, lists as in computer are not really lists: they are |
| 113 |
arrays. Some commands allow to deal with these data structures as |
| 114 |
queues, other commands as stacks and another commands as arrays. |
| 115 |
|
| 116 |
\section{Array, queue, stack, or list?} |
| 117 |
The package give the name ``list'' to the main data structure. First, |
| 118 |
we have to declare a new list with command \Cmd{\NewList}. There is |
| 119 |
nothing special about this command. It has a mandatory argument: the |
| 120 |
name of the list. |
| 121 |
|
| 122 |
Nearly any name is possible. However, don't use hyphen and number at |
| 123 |
the end of the name. For instance \texttt{mylist-1} isn't a good idea |
| 124 |
(\texttt{mylist*1} is a good one). Don't use fragile commands and |
| 125 |
special characters. However you can use commands inside list names. |
| 126 |
|
| 127 |
\subsection{Create, erase and show list} |
| 128 |
You can't create an already existing list. For instance, the code: |
| 129 |
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth] |
| 130 |
\NewList{MyList} |
| 131 |
\NewList{MyList} |
| 132 |
\end{Verbatim} |
| 133 |
give the error message: |
| 134 |
\begin{Verbatim}[frame=none] |
| 135 |
! Package randomlist Error: List MyList already exists. |
| 136 |
\end{Verbatim} |
| 137 |
If you want erase a list, use the command \Cmd{\ClearList}. |
| 138 |
|
| 139 |
You can't create a list \key{namelist} if the macro \cmd{\namelist} |
| 140 |
exists. For instance the code: |
| 141 |
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth] |
| 142 |
\NewList{def} |
| 143 |
\end{Verbatim} |
| 144 |
give the error message: |
| 145 |
\begin{Verbatim}[frame=none] |
| 146 |
! Package randomlist Error: Command \def already exists. |
| 147 |
\end{Verbatim} |
| 148 |
|
| 149 |
\medskip |
| 150 |
As you can see, the source is between horizontal rules and flush |
| 151 |
right. The result is flush left. When the result isn't an error |
| 152 |
message, source and result are side by side. |
| 153 |
|
| 154 |
For the next commands we must be able to see the state of list. The |
| 155 |
package \package{randomlist} offers the \Cmd{\ShowList} command which |
| 156 |
allows to see the whole list. When a list is just created, it is empty, |
| 157 |
so the \cmd{\ShowList} command shows it like that (source is typeseted |
| 158 |
at the right side and result is showed at the left side):\par\medskip |
| 159 |
|
| 160 |
\begin{SideBySideExample} |
| 161 |
\NewList{MyList} |
| 162 |
\ShowList{MyList} |
| 163 |
\end{SideBySideExample} |
| 164 |
\par\medskip |
| 165 |
|
| 166 |
\subsection{Writing and reading in a list} |
| 167 |
Once a list is created, you can write values and, after that, read |
| 168 |
values. |
| 169 |
|
| 170 |
In fact, these lists can behaves like queues, stacks, or arrays |
| 171 |
according to the used command. There are four kinds of command: |
| 172 |
\begin{itemize} |
| 173 |
\item Insert; |
| 174 |
\item Extract; |
| 175 |
\item Set; |
| 176 |
\item Get. |
| 177 |
\end{itemize} |
| 178 |
\goodbreak |
| 179 |
Each one has four variants to reach some position in the list: |
| 180 |
\begin{itemize} |
| 181 |
\item First; |
| 182 |
\item Last; |
| 183 |
\item Index (without prefix); |
| 184 |
\item Random. |
| 185 |
\end{itemize} |
| 186 |
Thus we have the commands: |
| 187 |
\begin{center} |
| 188 |
\begin{tabular}{*{4}{>{\ttfamily\textbackslash}l}} |
| 189 |
InsertFirstItem & ExtractFirstItem & SetFirstItem & GetFirstItem |
| 190 |
\\ |
| 191 |
InsertLastItem & ExtractLastItem & SetLastItem & GetLastItem |
| 192 |
\\ |
| 193 |
InsertItem & ExtractItem & SetItem & GetItem |
| 194 |
\\ |
| 195 |
InsertRandomItem & ExtractRandomItem & SetRandomItem & GetRandomItem |
| 196 |
\end{tabular} |
| 197 |
\end{center} |
| 198 |
|
| 199 |
Each one has also a ``List'' variant which acts on several items. That |
| 200 |
is, we have these commands: \cmd{\InsertList}, \cmd{\ExtractList}, |
| 201 |
\cmd{\SetList}, and \cmd{\GetList}. There is also a command |
| 202 |
\Cmd{\CopyList} which is a shortcut for a special \cmd{\SetList}. |
| 203 |
|
| 204 |
Finally, we have \cmd{\ShiftList} which is somewhere quiet special: it |
| 205 |
creates empty items or destroy items by shifting inside a list. This |
| 206 |
macro is rather for internal operation but you can use it. The syntax is: |
| 207 |
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth] |
| 208 |
\ShiftList{<list>}{<start>}{<nb>} |
| 209 |
\end{Verbatim} |
| 210 |
|
| 211 |
When \texttt{<nb>} is positive, then items from index \texttt{<start>} |
| 212 |
are right shifted. That is, index $n$ becomes index $n+\mathtt{nb}$ |
| 213 |
and index from \texttt{<start>} to |
| 214 |
$\text{\texttt{<start>}}+\text{\texttt{<nb>}}-1$ are empty. |
| 215 |
|
| 216 |
When \texttt{<nb>} is negative, then items starting from the end of |
| 217 |
the list are left shifted and \texttt{<nb>} items (from index |
| 218 |
\texttt{start}) disappear. |
| 219 |
|
| 220 |
Usually, you don't need \Cmd{\ShiftList}: the other commands, |
| 221 |
especially \cmd{\ExtractList} and \Cmd{\InsertList} are enough. |
| 222 |
|
| 223 |
\subsubsection{Insert commands} |
| 224 |
\paragraph[\string\InsertFirstItem]{\cmd{\InsertFirstItem}} |
| 225 |
\Cmd{\InsertFirstItem} writes a value at the beginning of a list and |
| 226 |
shift other values to the end of the list. This command has two |
| 227 |
arguments: the list name and the value to insert. For |
| 228 |
example:\par\medskip |
| 229 |
|
| 230 |
\begin{SideBySideExample} |
| 231 |
\NewList{MyList} |
| 232 |
\InsertFirstItem{MyList}{First value} |
| 233 |
\ShowList{MyList} |
| 234 |
\InsertFirstItem{MyList}{Second value} |
| 235 |
\ShowList{MyList} |
| 236 |
\InsertFirstItem{MyList}{Third value} |
| 237 |
\ShowList{MyList} |
| 238 |
\end{SideBySideExample} |
| 239 |
\par\medskip |
| 240 |
|
| 241 |
As you can see, the list is in the reverse order than the user |
| 242 |
one. With \cmd{\InsertFirstItem}, list behaves like stack. |
| 243 |
|
| 244 |
The value written in the list can be nearly anything. For |
| 245 |
instance:\par\medskip |
| 246 |
|
| 247 |
\begin{SideBySideExample} |
| 248 |
\NewList{MyList} |
| 249 |
\InsertFirstItem{MyList}{{two}{groups}} |
| 250 |
\InsertFirstItem{MyList}{\textbf{text}} |
| 251 |
\InsertFirstItem{MyList}{special^} |
| 252 |
\InsertFirstItem{MyList}{end} |
| 253 |
\ShowList{MyList} |
| 254 |
\end{SideBySideExample} |
| 255 |
\par\medskip |
| 256 |
|
| 257 |
\paragraph[\string\InsertLastItem]{\cmd{\InsertLastItem}} |
| 258 |
\Cmd{\InsertLastItem} is like \cmd{\InsertFirstItem} but the insertion |
| 259 |
is made at the end of the list. As for the previous command, it takes |
| 260 |
two arguments: the list name and the value to insert. The previous |
| 261 |
example give:\par\medskip |
| 262 |
|
| 263 |
\begin{SideBySideExample} |
| 264 |
\NewList{MyList} |
| 265 |
\InsertLastItem{MyList}{{two}{groups}} |
| 266 |
\InsertLastItem{MyList}{\textbf{text}} |
| 267 |
\InsertLastItem{MyList}{special^} |
| 268 |
\InsertLastItem{MyList}{end} |
| 269 |
\ShowList{MyList} |
| 270 |
\end{SideBySideExample} |
| 271 |
\par\medskip |
| 272 |
|
| 273 |
With this command, the list behaves as queue. |
| 274 |
|
| 275 |
\paragraph[\string\InsertItem]{\cmd{\InsertItem}} |
| 276 |
\Cmd{\InsertItem} is like \cmd{\InsertFirstItem} but the insertion |
| 277 |
is made to a specified position of the list. With this command, the list |
| 278 |
behaves as an array. |
| 279 |
|
| 280 |
The index starts from zero and if the list has $n$ elements then the |
| 281 |
index can't be greater than $n$. When a value is inserted in position |
| 282 |
$k$, then the previous values from $k$ to $n-1$ are shifted one |
| 283 |
position to the right. |
| 284 |
|
| 285 |
\cmd{\InsertItem} takes three arguments: the list name, the position |
| 286 |
and the value. Here is an example:\par\medskip |
| 287 |
|
| 288 |
\begin{SideBySideExample} |
| 289 |
\NewList{MyList} |
| 290 |
\InsertLastItem{MyList}{first} |
| 291 |
\InsertLastItem{MyList}{\textbf{second}} |
| 292 |
\InsertLastItem{MyList}{special^} |
| 293 |
\InsertLastItem{MyList}{end} |
| 294 |
\ShowList{MyList} |
| 295 |
\InsertItem{MyList}{2}{\textbf{insert!}} |
| 296 |
\InsertItem{MyList}{2}{\textbf{other!}} |
| 297 |
\InsertItem{MyList}{6}{real end} |
| 298 |
\ShowList{MyList} |
| 299 |
\end{SideBySideExample} |
| 300 |
\par\medskip |
| 301 |
|
| 302 |
As you can see, when the two values is inserted at position~2, the |
| 303 |
value \texttt{special\textasciicircum} is shifted from position~2 to |
| 304 |
position~4. Moreover, it's possible to insert a value to the |
| 305 |
nonexistent position $n$ when the length of the list is $n$: it's the |
| 306 |
only one possibility to specify a nonexistent index. |
| 307 |
|
| 308 |
\paragraph[\string\InsertRandfomItem]{\cmd{\InsertRandomItem}} |
| 309 |
This is the first command which use random numbers. We will see later |
| 310 |
how to manage random numbers themselves. |
| 311 |
|
| 312 |
\Cmd{\InsertRandomItem} command works as the \cmd{\InsertItem} one |
| 313 |
but the position is selected randomly by \TeX{}. Then there is only two |
| 314 |
arguments: the list name and the value to insert. Here is an example: |
| 315 |
\RLsetrandomseed 1\par\medskip |
| 316 |
|
| 317 |
\begin{SideBySideExample} |
| 318 |
\NewList{MyList} |
| 319 |
\InsertRandomItem{MyList}{first} |
| 320 |
\InsertRandomItem{MyList}{second} |
| 321 |
\InsertRandomItem{MyList}{third} |
| 322 |
\InsertRandomItem{MyList}{fourth} |
| 323 |
\InsertRandomItem{MyList}{fifth} |
| 324 |
\ShowList{MyList} |
| 325 |
\end{SideBySideExample} |
| 326 |
\par\medskip |
| 327 |
|
| 328 |
\paragraph[\string\InsertList]{\cmd{\InsertList}} |
| 329 |
\Cmd{\InsertList} allows to do in one shot what the \cmd{\InsertItem} |
| 330 |
can do in several ones. The principle of this command is to insert all |
| 331 |
the items of a list inside a second one. You have to give three |
| 332 |
arguments: the list which receive, the index to start insertion, and |
| 333 |
the list to insert. For instance:\par\medskip |
| 334 |
|
| 335 |
\begin{SideBySideExample} |
| 336 |
\NewList{MyList} |
| 337 |
\NewList{OtherList} |
| 338 |
\InsertLastItem{MyList}{first in M} |
| 339 |
\InsertLastItem{MyList}{fourth in M} |
| 340 |
\InsertLastItem{MyList}{fifth in M} |
| 341 |
\InsertLastItem{OtherList}{second in O} |
| 342 |
\InsertLastItem{OtherList}{third in O} |
| 343 |
\InsertList{MyList}{1}{OtherList} |
| 344 |
\ShowList{MyList} |
| 345 |
\ShowList{OtherList} |
| 346 |
\end{SideBySideExample} |
| 347 |
\par\medskip |
| 348 |
|
| 349 |
As you can see, the second list is unchanged after operation. |
| 350 |
|
| 351 |
Package \package{randomlist} checks that both lists exists and that |
| 352 |
index is compatible with list. Otherwise an error message will be |
| 353 |
raised. |
| 354 |
|
| 355 |
It's possible to insert an empty list:\par\medskip |
| 356 |
|
| 357 |
\begin{SideBySideExample} |
| 358 |
\NewList{MyList} |
| 359 |
\NewList{OtherList} |
| 360 |
\InsertLastItem{MyList}{first} |
| 361 |
\InsertLastItem{MyList}{second} |
| 362 |
\InsertLastItem{MyList}{third} |
| 363 |
\InsertList{MyList}{1}{OtherList} |
| 364 |
\ShowList{MyList} |
| 365 |
\end{SideBySideExample} |
| 366 |
\par\medskip |
| 367 |
|
| 368 |
\subsubsection{Extract commands} |
| 369 |
\paragraph[\string\ExtractFirstItem]{\cmd{\ExtractFirstItem}} |
| 370 |
The four commands \cmd{\Extract...Item} are the inverse one of the |
| 371 |
four commands \cmd{\Insert...Item}. |
| 372 |
|
| 373 |
\Cmd{\ExtractFirstItem} extract the first value of a list and store it |
| 374 |
in a macro. The other elements of the list are shifted left (the list |
| 375 |
length decreases by one). This command takes two argument: the list |
| 376 |
name and the macro name where the value is stored. The last argument |
| 377 |
is just the name of the macro, \emph{i.e.} the macro name without the |
| 378 |
backslash. For example:\par\medskip |
| 379 |
|
| 380 |
\begin{SideBySideExample} |
| 381 |
\NewList{MyList} |
| 382 |
\InsertLastItem{MyList}{\TeX} |
| 383 |
\InsertLastItem{MyList}{is} |
| 384 |
\InsertLastItem{MyList}{very} |
| 385 |
\InsertLastItem{MyList}{powerful} |
| 386 |
\ExtractFirstItem{MyList}{MyMacro} |
| 387 |
The first element was ``\MyMacro''. |
| 388 |
|
| 389 |
\ShowList{MyList} |
| 390 |
\end{SideBySideExample} |
| 391 |
\par\medskip |
| 392 |
|
| 393 |
When you extract an element from a list, the list length decreases by |
| 394 |
one. It explains why it's forbidden to extract an element from an |
| 395 |
empty list. If you try it, |
| 396 |
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth] |
| 397 |
\NewList{MyList} |
| 398 |
\ExtractFirstItem{MyList}{MyMacro} |
| 399 |
\end{Verbatim} |
| 400 |
you have the error message: |
| 401 |
\begin{Verbatim}[frame=none] |
| 402 |
! Package randomlist Error: List MyList is empty. |
| 403 |
\end{Verbatim} |
| 404 |
|
| 405 |
\paragraph[\string\ExtractLastItem]{\cmd{\ExtractLastItem}} |
| 406 |
\Cmd{\ExtractLastItem} behaves like \cmd{\ExtractFirstItem} but the |
| 407 |
element extracted is the last one. Thus there is no shifting, there is |
| 408 |
just a decrementation of the list length. |
| 409 |
|
| 410 |
Here is an example:\par\medskip |
| 411 |
|
| 412 |
\begin{SideBySideExample} |
| 413 |
\NewList{MyList} |
| 414 |
\InsertLastItem{MyList}{\TeX} |
| 415 |
\InsertLastItem{MyList}{is} |
| 416 |
\InsertLastItem{MyList}{very} |
| 417 |
\InsertLastItem{MyList}{powerful} |
| 418 |
\ExtractLastItem{MyList}{MyMacro} |
| 419 |
The last element was ``\MyMacro''. |
| 420 |
|
| 421 |
\ShowList{MyList} |
| 422 |
\end{SideBySideExample} |
| 423 |
\par\medskip |
| 424 |
|
| 425 |
\paragraph[\string\ExtractItem]{\cmd{\ExtractItem}} |
| 426 |
\Cmd{\ExtractItem} behaves like \cmd{\ExtractFirstItem} but the |
| 427 |
element extracted is the one indicated by its index. The command takes |
| 428 |
three argument: the list name, the index of element to extract, the |
| 429 |
macro used to store the element extracted. Don't forget that indexes |
| 430 |
start from zero. Here is an example:\par\medskip |
| 431 |
|
| 432 |
\begin{SideBySideExample} |
| 433 |
\NewList{MyList} |
| 434 |
\InsertLastItem{MyList}{\TeX} |
| 435 |
\InsertLastItem{MyList}{is} |
| 436 |
\InsertLastItem{MyList}{very} |
| 437 |
\InsertLastItem{MyList}{powerful} |
| 438 |
\ExtractItem{MyList}{2}{MyMacro} |
| 439 |
The third element was ``\MyMacro''. |
| 440 |
|
| 441 |
\ShowList{MyList} |
| 442 |
\end{SideBySideExample} |
| 443 |
\par\medskip |
| 444 |
|
| 445 |
There isn't anything special. The length of the list decreases by one |
| 446 |
and elements are shifted accordingly to the extracted one. |
| 447 |
|
| 448 |
\paragraph[\string\ExtractRandomItem]{\cmd{\ExtractRandomItem}} |
| 449 |
\Cmd{\ExtractRandomItem} works like the previous |
| 450 |
\cmd{\ExtractItem}. Here, the index is selected randomly by the |
| 451 |
computer. Then there are only two arguments: the list name and the |
| 452 |
macro to store the extracted element:\par\medskip |
| 453 |
|
| 454 |
\RLsetrandomseed 3 |
| 455 |
\begin{SideBySideExample} |
| 456 |
\NewList{MyList} |
| 457 |
\InsertLastItem{MyList}{\TeX} |
| 458 |
\InsertLastItem{MyList}{is} |
| 459 |
\InsertLastItem{MyList}{very} |
| 460 |
\InsertLastItem{MyList}{powerful} |
| 461 |
\ExtractRandomItem{MyList}{MyMacro} |
| 462 |
``\MyMacro'' was extracted. |
| 463 |
|
| 464 |
\ShowList{MyList} |
| 465 |
\end{SideBySideExample} |
| 466 |
\par\medskip |
| 467 |
|
| 468 |
Even the extraction is made on a random index, it's forbidden to |
| 469 |
extract something from an empty list. Then, the code: |
| 470 |
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth] |
| 471 |
\NewList{MyList} |
| 472 |
\ExtractRandomItem{MyList}{MyMacro} |
| 473 |
\end{Verbatim} |
| 474 |
gives the usual error message: |
| 475 |
\begin{Verbatim}[frame=none] |
| 476 |
! Package randomlist Error: List MyList is empty. |
| 477 |
\end{Verbatim} |
| 478 |
|
| 479 |
\paragraph[\string\ExtractList]{\cmd{\ExtractList}} |
| 480 |
The commands \cmd{\Extract...Item} extract one item and store it in a |
| 481 |
macro. With the command \Cmd{\ExtractList} we can extract several |
| 482 |
items and put them in a list. \cmd{\ExtractList} asks for four |
| 483 |
arguments: |
| 484 |
\begin{enumerate} |
| 485 |
\item the main list; |
| 486 |
\item the starting index; |
| 487 |
\item the ending index; |
| 488 |
\item the list which receive extracted values. |
| 489 |
\end{enumerate} |
| 490 |
Here is an example:\par\medskip |
| 491 |
|
| 492 |
\begin{SideBySideExample} |
| 493 |
\NewList{MyList} |
| 494 |
\NewList{OtherList} |
| 495 |
\InsertLastItem{MyList}{first} |
| 496 |
\InsertLastItem{MyList}{second} |
| 497 |
\InsertLastItem{MyList}{third} |
| 498 |
\InsertLastItem{MyList}{fourth} |
| 499 |
\InsertLastItem{MyList}{fifth} |
| 500 |
\InsertLastItem{MyList}{sixth} |
| 501 |
\ExtractList{MyList}{2}{4}{OtherList} |
| 502 |
|
| 503 |
\ShowList{MyList} |
| 504 |
\ShowList{OtherList} |
| 505 |
\end{SideBySideExample} |
| 506 |
\par\medskip |
| 507 |
|
| 508 |
Obviously, \package{randomlist} checks list and indexes. You can have |
| 509 |
the start index and the last index equals. In this case, |
| 510 |
\cmd{\ExtractList} behaves like \cmd{\ExtractItem} but the extracted |
| 511 |
value is put in a list rather than in a macro:\par\medskip |
| 512 |
|
| 513 |
\begin{SideBySideExample} |
| 514 |
\NewList{MyList} |
| 515 |
\NewList{OtherList} |
| 516 |
\InsertLastItem{MyList}{first} |
| 517 |
\InsertLastItem{MyList}{second} |
| 518 |
\InsertLastItem{MyList}{third} |
| 519 |
\InsertLastItem{MyList}{fourth} |
| 520 |
\InsertLastItem{MyList}{fifth} |
| 521 |
\InsertLastItem{MyList}{sixth} |
| 522 |
\ExtractList{MyList}{2}{2}{OtherList} |
| 523 |
|
| 524 |
\ShowList{MyList} |
| 525 |
\ShowList{OtherList} |
| 526 |
\end{SideBySideExample} |
| 527 |
\par\medskip |
| 528 |
|
| 529 |
\subsubsection{Set commands} |
| 530 |
\paragraph[\string\SetFirstItem]{\cmd{\SetFirstItem}} |
| 531 |
The commands \cmd{\Set...Item} modify the existing values of |
| 532 |
list. \Cmd{\SetFirstItem} modify the first value.\par\medskip |
| 533 |
|
| 534 |
\begin{SideBySideExample} |
| 535 |
\NewList{MyList} |
| 536 |
\InsertLastItem{MyList}{\TeX} |
| 537 |
\InsertLastItem{MyList}{is} |
| 538 |
\InsertLastItem{MyList}{very} |
| 539 |
\InsertLastItem{MyList}{powerful} |
| 540 |
\SetFirstItem{MyList}{\LaTeX} |
| 541 |
\ShowList{MyList} |
| 542 |
\end{SideBySideExample} |
| 543 |
\par\medskip |
| 544 |
|
| 545 |
If a list is empty, there is the classic error message about empty |
| 546 |
list. |
| 547 |
|
| 548 |
\paragraph[\string\SetLastItem]{\cmd{\SetLastItem}} |
| 549 |
\Cmd{\SetLastItem} acts like \cmd{\SetFirstItem} but at the end of the |
| 550 |
list. |
| 551 |
|
| 552 |
\paragraph[\string\SetItem]{\cmd{\SetItem}} |
| 553 |
\Cmd{\SetItem} acts like the previous commands. It takes three |
| 554 |
arguments: the list name, the index, the new value:\par\medskip |
| 555 |
|
| 556 |
\begin{SideBySideExample} |
| 557 |
\NewList{MyList} |
| 558 |
\InsertLastItem{MyList}{\TeX} |
| 559 |
\InsertLastItem{MyList}{is} |
| 560 |
\InsertLastItem{MyList}{very} |
| 561 |
\InsertLastItem{MyList}{powerful} |
| 562 |
\SetItem{MyList}{2}{quiet} |
| 563 |
\ShowList{MyList} |
| 564 |
\end{SideBySideExample} |
| 565 |
\par\medskip |
| 566 |
|
| 567 |
If the index doesn't exist, an error message is showed. Code: |
| 568 |
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth] |
| 569 |
\NewList{MyList} |
| 570 |
\InsertLastItem{MyList}{\TeX} |
| 571 |
\InsertLastItem{MyList}{is} |
| 572 |
\InsertLastItem{MyList}{very} |
| 573 |
\InsertLastItem{MyList}{powerful} |
| 574 |
\SetItem{MyList}{4}{isn't it?} |
| 575 |
\end{Verbatim} |
| 576 |
gives the error message: |
| 577 |
\begin{Verbatim}[frame=none] |
| 578 |
! Package randomlist Error: Index 4 is greater than last index of list MyList. |
| 579 |
\end{Verbatim} |
| 580 |
|
| 581 |
\paragraph[\string\SetRandomItem]{\cmd{\SetRandomItem}} |
| 582 |
\Cmd{\SetRandomItem} acts like the previous one but the index is |
| 583 |
selected randomly. The list must be non empty. Here is an |
| 584 |
example:\par\medskip |
| 585 |
|
| 586 |
\begin{SideBySideExample} |
| 587 |
\NewList{MyList} |
| 588 |
\InsertLastItem{MyList}{\TeX} |
| 589 |
\InsertLastItem{MyList}{is} |
| 590 |
\InsertLastItem{MyList}{really} |
| 591 |
\InsertLastItem{MyList}{very} |
| 592 |
\InsertLastItem{MyList}{powerful} |
| 593 |
\SetRandomItem{MyList}{snap!} |
| 594 |
\ShowList{MyList} |
| 595 |
\end{SideBySideExample} |
| 596 |
\par\medskip |
| 597 |
|
| 598 |
\paragraph[\string\SetList]{\cmd{\SetList}} |
| 599 |
Insert value one by one inside a list could be tiresome especially if |
| 600 |
you have many values. Package \package{randomlist} allows to insert |
| 601 |
many items in a row using the macro \Cmd{\SetList}. Items are |
| 602 |
separated with comma. For instance:\par\medskip |
| 603 |
|
| 604 |
\begin{SideBySideExample} |
| 605 |
\NewList{MyList} |
| 606 |
\SetList{MyList}{\TeX, is, very, |
| 607 |
powerful} |
| 608 |
\ShowList{MyList} |
| 609 |
\end{SideBySideExample} |
| 610 |
\par\medskip |
| 611 |
|
| 612 |
As you can see, spaces aren't discarded. A more satisfactory |
| 613 |
presentation would be:\par\medskip |
| 614 |
|
| 615 |
\begin{SideBySideExample} |
| 616 |
\NewList{MyList} |
| 617 |
\SetList{MyList}{\TeX,is,very,% |
| 618 |
powerful} |
| 619 |
\ShowList{MyList} |
| 620 |
\end{SideBySideExample} |
| 621 |
\par\medskip |
| 622 |
|
| 623 |
\paragraph[\string\CopyList]{\cmd{\CopyList}} |
| 624 |
Copy a list in another one. Both lists must exists: this command don't |
| 625 |
create list since only \cmd{\NewList} can do that. Here is an |
| 626 |
example:\par\medskip |
| 627 |
|
| 628 |
\begin{SideBySideExample} |
| 629 |
\NewList{MyList} |
| 630 |
\NewList{OtherList} |
| 631 |
\SetList{MyList}{\TeX,is,very,% |
| 632 |
powerful} |
| 633 |
\CopyList{MyList}{OtherList} |
| 634 |
\ShowList{OtherList} |
| 635 |
\end{SideBySideExample} |
| 636 |
\par\medskip |
| 637 |
|
| 638 |
\subsubsection{Get commands} |
| 639 |
\paragraph[\string\GetFirstItem]{\cmd{\GetFirstItem}} |
| 640 |
The \cmd{\get...list} look for a value in a list. They don't change |
| 641 |
the list. The index must exist elsewhere an error message will be |
| 642 |
show. That is, for first, last, and random variant, list must be non |
| 643 |
empty. |
| 644 |
|
| 645 |
\Cmd{\GetFirstItem} put the first value of a list into a macro. The |
| 646 |
arguments of the command are: the list name, the macro:\par\medskip |
| 647 |
|
| 648 |
\begin{SideBySideExample} |
| 649 |
\NewList{MyList} |
| 650 |
\SetList{MyList}{\TeX,is,so,cute} |
| 651 |
\GetFirstItem{MyList}{MyMacro} |
| 652 |
The first element is ``\MyMacro'' |
| 653 |
|
| 654 |
\ShowList{MyList} |
| 655 |
\end{SideBySideExample} |
| 656 |
\par\medskip |
| 657 |
|
| 658 |
\paragraph[\string\GetLastItem]{\cmd{\GetLastItem}} |
| 659 |
\Cmd{\GetLastItem} acts like \cmd{\GetFirstItem} but give the last value. |
| 660 |
|
| 661 |
\paragraph[\string\GetItem]{\cmd{\GetItem}} |
| 662 |
\Cmd{\GetItem} acts like the previous one but give the value of the |
| 663 |
element $k$ where $k$ is the second argument. Pay attention that |
| 664 |
indexes start from zero. Then the index $k$ maps to the $k+1$st |
| 665 |
element of the list.\par\medskip |
| 666 |
|
| 667 |
\begin{SideBySideExample} |
| 668 |
\NewList{MyList} |
| 669 |
\SetList{MyList}{\TeX,is,so,cute} |
| 670 |
\GetItem{MyList}{2}{MyMacro} |
| 671 |
The third element is ``\MyMacro'' |
| 672 |
|
| 673 |
\ShowList{MyList} |
| 674 |
\end{SideBySideExample} |
| 675 |
\par\medskip |
| 676 |
|
| 677 |
Package \package{randomlist} offers an other syntax to access to an |
| 678 |
item: \cmd{\<namelist>[<index>]}. Thus, we can write the previous |
| 679 |
example like that:\par\medskip |
| 680 |
|
| 681 |
\begin{SideBySideExample} |
| 682 |
\NewList{MyList} |
| 683 |
\SetList{MyList}{\TeX,is,so,cute} |
| 684 |
The third element is ``\MyList[2]'' |
| 685 |
\end{SideBySideExample} |
| 686 |
\par\medskip |
| 687 |
|
| 688 |
\paragraph[\string\GetRandomItem]{\cmd{\GetRandomItem}} |
| 689 |
\Cmd{\GetRandomItem} give the value of a randomly selected element of a |
| 690 |
list.\par\medskip |
| 691 |
|
| 692 |
\begin{SideBySideExample} |
| 693 |
\NewList{MyList} |
| 694 |
\SetList{MyList}{\TeX,is,so,cute} |
| 695 |
\GetRandomItem{MyList}{MyMacro} |
| 696 |
The random element is ``\MyMacro'' |
| 697 |
|
| 698 |
\ShowList{MyList} |
| 699 |
\end{SideBySideExample} |
| 700 |
\par\medskip |
| 701 |
|
| 702 |
\paragraph[\string\GetList]{\cmd{\GetList}} |
| 703 |
\Cmd{\GetList} builds a sub-list. Arguments are those of |
| 704 |
\cmd{\ExtractList}, that is, the read list, the first index, the last |
| 705 |
index, and the written list.\par\medskip |
| 706 |
|
| 707 |
\begin{SideBySideExample} |
| 708 |
\NewList{MyList} |
| 709 |
\NewList{OtherList} |
| 710 |
\SetList{MyList}{X1,X2,X3,X4,X5,X6} |
| 711 |
\GetList{MyList}{2}{4}{OtherList} |
| 712 |
\ShowList{MyList} |
| 713 |
\ShowList{OtherList} |
| 714 |
\end{SideBySideExample} |
| 715 |
\par\medskip |
| 716 |
|
| 717 |
Contrary to what \cmd{\ExtractItem} do, \cmd{\GetList} don't modify |
| 718 |
the source list. |
| 719 |
|
| 720 |
\section{Database} |
| 721 |
\subsection{Simple database} |
| 722 |
Package \package{randomlist} offers some features about databases. In |
| 723 |
fact that was the first aim of this package: to be able to product one |
| 724 |
assignment for one pupil (with all assignments different). |
| 725 |
|
| 726 |
For \package{randomlist} a database is a list. For instance the next |
| 727 |
example shows an usual list which is used as a database. We'll see |
| 728 |
later real databases with records and fields. For now, our |
| 729 |
database has records and each record has one single field: the name |
| 730 |
and first name of our pupils. In order to parse all entries of |
| 731 |
database \package{randomlist} offers the commands |
| 732 |
\cmd{\ForEach...Item}. These command extract one by one all the |
| 733 |
elements of a list and typeset, for each element, its third |
| 734 |
argument. Second argument give the macro name where element is stored. |
| 735 |
For these commands, the macro name is given without the |
| 736 |
backslash. Depending how the extraction is made, we have the three |
| 737 |
commands: \Cmd{\ForEachFirstItem}, \cmd{\ForEachLastItem}, and |
| 738 |
\cmd{\ForEachRandomItem}. In fact, the readind is made with an |
| 739 |
extraction but, as the work is made in a group, after the |
| 740 |
\cmd{\ForEach...} command, the list is restored. |
| 741 |
|
| 742 |
\subsubsection[\protect\texttt{\protect\textbackslash ForEachFirstItem}]{\cmd{\ForEachFirstItem}} |
| 743 |
|
| 744 |
\begin{SideBySideExample} |
| 745 |
\NewList{Pupils} |
| 746 |
\SetList{Pupils}{Alfred Aho,% |
| 747 |
Charles Babbage,Gregory Chaintin,% |
| 748 |
Edsger Dijkstra} |
| 749 |
\ForEachFirstItem{Pupils}{Name}{% |
| 750 |
Test for \Name\par |
| 751 |
blah blah blah\dots\par\smallskip |
| 752 |
} |
| 753 |
\end{SideBySideExample} |
| 754 |
\par\medskip |
| 755 |
|
| 756 |
\subsubsection[\protect\texttt{\protect\textbackslash ForEachLastItem}]{\cmd{\ForEachLastItem}} |
| 757 |
\Cmd{\ForEachLastItem} acts like \cmd{\ForEachFirstItem} but the |
| 758 |
reading is made in reverse order:\par\medskip |
| 759 |
|
| 760 |
\begin{SideBySideExample} |
| 761 |
\NewList{Pupils} |
| 762 |
\SetList{Pupils}{Alfred Aho,% |
| 763 |
Charles Babbage,Gregory Chaintin,% |
| 764 |
Edsger Dijkstra} |
| 765 |
\ForEachLastItem{Pupils}{Name}{% |
| 766 |
Test for \Name\par |
| 767 |
blah blah blah\dots\par\smallskip |
| 768 |
} |
| 769 |
\end{SideBySideExample} |
| 770 |
\par\medskip |
| 771 |
|
| 772 |
\subsubsection[\protect\texttt{\protect\textbackslash ForEachRandomItem}]{\cmd{\ForEachRandomItem}} |
| 773 |
\Cmd{\ForEachRandomItem} acts like the previous commands but the |
| 774 |
reading is made randomly. In the next example, we can see that the |
| 775 |
list is restored after the command |
| 776 |
\cmd{\ForEachRandomItem}:\par\medskip |
| 777 |
|
| 778 |
\begin{SideBySideExample} |
| 779 |
\NewList{Pupils} |
| 780 |
\SetList{Pupils}{Alfred Aho,% |
| 781 |
Charles Babbage,Gregory Chaintin,% |
| 782 |
Edsger Dijkstra} |
| 783 |
\ForEachRandomItem{Pupils}{Name}{% |
| 784 |
Test for \Name\par |
| 785 |
blah blah blah\dots\par\smallskip |
| 786 |
} |
| 787 |
\ShowList{Pupils} |
| 788 |
\end{SideBySideExample} |
| 789 |
\par\medskip |
| 790 |
|
| 791 |
You can put a command \cmd{\ForEach} inside another one. There is no |
| 792 |
limits (but the stacks of \TeX{}):\par\medskip |
| 793 |
|
| 794 |
\begin{SideBySideExample} |
| 795 |
\NewList{L-Man} |
| 796 |
\NewList{L-Woman} |
| 797 |
\SetList{L-Man}{Alfred,Charles,Gregory} |
| 798 |
\SetList{L-Woman}{Ada,Grace,Adele} |
| 799 |
\ForEachRandomItem{L-Man}{Man}{% |
| 800 |
\ForEachRandomItem{L-Woman}{Woman}{% |
| 801 |
\Man{} and \Woman{} are computer |
| 802 |
scientists\par |
| 803 |
} |
| 804 |
} |
| 805 |
\end{SideBySideExample} |
| 806 |
\par\medskip |
| 807 |
|
| 808 |
Actually, there is a bug that don't allow fragile commands inside |
| 809 |
lists when they are read with \cmd{\ForEach...Item} commands. I hope |
| 810 |
that the next version of \package{randomlist} will fix this! |
| 811 |
|
| 812 |
\subsection{Database with fields} |
| 813 |
Each record of a database is read as a set of fields. In fact it's a |
| 814 |
sequence of groups. \package{randomlist} allow to read each field with |
| 815 |
the macro \Cmd{\ReadFieldItem}. In order to make life easy, |
| 816 |
\package{randomlist} allow to read whole database from files with the |
| 817 |
command \Cmd{\ReadFileList}. |
| 818 |
|
| 819 |
This command read a field in a record and store it in a macro. It |
| 820 |
takes three arguments: a whole record or a macro containing the whole |
| 821 |
record, the rank of the field (starting zero), and a macro to store |
| 822 |
the value of this field. For instance:\par\medskip |
| 823 |
|
| 824 |
\begin{SideBySideExample} |
| 825 |
\def\record{{ein}{un}{one}} |
| 826 |
\ReadFieldItem{\record}{0}{Zahl} |
| 827 |
\ReadFieldItem{\record}{1}{Nombre} |
| 828 |
\ReadFieldItem{\record}{2}{Number} |
| 829 |
\Nombre\ French, \Zahl\ German, |
| 830 |
and \Number\ English. |
| 831 |
\end{SideBySideExample} |
| 832 |
\par\medskip |
| 833 |
|
| 834 |
If there are less fields than the indicating rank then an |
| 835 |
error message is raised: |
| 836 |
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth] |
| 837 |
\def\record{{ein}{un}{one}} |
| 838 |
\ReadFieldItem{\record}{3}{Stuff} |
| 839 |
\end{Verbatim} |
| 840 |
give the error message: |
| 841 |
\begin{Verbatim}[frame=none] |
| 842 |
! Package randomlist Error: There aren't enough fields in the record. |
| 843 |
\end{Verbatim} |
| 844 |
Remember that fields are numbered starting from zero! |
| 845 |
|
| 846 |
Obviously, the power of \Cmd{\ReadFieldItem} comes with list and real |
| 847 |
database. For instance:\par\medskip |
| 848 |
|
| 849 |
\begin{SideBySideExample} |
| 850 |
\NewList{Languages} |
| 851 |
\SetList{Languages}{{France}{un},% |
| 852 |
{Germany}{ein},{England}{one}} |
| 853 |
\ForEachFirstItem{Languages}{Unit}{% |
| 854 |
\ReadFieldItem{\Unit}{0}{Country}% |
| 855 |
\ReadFieldItem{\Unit}{1}{Number}% |
| 856 |
You say ``\Number'' in \Country.\par |
| 857 |
} |
| 858 |
\end{SideBySideExample} |
| 859 |
\par\medskip |
| 860 |
|
| 861 |
It's not very handy to write a whole database inside a \LaTeX{} |
| 862 |
source. \package{randomlist} allows to load a data base reading a |
| 863 |
extern file. For that, there is the command \Cmd{\ReadFileList}. This |
| 864 |
command takes two mandatory arguments: the name of the database and |
| 865 |
the name of the file. |
| 866 |
|
| 867 |
The file \texttt{pythagoras.dat} (page~\pageref{file-pythagoras}) |
| 868 |
shows 100~lines of three numbers separated by comma. When this file is |
| 869 |
read, the data base contains 100~records with three fields. That is, |
| 870 |
by default, \package{randomlist} read CSV files (Comma Separated |
| 871 |
Values).\par\medskip |
| 872 |
|
| 873 |
\begin{SideBySideExample} |
| 874 |
\NewList{Pyth} |
| 875 |
\ReadFileList{Pyth}{pythagoras.dat} |
| 876 |
\GetItem{Pyth}{10}{triple} |
| 877 |
\ReadFieldItem{\triple}{0}{triplea} |
| 878 |
\ReadFieldItem{\triple}{1}{tripleb} |
| 879 |
Do you know that |
| 880 |
$\sqrt{\triplea^2+\tripleb^2}$ is an |
| 881 |
integer? |
| 882 |
\end{SideBySideExample} |
| 883 |
\par\medskip |
| 884 |
|
| 885 |
\NewList{Pyth} |
| 886 |
\ReadFileList{Pyth}{pythagoras.dat} |
| 887 |
\GetItem{Pyth}{10}{triple} |
| 888 |
\ReadFieldItem{\triple}{0}{triplea} |
| 889 |
\ReadFieldItem{\triple}{1}{tripleb} |
| 890 |
\ReadFieldItem{\triple}{2}{triplec} |
| 891 |
To those who check the triple page~\pageref{file-pythagoras}, don't |
| 892 |
forget that the 10th rank maps with line number~11, that is, |
| 893 |
``\texttt{\triplea,\tripleb,\triplec}''. Moreover, you have the result |
| 894 |
to the operation : $\sqrt{\triplea^2+\tripleb^2}=\triplec$. |
| 895 |
|
| 896 |
A file could have any structure. In particular, it could have one or |
| 897 |
several title lines. It's the case for the file \texttt{pupils.dat} |
| 898 |
(page~\pageref{file-pupils}) where the first line is obviously a title |
| 899 |
line. You have just to extract this or these lines to obtain a |
| 900 |
``classical'' database.\par\medskip |
| 901 |
|
| 902 |
\begin{SideBySideExample} |
| 903 |
\NewList{Class} |
| 904 |
\ReadFileList{Class}{pupils.dat} |
| 905 |
\ExtractFirstItem{Class}{NULL} |
| 906 |
\GetRandomItem{Class}{pupil} |
| 907 |
\ReadFieldItem{\pupil}{0}{Name} |
| 908 |
\ReadFieldItem{\pupil}{1}{FName} |
| 909 |
\ReadFieldItem{\pupil}{2}{Note} |
| 910 |
Your child \Name{} \FName{} has |
| 911 |
\Note{} this term. This is |
| 912 |
\if A\Note very \fi |
| 913 |
\if C\Note not \fi |
| 914 |
good. |
| 915 |
\end{SideBySideExample} |
| 916 |
\par\medskip\noindent |
| 917 |
Processing this way, you have got a database with real datas (no title |
| 918 |
data). |
| 919 |
|
| 920 |
The file could be in another format than CSV. In fact, you can define |
| 921 |
a field separator (comma by default) and a string delimiter (double |
| 922 |
quote by default) which allow to put a field separator inside a |
| 923 |
field. |
| 924 |
%The syntax isn't very rich. Unlike real CSV file, there isn't |
| 925 |
%the syntax \texttt{""} to obtain a single \texttt{"} (or with an other |
| 926 |
%field separator). |
| 927 |
To indicate other symbols than comma and double |
| 928 |
quote, the command \cmd{\ReadFileList} accept an optional argument |
| 929 |
which declare the field separator and the string encloser by two |
| 930 |
characters. |
| 931 |
|
| 932 |
For instance, the file \texttt{comets.dat} (see page~ |
| 933 |
\pageref{file-comets}) has ``\texttt{\string|}'' as field |
| 934 |
separator. Therefore, the calling syntax becomes:\par\medskip |
| 935 |
|
| 936 |
\begingroup |
| 937 |
\catcode`\|=12 |
| 938 |
\begin{SideBySideExample} |
| 939 |
\NewList{Comets} |
| 940 |
\ReadFileList[|"]{Comets}{comets.dat} |
| 941 |
\ExtractFirstItem{Comets}{NULL} |
| 942 |
\ExtractFirstItem{Comets}{NULL} |
| 943 |
\GetRandomItem{Comets}{comet} |
| 944 |
\ReadFieldItem{\comet}{1}{Name} |
| 945 |
\ReadFieldItem{\comet}{2}{Discover} |
| 946 |
\ReadFieldItem{\comet}{3}{Year} |
| 947 |
\ReadFieldItem{\comet}{4}{Period} |
| 948 |
The comet \Name{} was discovered by |
| 949 |
\Discover{} in \Year{}. |
| 950 |
\unless\ifx\Period\empty |
| 951 |
Its period is \Period{} years. |
| 952 |
\fi |
| 953 |
\end{SideBySideExample} |
| 954 |
\endgroup |
| 955 |
\par\medskip |
| 956 |
|
| 957 |
Observe that we have two ``title lines'' to discard. As each line |
| 958 |
begins by a field separator, the first field of each record is |
| 959 |
empty. Thus we extract field starting one (not zero). We test if a |
| 960 |
period is empty because of 18D/Perrine-Mrkos comet. |
| 961 |
|
| 962 |
\subsection{Tricks, things, and other matters} |
| 963 |
\subsubsection{Random number} |
| 964 |
In the package \package{randomlist}, the (pseudo) random numbers are |
| 965 |
processed by the macro \Cmd{\RLuniformdeviate}\texttt{\{<n>\}\{<macro>\}} |
| 966 |
(choose a random integer number between 0 and $\text{\key{n}}-1$ and |
| 967 |
store it in |\<macro>|) and \Cmd{\RLsetrandomseed} (set the seed). |
| 968 |
|
| 969 |
When you say nothing, the seed is calculated with the current date |
| 970 |
(year, month, day, hour and minute). That is, if you run |
| 971 |
\texttt{latex} twice with a delay greater than one minute, you will |
| 972 |
have two different results. Sometime, it's what you want, sometime |
| 973 |
it's annoying. |
| 974 |
|
| 975 |
Under \LaTeX{}, you can set the seed with the package option |
| 976 |
\texttt{seed} with the syntax: |
| 977 |
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth] |
| 978 |
\usepackage[seed=<value>]{randomlist} |
| 979 |
\end{Verbatim} |
| 980 |
where \texttt{<value>} is an integer value. If \texttt{<value>} is |
| 981 |
zero then the seed is calculated using actual time, year, month and |
| 982 |
day. |
| 983 |
|
| 984 |
Under \TeX{} you have to use the command \Cmd{\RLsetrandomseed} to |
| 985 |
give the seed value. As for the option, with a zero value, the seed is |
| 986 |
calculated using actual time, year, month and day. The syntax is |
| 987 |
simple: |
| 988 |
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth] |
| 989 |
\RLsetrandomseed{<value>} |
| 990 |
\end{Verbatim} |
| 991 |
Of course, this command is available under \LaTeX{}. |
| 992 |
|
| 993 |
\subsubsection{Loop} |
| 994 |
For complex material with several databases, it could be useful to use |
| 995 |
external loop such \cmd{\foreach} from \package{pgffor} package or |
| 996 |
\cmd{\multido} from \package{multido} package. In fact, this is a good |
| 997 |
idea but not the best! These commands (\cmd{\foreach} and |
| 998 |
\cmd{\multido}) work inside a group at each loop. With |
| 999 |
\package{randomlist} that doesn't work everytime since lists are |
| 1000 |
restored at each loop. For example, you can't extract element of a |
| 1001 |
list. |
| 1002 |
|
| 1003 |
It is possible to read the list with \cmd{\Get...Item}. In this case, |
| 1004 |
you should probably use the command \Cmd{\CountList} to know the size |
| 1005 |
of a list. This command takes two arguments: the list name and a macro |
| 1006 |
to store the number of elements. As usual you give only the name of |
| 1007 |
the macro (without the backslash). The big difference between extract |
| 1008 |
and get is that with random reading, you can avoid to have twice (or |
| 1009 |
more) the same element. |
| 1010 |
|
| 1011 |
A real example is too long to be inserted here. We give only the code |
| 1012 |
source. The file \texttt{.tex} and the result \texttt{.pdf} are part |
| 1013 |
of the package distribution. In this example, we use two databases: |
| 1014 |
one for the pupils and the other one for the pythagorean triples. As |
| 1015 |
we read randomly the pythagorean triples and as we won't the same test |
| 1016 |
for two pupils, then we don't use the external loop described in the |
| 1017 |
latter paragraph. |
| 1018 |
|
| 1019 |
\VerbatimInput[gobble=0, frame=none, numbers=left, xleftmargin=0pt, |
| 1020 |
xrightmargin=0pt]{test.tex} |
| 1021 |
|
| 1022 |
Be careful! When we extract triples inside the loop, we must be sure |
| 1023 |
that there is more triples than pupils elsewhere an error about an |
| 1024 |
empty list is raised. |
| 1025 |
|
| 1026 |
Lines 15, 16, 17 read the data bases and extract the title line from |
| 1027 |
\texttt{pupils.dat}. After that, we enter in the main loop (lines~18 |
| 1028 |
to~60). |
| 1029 |
|
| 1030 |
At the beginning of the loop, we read the fields for the pupil (name, |
| 1031 |
first name and note) and the three fields of the pythagorean triple |
| 1032 |
(lines~20 to~26). It's here that we extract randomly a triple. Since |
| 1033 |
it's an extraction, another pupil will have another triple. |
| 1034 |
|
| 1035 |
Lines 27 to 39 typeset the test and lines 40 to 59 typeset the answer |
| 1036 |
to the test. We test the note of the pupil to decide the type of |
| 1037 |
exercise: Pythagorean theorem to find the hypotenuse (easy) or |
| 1038 |
Pythagorean theorem to find a side (less easy). |
| 1039 |
|
| 1040 |
\subsubsection{Internal} |
| 1041 |
You can access directly to a list. It's not recommended but\dots |
| 1042 |
|
| 1043 |
If the list name is \texttt{LName} (pay attention to the letter case), |
| 1044 |
then the length of the list is \cmd{\LName-len} and the $n$th element |
| 1045 |
of the list (starting from zero) is \cmd{\LName-$n$}. When an element |
| 1046 |
is a record with several fields, those fields are inside braces. For |
| 1047 |
example the first element of list \texttt{Triples} (see last example) |
| 1048 |
is: \cmd{\Triples-0}~$=$~\texttt{\{119\}\{120\}\{169\}}. As you can |
| 1049 |
see, inside a list, the characters for separator field and for string |
| 1050 |
delimiter don't exist. |
| 1051 |
|
| 1052 |
The authors don't see any situation where knowing internal is |
| 1053 |
important. If some users have good idea about it then writing to the |
| 1054 |
authors will be an appreciate initiative! |
| 1055 |
|
| 1056 |
\section{\LaTeX{} Lists} |
| 1057 |
Package \package{randomlist} offers two other special commands which |
| 1058 |
allow to build random lists. |
| 1059 |
|
| 1060 |
The first one is \Cmd{\RandomItemizeList} which build an itemize list |
| 1061 |
with random placement of items. Each item is a group.\par\medskip |
| 1062 |
|
| 1063 |
\RLsetrandomseed{1} |
| 1064 |
\begin{SideBySideExample} |
| 1065 |
\LaTeX{} is: |
| 1066 |
\RandomItemizeList |
| 1067 |
{magical} |
| 1068 |
{logical} |
| 1069 |
{practical} |
| 1070 |
{clinical} |
| 1071 |
{cynical} |
| 1072 |
\end{SideBySideExample} |
| 1073 |
\par\medskip |
| 1074 |
|
| 1075 |
The second command is for enumerate list. It is |
| 1076 |
\Cmd{\RandomEnumerateList} and it acts like the previous |
| 1077 |
one:\par\medskip |
| 1078 |
|
| 1079 |
\RLsetrandomseed{2} |
| 1080 |
\begin{SideBySideExample} |
| 1081 |
\LaTeX{} is: |
| 1082 |
\RandomEnumerateList |
| 1083 |
{magical} |
| 1084 |
{logical} |
| 1085 |
{practical} |
| 1086 |
{clinical} |
| 1087 |
{cynical} |
| 1088 |
\end{SideBySideExample} |
| 1089 |
\par\medskip |
| 1090 |
|
| 1091 |
|
| 1092 |
\newpage |
| 1093 |
\newgeometry{left=4cm, right=2cm, top=2cm, bottom=2cm} |
| 1094 |
\section{Package \package{randomlist} code} |
| 1095 |
\DocInput{randomlist.dtx} |
| 1096 |
\newpage |
| 1097 |
\newgeometry{left=2cm, right=2cm, top=2cm, bottom=2cm} |
| 1098 |
\PrintChanges |
| 1099 |
\PrintIndex |
| 1100 |
\newpage |
| 1101 |
\appendix |
| 1102 |
\fvset{gobble=0,frame=none,numbers=left,xleftmargin=0pt,xrightmargin=0pt} |
| 1103 |
\section{File \texttt{pythagoras.dat}} |
| 1104 |
\label{file-pythagoras} |
| 1105 |
This file contains Pythagorean triples which have three digits. There |
| 1106 |
isn't all these triple. In fact the triple are built with the famous |
| 1107 |
formula $(u^2-v^2,2uv,u^2+v^2)$ with $u$ and $v$ positive integers |
| 1108 |
such $u>v$. Here are only the hundred first triples with three |
| 1109 |
digits. |
| 1110 |
|
| 1111 |
\VerbatimInput{pythagoras.dat} |
| 1112 |
\newpage |
| 1113 |
\section{File \texttt{pupils.dat}} |
| 1114 |
\label{file-pupils} |
| 1115 |
This file shows a first line which isn't a data line. |
| 1116 |
|
| 1117 |
\VerbatimInput{pupils.dat} |
| 1118 |
\newpage |
| 1119 |
\section{File \texttt{comets.dat}} |
| 1120 |
\label{file-comets} |
| 1121 |
This file use lines which aren't data lines and weird separator. |
| 1122 |
|
| 1123 |
\VerbatimInput{comets.dat} |
| 1124 |
\end{document} |
| 1125 |
%</driver> |
| 1126 |
% \fi |
| 1127 |
% |
| 1128 |
% \changes{v0.1}{2013/05/03}{% |
| 1129 |
% First release |
| 1130 |
% } |
| 1131 |
% \changes{v0.1a}{2013/05/06}{% |
| 1132 |
% Fix bug about braces. |
| 1133 |
% } |
| 1134 |
% \changes{v0.1b}{2013/05/10}{% |
| 1135 |
% Add \cmd{\initrandomlist}. |
| 1136 |
% } |
| 1137 |
% \changes{v1.0}{2015/12/27}{% |
| 1138 |
% randomlist completly rewritten. |
| 1139 |
% } |
| 1140 |
% \changes{v1.1}{2016/01/16}{% |
| 1141 |
% Tons of improvements due to Christian Tellechea. |
| 1142 |
% } |
| 1143 |
% \changes{v1.2}{2016/07/13}{% |
| 1144 |
% First public release. |
| 1145 |
% } |
| 1146 |
% \changes{v1.3}{2017/09/11}{% |
| 1147 |
% Random operations are \global. |
| 1148 |
% } |
| 1149 |
% \CheckSum{0} |
| 1150 |
% \iffalse |
| 1151 |
%<*latex> |
| 1152 |
% \fi |
| 1153 |
% \subsection{\LaTeX's wrapper} |
| 1154 |
% \subsubsection{Introduction} |
| 1155 |
% We start with release number and date. |
| 1156 |
% \begin{macrocode} |
| 1157 |
\NeedsTeXFormat{LaTeX2e}[1995/06/01] |
| 1158 |
\ProvidesPackage{randomlist} |
| 1159 |
[2017/09/11 v1.3 Package for random list (JCC, CT)] |
| 1160 |
% \end{macrocode} |
| 1161 |
% \LaTeX's wrapper has the possibility to use option. There is only |
| 1162 |
% one option: the seed one. It requires the \package{(x)keyval} package. |
| 1163 |
% \begin{macrocode} |
| 1164 |
\RequirePackage{xkeyval} |
| 1165 |
\DeclareOptionX{seed}{\gdef\RL@seed{#1}} |
| 1166 |
\ExecuteOptions{seed=0} |
| 1167 |
\ProcessOptionsX |
| 1168 |
% \end{macrocode} |
| 1169 |
% We can now call the real randomlist code! |
| 1170 |
% \begin{macrocode} |
| 1171 |
\input{randomlist} |
| 1172 |
% \end{macrocode} |
| 1173 |
% \subsubsection{\LaTeX{} lists} |
| 1174 |
% Obviously, \LaTeX{} lists are useful only with \LaTeX{}! |
| 1175 |
% \begin{macro}{\RandomItemizeList} |
| 1176 |
% Build an itemize list with random placement of items. |
| 1177 |
% \begin{macrocode} |
| 1178 |
\NewList{*RandomList*} |
| 1179 |
\def\RandomItemizeList{% |
| 1180 |
\def\RL@Type{itemize}% |
| 1181 |
\ClearList{*RandomList*}% |
| 1182 |
\@ifnextchar\bgroup{\@randomlist}{\@@randomlist}% |
| 1183 |
} |
| 1184 |
\long\def\@randomlist#1{% |
| 1185 |
\InsertRandomItem{*RandomList*}{#1}% |
| 1186 |
\@ifnextchar\bgroup{\@randomlist}{\@@randomlist}% |
| 1187 |
} |
| 1188 |
\def\@@randomlist{% |
| 1189 |
\long\edef\RL@body{\noexpand\begin{\RL@Type}}% |
| 1190 |
\RLfor \RL@var = 0 to \RL@lenof{*RandomList*}-1 \do{% |
| 1191 |
\long\edef\RL@body{% |
| 1192 |
\unexpanded\expandafter{\RL@body}% |
| 1193 |
\unexpanded\expandafter{% |
| 1194 |
\expandafter\item \csname *RandomList*-\RL@var\endcsname |
| 1195 |
}% |
| 1196 |
}% |
| 1197 |
}% |
| 1198 |
\long\edef\RL@body{\unexpanded\expandafter{\RL@body}\noexpand\end{\RL@Type}}% |
| 1199 |
\RL@body |
| 1200 |
} |
| 1201 |
% \end{macrocode} |
| 1202 |
% \end{macro} |
| 1203 |
% \begin{macro}{\RandomEnumerateList} |
| 1204 |
% Like |randomitemize| but for enumerate list. |
| 1205 |
% \begin{macrocode} |
| 1206 |
\newcommand*\RandomEnumerateList{% |
| 1207 |
\def\RL@Type{enumerate} |
| 1208 |
\ClearList{*RandomList*}% |
| 1209 |
\@ifnextchar\bgroup{\@randomlist}{\@@randomlist} |
| 1210 |
} |
| 1211 |
% \end{macrocode} |
| 1212 |
% \end{macro} |
| 1213 |
% That's all for the \LaTeX's wrapper! |
| 1214 |
% \iffalse |
| 1215 |
%</latex> |
| 1216 |
%<*tex> |
| 1217 |
% \fi |
| 1218 |
% \subsection{\TeX{} code} |
| 1219 |
% At the beginning, we have to deal with multiple call and |@|'s |
| 1220 |
% catcode. |
| 1221 |
% \begin{macrocode} |
| 1222 |
\csname RandomListLoaded\endcsname |
| 1223 |
\let\RandomListLoaded\endinput |
| 1224 |
\edef\RLAtCatcode{\the\catcode`\@} |
| 1225 |
\catcode`\@=11 |
| 1226 |
% \end{macrocode} |
| 1227 |
% If we aren't under \LaTeX{} then we need some \LaTeX{} |
| 1228 |
% commands. It's just a copy of \LaTeX{}$2\epsilon$ code. |
| 1229 |
% \begin{macrocode} |
| 1230 |
\ifx\@ifnextchar\@undefined |
| 1231 |
% \end{macrocode} |
| 1232 |
% Definition of |\@ifnextchar|. |
| 1233 |
% \begin{macrocode} |
| 1234 |
\long\def\@ifnextchar#1#2#3{% |
| 1235 |
\let\reserved@d=#1% |
| 1236 |
\def\reserved@a{#2}% |
| 1237 |
\def\reserved@b{#3}% |
| 1238 |
\futurelet\@let@token\@ifnch} |
| 1239 |
\def\@ifnch{% |
| 1240 |
\ifx\@let@token\@sptoken |
| 1241 |
\let\reserved@c\@xifnch |
| 1242 |
\else |
| 1243 |
\ifx\@let@token\reserved@d |
| 1244 |
\let\reserved@c\reserved@a |
| 1245 |
\else |
| 1246 |
\let\reserved@c\reserved@b |
| 1247 |
\fi |
| 1248 |
\fi |
| 1249 |
\reserved@c} |
| 1250 |
\def\:{\let\@sptoken= } \: % |
| 1251 |
\def\:{\@xifnch} \expandafter\def\: {\futurelet\@let@token\@ifnch} |
| 1252 |
\fi |
| 1253 |
% \end{macrocode} |
| 1254 |
% Definition of |\PackageError| and some \LaTeX{} functions when |
| 1255 |
% running under \TeX{}. |
| 1256 |
% \begin{macrocode} |
| 1257 |
\ifx\PackageError\@undefined |
| 1258 |
\long\def\@firstoftwo#1#2{#1} |
| 1259 |
\long\def\@secondoftwo#1#2{#2} |
| 1260 |
\def\@nnil{\@nil}% |
| 1261 |
\alloc@7\write\chardef\sixt@@n\@unused |
| 1262 |
\def\typeout#1{\immediate\write\@unused{#1}}% |
| 1263 |
\def\@spaces{\space\space\space\space} |
| 1264 |
\def\PackageError#1#2#3{% |
| 1265 |
\begingroup |
| 1266 |
\newlinechar`\^^J |
| 1267 |
\edef\RL@temp{#3}% |
| 1268 |
\expandafter\errhelp\expandafter{\RL@temp}% |
| 1269 |
\typeout{% |
| 1270 |
#1 error. \space See User's Manual for further information.^^J |
| 1271 |
\@spaces\@spaces\@spaces\@spaces |
| 1272 |
Type \space H <return> \space for immediate help.}% |
| 1273 |
\errmessage{#2}% |
| 1274 |
\endgroup |
| 1275 |
} |
| 1276 |
\fi |
| 1277 |
% \end{macrocode} |
| 1278 |
% We check if we work with an engine which contain at least |
| 1279 |
% e\TeX{}. |
| 1280 |
% \begin{macrocode} |
| 1281 |
\ifx\numexpr\@undefined |
| 1282 |
\begingroup |
| 1283 |
\newlinechar`\^^J |
| 1284 |
\errhelp{Run under etex, pdftex, xetex, luatex, ... but not under |
| 1285 |
tex}% |
| 1286 |
\typeout{% |
| 1287 |
randomlist error. \space See User's Manual for further information.^^J |
| 1288 |
\@spaces\@spaces\@spaces\@spaces |
| 1289 |
Type \space H <return> \space for immediate help.}% |
| 1290 |
\errmessage{You can't use randomlist under tex without etex extension.}% |
| 1291 |
\endgroup |
| 1292 |
\fi |
| 1293 |
% \end{macrocode} |
| 1294 |
% \begin{macro}{\@gobble} |
| 1295 |
% Redefine |\@gobble| if needed. |
| 1296 |
% \begin{macrocode} |
| 1297 |
\ifx\@gobble\@undefined |
| 1298 |
\long\def\@gobble#1{} |
| 1299 |
\fi |
| 1300 |
% \end{macrocode} |
| 1301 |
% \end{macro} |
| 1302 |
% \begin{macro}{\RL@addtomacro} |
| 1303 |
% We needs to add some code to some macros sometimes. |
| 1304 |
% \begin{macrocode} |
| 1305 |
\def\RL@addtomacro#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}} |
| 1306 |
% \end{macrocode} |
| 1307 |
% \end{macro} |
| 1308 |
% \begin{macro}{\RL@ifempty} |
| 1309 |
% Test if something is empty. Execute code according to the answer. |
| 1310 |
% \begin{macrocode} |
| 1311 |
\def\RL@ifempty#1{% |
| 1312 |
\ifcat\relax\detokenize{#1}\relax |
| 1313 |
\expandafter\@firstoftwo |
| 1314 |
\else |
| 1315 |
\expandafter\@secondoftwo |
| 1316 |
\fi |
| 1317 |
} |
| 1318 |
% \end{macrocode} |
| 1319 |
% \end{macro} |
| 1320 |
% PDF\LaTeX, and X$_\mathrm{E}$\TeX know the primitives |
| 1321 |
% |\pdfsetrandomseed| and |\pdfuniformedeviate| but lua\TeX{} didn't |
| 1322 |
% know those primitives. Then we have to process ``by hand''! |
| 1323 |
% \begin{macro}{\RLsetrandomseed} |
| 1324 |
% We define the macro which give the initial seed. If the argument is |
| 1325 |
% zero, the value is a mixture of actual time, day, month and year. If |
| 1326 |
% the argument is nonzero, we process a new randomseed. |
| 1327 |
% |
| 1328 |
% The actual random number is stored in |\RL@random|. |
| 1329 |
% \begin{macrocode} |
| 1330 |
\newcount\RL@random |
| 1331 |
\newcount\RL@random@a |
| 1332 |
\newcount\RL@random@b |
| 1333 |
\def\RLsetrandomseed#1{% |
| 1334 |
\ifnum#1=0 |
| 1335 |
\global\RL@random \numexpr \time + \year * \month * \day \relax |
| 1336 |
\else |
| 1337 |
\global\RL@random \numexpr \ifnum#1<0 -\fi#1 \relax |
| 1338 |
\fi |
| 1339 |
} |
| 1340 |
% \end{macrocode} |
| 1341 |
% If |\RL@seed| exists -- that is, if we run under \LaTeX{} -- we process |
| 1342 |
% the seed (option of \LaTeX{} package). Otherwise, we use the zero |
| 1343 |
% value. |
| 1344 |
% \begin{macrocode} |
| 1345 |
\ifx\RL@seed\@undefined |
| 1346 |
\RLsetrandomseed{0} |
| 1347 |
\else |
| 1348 |
\RLsetrandomseed{\RL@seed} |
| 1349 |
\fi |
| 1350 |
% \end{macrocode} |
| 1351 |
% \end{macro} |
| 1352 |
% \begin{macro}{\RL@nextrand} |
| 1353 |
% Process the next random number using Linear Congruentiel Generator |
| 1354 |
% with Shrage's metthod. |
| 1355 |
% \begin{macrocode} |
| 1356 |
\def\RL@nextrand{% |
| 1357 |
% \end{macrocode} |
| 1358 |
% Use the LCG with : |
| 1359 |
% \[x_{n+1} = 7^5 \times x_n \pmod{2^{31}-1}.\] |
| 1360 |
% For that we take: |
| 1361 |
% \begin{itemize} |
| 1362 |
% \item $7^5=16807$; |
| 1363 |
% \item $2^{31}-1 = 2147483647$; |
| 1364 |
% \item $q = \mathrm{E}\left(\frac{2^{31}-1}{7^5}\right)=127773$; |
| 1365 |
% \item $r = 2^{31}-1 \pmod{7^5} = 2836$. |
| 1366 |
% \end{itemize} |
| 1367 |
% Then: |
| 1368 |
% \[x_{n+1} = 7^5(x_n \pmod{q}) - r\times\mathrm{E}\left(\frac{x_n}{q}\right).\] |
| 1369 |
% If $x_{n+1} < 0$ then $x_{n+1} = x_{n+1} + 2^{31}-1$ |
| 1370 |
% \begin{macrocode} |
| 1371 |
\global\RL@random@a=\RL@random |
| 1372 |
\global\divide\RL@random@a 127773 |
| 1373 |
\global\RL@random@b=\RL@random@a |
| 1374 |
\global\multiply\RL@random@a -2836 |
| 1375 |
\global\multiply\RL@random@b -127773 |
| 1376 |
\global\advance\RL@random\RL@random@b |
| 1377 |
\global\multiply\RL@random 16807 |
| 1378 |
\global\advance\RL@random\RL@random@a |
| 1379 |
% \end{macrocode} |
| 1380 |
% If random number is negative add $2^{31}-1$. |
| 1381 |
% \begin{macrocode} |
| 1382 |
\ifnum\RL@random<0 |
| 1383 |
\global\advance\RL@random 2147483647 |
| 1384 |
\fi |
| 1385 |
} |
| 1386 |
% \end{macrocode} |
| 1387 |
% \end{macro} |
| 1388 |
% \begin{macro}{\RLuniformdeviate} |
| 1389 |
% Use |\RL@nextrand| to calculate a random integer between 0 |
| 1390 |
% (inclusive) and |#1| (exclusive). Store the result in macro |#2|. |
| 1391 |
% \begin{macrocode} |
| 1392 |
\def\RLuniformdeviate#1#2{% |
| 1393 |
% \end{macrocode} |
| 1394 |
% Compute the next random number |\RL@random|. |
| 1395 |
% \begin{macrocode} |
| 1396 |
\RL@nextrand |
| 1397 |
% \end{macrocode} |
| 1398 |
% Compute $|\RL@random| \pmod{\mathtt{\string#1}}$. |
| 1399 |
% \begin{macrocode} |
| 1400 |
\global\RL@random@a=\RL@random |
| 1401 |
\global\RL@random@b=\RL@random |
| 1402 |
\global\divide\RL@random@a \numexpr#1\relax |
| 1403 |
\global\RL@random@b \numexpr\RL@random@b - \RL@random@a * (#1)\relax |
| 1404 |
\expandafter\edef\csname #2\endcsname{\number\RL@random@b}% |
| 1405 |
}% |
| 1406 |
% \end{macrocode} |
| 1407 |
% \end{macro} |
| 1408 |
% \subsubsection{Introduction and first commands} |
| 1409 |
% \begin{macro}{\@ifIsList} |
| 1410 |
% Test if a list exists and then executes true code or false code. For |
| 1411 |
% that the list of list names is stored inside the token register |
| 1412 |
% |\@ListOfList|. Each name is separed to the next one by a ``|\sep|'' |
| 1413 |
% markup. |
| 1414 |
% \begin{macrocode} |
| 1415 |
\newtoks\@ListOfList |
| 1416 |
% \end{macrocode} |
| 1417 |
% |\@ifIsList| test if the list |#1| exists. If yes then it executes the |
| 1418 |
% next argument else it executes the third argument. Test must be |
| 1419 |
% executed on an expanded argument. |
| 1420 |
% \begin{macrocode} |
| 1421 |
\def\@ifIsList#1{% |
| 1422 |
\expandafter\@ifIsList@\expandafter{#1}% |
| 1423 |
} |
| 1424 |
\def\@ifIsList@#1{% |
| 1425 |
\def\@@ifIsList##1#1\sep##2\@@ifIsList{% |
| 1426 |
\csname @\ifx\empty##2\empty second\else first\fi oftwo\endcsname |
| 1427 |
}% |
| 1428 |
\expandafter\@@ifIsList\the\@ListOfList#1\sep\@@ifIsList |
| 1429 |
} |
| 1430 |
% \end{macrocode} |
| 1431 |
% \end{macro} |
| 1432 |
% \begin{macro}{\RL@lenof} |
| 1433 |
% Shortcut allowing to get the len of a list |
| 1434 |
% \begin{macrocode} |
| 1435 |
\def\RL@lenof#1{\csname #1-len\endcsname} |
| 1436 |
% \end{macrocode} |
| 1437 |
% \end{macro} |
| 1438 |
% \begin{macro}{\@ifIsListNotEmpty} |
| 1439 |
% Test if a list exist and isn't empty. If double yes then it executes |
| 1440 |
% the second argument else it executes the third one. |
| 1441 |
% \begin{macrocode} |
| 1442 |
\newif\if@EmptyListFound |
| 1443 |
\def\@ifIsListNotEmpty#1{% |
| 1444 |
\global\@EmptyListFoundfalse |
| 1445 |
\@ifIsList{#1}{% |
| 1446 |
\ifnum\RL@lenof{#1}=0 |
| 1447 |
\global\@EmptyListFoundtrue |
| 1448 |
\expandafter\@secondoftwo |
| 1449 |
\else |
| 1450 |
\expandafter\@firstoftwo |
| 1451 |
\fi |
| 1452 |
}% |
| 1453 |
\@secondoftwo |
| 1454 |
} |
| 1455 |
% \end{macrocode} |
| 1456 |
% \end{macro} |
| 1457 |
% \begin{macro}{\@NoListError} |
| 1458 |
% Error for an unexisting list or an empty list. |
| 1459 |
% \begin{macrocode} |
| 1460 |
\def\@NoListError#1{% |
| 1461 |
\if@EmptyListFound |
| 1462 |
\@EmptyListError{#1}% |
| 1463 |
\global\@EmptyListFoundfalse |
| 1464 |
\else |
| 1465 |
\PackageError{randomlist}% |
| 1466 |
{List #1 doesn't exist}% |
| 1467 |
{Maybe you mistyped the list name?}% |
| 1468 |
\fi |
| 1469 |
} |
| 1470 |
% \end{macrocode} |
| 1471 |
% \end{macro} |
| 1472 |
% \begin{macro}{\@EmptyListError} |
| 1473 |
% Error for an empty list. |
| 1474 |
% \begin{macrocode} |
| 1475 |
\def\@EmptyListError#1{% |
| 1476 |
\if@EmptyListFound |
| 1477 |
\PackageError{randomlist}% |
| 1478 |
{List #1 is empty}% |
| 1479 |
{Ask yourself why this list is empty.}% |
| 1480 |
} |
| 1481 |
% \end{macrocode} |
| 1482 |
% \end{macro} |
| 1483 |
% \begin{macro}{\@OutOfRangeError} |
| 1484 |
% Error for index out of range. |
| 1485 |
% \begin{macrocode} |
| 1486 |
\def\@OutOfRangeError#1#2{% |
| 1487 |
\PackageError{randomlist}% |
| 1488 |
{Index #2 is greater than last index of list #1}% |
| 1489 |
{There aren't enough elements in the list.}% |
| 1490 |
} |
| 1491 |
% \end{macrocode} |
| 1492 |
% \end{macro} |
| 1493 |
% \begin{macro}{\RL@nameldef} |
| 1494 |
% |\long| version of |\@namedef|. |
| 1495 |
% \begin{macrocode} |
| 1496 |
\long\def\RL@nameldef#1{% |
| 1497 |
\long\expandafter\def\csname #1\endcsname |
| 1498 |
} |
| 1499 |
% \end{macrocode} |
| 1500 |
% \end{macro} |
| 1501 |
% \begin{macro}{\RL@nameledef} |
| 1502 |
% All the macros in \package{randomlist} are long ones. It's |
| 1503 |
% useless for now since there isn't argument but it's a precaution |
| 1504 |
% for the future. |
| 1505 |
% |
| 1506 |
% |\long| version of |\@nameedef|. |
| 1507 |
% \begin{macrocode} |
| 1508 |
\long\def\RL@nameledef#1{% |
| 1509 |
\long\expandafter\edef\csname #1\endcsname |
| 1510 |
} |
| 1511 |
% \end{macrocode} |
| 1512 |
% \end{macro} |
| 1513 |
% \begin{macro}{\RL@namelgdef} |
| 1514 |
% |\long| version of |\@namegdef|. |
| 1515 |
% \begin{macrocode} |
| 1516 |
\long\def\RL@namelgdef#1{% |
| 1517 |
\long\expandafter\gdef\csname #1\endcsname |
| 1518 |
} |
| 1519 |
% \end{macrocode} |
| 1520 |
% \end{macro} |
| 1521 |
% \begin{macro}{\RL@namelxdef} |
| 1522 |
% |\long| version of |\@namexdef|. |
| 1523 |
% \begin{macrocode} |
| 1524 |
\long\def\RL@namelxdef#1{% |
| 1525 |
\long\expandafter\xdef\csname #1\endcsname |
| 1526 |
} |
| 1527 |
% \end{macrocode} |
| 1528 |
% \end{macro} |
| 1529 |
% \begin{macro}{\RL@let} |
| 1530 |
% |\let| between two macros (with just the names) |
| 1531 |
% \begin{macrocode} |
| 1532 |
\def\RL@let#1#2{% |
| 1533 |
\expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname |
| 1534 |
} |
| 1535 |
% \end{macrocode} |
| 1536 |
% \end{macro} |
| 1537 |
% \begin{macro}{\RLfor} |
| 1538 |
% Loop without group. Syntax is |\RLfor<var>=<begin>to<end>\do| |
| 1539 |
% \begin{macrocode} |
| 1540 |
\long\def\RL@doafterfi#1\fi{\fi#1} |
| 1541 |
\def\RLfor#1=#2to#3\do{% |
| 1542 |
% \end{macrocode} |
| 1543 |
% Set the variable. |
| 1544 |
% \begin{macrocode} |
| 1545 |
\edef#1{\number\numexpr#2}% |
| 1546 |
% \end{macrocode} |
| 1547 |
% Set |\RL@sgncomp| to |<+| or |>-| if variable is greater or less |
| 1548 |
% than end |
| 1549 |
% \begin{macrocode} |
| 1550 |
\edef\RL@sgncomp{\ifnum#1<\numexpr#3\relax>+\else<-\fi}% |
| 1551 |
% \end{macrocode} |
| 1552 |
% Call auxiliary macro with five parameters: |
| 1553 |
% \begin{macrocode} |
| 1554 |
\expandafter\RLfor@i |
| 1555 |
% \end{macrocode} |
| 1556 |
% First argument (sub-recursive macro name build with the <variable> |
| 1557 |
% name). |
| 1558 |
% \begin{macrocode} |
| 1559 |
\csname RLfor@ii@\string#1\expandafter\endcsname\expandafter |
| 1560 |
% \end{macrocode} |
| 1561 |
% Second argument (max). |
| 1562 |
% \begin{macrocode} |
| 1563 |
{\number\numexpr#3\expandafter}% |
| 1564 |
% \end{macrocode} |
| 1565 |
% Third and fourth arguments since |\RL@sgncomp| is ``|<+|'' or ``|>-|''. |
| 1566 |
% \begin{macrocode} |
| 1567 |
\RL@sgncomp |
| 1568 |
% \end{macrocode} |
| 1569 |
% fifth argument (variable name). |
| 1570 |
% \begin{macrocode} |
| 1571 |
#1% |
| 1572 |
} |
| 1573 |
% \end{macrocode} |
| 1574 |
% Auxiliary macro: |
| 1575 |
% \begin{itemize} |
| 1576 |
% \item |#1| recursive macro name (like |\RLfor@ii@<var>|; |
| 1577 |
% \item |#2| max integer; |
| 1578 |
% \item |#3| ``|<|'' or ``|>|''; |
| 1579 |
% \item |#4| ``|+|'' or ``|-|'' (incrementation or decrementation); |
| 1580 |
% \item |#5| variable name; |
| 1581 |
% \item |#6| code to execute. |
| 1582 |
% \end{itemize} |
| 1583 |
% \begin{macrocode} |
| 1584 |
\long\def\RLfor@i#1#2#3#4#5#6{% |
| 1585 |
% \end{macrocode} |
| 1586 |
% Define the recursive submacro. |
| 1587 |
% \begin{macrocode} |
| 1588 |
\def#1{% |
| 1589 |
% \end{macrocode} |
| 1590 |
% While <var> isn't greater than max |
| 1591 |
% \begin{macrocode} |
| 1592 |
\unless\ifnum#5#3#2\relax |
| 1593 |
% \end{macrocode} |
| 1594 |
% In order to have a tail recursion. |
| 1595 |
% \begin{macrocode} |
| 1596 |
\RL@doafterfi{% |
| 1597 |
% \end{macrocode} |
| 1598 |
% Execute the loop code. |
| 1599 |
% \begin{macrocode} |
| 1600 |
#6% |
| 1601 |
% \end{macrocode} |
| 1602 |
% Increment <variable> by one. |
| 1603 |
% \begin{macrocode} |
| 1604 |
\edef#5{\number\numexpr#5#41\relax}% |
| 1605 |
% \end{macrocode} |
| 1606 |
% And repeat. |
| 1607 |
% \begin{macrocode} |
| 1608 |
#1% |
| 1609 |
}% |
| 1610 |
\fi |
| 1611 |
}% |
| 1612 |
% \end{macrocode} |
| 1613 |
% submacro recursive call. |
| 1614 |
% \begin{macrocode} |
| 1615 |
#1% |
| 1616 |
} |
| 1617 |
% \end{macrocode} |
| 1618 |
% \end{macro} |
| 1619 |
% \subsubsection{General list commands} |
| 1620 |
% \begin{macro}{\NewList} |
| 1621 |
% The main structure is the list. A list |L| is a collection of macros |
| 1622 |
% |L-<n>| where |<n>| is an index (starting from zero) and a macro |
| 1623 |
% |L-len| which store the len of the list, i.e. the last index plus |
| 1624 |
% one. |
| 1625 |
% |
| 1626 |
% When a new list is created, its name is stored in |@ListOfList|. A |
| 1627 |
% macro is also created for accessing data. |
| 1628 |
% \begin{macrocode} |
| 1629 |
\def\NewList#1{% |
| 1630 |
\@ifIsList{#1}{% |
| 1631 |
% \end{macrocode} |
| 1632 |
% If a list with the same name exists then raise an error. |
| 1633 |
% \begin{macrocode} |
| 1634 |
\PackageError{randomlist}% |
| 1635 |
{List #1 already exists}% |
| 1636 |
{Use \string\ClearList.}% |
| 1637 |
}% |
| 1638 |
{% |
| 1639 |
% \end{macrocode} |
| 1640 |
% When a list |MyName| is created, the macros |\MyName| and |
| 1641 |
% |\MyName-len| are created and there will be macros |\MyName-<n>| to |
| 1642 |
% store data. Then \package{randomlist} prohibit the name |MyName| for |
| 1643 |
% a list if the macro |\MyName| already exists. |
| 1644 |
% \begin{macrocode} |
| 1645 |
\ifcsname #1\endcsname |
| 1646 |
\PackageError{randomlist}% |
| 1647 |
{Command \csname#1\endcsname already exists}% |
| 1648 |
{Creating list #1 defines a \csname#1\endcsname command.}% |
| 1649 |
\else |
| 1650 |
% \end{macrocode} |
| 1651 |
% If everything is fine, create the len macro which store the len of |
| 1652 |
% the list (starting with 0); |
| 1653 |
% \begin{macrocode} |
| 1654 |
\RL@nameldef{#1-len}{0}% |
| 1655 |
% \end{macrocode} |
| 1656 |
% append the list name to the list of names |\@ListOfList|; |
| 1657 |
% \begin{macrocode} |
| 1658 |
\@ListOfList\expandafter{\the\@ListOfList#1\sep}% |
| 1659 |
% \end{macrocode} |
| 1660 |
% and create the |\Mylist[<index>]| macro. |
| 1661 |
% \begin{macrocode} |
| 1662 |
\expandafter\def\csname #1\endcsname[##1]{% |
| 1663 |
% \end{macrocode} |
| 1664 |
% If index is to big, the macro is |\relax| (a sort of undefined |
| 1665 |
% without error). |
| 1666 |
% \begin{macrocode} |
| 1667 |
\ifnum##1>\csname#1-len\endcsname |
| 1668 |
\relax |
| 1669 |
\else |
| 1670 |
\csname #1-##1\endcsname |
| 1671 |
\fi |
| 1672 |
}% |
| 1673 |
\fi |
| 1674 |
}% |
| 1675 |
} |
| 1676 |
% \end{macrocode} |
| 1677 |
% \end{macro} |
| 1678 |
% \begin{macro}{\ClearList} |
| 1679 |
% |\ClearList| erases a list. It sets the length to zero. There is no |
| 1680 |
% need to erase all the |\Mylist-<index>| macros. |
| 1681 |
% \begin{macrocode} |
| 1682 |
\def\ClearList#1{% |
| 1683 |
\@ifIsList{#1}{% |
| 1684 |
% \end{macrocode} |
| 1685 |
% Clear the list if it exists. |
| 1686 |
% \begin{macrocode} |
| 1687 |
\RL@nameldef{#1-len}{0}% |
| 1688 |
}% |
| 1689 |
{\@NoListError{#1}}% |
| 1690 |
} |
| 1691 |
% \end{macrocode} |
| 1692 |
% \end{macro} |
| 1693 |
% \begin{macro}{\CopyList} |
| 1694 |
% Copy list |#1| in list |#2|. |
| 1695 |
% \begin{macrocode} |
| 1696 |
\def\CopyList#1#2{% |
| 1697 |
\@ifIsList{#1}{% |
| 1698 |
\@ifIsList{#2}{% |
| 1699 |
\RL@let{#2-len}{#1-len}% |
| 1700 |
\ifnum\RL@lenof{#1}>0 |
| 1701 |
\RLfor\RL@iter=0 to \RL@lenof{#1}-1 \do{% |
| 1702 |
\RL@let{#2-\RL@iter}{#1-\RL@iter}% |
| 1703 |
}% |
| 1704 |
\fi |
| 1705 |
}% |
| 1706 |
{\@NoListError{#2}}% |
| 1707 |
}% |
| 1708 |
{\@NoListError{#1}}% |
| 1709 |
} |
| 1710 |
% \end{macrocode} |
| 1711 |
% \end{macro} |
| 1712 |
% \begin{macro}{\InsertList} |
| 1713 |
% Insert List |#3| to the list |#1| starting at index |#2|. |
| 1714 |
% \begin{macrocode} |
| 1715 |
\def\InsertList#1#2#3{% |
| 1716 |
\@ifIsList{#1}{% |
| 1717 |
\@ifIsList{#3}{% |
| 1718 |
\ifnum #2>\RL@lenof{#1} |
| 1719 |
\@OutOfRangeError{#1}{#2}% |
| 1720 |
\else |
| 1721 |
\ShiftList{#1}{#2}{\RL@lenof{#3}}% |
| 1722 |
\ifnum\RL@lenof{#3}>0 |
| 1723 |
\RLfor\RL@iter=0 to \RL@lenof{#3}-1 \do{% |
| 1724 |
\RL@let{#1-\number\numexpr\RL@iter+#2}{#3-\RL@iter}% |
| 1725 |
}% |
| 1726 |
\fi |
| 1727 |
\fi |
| 1728 |
}% |
| 1729 |
{\@NoListError{#3}}% |
| 1730 |
}% |
| 1731 |
{\@NoListError{#1}}% |
| 1732 |
} |
| 1733 |
% \end{macrocode} |
| 1734 |
% \end{macro} |
| 1735 |
% \begin{macro}{\ShowList} |
| 1736 |
% Macro for debugging purpose. First we declare some scratch count |
| 1737 |
% registers. |
| 1738 |
% \begin{macrocode} |
| 1739 |
\newcount\RL@counti |
| 1740 |
\newcount\RL@countii |
| 1741 |
\newcount\RL@countiii |
| 1742 |
\def\ShowList#1{% |
| 1743 |
% \end{macrocode} |
| 1744 |
% We show a list only if this list exists! |
| 1745 |
% \begin{macrocode} |
| 1746 |
\@ifIsList{#1}{% |
| 1747 |
\ifhmode\par\noindent\fi |
| 1748 |
% \end{macrocode} |
| 1749 |
% Typeset |BEGIN{MyList}|. As we typeset braces, we have to use |
| 1750 |
% ttfamily, then put the material inside a group. |
| 1751 |
% \begin{macrocode} |
| 1752 |
\begingroup |
| 1753 |
\ifdefined\ttfamily\ttfamily\else\tt\fi |
| 1754 |
BEGIN\detokenize{{#1}} |
| 1755 |
% \end{macrocode} |
| 1756 |
% Typeset the number of elements. |
| 1757 |
% \begin{macrocode} |
| 1758 |
(\ifcase\RL@lenof{#1} |
| 1759 |
empty list% |
| 1760 |
\or |
| 1761 |
1 element% |
| 1762 |
\else |
| 1763 |
\RL@lenof{#1} elements% |
| 1764 |
\fi)\par |
| 1765 |
% \end{macrocode} |
| 1766 |
% Loop to typeset element one after one. |
| 1767 |
% \begin{macrocode} |
| 1768 |
\ifnum\RL@lenof{#1}>0 |
| 1769 |
\parindent=1em |
| 1770 |
\RLfor\RL@iter=0 to \RL@lenof{#1}-1 \do {% |
| 1771 |
#1[\RL@iter] = \expandafter\RL@meaning\csname |
| 1772 |
#1-\RL@iter\endcsname |
| 1773 |
\par |
| 1774 |
}% |
| 1775 |
\fi |
| 1776 |
% \end{macrocode} |
| 1777 |
% Typeset |END{MyList}|. |
| 1778 |
% \begin{macrocode} |
| 1779 |
\noindent |
| 1780 |
END\detokenize{{#1}}\par |
| 1781 |
\endgroup |
| 1782 |
}% |
| 1783 |
{\@NoListError{#1}}% |
| 1784 |
} |
| 1785 |
% \end{macrocode} |
| 1786 |
% \end{macro} |
| 1787 |
% \begin{macro}{\RL@meaning} |
| 1788 |
% Like \TeX{} primitive |\meaning| without prefix |(\long) macro:->|: |
| 1789 |
% \begin{macrocode} |
| 1790 |
\def\RL@meaning#1{\expandafter\RL@meaningi\meaning#1} |
| 1791 |
\expandafter\def\expandafter\RL@meaningi\expandafter#\expandafter1\string>{} |
| 1792 |
% \end{macrocode} |
| 1793 |
% \end{macro} |
| 1794 |
% \begin{macro}{\CountList} |
| 1795 |
% Count the number of elements in the list |#1|. Store it in |#2|. |
| 1796 |
% \begin{macrocode} |
| 1797 |
\def\CountList#1#2{% |
| 1798 |
\@ifIsList{#1}% |
| 1799 |
{\RL@nameledef{#2}{\RL@lenof{#1}}}% |
| 1800 |
{\@NoListError{#1}}% |
| 1801 |
} |
| 1802 |
% \end{macrocode} |
| 1803 |
% \end{macro} |
| 1804 |
% \subsubsection{Writing and reading list commands} |
| 1805 |
% \begin{macro}{\ShiftList} |
| 1806 |
% Shift the elements of a list left or right. The syntax is: |
| 1807 |
% |
| 1808 |
% |\ShiftList{list name}{start}{shift}| |
| 1809 |
% |
| 1810 |
% where |start| is the first index to shift and |shift| the number of |
| 1811 |
% shifting. If |shift| is positive, it is a right shift. If |shitf| is |
| 1812 |
% negative, it is a left shift. |
| 1813 |
% \begin{macrocode} |
| 1814 |
\def\ShiftList#1#2#3{% |
| 1815 |
\@ifIsList{#1}% |
| 1816 |
{% |
| 1817 |
% \end{macrocode} |
| 1818 |
% No action if |shift| is zero! |
| 1819 |
% \begin{macrocode} |
| 1820 |
\unless\ifnum#3=0 |
| 1821 |
% \end{macrocode} |
| 1822 |
% If |<start>| is negative, raise an error. |
| 1823 |
% \begin{macrocode} |
| 1824 |
\ifnum\numexpr#2<0 |
| 1825 |
\PackageError{randomlist}% |
| 1826 |
{Negative index number}% |
| 1827 |
{Index must be equal or greater than 0}% |
| 1828 |
\else |
| 1829 |
% \end{macrocode} |
| 1830 |
% If |<start>| is greater than the lists length, raise an error. |
| 1831 |
% \begin{macrocode} |
| 1832 |
\ifnum\numexpr#2>\RL@lenof{#1}\relax |
| 1833 |
\PackageError{randomlist}% |
| 1834 |
{Index \number\numexpr #2\relax\space too big |
| 1835 |
(<=\RL@lenof{#1})}% |
| 1836 |
{Index must be equal or smaller than length of |
| 1837 |
the list}% |
| 1838 |
\else |
| 1839 |
% \end{macrocode} |
| 1840 |
% Here we have $0 \leq |<start>| \leq |len|(|<list>|)$. |
| 1841 |
% |
| 1842 |
% If |<shift>| is positive, we process a right shifting: it's alway |
| 1843 |
% possible. |
| 1844 |
% \begin{macrocode} |
| 1845 |
\ifnum\numexpr#3>0 |
| 1846 |
\RLfor\RL@iter = \RL@lenof{#1} to #2 \do{% |
| 1847 |
\RL@let{#1-\number\numexpr\RL@iter+#3}{#1-\RL@iter}% |
| 1848 |
}% |
| 1849 |
% \end{macrocode} |
| 1850 |
% Empty the items out of shift part. |
| 1851 |
% \begin{macrocode} |
| 1852 |
\RLfor\RL@iter = #2 to #2 + #3 - 1 \do{% |
| 1853 |
\RL@nameldef{#1-\RL@iter}{}% |
| 1854 |
}% |
| 1855 |
\else |
| 1856 |
\ifnum-#3>\numexpr#2\relax |
| 1857 |
% \end{macrocode} |
| 1858 |
% If the negative shifting is to big for index |#2| then raise an error. |
| 1859 |
% \begin{macrocode} |
| 1860 |
\PackageError{randomlist}% |
| 1861 |
{Negative shift to big}% |
| 1862 |
{When negative, shift must not be greater than index}% |
| 1863 |
\else |
| 1864 |
% \end{macrocode} |
| 1865 |
% Elsewhere, process the left shifting. |
| 1866 |
% \begin{macrocode} |
| 1867 |
\RLfor\RL@iter=#2 to \RL@lenof{#1} \do{% |
| 1868 |
\RL@let{#1-\number\numexpr\RL@iter+#3}{#1-\RL@iter}% |
| 1869 |
}% |
| 1870 |
\fi |
| 1871 |
\fi |
| 1872 |
% \end{macrocode} |
| 1873 |
% Set the list length for both positive and negative shifting. |
| 1874 |
% \begin{macrocode} |
| 1875 |
\RL@nameledef{#1-len}{\number\numexpr\RL@lenof{#1} + #3}% |
| 1876 |
\fi\fi\fi |
| 1877 |
}% |
| 1878 |
{\@NoListError{#1}}% |
| 1879 |
} |
| 1880 |
% \end{macrocode} |
| 1881 |
% \end{macro} |
| 1882 |
% \begin{macro}{\InsertLastItem} |
| 1883 |
% Add an element |#2| at the end of the list |#1|. |
| 1884 |
% \begin{macrocode} |
| 1885 |
\long\def\InsertLastItem#1#2{% |
| 1886 |
\@ifIsList{#1} |
| 1887 |
{% |
| 1888 |
\RL@nameldef{#1-\RL@lenof{#1}}{#2}% |
| 1889 |
\RL@nameledef{#1-len}{\number\numexpr\RL@lenof{#1}+1}% |
| 1890 |
} |
| 1891 |
{\@NoListError{#1}}% |
| 1892 |
} |
| 1893 |
% \end{macrocode} |
| 1894 |
% \end{macro} |
| 1895 |
% \begin{macro}{\InsertFirstItem} |
| 1896 |
% Add an element |#2| at the beginning of the list |#1|. For that, |
| 1897 |
% shift right all the element and then put |#3| at |L[0]|. |
| 1898 |
% \begin{macrocode} |
| 1899 |
\long\def\InsertFirstItem#1#2{% |
| 1900 |
\InsertItem{#1}{0}{#2}% |
| 1901 |
} |
| 1902 |
% \end{macrocode} |
| 1903 |
% \end{macro} |
| 1904 |
% \begin{macro}{\InsertItem} |
| 1905 |
% Add an element |#3| at the position |#2| of the list |#1|. For that, |
| 1906 |
% pass from |L[0]| to |L[#2-1]| then shift right from |L[#2]| to |
| 1907 |
% |L[len]| and finally put |#3| at |L[#2]|. To do this, we must have |
| 1908 |
% |#2| $\geq$ |L-len|. |
| 1909 |
% \begin{macrocode} |
| 1910 |
\long\def\InsertItem#1#2#3{% |
| 1911 |
\@ifIsList{#1}% |
| 1912 |
{% |
| 1913 |
\ShiftList{#1}{#2}{1}% |
| 1914 |
\RL@nameldef{#1-#2}{#3}% |
| 1915 |
}% |
| 1916 |
{\@NoListError{#1}}% |
| 1917 |
} |
| 1918 |
% \end{macrocode} |
| 1919 |
% \end{macro} |
| 1920 |
% \begin{macro}{\InsertRandomItem} |
| 1921 |
% Insert element |#2| in a random position of list |#1|. |
| 1922 |
% \begin{macrocode} |
| 1923 |
\long\def\InsertRandomItem#1#2{% |
| 1924 |
\@ifIsList{#1}% |
| 1925 |
{% |
| 1926 |
\RLuniformdeviate{\RL@lenof{#1}+1}{RL@temp}% |
| 1927 |
\InsertItem{#1}{\RL@temp}{#2}% |
| 1928 |
}% |
| 1929 |
{\@NoListError{#1}}% |
| 1930 |
} |
| 1931 |
% \end{macrocode} |
| 1932 |
% \end{macro} |
| 1933 |
% \begin{macro}{\ExtractFirstItem} |
| 1934 |
% Extract the first element of list |#1| and store it in |#2|. |
| 1935 |
% \begin{macrocode} |
| 1936 |
\def\ExtractFirstItem#1#2{% |
| 1937 |
\@ifIsList{#1}% |
| 1938 |
{% |
| 1939 |
\ExtractItem{#1}{0}{#2}% |
| 1940 |
}% |
| 1941 |
{\@NoListError{#1}}% |
| 1942 |
} |
| 1943 |
% \end{macrocode} |
| 1944 |
% \end{macro} |
| 1945 |
% \begin{macro}{\ExtractLastItem} |
| 1946 |
% Extract the last element of list |#1| and store it in |#2|. |
| 1947 |
% \begin{macrocode} |
| 1948 |
\def\ExtractLastItem#1#2{% |
| 1949 |
\@ifIsListNotEmpty{#1}% |
| 1950 |
{% |
| 1951 |
\RL@let{#2}{#1-\number\numexpr\RL@lenof{#1}-1}% |
| 1952 |
\RL@nameledef{#1-len}{\number\numexpr\RL@lenof{#1}-1}% |
| 1953 |
}% |
| 1954 |
{\@NoListError{#1}}% |
| 1955 |
} |
| 1956 |
% \end{macrocode} |
| 1957 |
% \end{macro} |
| 1958 |
% \begin{macro}{\ExtractItem} |
| 1959 |
% Extract the element at the position |#2| of the list |#1| and store |
| 1960 |
% it in |#3|. |
| 1961 |
% \begin{macrocode} |
| 1962 |
\def\ExtractItem#1#2#3{% |
| 1963 |
\@ifIsListNotEmpty{#1}% |
| 1964 |
{% |
| 1965 |
\RL@let{#3}{#1-#2}% |
| 1966 |
\ShiftList{#1}{#2+1}{-1}% |
| 1967 |
}% |
| 1968 |
{\@NoListError{#1}}% |
| 1969 |
} |
| 1970 |
% \end{macrocode} |
| 1971 |
% \end{macro} |
| 1972 |
% \begin{macro}{\ExtractRandomItem} |
| 1973 |
% Extract element in a random position of list |#1| and store it in |#2|. |
| 1974 |
% \begin{macrocode} |
| 1975 |
\def\ExtractRandomItem#1#2{% |
| 1976 |
\@ifIsListNotEmpty{#1}% |
| 1977 |
{% |
| 1978 |
\RLuniformdeviate{\RL@lenof{#1}}{RL@temp}% |
| 1979 |
\ExtractItem{#1}{\RL@temp}{#2}% |
| 1980 |
}% |
| 1981 |
{\@NoListError{#1}}% |
| 1982 |
} |
| 1983 |
% \end{macrocode} |
| 1984 |
% \end{macro} |
| 1985 |
% \begin{macro}{\ExtractList} |
| 1986 |
% |ExtractList| extract a list from a list. There are four arguments: |
| 1987 |
% \begin{itemize} |
| 1988 |
% \item |#1| is the list from which the extraction is made; |
| 1989 |
% \item |#2| is the starting index of extraction; |
| 1990 |
% \item |#3| is the ending index of extraction; |
| 1991 |
% \item |#4| is the list which receive the extracted list. |
| 1992 |
% \end{itemize} |
| 1993 |
% \begin{macrocode} |
| 1994 |
\def\ExtractList#1#2#3#4{% |
| 1995 |
% \end{macrocode} |
| 1996 |
% In order to do something, |#1| and |#2| must be lists, and indexes |
| 1997 |
% |#2| and |#3| must be inside list |#1|. |
| 1998 |
% \begin{macrocode} |
| 1999 |
\@ifIsList{#1}{% |
| 2000 |
\@ifIsList{#4}{% |
| 2001 |
\ifnum#2<\RL@lenof{#1}% |
| 2002 |
\ifnum#3<\RL@lenof{#1}% |
| 2003 |
\ifnum#2>#3\relax |
| 2004 |
% \end{macrocode} |
| 2005 |
% If $\text{start} > \text{end}$ we build an empty list. |
| 2006 |
% \begin{macrocode} |
| 2007 |
\RL@nameldef{#4-len}{0}% |
| 2008 |
\else |
| 2009 |
% \end{macrocode} |
| 2010 |
% If $\text{start} \leq \text{end}$ we build a real extracted list. We |
| 2011 |
% have to be careful because |\ExtractItem| uses the loop variable |
| 2012 |
% |\RL@iter|. Then we use another loop variable. |
| 2013 |
% \begin{macrocode} |
| 2014 |
\RLfor\RL@iterextract=0 to #3 - #2 \do{% |
| 2015 |
\RL@let{#4-\RL@iterextract}{#1-#2}% |
| 2016 |
\ExtractItem{#1}{#2}{RL@temp}% |
| 2017 |
}% |
| 2018 |
\RL@nameledef{#4-len}{\number\numexpr #3 - #2 + 1}% |
| 2019 |
\fi |
| 2020 |
\else |
| 2021 |
\@OutOfRangeError{#1}{#3}% |
| 2022 |
\fi |
| 2023 |
\else |
| 2024 |
\@OutOfRangeError{#1}{#2}% |
| 2025 |
\fi |
| 2026 |
}% |
| 2027 |
{\@NoListError{#4}}% |
| 2028 |
}% |
| 2029 |
{\@NoListError{#1}}% |
| 2030 |
} |
| 2031 |
% \end{macrocode} |
| 2032 |
% \end{macro} |
| 2033 |
% \begin{macro}{\GetFirstItem} |
| 2034 |
% Get the first element of list |#1| and store it in |#2|. |
| 2035 |
% \begin{macrocode} |
| 2036 |
\def\GetFirstItem#1#2{% |
| 2037 |
\GetItem{#1}{0}{#2}% |
| 2038 |
} |
| 2039 |
% \end{macrocode} |
| 2040 |
% \end{macro} |
| 2041 |
% \begin{macro}{\GetLastItem} |
| 2042 |
% Get the last element of list |#1| and store it in |#2|. |
| 2043 |
% \begin{macrocode} |
| 2044 |
\def\GetLastItem#1#2{% |
| 2045 |
\GetItem{#1}{\number\numexpr\RL@lenof{#1}-1}{#2}% |
| 2046 |
} |
| 2047 |
% \end{macrocode} |
| 2048 |
% \end{macro} |
| 2049 |
% \begin{macro}{\GetItem} |
| 2050 |
% Get the element of rank |#2| of list |#1| and store it in |#3|. |
| 2051 |
% \begin{macrocode} |
| 2052 |
\def\GetItem#1#2#3{% |
| 2053 |
\@ifIsListNotEmpty{#1} |
| 2054 |
{% |
| 2055 |
\ifnum\numexpr\RL@lenof{#1}-1-#2<0 |
| 2056 |
\@OutOfRangeError{#1}{#2}% |
| 2057 |
\else |
| 2058 |
\RL@let{#3}{#1-#2}% |
| 2059 |
\fi |
| 2060 |
} |
| 2061 |
{\@NoListError{#1}}% |
| 2062 |
} |
| 2063 |
% \end{macrocode} |
| 2064 |
% \end{macro} |
| 2065 |
% \begin{macro}{\GetRandomItem} |
| 2066 |
% Get element in a random position of list |#1| and store it in |#2|. |
| 2067 |
% \begin{macrocode} |
| 2068 |
\def\GetRandomItem#1#2{% |
| 2069 |
\@ifIsListNotEmpty{#1}% |
| 2070 |
{% |
| 2071 |
\RLuniformdeviate{\RL@lenof{#1}}{RL@temp}% |
| 2072 |
\GetItem{#1}{\RL@temp}{#2}% |
| 2073 |
}% |
| 2074 |
{\@NoListError{#1}}% |
| 2075 |
} |
| 2076 |
% \end{macrocode} |
| 2077 |
% \end{macro} |
| 2078 |
% \begin{macro}{\GetList} |
| 2079 |
% |\GetList| copy a sub-list from a list. There are four arguments: |
| 2080 |
% \begin{itemize} |
| 2081 |
% \item |#1| is the list from which the reading is made; |
| 2082 |
% \item |#2| is the starting index of extraction; |
| 2083 |
% \item |#3| is the ending index of extraction; |
| 2084 |
% \item |#4| is the list which receive the readen items. |
| 2085 |
% \end{itemize} |
| 2086 |
% \begin{macrocode} |
| 2087 |
\def\GetList#1#2#3#4{% |
| 2088 |
% \end{macrocode} |
| 2089 |
% In order to do something, |#1| and |#2| must be lists, and indexes |
| 2090 |
% |#2| and |#3| must be inside list |#1|. |
| 2091 |
% \begin{macrocode} |
| 2092 |
\@ifIsList{#1}{% |
| 2093 |
\@ifIsList{#4}{% |
| 2094 |
\ifnum#2<\RL@lenof{#1}% |
| 2095 |
\ifnum#3<\RL@lenof{#1}% |
| 2096 |
\ifnum#2>#3\relax |
| 2097 |
% \end{macrocode} |
| 2098 |
% If $\text{start} > \text{end}$ we build an empty list. |
| 2099 |
% \begin{macrocode} |
| 2100 |
\RL@nameldef{#4-len}{0}% |
| 2101 |
\else |
| 2102 |
% \end{macrocode} |
| 2103 |
% If $\text{start} \leq \text{end}$ we build a real extracted list. |
| 2104 |
% \begin{macrocode} |
| 2105 |
\RLfor\RL@iter=#2 to #3 \do{% |
| 2106 |
\RL@let{#4-\number\numexpr \RL@iter - #2}{#1-\RL@iter}% |
| 2107 |
}% |
| 2108 |
\RL@nameledef{#4-len}{\number\numexpr #3 - #2 + 1}% |
| 2109 |
\fi |
| 2110 |
\else |
| 2111 |
\@OutOfRangeError{#1}{#3}% |
| 2112 |
\fi |
| 2113 |
\else |
| 2114 |
\@OutOfRangeError{#1}{#2}% |
| 2115 |
\fi |
| 2116 |
}% |
| 2117 |
{\@NoListError{#4}}% |
| 2118 |
}% |
| 2119 |
{\@NoListError{#1}}% |
| 2120 |
} |
| 2121 |
% \end{macrocode} |
| 2122 |
% \end{macro} |
| 2123 |
% \begin{macro}{\SetFirstItem} |
| 2124 |
% Set the first element of list |#1| with value |#2|. |
| 2125 |
% \begin{macrocode} |
| 2126 |
\long\def\SetFirstItem#1#2{% |
| 2127 |
\SetItem{#1}{0}{#2}% |
| 2128 |
} |
| 2129 |
% \end{macrocode} |
| 2130 |
% \end{macro} |
| 2131 |
% \begin{macro}{\SetLastItem} |
| 2132 |
% Set the last element of list |#1| with value |#2|. |
| 2133 |
% \begin{macrocode} |
| 2134 |
\long\def\SetLastItem#1#2{% |
| 2135 |
\SetItem{#1}{\number\numexpr\RL@lenof{#1}-1}{#2}% |
| 2136 |
} |
| 2137 |
% \end{macrocode} |
| 2138 |
% \end{macro} |
| 2139 |
% \begin{macro}{\SetItem} |
| 2140 |
% Set the |#2| element of list |#1| with value |#3|. |
| 2141 |
% \begin{macrocode} |
| 2142 |
\long\def\SetItem#1#2#3{% |
| 2143 |
\@ifIsListNotEmpty{#1}% |
| 2144 |
{% |
| 2145 |
\ifnum\numexpr\RL@lenof{#1}-1-#2<0 |
| 2146 |
\@OutOfRangeError{#1}{#2}% |
| 2147 |
\else |
| 2148 |
\RL@nameldef{#1-#2}{#3}% |
| 2149 |
\fi |
| 2150 |
}% |
| 2151 |
{\@NoListError{#1}}%% |
| 2152 |
} |
| 2153 |
% \end{macrocode} |
| 2154 |
% \end{macro} |
| 2155 |
% \begin{macro}{\SetRandomItem} |
| 2156 |
% Set element in a random position of list |#1| with value |#2|. |
| 2157 |
% \begin{macrocode} |
| 2158 |
\long\def\SetRandomItem#1#2{% |
| 2159 |
\@ifIsListNotEmpty{#1}% |
| 2160 |
{% |
| 2161 |
\RLuniformdeviate{\RL@lenof{#1}}{RL@temp}% |
| 2162 |
\SetItem{#1}{\RL@temp}{#2}% |
| 2163 |
}% |
| 2164 |
{\@NoListError{#1}}% |
| 2165 |
} |
| 2166 |
% \end{macrocode} |
| 2167 |
% \end{macro} |
| 2168 |
% \begin{macro}{\SetList} |
| 2169 |
% |\SetList| allow to give multiple values to a list. This function |
| 2170 |
% acts like a repetition of |\InsertLastItem|. |
| 2171 |
% \begin{macrocode} |
| 2172 |
\def\SetList#1#2{% |
| 2173 |
\@ifIsList{#1}% |
| 2174 |
{% |
| 2175 |
\ClearList{#1}% |
| 2176 |
\def\RL@name{#1}% |
| 2177 |
\RL@setlist#2,\@nil,% |
| 2178 |
}% |
| 2179 |
{\@NoListError{#1}}% |
| 2180 |
} |
| 2181 |
\long\def\RL@setlist#1,{% |
| 2182 |
\def\RL@arg{#1}% |
| 2183 |
\unless\ifx\RL@arg\@nnil |
| 2184 |
\InsertLastItem{\RL@name}{#1}% |
| 2185 |
\expandafter\RL@setlist |
| 2186 |
\fi |
| 2187 |
} |
| 2188 |
% \end{macrocode} |
| 2189 |
% \end{macro} |
| 2190 |
% \subsubsection{Loop on list} |
| 2191 |
% \begin{macro}{\ForEachFirstItem} |
| 2192 |
% |\ForEachFirstItem| typesets |#3| for each element of the list |#1| |
| 2193 |
% extracting the actual first element (stored in |#2|). |
| 2194 |
% \begin{macrocode} |
| 2195 |
\long\def\ForEachFirstItem#1#2#3{% |
| 2196 |
\begingroup |
| 2197 |
\RLfor \RL@var = 0 to \RL@lenof{#1}-1 \do{% |
| 2198 |
\ExtractFirstItem{#1}{#2}% |
| 2199 |
#3% |
| 2200 |
}% |
| 2201 |
\endgroup |
| 2202 |
} |
| 2203 |
% \end{macrocode} |
| 2204 |
% \end{macro} |
| 2205 |
% \begin{macro}{\ForEachLastItem} |
| 2206 |
% |\ForEachLastItem| typesets |#3| for each element of the list |#1| |
| 2207 |
% extracting the actual last element (stored in |#2|). |
| 2208 |
% \begin{macrocode} |
| 2209 |
\long\def\ForEachLastItem#1#2#3{% |
| 2210 |
\begingroup |
| 2211 |
\RLfor \RL@var = 0 to \RL@lenof{#1}-1 \do{% |
| 2212 |
\ExtractLastItem{#1}{#2}% |
| 2213 |
#3% |
| 2214 |
}% |
| 2215 |
\endgroup |
| 2216 |
} |
| 2217 |
% \end{macrocode} |
| 2218 |
% \end{macro} |
| 2219 |
% \begin{macro}{\ForEachRandomItem} |
| 2220 |
% |\ForEachRandomItem| typesets |#3| for each element of the list |#1| |
| 2221 |
% extracting randomly an element (stored in |#2|). |
| 2222 |
% \begin{macrocode} |
| 2223 |
\long\def\ForEachRandomItem#1#2#3{% |
| 2224 |
\begingroup |
| 2225 |
\RLfor \RL@var = 0 to \RL@lenof{#1}-1 \do{% |
| 2226 |
\ExtractRandomItem{#1}{#2}% |
| 2227 |
#3% |
| 2228 |
}% |
| 2229 |
\endgroup |
| 2230 |
} |
| 2231 |
% \end{macrocode} |
| 2232 |
% \end{macro} |
| 2233 |
% \subsubsection{Database} |
| 2234 |
% \begin{macro}{\ReadFieldItem} |
| 2235 |
% Macro |\ReadFieldItem| read a field in a record. |
| 2236 |
% |
| 2237 |
% A record is a sequence of groups, each group is a field. |
| 2238 |
% \begin{itemize} |
| 2239 |
% \item |#1| is the record (sequence of groups; |
| 2240 |
% \item |#2| is the index of item (starting at zero); |
| 2241 |
% \item |#3| is the macro name which store the field. |
| 2242 |
% \end{itemize} |
| 2243 |
% \begin{macrocode} |
| 2244 |
\long\def\ReadFieldItem#1#2#3{% |
| 2245 |
% \end{macrocode} |
| 2246 |
% Store the field's index. |
| 2247 |
% \begin{macrocode} |
| 2248 |
\RL@counti #2\relax |
| 2249 |
% \end{macrocode} |
| 2250 |
% Call the recursive macro |
| 2251 |
% \begin{macrocode} |
| 2252 |
\expandafter\RL@ReadFieldItem#1\@nil |
| 2253 |
% \end{macrocode} |
| 2254 |
% Store the result in macro |\#3|. |
| 2255 |
% \begin{macrocode} |
| 2256 |
\expandafter\let\csname#3\endcsname\RL@temp |
| 2257 |
} |
| 2258 |
% \end{macrocode} |
| 2259 |
% In fact the first recusive call check for a left brace. A record |
| 2260 |
% must contain at least one field otherwise an error message is |
| 2261 |
% raised. |
| 2262 |
% \begin{macrocode} |
| 2263 |
\long\def\RL@ReadFieldItem{% |
| 2264 |
\@ifnextchar\bgroup{\RL@@ReadFieldItem}{\RL@@ReadFieldItemError}% |
| 2265 |
} |
| 2266 |
% \end{macrocode} |
| 2267 |
% |
| 2268 |
% \begin{macrocode} |
| 2269 |
\long\def\RL@@ReadFieldItem#1{% |
| 2270 |
\ifnum\RL@counti=\z@ |
| 2271 |
\def\RL@temp{#1}% |
| 2272 |
\expandafter\RL@@ReadFieldItemEnd |
| 2273 |
\else |
| 2274 |
\advance\RL@counti \m@ne |
| 2275 |
\expandafter\RL@ReadFieldItem |
| 2276 |
\fi |
| 2277 |
} |
| 2278 |
\long\def\RL@@ReadFieldItemEnd#1\@nil{} |
| 2279 |
\long\def\RL@@ReadFieldItemError#1\@nil{% |
| 2280 |
\PackageError{randomlist}% |
| 2281 |
{There aren't enough fields in the record}% |
| 2282 |
{Pay attention that field number starts from zero.}% |
| 2283 |
} |
| 2284 |
% \end{macrocode} |
| 2285 |
% \end{macro} |
| 2286 |
% \begin{macro}{\ReadFileList} |
| 2287 |
% First, we look for special delimiters for fields and strings. By |
| 2288 |
% default, the delimiter for fields is the comma and the delimiter for |
| 2289 |
% string is the double quote. |
| 2290 |
% \begin{macrocode} |
| 2291 |
\def\RL@SetDelimiters#1#2#3\@nil{% |
| 2292 |
% \end{macrocode} |
| 2293 |
% \begin{itemize} |
| 2294 |
% \item argument |#1| is the field separator; |
| 2295 |
% \item argument |#2| is the string delimiter; |
| 2296 |
% \item argument |#3| is the remainder to ignore. |
| 2297 |
% \end{itemize} |
| 2298 |
% \begin{macrocode} |
| 2299 |
\def\RL@markstrings##1{% |
| 2300 |
\let\RL@accu\empty |
| 2301 |
\expandafter\RL@markstrings@i##1#2\@nil#2% |
| 2302 |
\let##1=\RL@accu |
| 2303 |
}% |
| 2304 |
\def\RL@markstrings@i##1#2##2#2{% |
| 2305 |
\RL@addtomacro\RL@accu{##1}% |
| 2306 |
\def\RL@current{##2}% |
| 2307 |
\unless\ifx\@nnil\RL@current |
| 2308 |
\RL@addtomacro\RL@accu{\RL@string{##2}}% |
| 2309 |
\expandafter\RL@markstrings@i |
| 2310 |
\fi |
| 2311 |
}% |
| 2312 |
\def\RL@unmarkstrings##1{% |
| 2313 |
\let\RL@accuA\empty |
| 2314 |
\expandafter\RL@unmarkstrings@i##1\RL@string\@nil |
| 2315 |
\let##1=\RL@accuA |
| 2316 |
}% |
| 2317 |
\def\RL@unmarkstrings@i##1\RL@string##2{% |
| 2318 |
\RL@addtomacro\RL@accuA{##1}% |
| 2319 |
\def\RL@current{##2}% |
| 2320 |
\unless\ifx\@nnil\RL@current |
| 2321 |
\RL@ifempty{##2}% |
| 2322 |
{\RL@addtomacro\RL@accuA{#2}}% |
| 2323 |
{\RL@addtomacro\RL@accuA{##2}}% |
| 2324 |
\expandafter\RL@unmarkstrings@i |
| 2325 |
\fi |
| 2326 |
}% |
| 2327 |
\def\RL@parsefields##1{% |
| 2328 |
\let\RL@accu\empty |
| 2329 |
\expandafter\RL@parsefields@i##1#1\@nil#1% |
| 2330 |
\let##1=\RL@accu |
| 2331 |
}% |
| 2332 |
\def\RL@parsefields@i##1#1{% |
| 2333 |
\def\RL@current{##1}% |
| 2334 |
\unless\ifx\@nnil\RL@current |
| 2335 |
\RL@unmarkstrings\RL@current |
| 2336 |
\RL@removefirstspaces\RL@current |
| 2337 |
\RL@removelastspaces \RL@current |
| 2338 |
\expandafter\RL@addtomacro\expandafter\RL@accu\expandafter |
| 2339 |
{\expandafter{\RL@current}}% |
| 2340 |
\expandafter\RL@parsefields@i |
| 2341 |
\fi |
| 2342 |
}% |
| 2343 |
} |
| 2344 |
% \end{macrocode} |
| 2345 |
% The macro |\ReadFileList| uses a handle for the reading file. it |
| 2346 |
% needs also a macro to detect |\par| |
| 2347 |
% \begin{macrocode} |
| 2348 |
\newread\RL@hdle |
| 2349 |
\def\@ppar{\par} |
| 2350 |
% \end{macrocode} |
| 2351 |
% At first, |\ReadFileList| check for an optionnal argument giving |
| 2352 |
% delimiters. By default, delimiters are comma for field separator and |
| 2353 |
% double quote for string delimiter. |
| 2354 |
% \begin{macrocode} |
| 2355 |
\def\ReadFileList{\@ifnextchar[{\@ReadFileList}{\@ReadFileList[,"]}} |
| 2356 |
% \end{macrocode} |
| 2357 |
% \begin{itemize} |
| 2358 |
% \item |#1| contains the delimiters; |
| 2359 |
% \item |#2| is the data base name; |
| 2360 |
% \item |#3| is the file name. |
| 2361 |
% \end{itemize} |
| 2362 |
% \begin{macrocode} |
| 2363 |
\def\@ReadFileList[#1]#2#3{% |
| 2364 |
\openin \RL@hdle = #3 |
| 2365 |
\ifeof\RL@hdle |
| 2366 |
\PackageError{randomlist}% |
| 2367 |
{File #3 doesn't exist}% |
| 2368 |
{Verify its name, its extension, its location, its permissions.}% |
| 2369 |
\else |
| 2370 |
% \end{macrocode} |
| 2371 |
% If the optionnal argument is empty then raise an error and take the |
| 2372 |
% comma and the double quote instead. |
| 2373 |
% \begin{macrocode} |
| 2374 |
\RL@ifempty{#1}% |
| 2375 |
{% |
| 2376 |
\PackageError{randomlist} |
| 2377 |
{Optional argument empty: [,"] inserted} |
| 2378 |
{Do not leave an optional argument empty}% |
| 2379 |
\RL@SetDelimiters,"\@nil |
| 2380 |
} |
| 2381 |
% \end{macrocode} |
| 2382 |
% Else add double quote to for security. |
| 2383 |
% \begin{macrocode} |
| 2384 |
{\RL@SetDelimiters#1"\@nil}% |
| 2385 |
% \end{macrocode} |
| 2386 |
% The main loop read each line of the file. Don't process anything if |
| 2387 |
% the line is empty (it could be the very end of the file). |
| 2388 |
% \begin{macrocode} |
| 2389 |
\loop |
| 2390 |
\read\RL@hdle to \RL@buffer |
| 2391 |
\unless\ifx\RL@buffer\@ppar |
| 2392 |
% \end{macrocode} |
| 2393 |
% Mark the string |
| 2394 |
% \begin{macrocode} |
| 2395 |
\RL@markstrings\RL@buffer |
| 2396 |
% \end{macrocode} |
| 2397 |
% and process the fields. |
| 2398 |
% \begin{macrocode} |
| 2399 |
\RL@parsefields\RL@buffer |
| 2400 |
% \end{macrocode} |
| 2401 |
% Save current record with fields, that is, with sequence of groups. |
| 2402 |
% \begin{macrocode} |
| 2403 |
\def\RL@accuA{\InsertLastItem{#2}}% |
| 2404 |
\expandafter\RL@accuA\expandafter{\RL@buffer}% |
| 2405 |
\fi |
| 2406 |
\ifeof\RL@hdle\else |
| 2407 |
\repeat |
| 2408 |
\fi |
| 2409 |
} |
| 2410 |
% \end{macrocode} |
| 2411 |
% Check for a heading space. |
| 2412 |
% \begin{macrocode} |
| 2413 |
\def\RL@ifspacefirst#1{% |
| 2414 |
\RL@ifspacefirst@i#1A \@nil |
| 2415 |
} |
| 2416 |
\expandafter\def\expandafter\RL@ifspacefirst@i |
| 2417 |
\expandafter#\expandafter1\space#2\@nil{% |
| 2418 |
\RL@ifempty{#1}% |
| 2419 |
} |
| 2420 |
% \end{macrocode} |
| 2421 |
% \begin{macrocode} |
| 2422 |
% Remove all spaces at the start of argument (macro). |
| 2423 |
\def\RL@removefirstspaces#1{% |
| 2424 |
\expandafter\RL@ifspacefirst\expandafter{#1} |
| 2425 |
{\expandafter\removefistspace@i#1\@nil#1} |
| 2426 |
{}% |
| 2427 |
} |
| 2428 |
\expandafter\def\expandafter\removefistspace@i\space#1\@nil#2{% |
| 2429 |
\def#2{#1}% |
| 2430 |
\RL@removefirstspaces#2% |
| 2431 |
} |
| 2432 |
% \end{macrocode} |
| 2433 |
% \begin{macrocode} |
| 2434 |
% Store |^^00|'s catcode |
| 2435 |
\edef\RL@restorecatcodezero{\catcode0=\number\catcode0\relax} |
| 2436 |
% \end{macrocode} |
| 2437 |
% then set this catcode to other catcode. |
| 2438 |
% \begin{macrocode} |
| 2439 |
% puis le modifie à 12. |
| 2440 |
\catcode0=12 |
| 2441 |
% \end{macrocode} |
| 2442 |
% Remove all heading and trailing spaces of argument (macro). |
| 2443 |
% \begin{macrocode} |
| 2444 |
\def\RL@removelastspaces#1{% |
| 2445 |
\expandafter\def\expandafter#1\expandafter{% |
| 2446 |
\romannumeral\expandafter |
| 2447 |
\RL@removelastspaces@i\expandafter\relax#1^^00 ^^00\@nil |
| 2448 |
}% |
| 2449 |
} |
| 2450 |
\def\RL@removelastspaces@i#1 ^^00{\RL@removelastspaces@ii#1^^00} |
| 2451 |
\def\RL@removelastspaces@ii#1^^00#2\@nil{% |
| 2452 |
\RL@ifspacefirst{#2} |
| 2453 |
{\RL@removelastspaces@i#1^^00 ^^00\@nil} |
| 2454 |
{\expandafter\z@\@gobble#1}% |
| 2455 |
} |
| 2456 |
% \end{macrocode} |
| 2457 |
% \begin{macrocode} |
| 2458 |
% Restore |^^00|'s catcode. |
| 2459 |
\RL@restorecatcodezero |
| 2460 |
% \end{macrocode} |
| 2461 |
% \end{macro} |
| 2462 |
% At the very end of the package, we restore the |@|'s catcode. |
| 2463 |
% \begin{macrocode} |
| 2464 |
\catcode`\@=\RLAtCatcode\relax |
| 2465 |
% \end{macrocode} |
| 2466 |
% \iffalse |
| 2467 |
%</tex> |
| 2468 |
% \fi |