Debian Codegraph @

texmf-dist/source/generic/multiexpand/multiexpand.dtx

raw
1 % \iffalse meta-comment
2 %<*internal>
3 \iffalse
4 %</internal>
5 %<*readme>
6 Variations on the `\expandafter` TeX primitive
7 ==============================================
8 * E-mail: [email protected]
9 * Released under the LaTeX Project Public License v1.3c or later
10 See http://www.latex-project.org/lppl.txt
11
12 The package defines `\multiexpand`, `\multiexpandafter`, `\MultiExpand`
13 and `\MultiExpandAfter`, replacing large chains of `\expandafter`.
14 These four commands take as an argument the number of expansion to be
15 done. If eTeX is available, this number is evaluated using `\numexpr`.
16
17 Say we want to expand `\C` five times before `\A` and `\B` in `\A\B\C`.
18 The traditional approach would be to insert 31 `\expandafter` before
19 `\A` and the same number before `\B`. With this package one can use
20 any of
21
22 \expandafter\A\expandafter\B\romannumeral\multiexpand{5}\C
23
24 \expandafter\A\romannumeral\multiexpandafter{5}\B\C
25
26 \MultiExpandAfter{2}\A\MultiExpandAfter{5}\B\C
27
28 \MultiExpandAfter{2}\A\MultiExpandAfter{2}\B\MultiExpand{5}\C
29
30 In one step of expansion (triggered by the `\expandafter`'s),
31 `\romannumeral\multiexpand{5}` expands the following token 5 times,
32 whereas `\romannumeral\multiexpandafter{5}` expands the token after
33 that 5 times. The macros `\MultiExpandAfter` and `\MultiExpand`
34 take two steps of expansion, but do not require `\romannumeral`.
35 Another example is that in two steps of expansion,
36
37 \MultiExpandAfter{2}\a\MultiExpandAfter{3}{%
38 \MultiExpandAfter{9}\b\MultiExpandAfter{10}\c\d}
39
40 expands `\d` 10 times, then `\c` 7 times (9-2), then `\b` once (3-2).
41
42 The package can be built from the file `multiexpand.dtx' by running
43
44 pdflatex multiexpand.dtx
45 pdflatex multiexpand.dtx
46
47 %</readme>
48 %<*lvt>
49 \input regression-test.tex\relax
50 \START
51 \LONGTYPEOUT{Author: Bruno Le Floch}
52 %<lvt002> \let\numexpr\relax
53 \input multiexpand.sty\relax
54
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 % Introduce commands that expand to each other, to count how many times
57 % things were expanded.
58 \OMIT
59 \def\i{}
60 \def\v{\i\i\i\i}
61 \def\x{\i\i\i\i\v}
62 \def\l{\i\i\i\i\v\x\x\x\x}
63 \def\c{\i\i\i\i\v\x\x\x\x\l}
64 \def\d{\i\i\i\i\v\x\x\x\x\l\c\c\c\c}
65 \def\m{\i\i\i\i\v\x\x\x\x\l\c\c\c\c\d}
66 \TIMO
67
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 % We test |\MultiExpand|, which needs two expansions. In particular, we see
70 % that the implementation is reasonably quick, and we get the $2345$-th
71 % expansion of |\m\m\m\m|.
72 % 4000-2345 = 1655 = mdclv
73 \TEST{MultiExpand many times (vlcdm)}{%
74 \toks0\expandafter\expandafter\expandafter{\MultiExpand{2345}\m\m\m\m}%
75 \TYPE{\the\toks0.}%
76 }
77
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % Test |\MultiExpandAfter|, two expansions to get the specified expansion.
80 % The results indirectly show that expansions happen in the expected order.
81 % 100 = c
82 % 100-(27-2) = 75 = lxxv
83 % 100-(14-2) = 88 = lxxxviii
84 % 100-38 = 62 = lxii
85 \TEST{MultiExpandAfter chain (c vxxl iiivxxxl iixl)}{%
86 \toks0\expandafter\expandafter\expandafter{%
87 \MultiExpandAfter{27}\c
88 \MultiExpandAfter{14}\c
89 \MultiExpandAfter{38}\c\c}%
90 \TYPE{\the\toks0.}%
91 }
92
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 % Test the order of expansion. The example was given in the original
95 % question that led to writing this package. For the record, this
96 % package (at some point) used 32 |\expandafter| to do the expansion
97 % in the right order, which was better than other approaches; however
98 % this counting ignores the large overheads due to other primitives.
99 \OMIT
100 \def\a#1.{#1}
101 \def\b#1:{#1.}
102 \def\c#1,{#1:}
103 \def\d{Hello world!,}
104 \TIMO
105 %
106 \TEST{MultiExpandAfter order}{%
107 \toks0\MultiExpandAfter{3}{%
108 \MultiExpandAfter{3}\a
109 \MultiExpandAfter{3}\b
110 \MultiExpandAfter{1}\c
111 \d}%
112 \TYPE{\the\toks0}%
113 }
114
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % The other example from the same question (expanding seven tokens once
117 % each in the reverse order) takes 74 \expandafter.
118 \OMIT
119 \def\b.{.b} \def\c.{.c} \def\d.{.d}
120 \def\e.{.e} \def\f.{.f} \def\g.{.g}
121 \TIMO
122 %
123 \TEST{MultiExpandAfter order (2)}{%
124 \toks0\MultiExpandAfter{2}{%
125 \MultiExpandAfter3\a \MultiExpandAfter3\b
126 \MultiExpandAfter3\c \MultiExpandAfter3\d
127 \MultiExpandAfter3\e \MultiExpandAfter1\f\g.}%
128 \TYPE{\the\toks0}%
129 }
130
131 \END
132 %</lvt>
133 %<*tlg>
134 This is a generated file for the l3build validation system.
135 Don't change this file in any respect.
136 Author: Bruno Le Floch
137 (multiexpand.sty)
138 ============================================================
139 TEST 1: MultiExpand many times (vlcdm)
140 ============================================================
141 \v \l \c \d \m .
142 ============================================================
143 ============================================================
144 TEST 2: MultiExpandAfter chain (c vxxl iiivxxxl iixl)
145 ============================================================
146 \c \v \x \x \l \i \i \i \v \x \x \x \l \i \i \x \l .
147 ============================================================
148 ============================================================
149 TEST 3: MultiExpandAfter order
150 ============================================================
151 Hello world!
152 ============================================================
153 ============================================================
154 TEST 4: MultiExpandAfter order (2)
155 ============================================================
156 \a .bcdefg
157 ============================================================
158 %</tlg>
159 %<*internal>
160 \fi
161 \def\nameofplainTeX{plain}
162 \ifx\fmtname\nameofplainTeX\else
163 \expandafter\begingroup
164 \fi
165 %</internal>
166 %<*install>
167 \input docstrip.tex
168 \keepsilent
169 \askforoverwritefalse
170 \preamble
171 ----------------------------------------------------------------
172 multiexpand --- trigger multiple expansions in one expansion step.
173 E-mail: [email protected]
174 Released under the LaTeX Project Public License v1.3c or later
175 See http://www.latex-project.org/lppl.txt
176 ----------------------------------------------------------------
177
178 \endpreamble
179 \postamble
180
181 Copyright (C) 2011-2017 by Bruno Le Floch <[email protected]>
182
183 This work may be distributed and/or modified under the
184 conditions of the LaTeX Project Public License (LPPL), either
185 version 1.3c of this license or (at your option) any later
186 version. The latest version of this license is in the file:
187
188 http://www.latex-project.org/lppl.txt
189
190 This work is "maintained" (as per LPPL maintenance status) by
191 Bruno Le Floch.
192
193 This work consists of the file multiexpand.dtx
194 and the derived files multiexpand.ins,
195 multiexpand.pdf and
196 multiexpand.sty.
197
198 \endpostamble
199 \usedir{tex/latex/multiexpand}
200 \generate{
201 \file{\jobname.sty}{\from{\jobname.dtx}{package}}
202 }
203 %</install>
204 %<install>\endbatchfile
205 %<*internal>
206 \usedir{source/latex/multiexpand}
207 \generate{
208 \file{\jobname.ins}{\from{\jobname.dtx}{install}}
209 \file{\jobname 001.lvt}{\from{\jobname.dtx}{lvt}}
210 \file{\jobname 002.lvt}{\from{\jobname.dtx}{lvt,lvt002}}
211 }
212 \nopreamble\nopostamble
213 \generate{
214 \file{\jobname 001.tlg}{\from{\jobname.dtx}{tlg}}
215 \file{\jobname 002.tlg}{\from{\jobname.dtx}{tlg}}
216 }
217 \usedir{doc/latex/multiexpand}
218 \generate{
219 \file{README.md}{\from{\jobname.dtx}{readme}}
220 }
221
222 \ifx\fmtname\nameofplainTeX
223 \expandafter\endbatchfile
224 \else
225 \expandafter\endgroup
226 \fi
227 %</internal>
228 %<*driver>
229 \documentclass[12pt]{ltxdoc}
230 \usepackage[T1]{fontenc}
231 \usepackage{hologo}
232 \usepackage{multiexpand}
233 \usepackage[margin=4cm]{geometry}
234 \usepackage{hyperref}
235 \RecordChanges
236 \begin{document}
237 \DocInput{multiexpand.dtx}
238 \end{document}
239 %</driver>
240 % \fi
241 %
242 % \CheckSum{126}
243 %
244 % \def\fileversion{v1.5} \def\filedate{2017/11/29}
245 %\title{\texttt{multiexpand}\\
246 % Trigger multiple expansions \\ in one expansion step\thanks{This file
247 % describes version \fileversion, last revised \filedate.}}
248 %\author{Bruno Le Floch\thanks{E-mail: [email protected]}
249 % \thanks{I have gathered ideas from various posts in the \texttt{\{TeX\}}
250 % community at \url{http://tex.stackexchange.com}. Thanks to their authors.}}
251 %\date{Released \filedate}
252 %
253 %\maketitle
254 %
255 % \tableofcontents
256 % \changes{v1.0}{2011/02/15}{First version with documentation}
257 % \changes{v1.1}{2013/01/08}{Version submitted to CTAN}
258 % \changes{v1.2}{2013/01/09}{Use fewer expandafter for large arguments}
259 % \changes{v1.2}{2013/01/09}{Change ME prefix to multiexpand}
260 % \changes{v1.4}{2015/09/20}{Clarify that eTeX is not required}
261 % \changes{v1.5}{2017/11/29}{Updates due to l3build changes}
262 %
263 % \section{Two user commands}
264 %
265 % \begin{itemize}
266 % \item For $n>0$, expanding |\MultiExpand{|$n$|}\macro| twice gives
267 % the $n$-th expansion of |\macro|.
268 % \item For $n>0$, expanding |\MultiExpandAfter{|$n$|}\macroA\macroB|
269 % twice expands |\macroB| $n$ times before expanding |\macroA|.
270 % \end{itemize}
271 % Note that neither functions work for $n = 0$.
272 %
273 % These can typically be combined as
274 % \begin{verbatim}
275 % \MultiExpand{7}%
276 % \MultiExpandAfter{4}\a\MultiExpandAfter{7}\b%
277 % \MultiExpandAfter{3}\c\d
278 % \end{verbatim}
279 % which would expand |\d| $3$~times, then |\c| $5$~times ($2$~of the $7$~times
280 % were used to expand |\MultiExpandAfter{3}|), then |\b| twice ($4-2$), and
281 % finally |\a| five times ($7-2$). Note that all this happens in precisely
282 % \emph{two} steps of expansion.
283 %
284 % In some cases, one needs to achieve the same effect in \emph{one} step
285 % only. For this, we use the first expansion of |\MultiExpand|, which
286 % is |\romannumeral| |\multiexpand|, or of |\MultiExpandAfter|, which is
287 % |\romannumeral| |\multiexpandafter|. In detail, expanding |\romannumeral|
288 % |\multiexpand{|$n$|}| once expands the following token $n$ times, and
289 % similarly for |\romannumeral| |\multiexpandafter{|$n$|}|.
290 %
291 % These are especially useful when we want to expand several times a
292 % very specific token which is buried behind many others. For instance,
293 % expanding the following code once
294 % \begin{verbatim}
295 % \expandafter\macroA\expandafter\macroB
296 % \romannumeral\multiexpandafter{4}\macroC\macroD
297 % \end{verbatim}
298 % will expand |\macroD| $4$ times before the three other macros.
299 %
300 % Note: as we mentionned, this breaks for $n=0$. But in this case, consider
301 % using |\expandafter\empty|, or a variant thereof.
302 %
303 % \section{Implementation}
304 %\StopEventually{\PrintChanges\PrintIndex}
305 % \begin{macrocode}
306 %<*package>
307 % \end{macrocode}
308 %
309 % We work inside a group, to change the catcode of |@|. So we will only
310 % do |\gdef|s. Note that this code can be read several times with no
311 % issue; no need to bother to check whether it was already read or not.
312 % \begin{macrocode}
313 \begingroup
314 \catcode `\@=11
315 % \end{macrocode}
316 %
317 % \subsection{Common to the \hologo{eTeX} and non-\hologo{eTeX} cases}
318 %
319 % For the ``lazy'', who do not want to use |\romannumeral|, we provide
320 % |\MultiExpand| and |\MultiExpandAfter|, simple shorthands. A drawback
321 % is that they require two steps of expansion rather than only one.
322 % \begin{macrocode}
323 \gdef \MultiExpand {\romannumeral \multiexpand }
324 \gdef \MultiExpandAfter {\romannumeral \multiexpandafter }
325 % \end{macrocode}
326 %
327 % \subsection{Without \hologo{eTeX}'s \cs{numexpr}}
328 % \changes{v1.3}{2015/03/03}{Support TeX with no numexpr}
329 %
330 % No need for the usual |\begingroup\expandafter\endgroup| to prevent
331 % |\numexpr| from being set to |\relax|, because we are already in a
332 % group.
333 % \begin{macrocode}
334 \expandafter \ifx \csname numexpr\endcsname \relax
335 % \end{macrocode}
336 %
337 % A helper.
338 % \begin{macrocode}
339 \long \gdef \multiexpand@gobble #1{}
340 % \end{macrocode}
341 %
342 % The user commands |\multiexpand| and |\multiexpandafter|, to be used
343 % after |\romannumeral|. They only differ a little bit.
344 % \begin{macrocode}
345 \gdef \multiexpand {\multiexpand@aux \multiexpand@ }
346 \gdef \multiexpandafter {\multiexpand@aux \multiexpand@after }
347 % \end{macrocode}
348 % The user commands receives a number, and to accept various forms of
349 % numbers we hit it with |\number|. If it is non-positive, stop the
350 % |\romannumeral| expansion with |0| and a space. Otherwise, reverse
351 % the number, to make it easy to subtract~$1$.
352 % \begin{macrocode}
353 \long \gdef \multiexpand@aux #1#2%
354 {\expandafter \multiexpand@test \number #2;#1}
355 \gdef \multiexpand@test #1;#2%
356 {%
357 \ifnum #1>0
358 \multiexpand@reverse #1{?\multiexpand@reverse@end }?;;#2%
359 \fi
360 0 %
361 }
362 % \end{macrocode}
363 % The macro |\multiexpand@reverse| puts characters from the number one
364 % by one (as~|#1|) after the semicolon, to reverse the number. After
365 % the last digit, |#1| is |{?\multiexpand@reverse@end}|. The question
366 % mark is removed by |\multiexpand@gobble|, and the |reverse@end|
367 % macro cleans up. In particular, one should not forget to close the
368 % conditional using~|#5|, which is the trailing~|\fi|. At this stage,
369 % |#4| is the function that distinguishes |\multiexpand| from
370 % |\multiexpand@after|, and |#3| is the reversed
371 % number. \begin{macrocode}
372 \gdef \multiexpand@reverse #1#2;%
373 {\multiexpand@gobble #1\multiexpand@reverse #2;#1}
374 \gdef \multiexpand@reverse@end #1;?#2#3;#4#50
375 {#5\multiexpand@iterate #41#3;}
376 % \end{macrocode}
377 % The macro |\multiexpand@iterate| applies a \meta{function} a certain
378 % number of times to what follows in the input stream. It expects to
379 % receive \meta{function} \meta{nines} |1|\meta{reversed number}|;|.
380 % The argument \meta{nines}, made entirely of the digit~|9|, is used to
381 % compute carries when subtracting~$1$, and is initally empty. As a
382 % concrete example, after |\multiexpand{302}| the successive
383 % calls to |\multiexpand@iterate| would go as follows.
384 % \begin{verbatim}
385 % \multiexpand@iterate \multiexpand@ 1203;
386 % \multiexpand@iterate \multiexpand@ 1103;
387 % \multiexpand@iterate \multiexpand@ 1003;
388 % \multiexpand@iterate \multiexpand@ 9 103;
389 % \multiexpand@iterate \multiexpand@ 99 13;
390 % \multiexpand@iterate \multiexpand@ 1992;
391 % \multiexpand@iterate \multiexpand@ 1892;
392 % \multiexpand@iterate \multiexpand@ 1792;
393 % \end{verbatim}
394 % Note in particular how carries are done in several steps. The details
395 % are left as an exercise to the reader. The most common case is when
396 % |#2| is empty and |#3| is a non-zero digit. Then |\number| is
397 % expanded, triggering |\ifcase| which shifts |#3| by one unit, and |#1|
398 % takes care of expanding the tokens are required by |\multiexpand| or
399 % |\multiexpandafter|. If |#3| is~$0$, then |\multiexpand@zero| is
400 % called, closing the conditional with |#1|, and iterating, this time
401 % with a non-empty \meta{nines}, which are the argument |#2| of a new
402 % call to |\multiexpand@iterate|. Those \meta{nines} are put back into
403 % the number by |\multiexpand@iterate|, unless the next significant
404 % digit is also $0$, in which case |\multiexpand@zero| is called again,
405 % until finding a non-zero digit; at each step, one more $9$ is added to
406 % the \meta{nines}. If all digits are zero, we reach |;| this way, and
407 % end, after cleaning up.
408 % \begin{macrocode}
409 \gdef \multiexpand@iterate #1#21#3%
410 {%
411 \ifx ;#3\multiexpand@end \fi
412 \ifx 0#3\multiexpand@zero \fi
413 \expandafter \multiexpand@iterate
414 \expandafter #1%
415 \number 1#2%
416 \ifcase #3 \or 0\or 1\or 2\or 3\or 4\or 5\or 6\or 7\or 8\fi
417 #1%
418 }
419 \gdef \multiexpand@zero #1#2\number 1#3\ifcase #4\fi #5%
420 {#1\multiexpand@iterate #59#31}
421 \gdef \multiexpand@end #1#2\ifcase #3\fi #4{#10 }
422 % \end{macrocode}
423 % Finally, the two different expansion commands.
424 % \begin{macrocode}
425 \gdef \multiexpand@ #1;{#1\expandafter ;}
426 \gdef \multiexpand@after #1;{#1\expandafter ;\expandafter }
427 % \end{macrocode}
428 %
429 % \subsection{With \hologo{eTeX}}
430 %
431 % \begin{macrocode}
432 \else
433 % \end{macrocode}
434 %
435 % With \hologo{eTeX}, everything is much easier, since the engine knows
436 % how to subtract~$1$.
437 %
438 % The main looping macros expect their arguments as an integer followed
439 % by a semicolon. As long as the argument is at least~$2$, decrement
440 % it, and expand what follows. Once the argument is~$1$ (or less: the
441 % macros are not meant to handle that case), call |\multiexpand@end| to clean up
442 % and stop looping.
443 % \begin{macrocode}
444 \gdef \multiexpand@ #1;%
445 {%
446 \ifnum #1<2 \multiexpand@end \fi
447 \expandafter \multiexpand@
448 \the \numexpr #1-1\expandafter ;%
449 }
450 \gdef \multiexpand@after #1;%
451 {%
452 \ifnum #1<2 \multiexpand@end \fi
453 \expandafter \multiexpand@after
454 \the \numexpr #1-1\expandafter ;\expandafter
455 }
456 % \end{macrocode}
457 % The looping macros are used within an overarching |\romannumeral|
458 % expansion, which we end with a |0| and a space, as well as the
459 % appropriate |\expandafter|. Here, |#1| is |\fi| which needs to remain
460 % to close the conditional, |#2| is |\expandafter|, and there is a
461 % trailing |\expandafter| in the case of |\multiexpand@after|.
462 % \begin{macrocode}
463 \gdef \multiexpand@end #1#2#3;{#10#2 }
464 % \end{macrocode}
465 % Finally, user commands, used as |\romannumeral| |\multiexpand(after)|.
466 % Those evaluate their argument, and pass it to |\multiexpand@(after)|.
467 % The argument might contain |\par| tokens (who knows)
468 % \begin{macrocode}
469 \long \gdef \multiexpand #1%
470 {\expandafter \multiexpand@ \the \numexpr #1;}
471 \long \gdef \multiexpandafter #1%
472 {\expandafter \multiexpand@after \the \numexpr #1;}
473 % \end{macrocode}
474 %
475 % \begin{macrocode}
476 \fi
477 % \end{macrocode}
478 %
479 % Close the group.
480 % \begin{macrocode}
481 \endgroup
482 % \end{macrocode}
483 %
484 % \begin{macrocode}
485 %</package>
486 % \end{macrocode}
487 %\Finale
488 \endinput
489
490
491