Debian Codegraph @

texmf-dist/tex/generic/multido/multido.tex

raw
1 %% $Id: multido.tex 317 2010-05-14 21:54:15Z herbert $
2 %% BEGIN multido.tex
3 %%
4 \def\fileversion{1.42}
5 \def\filedate{2010/05/14}
6 %%
7 %% COPYRIGHT 1993, 1994, 1999 by Timothy Van Zandt, [email protected].
8 %% Version 1.41 (2004) prepared by Rolf Niepraschk <[email protected]>
9 %% Herbert Voss <[email protected]>
10 %% COPYRIGHT 2010 by Herbert Voss <[email protected]>
11 %%
12 %%
13 %% This program can be redistributed and/or modified under the terms
14 %% of the LaTeX Project Public License Distributed from CTAN
15 %% archives in directory macros/latex/base/lppl.txt.
16 %%
17 %% DESCRIPTION:
18 %% multido.tex/multido.sty contains a loop macro, \multido, that supports
19 %% fixed-point addition and has a nice interface. Among other things, it
20 %% is useful for pictures and putting numbers on axes. multido.tex
21 %% is compatible with most TeX macro packages, including Plain TeX, LaTeX,
22 %% AmSTeX, and Ams-LaTeX.
23 %%
24 %% CODE:
25 % These macros use TeX primitives, plus the Plain TeX commands:
26 % \dimen@, \dimen@i, \count@, \newcount, \newtoks, \@ne, \tw@, \@z@.
27 %
28 % Check if file is loaded, announce file on terminal, and take care of @:
29 %
30 \message{ v\fileversion, \filedate\space <tvz>}
31 \csname MultidoLoaded\endcsname
32 \let\MultidoLoaded\endinput
33 \edef\TheAtCode{\the\catcode`\@}
34 \catcode`\@=11
35
36 % These are some useful hacks. "\multido@dimtonum" strips the value of "#1",
37 % a dimension register, of the "pt", and assigns the result to "#2",
38 % a command sequence.
39
40 \def\@empty{}
41 \def\@nnil{\@nil}
42 \def\multido@dimtonum#1#2{\edef#2{\multido@@dimtonum#1}}
43 \def\multido@@dimtonum#1{\expandafter\multido@@@dimtonum\the#1}
44 \edef\next{%
45 \noexpand\def\noexpand\multido@@@dimtonum##1\string p\string t{##1}}
46 \next
47
48 % "\multido@count" stores the number of repetitions. "\multidocount" keeps
49 % track of the iteration. These are also used locally as scratch counters
50 % by "\FPadd@". "\multido@stuff" is used to store the <stuff> that is
51 % to be repeated.
52
53 \newcount\multido@count
54 \newcount\multidocount
55 \newtoks\multido@stuff
56
57 \def\multido{\multido@{}{\begingroup}{\endgroup}}
58 \def\mmultido{\multido@{\multido@stepvar}{\begingroup}{\endgroup}}
59 \def\Multido{\multido@{}{}{}}
60 \def\MMultido{\multido@{\multido@stepvar}{}{}}
61
62 % "\multido@initvar" processes the variable declarations, initializing
63 % the value of the variables and defining "\multido@stepvar" to increment
64 % the variables with each repetition. "\multido@count" is set to the number
65 % of repetitions.
66
67 \long\def\multido@#1#2#3#4#5#6{%
68 #2%
69 \multido@count=#5\relax
70 \ifnum\multido@count=\z@\else\multido@@{#1}{#4}{#6}\fi
71 #3%
72 \ignorespaces}
73 \def\multido@@#1#2#3{%
74 \def\multido@stepvar{}%
75 \def\do{\noexpand\do\noexpand}%
76 \multido@initvar#2,\@nil,%
77 \let\do\noexpand
78 \edef\multido@stepvar{\multido@stepvar}%
79 \ifnum\multido@count<\z@\multido@count=-\multido@count\fi
80 \multidocount=1\relax
81 #1%
82 \multido@stuff{#3}%
83 \multido@loop}
84 \def\multido@loop{%
85 \the\multido@stuff
86 \ifnum\multidocount<\multido@count
87 \advance\multidocount\@ne
88 \multido@stepvar
89 \expandafter\multido@loop
90 \fi}
91 \def\multidostop{\multidocount=\multido@count}
92 \def\multido@badvar{%
93 \multido@count=0
94 \errhelp{\multido command will be skipped.}%
95 \errmessage{Bad \string\multido\space variable declaration}}
96 \def\multido@initvar#1,{%
97 \def\multido@temp{#1}%
98 \ifx\multido@temp\@nnil\else
99 \ifx\multido@temp\@empty\else
100 \multido@@initvar#1\@nil=+\@nil\relax
101 \fi
102 \expandafter\multido@initvar
103 \fi}
104 \def\multido@@initvar#1=#2+#3\@nil#4\relax{%
105 \ifx\@empty#4\@empty
106 \multido@badvar
107 \else
108 \multido@vartype#1\@empty
109 \ifx\multido@temp\relax
110 \multido@badvar
111 \else
112 \multido@temp{#2}{#3}#1%
113 \fi
114 \fi}
115 \def\multido@vartype#1{%
116 \ifcat\noexpand\@nil\noexpand#1%
117 \expandafter\multido@@vartype\string#1\@nil
118 \else
119 \let\multido@temp\relax
120 \fi}
121 \def\multido@@vartype#1#2#3\@nil{%
122 \expandafter\let\expandafter\multido@temp
123 \csname multido@init@#2\endcsname}
124 \def\MultidoCheckNames{%
125 \let\multido@@@vartype\multido@vartype
126 \def\multido@vartype##1{%
127 \ifx\undefined##1%
128 \multido@@@vartype{##1}%
129 \else
130 \errhelp{\multido command will be skipped.}%
131 \errmessage{Multido variable \string##1 already defined}%
132 \let\multido@temp\relax
133 \fi}}
134
135 % For each variable type, we must now define \multido@init<vartype>.
136 % The syntax of these macros is:
137 %
138 % \multido@init@<vartype>{initial value}{increment}{variable}
139 %
140 % and the outcome should be:
141 %
142 % 1. Set <variable> to the initial value.
143 % 2. Invoke \multido@addtostep{step stuff}, where <step stuff> is
144 % whatever should be done to increment the variable. <step stuff> is
145 % expanded first, with expansion suppressed by "\do".
146
147 \def\multido@addtostep#1{\edef\multido@stepvar{\multido@stepvar#1}}
148
149 % Variable type for dimensions ("d", "D"):
150
151 \def\multido@init@d#1#2#3{%
152 \expandafter\ifx\csname pssetlength\endcsname\relax
153 \dimen@=#1\relax\dimen@i=#2\relax
154 \else
155 \pssetlength\dimen@{#1}\pssetlength\dimen@i{#2}%
156 \fi
157 \edef#3{\number\dimen@ sp}%
158 \ifnum\multido@count<\z@\dimen@i=-\dimen@i\fi
159 \multido@addtostep{\do\multido@step@d{\do#3}{\number\dimen@i sp}}}
160 \def\multido@step@d#1#2{%
161 \dimen@=#1\advance\dimen@#2
162 \edef#1{\number\dimen@ sp}}%
163 \def\multido@init@D{\multido@init@d}
164
165 % Variable type for integers ("i", "I"):
166
167 \def\multido@init@i#1#2#3{%
168 \count@=#1\relax
169 \edef#3{\the\count@}%
170 \count@=#2\relax
171 \ifnum\multido@count<\z@\count@=-\count@\fi
172 \multido@addtostep{\do\multido@step@i{\do#3}{\the\count@}}}
173 \def\multido@step@i#1#2{%
174 \count@=#1\advance\count@ by #2
175 \edef#1{\the\count@}}
176 \def\multido@init@I{\multido@init@i}
177
178 % Variable type for reals ("r", "R").
179
180 \def\multido@init@r#1#2#3{%
181 \dimen@=#1pt
182 \multido@dimtonum\dimen@#3%
183 \dimen@=#2pt
184 \ifnum\multido@count<\z@\dimen@=-\dimen@\fi
185 \multido@addtostep{\do\multido@step@r{\do#3}{\number\dimen@ sp}}}
186 \def\multido@step@r#1#2{%
187 \dimen@=#1pt\advance\dimen@#2
188 \multido@dimtonum\dimen@#1}
189 \def\multido@init@R{\multido@init@r}
190
191 % Variable type for numbers ("n", "N").
192
193 \def\multido@init@n#1#2#3{%
194 \edef#3{#1}%
195 \ifnum\multido@count<\z@\expandafter\fpSub\else\expandafter\fpAdd\fi
196 {0}{#2}\multido@temp
197 \multido@addtostep{\do\fpAdd{\do#3}{\multido@temp}{\do#3}}}
198 \def\multido@init@N{\multido@init@n}
199
200 % Simple fixed-point addition couldn't be harder.
201 % "\dimen@" and "\dimen@i" are used as scratch {\em counters} (to avoid
202 % creating a new counter). "\count@", "\multido@count" and "\multidocount"
203 % are also used as scratch counters.
204
205 % This is probably pretty close to optimal, given the requirements that
206 % leading and trailing spaces be OK in the arguments, and that there need
207 % not be any numbers to the left of the decimal.
208
209 % RN modification begin -- 2004/05/17; [email protected]
210 % \def\FPadd#1#2#3{%
211 % \edef\multido@temp{#1..\noexpand\@nil#2}%
212 % \expandafter\FPadd@\multido@temp..\@nil
213 % \let#3\multido@temp}
214 % \def\FPsub#1#2{%
215 % \edef\multido@temp{\noexpand\FPsub@#2\noexpand\@empty}%
216 % \FPadd{#1}{\multido@temp}}
217 \def\fpAdd#1#2#3{%
218 \edef\multido@temp{#1..\noexpand\@nil#2}%
219 \expandafter\FPadd@\multido@temp..\@nil
220 \let#3\multido@temp}
221 \def\fpSub#1#2{%
222 \edef\multido@temp{\noexpand\FPsub@#2\noexpand\@empty}%
223 \fpAdd{#1}{\multido@temp}}
224 \ifx\FPadd\undefined\let\FPadd\fpAdd\let\FPsub\fpSub\fi
225 % RN modification end
226 \def\FPsub@#1{\ifx-#1\else-#1\fi}
227 \def\FPadd@#1.#2.#3\@nil#4.#5.#6\@nil{%
228 \begingroup
229 \def\multido@temp{\let\next\relax}%
230 \let\next\z@
231 \afterassignment\multido@temp\count@=#1\next
232 \dimen@i=0#2sp\relax
233 \let\next\z@
234 \afterassignment\multido@temp\multido@count=#4\next
235 \multidocount=0#5\relax
236 \dimen@=\number\count@ sp
237 \count@=\@ne
238 \FPadd@@@#500000000\@nil
239 \ifnum#11<\z@
240 \count@=-\tw@
241 \dimen@=-\dimen@
242 \fi
243 \ifnum#41<\z@
244 \count@=-\count@
245 \multido@count=-\multido@count
246 \fi
247 \ifnum\count@>\z@
248 \advance\multido@count\dimen@
249 \advance\multidocount\dimen@i
250 \ifnum\multidocount<\multido@temp\relax
251 \advance\multidocount\multido@temp\relax
252 \else
253 \advance\multido@count\@ne
254 \fi
255 \advance\count@-3
256 \else
257 \advance\multido@count-\dimen@
258 \advance\multidocount-\dimen@i
259 \ifnum\multido@count<\z@
260 \multido@count=-\multido@count
261 \multidocount=-\multidocount
262 \advance\count@\@ne
263 \else
264 \ifnum\multido@count=\z@
265 \ifnum\multidocount<\z@
266 \multidocount=-\multidocount
267 \advance\count@\@ne
268 \fi
269 \fi
270 \fi
271 \ifnum\multidocount<\z@
272 \advance\multidocount\multido@temp
273 \advance\multido@count-\@ne
274 \fi
275 \advance\multidocount\multido@temp\relax
276 \fi
277 \expandafter\FPadd@@\the\multidocount\@empty\@nil
278 \endgroup}
279 \def\FPadd@@#1#2#3\@nil{\xdef\multido@temp{%
280 \ifnum\count@=-1-\fi
281 \the\multido@count\ifx#2\@empty\else.#2#3\fi}}
282 \def\FPadd@@@#1#2#3#4#5#6#7#8#9\@nil{\def\multido@temp{1#9}}
283
284 \catcode`\@=\TheAtCode\relax
285
286 \endinput
287 %%
288 %% END multido.tex