Debian Codegraph @

texmf-dist/source/generic/dowith/dowith.tex

raw
1 \ProvidesFile{dowith.tex}[2015/11/14 documenting dowith.sty]
2 \title{%%%\kern-\baselineskip
3 \textsf{\huge dowith.sty}\\---\\Apply Command to
4 % Elements of Lists without Separators\,---\,%%% 2012/05/14
5 % and without Iterator\thanks{This
6 %% 2012/05/15, "in" 2012/05/18:
7 Each Item \\ in a List of Arguments in ``\TeX's Gullet"\thanks{This
8 document describes version
9 \textcolor{blue}{\UseVersionOf{\jobname.sty}}
10 of \textsf{\jobname.sty} as of \UseDateOf{\jobname.sty}.}}
11 { \RequirePackage{makedoc} \ProcessLineMessage{}
12 \renewcommand*\mdSectionLevelOne{\string\subsection}
13 \renewcommand*\mdSectionLevelTwo{\string\subsubsection}
14 \MakeJobDoc{18}%% 2012/11/05 for v0.3
15 {\SectionLevelTwoParseInput} }
16 % \RequirePackage[ir]{inputtrc}
17 \documentclass[fleqn]{article}%% TODO paper dimensions!?
18 \input{makedoc.cfg} %% shared formatting settings
19 \ifpdf\else \errhelp{hyperref draft bad with {equation}}
20 \errmessage{run this with pdflatex only}\fi
21 % \ReadPackageInfos{dowith}
22 \usepackage{dowith} %% 2012/05/17b
23 \sloppy
24 \MDkeywords{programming structures; %% 2012/05/14b
25 macro programming, loops, list macros}
26 \hypersetup{%
27 pdftitle=dowith.sty handles lists without separators,
28 pdfsubject=documenting dowith.sty
29 }%% 2011/08/22
30 \usepackage{fixltx2e} %% \textsubscript 2012/05/17b
31 %% <- TODO with `lmodern'?
32 % \newcommand*{\ctanpkgdref}[1]{% %% 2012/11/19
33 % \ctanpkgref{#1}\,\foothttpurlref{ctan.org/pkg/#1}}
34 %% <- in `makedoc.cfg' 2015/11/14
35 \makeatletter %% etc. 2012/05/17b
36 %% TODO `actcodes'!? 2012/11/02
37 \newcommand*{\GetOtherChar}[2]{%
38 \@ifdefinable#1{%
39 \edef#1{\expandafter\@gobble\string#2}}}
40 \makeatother
41 \GetOtherChar\codeLB\{
42 \GetOtherChar\codeRB\}
43 % \GetOtherChar\codeSP\ % %% ???
44 {\MakeOther\ \xdef\codeSP{ }}
45 \newcommand*{\codelb}{\code\codeLB}
46 \newcommand*{\coderb}{\code\codeRB}
47 \newcommand*{\codesp}{\code\codeSP}
48 \newcommand*{\codebd}[1]{\code{\codeLB#1\codeRB}} %% 2012/05/19
49 %% rm. 2012/05/17b:
50 % \DeclareRobustCommand*{\code}[1]{%
51 % \texttt{%
52 % \let\{\codeLB \let\}\codeRB \let\ \codeSP
53 % #1}}
54 % \newcommand*{\chtok}[2]{\ensuremath{\code{#1}\sb{#2}}}
55 %% <- 2012/05/17b ->
56 \newcommand*{\chtok}[2]{\code{#1}\textsubscript{#2}}
57 \newcommand*{\lbtok}{\chtok\codeLB{1}}
58 \newcommand*{\rbtok}{\chtok\codeRB{2}}
59 \newcommand*{\lttok}[1]{\chtok{#1}{11}}
60 \newcommand*{\sptok}{\chtok\codeSP{10}}
61 \providecommand*{\TTb}{\meta{The\nolinebreak[3] \TeX book}}
62 \providecommand*{\TTbp}{\TTb\nolinebreak[3] p}
63 \newenvironment*{smallpar}
64 {\medskip\par\begingroup\footnotesize}
65 {\par\endgroup\medskip}
66 \newcommand*{\NTOK}[1]{\textsf{ntok}(\code{#1})}
67 \newcommand*{\ntok}[1]{\textrm{?}\code{#1}}
68 \newenvironment*{example}[1]
69 {\trivlist\item
70 (\ulroman{#1})}
71 {\endtrivlist}
72 \providecommand*{\ulroman}[1]{\meta{\romannumeral #1 }}
73 \newcommand*{\inlineitem}[1]{\ (\ulroman{#1})\enspace
74 \ignorespaces}
75 \newcommand*{\pdots}{~.\kern\fontdimen3\font
76 .\kern\fontdimen3\font. }
77 \providecommand*{\Chi}{\mathrm{X}}
78 \renewcommand*{\httpprefix}{\theHTTPprefix}
79 \begin{document}
80 \maketitle
81 \begin{MDabstract}
82 This package provides macros for applying a
83 %% mod. 2012/05/15:
84 % ``command" <cmd> to all items of a list $<arg-1><arg-2>\dots<arg-$n$>$
85 ``command" to all items in a ``list of possible macro arguments," %% v0.21a
86 % in ``\TeX's mouth,"
87 % such as `\DoWithAllIn{<cmd>}{<list-macro>}',
88 and also for extending and reducing macros storing such lists.
89 ``Brace groups" are single items of such lists, as opposed to
90 token lists. Iteration is implemented within \TeX's expansion
91 processor, so works within `\write' as with 'blog.sty'.
92 %%% ---pleading for mathematical rigour in \TeX ology!) %% rm. 2012/05/17b
93 % Applications in mind belonged to \LaTeX, but the package should work
94 % with other formats as well.
95 Loop and list macros in other packages are discussed in the documentation.
96 % %% 2012/05/09:
97 % There is an emphasis on expandability
98 % %% mod./add. 2012/05/15:
99 % (no \wikienref{Iterator}{iterator;}
100 % essential within \cs{write} as with \CtanPkgRef{morehype}{blog.sty}),
101 % without relying on \CtanPkgRef{e-tex}{$\varepsilon$-\TeX}.
102 %% 2012/05/18:
103 % Iteration is implemented within ``\TeX's mouth," so works within
104 % `\write' as with \CtanPkgRef{morehype}{blog.sty}.
105 There is no need for \CtanPkgRef{e-tex}{$\varepsilon$-\TeX} to which
106 some of them refer.
107
108 The package is ``generic," i.e., should also work with Plain \TeX\
109 or even other formats, relying on the \ctanpkgref{plainpkg} package
110 for some minimal \LaTeX-like behaviour. %% \ 2012/11/27
111
112 \MDaddtoabstract{Related packages}
113 \let\pkg\ctanpkgref \pkg{catoptions},
114 \pkg{etextools}, \pkg{etoolbox}, \pkg{forarray}, \pkg{forloop},
115 \pkg{loops}, \pkg{multido}, \pkg{moredefs}, \pkg{lmake},
116 \pkg{texapi}, \pkg{xfor}, \pkg{xspace}
117 \end{MDabstract}
118 \tableofcontents
119
120 % \newpage
121 \section{Usage and Features} %% restructured 2012/05/16
122
123 \subsection{Installing and Calling}
124 The file 'dowith.sty' is provided ready, installation only requires
125 putting it somewhere where \TeX\ finds it
126 (which may need updating the filename data
127 base).\urlfoot{ukfaqref}{inst-wlcf} %% corr. 2011/02/08
128 The packages \ctanpkgdref{plainpkg}
129 and 'stacklet' (\ctanpkgref{catcodes})\urlfoot{ctanpkgref}{catcodes}
130 must be installed as well.
131
132 As to calling (loading): 'dowith' is a ``\pkg{plainpkg} package"
133 in the sense of the \ctanpkgref{plainpkg}
134 documentation that you may consult for details.
135 So roughly,
136 \begin{itemize}
137 \item load it by \ |\usepackage{dowith}| \ if you can,
138 \item otherwise by \ |\RequirePackage{dowith}| \\
139 (perhaps from within another ``\pkg{plainpkg} package"),
140 \item or by \ |\input dowith.sty|
141 \item or even by \ |\input{dowith.sty}|~\dots
142 \end{itemize}
143
144 % \section{Example}
145
146 % \section{Discussion} %% 2012/05/16
147 % \subsection{What It Seems to Do \dots}
148 % The 'dowith' package provides tools to simplify \TeX\ macro programming.
149 % Understanding it \emph{really} may require understanding certain passages of
150 % \TTb, such as pp.~38f. It may even require overcoming
151 % terrible confusions in \TTb.
152 % Let this be a last try at understanding the package without understanding \TeX:
153 % It allows you to abbreviate
154 % \[<cmd><arg-1><cmd><arg-2>\dots<cmd><arg-$n$>\]
155 % by
156 % \[`\DoWith<cmd><arg-1><arg-2>\dots<arg-$n$>\StopDoing'\]
157 % or by
158 % \[`\DoWithAllOf<cmd>{<arg-1><arg-2>\dots<arg-$n$>}'\]
159 %
160 % \pagebreak %% 2012/05/19
161 \subsection{What It Does With What Lists}
162 % \subsection{What It Actually Does \dots}
163 \label{sec:lists-intuit}
164
165 The single commands that the package provides are described
166 in the \hyperref[sec:implement]{implementation section} below.
167 What follows here is some general background about how the
168 commands work.
169 %% <- moved down from previous section, mod. 2012/05/19
170
171 The term \qtd{list} may refer to various things and need clarification here.
172
173 First of all, we are not referring to \LaTeX\ `list' environments
174 such as `enumerate' or `itemize';
175 neither to ``\acro{TODO}" lists of what needs to be done soon.
176
177 Rather, 'dowith' allows you to abbreviate
178 \[<cmd><arg-1><cmd><arg-2>\dots<cmd><arg-$n$>\]
179 by
180 \[`\DoWith<cmd><arg-1><arg-2>'\dots<arg-$n$>`\StopDoing'\]
181 or by
182 \[`\DoWithAllOf<cmd>{<arg-1><arg-2>'\dots<arg-$n$>`}'\]
183 With small $n$, one may doubt whether this really is an abbreviation~\dots;
184 anyway,
185 \[<arg-1><arg-2>\dots<arg-$n$>\]
186 was an attempt to refer to the kind of lists we are dealing with.
187 \[<arg-1>, <art-2>, \dots, <arg-$n$>\]
188 are the ``items" of the list.
189 The question is: what counts as an item?
190
191 We might say that `aa' is a list of \emph{two} items,
192 <arg-1> being `a' and <arg-2> being `a', too.
193
194 When we do \emph{three} keystrokes to get `a a' instead of `aa',
195 we still have \emph{two} items,
196 <arg-1> being `a' and <arg-2> being `a' too.
197 Strange, isn't it?
198
199 Also, when in `aa' we replace the first `a' by a backslash, `\',
200 we get `\a', and this is a list of a \emph{single} item,
201 $<arg-1>=`\a'$~\dots
202
203 You shouldn't believe these stories of mine entirely.
204 What I am alluding to is that the \emph{``items"} 'dowith' is about
205 are determined in terms of \emph{\TeX's tokens}, and the relation
206 between the ``characters you type" and \emph{\TeX's tokens}
207 is not entirely straightforward.
208
209 \subsection{The Notion of Arglists for \LaTeX\ Users}
210 %% <- 2012/05/17b ->
211 % \subsection{The Notion of Arglist}
212 \label{sec:arglists-intuit}
213 Still, it may suffice to clarify what counts as an <arg-$i$>
214 without speaking of \emph{tokens} explicitly: It is simply
215 what a \emph{one-parameter macro}
216 (where the parameter is \emph{not delimited} in terms of
217 \TTbp p.~203f.)
218 can take as an \emph{argument.}
219
220 The lists 'dowith' is about then are lists \emph{of possible arguments}
221 in the previous sense---let me call them \emph{``arglists."}\footnote{Not
222 to be confused with German \httpref{de.wiktionary.org/wiki/Arglist}{Arglist.}}
223 The single \emph{items} of such lists are those single possible arguments.
224 They become \emph{actual} arguments beginning from the leftmost \emph{possible} one
225 when 'dowith' presents them to that <cmd> mentioned earlier---where
226 <cmd> \emph{should} be a one-parameter macro (or some \TeX\ primitive
227 parsing arguments similarly).
228
229 The reader perhaps has an \emph{intuitive} understanding of
230 what can be an argument of a one-parameter macro.
231 A \emph{strict \LaTeX} user may think that such an argument <arg-$i$>
232 just has form `{'<ark-$i$>`}', i.e., $<arg-$i$>=`{'<ark-$i$>`}'$
233 for some <ark-$i$>. Such arguments are also called \emph{``brace groups".}
234 (\LaTeX's \emph{optional} arguments `[<extra>]'
235 do not count as possible arguments here, they are not macro arguments
236 in the sense of \TTb.)
237 In this restricted \LaTeX\ sense, arglists consist of brace groups
238 \[`{<ark-1>}{<ark-2>}'\dots`{'<ark-$n$>`}',\]
239 and each single brace group is an \emph{item} of it.
240
241 The \emph{\TeX\ macro writer}, by contrast, knows that a macro argument
242 doesn't need outer braces. In an intuitive sense, a single
243 ``command" can be a macro argument, too. ``Command" may be understood
244 as ``control sequence" (starting with a backslash),
245 but some authors also have considered single \emph{characters}
246 (character \emph{tokens}?) ``commands." Blank spaces, by contrast,
247 are ignored when a macro looks for its argument. %% 2012/05/17b
248
249 %% useless, just name items 2012/05/17b:
250 % We arrive at an ``intuitive recursive definition" of ``arglist:" \
251 % (\meta{i})\enspace The empty list is an arglist; it doesn't have an item
252 % for 'dowith'. \ (\meta{ii})\enspace If <list> is an arglist, then
253 % (a)~`{<ark>}<list>' is an arglist whose first item for 'dowith' is `{<ark>}';
254 % (b)~`<cs><list>' with a ``control sequence" <cs>
255 % is an arglist whose first item is `<cs>'
256 % (for the command <cmd> to which 'dowith' presents <cs>); and
257 % (c)~`<char><list>' with a non-blank character <char>
258 % is an arglist whose first item is `<char>'. \
259 % (\meta{iii})\enspace Nothing else is an arglist.
260 %
261 % But keep in mind that this ``intuitive" understanding essentially is wrong.
262
263 \subsection{Anatomy of \TeX}
264 The documentation of v0.22 as of 2012-06-04 said that the package
265 is about ``lists in \TeX's mouth." However, this was very wrong.
266 I believed it following Alan Jeffrey's paper ``Lists in \TeX's Mouth",\footnote{%
267 Alan Jeffrey: \tugbartref{tb11-2/tb28jeffrey}{``Lists in \TeX's Mouth,"}
268 \acro{TUG}boat Vol.~11 (1990), No.~2, pp.~237--245),
269 \urlhttpref{tug.org/TUGboat/tb11-2/tb28jeffrey.pdf}.}
270 in whose Section~2 you read:
271 \begin{quote}\it
272 \TeX's programming facilities come in two forms---there
273 are \TeX's \emph{macros} which are expanded in its
274 mouth, and some additional \emph{assignment} operations
275 like \cs{def} which take place in the stomach.
276 \end{quote}
277 The macros that Jeffrey lists and describes in that article can be
278 obtained as a \acro{CTAN} package
279 \ctanpkgref{lambda-lists}.\footnote{\urlhttpref{ctan.org/pkg/lambda-lists}}
280 If you follow the link given here (in the footnote),
281 you currently (2012-11-03) read about this package:
282 \begin{quote}\it
283 These list-processing macros avoid the reassignments
284 employed in the macros shown in Appendix D of the TeXbook:
285 all the manipulations take place in what Knuth is pleased to
286 call ``TeX's mouth".
287 \end{quote}
288 But Knuth doesn't. On page 267 of \TTb, you read:
289 \begin{quote}\it
290 Chapter 7 has described the process by which input files
291 are converted to lists of tokens in \TeX's ``mouth,"
292 and Chapter 20 explained how expandable tokens are converted
293 to unexpandable ones in \TeX's ``gullet" by a process
294 similar to regurgitation.
295 \end{quote}
296 I.e., the ``mouth" is \TeX's ``tokenizer," the inner part of what
297 van Eijkhout calls \TeX's ``input processor" on, e.g., p.~15 of his
298 \meta{\TeX~by Topic}.\footnote{It is available as a \acro{CTAN} package
299 \ctanpkgref{texbytopic} at \urlhttpref{ctan.org/pkg/texbytopic}.}
300 The exact rules the tokenizer follows are described on pp.~46f.\
301 (Chapter~8!) of \TTb.
302 \emph{Macro expansion} takes place in \TeX's \emph{``gullet"},
303 which van Eijkhout calls \TeX's ``expansion processor" (p.~16).
304 Abrahams, Hargreaves, and Berry follow Knuth's terminology on
305 pp.~16 and 46f.\ of their \meta{\TeX~for the Impatient}.\footnote{It
306 is available as \acro{CTAN} package \ctanpkgref{impatient},
307 \urlhttpref{ctan.org/pkg/impatient}.}
308 % Only Jeffreys and my wrong old documentation say this were
309 % ``\TeX's mouth"---and I made the same mistake with the
310 % \ctanpkgref{bitelist} package.
311 %% <- 2012/11/19 ->
312
313 \TeX's gullet has been called ``\TeX's mouth" also
314 in the documentation of my \ctanpkgdref{bitelist} package and
315 in the documentation of the package \ctanpkgdref{bibleref-mouth}.
316
317 Moreover, I should have clarified that Jeffrey's paper deals with
318 ``lists" in some general, rather abstract sense, different from
319 the kind of lists the present documentation tries to characterize
320 as the objects for 'dowith'.
321
322 \subsection{\TeX's Tokens}
323 \label{sec:toks}
324
325 % The \emph{\TeX\ macro writer} understanding \TeX\ properly
326 % does not really think of arglists. \TeX perts instead think of
327 % What \TeX nically matters is
328 % what happens in ``\TeX's mouth,"\footnote{Cf.~\TTbp.~46.}
329 % as some authors have suggested a metaphor,\footnote{%
330 The 'dowith' package is a tool that affects the order of tokens in
331 \TeX's gullet.
332
333 \begin{smallpar}
334 The ``characters you type" enter ``\TeX's mouth" line by line,
335 in a slightly modified appearance. Each line forms a \emph{string}.
336 \TeX\ takes initial substrings away from it and turns them
337 into \emph{tokens} that are appended to the right of \TeX's
338 \emph{expansion buffer} (``gullet").
339
340 %% Removing 2012/11/04:
341 % More formally, \TeX\ has a \emph{character buffer.}
342 % It forms a single token from an initial segment of the buffer content---unless
343 % there is a special situation with blank spaces or something pathological.
344 % When an \emph{escape character}, as the backslash usually is one,
345 % has been noticed recently (that isn't followed by another one immediately),
346 % the character buffer may need to be feeded from more outside,
347 %% <- especially wrong, 2012/11/04
348 % until it contains enough material to form a token from.
349
350 There are \emph{two kinds of tokens} here: \emph{named} tokens
351 and \emph{character} tokens. ``Named" tokens usually are referred
352 to as ``control sequence tokens" or just ``control sequences"---I~really
353 want to avoid those horrible confusions from \TTb.
354 There never are any ``parameter tokens" in \TeX's gullet
355 (perhaps unless one considers a one-step macro expansion
356 a two-or-more-step procedure).
357 % merged 2012/11/04
358 %% Moving down 2012/11/04
359 The character(s) \emph{after} the escape character until
360 some delimiting character form a \emph{string} that is the
361 \emph{name} of the token that is formed---a \emph{named}
362 token, as I am saying.
363 % What has been used to form a token is removed from the character buffer.
364 %% 2012/11/04:
365 \emph{Character} tokens are formed by removing a character from the
366 beginning of the character buffer and appending it to the token buffer
367 paired with its \emph{category} code.
368
369 For every \emph{string of characters}, there is exactly one
370 (possible) \emph{named token} whose name the string is.\footnote{``Possible"
371 refers to the fact that \TeX\ does not store named tokens anywhere
372 before they appear in its gullet, maybe apart from ``primitive"
373 tokens that have a ``pre-assigned meaning" when a \TeX\ run %% \ 2012/11/04
374 starts.---What is more bad with my claim is that
375 the \TeX\ program by design cannot extend its memory arbitrarily---even
376 not using the ``cloud''---, so it doesn't support tokens
377 whose name lengths are above a certain limit.}
378 It is so common (starting from \TTb) to denote the token whose name
379 is <string> by \lq\verb+\+<string>\rq. For instance, the token whose
380 name is `input' is denoted by \qtd{&\input}. On the other hand,
381 on page~7 of \TTb\ \qtd{&\input} is a ``string of characters."
382 With this notation, it is already difficult to explain what
383 the \LaTeX\ command `\DeclareRobustCommand' does or what the difference
384 between a starred \LaTeX\ command and a starred \LaTeX\ environment is.\footnote{%
385 A reader knowing \LaTeX\ only thinks that \qtd{\code{&\\\codesp}}
386 is the result of typing a double backslash and a space
387 and that \qtd{\cs{equation*}} is the ``command" \cs{equation}
388 followed by a `*'.}
389 \TTb\ makes it worse by saying on page~39:
390 \meta{``A control sequence is considered to be a single object
391 that is no longer composed of a sequence of symbols."}
392 So ``it depends" whether \qtd{&\input} is a string of characters or not---it \emph{is
393 before} tokenization, but \emph{no longer} afterwards.
394 So if you have two computers and start a \TeX\ run on each of them
395 with a little difference in time, there will be a moment where
396 \qtd{&\input} is a string on the one computer but not on the other?
397 This
398 appears to me %%% is %% 2012/11/04
399 like saying \meta{``When we apply the square root function to
400 the number 4, the number 4 will no longer be the number 4,
401 it will be the number 2 instead."}
402 \end{smallpar}
403
404 \TTb\ does offer an alternative notation for named tokens: ``boxing;"
405 so the token whose name is `input' can be denoted by the rather
406 ``graphical" notation \qtd{|input|} (used only exceptionally).\footnote{%
407 The box notation is introduced on page~38 without explanation,
408 as if it explained something.}
409 \emph{I}~would suggest something like
410 \qtd{\NTOK{input}} for clarity and \qtd{\ntok{input}}
411 for brevity.\footnote{I am suggesting the question mark for named tokens
412 since \TeX\ ``must look up the current definition" of a named token
413 according to \TTbp.~39, while the meaning of character tokens rather
414 is ``fixed," at least according to \TTbp.~39. However,
415 \emph{active}-character such as .&~ are in the same situation
416 as named tokens as to this respect. The dot notation may be fine for them,
417 though.}
418
419 \begin{smallpar}
420 %% rm. 2012/11/04, cf. above
421 % \emph{Character tokens} get into \TeX's mouth by tokenization
422 % when characters begin the buffer content while \emph{not} scanning
423 % a name for a named token. A single character then is removed from the
424 % character buffer, and a token storing its character code and current
425 % category code is pushed into \TeX's mouth.
426
427 Named tokens may get into \TeX's gullet by ``tokenization" as described above,
428 i.e., they are drawn from the character buffer. But they also can
429 appear in \TeX's gullet ``from within,"
430 by the manipulation inside \TeX's gullet.
431
432 More formally, those manipulations are called ``expansion,"
433 and \TeX's gullet can be conceived of as a \emph{token buffer}
434 that is feeded to the right (or end) by tokenization from the character buffer.
435 Expansion means that certain tokens in the token buffer are substituted
436 by other ones. This way tokens may get into \TeX's gullet that
437 emerged from tokenization a ``long time ago", maybe in a previous
438 run that created the \emph{format} (\TeX's variant \code{INITEX});
439 or tokens may appear by some hardwired expansion function.
440
441 However, \emph{named tokens} may get into \TeX's gullet
442 also by \emph{expansion}, never having been drawn by tokenization
443 and not being hardwired. This happens by the `\csname'
444 % name %% rm. 2012/07/25
445 construct.
446 The input \emph{code} may contain
447 \[`\csname tupni\endcsname'\]
448 This may be converted into 7 tokens entering \TeX's gullet,
449 the first one being \NTOK{csname}, the last one
450 \NTOK{endcsname}, and five character tokens in between.
451 Due to some \emph{function} (which I would denote as *\code{csname})
452 originally associated with the token \NTOK{csname},
453 those seven tokens then are replaced by \NTOK{tupni},
454 the named token whose name is `tupni'. It is not required that
455 the \TeX\ program knows about a token \NTOK{tupni},
456 neither anybody must type \qtd{&\tupni} in any file.\footnote{These
457 considerations may not be essential here,
458 rather a draft for a paper. Using 'dowith',
459 one better just thinks of the arglist items one actually lists.}
460 \end{smallpar}
461
462 \subsection{Arglists vs.\ Lists of Tokens---Example}
463 % \subsection{Arglists and 'dowith' \TeX nically}
464 % In the \TeX nical sense, I think of arglists \emph{and their items}
465 % as follows. Arglists are lists (or sequences) of tokens.
466 % What is somewhat difficult is that the \emph{items} of a token list
467 % usually are \emph{tokens.} Especially, the curly braces in the code
468 % you type usually are converted into certain \emph{character tokens}
469 % that are single items of the resulting token list.\footnote{I discover
470 % this conceptual puzzle 2012-05-16.}
471 %
472 % The conceptual trap here may have resulted from denoting lists
473 % in a \emph{\Wikienref{juxtaposition}} notation.
474 % In Section~\ref{sec:lists-intuit}, I have written `aa' for a
475 % ``list" of \emph{two} items. ``List" is rather a \emph{computer science}
476 % term, its mathematical counterpart rather is the notion of (finite)
477 % \emph{sequences.} The usual \emph{mathematical} notation for a finite sequence
478 % writes list items surrounded by \emph{\wikienref{bracket}{brackets}}
479 % (round---\emph{\Wikienref{parentheses}}---seems to be more common than square,
480 % also \wikienref{angle brackets}{chevrons} are used).
481 %
482 % So the \emph{string} `aa' can be written more clearly---mathematically---as
483 % $(`a',`a')$. The trap with `\a' is that it could be \emph{either} the \emph{string}
484 % $(`\',`a')$ \emph{or} the one-item list $(`\a')$ of strings.
485 % But even in the latter case, I urge not to consider it a \emph{\TeX\ token}.
486 % Rather, I consider `\a' a mistaken way of referring to the
487 % named token \NTOK{a} whose name is the string `a' (or $(`a')$).
488 % (Section~\ref{sec:toks}).
489 %
490 % However, the conceptual trap about arglists and token lists
491 % (there must have been some ``Arglist"!\@) lurks on another level,
492 % as follows. Recall \TTb's notation of \chtok{<char>}{<cat>}
493 % for the \emph{character token} that \TeX's tokenizer forms
494 % from <char> in the character buffer when <char>'s category code is <cat>.
495 % Usually, the \emph{character} `a' is converted into the
496 % \emph{character token} \lttok{a}, `{' is converted into \lbtok,
497 % and `}' is converted into \rbtok.
498 %
499 % We are turning to some \strong{examples} and \strong{counterexamples}.
500 % Let us see what confusions occurred in the ``intuitive" view on
501 % arglists in Section~\ref{sec:arglists-intuit}.
502 %
503 % % \begin{example}{1} It
504 % First, it was bad in Section~\ref{sec:arglists-intuit}
505 % to think that `aa' is a two-item arglist. It was confused with something
506 % like $\lttok{a}\lttok{a}$. The latter looks like a token list---or is it an arglist?
507 % Both? Anyway, it is juxtaposition notation applied to tokens,
508 % mathematically it is $(\lttok{a},\lttok{a})$, so~\dots
509 % % \end{example}
510 %
511 % Now let us reconsider the ``intuitive recursive definition" of arglist.
512 % Or let us look at a recursive definition of \emph{token} list.
513 % \inlineitem{1} There is nothing wrong with saying the the empty list is
514 % a token list, the same holds for arglists.
515 % \inlineitem{2} a.~When we attach an arbitrary token to a \emph{token} list
516 % (at the left), the result is another token list---fine.
517 % There are only certain difficulties with ``handling" special token lists
518 % such as $\lbtok\lttok{a} = (\lbtok,\lttok{a})$.
519 % And the latter is \emph{not} an \emph{arglist!} \
520 % b.~When we attach a \emph{named} token to an arglist (at the left),
521 % the result \emph{is} an \emph{arglist}.\footnote{This is
522 % a \emph{conjecture} only right now---2012-05-16---in which I strongly believe.
523 % Likewise later.}
524 % c.~When we attach a \emph{letter} token \lttok{<char>} to an arglist
525 % (at the left), the result \emph{is} an \emph{arglist}.
526 % d.~When we talked about ``brace groups," they seemed to be \emph{strings}
527 % of characters. Instead, I would like to suggest that a brace group
528 % is an \emph{arglist}\pdots
529 % When \lbtok\ is attached to the left of an arglist and \rbtok\ to the right,
530 % the result is an arglist---this is what I would call a ``brace group"!
531 % e.~The \Wikienref{concatenation} of two arglists is an arglist.
532 %
533 % \begin{smallpar}
534 % The above notion of ``attaching" a token to a token list or an arglist
535 % % should be clarified, but instead of a general definition in terms of
536 % % ``words" of formal languages, examples may suffice here and now.\footnote{2012-05-16.}
537 % should be clarified. Attaching an item $j$ at the left of a list $\lambda$
538 % is the same as concatenating the one-item list $(j)$ with $\lambda$\pdots
539 % but the English \wikienref{append}{Wikipedia} seems to explain concatenation by
540 % \emph{appending}. As we \emph{remove} items one-by-one from the \emph{left}
541 % (beginning) of a list, I prefer the inverse \emph{prepending} items as basic operation
542 % for building lists---cf.\ \Wikienref{CAR and CDR}.
543 % \end{smallpar}
544 %
545 % We won't complete a formal (recursive) definition of arglist here and
546 % now.\footnote{2012-05-16.} Just observe that ``brace groups"
547 % make the difference between token lists and arglists.
548 % Recall that an ``item" of an arglist ``operationally" is defined
549 % as something that a one-parameter macro removes.
550 % Such a macro removes certain single tokens
551 % (space tokens not among them)---and entire ``brace groups"!
552 % Especially, consider \[\lbtok\rbtok\]%%%.
553 % Actually, this is another ambiguous notation.
554 % If it refers to $(\lbtok,\rbtok)$, it is a \emph{token} list,
555 % not an arglist. If it refers to $(\lbtok\rbtok)$---it should
556 % better refer to $((\lbtok,\rbtok))$, which is a
557 % \emph{one-item arglist} whose only item is the former two-item token list!
558 %
559 %% <- 2012/05/17b ->
560 Let us reconsider the examples from Sections~\ref{sec:lists-intuit}
561 and~\ref{sec:arglists-intuit}, and pack them into a single example.
562 If you type a file line
563 \begin{equation}
564 `a a\a{a}'
565 \end{equation}
566 (\emph{eight} keystrokes),
567 it should usually be converted into this \emph{seven}-item list
568 of
569 %%% (five) %% rm. 2015/11/14 -- ???
570 tokens:
571 \begin{equation}
572 \label{eq:toks}
573 \lttok{a}\;\sptok\;\lttok{a}\;\NTOK{a}\;\lbtok\;\lttok{a}\;\rbtok
574 \end{equation}
575 ---with notation from Section~\ref{sec:toks} and
576 \TTb's notation \chtok{<char>}{<cat>}
577 for the \emph{character token} that \TeX's tokenizer forms
578 from <char> in the character buffer when <char>'s category code is <cat>.
579
580 It turns out that the token list in \ref{eq:toks}
581 provides an arglist of \emph{four} items: The token \lttok{a}
582 at the first and third place, the named token \NTOK{a}, and the entire
583 token list $\lbtok\lttok{a}\rbtok$ as a single item---a ``brace group."
584 The space token is ignored.\footnote{\TTbp.~201: ``\TeX\ doesn't use
585 single spaces as undelimited arguments."}
586
587 You can try this after `\renewcommand{\a}{A}'\footnote{Otherwise
588 \cs{a} is a one-parameter macro that breaks 'dowith''s control.}
589 with 'dowith':
590 \begin{equation}
591 \label{eq:sample-code}
592 `\DoWith\typein a a\a{a}\StopDoing'
593 \end{equation}
594 Then \LaTeX\ shows `a', `a', `A' from `\a', and another `a' from within
595 the braces---`\typein' (as any macro with arguments) removes them.
596
597 % \show\a
598 % \a{a}
599 \renewcommand*{\a}{A}
600 % {\MakeNormalHere\# \newcommand*{\TypeOut}[1]{\typein{#1}}
601 % \global\let\TypeOut\TypeOut}
602 % \let\TypeOut\typein
603 % \let\TypeOut\typeout
604 % \DoWith\TypeOut a a\a{a}\StopDoing
605 % \DoWith\typein a a\a{a}\StopDoing
606 % { %%% \tracingmacros=1 \tracingonline=1
607 % \MakeNormalHere\# \AssignCatCodeTo{2}\]
608 % \DoWith\typein #a\a{a]\StopDoing}
609
610 I have avoided saying \ref{eq:toks} \emph{were} an arglist of 4 items.
611 The mathematical basic way of writing lists---understood as finite
612 \emph{\wikienref{sequence}{sequences}}---as ``commma-separated lists"
613 within \wikienref{bracket}{brackets} may clarify the difference
614 (that the \Wikienref{juxtaposition} notation tends to conceal).
615 The \emph{token} list is
616 \begin{equation}
617 \label{eq:toks,}
618 (\lttok{a}, \sptok, \lttok{a}, \NTOK{a}, \lbtok, \lttok{a}, \rbtok)
619 \end{equation}
620 while the list of macro arguments is
621 \begin{equation}
622 \label{eq:args}
623 (\lttok{a}, \lttok{a}, \NTOK{a}, (\lbtok, \lttok{a}, \rbtok)).
624 \end{equation}
625 \ref{eq:toks} or \ref{eq:toks,} simply is \emph{not} an arglist
626 (since neither \lbtok\ nor \rbtok\ can be a macro argument),
627 and the arglist \ref{eq:args} ``provided" by the list of tokens
628 is \emph{not} a list of \emph{tokens}---its final item is a
629 three-item list of tokens, and a token cannot be a list of
630 two or more tokens itself(\emph{!?}).
631
632 \subsection{Another Notation and the Example's Steps}
633 \label{sec:steps} %% 2012/05/20
634 \begin{smallpar}
635 To write token lists easier and hopefully easier to read,
636 I would suggest writing \qtd{.<char>} for the character token
637 that the tokenizer ``usually" forms from character <char>, i.e.,
638 adding the \emph{standard} category code as in \TTb\ (page~37).
639 Then \ref{eq:toks} would read\footnote{See Section~\ref{sec:toks} for the question mark.}
640 \begin{equation}
641 .&a\,.\codesp\,.&a\,\ntok{a}\,.\codelb\,.&a\,.\coderb
642 \end{equation}
643 and the corresponding arglist is
644 \begin{equation}
645 (.&a,.&a,\ntok{a},(.\codelb\,.&a\,.\coderb))
646 \end{equation}
647 In ``retrospect," the result of tokenizing \ref{eq:toks} should be
648 \begin{equation}
649 \label{eq:retro}
650 \ntok{DoWith}\,\ntok{typein}\,
651 .&a\,.\codesp\,.&a\,\ntok{a}\,.\codelb\,.&a\,.\coderb\,
652 \ntok{StopDoing}
653 \end{equation}
654 and the intention is that it works like
655 \begin{equation}
656 %% added \,s 2012/06/07:
657 \ntok{typein}\,.\codelb\,.&a\,.\coderb\,
658 \ntok{typein}\,.\codelb\,.&a\,.\coderb\,
659 \ntok{typein}\,.\codelb \ntok{a}\,.\coderb\,
660 \ntok{typein}\,.\codelb\,.&a\,.\coderb
661 \end{equation}
662 (The definition of `\DoWith' in Section~\ref{sec:core} indeed adds surrounding braces,
663 if missing.)
664 However, \TeX\ rather tries to work with as few tokens ahead as possible.
665 When it finds \ntok{DoWith} and the latter's meaning is the one intended
666 by 'dowith', it first looks for nothing more than the two arguments
667 required by our definition of `\DoWith'. A few moments later,
668 the token buffer's content will just be\footnote{If you use
669 `&\DoWithAllOf&\typein\codeLB a\codeSP a&\a\codeLB a\codeRB\codeRB'
670 %% <- \cs bad 2015/11/14
671 instead, the entire token sequence \ref{eq:retro} will appear in the
672 token buffer ``at once."}
673 \begin{equation}
674 \ntok{typein}.\codelb.&a.\coderb\,\ntok{expandafter}\,\ntok{DoWith}\,
675 \ntok{expandafter}\,\ntok{typein}\,\ntok{fi}
676 \end{equation}
677 Next $\ntok{typein}.\codelb.&a.\coderb$ is expanded according to the code for
678 `\typein' in \file{latex.ltx}. Some unexpandable tokens will emerge
679 and be moved into the ``instruction buffer," %% cmd -> instr 2012/11/04
680 and you should get a screen
681 message with `a' and a prompt. When you have entered something,
682 the remaining \ntok{expandafter} tokens and the \ntok{fi} will be
683 removed from the character buffer, and it contains only
684 \begin{equation}
685 \ntok{DoWith}\,\ntok{typein}
686 \end{equation}
687 Another token is ordered from the tokenizer to provide a second
688 argument for expanding \ntok{DoWith}. The token .\codesp\ comes in,
689 but that doesn't serve as a macro argument. It is removed, and the
690 next token is .&a. The same story as before happens, until the
691 named token \ntok{a} is found\pdots
692 \end{smallpar}
693
694 \subsection{Summary of Possible Arglist Items}
695 \begin{smallpar}
696 For $0\leq i\leq 15$, let $\Chi_i$ be the set of character tokens
697 of category code $i$. $\Chi_1$ is the set of tokens working like
698 $\lbtok$, and $\Chi_2$ is the set of tokens working like $\rbtok$.
699
700 Let $E$ be the set $\{3,4,6,7,8,11,12,13\}$.
701 These numbers are the category codes for
702 \meta{math}, \meta{align}, \meta{parameter}, \meta{super}, \meta{sub},
703 \meta{letter}, \meta{other}, \meta{active} respectively.
704 Let $\Chi_E$ be the set of character tokens of category code in $E$
705 (so $\Chi_E=\bigcup_{i\in E}\Chi_i$).
706
707 Let $\circ$ be the \emph{concatenation} operation among token lists.\footnote{%
708 %% 2012/05/18
709 \TODO: Define for representations by maps, or: %% \ 2012/11/05
710 ``Concatenation is about as basic as natural numbers and is
711 understood in terms of axioms rather than by a definition.''---See
712 notes from 2011 (even with attempts with \Wikienref{Category theory})
713 % the \wikienref{Sequence}{English}
714 the English Wikipedia for
715 \wikienref{Sequence}{sequences}---\wikideref{Folge (Mathematik)}{German}
716 article too much restricted to maps.}
717
718 The following kinds of token lists form a single arglist item,
719 i.e., can serve as an argument for an undelimited parameter:
720 \begin{enumerate}
721 \item a \emph{named} token, or the single-token list consisting of it,
722 if you prefer that;
723 \item a \emph{character} token from $\Chi_E$ or the list consisting of it;
724 \item a \emph{brace group.}
725 That is a token list meeting the following conditions:
726 \inlineitem{1} its \emph{first} token is in $\Chi_1$,
727 \inlineitem{2} its \emph{last} token is in $\Chi_2$,
728 \inlineitem{3} it has as many occurrences of tokens from $\Chi_1$ as from $\Chi_2$,
729 \inlineitem{4} if it is split as $\lambda\circ\rho$, there are not
730 more $\Chi_2$ occurrences in $\lambda$ than $\Chi_1$ occurrences in $\rho$
731 (``don't close before opening").
732 \end{enumerate}
733 The second claim can be checked with
734 \begin{equation}
735 `\DoWith\typein$#^_a1~\StopDoing'
736 \end{equation}
737 % \begingroup
738 % \def\a{A}\MakeNormalHere\#
739 % \DoWith\typein$# ^_a1~\StopDoing
740 % \DoWith\typein#1\StopDoing
741 % \endgroup
742 as to what works.
743 (The claim is not affected by one or two surprises.)\footnote{Moreover,
744 `&\DoWith&\typein&#1&\StopDoing' %% \cs bad 2015/11/14
745 tells something about ``parameter tokens."}
746 Characters with different category codes
747 either are not converted into a character token\footnote{\TTbp.~47.}
748 or are not accepted as macro arguments. The latter applies to ``brace" tokens
749 in $\Chi_1$, $\Chi_2$ and to the single space token \sptok.
750
751 As to \emph{``brace groups"}, the third and fourth condition above
752 are intended to say that what is between the two outer tokens
753 is $\langle$balanced text$\rangle$ in the sense of \TTbp p.~275f. and~385;
754 i.e., for two tokens $a$, $b$ and a token list $\beta$,
755 $(a)\circ\beta\circ(b)$ is a brace group exactly if $a$ is from $\Chi_1$,
756 $b$ is from $\Chi_2$, and $\beta$ is $\langle$balanced text$\rangle$.
757 The conditions are more formal than what I can find in \TTb,
758 but still they don't give me an idea of all possibities.
759 This should be improved by the following recursive definition:
760
761 \begin{trivlist}\item
762 B1.~The empty list is balanced text.
763 B2.~For any token $t$ not in $\Chi_1$ or $\Chi_2$,
764 the single-item token list $(t)$ is balanced text.
765 (Such a token is either a \emph{named} token or a
766 \emph{character} token from $\Chi_E$ or \emph{the space token}~\sptok.)
767 B3.~If $\alpha$ and $\beta$ are balanced texts,
768 then $\alpha\circ\beta$ is balanced text.
769 B4.~If $\beta$ is balanced text,
770 $a$ is from $\Chi_1$, and $b$ is from $\Chi_2$,
771 then $(a)\circ\beta\circ(b)$ is balanced text.
772 (This is a brace group, and the only way of getting a brace group.)
773 B5.~Nothing else is balanced text.
774 \end{trivlist}
775
776 In other words, a token list is a brace group if and only if
777 it is balanced text and starts with a token from $\Chi_1$
778 and ends with a token from $\Chi_2$.\footnote{Again, this may be more
779 of a draft for a paper, or notes for it, than package documentation.}
780 \end{smallpar}
781
782 \subsection{Summary: ``Commands" Usable with 'dowith'}
783 In the \hyperref[sec:apply]{implementation section,}
784 you learn about
785 \[`\DoWith<cmd>',\quad `\DoWithAllOf<cmd>',\quad
786 \mbox{and}\quad `\DoWithAllIn<cmd>'.\]
787 (\LaTeX\ users may type `{<cmd>}' instead.)
788 What <cmd>s are allowed?
789
790 \begin{enumerate}
791 \item All \strong{one-parameter macros} <cmd> work this way,
792 unless there are programming mistakes outside 'dowith'
793 (also thinking of arguments that take over control
794 from 'dowith' commands before the argument list is finished).
795 \item \strong{Other one-parameter} ``commands" <cmd> such as
796 \TeX\ \strong{primitives} may work---you must think of
797 the fact that surrounding \emph{braces} are added.\footnote{\TODO: %% \ 2012/11/05
798 in the future, variants not adding braces could be added.}
799 So the \strong{primitives} `\hbox' and `\vbox' work,
800 for instance. `\show' is an example that doesn't work at all,
801 it takes the single starting brace token and then confuses
802 `\DoWith'.
803 \item Some <cmd>s taking \strong{no argument} may make sense, e.g.,
804 for getting
805 \begin{enumerate}
806 \DoWithAllOf{\item}{{apples,}{pears,}{peaches}}
807 \end{enumerate}
808 from
809 \begin{quote}
810 &\begin{enumerate}\\
811 \null\code{~~}&\DoWithAllOf{&\item}{%
812 \codebd{apples,}\codebd{pears,}\codebd{peaches}}\\
813 &\end{enumerate}
814 \end{quote}
815 Recall that `\item' at most takes an \emph{optional} argument.
816 \item <cmd> must \strong{not take more than one} parameter.
817 %% see def. \DoWith
818 A different package will support multi-parameter macros.
819 \end{enumerate}
820
821 % Rather, ``list" is a term from \Wikienref{computer science} here.
822 % It corresponds to the notion of ``sequence" in mathematics
823 % and to ``word" with formal languages.
824 %
825 % Especially, we have (\meta{i})~lists of \emph{characters} that
826 % the \emph{\TeX} program recieves from files and
827 % (\meta{ii})~lists of so-called \emph{tokens}
828 % that the \TeX\ program forms from the incoming list of characters
829 % and that it works on.
830 %
831 % There are \emph{more} kinds of lists that \TeX\ works on.
832 % Here we are dealing with ``\TeX's gullet."\footnote{Cf.\ documentation
833 % of the \ctanpkgref{bitelist} package.}
834 % \TeX's mouth processes tokens formed from character input.
835 % It may turn a list `<toks-a><toks_b><toks-c>' of tokens
836 % into a list `<toks-a><toks-B><toks-c>', i.e.,
837 % replace <toks-b> by <toks-B>---by so-called ``expansion".
838 % The result may be subject to expansion as well.
839 % When nothing is left to be expanded, results are passed to another,
840 % more interior subprocessor of \TeX. This one is deeper than \TeX's mouth,
841 % we are not concerned with that here.
842 %
843 % Not \emph{all} of those tokens in ``\TeX's mouth" are formed
844 % (``directly") from input characters.
845 % Rather, some \cs{csname} function may form \emph{new} tokens
846 % from other tokens in \TeX's mouth---and place them there again.
847 % This way characters `\csname a\endcsname' you type may just work like
848 % `\a'.
849 %
850 % It is difficult to tell on the \emph{character level} what the
851 % present package does. The relation between incoming characters
852 % and resulting tokens can hardly be explained by a single sentence or so.
853 % However, <cmd> here refers to a command <macro> with a single
854 % undelimited macro. More precisely, a certain character sequence
855 % (``string") in the code you type, will be converted into a TODO
856 %
857 % \subsection{Separators}
858 % TODO
859 %
860 % \subsection{Iterators and \TeX's Mouth}
861 % TODO
862 %
863 \section{Similar Commands in other Packages}
864 %% <- section again 2012/05/17b
865 \subsection{``Heavy" Packages} %% add. heading 2012/05/17b
866 The \CtanPkgRef{etex}{$\varepsilon$-TeX}-related %% e->... 2012/05/09
867 packages \ctanpkgref{etextools}
868 (Florent Chervet), \ctanpkgref{etoolbox} (Philipp Lehman),
869 and \ctanpkgref{texapi} (Paul Isambert) seem to include and
870 (very much) extend the functionality of 'dowith'.
871 Also the `\ForEach'\texttt{\dots\unkern\@} macros of %% \@ 2012/06/03
872 \ctanpkgref{forarray} (Christian Schr\"oppel) seem to extend
873 the present `\DoWith'\texttt{\dots\unkern\@} commands. %% \@ 2012/06/03
874 Moreover, Ahmed Musa describes such commands as %% 2012/06/03
875 ``Parsing \qtd{tsv} lists" in documenting his
876 \ctanpkgref{catoptions} package.
877 \ctanpkgref{moredefs} (Matt Swift) provides list handling commands
878 like the few that are here.\footnote{\ctanpkgref{arrayjobx}% %% % 2012/11/05
879 provides somewhat ``exotic" handling of ``lists".} %% 2012/05/10
880 %% 2012/11/05:
881 ---In October 2012, Ahmed Musa's \ctanpkgref{loops} appeared on
882 \acro{CTAN}, offering loops of several ``categories" about as those
883 that are listed below, very elaborate.%
884 %% /2012/11/05
885 % (I~do not want to load that much.)
886 %% <- 2012/11/04 ->
887 ---I do not want to load that much. I need and only need something
888 excessively simple, very few lines of code, as presented in
889 Section~\ref{sec:implement}. The next sections somewhat point
890 out single features of loop constructs that I do not want to have.
891
892 \subsection{Separators} %% add. heading 2012/05/17b
893 Regarding \LaTeX\ macros in `latex.ltx', the basic macro `\DoWith' of
894 the present package resembles |\@tfor| very much, which likewise deals
895 with lists without separators. By contrast, \LaTeX's |\@for| deals with
896 \emph{comma-separated} lists (such as lists of package options).
897 With comma-separated lists, a ``string" of characters counts as
898 an item when it is delimited by commas, or by a comma and the
899 list ``border," or spaces may be used as separators additionally.
900 However, when \LaTeX\ analyzes such lists (in ``\TeX's gullet"),
901 it uses representations by \emph{character tokens} of them.
902
903 %% moved here 2012/05/17b:
904 % %% 2011/11/10:
905 % Also Heiko Oberdiek's \ctanpkgref{zref} deals with ``lists" of
906 % ``properties" of ``entities,"
907 %% 2011/11/11: comma separated!
908
909 %% 2012/05/09f.:
910 The more recent \ctanpkgref{lmake}
911 (Shengjun Pan) %% 2012/05/18
912 provides a key-value syntax for printing lists of
913 complex mathematical expressions easily (using some assignments)
914 as well as defining commands according to a pattern from a list.
915 Those lists are comma-separated.
916
917 \subsection{``For" Loops vs.\ ``Foreach" Loops} %% mv. up 2012/05/20
918 What about \ctanpkgref{forloop} (Nick Setzer),
919 \ctanpkgref{multido} (Timothy Van Zandt, Rolf Nie\-praksch, Herbert
920 Vo\ss), and \ctanpkgref{xfor} (Nicola Talbot)?
921
922 'xfor' is just a reimplementation of `\@for'.
923 'forloop' and 'multido' are more close to ``real \qtd{for} loops"
924 (cf.\ \wikienref{for loop}{\meta{Wikipedia}}).
925 Loops of the latter kind go through a certain set as well,
926 but such sets rather consist of \emph{numbers} and are exhausted
927 by incrementing (or also decrementing) variables
928 \wikienref{Loop counter}{(counters).}
929 This is essentially not needed
930 (neither helpful) %% 2012/05/20
931 when a list literally is
932 \emph{enumerated}---such loops are distinguished as
933 \wikienref{Foreach loop}{``foreach loops."} %% 2012/05/19
934
935 \subsection{Iterators} %% add. heading 2012/05/17b
936 So `\DoWith' and |\@tfor| rather provide ``foreach" loops.
937 A major difference between them is that the latter uses a
938 \wikienref{For loop}{``loop variable"} or
939 %%% rather %% rm. 2012/05/20
940 \wikienref{Iterator}{``iterator"} to which the elements
941 of the list are assigned.
942 `\DoWith<cmd>' does not use such a loop variable
943 or such assignments and thus is ``expandable" at least when <cmd>
944 (and the elements, depending on <cmd>) are expandable.
945 On the other hand, `\@tfor' applies some procedure to the list
946 elements without needing a \emph{name} for the procedure
947 (or a \emph{macro} storing the procedure).
948 %
949 I wondered whether behind \LaTeX's
950 `\@tfor' (and `\@for') there was an ``ideological" consideration
951 such as ``A loop must have a loop variable!"\pdots
952 % However, avoiding usage of a macro name
953 % (to store the ``loop body" code) %% 2012/05/18
954 % and a macro parameter
955 % (to incorporate the list item into the body code) %% 2012/05/18
956 % may have been a good reason.
957 %% <- 2012/05/19
958
959 %% 2012/05/20:
960 Hopefully more clearly on ``loop variable" vs.\ our approach:
961 In order to run
962 \[<code-before><item><code-after>\]
963 on each <item> of a <list>, \emph{we here}
964 % \[
965 \begin{equation}
966 \mbox{define} \quad `\do' \quad \mbox{as} \quad
967 `#1'\;\to\;`<code-before>#1<code-after>'
968 \end{equation}
969 % \]
970 and then run `\do{<item>}' for each <item> in <list>,\footnote{Cf.~description
971 of procedure in terms of tokens in Section~\ref{sec:steps}.}
972 \begin{equation}
973 \mbox{always replacing}\quad `\do{<item>}'\quad \mbox{by}\quad `\do{<item>}\do'.
974 \end{equation}
975 (`\do' is only an example command that 'dowith' supports especially.)
976 % The ``received" approach from ``usual" programming languages,
977 % \file{latex.ltx}, and its followers is
978 In \file{latex.ltx} instead, we find things like
979 % \[
980 \begin{equation}
981 \label{eq:tfor}
982 `\@tfor\@tmp:=<list>\do{<code-before>\@tmp<code-after>}'
983 \end{equation}
984 % \]
985 where `\@tmp' is a \emph{macro} that is set to be <item> at each
986 iteration of the loop, by
987 \begin{equation}
988 `\def\@tmp{<item>}'
989 \end{equation}
990 within `\@tforloop'. After that,
991 \begin{equation}
992 \label{eq:code-tmp-code}
993 `<code-before>\@tmp<code-after>'
994 \end{equation}
995 from \ref{eq:tfor} is run.---\ref{eq:code-tmp-code} like
996 \ref{eq:tfor} is stored in a larger macro. `\do' in \ref{eq:tfor}
997 does not act as a macro, it just delimits a macro parameter
998 in order give a feeling of some familiar programming structure.
999 This organisation of macros is fine when the loop body code
1000 is only used by the containing macro, while the 'dowith' approach
1001 to store the ``loop body" in an own macro has been useful when
1002 the loop body code also is used for different purposes
1003 or when it has been introduced before I thought of using it
1004 in a loop.
1005 % \[\mbox{defining}\;`\@tmp'\;\mbox{as}\;<item>\quad \mbox{and run} \quad
1006 % `<code-before>\@tmp<code-after>'\]
1007 % for each <item> in <list>.
1008
1009 Note that this only was an example. In general, <item> may appear
1010 more than once in the ``loop body."
1011
1012 ``Expandability" by \emph{avoiding} something iterating `\def\@tmp{<item>}'
1013 and doing iteration in \TeX's gullet (`\do' or so must have been defined earlier)
1014 is essential especially within \cs{write}.
1015 Assignments do not work there. A major motivation for developing
1016 'dowith' developed with the \ctanpkgref{blog} package
1017 that \cs{write}s \acro{HTML} code.
1018 Assignments happen in ``\TeX's stomach." %% 2012/11/04
1019 That place might be called the
1020 ``instruction buffer" to which %% cmd -> instr 2012/11/04
1021 the ``expansion processor" moves items from the incoming token buffer
1022 that cannot be expanded (any more).
1023
1024 \subsection{Separator Macros} %% add. heading 2012/05/17b
1025 Commands like `\DoWith' also could save tokens thinking of list macros
1026 (in \LaTeX/`latex.ltx') that use a \emph{separator macro}
1027 which may be used as a \emph{command} to be applied to the list
1028 elements. One example is
1029 `\dospecials' that already is in Plain \TeX\ and expands to
1030 \[`\do\ \do\\\do\{\do\}\do\$\do\&\do\#\do\^\do\_\do\%\do\~'\]
1031 % \begin{verbatim}
1032 % `\do\ \do\\\do\{\do\}\do\$\do\&\do\#\do\^\do\_\do\%\do\~'
1033 % \end{verbatim}
1034 An important application of `\dospecials' is temporarily
1035 switching off the ``special" functionality of the ``elements"
1036 in `\dospecials'. With \LaTeX, this may happen thus:
1037 \[`\let\do\@makeother\dospecials'\]
1038 % \begin{verbatim}
1039 % `\let\do\@makeother
1040 % \end{verbatim}
1041 With 'dowith', you can do the same with a shorter variant
1042 `\specials' of `\dospecials', defined by
1043 \[`\def\specials{\ \\\{\}\$\&\#\^\_\%\~}'\]
1044 and then
1045 \[`\DoWithAllIn\@makeother\specials'\]
1046 `latex.ltx' uses `\@elt' instead of `\do' for its own list macros.
1047
1048 \subsection{Ye Olde \cs{loop}} %% add. heading 2012/05/17b
1049 %% added 2011/11/03:
1050 There also is |\loop<loop-body>\repeat| in Plain \TeX\ and a
1051 refined\footnote{Using Kabelschacht's suggestion, cf.\ Section~\ref{sec:kabel}}
1052 version of it in `latex.ltx'. It is \emph{not} expandable
1053 since it starts with an assignment for `\body' (Plain \TeX) or
1054 `\iterate' (`latex.ltx'),
1055 %% add. 2012/05/20:
1056 and then some assignments are needed to stop the loop,
1057 such as incrementing or decrementing a \emph{counter.}
1058 As to the programming structure, it is very simple and general,
1059 I think any kind of loop can be implemented by this
1060 (apart from nested loops). E.g., I realize\footnote{2012-05-20}
1061 that even a ``foreach" loop could be implemented by managing
1062 a list macro, e.g., using \LaTeX's internal `\@next'.
1063 %% rm. 2012/05/20:
1064 % that you cannot probably
1065 % what kind of loops it addresses. However, the applications
1066 % I have seen have been ``for" or (rather) ``while" loops.
1067 % ``While" loops can ``emulate" ``for" and ``foreach" loops
1068 % by having the ``incrementation" method or the ``enumeration" method in
1069 % their body. This is quite obvious for ``for" loops, not quite so for
1070 % ``foreach" loops; which for practical application (in my view) means
1071 % that neither \LaTeX/\TeX's `\loop' macro nor in general ``while" loops
1072 % is/are very helpful for implementing ``foreach" loops,
1073 % as rather `\DoWith' and similar constructions are.
1074 % The reason for this is (as it seems to me) is that you
1075 % (a human being) can much more easily enumerate (``list")
1076 % the items of a list (you have in mind) than define the \emph{method}
1077 % that (allegedly) is behind your enumeration. \ \meta{Example:}
1078 % \[`\DoWithAllOf{\printsamplearea}{\red\green\blue}'\]
1079 % ---\emph{how} (according to what ``method"?) did you ``proceed" from
1080 % `\red' to `\green' and from `\green' to `\blue'?
1081
1082 \subsection{Without Iterator and Separators} %% add. heading 2012/05/17b
1083 %% 2011/11/07:
1084 In \LaTeX's \ctanpkgref{tools} bundle, \ctanpkgref{xspace}
1085 %% add. 2012/05/18:
1086 was developed in the nineties by David Carlisle.
1087 It had a rather fixed exception list implemented by a deeply
1088 nested conditional. In 2004 Morton H\o gholm joined,
1089 and now 'xspace' has a list macro
1090 `\@xspace@exceptions@tlp' %% was xpspace 2012/05/17b
1091 without separators.
1092 It is handled like here, except that it ``breaks" the loop
1093 when an item is found that applies.
1094 %% add. 2012/05/17bf.:
1095 After the ``next" token is stored by the usual \cs{futurelet},
1096 the exception list is searched without using an iterator.
1097 Addition and removal commands are provided as well.
1098
1099 % \pagebreak %% 2012/05/17b
1100 % \newpage %% 2012/05/19
1101 \section{Implementation} %% 2012/05/10
1102 \label{sec:implement}
1103 \subsection{Package File Header (Legalese)} %% sub 2012/05/10
1104 \input{dowith.doc}
1105
1106 \newpage %% 2012/06/04
1107 \section{Ack.: 25 Years of Kabel\-schacht's \cs{expandafter}} %% 2012/05/20
1108 \label{sec:kabel}
1109 The essential idea of 'dowith' and `\DoWith' is
1110 \[`\if<code>\expandafter<one-token>\fi'\]
1111 % Alan Jeffrey: \tugbartref{tb11-2/tb28jeffrey}{``Lists in \TeX's Mouth,"}
1112 % \acro{TUG}boat Vol.~11 (1990), No.~2, pp.~237--245),
1113 % \urlhttpref{tug.org/TUGboat/tb11-2/tb28jeffrey.pdf}.}
1114 It was described by \textsc{Alois Kabelschacht} as
1115 \tugbartref{tb08-2/tb18kabel}{``&\expandafter\ vs.\ &\let\ and &\def\
1116 in Conditionals and a Generalization of PLAIN's &\loop"}
1117 in \acro{TUG}boat Vol.~8 (1987), No.~2, pp.~184f.\
1118 (a little more than one column).\foothttpurlref{tug.org/TUGboat/%
1119 tb08-2/tb18kabel.pdf}
1120 See some German biographical notes on Kabelschacht in the
1121 \wikideref{Benutzer:RolteVolte/Alois_Kabelschacht}{German Wikipedia.}\foothttpurlref{%%
1122 de.wikipedia.org/wiki/Benutzer:RolteVolte/Alois\string_Kabelschacht}
1123 It seems to me that Knuth didn't note this application of `\expandafter'
1124 in \TTb.\footnote{% %% 2012/05/22
1125 However, the paper \meta{\qtd{uses the fact that the expansion of both
1126 \cs{else ... }\cs{fi} and \cs{fi} is empty.}} In \TTb\ I only find
1127 %% &\else and &\fi failed
1128 \meta{\qtd{The ``expansion" of a conditional is empty}} on page~213.}
1129 It was then applied in many macros of \file{latex.ltx},
1130 cf.~\file{source2e.pdf}.
1131
1132 \end{document}
1133
1134 VERSION HISTORY
1135
1136 2011/11/02 for v0.1 very first
1137 2011/11/03 discussing \loop; \pagebreak
1138 2011/11/07 xspace
1139 2011/11/10f. zref added/removed
1140 2012/05/09 for v0.2 $\varepsilon$-\TeX
1141 2012/05/10 "iterator", \MDkeywords, \hypersetup,
1142 Legalize -> Legalese, "Related packages",
1143 tighter sectioning
1144 2012/05/14 for v0.21 spurious space in title fixed
1145 2012/05/14b r0.21a another keyword
1146 2012/05/15 abstract: why expandable
1147 2012/05/16 discussion much extended;
1148 stored separately before reworking
1149 2012/05/16b r0.21b reworking ... many mistakes!
1150 2012/05/17 updating date, was 2012/05/14 before!
1151 and from 2012/05/14b onwards it should
1152 have been r0.21a; storing again,
1153 renaming dir.s ...
1154 2012/05/17bf. r0.21c reducing text ...
1155 2012/05/19 r0.21d braces and commands applicable
1156 2012/05/20 corrections, clarifications, reorder;
1157 especially examples for `dowith' vs.
1158 `\@tfor'; remarks about `\loop' corr.:
1159 how to `foreach' ...; Kabelschacht
1160 2012/05/22 r0.21e Kabelschacht vs. TeXbook
1161 2012/06/03 r0.21f cf. `catoptions'; \dots\unkern\@, vs.\
1162 2012/06/07 r0.22a added \,s in "?typein ..."
1163 2012/07/25 r0.3 "\csname name construct"?
1164 2012/11/02ff. gullet, plainpkg, more modifications
1165 2012/11/19 mod. on "mouth" (bibleref-mouth)
1166 2012/11/27 r0.3a plainpkg
1167 2015/11/14 rm. "(five)" & \ctanpkgdref, code in two
1168 footnotes, don't use hyperref draft