summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/getitems/getitems.dtx
blob: f5dc7c12bacdb3b3f811aa36ebf7821a0a0005b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
% \iffalse meta-comment
%
% getitems.dtx
% Copyright 2016 by Anders O.F. Hendrickson (anders.hendrickson@snc.edu)
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Anders O.F. Hendrickson.
%
% This work consists of the files moodle.dtx and moodle.ins
% and the derived files moodle.sty and getitems.sty.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{getitems.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{getitems}
%<*package>
    [2016/01/11 v1.0 gather items from a list]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{getitems}
\usepackage{fancyvrb}
\usepackage{amssymb}
\usepackage{xcolor}
\EnableCrossrefs         
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{getitems.dtx}
  \PrintChanges
  %\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{212}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.0}{2016/01/11}{Initial version}
%
% \GetFileInfo{getitems.sty}
%
% \DoNotIndex{\newcommand,\newenvironment,\def}
% 
%
% \title{The \textsf{getitems} package: \\
%        gathering {\tt\string\item}'s from a list-like environment\thanks{This document
%                corresponds to \textsf{getitems.sty}~\fileversion, dated \filedate.}}
% \author{Anders Hendrickson\\ St.~Norbert College, De~Pere, WI, USA \\ \texttt{anders.hendrickson@snc.edu}}
%
% 
% \maketitle
%
% \section{Overview}
%
% The |enumerate| and |itemize| environments of \LaTeX\ organize their contents
% through the use of the |\item| command.  Each entry in these lists is prefaced
% with the command |\item|, making for very compact and easily readable source 
% code.  Package designers may find it useful to use the same syntax for
% their custom environments.  The \textsf{getitems} package makes
% it easy to code such environments by parsing a string of tokens, separating them
% by the occurrence of |\item|'s, and saving the contents as macros.  
% Nested environments are handled correctly.
% 
% Moreover, some typesetting tasks naturally consist of a ``header'' followed by 
% several related items; one example would be a multiple-choice question on a 
% school examination.  This package saves any \TeX\ tokens appearing before the
% first |\item| as the zeroth item for special handling.
% 
% \section{Usage}
% 
% \DescribeMacro{\gatheritems}
% To parse a string of text, such as the body of an environment,
% call 
% \begin{center}
%   |\gatheritems|\marg{text to parse}.
% \end{center}
% This will scan through the \meta{text to parse}, dividing it at each |\item|
% while respecting \TeX\ groupings and nested environments, 
% and store the divided portions of text into memory.
% 
% \DescribeMacro{numgathereditems}
% The total number of items in the parsed text is stored in the \LaTeX\ counter
% |numgathereditems|.
% 
% \DescribeMacro{\gathereditem}
% To retrieve a stored item, you may call |\gathereditem|\marg{item number};
% the \meta{item number} should expand to an arabic representation of a nonnegative integer.
% Any tokens occurring before the first |\item| may be retrieved with
% |\gathereditem{0}|.
% 
% \DescribeMacro{\loopthroughitemswithcommand}
% Once the items are gathered, it will probably be necessary to loop through
% all of them.  
% Of course a package author can do so manually, but |getitems| provides a 
% built-in way to do so by calling |\loopthroughitemswithcommand|\marg{macro}.
% The \meta{macro} must be a control sequence taking exactly one argument;
% it will be called successively with the item text.
% For example,
% \begin{quote}
% \begin{tabular}{p{3in}p{1.5in}}
% \begin{minipage}{3in}
%   \begin{Verbatim}[gobble=6]
%     \gatheritems{%
%       Zero
%       \item One
%       \item Two
%       \item Three
%     }
%     \loopthroughitemswithcommand{\fbox}
%   \end{Verbatim}
% \end{minipage}
% &
% \begin{minipage}{1.5in}
%     \gatheritems{%
%       Zero
%       \item One
%       \item Two
%       \item Three
%     }
%     \loopthroughitemswithcommand{\fbox}
% \end{minipage} 
% \end{tabular}
% \end{quote}
% The result is the same as processing |\fbox{One}|, then |\fbox{Two}|, and finally |\fbox{Three}|.
% Note that |\loopthroughitemswithcommand| deliberately ignores the zeroth entry, which occurs
% before the first |\item|.
% 
% \DescribeMacro{currentitemnumber}
% Typically the package author will create a custom macro to process each item.
% This macro may make use of the index of the loop, which is stored in the 
% \LaTeX\ counter |currentitemnumber|.
% \DescribeMacro{\ifgatherbeginningofloop}
% A conditional |\ifgatherbeginningofloop| is also available,
% which only evaluates as true when
% processing the first item; it is thus functionally equivalent to
% |\ifnum1=\c@currentitemnumber|.
% The custom macro may take advantage of this to run special code for the first item only.
% 
% 
% \section{Example}
% An example using |getitems| to create a custom environment may be informative.
% We use the |\NewEnviron| command from the |environ| package 
% (automatically loaded by |getitems|) to define a |question| environment;
% the body between the |\begin{question}| and |\end{question}| is available as |\BODY|.
% \medskip
% 
% \par \noindent
% \begin{tabular}{p{3in}p{1.5in}}
% \begin{minipage}{3in}
%   \small
%   \begin{Verbatim}[gobble=6]
%     \def\doitem#1{\item #1\hfill $\Box$}%
%     \NewEnviron{question}{%
%       \expandafter\gatheritems\expandafter{\BODY}%
%       \gathereditem{0}%
%       \begin{itemize}
%         \loopthroughitemswithcommand{\doitem}
%       \end{itemize}
%     }
%     \begin{question}
%       Who proved the unsolvability of the quintic?
%       \item Abel
%       \item Galois
%       \item Lie
%     \end{question}
%   \end{Verbatim}
% \end{minipage}
% &
% \begin{minipage}{1.5in}
%     \def\doitem#1{\item #1\hfill $\Box$}%
%     \NewEnviron{question}{%
%       \expandafter\gatheritems\expandafter{\BODY}%
%       \gathereditem{0}%
%       \begin{itemize}
%         \loopthroughitemswithcommand{\doitem}
%       \end{itemize}
%     }
%     \begin{question}
%       Who proved the unsolvability of the quintic?
%       Check the appropriate box.
%       \item Abel
%       \item Galois
%       \item Lie
%     \end{question}
% \end{minipage} 
% \end{tabular}
% \medskip
% 
% 
% This second example shows that nested environments are handled as expected.
% \medskip
% 
% \par \noindent
% \begin{tabular}{p{3in}p{2in}}
% \begin{minipage}{3in}
%   \small
%   \begin{Verbatim}[gobble=6]
%     \def\doitem#1{\item[$\Box$] 
%                   \fbox{\parbox[t]{1.75in}{#1}}}%
%     \NewEnviron{question}{%
%       \expandafter\gatheritems\expandafter{\BODY}%
%       \gathereditem{0}%
%       \begin{itemize}
%         \loopthroughitemswithcommand{\doitem}
%       \end{itemize}
%     }
%     \begin{question}
%       Who proved the unsolvability of the quintic?
%       Check the appropriate box.
%       \item Abel
%             \begin{itemize}
%               \item Born August 5, 1802
%               \item Died April 6, 1829
%             \end{itemize}
%       \item Galois
%             \begin{itemize}
%               \item Born October 25, 1811
%               \item Died May 31, 1832
%             \end{itemize}
%       \item Lie
%             \begin{itemize}
%               \item Born December 17, 1842
%               \item Died February 18, 1899
%             \end{itemize}
%     \end{question}
%   \end{Verbatim}
% \end{minipage}
% &
% \begin{minipage}{2.5in}
%     \def\doitem#1{\item[$\Box$] \fbox{\parbox[t]{1.75in}{#1}}}%
%     \NewEnviron{question}{%
%       \expandafter\gatheritems\expandafter{\BODY}%
%       \gathereditem{0}%
%       \begin{itemize}
%         \loopthroughitemswithcommand{\doitem}
%       \end{itemize}
%     }
%     \begin{question}
%       Who proved the unsolvability of the quintic?
%       Check the appropriate box.
%       \item Abel
%             \begin{itemize}
%               \item Born August 5, 1802
%               \item Died April 6, 1829
%             \end{itemize}
%       \item Galois
%             \begin{itemize}
%               \item Born October 25, 1811
%               \item Died May 31, 1832
%             \end{itemize}
%       \item Lie
%             \begin{itemize}
%               \item Born December 17, 1842
%               \item Died February 18, 1899
%             \end{itemize}
%     \end{question}
% \end{minipage} 
% \end{tabular}
% 
% 
% \StopEventually{}
%
% \section{Implementation}
% 
% We need the |trimspaces| package to remove excess spaces
% from the items we find.
% Although the |environ| package is not used by |getitems| itself,
% it will almost certainly be needed.
%    \begin{macrocode}
\RequirePackage{environ}
\RequirePackage{trimspaces}
\let\xa=\expandafter
%    \end{macrocode}
%  \begin{macro}{\gathereditem}
%    The $k$th item found will be stored in the macro |\getitems@item@|\meta{k};
%    the user can access it through the |\gathereditem| macro.
%    \begin{macrocode}
\def\gathereditem#1{\csname getitems@item@#1\endcsname}
%    \end{macrocode}
%  \end{macro}
%  
%  \begin{macro}{numgathereditems}
%    We define the \LaTeX\ counter |numgathereditems|.
%    \begin{macrocode}
\newcounter{numgathereditems}
%    \end{macrocode}
%  \end{macro}
%  
%  \begin{macro}{\gatheritems}
%    The main control sequence of this package is |\gatheritems|.
%    The na\"ive strategy is to use the delimiter mechanism of \TeX\ to
%    split the text at the first occurrence of the token ``|\item|.''
%    We add |\getitems@relax| before, 
%    and ``|\item\getitems@terminalitem|'' after, the text to help us
%    detect empty items and prevent
%    errors after we have found all the genuine |\item|'s.
%    \begin{macrocode}
\long\def\gatheritems#1{%
  \setcounter{getitems@begindepth}{0}%
  \setcounter{numgathereditems}{0}%
  \xa\long\xa\gdef\csname getitems@item@0\endcsname{}%
  \gatheritems@int\getitems@relax#1\item\getitems@terminalitem\getitems@endgatheritems
  \xa\let\xa\gatheredheader\xa=\csname getitems@item@0\endcsname
}
%    \end{macrocode}
%  \end{macro}
%  The trouble with the na\"ive strategy is that it won't handle nested
%  environments correctly.  To do that, we need to keep track of how deeply
%  nested we are with the macro |\getitems@trackbegindepth|, defined below.
%  That macro stores its results in the \LaTeX\ counter |getitems@begindepth|;
%  a value of 0 indicates the top-level within the argument of |\gatheritems|.
%    \begin{macrocode}
\def\@getitems@terminalitem{\getitems@terminalitem}%
\def\@dummy@relax{\getitems@relax}%
\long\def\gatheritems@int#1\item#2\getitems@endgatheritems{%
  \getitems@trackbegindepth{#1}%
  \ifnum\c@getitems@begindepth=0\relax
%    \end{macrocode}
% At this point we have gathered a complete |\item|; we have not stopped 
% accidentally at a sub|\item|.  The original |\item| might have had no content,
% in which case |#1| will be simply ``|\getitems@relax|'', and we do nothing;
% otherwise we strip off the |\getitems@relax| and store those tokens in 
% |\getitems@item@|\meta{numgathereditems}.
%    \begin{macrocode}
    \def\getitems@test@i{#1}%
    \ifx\getitems@test@i\@dummy@relax
      \relax
    \else
      \xa\xa\xa\g@addto@macro
         \xa\xa\csname getitems@item@\the\c@numgathereditems\endcsname
            \xa{\getitems@stripfirsttokenfrom#1\getitems@endstrip}%
    \fi
%    \end{macrocode}
% Now we test whether we have reached the end of the text to be parsed.
% This is the case if |#2| is simply |\getitems@terminalitem|,
% and we stop the recursion.
% Otherwise there is at least one more |\item| to process, so we increment |numgathereditems|,
% prepare |\getitems@item@|\meta{k+1},
% and prepare to recurse.
%    \begin{macrocode}
    \def\getitems@test@ii{#2}%
    \ifx\getitems@test@ii\@getitems@terminalitem
      \let\getitems@next=\relax
    \else
      \stepcounter{numgathereditems}%
      \xa\gdef\csname getitems@item@\the\c@numgathereditems\endcsname{}%
      \def\getitems@next{\gatheritems@int\getitems@relax#2\getitems@endgatheritems}%
    \fi
  \else
%    \end{macrocode}
% We are now in the case where |getitems@begindepth|${}\neq 0$.
% This essentially means that the text in |#1| has more |\begin|'s than |\end|'s,
% so we have not read a complete |\item|; we stopped at an ``|\item|'' token
% within a sub-environment.
% We save the text gathered so far to |\getitems@item@|\meta{k},
% including the |\item| we parsed by mistake,
% and then call |\gatheritems@int| again to sweep up more tokens.
%    \begin{macrocode}
    \xa\xa\xa\g@addto@macro
       \xa\xa\csname getitems@item@\the\c@numgathereditems\endcsname
          \xa{\getitems@stripfirsttokenfrom#1\getitems@endstrip}%
    \xa\g@addto@macro\csname getitems@item@\the\c@numgathereditems\endcsname{\item}%
    \def\getitems@next{\gatheritems@int\getitems@relax#2\getitems@endgatheritems}%
  \fi
  \getitems@next
}
%    \end{macrocode}
% This next macro is used by |\gatheritems@int| to strip off a 
% dummy |\getitems@relax| token from the beginning of its first parameter.
%    \begin{macrocode}
\long\def\getitems@stripfirsttokenfrom#1#2\getitems@endstrip{#2}
%    \end{macrocode}
% Here is the code used to track the depth of nesting of |\begin|'s in a text.
%    \begin{macrocode}
\newcounter{getitems@begindepth}
\long\def\getitems@trackbegindepth#1{%
  \getitems@trackbegindepth@int#1\getitems@terminalbegindepth\getitems@endtrackbegindepth
}
\def\@getitems@begin{\begin}%
\def\@getitems@end{\end}%
\def\@getitems@terminalbegindepth{\getitems@terminalbegindepth}%
\long\def\getitems@trackbegindepth@int#1#2\getitems@endtrackbegindepth{%
  \def\getitems@test@i{#1}%
  \ifx\getitems@test@i\@getitems@begin
    \advance\c@getitems@begindepth by 1\relax
  \else
    \ifx\getitems@test@i\@getitems@end
      \advance\c@getitems@begindepth by -1\relax
    \fi
  \fi
  \def\getitems@test@ii{#2}%
  \trim@spaces@in\getitems@test@ii
  \ifx\getitems@test@ii\@getitems@terminalbegindepth
    \let\getitems@trackbegindepth@next=\relax
  \else
    \def\getitems@trackbegindepth@next{%
      \getitems@trackbegindepth@int#2\getitems@endtrackbegindepth}%
  \fi
  \getitems@trackbegindepth@next
}
%    \end{macrocode}
% \begin{macro}{\loopthroughitemswithcommand}
%   Finally, we define the user-level command to loop through
%   the gathered items from 1 through |numgathereditems|.
%    \begin{macrocode}
\newif\ifgatherbeginningofloop
\newcounter{currentitemnumber}
\def\loopthroughitemswithcommand#1{%
  \setcounter{currentitemnumber}{1}%
  \gatherbeginningoflooptrue
  \loopthroughitemswithcommand@int{#1}%
}

\def\loopthroughitemswithcommand@int#1{%
  \ifnum\c@currentitemnumber>\c@numgathereditems\relax
    \let\getitems@loop@next=\relax%
  \else
    \xa\xa\xa#1\xa\xa\xa{\csname getitems@item@\the\c@currentitemnumber\endcsname}%
    \def\getitems@loop@next{\loopthroughitemswithcommand@int{#1}}%
    \stepcounter{currentitemnumber}%
  \fi
  \gatherbeginningofloopfalse
  \getitems@loop@next
}
%    \end{macrocode}
% \end{macro}
%
% \Finale
\endinput