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
|
%\iffalse
% xfor.dtx generated using makedtx version 0.94b (c) Nicola Talbot
% Command line args:
% -src "xfor.sty=>xfor.sty"
% -doc "manual.tex"
% -author "Nicola Talbot"
% -dir "source"
% xfor
% Created on 2007/8/21 17:28
%\fi
%\iffalse
%<*package>
%% \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 \~}
%</package>
%\fi
% \iffalse
% Doc-Source file to use with LaTeX2e
% Copyright (C) 2007 Nicola Talbot, all rights reserved.
% \fi
% \iffalse
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage{xfor}
\usepackage{ifthen}
\usepackage{alltt}
\usepackage[colorlinks,
bookmarks,
hyperindex=false,
pdfauthor={Nicola L.C. Talbot},
pdftitle={xfor: redefinition of '@for},
pdfkeywords={LaTeX,package development}]{hyperref}
\CheckSum{117}
\PageIndex
\RecordChanges
\newcommand*{\sty}[1]{\textsf{#1}}
\begin{document}
\DocInput{xfor.dtx}
\end{document}
%</driver>
%\fi
%\MakeShortVerb{"}
%
% \title{xfor v1.03: Reimplementation of \cs{@for} to allow
%premature termination of the loop}
% \author{Nicola L.C. Talbot\\[10pt]
%School of Computing Sciences\\
%University of East Anglia\\
%Norwich. Norfolk\\
%NR4 7TJ. United Kingdom.\\
%\url{http://theoval.cmp.uea.ac.uk/~nlct/}}
%
%\date{21st August 2007}
%\maketitle
%\tableofcontents
%
%\section{Introduction}
%\changes{1.0}{2007 June 27}{Initial version}
%The \sty{xfor} package redefines \cs{@for} so that the loop can
%be prematurely terminated, akin to C/Java's \texttt{break} statement
%except that the loop will terminate at the \emph{end} of the
%current iteration. The syntax for \DescribeMacro{\@for}\cs{@for}
%remains the same:
%\begin{quote}
%\cs{@for}\meta{cmd}":="\meta{list}"\do"\marg{body}
%\end{quote}
%where \meta{cmd} is a command name that is assigned to the current
%element of the list given by \meta{list} at each iteration.
%
%To terminate the loop at the end of the current
%iteration, use the command \DescribeMacro{\@endfortrue}\cs{@endfortrue}.
%This command may be used anywhere in \meta{body}, but will only
%take effect at the end of the current iteration.
%The remainder of the list is stored in
%\DescribeMacro{\@forremainder}\cs{@forremainder}. You can test
%whether the loop was prematurely terminated using the conditional
%\DescribeMacro{\if@endfor}\cs{if@endfor}.
%
%As from version 1.02, there is also provision for looking ahead.
%At each iteration in the loop, the next element is stored in
%\cs{@xfor@nextelement}\DescribeMacro{\@xfor@nextelement}.
%On the last iteration, this value
%will be \cs{@nil}, and so can be checked using
%\begin{verbatim}
% \ifx\@xfor@nextelement\@nnil
% % last iteration
% \else
% % not last iteration
% \fi
%\end{verbatim}
%
%\subsection{Example (ordered insertion)}
%
%Suppose you have list of sorted numbers stored in the command
%\cs{mylist}, e.g.:
%\begin{verbatim}
%\def\mylist{1,3,5,7,8,12,15,20}
%\end{verbatim}
%and you want to insert a new value given by the command
%\cs{newval}, e.g.
%\begin{verbatim}
%\def\newval{11}
%\end{verbatim}
%in the correct order. You can use \cs{@for} to iterate through
%each element in the sorted list, testing the value against the
%new value to be inserted. Once the new value has been inserted,
%the loop can be terminated, and any remaining elements can be
%appended to the new list. The following
%defines the command \cs{insertinto}\marg{new val}\marg{list}
%which uses this method:
%\begin{verbatim}
% \newcommand{\insertinto}[2]{%
% \def\nlst{}%
% \@for\n:=#2\do{%
% % store new list in \toks@
% \expandafter\toks@\expandafter{\nlst}%
% % test current value against new value
% \ifnum\n>#1\relax
% \edef\newstuff{\number#1,\n}%
% % end for loop at the end of this iteration
% \@endfortrue
% \else
% \edef\newstuff{\n}%
% \fi
% % append new stuff to new list
% \ifx\nlst\@empty
% \edef\nlst{\newstuff}%
% \else
% \edef\nlst{\the\toks@,\newstuff}%
% \fi
% }%
% % check to see if for loop was prematurely terminated
% \if@endfor
% % loop may have been terminated during final iteration, in
% % which case \@forremainder is empty.
% \ifx\@forremainder\@empty
% % do nothing
% \else
% % loop prematurely ended, append remainder of original list
% % to new list
% \expandafter\toks@\expandafter{\nlst}%
% \edef\nlst{\the\toks@,\@forremainder}%
% \fi
% \else
% % wasn't prematurely terminated, so new value hasn't been added
% % add now.
% \expandafter\toks@\expandafter{\nlst}%
% \ifx\nlst\@empty
% \edef\nlst{\number#1}%
% \else
% \edef\nlst{\the\toks@,\number#1}%
% \fi
% \fi
% \let#2=\nlst
% }
%\end{verbatim}
%The \cs{insertinto} macro can then be used as follows:
%\begin{verbatim}
%\def\mylist{1,2,5,9,12,15,18,20}%
%\def\newval{11}%
%Original list: \mylist. New value: \newval.
%
%\insertinto{\newval}{\mylist}
%New list: \mylist.
%\end{verbatim}
%
%\subsection{Example (numerical insertion sort)}
%
%Care needs to be taken when nesting \cs{@for}-loops.
%Suppose you have a list of unsorted numbers, say
%\begin{verbatim}
%\def\mylist{4,2,7,1,10,11,20,15}
%\end{verbatim}
%and you want to sort the list in numerical order using an insertion
%sort method. To do this, a macro needs to be defined which iterates
%through each element in the unordered list, and the element
%is then inserted into an ordered list. The previous example
%described the macro \cs{insertinto} which does this, but this results
%in nested \cs{@for} commands. The \cs{insertinto} command will need
%to be grouped to avoid errors:
%\begin{verbatim}
%\newcommand*{\insertionsort}[1]{%
%\def\sortedlist{}%
%\@for\val:=#1\do{{\insertinto{\val}{\sortedlist}}}%
%\let#1=\sortedlist
%}
%\end{verbatim}
%This won't work with the definition of \cs{insertinto} as
%given in the previous section, as the grouping causes the
%definition of the sorted list to be localised to that group.
%Replacing
%\begin{verbatim}
%\let#2=\nlst
%\end{verbatim}
%with
%\begin{verbatim}
%\global\let#2=\nlst
%\end{verbatim}
%at the end of the definition of \cs{insertinto} will fix that.
%
%\subsection{Example (looking ahead)}
%
%This example checks the next value to determine if the loop
%is on the last iteration, if it is, it does nothing, otherwise
%it does a semi-colon:
%\begin{verbatim}
%\makeatletter
%\def\mylist{1,2,3,4,5}%
%\@for\val:=\mylist\do{\val
%\ifx\@xfor@nextelement\@nnil \else ;\fi}
%\makeatother
%\end{verbatim}
%which produces:
%\makeatletter
%\def\mylist{1,2,3,4,5}\relax
%\@for\val:=\mylist\do{\val
%\ifx\@xfor@nextelement\@nnil \else ;\fi}
%\makeatother
%
% \StopEventually{\clearpage\phantomsection
%\addcontentsline{toc}{section}{Change History}
%\PrintChanges\phantomsection
%\addcontentsline{toc}{section}{Index}
%\PrintIndex}
%
%
%
%\section{The Code}
%\iffalse
% \begin{macrocode}
%<*xfor.sty>
% \end{macrocode}
%\fi
% Declare package:
% \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xfor}[2007/08/21 v1.03 (NLCT)]
% \end{macrocode}
% Define a switch to determine if the for loop should be
% terminated:
%\begin{macro}{\if@endfor}
% \begin{macrocode}
\newif\if@endfor
% \end{macrocode}
%\end{macro}
% Redefine \cs{@for}, so that it resets \cs{if@endfor}
% and \cs{@forremainder}:
%\begin{macro}{\@for}
% \begin{macrocode}
\long\def\@for#1:=#2\do#3{%
\@endforfalse
\def\@forremainder{}%
\expandafter\def\expandafter\@fortmp\expandafter{#2}%
\ifx\@fortmp\@empty
\else
\expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}%
\fi
}
% \end{macrocode}
%\end{macro}
% Redefine \cs{@fornoop} to be a long command:
%\begin{macro}{\@fornoop}
% \begin{macrocode}
\long\def\@fornoop#1\@@#2#3{}
% \end{macrocode}
%\end{macro}
% Redefine \cs{@forloop} to check for \cs{if@endfor}:
%\begin{macro}{\@forloop}
% \begin{macrocode}
\long\def\@forloop#1,#2,#3\@@#4#5{%
\def#4{#1}%
\ifx#4\@nnil
\else
\@xfor@storenext#2,#3\@@%
#5%
\if@endfor
\@iforgatherrest#2,#3%
\else
\def#4{#2}%
\ifx#4\@nnil
\else
\@xfor@storenext#3,\@nil\@@%
#5%
\if@endfor
\@iforgatherrest#3%
\else
\@iforloop#3\@@#4{#5}%
\fi
\fi
\fi
\fi
}
% \end{macrocode}
%\end{macro}
%\changes{1.02}{2007 August 16}{Added \cs{@xfor@storenext}}%
%\begin{macro}{\@xfor@storenext}
% Stores first argument in \cs{@xfor@nextelement}.
% \begin{macrocode}
\def\@xfor@storenext#1,#2\@@{%
\def\@xfor@nextelement{#1}%
}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@forgatherrest}
% Get remainder of list (stores in \cs{@forremainder}):
%\changes{1.01}{2007 July 11}{made long}%
% \begin{macrocode}
\long\def\@forgatherrest#1,\@nil,\@nil{\def\@forremainder{#1}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@iforgatherrest}
% As above, but there may not be anything before \cs{@nil}:
%\changes{1.01}{2007 July 11}{made long}%
% \begin{macrocode}
\long\def\@iforgatherrest#1\@nil,\@nil{%
\def\@fortmp{#1}%
\ifx\@fortmp\@empty
\def\@forremainder{}%
\else
\@forgatherrest#1\@nil,\@nil
\fi
}
% \end{macrocode}
%\end{macro}
% Redefine \cs{@iforloop} to check \cs{if@endfor}:
%\begin{macro}{\@iforloop}
% \begin{macrocode}
\long\def\@iforloop#1,#2\@@#3#4{%
\def#3{#1}%
\ifx#3\@nnil
\let\@ifornext\@fornoop
\else
\@xfor@storenext#2,\@nil\@@%
#4\relax
\if@endfor
\@iforgatherrest#2\relax
\let\@ifornext\@fornoop
\else
\let\@ifornext\@iforloop
\fi
\fi
\@ifornext#2\@@#3{#4}%
}
% \end{macrocode}
%\end{macro}
%\iffalse
% \begin{macrocode}
%</xfor.sty>
% \end{macrocode}
%\fi
%\Finale
\endinput
|