1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
This is the README file for the 'loops' package.
AUTHOR
Ahmed Musa
VERSION
Version 1.1, 2012/10/16.
LOCATION ON CTAN
macros/latex/contrib/loops/
LICENSE
Copyright (c) 2012 Ahmed Musa
This software is author-maintained. Permission is granted to copy,
distribute and/or modify this software under the terms of the
LaTeX Project Public License, version 1.3 or higher. This software
is provided 'as it is', without warranty of any kind, either
expressed or implied, including, but not limited to, the implied
warranties of merchantability and fitness for any particular purpose.
NOTE
Non-LaTeX users can use the \newforeach macro (and some other
looping macros) by loading the file skeyval-for.tex. These exclude
\foxloop or \foreachfox, which are available only in loops.sty.
SUMMARY
The 'loops' package provides efficient looping macros for processing
both csv (separated-values) and nsv/tsv (non-separated values) lists.
csv lists having arbitrary parsers, and even active parsers, can
be processed with the tools of this package.
All our loops can be safely nested. Breaking the inner loop doesn't
affect the continuation of the outer loop.
The looping macros include:
\skvrecurse:
\skvrecurse{<body>}\while<\if...>\fi
A variant of D.E. Knuth's \loop macro.
\@tempcnta\z@
\skvrecurse{
\advance\@tempcnta\@ne
\@tempcntb\z@
\skvrecurse
\advance\@tempcntb\@ne
\typeout{Doing \the\@tempcnta,\the\@tempcntb}
\while
\ifnum\@tempcntb<3\relax
\fi
}
\while
\ifnum\@tempcnta<4\relax
\fi
\carlisleloop:
\carlisleloop<id>{<body>}<\if...>\repeat<id>
A nesting version of D.E. Knuth's \loop macro,
adapted from the original macros of David Carlisle.
\skvnewcounts{rowcnt,colcnt,maxrow,maxcol}
\maxrow4 \maxcol3\relax
\def\entries{}
\let\xp\skvexpandonce
\def\generate{%
\advance\colcnt\@ne
\edef\entries{\xp\entries\the\numexpr\rowcnt*\colcnt\relax}%
\ifnum\colcnt<\numexpr\maxcol+1\relax\relax
\edef\entries{\xp\entries\noexpand&}%
}
\def\generateentries{%
\carlisleloop\a
\advance\rowcnt\@ne\colcnt\z@
\carlisleloop\b
\generate
\repeat\b
\ifnum\rowcnt<\numexpr\maxrow+1\relax\relax
\edef\entries{\xp\entries\noexpand\cr}%
\repeat\a
\edef\entries{\xp\entries\noexpand\crcr}%
}
\begin{document}
\generateentries
$$\vbox{\halign{&\ \hfil#\hfil\strut\cr\entries}}$$
\end{document}
\akterloop:
\akterloop{<body>}<\if...>\repeat
A generalized and naturally nesting version of D.E. Knuth's
\loop macro. No nesting brace is required, unlike in \skvrecurse.
No <id> is required, unlike in \carlisleloop.
\def\generateentries{%
\akterloop
\advance\rowcnt\@ne\colcnt\z@
\akterloop
\generate
\repeat
\ifnum\rowcnt<\numexpr\maxrow+1\relax\relax
\edef\entries{\xp\entries\noexpand\cr}%
\repeat
\edef\entries{\xp\entries\noexpand\crcr}%
}
\begin{document}
\generateentries
$$\vbox{\halign{&\ \hfil#\hfil\strut\cr\entries}}$$
\end{document}
\skvtfor:
\skvtfor<holder.cmd>:={<list>}\do{<callback>}
\skvtfor*<holder.cmd>:={<listcmd>}\do{<callback>}
A variant of LaTeX kernel's \@tfor that has a different
approach to breaking the loop prematurely.
\skvnewregister\count\nr
\skvtfor\x:=a \ifx {\def\x#1{#1}} \fi\do{%
\nr\z@
\skvtfor\y:=12345\do{%
\typeout{Doing: \detokenize\expandafter{\x}--\y}%
\advance\nr\@ne
\ifnum\nr>\tw@\skvbreakloop\fi
}%
}
\ayeloop:
\ayeloop{<list>}\do{<1.parameter.callback>}
\ayeloop*{<listcmd>}\do{<1.parameter.callback>}
A variant of \skvtfor that uses parameter characters
directly, instead of a holder macro.
\ayeloop abcd\do{%
\def\nr{0}%
\ayeloop 12345\do{%
\typeout{Doing #1--##1}%
\skvpushnumber\nr
\ifnum\nr>3\relax\skvbreakloop\fi
}%
}
\sifakaloop:
\sifakaloop{<list>}{<1.parameter.callback>}
\sifakaloop*{<listcmd>}{<1.parameter.callback>}
\sifakaloop is as \ayeloop but will first normalize the list
using \skvtsvnormalize before parsing. Unlike all the
other nsv/tsv loops, it preserves outer braces in the
arguments. Hence, it is costlier. You can insert \listbreaker
as a token in <list> or <listcmd> to terminate the list prematurely.
\skvfor:
\skvfor{<list>}<1.parameter.callback.macro>
\skvfor is an expandable comma loop. The list is not
normalized prior to parsing, and it is not possible to
terminate the loop prematurely (ie, before exhausting
the list).
\newcount\nr\nr=\tw@
\def\do#1{%
\let\noexpand#1%
\expandafter\noexpand\csname\skvremovescape{#1}@%
\romannumeral\nr\endcsname
}
\edef\x{\skvfor{\cmda,\cmdb}\do}
\show\x -> \let\cmda\cmda@ii \let\cmdb\cmdb@ii
\skvgenloop:
\skvgenloop{<parser>}{<list>}{<1.parameter.callback>}
\skvgenloop is non-expandable but it takes any arbitrary
list separator (parser). In the case of non-separated (nsv/tsv)
lists, <parser> can be empty. The list is not normalized prior to
parsing, and it is not possible to terminate the loop prematurely
(ie, before exhausting the list).
Here, <1.parameter.callback> is not a macro, unlike the
<1.parameter.callback.macro> for \skvfor.
\newcount\nr\nr=\tw@
\def\stack{}
\skvgenloop{,}{\cmda,\cmdb}{%
\edef\stack{%
\unexpanded\expandafter{\stack}%
\let\noexpand#1%
\expandafter\noexpand\csname\skvremovescape{#1}@%
\romannumeral\nr\endcsname
}%
}
\show\stack
\skvcommaloop, \skvecommaloop, \skvcommaparse, \skvkvparse:
\skvcommaloop{<list>}<holder-cmd>{<callback>}
\skvcommaloop*{<listcmd>}<holder-cmd>{<callback>}
All these have the same syntax. \skvecommaloop is as
\skvcommaloop but will execute <callback> once when <list>
is empty. This is needed for processing empty key families.
\skvcommaparse is as \skvcommaloop but will first normalize
<list> using \skvcsvnormalize. \skvkvparse is as \skvcommaloop
but will first normalize <list> using \skvkvnormalize.
\skvkvparse is meant for processing key-value lists.
\skvdolist and \skvparselist:
\skvparselist{<parser>}{<list>}<holdercmd>{<callback>}
\skvparselist*{<parser>}{<listcmd>}<holdercmd>{<callback>}
These can process lists with arbitrary parsers/list
separators.The only difference between \skvdolist and
\skvparselist is that \skvparselist will first normalize
the list before parsing.
\cicadaloop:
\cicadaloop[<parser>]{<list>}<\if...>\fi{<callback>}
\cicadaloop*[<parser>]{<listcmd>}<\if...>\fi{<callback>}
\cicadaloop will terminate prematurely on the current depth
whenever the condition specified by <\if...> is true.
<callback> is the regular code, to be executed
for every item of the list before the loop is terminated.
Some internal macros are accessible by the user: \iflastcicada,
\currentcicada, \nextcicada, \lastcicada, \cicadacount.
\cicadacount is depth-dependent.
You can insert \listbreaker as an item in <list> or <listcmd>
to terminate the list prematurely. \listpauser isn't recognized by
\cicadaloop.
\let\romn\romannumeral
\@tempcnta\z@
\def\blist{{X};{Y};Z;\fi}
\def\stack{}
\cicadaloop{{a},{b},c,\if}\if01\fi{%
\advance\@tempcnta\@ne
\@tempcntb\z@
\@tempswafalse
\cicadaloop*[;]\blist\if@tempswa\fi{%
\advance\@tempcntb\@ne
\typeout{Doing \romn\@tempcnta,\romn\@tempcntb:
\detokenize{#1,##1}}%
\edef\stack{%
\unexpanded\expandafter{\stack}\noexpand\do
{\romn\@tempcnta,\romn\@tempcntb}{\unexpanded{#1;##1}}%
}%
\ifnum\@tempcntb>\@ne
\@tempswatrue
\skvcsedef{cmd@\romn\cicadaloopdepth}{\lastcicada,\cicadacount}%
\fi
}%
}
\show\stack
\newforeach:
\newforeach[<options>]<holder.macros>{<optional.'in'>}
{<list.or.listcmd>}{<optional.'do'>}{<callback>}
\newforeach<holder.macros>[<options>]{<optional.'in'>}
{<list.or.listcmd>}{<optional.'do'>}{<callback>}
The star (*) form of \newforeach is equivalent to declaring
the option 'list is a macro' in <options>.
\newforeach is a more versatile and robust version of the popular
\foreach macro of the PGF bundle. The macro accepts arbitrary list
parsers and sub-parsers, including active list separators.
When needed, parameters corresponding to the holder macros can
be used directly in the callback. \newforeach does everything
that PGF's \foreach does. It doesn't automatically scope
calculations as \foreach does. Automatically creating local
groups for computations has been a problem for some users
of \foreach. Instead, \newforeach uses stacks to save and
restore the values of holder macros and some other internal
parameters/quantities.
There is a long list of options/keys in <options>, available for
\newforeach and \foxloop or \foreachfox. For example, key 'reverse list'
allows users to automatically reverse their lists prior to processing.
Also, there are many internal macros that can be accessed by the user.
A few of them are
\interruptforeach, \foreachcurrentitem, \foreachnextitem,
\foreachitemcount, \foreachprevitem, \prependtobeginforeach,
\appendtobeginforeach, \prependtoendforeach, \appendtoendforeach,
\ifforeachlastitem, \foreachnestdepth, \foreachlistremainder,
\skvforeachonlyinitially.
\foreachitemcount counts the items as the loop progresses.
It is depth dependent, ie, it can be called to access the
number of items processed on each depth of nesting of
\newforeach. On any depth, the user can say, eg,
\ifnum\foreachitemcount>10\relax\interruptforeach\fi
or
\ifnum\foreachitemcount>10\relax\interruptloop\fi
You can insert \listbreaker as an item in <list> or <listcmd>
to terminate the list prematurely. \listpauser will pause
the processing for user action.
\newforeach found a bug in PGF's \foreach, as reported at
<http://tex.stackexchange.com/questions/72707/an-unexpected-
outcome-from-pgfs-foreach>. However, \newforeach itself
can't be said to be bug free.
Maybe you fancy the following syntactic sugar, which PGF's
\foreach can't presently process:
\begingroup
\catcode`\,=13
\gdef\alist{1,2,...,5,7,8,...,12}
\endgroup
\parindent-20pt
\begin{tikzpicture}
\draw[step=.5cm,blue!65,very thin] (0,0) grid (13,6);
\newforeach \x [
expand list once,
count in=\xc all \x satisfying \ifnum\x>5\fi
] in \alist {
\newforeach [
parser={;},
subparser=:,
evaluate=\y as \ye using \numexpr\y*10
]
\y:\z in {1:red; 2:green; 3:blue; 4:brown; 5:purple} do {
\draw [fill=\z\ifnum\x>5!\ye\fi] (\x,\y) +(-.5,-.5)
rectangle ++(.5,.5);
\draw (\x,\y) node {\x,\y};
}
}
\global\let\xc\xc
\end{tikzpicture}
\show\xc
% Example credit: Tom Bombadil
\usetikzlibrary{arrows}
\parindent-40pt
\begin{tikzpicture}
% You may fix \pgfmathsetseed to get a constant output:
% \pgfmathsetseed{42}
\newforeach \y [count=\yc] in {-0.5,-1,...,-2.5} {%
\node[right] at (15.2,\y+0.1) {\scriptsize Subject \yc};
\newforeach \x in {1,...,300}{%
\pgfmathtruncatemacro{\drawbool}{rand-0.7 > 0 ? 1 : 0}%
\ifnum\drawbool=1\relax
\fill (\x/20,\y) rectangle (\x/20+0.05,\y+0.3);
\fi
}%
}%
\draw[-stealth] (0,0) -- (15.5,0);
\node[right] at (15.5,0.2) {t in ms};
\newforeach \x [
pgf evaluate=\x as \xv using int(20*\x)
] in {0,...,15} {%
\draw (\x,-0.05) -- (\x,0.05) node[above] {\xv};
}
\end{tikzpicture}
Here, it is possible to use #1, ##1, ##2 in the callback to
refer to \x, y, \z, respectively. This relieves the user of the
need to manually expand the holder macros \x,\y,\z in the callbacks,
as I have seen some TikZ users do.
Here is an example that uses parameters:
\newforeach \x [
item counter = \xc,
exit when = \ifskvstreq#1\with b\then\fi,
] in {a,b,c} do {%
\newforeach [
count in = \yc all \y satisfying \ifnum\y>2\fi,
loop stopper = \ifnum\y>3\fi
] \y in {1,2,3,4,5} {%
\skvcsdef{#1##1}{Items: #1, ##1}%
}%
}
\begin{document}
Numbers: {\tt\string\xc}: \xc, {\tt\string\yc}: \yc
\par\skvcsuse{a1}
\par\skvcsuse{e10}
\end{document}
\begin{document}
\begin{tikzpicture}[scale=2]
\let\pgftrun\pgfmathtruncatemacro
\newforeach [count=\ic] \x/\y in {1/.5,1/1,0/1,0/0,1/0,1/-1,0/-1,0/-.5,0/0}
\coordinate (p\ic) at (\x,\y);
\draw[line width=1pt,blue] (p1) \newforeach \p in {2,...,8} {-- (p\p)};
\newforeach \q in {1,...,7} {%
\pgftrun{\qa}{\q+1}\pgftrun{\qb}{\q+2}\pgftrun{\ind}{2*\q-1}%
\pgftrun{\next}{2*\q}%
\coordinate (n\ind) at (barycentric cs:p\q=0.5,p\qa=0.5);
\coordinate (n\next) at (barycentric cs:p\q=0.125,p\qa=0.75,p\qb=0.125);
}
\draw[line width=2pt,red] (n1)\newforeach \q in {2,...,13}{--(n\q)};
\end{tikzpicture}
\end{document}
% A complete graph
% Example credit:
% Quintin Jean-Noël
% <http://moais.imag.fr/membres/jean-noel.quintin/>
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
\usetikzlibrary[topaths]
\newcount\mycount
\begin{document}
\begin{tikzpicture}[transform shape]
\newforeach \nr in {1,...,8}{
\mycount=\numexpr(\nr-1)*45\relax
\node[draw,circle,inner sep=0.25cm] (N-\nr) at
(\the\mycount:5.4cm) {};
}
\newforeach \nr in {9,...,16}{
\mycount=\numexpr(\nr-1)*45+22\relax
\node[draw,circle,inner sep=0.25cm] (N-\nr) at
(\the\mycount:5.4cm) {};
}
\newforeach \nr in {1,...,15}{
\mycount=\numexpr\nr+1\relax
\newforeach \nra in {\the\mycount,...,16}{
\path (N-\nr) edge[->,bend right=3] (N-\nra)
edge[<-,bend left=3](N-\nra);
}
}
\end{tikzpicture}
\foxloop or \foreachfox:
\foxloop[<options>]{<list>}{<parametered.callback>}
\foxloop*[<options>]{<listcmd>}{<parametered.callback>}
\foxloop does everything that \newforeach does, but, in addition,
it can process nsv/tsv (non-separated, tokenwise, lists). It can
auto-complete ellipsis lists (ie, lists with '...'), but this
feature is available only for csv lists and not for tsv lists.
1. One important characteristic of \foxloop is that it doesn't use
holder macros (eg, \x/\y), but instead it uses parameter
characters directly. So it doesn't accept/expect holder macros.
2. That means that if the argument isn't simple (ie, if it isn't #1),
then it has to be specified as the value of the key/option 'arg'
(eg, arg=#1/#2).
3. Note that \foxloop doesn't expect any optional 'in' or 'do' in the
argument list.
4. Unlike \newforeach, \foxloop doesn't recognize the semicolon
(;) as a callback terminator. In fact, the callback for \foxloop
should ideally be always enclosed in balanced braces. PGF
often uses the semicolon as the callback terminator. \newforeach
supports that feature.
The following macros are available to the user:
\breakfoxloop, \interruptloop, \foxloopdepth, \foxcount,
\previousfox, \currentfox, \nextfox, \iflastfox,
\foxlistremainder, \prependtobeginfoxloop, \appendtobeginfoxloop,
\prependtoendfoxloop, \appendtoendfoxloop.
Examples:
\foxloop [
remember=#1 as \x initially A
] {B,...,H} {%
$\overrightarrow{\x#1}$\iflastfox.\else,\space\fi
}
This is equivalent to:
\foxloop [
list type=tsv,
remember=#1 as \x initially A
] {BCDEFGH} {%
$\overrightarrow{\x#1}$\iflastfox.\else,\space\fi
}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
\foxloop [count=\nchar] {a,...,f} {
\foxloop [
evaluate=##1 as \xe using \numexpr##1*20,
evaluate=##1 as \hx using \pgfmathparse{##1 mm*5},
evaluate=##1 as \vx using \pgfmathparse{##1 mm*1.5},
count in= \ci all ##1 satisfying \ifnum##1<4\fi initially 2,
count in= \di all ##1 satisfying \ifnum##1>2\fi,
] {1,...,6} {
\endgraf
Doing: #1, ##1, \nchar, \ci, \di
\endgraf
\ifnum\nchar>3\relax
\vskip-\vx pt\relax
\fi
\hskip\hx pt\relax
\tikz[rounded corners,ultra thick]{
\shade[ball color=red!\xe!blue] (0,0) circle (.25cm);
\node[white,font=\large] (0,0) {#1};
}%
}%
}
\end{multicols}
\end{document}
% Example credit: Heiko Oberdiek.
\makeatletter
\let\do\newdimen\do\DimWD\do\DimHT\do\DimDP
\newcommand*{\DimBox}[1]{%
\begingroup
\sbox0{\makebox[\DimWD]{#1}}%
\ht0=\DimHT\dp0=\DimDP\usebox\z@
\endgroup
}
\newcommand*{\DimMeasure}[3]{%
\node at (0,0) {%
\let\do\global
\do\DimWD\z@\do\DimHT\z@\do\DimDP\z@
\foxloop* [arg=##1/##2,parser={#1}] #2 {%
\sbox0{#3}%
\ifdim\wd0>\DimWD\global\DimWD=\wd0\fi
\ifdim\ht0>\DimHT\global\DimHT=\ht0\fi
\ifdim\dp0>\DimDP\global\DimDP=\dp0\fi
}%
};%
}
\makeatother
\begin{document}
\def\yellowlist{a/-1,-2;b/1,-2;c/2,-1;d/2,1;e/1,2;f/-1,2;g/-2,1;h/-2,-1}
\def\bluelist{a/b,b/c,c/d,d/e,e/f,f/g,g/h,h/a}
\begin{tikzpicture}[scale=1.2,auto=left,every node/.style={circle,thick}]
\DimMeasure{;}{\yellowlist}{#1}
\foxloop* [arg=#1/#2,parser={;},count=\xc] \yellowlist {
\node (#1) at (#2) [fill=blue!20,draw=yellow] {\DimBox{#1}};
}
\DimMeasure{,}{\bluelist}{#1--#2}
\foxloop* [arg=#1/#2] \bluelist {
\draw [->] (#1) -- (#2)
node [midway,fill=red!20,draw=blue]{\DimBox{#1--#2}};
}
\end{tikzpicture}
\end{document}
% End of readme file of loops.sty
|