Debian Codegraph @

texmf-dist/doc/latex/functional/functional.tex

raw
1 % -*- coding: utf-8 -*-
2 % !TEX program = lualatex
3 \documentclass[oneside]{book}
4 \usepackage[a4paper,margin=2.5cm]{geometry}
5
6 \newcommand*{\myversion}{2024C}
7 \newcommand*{\mydate}{Version \myversion\ (\the\year-\mylpad\month-\mylpad\day)}
8 \newcommand*{\mylpad}[1]{\ifnum#1<10 0\the#1\else\the#1\fi}
9
10 \setlength{\parindent}{0pt}
11 \setlength{\parskip}{4pt plus 1pt minus 1pt}
12
13 \usepackage{codehigh}
14
15 \colorlet{highback}{blue9}
16 %\CodeHigh{lite}
17 \CodeHigh{language=latex/latex2,style/main=highback,style/code=highback}
18 \NewCodeHighEnv{code}{style/main=gray9,style/code=gray9}
19 \NewCodeHighEnv{demo}{style/main=gray9,style/code=gray9,demo}
20
21 \usepackage{enumitem}
22
23 \NewDocumentCommand\MySubScript{m}{$_{#1}$}
24
25 \ExplSyntaxOn
26 \NewDocumentCommand\PrintVarList{m}{
27 \clist_set:Nn \l_tmpa_clist {#1}
28 \clist_map_inline:Nn \l_tmpa_clist
29 {
30 \token_to_str:N ##1 ~
31 }
32 }
33 \NewDocumentCommand\RelaceChacters{m}{
34 \tl_set:Nn \lTmpaTl {#1}
35 \regex_replace_once:nnN { \_ } { \c{MySubScript} } \lTmpaTl
36 }
37 \NewDocumentCommand\RelaceUnderScoreAll{m}{
38 \tl_set:Nn \lTmpaTl {#1}
39 \regex_replace_all:nnN { \_ } { \c{_} } \lTmpaTl
40 \lTmpaTl
41 }
42 \ExplSyntaxOff
43
44 \NewDocumentEnvironment{variable}{om}{
45 \vspace{5pt}
46 \begin{minipage}{\linewidth}
47 \hrule\vspace{4pt}\obeylines%
48 \begingroup
49 \ttfamily\bfseries\color{azure3}
50 \PrintVarList{#2}
51 \endgroup
52 \par\vspace{4pt}\hrule
53 \end{minipage}\par\nopagebreak\vspace{4pt}
54 }{%
55 \vspace{5pt}%
56 }
57
58 \NewDocumentEnvironment{function}{om}{
59 \vspace{5pt}%
60 }{\vspace{5pt}}
61
62 \NewDocumentEnvironment{syntax}{}{%
63 \begin{minipage}{\linewidth}
64 \hrule\vspace{4pt}\obeylines%
65 }{%
66 \par\vspace{4pt}\hrule
67 \end{minipage}\par\nopagebreak\vspace{4pt}
68 }
69
70 \NewDocumentEnvironment{texnote}{}{}{}
71
72 \NewDocumentCommand\cs{O{}m}{%
73 \texttt{\bfseries\color{purple3}\cBackslashStr\RelaceUnderScoreAll{#2}}%
74 }
75 \NewDocumentCommand\meta{m}{%
76 \RelaceChacters{#1}%
77 \textsl{$\langle$\ignorespaces\lTmpaTl\unskip$\rangle$}%
78 }
79 \NewDocumentCommand\Arg{m}{%
80 \RelaceChacters{#1}%
81 \texttt{\{}\textsl{$\langle$\ignorespaces\lTmpaTl\unskip$\rangle$}\texttt{\}}%
82 }
83
84 \NewDocumentCommand\pkg{m}{\textsf{#1}}
85
86 \NewDocumentCommand\nan{}{\texttt{NaN}}
87 \NewDocumentCommand\enquote{m}{``#1''}
88
89 \let\tn=\cs
90
91 \RenewDocumentCommand\emph{m}{%
92 \underline{\textsl{#1}}%
93 }
94
95 \newenvironment{l3regex-syntax}
96 {\begin{itemize}\def\\{\char`\\}\def\makelabel##1{\hss\llap{\ttfamily##1}}}
97 {\end{itemize}}
98
99 \usepackage{shortvrb}
100
101 \NewDocumentCommand \MyMakeShortVerb {} {%
102 \MakeShortVerb \|%
103 \MakeShortVerb \"%
104 }
105 \NewDocumentCommand \MyDeleteShortVerb {} {%
106 \DeleteShortVerb \|%
107 \DeleteShortVerb \"%
108 }
109
110 \AtBeginDocument{\MyMakeShortVerb}
111 \AtEndDocument{\MyDeleteShortVerb}
112 \AddToHook{env/demohigh/before}{\MyDeleteShortVerb}
113 \AddToHook{env/demohigh/after}{\MyMakeShortVerb}
114
115 \usepackage{hologo}
116 \providecommand*\LuaTeX{\hologo{LuaTeX}}
117 \providecommand*\pdfTeX{\hologo{pdfTeX}}
118 \providecommand*\XeTeX{\hologo{XeTeX}}
119
120 \usepackage{amsmath}
121
122 \usepackage{hyperref}
123 \hypersetup{
124 colorlinks=true,
125 urlcolor=blue3,
126 linkcolor=blue3,
127 }
128
129 \makeatletter
130 \ExplSyntaxOn
131 \NewDocumentCommand\MyCheckLoadPackage{m}{
132 \@ifpackageloaded{docmute}{
133 \usepackage{#1}
134 }{
135 \str_if_eq:onTF{\jobname}{#1}{
136 \usepackage{#1}
137 }{
138 \debug_on:n{check-declarations}
139 \usepackage{#1}
140 \debug_off:n{check-declarations}
141 }
142 }
143 }
144 \ExplSyntaxOff
145 \makeatother
146 \MyCheckLoadPackage{functional}
147 %\usepackage{functional}
148 %\Functional{scoping=false,tracing=true}
149
150 \begin{document}
151
152 \title{\sffamily Intuitive \textcolor{green3}{Functional} Programming Interface for LaTeX2}
153 \author{Jianrui Lyu ([email protected])\\\url{https://github.com/lvjr/functional}}
154 \date{\mydate\vspace{1cm}\\\myabstract\vspace{10cm}}
155
156 \newcommand\myabstract{\parbox{\linewidth}{\hrule\vspace{0.8em}\large
157 This package provides an intuitive functional programming interface for LaTeX2,
158 which is an alternative choice to \textsf{expl3} or LuaTeX,
159 if you want to do programming in LaTeX.
160 \par\vspace{0.5em}
161 Although there are functions in LaTeX3 programming layer (\textsf{expl3}),
162 the evaluation of them is from outside to inside.
163 With this package, the evaluation of functions is from inside to outside,
164 which is the same as other programming languages such as \texttt{Lua}.
165 In this way, it is rather easy to debug code too.
166 \par\vspace{0.5em}
167 Note that many paragraphs in this manual are copied from the documentation of \textsf{expl3}.
168 \par\vspace{0.8em}\hrule}}
169
170 {\let\newpage\relax\vspace{-4cm}\maketitle}
171
172 \tableofcontents
173
174 \chapter{Overview of Features}
175
176 \section{Evaluation from Inside to Outside}
177
178 We will compare our first example with a similar \verb!Lua! example:
179
180 \begin{minipage}{0.55\textwidth}
181 \begin{codehigh}
182 \IgnoreSpacesOn
183 \prgNewFunction \mathSquare { m } {
184 \intSet \lTmpaInt {\intEval {#1 * #1}}
185 \prgReturn {\expValue \lTmpaInt}
186 }
187 \IgnoreSpacesOff
188 \mathSquare{5}
189 \mathSquare{\mathSquare{5}}
190 \end{codehigh}
191 \end{minipage}%
192 \begin{minipage}{0.45\textwidth}
193 \begin{code}
194 -- define a function --
195 function MathSquare (arg)
196 local lTmpaInt = arg * arg
197 return lTmpaInt
198 end
199 -- use the function --
200 print(MathSquare(5))
201 print(MathSquare(MathSquare(5)))
202 \end{code}
203 \end{minipage}
204
205 %\IgnoreSpacesOn
206 %\prgNewFunction \mathSquare { m }{
207 % \intSet \lTmpaInt { \intEval { #1 * #1 } }
208 % \prgReturn { \expValue \lTmpaInt }
209 %}
210 %\IgnoreSpacesOff
211 %\mathSquare{5}
212 %\mathSquare{\mathSquare{5}}
213
214 %\IgnoreSpacesOn
215 %\prgNewFunction \mathCubic { m }
216 % {
217 % \intSet \lTmpaInt { \intEval { #1 * #1 * #1 } }
218 % \prgReturn { \expValue \lTmpaInt }
219 % }
220 %\IgnoreSpacesOff
221 %\mathCubic{2}
222 %\mathCubic{\mathCubic{2}}
223
224 Both examples calculate first the square of $5$ and produce $25$,
225 then calculate the square of $25$ and produce $625$.
226 In contrast to \verb!expl3!, this \verb!functional! package
227 does evaluation of functions from inside to outside,
228 which means composition of functions works like other programming languages
229 such as \verb!Lua! or \verb!JavaScript!.
230
231 You can define new functions with \cs{prgNewFunction} command.
232 To make composition of functions work as expected,
233 every function \emph{must not} insert directly any token to the input stream.
234 Instead, a function \emph{must} pass the result (if any) to \verb!functional! package
235 with \cs{prgReturn} command. And \verb!functional! package is responsible for
236 inserting result tokens to the input stream at the appropriate time.
237
238 To remove space tokens inside function code in defining functions,
239 you'd better put function definitions inside \verb!\IgnoreSpacesOn! and
240 \verb!\IgnoreSpacesOff! block. Within this block, \verb!~! is used to input a space.
241
242 At the end of this section,
243 we will compare our factorial example with a similar \verb!Lua! example:
244
245 \begin{minipage}{0.68\textwidth}
246 \begin{codehigh}
247 \IgnoreSpacesOn
248 \prgNewFunction \mathFact { m } {
249 \intCompareTF {#1} = {0} {
250 \prgReturn {1}
251 }{
252 \prgReturn {\intEval{#1*\mathFact{\intEval{#1-1}}}}
253 }
254 }
255 \IgnoreSpacesOff
256 \mathFact{4}
257 \end{codehigh}
258 \end{minipage}%
259 \begin{minipage}{0.32\textwidth}
260 \begin{code}
261 -- define a function --
262 function Fact (n)
263 if n == 0 then
264 return 1
265 else
266 return n * Fact(n-1)
267 end
268 end
269 -- use the function --
270 print(Fact(4))
271 \end{code}
272 \end{minipage}
273
274 %\IgnoreSpacesOn
275 %\prgNewFunction \Factorial { m } {
276 % \intCompareTF {#1} = {0} {
277 % \prgReturn {1}
278 % }{
279 % \prgReturn { \intMathMult {#1} { \Factorial { \intMathSub{#1}{1} } } }
280 % }
281 %}
282 %\IgnoreSpacesOff
283 %\Factorial{0}
284 %\Factorial{4}
285
286 \section{Group Scoping of Functions}
287
288 In \verb!Lua! language, a function or a condition expression makes a block,
289 and the values of local variables will be reset after a block.
290 In \verb!functional! package, a condition expression is in fact a function,
291 and you can make every function become a group by setting
292 \verb!\Functional{scoping=true}!. For example
293
294 \begin{minipage}{0.58\textwidth}
295 \begin{codehigh}
296 \Functional{scoping=true}
297 \IgnoreSpacesOn
298 \intSet \lTmpaInt {1}
299 \intVarLog \lTmpaInt % ---- 1
300 \prgNewFunction \someFun { } {
301 \intSet \lTmpaInt {2}
302 \intVarLog \lTmpaInt % ---- 2
303 \intCompareTF {1} > {0} {
304 \intSet \lTmpaInt {3}
305 \intVarLog \lTmpaInt % ---- 3
306 }{ }
307 \intVarLog \lTmpaInt % ---- 2
308 }
309 \someFun
310 \intVarLog \lTmpaInt % ---- 1
311 \IgnoreSpacesOff
312 \end{codehigh}
313 \end{minipage}%
314 \begin{minipage}{0.42\textwidth}
315 \begin{code}
316 -- lua code --
317 -- begin example --
318 local a = 1
319 print(a) ---- 1
320 function SomeFun()
321 local a = 2
322 print(a) ---- 2
323 if 1 > 0 then
324 local a = 3
325 print(a) ---- 3
326 end
327 print(a) ---- 2
328 end
329 SomeFun()
330 print(a) ---- 1
331 -- end example --
332 \end{code}
333 \end{minipage}
334
335 Same as \verb!expl3!, the names of local variables \emph{must} start with \verb!l!,
336 while names of global variables \emph{must} start with \verb!g!.
337 The difference is that \verb!functional! package provides only one function for setting
338 both local and global variables of the same type,
339 by checking leading letters of their names. So for integer variables, you can write
340 \verb!\intSet\lTmpaInt{1}! and \verb!\intSet\gTmpbInt{2}!.
341
342 The previous example will produce different result
343 if we change variable from \verb!\lTmpaInt! to \verb!\gTmpaInt!.
344
345 \begin{minipage}{0.58\textwidth}
346 \begin{codehigh}
347 \Functional{scoping=true}
348 \IgnoreSpacesOn
349 \intSet \gTmpaInt {1}
350 \intVarLog \gTmpaInt % ---- 1
351 \prgNewFunction \someFun { } {
352 \intSet \gTmpaInt {2}
353 \intVarLog \gTmpaInt % ---- 2
354 \intCompareTF {1} > {0} {
355 \intSet \gTmpaInt {3}
356 \intVarLog \gTmpaInt % ---- 3
357 }{ }
358 \intVarLog \gTmpaInt % ---- 3
359 }
360 \someFun
361 \intVarLog \gTmpaInt % ---- 3
362 \IgnoreSpacesOff
363 \end{codehigh}
364 \end{minipage}%
365 \begin{minipage}{0.42\textwidth}
366 \begin{code}
367 -- lua code --
368 -- begin example --
369 a = 1
370 print(a) ---- 1
371 function SomeFun()
372 a = 2
373 print(a) ---- 2
374 if 1 > 0 then
375 a = 3
376 print(a) ---- 3
377 end
378 print(a) ---- 3
379 end
380 SomeFun()
381 print(a) ---- 3
382 -- end example --
383 \end{code}
384 \end{minipage}
385
386 As you can see, the values of global variables will never be reset after a group.
387
388 \section{Tracing Evaluation of Functions}
389
390 Since every function in \verb!functional! package will pass its return value to
391 the package, it is quite easy to debug your code.
392 You can turn on the tracing by setting \verb!\Functional{tracing=true}!.
393 For example, the tracing log of the first example in this chapter will be the following:
394
395 % FIXME: spaces at the first line will be removed
396 %\begin{codehigh}[]
397 % [I] \mathSquare{5}
398 % [I] \intEval{5*5}
399 % [I] \expWhole{\int_eval:n {5*5}}
400 % [O] 25
401 % [I] \prgReturn{25}
402 % [O] 25
403 % [O] 25
404 % [I] \intSet\lTmpaInt {25}
405 % [O]
406 % [I] \expValue\lTmpaInt
407 % [O] 25
408 % [I] \prgReturn{25}
409 % [O] 25
410 % [O] 25
411 %\end{codehigh}
412 \begin{codehigh}[]
413 [I] \mathSquare{5}
414 [I] \intEval{5*5}
415 [I] \expWhole{\int_eval:n {5*5}}
416 [O] 25
417 [I] \prgReturn{25}
418 [O] 25
419 [O] 25
420 [I] \intSet{\lTmpaInt }{25}
421 [O]
422 [I] \expValue{\lTmpaInt }
423 [O] 25
424 [I] \prgReturn{25}
425 [O] 25
426 [O] 25
427 [I] \mathSquare{25}
428 [I] \intEval{25*25}
429 [I] \expWhole{\int_eval:n {25*25}}
430 [O] 625
431 [I] \prgReturn{625}
432 [O] 625
433 [O] 625
434 [I] \intSet{\lTmpaInt }{625}
435 [O]
436 [I] \expValue{\lTmpaInt }
437 [O] 625
438 [I] \prgReturn{625}
439 [O] 625
440 [O] 625
441 \end{codehigh}
442
443 \section{Definitions of Functions}
444
445 Within \verb!expl3!, there are eight commands for defining new functions,
446 which is good for power users.
447
448 \begin{minipage}{0.5\textwidth}
449 \begin{code}[language=latex/latex3]
450 \cs_new:Npn
451 \cs_new_nopar:Npn
452 \cs_new_protected:Npn
453 \cs_new_protected_nopar:Npn
454 \end{code}
455 \end{minipage}%
456 \begin{minipage}{0.5\textwidth}
457 \begin{code}[language=latex/latex3]
458 \cs_new:Nn
459 \cs_new_nopar:Nn
460 \cs_new_protected:Nn
461 \cs_new_protected_nopar:Nn
462 \end{code}
463 \end{minipage}
464
465 Within \verb!functional! package, there is only one command (\cs{prgNewFunction})
466 for defining new functions, which is good for regular users.
467 The created functions are always protected and accept \verb!\par! in their arguments.
468
469 Since \verb!functional! package gets the results of functions by evaluation
470 (including expansion and execution by \TeX), it is natural to protect all functions.
471
472 \section{Variants of Arguments}
473
474 Within \verb!expl3!, there are several expansion variants for arguments,
475 and many expansion functions for expanding them, which are necessary for power users.
476
477 \begin{minipage}{0.5\textwidth}
478 \begin{code}[language=latex/latex3]
479 \module_foo:c
480 \module_bar:e
481 \module_bar:x
482 \module_bar:f
483 \module_bar:o
484 \module_bar:V
485 \module_bar:v
486 \end{code}
487 \end{minipage}%
488 \begin{minipage}{0.5\textwidth}
489 \begin{code}[language=latex/latex3]
490 \exp_args:Nc
491 \exp_args:Ne
492 \exp_args:Nx
493 \exp_args:Nf
494 \exp_args:No
495 \exp_args:NV
496 \exp_args:Nv
497 \end{code}
498 \end{minipage}
499
500 Within \verb!functional! package, there are only three variants
501 (\verb!c!, \verb!e!, \verb!V!) are provided, and these variants are defined
502 as functions (\cs{expName}, \cs{expWhole}, \cs{expValue}, respectively),
503 which are easier to use for regular users.
504
505 \begin{demohigh}
506 \newcommand\test{uvw}
507 \expName{test}
508 \end{demohigh}
509
510 \begin{demohigh}
511 \newcommand\test{uvw}
512 \expWhole{111\test222}
513 \end{demohigh}
514
515 \begin{demohigh}
516 \intSet\lTmpaInt{123}
517 \expValue\lTmpaInt
518 \end{demohigh}
519
520 The most interesting feature is that you can compose these functions.
521 For example, you can easily get the \verb!v! variant of \verb!expl3! by
522 simply composing \cs{expName} and \cs{expValue} functions:
523
524 \begin{demohigh}
525 \intSet\lTmpaInt{123}
526 \expValue{\expName{lTmpaInt}}
527 \end{demohigh}
528
529 \chapter{Functional Progarmming (\texttt{Prg})}
530
531 \section{De๏ฌning Functions and Conditionals}
532
533 \begin{function}{\prgNewFunction}
534 \begin{syntax}
535 \cs{prgNewFunction} \meta{function} \Arg{argument specification} \Arg{code}
536 \end{syntax}
537 Creates protected \meta{function} for evaluating the \meta{code}.
538 Within the \meta{code}, the parameters (\verb|#1|, \verb|#2|,
539 \emph{etc.}) will be replaced by those absorbed by the function.
540 The returned value \emph{must} be passed with \cs{prgReturn} function.
541 The definition is global and an error results if the
542 \meta{function} is already defined.\par
543 The \Arg{argument specification} in a list of letters,
544 where each letter is one of the following argument specifiers
545 (nearly all of them are \texttt{M} or \texttt{m} for functions provided by this package):\par
546 {\centering\begin{tabular}{ll}
547 %\hline
548 \texttt{M} & single-token argument, which will be manipulated first \\
549 \texttt{m} & multi-token argument, which will be manipulated first \\
550 \texttt{N} & single-token argument, which will not be manipulated first \\
551 \texttt{n} & multi-token argument, which will not be manipulated first \\
552 %\hline
553 \end{tabular}\par}
554 The argument manipulation for argument type \texttt{M} or \texttt{m}
555 is: if the argument starts with a function defined with \cs{prgNewFunction},
556 the argument will be evaluated and replaced with the returned value.
557 \end{function}
558
559 \begin{function}{\prgSetEqFunction}
560 \begin{syntax}
561 \cs{prgSetEqFunction} \meta{function_1} \meta{function_2}
562 \end{syntax}
563 Sets \meta{function_1} as an alias of \meta{function_2}.
564 \end{function}
565
566 \begin{function}{\prgNewConditional}
567 \begin{syntax}
568 \cs{prgNewConditional} \meta{function} \Arg{argument specification} \Arg{code}
569 \end{syntax}
570 Creates protected conditional \meta{function} for evaluating the \meta{code}.
571 The returned value of the \meta{function} \emph{must} be either |\cTrueBool|
572 or |\cFalseBool| and be passed with \cs{prgReturn} function..
573 The definition is global and an error results if the \meta{function} is already defined.
574 \par
575 Assume the \meta{function} is |\fooIfBar|, then another three functions
576 are also created at the same time: |\fooIfBarT|, |\fooIfBarF|, and |\fooIfBarTF|.
577 They have extra arguments which are \Arg{true code} or/and \Arg{false code}.
578 For example, if you write
579 \begin{codehigh}
580 \prgNewConditional \fooIfBar {Mm} {code with return value \cTrueBool or \cFalseBool}
581 \end{codehigh}
582 Then the following four functions are created:
583 \begin{itemize}
584 \item |\fooIfBar| \meta{arg_1} \Arg{arg_2}
585 \item |\fooIfBarT| \meta{arg_1} \Arg{arg_2} \Arg{true code}
586 \item |\fooIfBarF| \meta{arg_1} \Arg{arg_2} \Arg{false code}
587 \item |\fooIfBarTF| \meta{arg_1} \Arg{arg_2} \Arg{true code} \Arg{false code}
588 \end{itemize}
589 \end{function}
590
591 \section{Returning Values and Printing Tokens}
592
593 Just like \LuaTeX, \pkg{functional} package also provides \cs{prgReturn} and \cs{prgPrint} functions.
594
595 \begin{function}{\prgReturn}
596 \begin{syntax}
597 \cs{prgReturn} \Arg{tokens}
598 \end{syntax}
599 Returns \meta{tokens} as result of current function or conditional.
600 This function is normally used in the \meta{code} of \cs{prgNewFunction}
601 or \cs{prgNewConditional}, and it \emph{must} be the last function evaluated in the \meta{code}.
602 If it is missing, the return value of the last function evaluated in the \meta{code}
603 is returned. Therefore, the following two examples produce the same output:
604 \begin{codehigh}
605 \IgnoreSpacesOn
606 \prgNewFunction \mathSquare { m } {
607 \intSet \lTmpaInt {\intEval {#1 * #1}}
608 \prgReturn {\expValue \lTmpaInt}
609 }
610 \IgnoreSpacesOff
611 \mathSquare{5}
612 \end{codehigh}
613 \begin{codehigh}
614 \IgnoreSpacesOn
615 \prgNewFunction \mathSquare { m } {
616 \intSet \lTmpaInt {\intEval {#1 * #1}}
617 \expValue \lTmpaInt
618 }
619 \IgnoreSpacesOff
620 \mathSquare{5}
621 \end{codehigh}
622 \pkg{Functional} package takes care of return values,
623 and only print them to the input stream if the outer most functions are evaluated.
624 \end{function}
625
626 \begin{function}{\prgPrint}
627 \begin{syntax}
628 \cs{prgPrint} \Arg{tokens}
629 \end{syntax}
630 Prints \meta{tokens} directly to the input stream.
631 If there is no function defined with \cs{prgNewFunction} in \meta{tokens},
632 you can omit \cs{prgPrint} and write only \meta{tokens}.
633 But if there is any function defined with \cs{prgNewFunction} in \meta{tokens},
634 you \emph{have to} use \cs{prgPrint} function.
635 \end{function}
636
637 \section{Running Code with Anonymous Functions}
638
639 \begin{function}{\prgDo}
640 \begin{syntax}
641 \cs{prgDo} \Arg{code}
642 \end{syntax}
643 Treats \meta{code} as an anonymous function without arguments and evaluates it.
644 \end{function}
645
646 \begin{function}{\prgRunOneArgCode,\prgRunTwoArgCode,\prgRunThreeArgCode,\prgRunFourArgCode}
647 \begin{syntax}
648 \cs{prgRunOneArgCode} \Arg{arg_1} \Arg{code}
649 \cs{prgRunTwoArgCode} \Arg{arg_1} \Arg{arg_2} \Arg{code}
650 \cs{prgRunThreeArgCode} \Arg{arg_1} \Arg{arg_2} \Arg{arg_3} \Arg{code}
651 \cs{prgRunFourArgCode} \Arg{arg_1} \Arg{arg_2} \Arg{arg_3} \Arg{arg_4} \Arg{code}
652 \end{syntax}
653 Treats \meta{code} as an anonymous function with one to four arguments respectively,
654 and evaluates it. In evaluating the \meta{code}, \pkg{functional} package first evaluates
655 \meta{arg_1} to \meta{arg_4}, then replaces |#1| to |#4| in \meta{code}
656 with the return values respectively.
657 \end{function}
658
659 \chapter{Argument Using (\texttt{Use})}
660
661 \section{Evaluating Functions}
662
663 \begin{function}{\evalWhole}
664 \begin{syntax}
665 \cs{evalWhole} \Arg{tokens}
666 \end{syntax}
667 Evaluates all functions (defined with \cs{prgNewFunction}) in \meta{tokens}
668 and replaces them with their return values, then returns the resulting tokens.
669 \begin{codehigh}
670 \tlSet \lTmpaTl {a\intEval{2*3}b}
671 \tlSet \lTmpbTl {\evalWhole {a\intEval{2*3}b}}
672 \end{codehigh}
673 In the above example, |\lTmpaTl| contains |a\intEval{2*3}b|,
674 while |\lTmpbTl| contains |a6b|.
675 \end{function}
676
677 \begin{function}{\evalNone}
678 \begin{syntax}
679 \cs{evalNone} \Arg{tokens}
680 \end{syntax}
681 Prevents the evaluation of its argument, returning \meta{tokens} without touching them.
682 \begin{codehigh}
683 \tlSet \lTmpaTl {\intEval{2*3}}
684 \tlSet \lTmpbTl {\evalNone {\intEval{2*3}}}
685 \end{codehigh}
686 In the above example, |\lTmpaTl| contains |6|,
687 while |\lTmpbTl| contains |\intEval{2*3}|.
688 \end{function}
689
690 \section{Expanding Tokens}
691
692 \begin{function}{\expName}
693 \begin{syntax}
694 \cs{expName} \Arg{control sequence name}
695 \end{syntax}
696 Expands the \meta{control sequence name} until only characters
697 remain, then converts this into a control sequence and returns it.
698 The \meta{control sequence name} must consist of character tokens %,
699 %typically a mixture of category code $10$ (space), $11$ (letter) and $12$ (other).
700 when exhaustively expanded.%
701 %\begin{texnote}
702 %Protected macros that appear in a \texttt{c}-type argument are
703 %expanded despite being protected; \cs{exp_not:n} also has no
704 %effect. An internal error occurs if non-characters or active
705 %characters remain after full expansion, as the conversion to a
706 %control sequence is not possible.
707 %\end{texnote}
708 \end{function}
709
710 \begin{function}{\expValue}
711 \begin{syntax}
712 \cs{expValue} \meta{variable}
713 \end{syntax}
714 Recovers the content of a \meta{variable} and returns the value.
715 An error is raised if the variable does not exist or if it is invalid.
716 Note that it is the same as \cs{tlUse} for \meta{tl var}, or \cs{intUse} for \meta{int var}.
717 \end{function}
718
719 \begin{function}{\expWhole}
720 \begin{syntax}
721 \cs{expWhole} \Arg{tokens}
722 \end{syntax}
723 Expands the \meta{tokens} exhaustively and returns the result.
724 \end{function}
725
726 \begin{function}{\unExpand}
727 \begin{syntax}
728 \cs{unExpand} \Arg{tokens}
729 \end{syntax}
730 Prevents expansion of the \meta{tokens} inside the argument of \cs{expWhole} function.
731 The argument of \cs{unExpand} \emph{must} be surrounded by braces.
732 %\begin{texnote}
733 %This is the \eTeX{} \tn{unexpanded} primitive. In an
734 %|x|-expanding definition (\cs{cs_new:Npx}), \cs{exp_not:n}~|{#1}|
735 %is equivalent to |##1| rather than to~|#1|, namely it inserts the
736 %two characters |#| and~|1|. In an |e|-type argument
737 %\cs{exp_not:n}~|{#}| is equivalent to |#|, namely it inserts the
738 %character~|#|.
739 %\end{texnote}
740 \end{function}
741
742 \begin{function}{\onlyName}
743 \begin{syntax}
744 \cs{onlyName} \Arg{tokens}
745 \end{syntax}
746 Expands the \meta{tokens} until only characters remain, and then
747 converts this into a control sequence.
748 Further expansion of this control sequence is then inhibited
749 inside the argument of \cs{expWhole} function.
750 %\begin{texnote}
751 %Protected macros that appear in a \texttt{c}-type argument are
752 %expanded despite being protected; \cs{exp_not:n} also has no
753 %effect. An internal error occurs if non-characters or active
754 %characters remain after full expansion, as the conversion to a
755 %control sequence is not possible.
756 %\end{texnote}
757 \end{function}
758
759 \begin{function}{\onlyValue}
760 \begin{syntax}
761 \cs{onlyValue} \meta{variable}
762 \end{syntax}
763 Recovers the content of the \meta{variable}, then prevents expansion
764 of this material inside the argument of \cs{expWhole} function.
765 \end{function}
766
767 \section{Using Tokens}
768
769 \begin{function}{\useOne,\gobbleOne}
770 \begin{syntax}
771 \cs{useOne} \Arg{argument}
772 \cs{gobbleOne} \Arg{argument}
773 \end{syntax}
774 The function \cs{useOne} absorbs one argument and returns it.
775 %\begin{texnote}
776 %The \cs{UseOne} function is equivalent to \LaTeXe{}'s \tn{@firstofone}.
777 %\end{texnote}
778 \cs{gobbleOne} absorbs one argument and returns nothing.
779 %\begin{texnote}
780 %These are equivalent to \LaTeXe{}'s \tn{@gobble}, \tn{@gobbbletwo},
781 %\emph{etc.}
782 %\end{texnote}
783 For example
784 \begin{demohigh}
785 \useOne{abc}\gobbleOne{ijk}\useOne{xyz}
786 \end{demohigh}
787 \end{function}
788
789 \begin{function}{\useGobble,\gobbleUse}
790 \begin{syntax}
791 \cs{useGobble} \Arg{arg_1} \Arg{arg_2}
792 \cs{gobbleUse} \Arg{arg_1} \Arg{arg_2}
793 \end{syntax}
794 These functions absorb two arguments.
795 The function \cs{useGobble} discards the second argument,
796 and returns the content of the first argument.
797 \cs{gobbleUse} discards the first argument,
798 and returns the content of the second argument.
799 %\begin{texnote}
800 %These are equivalent to \LaTeXe{}'s \tn{@firstoftwo} and
801 %\tn{@secondoftwo}.
802 %\end{texnote}
803 For example
804 \begin{demohigh}
805 \useGobble{abc}{uvw}\gobbleUse{abc}{uvw}
806 \end{demohigh}
807 \end{function}
808
809 \chapter{Control Structures (\texttt{Bool})}
810
811 \section{Constant and Scratch Booleans}
812
813 \begin{variable}{\cTrueBool,\cFalseBool}
814 Constants that represent \verb|true| and \verb|false|, respectively. Used to
815 implement predicates. For example
816 \begin{demohigh}
817 \boolVarIfTF \cTrueBool {\prgReturn{True!}} {\prgReturn{False!}}
818 \boolVarIfTF \cFalseBool {\prgReturn{True!}} {\prgReturn{False!}}
819 \end{demohigh}
820 \end{variable}
821
822 \begin{variable}{\lTmpaBool,\lTmpbBool,\lTmpcBool,\lTmpiBool,\lTmpjBool,\lTmpkBool}
823 Scratch booleans for local assignment. These are never used by
824 the \verb!functional! package, and so are safe for use with any
825 function. However, they may be overwritten by other
826 code and so should only be used for short-term storage.
827 \end{variable}
828
829 \begin{variable}{\gTmpaBool,\gTmpbBool,\gTmpcBool,\gTmpiBool,\gTmpjBool,\gTmpkBool}
830 Scratch booleans for global assignment. These are never used by
831 the \verb!functional! package, and so are safe for use with any
832 function. However, they may be overwritten by other
833 code and so should only be used for short-term storage.
834 \end{variable}
835
836 \section{Boolean Expressions}
837
838 As we have a boolean datatype and predicate functions returning
839 boolean \meta{true} or \meta{false} values, it seems only fitting
840 that we also provide a parser for \meta{boolean expressions}.
841
842 A boolean expression is an expression which given input in the form
843 of predicate functions and boolean variables, return boolean
844 \meta{true} or \meta{false}. It supports the logical operations And,
845 Or and Not as the well-known infix operators \verb|&&| and \verb"||" and
846 prefix~\verb|!| with their usual precedences (namely, \verb|&&| binds
847 more tightly than \verb"||"). In addition to this, parentheses can be
848 used to isolate sub-expressions. For example,
849 \begin{codehigh}
850 \intCompare {1} = {1} &&
851 (
852 \intCompare {2} = {3} ||
853 \intCompare {4} < {4} ||
854 \strIfEq {abc} {def}
855 ) &&
856 ! \intCompare {2} = {4}
857 \end{codehigh}
858 is a valid boolean expression.
859
860 Contrarily to some other programming languages, the operators \verb|&&| and
861 \verb"||" evaluate both operands in all cases, even when the first
862 operand is enough to determine the result.
863
864 \section{Creating and Setting Booleans}
865
866 \begin{function}{\boolNew}
867 \begin{syntax}
868 \cs{boolNew} \meta{boolean}
869 \end{syntax}
870 Creates a new \meta{boolean} or raises an error if the
871 name is already taken. The declaration is global. The
872 \meta{boolean} is initially \texttt{false}.
873 \end{function}
874
875 \begin{function}{\boolConst}
876 \begin{syntax}
877 \cs{boolConst} \meta{boolean} \Arg{boolexpr}
878 \end{syntax}
879 Creates a new constant \meta{boolean} or raises an error if the name
880 is already taken. The value of the \meta{boolean} is set globally to
881 the result of evaluating the \meta{boolexpr}.
882 For example
883 \begin{codehigh}
884 \boolConst \cFooSomeBool {\intCompare{3}>{2}}
885 \boolVarLog \cFooSomeBool
886 \end{codehigh}
887 \end{function}
888
889 \begin{function}{\boolSet}
890 \begin{syntax}
891 \cs{boolSet} \meta{boolean} \Arg{boolexpr}
892 \end{syntax}
893 Evaluates the \meta{boolean expression} and sets the \meta{boolean} variable to
894 the logical truth of this evaluation.
895 For example
896 \begin{codehigh}
897 \boolSet \lTmpaBool {\intCompare{3}<{4}}
898 \boolVarLog \lTmpaBool
899 \end{codehigh}
900 \begin{codehigh}
901 \boolSet \lTmpaBool {\intCompare{3}<{4} && \strIfEq{abc}{uvw}}
902 \boolVarLog \lTmpaBool
903 \end{codehigh}
904 \end{function}
905
906 \begin{function}{\boolSetTrue}
907 \begin{syntax}
908 \cs{boolSetTrue} \meta{boolean}
909 \end{syntax}
910 Sets \meta{boolean} logically \texttt{true}.
911 \end{function}
912
913 \begin{function}{\boolSetFalse}
914 \begin{syntax}
915 \cs{boolSetFalse} \meta{boolean}
916 \end{syntax}
917 Sets \meta{boolean} logically \texttt{false}.
918 \end{function}
919
920 \begin{function}{\boolSetEq}
921 \begin{syntax}
922 \cs{boolSetEq} \meta{boolean_1} \meta{boolean_2}
923 \end{syntax}
924 Sets \meta{boolean_1} to the current value of \meta{boolean_2}.
925 For example
926 \begin{codehigh}
927 \boolSetTrue \lTmpaBool
928 \boolSetEq \lTmpbBool \lTmpaBool
929 \boolVarLog \lTmpbBool
930 \end{codehigh}
931 \end{function}
932
933 \section{Viewing Booleans}
934
935 \begin{function}{\boolLog}
936 \begin{syntax}
937 \cs{boolLog} \Arg{boolean expression}
938 \end{syntax}
939 Writes the logical truth of the \meta{boolean expression} in the log file.
940 \end{function}
941
942 \begin{function}{\boolVarLog}
943 \begin{syntax}
944 \cs{boolVarLog} \meta{boolean}
945 \end{syntax}
946 Writes the logical truth of the \meta{boolean} in the log file.
947 \end{function}
948
949 \begin{function}{\boolShow}
950 \begin{syntax}
951 \cs{boolShow} \Arg{boolean expression}
952 \end{syntax}
953 Displays the logical truth of the \meta{boolean expression} on the terminal.
954 \end{function}
955
956 \begin{function}{\boolVarShow}
957 \begin{syntax}
958 \cs{boolVarShow} \meta{boolean}
959 \end{syntax}
960 Displays the logical truth of the \meta{boolean} on the terminal.
961 \end{function}
962
963 \section{Booleans and Conditionals}
964
965 \begin{function}{\boolIfExist,\boolIfExistT,\boolIfExistF,\boolIfExistTF}
966 \begin{syntax}
967 \cs{boolIfExist} \meta{boolean}
968 \cs{boolIfExistT} \meta{boolean} \Arg{true code}
969 \cs{boolIfExistF} \meta{boolean} \Arg{false code}
970 \cs{boolIfExistTF} \meta{boolean} \Arg{true code} \Arg{false code}
971 \end{syntax}
972 Tests whether the \meta{boolean} is currently defined. This does not
973 check that the \meta{boolean} really is a boolean variable.
974 For example
975 \begin{demohigh}
976 \boolIfExistTF \lTmpaBool {\prgReturn{Yes}} {\prgReturn{No}}
977 \boolIfExistTF \lFooUndefinedBool {\prgReturn{Yes}} {\prgReturn{No}}
978 \end{demohigh}
979 \end{function}
980
981 \begin{function}{\boolVarIf,\boolVarIfT,\boolVarIfF,\boolVarIfTF}
982 \begin{syntax}
983 \cs{boolVarIf} \meta{boolean}
984 \cs{boolVarIfT} \meta{boolean} \Arg{true code}
985 \cs{boolVarIfF} \meta{boolean} \Arg{false code}
986 \cs{boolVarIfTF} \meta{boolean} \Arg{true code} \Arg{false code}
987 \end{syntax}
988 Tests the current truth of \meta{boolean}, and continues evaluation
989 based on this result. For example
990 \begin{demohigh}
991 \boolSetTrue \lTmpaBool
992 \boolVarIfTF \lTmpaBool {\prgReturn{True!}} {\prgReturn{False!}}
993 \boolSetFalse \lTmpaBool
994 \boolVarIfTF \lTmpaBool {\prgReturn{True!}} {\prgReturn{False!}}
995 \end{demohigh}
996 \end{function}
997
998 \begin{function}{\boolVarNot,\boolVarNotT,\boolVarNotF,\boolVarNotTF}
999 \begin{syntax}
1000 \cs{boolVarNot} \meta{boolean}
1001 \cs{boolVarNotT} \meta{boolean} \Arg{true code}
1002 \cs{boolVarNotF} \meta{boolean} \Arg{false code}
1003 \cs{boolVarNotTF} \meta{boolean} \Arg{true code} \Arg{false code}
1004 \end{syntax}
1005 Evaluates \meta{true code} if \meta{boolean} is \verb!false!,
1006 and \meta{false code} if \meta{boolean} is \verb!true!.
1007 For example
1008 \begin{demohigh}
1009 \boolVarNotTF {\intCompare{3}>{2}} {\prgReturn{Yes}} {\prgReturn{No}}
1010 \end{demohigh}
1011 \end{function}
1012
1013 \begin{function}{\boolVarAnd,\boolVarAndT,\boolVarAndF,\boolVarAndTF}
1014 \begin{syntax}
1015 \cs{boolVarAnd} \meta{boolean_1} \meta{boolean_2}
1016 \cs{boolVarAndT} \meta{boolean_1} \meta{boolean_2} \Arg{true code}
1017 \cs{boolVarAndF} \meta{boolean_1} \meta{boolean_2} \Arg{false code}
1018 \cs{boolVarAndTF} \meta{boolean_1} \meta{boolean_2} \Arg{true code} \Arg{false code}
1019 \end{syntax}
1020 Implements the \enquote{And} operation between two booleans,
1021 hence is \texttt{true} if both are \texttt{true}.
1022 %Contrarily to the infix operator \verb|&&|,
1023 %The \meta{boolean_2} is only evaluated if it is needed to determine the result of
1024 %\cs{boolVarAnd}.
1025 For example
1026 \begin{demohigh}
1027 \boolVarAndTF {\intCompare{3}>{2}} {\intIfOdd{6}} {\prgReturn{Yes}} {\prgReturn{No}}
1028 \end{demohigh}
1029 \end{function}
1030
1031 \begin{function}{\boolVarOr,\boolVarOrT,\boolVarOrF,\boolVarOrTF}
1032 \begin{syntax}
1033 \cs{boolVarOr} \meta{boolean_1} \meta{boolean_2}
1034 \cs{boolVarOrT} \meta{boolean_1} \meta{boolean_2} \Arg{true code}
1035 \cs{boolVarOrF} \meta{boolean_1} \meta{boolean_2} \Arg{false code}
1036 \cs{boolVarOrTF} \meta{boolean_1} \meta{boolean_2} \Arg{true code} \Arg{false code}
1037 \end{syntax}
1038 Implements the \enquote{Or} operation between two booleans,
1039 hence is \texttt{true} if either one is \texttt{true}.
1040 %Contrarily to the infix operator \verb"||",
1041 %The \meta{boolean_2} is only evaluated if it is needed to determine the result of
1042 %\cs{boolVarOr}.
1043 For example
1044 \begin{demohigh}
1045 \boolVarOrTF {\intCompare{3}>{2}} {\intIfOdd{6}} {\prgReturn{Yes}} {\prgReturn{No}}
1046 \end{demohigh}
1047 \end{function}
1048
1049 \begin{function}{\boolVarXor,\boolVarXorT,\boolVarXorF,\boolVarXorTF}
1050 \begin{syntax}
1051 \cs{boolVarXor} \meta{boolean_1} \meta{boolean_2}
1052 \cs{boolVarXorT} \meta{boolean_1} \meta{boolean_2} \Arg{true code}
1053 \cs{boolVarXorF} \meta{boolean_1} \meta{boolean_2} \Arg{false code}
1054 \cs{boolVarXorTF} \meta{boolean_1} \meta{boolean_2} \Arg{true code} \Arg{false code}
1055 \end{syntax}
1056 Implements an \enquote{exclusive or} operation between two booleans.
1057 For example
1058 \begin{demohigh}
1059 \boolVarXorTF {\intCompare{3}>{2}} {\intIfOdd{6}} {\prgReturn{Yes}} {\prgReturn{No}}
1060 \end{demohigh}
1061 \end{function}
1062
1063 \section{Booleans and Logical Loops}
1064
1065 Loops using either boolean expressions or stored boolean values.
1066
1067 \begin{function}{\boolVarDoUntil}
1068 \begin{syntax}
1069 \cs{boolVarDoUntil} \meta{boolean} \Arg{code}
1070 \end{syntax}
1071 Places the \meta{code} in the input stream for \TeX{} to process,
1072 and then checks the logical value of the \meta{boolean}. If it is
1073 \texttt{false} then the \meta{code} is inserted into the input
1074 stream again and the process loops until the \meta{boolean} is
1075 \texttt{true}.
1076 \begin{demohigh}
1077 \IgnoreSpacesOn
1078 \boolSetFalse \lTmpaBool
1079 \intZero \lTmpaInt
1080 \clistClear \lTmpaClist
1081 \boolVarDoUntil \lTmpaBool {
1082 \intIncr \lTmpaInt
1083 \clistPutRight \lTmpaClist {\expValue\lTmpaInt}
1084 \intCompareT {\lTmpaInt} = {10} {\boolSetTrue \lTmpaBool}
1085 }
1086 \clistVarJoin \lTmpaClist {:}
1087 \IgnoreSpacesOff
1088 \end{demohigh}
1089 \end{function}
1090
1091 \begin{function}{\boolVarDoWhile}
1092 \begin{syntax}
1093 \cs{boolVarDoWhile} \meta{boolean} \Arg{code}
1094 \end{syntax}
1095 Places the \meta{code} in the input stream for \TeX{} to process,
1096 and then checks the logical value of the \meta{boolean}. If it is
1097 \texttt{true} then the \meta{code} is inserted into the input
1098 stream again and the process loops until the \meta{boolean} is
1099 \texttt{false}.
1100 \begin{demohigh}
1101 \IgnoreSpacesOn
1102 \boolSetTrue \lTmpaBool
1103 \intZero \lTmpaInt
1104 \clistClear \lTmpaClist
1105 \boolVarDoWhile \lTmpaBool {
1106 \intIncr \lTmpaInt
1107 \clistPutRight \lTmpaClist {\expValue\lTmpaInt}
1108 \intCompareT {\lTmpaInt} = {10} {\boolSetFalse \lTmpaBool}
1109 }
1110 \clistVarJoin \lTmpaClist {:}
1111 \IgnoreSpacesOff
1112 \end{demohigh}
1113 \end{function}
1114
1115 \begin{function}{\boolVarUntilDo}
1116 \begin{syntax}
1117 \cs{boolVarUntilDo} \meta{boolean} \Arg{code}
1118 \end{syntax}
1119 This function firsts checks the logical value of the \meta{boolean}.
1120 If it is \texttt{false} the \meta{code} is placed in the input stream
1121 and expanded. After the completion of the \meta{code} the truth
1122 of the \meta{boolean} is re-evaluated. The process then loops
1123 until the \meta{boolean} is \texttt{true}.
1124 \begin{demohigh}
1125 \IgnoreSpacesOn
1126 \boolSetFalse \lTmpaBool
1127 \intZero \lTmpaInt
1128 \clistClear \lTmpaClist
1129 \boolVarUntilDo \lTmpaBool {
1130 \intIncr \lTmpaInt
1131 \clistPutRight \lTmpaClist {\expValue\lTmpaInt}
1132 \intCompareT {\lTmpaInt} = {10} {\boolSetTrue \lTmpaBool}
1133 }
1134 \clistVarJoin \lTmpaClist {:}
1135 \IgnoreSpacesOff
1136 \end{demohigh}
1137 \end{function}
1138
1139 \begin{function}{\boolVarWhileDo}
1140 \begin{syntax}
1141 \cs{boolVarWhileDo} \meta{boolean} \Arg{code}
1142 \end{syntax}
1143 This function firsts checks the logical value of the \meta{boolean}.
1144 If it is \texttt{true} the \meta{code} is placed in the input stream
1145 and expanded. After the completion of the \meta{code} the truth
1146 of the \meta{boolean} is re-evaluated. The process then loops
1147 until the \meta{boolean} is \texttt{false}.
1148 \begin{demohigh}
1149 \IgnoreSpacesOn
1150 \boolSetTrue \lTmpaBool
1151 \intZero \lTmpaInt
1152 \clistClear \lTmpaClist
1153 \boolVarWhileDo \lTmpaBool {
1154 \intIncr \lTmpaInt
1155 \clistPutRight \lTmpaClist {\expValue\lTmpaInt}
1156 \intCompareT {\lTmpaInt} = {10} {\boolSetFalse \lTmpaBool}
1157 }
1158 \clistVarJoin \lTmpaClist {:}
1159 \IgnoreSpacesOff
1160 \end{demohigh}
1161 \end{function}
1162
1163 \chapter{Token Lists (\texttt{Tl})}
1164
1165 \TeX{} works with tokens, and \LaTeX3 therefore provides a number of
1166 functions to deal with lists of tokens. Token lists may be present
1167 directly in the argument to a function:
1168 \begin{codehigh}
1169 \tlFoo {a collection of \tokens}
1170 \end{codehigh}
1171 or may be stored in a so-called \enquote{token list variable}, which
1172 have the suffix \texttt{Tl}: a token list variable can also be used as
1173 the argument to a function, for example
1174 \begin{codehigh}
1175 \tlVarFoo \lSomeTl
1176 \end{codehigh}
1177 In both cases, functions are available to test and manipulate the lists
1178 of tokens, and these have the module prefix \texttt{Tl}.
1179 In many cases, functions which can be applied to token list variables
1180 are paired with similar functions for application to explicit lists
1181 of tokens: the two \enquote{views} of a token list are therefore collected
1182 together here.
1183
1184 A token list (explicit, or stored in a variable) can be seen either
1185 as a list of \enquote{items},
1186 or a list of \enquote{tokens}. An item is whatever \cs{useOne} would
1187 grab as its argument: a single non-space token or a brace group,
1188 with optional leading explicit space characters (each item is thus
1189 itself a token list). A token is either a normal \texttt{N} argument,
1190 or \verb*| |, \verb|{|, or \verb|}| (assuming normal \TeX{} category codes).
1191 Thus for example
1192 \begin{codehigh}
1193 {Hello} world
1194 \end{codehigh}
1195 contains 6 items (\texttt{Hello}, \texttt{w}, \texttt{o}, \texttt{r},
1196 \texttt{l} and \texttt{d}), but 13 tokens (\verb|{|, \texttt{H}, \texttt{e},
1197 \texttt{l}, \texttt{l}, \texttt{o}, \verb|}|, \verb*| |, \texttt{w}, \texttt{o},
1198 \texttt{r}, \texttt{l} and \texttt{d}).
1199 Functions which act on items are often faster than their analogue acting
1200 directly on tokens.
1201
1202 \section{Constant and Scratch Token Lists}
1203
1204 \begin{variable}{\cSpaceTl}
1205 An explicit space character contained in a token list%
1206 %(compare this with \cs{c_space_token})
1207 . For use where an explicit space is required.
1208 \end{variable}
1209
1210 \begin{variable}{\cEmptyTl}
1211 Constant that is always empty.
1212 \end{variable}
1213
1214 %\begin{variable}{\cNoValueTl}
1215 %A marker for the absence of an argument. This constant \verb|tl| can safely
1216 %be typeset (\emph{cf.}~\cs{qNil}), with the result being \verb|-NoValue-|.
1217 %It is important to note that \cs{cNoValueTl} is constructed such that it
1218 %will \emph{not} match the simple text input \verb|-NoValue-|, \emph{i.e.}
1219 %that
1220 %\begin{demohigh}
1221 %\tlIfEqTF {\tlUse\cNoValueTl} {-NoValue-} {Result{Yes}} {\prgReturn{No}}
1222 %\end{demohigh}
1223 %The \cs{cNoValueTl} marker is intended for
1224 %use in creating document-level interfaces, where it serves as an indicator
1225 %that an (optional) argument was omitted. In particular, it is distinct
1226 %from a simple empty \verb|tl|.
1227 %\end{variable}
1228
1229 \begin{variable}{\lTmpaTl,\lTmpbTl,\lTmpcTl,\lTmpiTl,\lTmpjTl,\lTmpkTl}
1230 Scratch token lists for local assignment. These are never used by
1231 the \verb!functional! package, and so are safe for use with any
1232 function. However, they may be overwritten by other
1233 code and so should only be used for short-term storage.
1234 \end{variable}
1235
1236 \begin{variable}{\gTmpaTl,\gTmpbTl,\gTmpcTl,\gTmpiTl,\gTmpjTl,\gTmpkTl}
1237 Scratch token lists for global assignment. These are never used by
1238 the \verb!functional! package, and so are safe for use with any
1239 function. However, they may be overwritten by other
1240 code and so should only be used for short-term storage.
1241 \end{variable}
1242
1243 \section{Creating and Using Token Lists}
1244
1245 \begin{function}{\tlNew}
1246 \begin{syntax}
1247 \cs{tlNew} \meta{tl var}
1248 \end{syntax}
1249 Creates a new \meta{tl var} or raises an error if the
1250 name is already taken. The declaration is global. The
1251 \meta{tl~var} is initially empty.
1252 \begin{codehigh}
1253 \tlNew \lFooSomeTl
1254 \end{codehigh}
1255 \end{function}
1256
1257 \begin{function}{\tlConst}
1258 \begin{syntax}
1259 \cs{tlConst} \meta{tl var} \Arg{token list}
1260 \end{syntax}
1261 Creates a new constant \meta{tl var} or raises an error
1262 if the name is already taken. The value of the
1263 \meta{tl var} is set globally to the \meta{token list}.
1264 \begin{codehigh}
1265 \tlConst \cFooSomeTl {abc}
1266 \end{codehigh}
1267 \end{function}
1268
1269 \begin{function}{\tlUse}
1270 \begin{syntax}
1271 \cs{tlUse} \meta{tl~var}
1272 \end{syntax}
1273 Recovers the content of a \meta{tl~var} and returns the value.
1274 An error is raised if the variable
1275 does not exist or if it is invalid. Note that it is possible to use
1276 a \meta{tl~var} directly without an accessor function.
1277 \begin{codehigh}
1278 \tlUse \lTmpbTl
1279 \end{codehigh}
1280 \end{function}
1281
1282 \begin{function}{\tlToStr}
1283 \begin{syntax}
1284 \cs{tlToStr} \Arg{token list}
1285 \end{syntax}
1286 Converts the \meta{token list} to a \meta{string}, returning the
1287 resulting character tokens. A \meta{string}
1288 is a series of tokens with category code $12$ (other) with the exception
1289 of spaces, which retain category code $10$ (space).
1290 \begin{demohigh}
1291 \tlToStr {12\abc34}
1292 \end{demohigh}
1293 %This function requires only a single expansion.
1294 %Its argument \emph{must} be braced.
1295 %\begin{texnote}
1296 %This is the \eTeX{} primitive \tn{detokenize}.
1297 %Converting a \meta{token list} to a \meta{string} yields a
1298 %concatenation of the string representations of every token in the
1299 %\meta{token list}.
1300 %The string representation of a control sequence is
1301 %\begin{itemize}
1302 %\item an escape character, whose character code is given by the
1303 %internal parameter \tn{escapechar}, absent if the
1304 %\tn{escapechar} is negative or greater than the largest
1305 %character code;
1306 %\item the control sequence name, as defined by \cs{cs_to_str:N};
1307 %\item a space, unless the control sequence name is a single
1308 %character whose category at the time of expansion of
1309 %\cs{tl_to_str:n} is not \enquote{letter}.
1310 %\end{itemize}
1311 %The string representation of an explicit character token is that
1312 %character, doubled in the case of (explicit) macro parameter
1313 %characters (normally \verb|#|).
1314 %In particular, the string representation of a token list may
1315 %depend on the category codes in effect when it is evaluated, and
1316 %the value of the \tn{escapechar}: for instance |\tl_to_str:n {\a}|
1317 %normally produces the three character \enquote{backslash},
1318 %\enquote{lower-case a}, \enquote{space}, but it may also produce a
1319 %single \enquote{lower-case a} if the escape character is negative
1320 %and \texttt{a} is currently not a letter.
1321 %\end{texnote}
1322 \end{function}
1323
1324 \begin{function}{\tlVarToStr}
1325 \begin{syntax}
1326 \cs{tlVarToStr} \meta{tl var}
1327 \end{syntax}
1328 Converts the content of the \meta{tl var} to a string, returning the
1329 resulting character tokens. A \meta{string}
1330 is a series of tokens with category code $12$ (other) with the exception
1331 of spaces, which retain category code $10$ (space).
1332 \begin{demohigh}
1333 \tlSet \lTmpaTl {12\abc34}
1334 \tlVarToStr \lTmpaTl
1335 \end{demohigh}
1336 \end{function}
1337
1338 \section{Viewing Token Lists}
1339
1340 \begin{function}{\tlLog}
1341 \begin{syntax}
1342 \cs{tlLog} \Arg{token list}
1343 \end{syntax}
1344 Writes the \meta{token list} in the log file. See also
1345 \cs{tlShow} which displays the result in the terminal.
1346 \begin{codehigh}
1347 \tlLog {123\abc456}
1348 \end{codehigh}
1349 \end{function}
1350
1351 \begin{function}{\tlVarLog}
1352 \begin{syntax}
1353 \cs{tlVarLog} \meta{tl var}
1354 \end{syntax}
1355 Writes the content of the \meta{tl var} in the log file. See also
1356 \cs{tlVarShow} which displays the result in the terminal.
1357 \begin{codehigh}
1358 \tlSet \lTmpaTl {123\abc456}
1359 \tlVarLog \lTmpaTl
1360 \end{codehigh}
1361 \end{function}
1362
1363 \begin{function}{\tlShow}
1364 \begin{syntax}
1365 \cs{tlShow} \Arg{token list}
1366 \end{syntax}
1367 Displays the \meta{token list} on the terminal.
1368 \begin{codehigh}
1369 \tlShow {123\abc456}
1370 \end{codehigh}
1371 %\begin{texnote}
1372 %This is similar to the \eTeX{} primitive \tn{showtokens}, wrapped
1373 %to a fixed number of characters per line.
1374 %\end{texnote}
1375 \end{function}
1376
1377 \begin{function}{\tlVarShow}
1378 \begin{syntax}
1379 \cs{tlVarShow} \meta{tl var}
1380 \end{syntax}
1381 Displays the content of the \meta{tl var} on the terminal.
1382 \begin{codehigh}
1383 \tlSet \lTmpaTl {123\abc456}
1384 \tlVarShow \lTmpaTl
1385 \end{codehigh}
1386 %\begin{texnote}
1387 %This is similar to the \TeX{} primitive \tn{show}, wrapped to a
1388 %fixed number of characters per line.
1389 %\end{texnote}
1390 \end{function}
1391
1392 \section{Setting Token List Variables}
1393
1394 \begin{function}{\tlSet}
1395 \begin{syntax}
1396 \cs{tlSet} \meta{tl~var} \Arg{tokens}
1397 \end{syntax}
1398 Sets \meta{tl~var} to contain \meta{tokens},
1399 removing any previous content from the variable.
1400 \begin{demohigh}
1401 \tlSet \lTmpiTl {\intMathMult{4}{5}}
1402 \tlUse \lTmpiTl
1403 \end{demohigh}
1404 \end{function}
1405
1406 \begin{function}{\tlSetEq}
1407 \begin{syntax}
1408 \cs{tlSetEq} \meta{tl var_1} \meta{tl var_2}
1409 \end{syntax}
1410 Sets the content of \meta{tl var_1} equal to that of \meta{tl var_2}.
1411 \begin{demohigh}
1412 \tlSet \lTmpaTl {abc}
1413 \tlSetEq \lTmpbTl \lTmpaTl
1414 \tlUse \lTmpbTl
1415 \end{demohigh}
1416 \end{function}
1417
1418 \begin{function}{\tlClear}
1419 \begin{syntax}
1420 \cs{tlClear} \meta{tl~var}
1421 \end{syntax}
1422 Clears all entries from the \meta{tl~var}.
1423 \begin{demohigh}
1424 \tlSet \lTmpjTl {One}
1425 \tlClear \lTmpjTl
1426 \tlSet \lTmpjTl {Two}
1427 \tlUse \lTmpjTl
1428 \end{demohigh}
1429 \end{function}
1430
1431 \begin{function}{\tlClearNew}
1432 \begin{syntax}
1433 \cs{tlClearNew} \meta{tl var}
1434 \end{syntax}
1435 Ensures that the \meta{tl var} exists globally by applying
1436 \cs{tlNew} if necessary, then applies \cs{tlClear} to leave
1437 the \meta{tl var} empty.
1438 \begin{codehigh}
1439 \tlClearNew \lFooSomeTl
1440 \end{codehigh}
1441 \end{function}
1442
1443 \begin{function}{\tlConcat}
1444 \begin{syntax}
1445 \cs{tlConcat} \meta{tl var_1} \meta{tl var_2} \meta{tl var_3}
1446 \end{syntax}
1447 Concatenates the content of \meta{tl var_2} and \meta{tl var_3}
1448 together and saves the result in \meta{tl var_1}. The \meta{tl var_2}
1449 is placed at the left side of the new token list.
1450 \begin{demohigh}
1451 \tlSet \lTmpbTl {con}
1452 \tlSet \lTmpcTl {cat}
1453 \tlConcat \lTmpaTl \lTmpbTl \lTmpcTl
1454 \tlUse \lTmpaTl
1455 \end{demohigh}
1456 \end{function}
1457
1458 \begin{function}{\tlPutLeft}
1459 \begin{syntax}
1460 \cs{tlPutLeft} \meta{tl~var} \Arg{tokens}
1461 \end{syntax}
1462 Appends \meta{tokens} to the left side of the current content of \meta{tl~var}.
1463 \begin{demohigh}
1464 \tlSet \lTmpkTl {Functional}
1465 \tlPutLeft \lTmpkTl {Hello}
1466 \tlUse \lTmpkTl
1467 \end{demohigh}
1468 \end{function}
1469
1470 \begin{function}{\tlPutRight}
1471 \begin{syntax}
1472 \cs{tlPutRight} \meta{tl~var} \Arg{tokens}
1473 \end{syntax}
1474 Appends \meta{tokens} to the right side of the current content of \meta{tl~var}.
1475 \begin{demohigh}
1476 \tlSet \lTmpkTl {Functional}
1477 \tlPutRight \lTmpkTl {World}
1478 \tlUse \lTmpkTl
1479 \end{demohigh}
1480 \end{function}
1481
1482 \section{Replacing Tokens}
1483
1484 Within token lists, replacement takes place at the top level: there is
1485 no recursion into brace groups (more precisely, within a group defined by
1486 a categroy code $1$/$2$ pair).
1487
1488 \begin{function}{\tlVarReplaceOnce}
1489 \begin{syntax}
1490 \cs{tlVarReplaceOnce} \meta{tl var} \Arg{old tokens} \Arg{new tokens}
1491 \end{syntax}
1492 Replaces the first (leftmost) occurrence of \meta{old tokens} in the
1493 \meta{tl var} with \meta{new tokens}. \meta{Old tokens}
1494 cannot contain \verb|{|, \verb|}| or \verb|#|
1495 (more precisely, explicit character tokens with category code $1$
1496 (begin-group) or $2$ (end-group), and tokens with category code $6$).
1497 \begin{demohigh}
1498 \tlSet \lTmpaTl {1{bc}2bc3}
1499 \tlVarReplaceOnce \lTmpaTl {bc} {xx}
1500 \tlUse \lTmpaTl
1501 \end{demohigh}
1502 \end{function}
1503
1504 \begin{function}{\tlVarReplaceAll}
1505 \begin{syntax}
1506 \cs{tlVarReplaceAll} \meta{tl var} \Arg{old tokens} \Arg{new tokens}
1507 \end{syntax}
1508 Replaces all occurrences of \meta{old tokens} in the
1509 \meta{tl var} with \meta{new tokens}. \meta{Old tokens}
1510 cannot contain \verb|{|, \verb|}| or \verb|#|
1511 (more precisely, explicit character tokens with category code $1$
1512 (begin-group) or $2$ (end-group), and tokens with category code $6$).
1513 As this function
1514 operates from left to right, the pattern \meta{old tokens}
1515 may remain after the replacement (see \cs{tlVarRemoveAll} for an example).
1516 \begin{demohigh}
1517 \tlSet \lTmpaTl {1{bc}2bc3}
1518 \tlVarReplaceAll \lTmpaTl {bc} {xx}
1519 \tlUse \lTmpaTl
1520 \end{demohigh}
1521 \end{function}
1522
1523 \begin{function}{\tlVarRemoveOnce}
1524 \begin{syntax}
1525 \cs{tlVarRemoveOnce} \meta{tl var} \Arg{tokens}
1526 \end{syntax}
1527 Removes the first (leftmost) occurrence of \meta{tokens} from the
1528 \meta{tl var}. \meta{Tokens} cannot contain \verb|{|, \verb|}| or \verb|#|
1529 (more precisely, explicit character tokens with category code $1$
1530 (begin-group) or $2$ (end-group), and tokens with category code $6$).
1531 \begin{demohigh}
1532 \tlSet \lTmpaTl {1{bc}2bc3}
1533 \tlVarRemoveOnce \lTmpaTl {bc}
1534 \tlUse \lTmpaTl
1535 \end{demohigh}
1536 \end{function}
1537
1538 \begin{function}{\tlVarRemoveAll}
1539 \begin{syntax}
1540 \cs{tlVarRemoveAll} \meta{tl var} \Arg{tokens}
1541 \end{syntax}
1542 Removes all occurrences of \meta{tokens} from the
1543 \meta{tl var}. \meta{Tokens} cannot contain \verb|{|, \verb|}| or \verb|#|
1544 (more precisely, explicit character tokens with category code $1$
1545 (begin-group) or $2$ (end-group), and tokens with category code $6$).
1546 As this function
1547 operates from left to right, the pattern \meta{tokens}
1548 may remain after the removal, for instance,
1549 \begin{demohigh}
1550 \tlSet \lTmpaTl {abbccd}
1551 \tlVarRemoveAll \lTmpaTl {bc}
1552 \tlUse \lTmpaTl
1553 \end{demohigh}
1554 \end{function}
1555
1556 \begin{function}{\tlTrimSpaces}
1557 \begin{syntax}
1558 \cs{tlTrimSpaces} \Arg{token list}
1559 \end{syntax}
1560 Removes any leading and trailing explicit space characters
1561 (explicit tokens with character code $32$ and category code $10$)
1562 from the \meta{token list} and returns the result.
1563 \begin{demohigh}
1564 Foo\tlTrimSpaces { 12 34 }Bar
1565 \end{demohigh}
1566 %\begin{texnote}
1567 %The result is returned within \tn{unexpanded}, which means that the token
1568 %list does not expand further when appearing in an \texttt{x}-type
1569 %or \texttt{e}-type argument expansion.
1570 %\end{texnote}
1571 \end{function}
1572
1573 \begin{function}{\tlVarTrimSpaces}
1574 \begin{syntax}
1575 \cs{tlVarTrimSpaces} \meta{tl var}
1576 \end{syntax}
1577 Sets the \meta{tl var} to contain the result of removing any leading
1578 and trailing explicit space characters (explicit tokens with
1579 character code $32$ and category code $10$) from its contents.
1580 \begin{demohigh}
1581 \tlSet \lTmpaTl { 12 34 }
1582 \tlVarTrimSpaces \lTmpaTl
1583 Foo\tlUse \lTmpaTl Bar
1584 \end{demohigh}
1585 \end{function}
1586
1587 \section{Working with the Content of Token Lists}
1588
1589 \begin{function}{\tlCount}
1590 \begin{syntax}
1591 \cs{tlCount} \Arg{tokens}
1592 \end{syntax}
1593 Counts the number of \meta{items} in \meta{tokens} and returns this information.
1594 Unbraced tokens count as one element as do each token group (\verb|{|$\cdots$\verb|}|).
1595 This process ignores any unprotected spaces within \meta{tokens}. %See also \cs{tlVarCount}.
1596 %This function requires three expansions, giving an \meta{integer denotation}.
1597 \begin{demohigh}
1598 \tlCount {12\abc34}
1599 \end{demohigh}
1600 \end{function}
1601
1602 \begin{function}{\tlVarCount}
1603 \begin{syntax}
1604 \cs{tlVarCount} \meta{tl var}
1605 \end{syntax}
1606 Counts the number of \meta{items} in the \meta{tl var} and returns this information.
1607 Unbraced tokens count as one element as do each token group (\verb|{|$\cdots$\verb|}|).
1608 This process ignores any unprotected spaces within the \meta{tl var}. %See also \cs{tlCount}.
1609 %This function requires three expansions, giving an \meta{integer denotation}.
1610 \begin{demohigh}
1611 \tlSet \lTmpaTl {12\abc34}
1612 \tlVarCount \lTmpaTl
1613 \end{demohigh}
1614 \end{function}
1615
1616 \begin{function}{\tlHead}
1617 \begin{syntax}
1618 \cs{tlHead} \Arg{token list}
1619 \end{syntax}
1620 Returns the first \meta{item} in the \meta{token list},
1621 discarding the rest of the \meta{token list}.
1622 All leading explicit space characters
1623 (explicit tokens with character code $32$ and category code $10$)
1624 are discarded; for example
1625 \begin{demohigh}
1626 \fbox {1\tlHead{ abc }2}
1627 \fbox {1\tlHead{ abc }2}
1628 \end{demohigh}
1629 If the \enquote{head} is a brace group, rather than a single token,
1630 the braces are removed, and so
1631 \begin{codehigh}
1632 \tlHead { { ab} c }
1633 \end{codehigh}
1634 yields \verb*| ab|.
1635 A blank \meta{token list} (see \cs{tlIfBlank}) results in
1636 \cs{tlHead} returning nothing.
1637 %\begin{texnote}
1638 %The result is returned within \cs{exp_not:n}, which means that the token
1639 %list does not expand further when appearing in an \texttt{x}-type
1640 %or \texttt{e}-type argument expansion.
1641 %\end{texnote}
1642 \end{function}
1643
1644 \begin{function}{\tlVarHead}
1645 \begin{syntax}
1646 \cs{tlVarHead} \meta{tl var}
1647 \end{syntax}
1648 Returns the first \meta{item} in the \meta{tl var},
1649 discarding the rest of the \meta{tl var}.
1650 All leading explicit space characters (explicit tokens with character code $32$
1651 and category code $10$) are discarded.
1652 \begin{demohigh}
1653 \tlSet \lTmpaTl {HELLO}
1654 \tlVarHead \lTmpaTl
1655 \end{demohigh}
1656 \end{function}
1657
1658 \begin{function}{\tlTail}
1659 \begin{syntax}
1660 \cs{tlTail} \Arg{token list}
1661 \end{syntax}
1662 Discards all leading explicit space characters
1663 (explicit tokens with character code $32$ and category code $10$)
1664 and the first \meta{item} in the \meta{token list}, and returns the
1665 remaining tokens. Thus for example
1666 \begin{codehigh}
1667 \tlTail { a {bc} d }
1668 \end{codehigh}
1669 and
1670 \begin{codehigh}
1671 \tlTail { a {bc} d }
1672 \end{codehigh}
1673 both return \verb*| {bc} d |. A blank \meta{token list} (see \cs{tlIfBlank})
1674 results in \cs{tlTail} returning nothing.
1675 %\begin{texnote}
1676 %The result is returned within \cs{exp_not:n}, which means that the
1677 %token list does not expand further when appearing in an \texttt{x}-type
1678 %or \texttt{e}-type argument expansion.
1679 %\end{texnote}
1680 \end{function}
1681
1682 \begin{function}{\tlVarTail}
1683 \begin{syntax}
1684 \cs{tlVarTail} \meta{tl var}
1685 \end{syntax}
1686 Discards all leading explicit space characters
1687 (explicit tokens with character code $32$ and category code $10$)
1688 and the first \meta{item} in the \meta{tl var}, and returns the
1689 remaining tokens.
1690 \begin{demohigh}
1691 \tlSet \lTmpaTl {HELLO}
1692 \tlVarTail \lTmpaTl
1693 \end{demohigh}
1694 \end{function}
1695
1696 \begin{function}{\tlItem,\tlVarItem}
1697 \begin{syntax}
1698 \cs{tlItem} \Arg{token list} \Arg{integer expression}
1699 \cs{tlVarItem} \meta{tl var} \Arg{integer expression}
1700 \end{syntax}
1701 Indexing items in the \meta{token list} from $1$ on the left, this
1702 function evaluates the \meta{integer expression} and returns the
1703 appropriate item from the \meta{token list}.
1704 If the \meta{integer expression} is negative, indexing occurs from
1705 the right of the token list, starting at $-1$ for the right-most item.
1706 If the index is out of bounds, then the function returns nothing.
1707 \begin{demohigh}
1708 \tlItem {abcd} {3}
1709 \end{demohigh}
1710 %\begin{texnote}
1711 %The result is returned within the \tn{unexpanded}
1712 %primitive (\cs{exp_not:n}), which means that the \meta{item}
1713 %does not expand further when appearing in an \texttt{x}-type
1714 %or \texttt{e}-type argument expansion.
1715 %\end{texnote}
1716 \end{function}
1717
1718 \begin{function}{\tlRandItem,\tlVarRandItem}
1719 \begin{syntax}
1720 \cs{tlRandItem} \Arg{token list}
1721 \cs{tlVarRandItem} \meta{tl var}
1722 \end{syntax}
1723 Selects and returns a pseudo-random item of the \meta{token list}.
1724 If the \meta{token list} is blank, the result is empty.
1725 %This is not available in older versions of \XeTeX{}.
1726 \begin{demohigh}
1727 \tlRandItem {abcdef}
1728 \tlRandItem {abcdef}
1729 \end{demohigh}
1730 %\begin{texnote}
1731 %The result is returned within the \tn{unexpanded}
1732 %primitive (\cs{exp_not:n}), which means that the \meta{item}
1733 %does not expand further when appearing in an \texttt{x}-type
1734 %or \texttt{e}-type argument expansion.
1735 %\end{texnote}
1736 \end{function}
1737
1738 \section{Mapping over Token Lists}
1739
1740 %All mappings are done at the current group level, \emph{i.e.} any
1741 %local assignments made by the \meta{function} or \meta{code} discussed
1742 %below remain in effect after the loop.
1743
1744 %\begin{function}{\tlMapFunction}
1745 %\begin{syntax}
1746 %\cs{tlMapFunction} \Arg{token list} \meta{function}
1747 %\end{syntax}
1748 %Applies \meta{function} to every \meta{item} in the \meta{token list},
1749 %The \meta{function} receives one argument for each iteration.
1750 %This may be a number of tokens if the \meta{item} was stored within
1751 %braces. Hence the \meta{function} should anticipate receiving
1752 %\texttt{n}-type arguments.
1753 %\end{function}
1754 %
1755 %\begin{function}{\tlVarMapFunction}
1756 %\begin{syntax}
1757 %\cs{tlVarMapFunction} \meta{tl var} \meta{function}
1758 %\end{syntax}
1759 %Applies \meta{function} to every \meta{item} in the \meta{tl var}.
1760 %The \meta{function} receives one argument for each iteration.
1761 %This may be a number of tokens if the \meta{item} was stored within
1762 %braces. Hence the \meta{function} should anticipate receiving
1763 %\texttt{n}-type arguments.
1764 %\end{function}
1765
1766 \begin{function}{\tlMapInline}
1767 \begin{syntax}
1768 \cs{tlMapInline} \Arg{token list} \Arg{inline function}
1769 \end{syntax}
1770 Applies the \meta{inline function} to every \meta{item} stored within the
1771 \meta{token list}. The \meta{inline function} should consist of code which
1772 receives the \meta{item} as \verb|#1|.
1773 \begin{demohigh}
1774 \IgnoreSpacesOn
1775 \tlClear \lTmpaTl
1776 \tlMapInline {one} {
1777 \tlPutRight \lTmpaTl {[#1]}
1778 }
1779 \tlUse \lTmpaTl
1780 \IgnoreSpacesOff
1781 \end{demohigh}
1782 \end{function}
1783
1784 \begin{function}{\tlVarMapInline}
1785 \begin{syntax}
1786 \cs{tlVarMapInline} \meta{tl var} \Arg{inline function}
1787 \end{syntax}
1788 Applies the \meta{inline function} to every \meta{item} stored within the
1789 \meta{tl var}. The \meta{inline function} should consist of code which
1790 receives the \meta{item} as \verb|#1|.
1791 \begin{demohigh}
1792 \IgnoreSpacesOn
1793 \tlClear \lTmpaTl
1794 \tlSet \lTmpkTl {one}
1795 \tlVarMapInline \lTmpkTl {
1796 \tlPutRight \lTmpaTl {[#1]}
1797 }
1798 \tlUse \lTmpaTl
1799 \IgnoreSpacesOff
1800 \end{demohigh}
1801 \end{function}
1802
1803 %\begin{function}{\tlMapTokens,\tlVarMapTokens}
1804 %\begin{syntax}
1805 %\cs{tlMapTokens} \Arg{tokens} \Arg{code}
1806 %\cs{tlVarMapTokens} \meta{tl var} \Arg{code}
1807 %\end{syntax}
1808 %Analogue of \cs{tl_map_function:NN} which maps several tokens
1809 %instead of a single function. The \meta{code} receives each \meta{item} in
1810 %the \meta{tl var} or in \meta{tokens} as a trailing brace group. For
1811 %instance,
1812 %\begin{verbatim}
1813 %\tl_map_tokens:Nn \l_my_tl { \prg_replicate:nn { 2 } }
1814 %\end{verbatim}
1815 %expands to twice each \meta{item} in the \meta{tl var}: for each \meta{item} in
1816 %\cs{l_my_tl} the function \cs{prg_replicate:nn} receives |2| and
1817 %\meta{item} as its two arguments. The function
1818 %\cs{tl_map_inline:Nn} is typically faster but is not expandable.
1819 %\end{function}
1820
1821 \begin{function}{\tlMapVariable}
1822 \begin{syntax}
1823 \cs{tlMapVariable} \Arg{token list} \meta{variable} \Arg{code}
1824 \end{syntax}
1825 Stores each \meta{item} of the \meta{token list} in turn in the
1826 (token list) \meta{variable} and applies the \meta{code}. The
1827 \meta{code} will usually make use of the \meta{variable}, but this
1828 is not enforced. The assignments to the \meta{variable} are local.
1829 Its value after the loop is the last \meta{item} in the
1830 \meta{tl var}, or its original value if the \meta{tl var} is blank.
1831 \begin{demohigh}
1832 \IgnoreSpacesOn
1833 \tlClear \lTmpaTl
1834 \tlMapVariable {one} \lTmpiTl {
1835 \tlPutRight \lTmpaTl {\expWhole {[\lTmpiTl]}}
1836 }
1837 \prgReturn{\tlUse\lTmpaTl}
1838 \IgnoreSpacesOff
1839 \end{demohigh}
1840 \end{function}
1841
1842 \begin{function}{\tlVarMapVariable}
1843 \begin{syntax}
1844 \cs{tlVarMapVariable} \meta{tl var} \meta{variable} \Arg{code}
1845 \end{syntax}
1846 Stores each \meta{item} of the \meta{tl var} in turn in the (token
1847 list) \meta{variable} and applies the \meta{code}. The \meta{code}
1848 will usually make use of the \meta{variable}, but this is not
1849 enforced. The assignments to the \meta{variable} are local. Its
1850 value after the loop is the last \meta{item} in the \meta{tl var},
1851 or its original value if the \meta{tl var} is blank.
1852 \begin{demohigh}
1853 \IgnoreSpacesOn
1854 \tlClear \lTmpaTl
1855 \tlSet \lTmpkTl {one}
1856 \tlVarMapVariable \lTmpkTl \lTmpiTl {
1857 \tlPutRight \lTmpaTl {\expWhole {[\lTmpiTl]}}
1858 }
1859 \prgReturn{\tlUse\lTmpaTl}
1860 \IgnoreSpacesOff
1861 \end{demohigh}
1862 \end{function}
1863
1864 %\begin{function}{\tlMapBreak}
1865 %\begin{syntax}
1866 %\cs{tlMapBreak}
1867 %\end{syntax}
1868 %Used to terminate a tl map function before all
1869 %entries in the \meta{token list variable} have been processed. This
1870 %normally takes place within a conditional statement, for example
1871 %\begin{verbatim}
1872 %\tl_map_inline:Nn \l_my_tl
1873 %{
1874 %\str_if_eq:nnT { #1 } { bingo } { \tl_map_break: }
1875 %Do something useful
1876 %}
1877 %\end{verbatim}
1878 %See also \cs{tl_map_break:n}.
1879 %Use outside of a tl map scenario leads to low
1880 %level \TeX{} errors.
1881 %\begin{texnote}
1882 %When the mapping is broken, additional tokens may be inserted
1883 %before the \meta{tokens} are
1884 %inserted into the input stream.
1885 %This depends on the design of the mapping function.
1886 %\end{texnote}
1887 %\end{function}
1888 %
1889 %\begin{function}{\tlMapBreakDo}
1890 %\begin{syntax}
1891 %\cs{tlMapBreakDo} \Arg{code}
1892 %\end{syntax}
1893 %Used to terminate a tl map function before all
1894 %entries in the \meta{token list variable} have been processed, inserting
1895 %the \meta{code} after the mapping has ended. This
1896 %normally takes place within a conditional statement, for example
1897 %\begin{verbatim}
1898 %\tl_map_inline:Nn \l_my_tl
1899 %{
1900 %\str_if_eq:nnT { #1 } { bingo }
1901 %{ \tl_map_break:n { <code> } }
1902 %Do something useful
1903 %}
1904 %\end{verbatim}
1905 %Use outside of a tl map scenario leads to low
1906 %level \TeX{} errors.
1907 %\begin{texnote}
1908 %When the mapping is broken, additional tokens may be inserted
1909 %before the \meta{code} is
1910 %inserted into the input stream.
1911 %This depends on the design of the mapping function.
1912 %\end{texnote}
1913 %\end{function}
1914
1915 \section{Token List Conditionals}
1916
1917 \begin{function}{\tlIfExist,\tlIfExistT,\tlIfExistF,\tlIfExistTF}
1918 \begin{syntax}
1919 \cs{tlIfExist} \meta{tl var}
1920 \cs{tlIfExistT} \meta{tl var} \Arg{true code}
1921 \cs{tlIfExistF} \meta{tl var} \Arg{false code}
1922 \cs{tlIfExistTF} \meta{tl var} \Arg{true code} \Arg{false code}
1923 \end{syntax}
1924 Tests whether the \meta{tl var} is currently defined. This does not
1925 check that the \meta{tl var} really is a token list variable.
1926 \begin{demohigh}
1927 \tlIfExistTF \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
1928 \tlIfExistTF \lFooUndefinedTl {\prgReturn{Yes}} {\prgReturn{No}}
1929 \end{demohigh}
1930 \end{function}
1931
1932 \begin{function}{\tlIfEmpty,\tlIfEmptyT,\tlIfEmptyF,\tlIfEmptyTF}
1933 \begin{syntax}
1934 \cs{tlIfEmpty} \Arg{token list}
1935 \cs{tlIfEmptyT} \Arg{token list} \Arg{true code}
1936 \cs{tlIfEmptyF} \Arg{token list} \Arg{false code}
1937 \cs{tlIfEmptyTF} \Arg{token list} \Arg{true code} \Arg{false code}
1938 \end{syntax}
1939 Tests if the \meta{token list} is entirely empty
1940 (\emph{i.e.}~contains no tokens at all). For example
1941 \begin{demohigh}
1942 \tlIfEmptyTF {abc} {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
1943 \tlIfEmptyTF {} {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
1944 \end{demohigh}
1945 \end{function}
1946
1947 \begin{function}{\tlVarIfEmpty,\tlVarIfEmptyT,\tlVarIfEmptyF,\tlVarIfEmptyTF}
1948 \begin{syntax}
1949 \cs{tlVarIfEmpty} \meta{tl~var}
1950 \cs{tlVarIfEmptyT} \meta{tl~var} \Arg{true code}
1951 \cs{tlVarIfEmptyF} \meta{tl~var} \Arg{false code}
1952 \cs{tlVarIfEmptyTF} \meta{tl~var} \Arg{true code} \Arg{false code}
1953 \end{syntax}
1954 Tests if the \meta{token list variable} is entirely empty
1955 (\emph{i.e.}~contains no tokens at all). For example
1956 \begin{demohigh}
1957 \tlSet \lTmpaTl {abc}
1958 \tlVarIfEmptyTF \lTmpaTl {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
1959 \tlClear \lTmpaTl
1960 \tlVarIfEmptyTF \lTmpaTl {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
1961 \end{demohigh}
1962 \end{function}
1963
1964 \begin{function}{\tlIfBlank,\tlIfBlankT,\tlIfBlankF,\tlIfBlankTF}
1965 \begin{syntax}
1966 \cs{tlIfBlank} \Arg{token list}
1967 \cs{tlIfBlankT} \Arg{token list} \Arg{true code}
1968 \cs{tlIfBlankF} \Arg{token list} \Arg{false code}
1969 \cs{tlIfBlankTF} \Arg{token list} \Arg{true code} \Arg{false code}
1970 \end{syntax}
1971 Tests if the \meta{token list} consists only of blank spaces
1972 (\emph{i.e.} contains no item). The test is \texttt{true} if
1973 \meta{token list} is zero or more explicit space characters
1974 (explicit tokens with character code $32$ and category code $10$),
1975 and is \texttt{false} otherwise.
1976 \begin{demohigh}
1977 \tlIfEmptyTF { } {\prgReturn{Yes}} {\prgReturn{No}}
1978 \tlIfBlankTF { } {\prgReturn{Yes}} {\prgReturn{No}}
1979 \end{demohigh}
1980 \end{function}
1981
1982 \begin{function}{\tlIfEq,\tlIfEqT,\tlIfEqF,\tlIfEqTF}
1983 \begin{syntax}
1984 \cs{tlIfEq} \Arg{token list_1} \Arg{token list_2}
1985 \cs{tlIfEqT} \Arg{token list_1} \Arg{token list_2} \Arg{true code}
1986 \cs{tlIfEqF} \Arg{token list_1} \Arg{token list_2} \Arg{false code}
1987 \cs{tlIfEqTF} \Arg{token list_1} \Arg{token list_2} \Arg{true code} \Arg{false code}
1988 \end{syntax}
1989 Tests if \meta{token list_1} and \meta{token list_2} contain the
1990 same list of tokens, both in respect of character codes and category
1991 codes. See \cs{strIfEq} if category codes are not important.
1992 For example
1993 \begin{demohigh}
1994 \tlIfEqTF {abc} {abc} {\prgReturn{Yes}} {\prgReturn{No}}
1995 \tlIfEqTF {abc} {xyz} {\prgReturn{Yes}} {\prgReturn{No}}
1996 \end{demohigh}
1997 %\begin{demohigh}
1998 %\tlSet\lTmpaTl{abc}
1999 %\tlSet\lTmpbTl{abc}
2000 %\tlSet\lTmpcTl{xyz}
2001 %\tlIfEqTF{\tlUse\lTmpaTl}{\tlUse\lTmpbTl}{\prgReturn{Yes}}{\prgReturn{No}}
2002 %\tlIfEqTF{\tlUse\lTmpaTl}{\tlUse\lTmpcTl}{\prgReturn{Yes}}{\prgReturn{No}}
2003 %\end{demohigh}
2004 \end{function}
2005
2006 \begin{function}{\tlVarIfEq,\tlVarIfEqT,\tlVarIfEqF,\tlVarIfEqTF}
2007 \begin{syntax}
2008 \cs{tlVarIfEq} \meta{tl var_1} \meta{tl var_2}
2009 \cs{tlVarIfEqT} \meta{tl var_1} \meta{tl var_2} \Arg{true code}
2010 \cs{tlVarIfEqF} \meta{tl var_1} \meta{tl var_2} \Arg{false code}
2011 \cs{tlVarIfEqTF} \meta{tl var_1} \meta{tl var_2} \Arg{true code} \Arg{false code}
2012 \end{syntax}
2013 Compares the content of two \meta{token list variables} and
2014 is logically \texttt{true} if the two contain the same list of
2015 tokens (\emph{i.e.}~identical in both the list of characters they
2016 contain and the category codes of those characters). For example
2017 \begin{demohigh}
2018 \tlSet \lTmpaTl {abc}
2019 \tlSet \lTmpbTl {abc}
2020 \tlSet \lTmpcTl {xyz}
2021 \tlVarIfEqTF \lTmpaTl \lTmpbTl {\prgReturn{Yes}} {\prgReturn{No}}
2022 \tlVarIfEqTF \lTmpaTl \lTmpcTl {\prgReturn{Yes}} {\prgReturn{No}}
2023 \end{demohigh}
2024 See also \cs{strVarIfEq} for a comparison that ignores category codes.
2025 \end{function}
2026
2027 \begin{function}{\tlIfIn,\tlIfInT,\tlIfInF,\tlIfInTF}
2028 \begin{syntax}
2029 \cs{tlIfIn} \Arg{token list_1} \Arg{token list_2}
2030 \cs{tlIfInT} \Arg{token list_1} \Arg{token list_2} \Arg{true code}
2031 \cs{tlIfInF} \Arg{token list_1} \Arg{token list_2} \Arg{false code}
2032 \cs{tlIfInTF} \Arg{token list_1} \Arg{token list_2} \Arg{true code} \Arg{false code}
2033 \end{syntax}
2034 Tests if \meta{token list_2} is found inside \meta{token list_1}.
2035 The \meta{token list_2} cannot contain the tokens \verb|{|, \verb|}| or \verb|#|
2036 (more precisely, explicit character tokens with category code $1$
2037 (begin-group) or $2$ (end-group), and tokens with category code $6$).
2038 The search does \emph{not} enter brace (category code $1$/$2$) groups.
2039 \begin{demohigh}
2040 \tlIfInTF {hello world} {o} {\prgReturn{Yes}} {\prgReturn{No}}
2041 \tlIfInTF {hello world} {a} {\prgReturn{Yes}} {\prgReturn{No}}
2042 \end{demohigh}
2043 \end{function}
2044
2045 \begin{function}{\tlVarIfIn,\tlVarIfInT,\tlVarIfInF,\tlVarIfInTF}
2046 \begin{syntax}
2047 \cs{tlVarIfIn} \meta{tl var} \Arg{token list}
2048 \cs{tlVarIfInT} \meta{tl var} \Arg{token list} \Arg{true code}
2049 \cs{tlVarIfInF} \meta{tl var} \Arg{token list} \Arg{false code}
2050 \cs{tlVarIfInTF} \meta{tl var} \Arg{token list} \Arg{true code} \Arg{false code}
2051 \end{syntax}
2052 Tests if the \meta{token list} is found in the content of the
2053 \meta{tl var}. The \meta{token list} cannot contain
2054 the tokens \verb|{|, \verb|}| or \verb|#|
2055 (more precisely, explicit character tokens with category code $1$
2056 (begin-group) or $2$ (end-group), and tokens with category code $6$).
2057 \begin{demohigh}
2058 \tlSet \lTmpaTl {hello world}
2059 \tlVarIfInTF \lTmpaTl {o} {\prgReturn{Yes}} {\prgReturn{No}}
2060 \tlVarIfInTF \lTmpaTl {a} {\prgReturn{Yes}} {\prgReturn{No}}
2061 \end{demohigh}
2062 \end{function}
2063
2064 \begin{function}{\tlIfSingle,\tlIfSingleT,\tlIfSingleF,\tlIfSingleTF}
2065 \begin{syntax}
2066 \cs{tlIfSingle} \Arg{token list}
2067 \cs{tlIfSingleT} \Arg{token list} \Arg{true code}
2068 \cs{tlIfSingleF} \Arg{token list} \Arg{false code}
2069 \cs{tlIfSingleTF} \Arg{token list} \Arg{true code} \Arg{false code}
2070 \end{syntax}
2071 Tests if the \meta{token list} has exactly one \meta{item}, \emph{i.e.} is
2072 a single normal token (neither an explicit space character nor a
2073 begin-group character) or a single brace group, surrounded by
2074 optional spaces on both sides. In other words, such a token list has
2075 token count $1$ according to \cs{tlCount}.
2076 \begin{demohigh}
2077 \tlIfSingleTF {a} {\prgReturn{Yes}} {\prgReturn{No}}
2078 \tlIfSingleTF {abc} {\prgReturn{Yes}} {\prgReturn{No}}
2079 \end{demohigh}
2080 \end{function}
2081
2082 \begin{function}{\tlVarIfSingle,\tlVarIfSingleT,\tlVarIfSingleF,\tlVarIfSingleTF}
2083 \begin{syntax}
2084 \cs{tlVarIfSingle} \meta{tl var}
2085 \cs{tlVarIfSingleT} \meta{tl var} \Arg{true code}
2086 \cs{tlVarIfSingleF} \meta{tl var} \Arg{false code}
2087 \cs{tlVarIfSingleTF} \meta{tl var} \Arg{true code} \Arg{false code}
2088 \end{syntax}
2089 Tests if the content of the \meta{tl var} consists of a single \meta{item},
2090 \emph{i.e.} is a single normal token (neither an explicit space
2091 character nor a begin-group character) or a single brace group,
2092 surrounded by optional spaces on both sides. In other words, such a
2093 token list has token count $1$ according to \cs{tlVarCount}.
2094 \begin{demohigh}
2095 \tlSet \lTmpaTl {a}
2096 \tlVarIfSingleTF \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
2097 \tlSet \lTmpaTl {abc}
2098 \tlVarIfSingleTF \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
2099 \end{demohigh}
2100 \end{function}
2101
2102 \section{Token List Case Functions}
2103
2104 \begin{function}{\tlVarCase}
2105 \begin{syntax}
2106 \cs{tlVarCase} \meta{test token list variable}
2107 ~ ~ \verb"{"
2108 ~ ~ ~ ~ \meta{token list variable case_1} \Arg{code case_1}
2109 ~ ~ ~ ~ \meta{token list variable case_2} \Arg{code case_2}
2110 ~ ~ ~ ~ $\cdots$
2111 ~ ~ ~ ~ \meta{token list variable case_n} \Arg{code case_n}
2112 ~ ~ \verb"}"
2113 \end{syntax}
2114 This function compares the \meta{test token list variable} in turn
2115 with each of the \meta{token list variable cases}. If the two
2116 are equal (as described for \cs{tlVarIfEq})
2117 then the associated \meta{code} is left in the input
2118 stream and other cases are discarded. The function
2119 does nothing if there is no match.
2120 \begin{demohigh}
2121 \IgnoreSpacesOn
2122 \tlSet \lTmpaTl {a}
2123 \tlSet \lTmpbTl {b}
2124 \tlSet \lTmpcTl {c}
2125 \tlSet \lTmpkTl {b}
2126 \tlVarCase \lTmpkTl {
2127 \lTmpaTl {\prgReturn {First}}
2128 \lTmpbTl {\prgReturn {Second}}
2129 \lTmpcTl {\prgReturn {Third}}
2130 }
2131 \IgnoreSpacesOff
2132 \end{demohigh}
2133 \end{function}
2134
2135 \begin{function}{\tlVarCaseT}
2136 \begin{syntax}
2137 \cs{tlVarCaseT} \meta{test token list variable}
2138 ~ ~ \verb"{"
2139 ~ ~ ~ ~ \meta{token list variable case_1} \Arg{code case_1}
2140 ~ ~ ~ ~ \meta{token list variable case_2} \Arg{code case_2}
2141 ~ ~ ~ ~ $\cdots$
2142 ~ ~ ~ ~ \meta{token list variable case_n} \Arg{code case_n}
2143 ~ ~ \verb"}"
2144 ~ ~ \Arg{true code}
2145 \end{syntax}
2146 This function compares the \meta{test token list variable} in turn
2147 with each of the \meta{token list variable cases}. If the two
2148 are equal (as described for \cs{tlVarIfEq})
2149 then the associated \meta{code} is left in the input
2150 stream and other cases are discarded. If any of the
2151 cases are matched, the \meta{true code} is also inserted into the
2152 input stream (after the code for the appropriate case).
2153 \begin{demohigh}
2154 \IgnoreSpacesOn
2155 \tlSet \lTmpaTl {a}
2156 \tlSet \lTmpbTl {b}
2157 \tlSet \lTmpcTl {c}
2158 \tlSet \lTmpkTl {b}
2159 \tlVarCaseT \lTmpkTl {
2160 \lTmpaTl {\intSet \lTmpkInt {1}}
2161 \lTmpbTl {\intSet \lTmpkInt {2}}
2162 \lTmpcTl {\intSet \lTmpkInt {3}}
2163 }{
2164 \prgReturn {\intUse \lTmpkInt}
2165 }
2166 \IgnoreSpacesOff
2167 \end{demohigh}
2168 \end{function}
2169
2170 \begin{function}{\tlVarCaseF}
2171 \begin{syntax}
2172 \cs{tlVarCaseF} \meta{test token list variable}
2173 ~ ~ \verb"{"
2174 ~ ~ ~ ~ \meta{token list variable case_1} \Arg{code case_1}
2175 ~ ~ ~ ~ \meta{token list variable case_2} \Arg{code case_2}
2176 ~ ~ ~ ~ $\cdots$
2177 ~ ~ ~ ~ \meta{token list variable case_n} \Arg{code case_n}
2178 ~ ~ \verb"}"
2179 ~ ~ \Arg{false code}
2180 \end{syntax}
2181 This function compares the \meta{test token list variable} in turn
2182 with each of the \meta{token list variable cases}. If the two
2183 are equal (as described for \cs{tlVarIfEq})
2184 then the associated \meta{code} is left in the input
2185 stream and other cases are discarded. If none match then the \meta{false code}
2186 is inserted into the input stream (after the code for the appropriate case).
2187 \begin{demohigh}
2188 \IgnoreSpacesOn
2189 \tlSet \lTmpaTl {a}
2190 \tlSet \lTmpbTl {b}
2191 \tlSet \lTmpcTl {c}
2192 \tlSet \lTmpkTl {b}
2193 \tlVarCaseF \lTmpkTl{
2194 \lTmpaTl {\prgReturn {First}}
2195 \lTmpbTl {\prgReturn {Second}}
2196 \lTmpcTl {\prgReturn {Third}}
2197 }{
2198 \prgReturn {No~Match!}
2199 }
2200 \IgnoreSpacesOff
2201 \end{demohigh}
2202 \end{function}
2203
2204 \begin{function}{\tlVarCaseTF}
2205 \begin{syntax}
2206 \cs{tlVarCaseTF} \meta{test token list variable}
2207 ~ ~ \verb"{"
2208 ~ ~ ~ ~ \meta{token list variable case_1} \Arg{code case_1}
2209 ~ ~ ~ ~ \meta{token list variable case_2} \Arg{code case_2}
2210 ~ ~ ~ ~ $\cdots$
2211 ~ ~ ~ ~ \meta{token list variable case_n} \Arg{code case_n}
2212 ~ ~ \verb"}"
2213 ~ ~ \Arg{true code}
2214 ~ ~ \Arg{false code}
2215 \end{syntax}
2216 This function compares the \meta{test token list variable} in turn
2217 with each of the \meta{token list variable cases}. If the two
2218 are equal (as described for \cs{tlVarIfEq})
2219 then the associated \meta{code} is left in the input
2220 stream and other cases are discarded. If any of the
2221 cases are matched, the \meta{true code} is also inserted into the
2222 input stream (after the code for the appropriate case), while if none
2223 match then the \meta{false code} is inserted. The function \cs{tlVarCase},
2224 which does nothing if there is no match, is also available.
2225 \begin{demohigh}
2226 \IgnoreSpacesOn
2227 \tlSet \lTmpaTl {a}
2228 \tlSet \lTmpbTl {b}
2229 \tlSet \lTmpcTl {c}
2230 \tlSet \lTmpkTl {b}
2231 \tlVarCaseTF \lTmpkTl {
2232 \lTmpaTl {\intSet \lTmpkInt {1}}
2233 \lTmpbTl {\intSet \lTmpkInt {2}}
2234 \lTmpcTl {\intSet \lTmpkInt {3}}
2235 }{
2236 \prgReturn {\intUse \lTmpkInt}
2237 }{
2238 \prgReturn {0}
2239 }
2240 \IgnoreSpacesOff
2241 \end{demohigh}
2242 \end{function}
2243
2244 \chapter{Strings (\texttt{Str})}
2245
2246 \TeX{} associates each character with a category code: as such, there is no
2247 concept of a \enquote{string} as commonly understood in many other
2248 programming languages. However, there are places where we wish to manipulate
2249 token lists while in some sense \enquote{ignoring} category codes: this is
2250 done by treating token lists as strings in a \TeX{} sense.
2251
2252 A \TeX{} string (and thus an \pkg{expl3} string) is a series of characters
2253 which have category code $12$ (\enquote{other}) with the exception of
2254 space characters which have category code $10$ (\enquote{space}). Thus
2255 at a technical level, a \TeX{} string is a token list with the appropriate
2256 category codes. In this documentation, these are simply referred to as
2257 strings.
2258
2259 String variables are simply specialised token lists, but by convention
2260 should be named with the suffix \texttt{Str}. Such variables
2261 should contain characters with category code $12$ (other), except
2262 spaces, which have category code $10$ (blank space). All the
2263 functions in this module which accept a token list argument first
2264 convert it to a string using \cs{tlToStr} for internal processing,
2265 and do not treat a token list or the corresponding string
2266 representation differently.
2267
2268 As a string is a subset of the more general token list, it is sometimes unclear
2269 when one should be used over the other.
2270 Use a string variable for data that isn't primarily intended for typesetting
2271 and for which a level of protection from unwanted expansion is suitable.
2272 This data type simplifies comparison of variables since there are no concerns
2273 about expansion of their contents.
2274
2275 %The functions \cs{CsToStr}, \cs{tlToStr}, \cs{tlVarToStr} and
2276 %\cs{TokenToStr} (and variants) generate strings from the appropriate
2277 %input: these are documented in modules \pkg{Cs}, \pkg{Tl} and \pkg{Token},
2278 %respectively.
2279
2280 \section{Constant and Scratch Strings}
2281
2282 \begin{variable}{\cAmpersandStr,\cAtsignStr,\cBackslashStr,\cLeftBraceStr,\cRightBraceStr,
2283 \cCircumflexStr,\cColonStr,\cDollarStr,\cHashStr,\cPercentStr,\cTildeStr,\cUnderscoreStr,\cZeroStr}
2284 Constant strings, containing a single character token, with category code $12$.
2285 \end{variable}
2286
2287 \begin{variable}{\lTmpaStr,\lTmpbStr,\lTmpcStr,\lTmpiStr,\lTmpjStr,\lTmpkStr}
2288 Scratch strings for local assignment. These are never used by
2289 the \verb!functional! package, and so are safe for use with any
2290 function. However, they may be overwritten by other
2291 code and so should only be used for short-term storage.
2292 \end{variable}
2293
2294 \begin{variable}{\gTmpaStr,\gTmpbStr,\gTmpcStr,\gTmpiStr,\gTmpjStr,\gTmpkStr}
2295 Scratch strings for global assignment. These are never used by
2296 the \verb!functional! package, and so are safe for use with any
2297 function. However, they may be overwritten by other
2298 code and so should only be used for short-term storage.
2299 \end{variable}
2300
2301 \section{Creating and Using Strings}
2302
2303 \begin{function}{\strNew}
2304 \begin{syntax}
2305 \cs{strNew} \meta{str var}
2306 \end{syntax}
2307 Creates a new \meta{str var} or raises an error if the name is
2308 already taken. The declaration is global. The \meta{str var} is
2309 initially empty.
2310 \begin{codehigh}
2311 \strNew \lFooSomeStr
2312 \end{codehigh}
2313 \end{function}
2314
2315 \begin{function}{\strConst}
2316 \begin{syntax}
2317 \cs{strConst} \meta{str var} \Arg{token list}
2318 \end{syntax}
2319 Creates a new constant \meta{str var} or raises an error if the name
2320 is already taken. The value of the \meta{str var} is set
2321 globally to the \meta{token list}, converted to a string.
2322 \begin{codehigh}
2323 \strConst \cFooSomeStr {12\abc34}
2324 \end{codehigh}
2325 \end{function}
2326
2327 \begin{function}{\strUse}
2328 \begin{syntax}
2329 \cs{strUse} \meta{str var}
2330 \end{syntax}
2331 Recovers the content of a \meta{str var} and returns the value.
2332 An error is raised if the variable
2333 does not exist or if it is invalid. Note that it is possible to use
2334 a \meta{str} directly without an accessor function.
2335 \begin{codehigh}
2336 \strUse \lTmpaStr
2337 \end{codehigh}
2338 \end{function}
2339
2340 \section{Viewing Strings}
2341
2342 \begin{function}{\strLog}
2343 \begin{syntax}
2344 \cs{strLog} \Arg{token list}
2345 \end{syntax}
2346 Writes \meta{token list} in the log file.
2347 \begin{codehigh}
2348 \strLog {1234\abcd5678}
2349 \end{codehigh}
2350 \end{function}
2351
2352 \begin{function}{\strVarLog}
2353 \begin{syntax}
2354 \cs{strVarLog} \meta{str var}
2355 \end{syntax}
2356 Writes the content of the \meta{str var} in the log file.
2357 \begin{codehigh}
2358 \strSet \lTmpiStr {1234\abcd5678}
2359 \strVarLog \lTmpiStr
2360 \end{codehigh}
2361 \end{function}
2362
2363 \begin{function}{\strShow}
2364 \begin{syntax}
2365 \cs{strShow} \Arg{token list}
2366 \end{syntax}
2367 Displays \meta{token list} on the terminal.
2368 \begin{codehigh}
2369 \strShow {1234\abcd5678}
2370 \end{codehigh}
2371 \end{function}
2372
2373 \begin{function}{\strVarShow}
2374 \begin{syntax}
2375 \cs{strVarShow} \meta{str var}
2376 \end{syntax}
2377 Displays the content of the \meta{str var} on the terminal.
2378 \begin{codehigh}
2379 \strSet \lTmpiStr {1234\abcd5678}
2380 \strVarShow \lTmpiStr
2381 \end{codehigh}
2382 \end{function}
2383
2384 \section{Setting String Variables}
2385
2386 \begin{function}{\strSet}
2387 \begin{syntax}
2388 \cs{strSet} \meta{str var} \Arg{token list}
2389 \end{syntax}
2390 Converts the \meta{token list} to a \meta{string}, and stores the
2391 result in \meta{str var}.
2392 \begin{demohigh}
2393 \strSet \lTmpiStr {\intMathMult{4}{5}}
2394 \strUse \lTmpiStr
2395 \end{demohigh}
2396 \end{function}
2397
2398 \begin{function}{\strSetEq}
2399 \begin{syntax}
2400 \cs{strSetEq} \meta{str var_1} \meta{str var_2}
2401 \end{syntax}
2402 Sets the content of \meta{str var_1} equal to that of \meta{str var_2}.
2403 \begin{demohigh}
2404 \strSet \lTmpaStr {abc}
2405 \strSetEq \lTmpbStr \lTmpaStr
2406 \strUse \lTmpbStr
2407 \end{demohigh}
2408 \end{function}
2409
2410 \begin{function}{\strClear}
2411 \begin{syntax}
2412 \cs{strClear} \meta{str var}
2413 \end{syntax}
2414 Clears the content of the \meta{str var}. For example
2415 \begin{demohigh}
2416 \strSet \lTmpjStr {One}
2417 \strClear \lTmpjStr
2418 \strSet \lTmpjStr {Two}
2419 \strUse \lTmpjStr
2420 \end{demohigh}
2421 \end{function}
2422
2423 \begin{function}{\strClearNew}
2424 \begin{syntax}
2425 \cs{strClearNew} \meta{str var}
2426 \end{syntax}
2427 Ensures that the \meta{str var} exists globally by applying
2428 \cs{strNew} if necessary, then applies \cs{strClear} to leave
2429 the \meta{str var} empty.
2430 \begin{codehigh}
2431 \strClearNew \lFooSomeStr
2432 \strUse \lFooSomeStr
2433 \end{codehigh}
2434 \end{function}
2435
2436 \begin{function}{\strConcat}
2437 \begin{syntax}
2438 \cs{strConcat} \meta{str var_1} \meta{str var_2} \meta{str var_3}
2439 \end{syntax}
2440 Concatenates the content of \meta{str var_2} and \meta{str var_3}
2441 together and saves the result in \meta{str var_1}. The \meta{str var_2}
2442 is placed at the left side of the new string variable.
2443 The \meta{str var_2} and \meta{str var_3} must indeed be strings, as
2444 this function does not convert their contents to a string.
2445 \begin{demohigh}
2446 \strSet \lTmpbStr {con}
2447 \strSet \lTmpcStr {cat}
2448 \strConcat \lTmpaStr \lTmpbStr \lTmpcStr
2449 \strUse \lTmpaStr
2450 \end{demohigh}
2451 \end{function}
2452
2453 \begin{function}{\strPutLeft}
2454 \begin{syntax}
2455 \cs{strPutLeft} \meta{str var} \Arg{token list}
2456 \end{syntax}
2457 Converts the \meta{token list} to a \meta{string}, and prepends the
2458 result to \meta{str var}. The current contents of the \meta{str
2459 var} are not automatically converted to a string.
2460 \begin{demohigh}
2461 \strSet \lTmpkStr {Functional}
2462 \strPutLeft \lTmpkStr {Hello}
2463 \strUse \lTmpkStr
2464 \end{demohigh}
2465 \end{function}
2466
2467 \begin{function}{\strPutRight}
2468 \begin{syntax}
2469 \cs{strPutRight} \meta{str var} \Arg{token list}
2470 \end{syntax}
2471 Converts the \meta{token list} to a \meta{string}, and appends the
2472 result to \meta{str var}. The current contents of the \meta{str
2473 var} are not automatically converted to a string.
2474 \begin{demohigh}
2475 \strSet \lTmpkStr {Functional}
2476 \strPutRight \lTmpkStr {World}
2477 \strUse \lTmpkStr
2478 \end{demohigh}
2479 \end{function}
2480
2481 \section{Modifying String Variables}
2482
2483 \begin{function}{\strVarReplaceOnce}
2484 \begin{syntax}
2485 \cs{strVarReplaceOnce} \meta{str var} \Arg{old} \Arg{new}
2486 \end{syntax}
2487 Converts the \meta{old} and \meta{new} token lists to strings, then
2488 replaces the first (leftmost) occurrence of \meta{old string} in the
2489 \meta{str var} with \meta{new string}.
2490 \begin{demohigh}
2491 \strSet \lTmpaStr {a{bc}bcd}
2492 \strVarReplaceOnce \lTmpaStr {bc} {xx}
2493 \strUse \lTmpaStr
2494 \end{demohigh}
2495 \end{function}
2496
2497 \begin{function}{\strVarReplaceAll}
2498 \begin{syntax}
2499 \cs{strVarReplaceAll} \meta{str var} \Arg{old} \Arg{new}
2500 \end{syntax}
2501 Converts the \meta{old} and \meta{new} token lists to strings, then
2502 replaces all occurrences of \meta{old string} in the
2503 \meta{str var} with \meta{new string}.
2504 As this function
2505 operates from left to right, the pattern \meta{old string}
2506 may remain after the replacement.
2507 \begin{demohigh}
2508 \strSet \lTmpaStr {a{bc}bcd}
2509 \strVarReplaceAll \lTmpaStr {bc} {xx}
2510 \strUse \lTmpaStr
2511 \end{demohigh}
2512 \end{function}
2513
2514 \begin{function}{\strVarRemoveOnce}
2515 \begin{syntax}
2516 \cs{strVarRemoveOnce} \meta{str var} \Arg{token list}
2517 \end{syntax}
2518 Converts the \meta{token list} to a \meta{string} then
2519 removes the first (leftmost) occurrence of \meta{string} from the \meta{str var}.
2520 \begin{demohigh}
2521 \strSet \lTmpaStr {a{bc}bcd}
2522 \strVarRemoveOnce \lTmpaStr {bc}
2523 \strUse \lTmpaStr
2524 \end{demohigh}
2525 \end{function}
2526
2527 \begin{function}{\strVarRemoveAll}
2528 \begin{syntax}
2529 \cs{strVarRemoveAll} \meta{str var} \Arg{token list}
2530 \end{syntax}
2531 Converts the \meta{token list} to a \meta{string} then
2532 removes all occurrences of \meta{string} from the \meta{str var}.
2533 As this function operates from left to right,
2534 the pattern \meta{string} may remain after the removal, for instance,
2535 \begin{demohigh}
2536 \strSet \lTmpaStr {abbccd}
2537 \strVarRemoveAll \lTmpaStr {bc}
2538 \tlUse \lTmpaStr
2539 \end{demohigh}
2540 \end{function}
2541
2542 \section{Working with the Content of Strings}
2543
2544 \begin{function}{\strCount}
2545 \begin{syntax}
2546 \cs{strCount} \Arg{token list}
2547 \end{syntax}
2548 Returns the number of characters in the string representation of \meta{token list},
2549 as an integer denotation. All characters including spaces are counted.
2550 \begin{demohigh}
2551 \strCount {12\abc34}
2552 \end{demohigh}
2553 \end{function}
2554
2555 \begin{function}{\StVarCount}
2556 \begin{syntax}
2557 \cs{strVarCount} \meta{tl var}
2558 \end{syntax}
2559 Returns the number of characters in the string representation of the \meta{tl var},
2560 as an integer denotation. All characters including spaces are counted.
2561 \begin{demohigh}
2562 \strSet \lTmpaStr {12\abc34}
2563 \strVarCount \lTmpaStr
2564 \end{demohigh}
2565 \end{function}
2566
2567 \begin{function}{\strHead}
2568 \begin{syntax}
2569 \cs{strHead} \Arg{token list}
2570 \end{syntax}
2571 Converts the \meta{token list} into a \meta{string}.
2572 The first character in the \meta{string} is then returned,
2573 with category code \enquote{other}. If the first character is a space,
2574 it returns a space token with category code $10$ (blank space).
2575 If the \meta{string} is empty, then nothing is returned.
2576 \begin{demohigh}
2577 \strHead {HELLO}
2578 \end{demohigh}
2579 \end{function}
2580
2581 \begin{function}{\strVarHead}
2582 \begin{syntax}
2583 \cs{strVarHead} \meta{tl var}
2584 \end{syntax}
2585 Converts the \meta{tl var} into a \meta{string}.
2586 The first character in the \meta{string} is then returned,
2587 with category code \enquote{other}. If the first character is a space,
2588 it returns a space token with category code $10$ (blank space).
2589 If the \meta{string} is empty, then nothing is returned.
2590 \begin{demohigh}
2591 \strSet \lTmpaStr {HELLO}
2592 \strVarHead \lTmpaStr
2593 \end{demohigh}
2594 \end{function}
2595
2596 \begin{function}{\strTail}
2597 \begin{syntax}
2598 \cs{strTail} \Arg{token list}
2599 \end{syntax}
2600 Converts the \meta{token list} to a \meta{string}, removes the first
2601 character, and returns the remaining characters (if any)
2602 with category codes $12$ and $10$ (for spaces).
2603 If the first character is a space, it only trims that space.
2604 If the \meta{token list} is empty,
2605 then nothing is left on the input stream.
2606 \begin{demohigh}
2607 \strTail {HELLO}
2608 \end{demohigh}
2609 \end{function}
2610
2611 \begin{function}{\strVarTail}
2612 \begin{syntax}
2613 \cs{strVarTail} \meta{tl var}
2614 \end{syntax}
2615 Converts the \meta{tl var} to a \meta{string}, removes the first
2616 character, and returns the remaining characters (if any)
2617 with category codes $12$ and $10$ (for spaces).
2618 If the first character is a space, it only trims that space.
2619 If the \meta{token list} is empty,
2620 then nothing is left on the input stream.
2621 \begin{demohigh}
2622 \strSet \lTmpaStr {HELLO}
2623 \strVarTail \lTmpaStr
2624 \end{demohigh}
2625 \end{function}
2626
2627 \begin{function}{\strItem}
2628 \begin{syntax}
2629 \cs{strItem} \Arg{token list} \Arg{integer expression}
2630 \end{syntax}
2631 Converts the \meta{token list} to a \meta{string},
2632 and returns the character in position \meta{integer expression} of
2633 the \meta{string}, starting at $1$ for the first (left-most)
2634 character. All characters including spaces are taken into account.
2635 If the \meta{integer expression} is negative,
2636 characters are counted from the end of the \meta{string}. Hence,
2637 $-1$ is the right-most character, \emph{etc.}
2638 \begin{demohigh}
2639 \strItem {abcd} {3}
2640 \end{demohigh}
2641 \end{function}
2642
2643 \begin{function}{\strVarItem}
2644 \begin{syntax}
2645 \cs{strVarItem} \meta{tl var} \Arg{integer expression}
2646 \end{syntax}
2647 Converts the \meta{tl var} to a \meta{string},
2648 and returns the character in position \meta{integer expression} of
2649 the \meta{string}, starting at $1$ for the first (left-most)
2650 character. All characters including spaces are taken into account.
2651 If the \meta{integer expression} is negative,
2652 characters are counted from the end of the \meta{string}. Hence,
2653 $-1$ is the right-most character, \emph{etc.}
2654 \begin{demohigh}
2655 \strSet \lTmpaStr {abcd}
2656 \strVarItem \lTmpaStr {3}
2657 \end{demohigh}
2658 \end{function}
2659
2660 \section{Mapping over Strings}
2661
2662 %All mappings are done at the current group level, \emph{i.e.} any
2663 %local assignments made by the \meta{function} or \meta{code} discussed
2664 %below remain in effect after the loop.
2665
2666 %\begin{function}{\strMapFunction,\strVarMapFunction}
2667 %\begin{syntax}
2668 %\cs{strMapFunction} \Arg{token list} \meta{function}
2669 %\cs{strVarMapFunction} \meta{str var} \meta{function}
2670 %\end{syntax}
2671 %Converts the \meta{token list} to a \meta{string} then
2672 %applies \meta{function} to every \meta{character} in the
2673 %\meta{string} including spaces.
2674 %\end{function}
2675
2676 \begin{function}{\strMapInline,\strVarMapInline}
2677 \begin{syntax}
2678 \cs{strMapInline} \Arg{token list} \Arg{inline function}
2679 \cs{strVarMapInline} \meta{str var} \Arg{inline function}
2680 \end{syntax}
2681 Converts the \meta{token list} to a \meta{string} then
2682 applies the \meta{inline function} to every \meta{character} in the
2683 \meta{str var} including spaces.
2684 The \meta{inline function} should consist of code which
2685 receives the \meta{character} as \verb|#1|.
2686 \begin{demohigh}
2687 \IgnoreSpacesOn
2688 \strClear \lTmpaStr
2689 \strMapInline {one} {
2690 \strPutRight \lTmpaStr {[#1]}
2691 }
2692 \strUse \lTmpaStr
2693 \IgnoreSpacesOff
2694 \end{demohigh}
2695 \end{function}
2696
2697 %\begin{function}{\strMapTokens, \strVarMapTokens}
2698 %\begin{syntax}
2699 %\cs{strMapTokens} \Arg{token list} \Arg{code}
2700 %\cs{strVarMapTokens} \meta{str var} \Arg{code}
2701 %\end{syntax}
2702 %Converts the \meta{token list} to a \meta{string} then applies
2703 %\meta{code} to every \meta{character} in the \meta{string} including spaces.
2704 %The \meta{code} receives each character as a trailing brace group.
2705 %This is equivalent to \cs{strMapFunction} if the
2706 %\meta{code} consists of a single function.
2707 %\end{function}
2708
2709 \begin{function}{\strMapVariable,\strMapVariable}
2710 \begin{syntax}
2711 \cs{strMapVariable} \Arg{token list} \meta{variable} \Arg{code}
2712 \cs{strVarMapVariable} \meta{str var} \meta{variable} \Arg{code}
2713 \end{syntax}
2714 Converts the \meta{token list} to a \meta{string} then stores each
2715 \meta{character} in the \meta{string} (including spaces) in turn in
2716 the (string or token list) \meta{variable} and applies the
2717 \meta{code}. The \meta{code} will usually make use of the
2718 \meta{variable}, but this is not enforced. The assignments to the
2719 \meta{variable} are local. Its value after the loop is the last
2720 \meta{character} in the \meta{string}, or its original value if the
2721 \meta{string} is empty.
2722 \begin{demohigh}
2723 \IgnoreSpacesOn
2724 \strClear \lTmpaStr
2725 \strMapVariable {one} \lTmpiStr {
2726 \strPutRight \lTmpaStr {\expWhole {[\lTmpiStr]}}
2727 }
2728 \strUse \lTmpaStr
2729 \IgnoreSpacesOff
2730 \end{demohigh}
2731 \end{function}
2732
2733 %\begin{function}{\strMapBreak}
2734 %\begin{syntax}
2735 %\cs{strMapBreak}
2736 %\end{syntax}
2737 %Used to terminate a string map function before all
2738 %characters in the \meta{string} have been processed. This
2739 %normally takes place within a conditional statement, for example
2740 %\begin{verbatim}
2741 %\str_map_inline:Nn \l_my_str
2742 %{
2743 %\str_if_eq:nnT { #1 } { bingo } { \str_map_break: }
2744 %Do something useful
2745 %}
2746 %\end{verbatim}
2747 %See also \cs{strMapBreakDo}.
2748 %Use outside of a string map scenario leads to low
2749 %level \TeX{} errors.
2750 %\begin{texnote}
2751 %When the mapping is broken, additional tokens may be inserted
2752 %before continuing with the
2753 %code that follows the loop.
2754 %This depends on the design of the mapping function.
2755 %\end{texnote}
2756 %\end{function}
2757 %
2758 %\begin{function}{\strMapBreakDo}
2759 %\begin{syntax}
2760 %\cs{strMapBreakDo} \Arg{code}
2761 %\end{syntax}
2762 %Used to terminate a string map function before all
2763 %characters in the \meta{string} have been processed, inserting
2764 %the \meta{code} after the mapping has ended. This
2765 %normally takes place within a conditional statement, for example
2766 %\begin{verbatim}
2767 %\str_map_inline:Nn \l_my_str
2768 %{
2769 %\str_if_eq:nnT { #1 } { bingo }
2770 %{ \str_map_break:n { <code> } }
2771 %Do something useful
2772 %}
2773 %\end{verbatim}
2774 %Use outside of a string map scenario leads to low
2775 %level \TeX{} errors.
2776 %\begin{texnote}
2777 %When the mapping is broken, additional tokens may be inserted
2778 %before the \meta{code} is
2779 %inserted into the input stream.
2780 %This depends on the design of the mapping function.
2781 %\end{texnote}
2782 %\end{function}
2783
2784 \section{String Conditionals}
2785
2786 \begin{function}{\strIfExist,\strIfExistT,\strIfExistF,\strIfExistTF}
2787 \begin{syntax}
2788 \cs{strIfExist} \meta{str var}
2789 \cs{strIfExistT} \meta{str var} \Arg{true code}
2790 \cs{strIfExistF} \meta{str var} \Arg{false code}
2791 \cs{strIfExistTF} \meta{str var} \Arg{true code} \Arg{false code}
2792 \end{syntax}
2793 Tests whether the \meta{str var} is currently defined. This does not
2794 check that the \meta{str var} really is a string.
2795 \begin{demohigh}
2796 \strIfExistTF \lTmpaStr {\prgReturn{Yes}} {\prgReturn{No}}
2797 \strIfExistTF \lFooUndefinedStr {\prgReturn{Yes}} {\prgReturn{No}}
2798 \end{demohigh}
2799 \end{function}
2800
2801 \begin{function}{\strVarIfEmpty,\strVarIfEmptyT,\strVarIfEmptyF,\strVarIfEmptyTF}
2802 \begin{syntax}
2803 \cs{strVarIfEmpty} \meta{str var}
2804 \cs{strVarIfEmptyT} \meta{str var} \Arg{true code}
2805 \cs{strVarIfEmptyF} \meta{str var} \Arg{false code}
2806 \cs{strVarIfEmptyTF} \meta{str var} \Arg{true code} \Arg{false code}
2807 \end{syntax}
2808 Tests if the \meta{string variable} is entirely empty
2809 (\emph{i.e.} contains no characters at all).
2810 \begin{demohigh}
2811 \strSet \lTmpaStr {abc}
2812 \strVarIfEmptyTF \lTmpaStr {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
2813 \strClear \lTmpaStr
2814 \strVarIfEmptyTF \lTmpaStr {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
2815 \end{demohigh}
2816 \end{function}
2817
2818 \begin{function}{\strIfEq,\strIfEqT,\strIfEqF,\strIfEqTF}
2819 \begin{syntax}
2820 \cs{strIfEq} \Arg{tl_1} \Arg{tl_2}
2821 \cs{strIfEqT} \Arg{tl_1} \Arg{tl_2} \Arg{true code}
2822 \cs{strIfEqF} \Arg{tl_1} \Arg{tl_2} \Arg{false code}
2823 \cs{strIfEqTF} \Arg{tl_1} \Arg{tl_2} \Arg{true code} \Arg{false code}
2824 \end{syntax}
2825 Compares the two \meta{token lists} on a character by character
2826 basis (namely after converting them to strings),
2827 and is \texttt{true} if the two \meta{strings} contain the same
2828 characters in the same order.
2829 %Thus for example
2830 %\begin{codehigh}
2831 %\strIfEq{abc}{\tlToStr{abc}}
2832 %\end{codehigh}
2833 %is logically \texttt{true}.
2834 See \cs{tlIfEq} to compare
2835 tokens (including their category codes) rather than characters.
2836 For example
2837 \begin{demohigh}
2838 \strIfEqTF {abc} {abc} {\prgReturn{Yes}} {\prgReturn{No}}
2839 \strIfEqTF {abc} {xyz} {\prgReturn{Yes}} {\prgReturn{No}}
2840 \end{demohigh}
2841 %\begin{demohigh}
2842 %\strSet\lTmpaStr{abc}
2843 %\strSet\lTmpbStr{abc}
2844 %\strSet\lTmpcStr{xyz}
2845 %\strIfEqTF{\lTmpaStr}{\lTmpbStr}{\prgReturn{Yes}}{\prgReturn{No}}
2846 %\strIfEqTF{\lTmpaStr}{\lTmpcStr}{\prgReturn{Yes}}{\prgReturn{No}}
2847 %\strIfEqTF{\strUse\lTmpaStr}{\strUse\lTmpbStr}{\prgReturn{Yes}}{\prgReturn{No}}
2848 %\strIfEqTF{\strUse\lTmpaStr}{\strUse\lTmpcStr}{\prgReturn{Yes}}{\prgReturn{No}}
2849 %\end{demohigh}
2850 \end{function}
2851
2852 \begin{function}{\strVarIfEq,\strVarIfEqT,\strVarIfEqF,\strVarIfEqTF}
2853 \begin{syntax}
2854 \cs{strVarIfEq} \meta{str var_1} \meta{str var_2}
2855 \cs{strVarIfEqT} \meta{str var_1} \meta{str var_2} \Arg{true code}
2856 \cs{strVarIfEqF} \meta{str var_1} \meta{str var_2} \Arg{false code}
2857 \cs{strVarIfEqTF} \meta{str var_1} \meta{str var_2} \Arg{true code} \Arg{false code}
2858 \end{syntax}
2859 Compares the content of two \meta{str variables} and
2860 is logically \texttt{true} if the two contain the same characters
2861 in the same order. See \cs{tlVarIfEq} to compare tokens
2862 (including their category codes) rather than characters.
2863 \begin{demohigh}
2864 \strSet \lTmpaStr {abc}
2865 \strSet \lTmpbStr {abc}
2866 \strSet \lTmpcStr {xyz}
2867 \strVarIfEqTF \lTmpaStr \lTmpbStr {\prgReturn{Yes}} {\prgReturn{No}}
2868 \strVarIfEqTF \lTmpaStr \lTmpcStr {\prgReturn{Yes}} {\prgReturn{No}}
2869 \end{demohigh}
2870 \end{function}
2871
2872 \begin{function}{\strIfIn,\strIfInT,\strIfInF,\strIfInTF}
2873 \begin{syntax}
2874 \cs{strIfIn} \Arg{tl_1} \Arg{tl_2}
2875 \cs{strIfInT} \Arg{tl_1} \Arg{tl_2} \Arg{true code}
2876 \cs{strIfInF} \Arg{tl_1} \Arg{tl_2} \Arg{false code}
2877 \cs{strIfInTF} \Arg{tl_1} \Arg{tl_2} \Arg{true code} \Arg{false code}
2878 \end{syntax}
2879 Converts both \meta{token lists} to \meta{strings} and
2880 tests whether \meta{string_2} is found inside \meta{string_1}.
2881 \begin{demohigh}
2882 \strIfInTF {hello world} {o} {\prgReturn{Yes}}{\prgReturn{No}}
2883 \strIfInTF {hello world} {a} {\prgReturn{Yes}}{\prgReturn{No}}
2884 \end{demohigh}
2885 \end{function}
2886
2887 \begin{function}{\strVarIfIn,\strVarIfInT,\strVarIfInF,\strVarIfInTF}
2888 \begin{syntax}
2889 \cs{strVarIfIn} \meta{str var} \Arg{token list}
2890 \cs{strVarIfInT} \meta{str var} \Arg{token list} \Arg{true code}
2891 \cs{strVarIfInF} \meta{str var} \Arg{token list} \Arg{false code}
2892 \cs{strVarIfInTF} \meta{str var} \Arg{token list} \Arg{true code} \Arg{false code}
2893 \end{syntax}
2894 Converts the \meta{token list} to a \meta{string} and
2895 tests if that \meta{string} is found in the content of the \meta{str var}.
2896 \begin{demohigh}
2897 \strSet \lTmpaStr {hello world}
2898 \strVarIfInTF \lTmpaStr {o} {\prgReturn{Yes}}{\prgReturn{No}}
2899 \strVarIfInTF \lTmpaStr {a} {\prgReturn{Yes}}{\prgReturn{No}}
2900 \end{demohigh}
2901 \end{function}
2902
2903 \begin{function}{\strCompare,\strCompareT,\strCompareF,\strCompareTF}
2904 \begin{syntax}
2905 \cs{strCompare} \Arg{tl_1} \meta{relation} \Arg{tl_2}
2906 \cs{strCompareT} \Arg{tl_1} \meta{relation} \Arg{tl_2} \Arg{true code}
2907 \cs{strCompareF} \Arg{tl_1} \meta{relation} \Arg{tl_2} \Arg{false code}
2908 \cs{strCompareTF} \Arg{tl_1} \meta{relation} \Arg{tl_2} \Arg{true code} \Arg{false code}
2909 \end{syntax}
2910 Compares the two \meta{token lists} on a character by character
2911 basis (namely after converting them to strings) in a lexicographic
2912 order according to the character codes of the characters. The
2913 \meta{relation} can be \verb|<|, \verb|=|, or \verb|>| and the test is
2914 \texttt{true} under the following conditions:
2915 \begin{itemize}[nosep]
2916 \item for \verb|<|, if the first string is earlier than the second in lexicographic order;
2917 \item for \verb|=|, if the two strings have exactly the same characters;
2918 \item for \verb|>|, if the first string is later than the second in lexicographic order.
2919 \end{itemize}
2920 For example:
2921 \begin{demohigh}
2922 \strCompareTF {ab} < {abc} {\prgReturn{Yes}} {\prgReturn{No}}
2923 \strCompareTF {ab} < {aa} {\prgReturn{Yes}} {\prgReturn{No}}
2924 \end{demohigh}
2925 %\begin{texnote}
2926 %This is a wrapper around the \TeX{} primitive
2927 %\cs{(pdf)strcmp}. It is meant for programming
2928 %and not for sorting textual contents, as it simply considers
2929 %character codes and not more elaborate considerations of grapheme
2930 %clusters, locale, etc.
2931 %\end{texnote}
2932 \end{function}
2933
2934 \section{String Case Functions}
2935
2936 \begin{function}{\strCase}
2937 \begin{syntax}
2938 \cs{strCase} \Arg{test string}
2939 ~ ~ \verb|{|
2940 ~ ~ ~ ~ \Arg{string case_1} \Arg{code case_1}
2941 ~ ~ ~ ~ \Arg{string case_2} \Arg{code case_2}
2942 ~ ~ ~ ~ $\cdots$
2943 ~ ~ ~ ~ \Arg{string case_n} \Arg{code case_n}
2944 ~ ~ \verb|}|
2945 \end{syntax}
2946 Compares the \meta{test string} in turn with each
2947 of the \meta{string cases} (all token lists are converted to strings).
2948 If the two are equal (as described for
2949 \cs{strIfEq}) then the associated \meta{code} is left in the
2950 input stream and other cases are discarded.
2951 \begin{demohigh}
2952 \IgnoreSpacesOn
2953 \strCase {bbb} {
2954 {aaa} {\prgReturn{First}}
2955 {bbb} {\prgReturn{Second}}
2956 {ccb} {\prgReturn{Third}}
2957 }
2958 \IgnoreSpacesOff
2959 \end{demohigh}
2960 \end{function}
2961
2962 \begin{function}{\strCaseT}
2963 \begin{syntax}
2964 \cs{strCaseT} \Arg{test string}
2965 ~ ~ \verb|{|
2966 ~ ~ ~ ~ \Arg{string case_1} \Arg{code case_1}
2967 ~ ~ ~ ~ \Arg{string case_2} \Arg{code case_2}
2968 ~ ~ ~ ~ $\cdots$
2969 ~ ~ ~ ~ \Arg{string case_n} \Arg{code case_n}
2970 ~ ~ \verb|}|
2971 ~ ~ \Arg{true code}
2972 \end{syntax}
2973 Compares the \meta{test string} in turn with each
2974 of the \meta{string cases} (all token lists are converted to strings).
2975 If the two are equal (as described for
2976 \cs{strIfEq}) then the associated \meta{code} is left in the
2977 input stream and other cases are discarded. If any of the
2978 cases are matched, the \meta{true code} is also inserted into the
2979 input stream (after the code for the appropriate case).
2980 \begin{demohigh}
2981 \IgnoreSpacesOn
2982 \strCaseT {bbb} {
2983 {aaa} {\tlSet\lTmpkTl{First}}
2984 {bbb} {\tlSet\lTmpkTl{Second}}
2985 {ccb} {\tlSet\lTmpkTl{Third}}
2986 }{
2987 \prgReturn{\tlUse\lTmpkTl}
2988 }
2989 \IgnoreSpacesOff
2990 \end{demohigh}
2991 \end{function}
2992
2993 \begin{function}{\strCaseF}
2994 \begin{syntax}
2995 \cs{strCaseF} \Arg{test string}
2996 ~ ~ \verb|{|
2997 ~ ~ ~ ~ \Arg{string case_1} \Arg{code case_1}
2998 ~ ~ ~ ~ \Arg{string case_2} \Arg{code case_2}
2999 ~ ~ ~ ~ $\cdots$
3000 ~ ~ ~ ~ \Arg{string case_n} \Arg{code case_n}
3001 ~ ~ \verb|}|
3002 ~ ~ \Arg{false code}
3003 \end{syntax}
3004 Compares the \meta{test string} in turn with each
3005 of the \meta{string cases} (all token lists are converted to strings).
3006 If the two are equal (as described for
3007 \cs{strIfEq}) then the associated \meta{code} is left in the
3008 input stream and other cases are discarded. If none
3009 match then the \meta{false code} is inserted.
3010 \begin{demohigh}
3011 \IgnoreSpacesOn
3012 \strCaseF {bbb} {
3013 {aaa} {\prgReturn{First}}
3014 {bbb} {\prgReturn{Second}}
3015 {ccb} {\prgReturn{Third}}
3016 }{
3017 \prgReturn{No~Match!}
3018 }
3019 \IgnoreSpacesOff
3020 \end{demohigh}
3021 \end{function}
3022
3023 \begin{function}{\strCaseTF}
3024 \begin{syntax}
3025 \cs{strCaseTF} \Arg{test string}
3026 ~ ~ \verb|{|
3027 ~ ~ ~ ~ \Arg{string case_1} \Arg{code case_1}
3028 ~ ~ ~ ~ \Arg{string case_2} \Arg{code case_2}
3029 ~ ~ ~ ~ $\cdots$
3030 ~ ~ ~ ~ \Arg{string case_n} \Arg{code case_n}
3031 ~ ~ \verb|}|
3032 ~ ~ \Arg{true code}
3033 ~ ~ \Arg{false code}
3034 \end{syntax}
3035 Compares the \meta{test string} in turn with each
3036 of the \meta{string cases} (all token lists are converted to strings).
3037 If the two are equal (as described for
3038 \cs{strIfEq}) then the associated \meta{code} is left in the
3039 input stream and other cases are discarded. If any of the
3040 cases are matched, the \meta{true code} is also inserted into the
3041 input stream (after the code for the appropriate case), while if none
3042 match then the \meta{false code} is inserted.
3043 \begin{demohigh}
3044 \IgnoreSpacesOn
3045 \strCaseTF {bbb} {
3046 {aaa} {\tlSet\lTmpkTl{First}}
3047 {bbb} {\tlSet\lTmpkTl{Second}}
3048 {ccb} {\tlSet\lTmpkTl{Third}}
3049 }{
3050 \prgReturn{\tlUse\lTmpkTl}
3051 }{
3052 \prgReturn{No~Match!}
3053 }
3054 \IgnoreSpacesOff
3055 \end{demohigh}
3056 \end{function}
3057
3058 \chapter{Integers (\texttt{Int})}
3059
3060 \section{Constant and Scratch Integers}
3061
3062 \begin{variable}{\cZeroInt,\cOneInt}
3063 Integer values used with primitive tests and assignments: their
3064 self-terminating nature makes these more convenient and faster than
3065 literal numbers.
3066 \end{variable}
3067
3068 \begin{variable}{\cMaxInt}
3069 The maximum value that can be stored as an integer.
3070 \end{variable}
3071
3072 \begin{variable}{\cMaxRegisterInt}
3073 Maximum number of registers.
3074 \end{variable}
3075
3076 \begin{variable}{\cMaxCharInt}
3077 Maximum character code completely supported by the engine.
3078 \end{variable}
3079
3080 \begin{variable}{\lTmpaInt,\lTmpbInt,\lTmpcInt,\lTmpiInt,\lTmpjInt,\lTmpkInt}
3081 Scratch integer for local assignment. These are never used by
3082 the \verb!functional! package, and so are safe for use with any
3083 function. However, they may be overwritten by other
3084 code and so should only be used for short-term storage.
3085 \end{variable}
3086
3087 \begin{variable}{\gTmpaInt,\gTmpbInt,\gTmpcInt,\gTmpiInt,\gTmpjInt,\gTmpkInt}
3088 Scratch integer for global assignment. These are never used by
3089 the \verb!functional! package, and so are safe for use with any
3090 function. However, they may be overwritten by other
3091 code and so should only be used for short-term storage.
3092 \end{variable}
3093
3094 \section{The Syntax of Integer Expressions}
3095
3096 An \meta{integer expression} should consist,
3097 after evaluation of functions defined with \cs{PrgNewFunction}
3098 and expansion, of \texttt{+}, \texttt{-}, \texttt{*}, \texttt{/},
3099 \texttt{(}, \texttt{)} and of course integer operands. The result
3100 is calculated by applying standard mathematical rules with the
3101 following peculiarities:
3102 \begin{itemize}
3103 \item \texttt{/} denotes division rounded to the closest integer with
3104 ties rounded away from zero;
3105 \item there is an error and the overall expression evaluates to zero
3106 whenever the absolute value of any intermediate result exceeds
3107 $2^{31}-1$, except in the case of scaling operations
3108 $a$\texttt{*}$b$\texttt{/}$c$, for which $a$\texttt{*}$b$ may be
3109 arbitrarily large (but the operands $a$, $b$, $c$ are still
3110 constrained to an absolute value at most $2^{31}-1$);
3111 \item parentheses may not appear after unary \texttt{+} or
3112 \texttt{-}, namely placing \texttt{+(} or \texttt{-(} at the start
3113 of an expression or after \texttt{+}, \texttt{-}, \texttt{*},
3114 \texttt{/} or~\texttt{(} leads to an error.
3115 \end{itemize}
3116 Each integer operand can be either an integer variable (with no need
3117 for \cs{intUse}) or an integer denotation.
3118 For example both of the following give the same result
3119 because \cs{lFooSomeTl} expands to the integer denotation~$5$
3120 while the integer variable \cs{lFooSomeInt} takes the value~$4$.
3121 \begin{demohigh}
3122 \intEval {5 + 4 * 3 - (3 + 4 * 5)}
3123 \end{demohigh}
3124 \begin{demohigh}
3125 \tlNew \lFooSomeTl
3126 \tlSet \lFooSomeTl {5}
3127 \intNew \lFooSomeInt
3128 \intSet \lFooSomeInt {4}
3129 \intEval {\lFooSomeTl + \lFooSomeInt * 3 - (3 + 4 * 5)}
3130 \end{demohigh}
3131
3132 %\begin{texnote}
3133 %As all \TeX{} integers, integer operands can also be:
3134 %\tn{value}\Arg{\LaTeXe{} counter}; dimension or skip variables,
3135 %converted to integers in~\texttt{sp}; the character code of some
3136 %character given as \texttt{`}\meta{char} or
3137 %\texttt{`\textbackslash}\meta{char}; octal numbers given as
3138 %\texttt{'} followed by digits from \texttt{0} to \texttt{7}; or
3139 %hexadecimal numbers given as \verb|"| followed by digits and upper case
3140 %letters from \texttt{A} to~\texttt{F}.
3141 %\end{texnote}
3142
3143 \section{Using Integer Expressions}
3144
3145 \begin{function}{\intEval}
3146 \begin{syntax}
3147 \cs{intEval} \Arg{integer expression}
3148 \end{syntax}
3149 Evaluates the \meta{integer expression} and returns the result:
3150 for positive results an
3151 explicit sequence of decimal digits not starting with~\texttt{0},
3152 for negative results \texttt{-}~followed by such a sequence, and
3153 \texttt{0}~for zero. For example
3154 \begin{demohigh}
3155 \intEval {(1+4)*(2-3)/5}
3156 \end{demohigh}
3157 \begin{demohigh}
3158 \intEval {\strCount{12\TeX34} - \tlCount{12\TeX34}}
3159 \end{demohigh}
3160 \end{function}
3161
3162 \begin{function}{\intMathAdd}
3163 \begin{syntax}
3164 \cs{intMathAdd} \Arg{integer expression_1} \Arg{integer expression_2}
3165 \end{syntax}
3166 Adds \Arg{integer expression_1} and \Arg{integer expression_2},
3167 and returns the result. For example
3168 \begin{demohigh}
3169 \intMathAdd {7} {3}
3170 \end{demohigh}
3171 \end{function}
3172
3173 \begin{function}{\intMathSub}
3174 \begin{syntax}
3175 \cs{intMathSub} \Arg{integer expression_1} \Arg{integer expression_2}
3176 \end{syntax}
3177 Subtracts \Arg{integer expression_2} from \Arg{integer expression_1},
3178 and returns the result. For example
3179 \begin{demohigh}
3180 \intMathSub {7} {3}
3181 \end{demohigh}
3182 \end{function}
3183
3184 \begin{function}{\intMathMult}
3185 \begin{syntax}
3186 \cs{intMathMult} \Arg{integer expression_1} \Arg{integer expression_2}
3187 \end{syntax}
3188 Multiplies \Arg{integer expression_1} by \Arg{integer expression_2},
3189 and returns the result. For example
3190 \begin{demohigh}
3191 \intMathMult {7} {3}
3192 \end{demohigh}
3193 \end{function}
3194
3195 \begin{function}{\intMathDiv}
3196 \begin{syntax}
3197 \cs{intMathDiv} \Arg{integer expression_1} \Arg{integer expression_2}
3198 \end{syntax}
3199 Evaluates the two \meta{integer expressions} as described earlier,
3200 then divides the first value by the second, and rounds the result
3201 to the closest integer. Ties are rounded away from zero. Note that
3202 this is identical to using \verb|/| directly in an \meta{integer expression}.
3203 The result is returned as an \meta{integer denotation}.
3204 For example
3205 \begin{demohigh}
3206 \intMathDiv {8} {3}
3207 \end{demohigh}
3208 \end{function}
3209
3210 \begin{function}{\intMathDivTrancate}
3211 \begin{syntax}
3212 \cs{intMathDivTrancate} \Arg{integer expression_1} \Arg{integer expression_2}
3213 \end{syntax}
3214 Evaluates the two \meta{integer expressions} as described earlier,
3215 then divides the first value by the second, and rounds the result towards zero.
3216 Note that division using \verb|/| rounds to the closest integer instead.
3217 The result is returned as an \meta{integer denotation}.
3218 For example
3219 \begin{demohigh}
3220 \intMathDivTruncate {8} {3}
3221 \end{demohigh}
3222 \end{function}
3223
3224 \begin{function}{\intMathSign}
3225 \begin{syntax}
3226 \cs{intMathSign} \Arg{intexpr}
3227 \end{syntax}
3228 Evaluates the \meta{integer expression} then leaves $1$ or $0$ or
3229 $-1$ in the input stream according to the sign of the result.
3230 \end{function}
3231
3232 \begin{function}{\intMathAbs}
3233 \begin{syntax}
3234 \cs{intMathAbs} \Arg{integer expression}
3235 \end{syntax}
3236 Evaluates the \meta{integer expression} as described for
3237 \cs{intEval} and leaves the absolute value of the result in
3238 the input stream as an \meta{integer denotation} after two
3239 expansions.
3240 \end{function}
3241
3242 \begin{function}{\intMathMax,\intMathMin}
3243 \begin{syntax}
3244 \cs{intMathMax} \Arg{intexpr_1} \Arg{intexpr_2}
3245 \cs{intMathMin} \Arg{intexpr_1} \Arg{intexpr_2}
3246 \end{syntax}
3247 Evaluates the \meta{integer expressions} as described for
3248 \cs{intEval} and leaves either the larger or smaller value
3249 in the input stream as an \meta{integer denotation} after two
3250 expansions.
3251 \end{function}
3252
3253 \begin{function}{\intMathMod}
3254 \begin{syntax}
3255 \cs{intMathMod} \Arg{intexpr_1} \Arg{intexpr_2}
3256 \end{syntax}
3257 Evaluates the two \meta{integer expressions} as described earlier,
3258 then calculates the integer remainder of dividing the first
3259 expression by the second. This is obtained by subtracting
3260 \cs{intMathDivTruncate} \Arg{intexpr_1} \Arg{intexpr_2} times
3261 \meta{intexpr_2} from \meta{intexpr_1}. Thus, the result has the
3262 same sign as \meta{intexpr_1} and its absolute value is strictly
3263 less than that of \meta{intexpr_2}. The result is left in the input
3264 stream as an \meta{integer denotation} after two expansions.
3265 \end{function}
3266
3267 \begin{function}{\intMathRand}
3268 \begin{syntax}
3269 \cs{intMathRand} \Arg{intexpr_1} \Arg{intexpr_2}
3270 \end{syntax}
3271 Evaluates the two \meta{integer expressions} and produces a
3272 pseudo-random number between the two (with bounds included).
3273 %This is not available in older versions of \XeTeX{}.
3274 \end{function}
3275
3276 \section{Creating and Using Integers}
3277
3278 \begin{function}{\intNew}
3279 \begin{syntax}
3280 \cs{intNew} \meta{integer}
3281 \end{syntax}
3282 Creates a new \meta{integer} or raises an error if the name is
3283 already taken. The declaration is global. The \meta{integer} is
3284 initially equal to $0$.
3285 \end{function}
3286
3287 \begin{function}{\intConst}
3288 \begin{syntax}
3289 \cs{intConst} \meta{integer} \Arg{integer expression}
3290 \end{syntax}
3291 Creates a new constant \meta{integer} or raises an error if the name
3292 is already taken. The value of the \meta{integer} is set
3293 globally to the \meta{integer expression}.
3294 \end{function}
3295
3296 \begin{function}{\intUse}
3297 \begin{syntax}
3298 \cs{intUse} \meta{integer}
3299 \end{syntax}
3300 Recovers the content of an \meta{integer} and returns the value.
3301 An error is raised if the variable does not exist or if it is invalid.
3302 \end{function}
3303
3304 \section{Viewing Integers}
3305
3306 \begin{function}{\intLog}
3307 \begin{syntax}
3308 \cs{intLog} \Arg{integer expression}
3309 \end{syntax}
3310 Writes the result of evaluating the \meta{integer expression}
3311 in the log file.
3312 \end{function}
3313
3314 \begin{function}{\intVarLog}
3315 \begin{syntax}
3316 \cs{intVarLog} \meta{integer}
3317 \end{syntax}
3318 Writes the value of the \meta{integer} in the log file.
3319 \end{function}
3320
3321 \begin{function}{\intShow}
3322 \begin{syntax}
3323 \cs{intShow} \Arg{integer expression}
3324 \end{syntax}
3325 Displays the result of evaluating the \meta{integer expression}
3326 on the terminal.
3327 \end{function}
3328
3329 \begin{function}{\intVarShow}
3330 \begin{syntax}
3331 \cs{intVarShow} \meta{integer}
3332 \end{syntax}
3333 Displays the value of the \meta{integer} on the terminal.
3334 \end{function}
3335
3336 \section{Setting Integer Variables}
3337
3338 \begin{function}{\intSet}
3339 \begin{syntax}
3340 \cs{intSet} \meta{integer} \Arg{integer expression}
3341 \end{syntax}
3342 Sets \meta{integer} to the value of \meta{integer expression},
3343 which must evaluate to an integer (as described for \cs{intEval}).
3344 For example
3345 \begin{demohigh}
3346 \intSet \lTmpaInt {3+5}
3347 \intUse \lTmpaInt
3348 \end{demohigh}
3349 \end{function}
3350
3351 \begin{function}{\intSetEq}
3352 \begin{syntax}
3353 \cs{intSetEq} \meta{integer_1} \meta{integer_2}
3354 \end{syntax}
3355 Sets the content of \meta{integer_1} equal to that of
3356 \meta{integer_2}.
3357 \end{function}
3358
3359 \begin{function}{\intZero}
3360 \begin{syntax}
3361 \cs{intZero} \meta{integer}
3362 \end{syntax}
3363 Sets \meta{integer} to $0$. For example
3364 \begin{demohigh}
3365 \intSet \lTmpaInt {5}
3366 \intZero \lTmpaInt
3367 \intUse \lTmpaInt
3368 \end{demohigh}
3369 \end{function}
3370
3371 \begin{function}{\intZeroNew}
3372 \begin{syntax}
3373 \cs{intZeroNew} \meta{integer}
3374 \end{syntax}
3375 Ensures that the \meta{integer} exists globally by applying
3376 \cs{intNew} if necessary, then applies \cs{intZero} to leave
3377 the \meta{integer} set to zero.
3378 \end{function}
3379
3380 \begin{function}{\intIncr}
3381 \begin{syntax}
3382 \cs{intIncr} \meta{integer}
3383 \end{syntax}
3384 Increases the value stored in \meta{integer} by $1$.
3385 For example
3386 \begin{demohigh}
3387 \intSet \lTmpaInt {5}
3388 \intIncr \lTmpaInt
3389 \intUse \lTmpaInt
3390 \end{demohigh}
3391 \end{function}
3392
3393 \begin{function}{\intDecr}
3394 \begin{syntax}
3395 \cs{intDecr} \meta{integer}
3396 \end{syntax}
3397 Decreases the value stored in \meta{integer} by $1$.
3398 For example
3399 \begin{demohigh}
3400 \intSet \lTmpaInt {5}
3401 \intDecr \lTmpaInt
3402 \intUse \lTmpaInt
3403 \end{demohigh}
3404 \end{function}
3405
3406 \begin{function}{\intAdd}
3407 \begin{syntax}
3408 \cs{intAdd} \meta{integer} \Arg{integer expression}
3409 \end{syntax}
3410 Adds the result of the \meta{integer expression} to the current
3411 content of the \meta{integer}. For example
3412 \begin{demohigh}
3413 \intSet \lTmpaInt {5}
3414 \intAdd \lTmpaInt {2}
3415 \intUse \lTmpaInt
3416 \end{demohigh}
3417 \end{function}
3418
3419 \begin{function}{\intSub}
3420 \begin{syntax}
3421 \cs{intSub} \meta{integer} \Arg{integer expression}
3422 \end{syntax}
3423 Subtracts the result of the \meta{integer expression} from the
3424 current content of the \meta{integer}. For example
3425 \begin{demohigh}
3426 \intSet \lTmpaInt {5}
3427 \intSub \lTmpaInt {3}
3428 \intUse \lTmpaInt
3429 \end{demohigh}
3430 \end{function}
3431
3432 \section{Integer Step Functions}
3433
3434 \begin{function}{\intReplicate}
3435 \begin{syntax}
3436 \cs{intReplicate} \Arg{integer expression} \Arg{tokens}
3437 \end{syntax}
3438 Evaluates the \meta{integer expression} (which should be zero or positive)
3439 and returns the resulting number of copies of the \meta{tokens}.
3440 \begin{demohigh}
3441 \intReplicate {4} {Hello}
3442 \end{demohigh}
3443 \end{function}
3444
3445 %\begin{function}{\intStepFunction}
3446 %\begin{syntax}
3447 %\cs{intStepFunction} \Arg{initial value} \Arg{step} \Arg{final value} \meta{function}
3448 %\end{syntax}
3449 %This function first evaluates the \meta{initial value}, \meta{step}
3450 %and \meta{final value}, all of which should be integer expressions.
3451 %The \meta{function} is then placed in front of each \meta{value}
3452 %from the \meta{initial value} to the \meta{final value} in turn
3453 %(using \meta{step} between each \meta{value}). The \meta{step} must
3454 %be non-zero. If the \meta{step} is positive, the loop stops when
3455 %the \meta{value} becomes larger than the \meta{final value}. If the
3456 %\meta{step} is negative, the loop stops when the \meta{value}
3457 %becomes smaller than the \meta{final value}. The \meta{function}
3458 %should absorb one numerical argument. For example
3459 %\begin{verbatim}
3460 %\cs_set:Npn \my_func:n #1 { [I~saw~#1] \quad }
3461 %\int_step_function:nnnN { 1 } { 1 } { 5 } \my_func:n
3462 %\end{verbatim}
3463 %would print
3464 %\begin{quote}
3465 %[I saw 1] \quad
3466 %[I saw 2] \quad
3467 %[I saw 3] \quad
3468 %[I saw 4] \quad
3469 %[I saw 5] \quad
3470 %\end{quote}
3471 %\end{function}
3472
3473 \begin{function}{\intStepInline}
3474 \begin{syntax}
3475 \cs{intStepInline} \Arg{initial value} \Arg{step} \Arg{final value} \Arg{code}
3476 \end{syntax}
3477 This function first evaluates the \meta{initial value}, \meta{step}
3478 and \meta{final value}, all of which should be integer expressions.
3479 Then for each \meta{value} from the \meta{initial value} to the
3480 \meta{final value} in turn (using \meta{step} between each
3481 \meta{value}), the \meta{code} is inserted into the input stream
3482 with \verb|#1| replaced by the current \meta{value}. Thus the
3483 \meta{code} should define a function of one argument~(\verb|#1|).
3484 \begin{demohigh}
3485 \IgnoreSpacesOn
3486 \tlClear \lTmpaTl
3487 \intStepInline {1} {3} {30} {
3488 \tlPutRight \lTmpaTl {[#1]}
3489 }
3490 \tlUse \lTmpaTl
3491 \IgnoreSpacesOff
3492 \end{demohigh}
3493 \end{function}
3494
3495 \begin{function}{\intStepOneInline}
3496 \begin{syntax}
3497 \cs{intStepOneInline} \Arg{initial value} \Arg{final value} \Arg{code}
3498 \end{syntax}
3499 This function first evaluates the \meta{initial value} and \meta{final value},
3500 all of which should be integer expressions.
3501 Then for each \meta{value} from the \meta{initial value} to the
3502 \meta{final value} in turn (using a fixed step of $1$ between each
3503 \meta{value}), the \meta{code} is inserted into the input stream
3504 with \verb|#1| replaced by the current \meta{value}. Thus the
3505 \meta{code} should define a function of one argument~(\verb|#1|).
3506 \begin{demohigh}
3507 \IgnoreSpacesOn
3508 \tlClear \lTmpaTl
3509 \intStepOneInline {1} {10} {
3510 \tlPutRight \lTmpaTl {[#1]}
3511 }
3512 \tlUse \lTmpaTl
3513 \IgnoreSpacesOff
3514 \end{demohigh}
3515 \end{function}
3516
3517 \begin{function}{\intStepVariable}
3518 \begin{syntax}
3519 \cs{intStepVariable} \Arg{initial value} \Arg{step} \Arg{final value} \meta{tl var} \Arg{code}
3520 \end{syntax}
3521 This function first evaluates the \meta{initial value}, \meta{step}
3522 and \meta{final value}, all of which should be integer expressions.
3523 Then for each \meta{value} from the \meta{initial value} to the
3524 \meta{final value} in turn (using \meta{step} between each
3525 \meta{value}), the \meta{code} is evaluated,
3526 with the \meta{tl~var} defined as the current \meta{value}. Thus
3527 the \meta{code} should make use of the \meta{tl~var}.
3528 %For example
3529 %\begin{demohigh}
3530 %\IgnoreSpacesOn
3531 %\tlClear\lTmpaTl
3532 %\intStepVariable{1}{3}{30}\lTmpiTl{
3533 % \tlPutRight\lTmpaTl{\expValue\lTmpiTl}
3534 % \tlPutRight\lTmpaTl{~}
3535 %}
3536 %\prgReturn{\expValue\lTmpaTl}
3537 %\IgnoreSpacesOff
3538 %\end{demohigh}
3539 \end{function}
3540
3541 \begin{function}{\intStepOneVariable}
3542 \begin{syntax}
3543 \cs{intStepOneVariable} \Arg{initial value} \Arg{final value} \meta{tl var} \Arg{code}
3544 \end{syntax}
3545 This function first evaluates the \meta{initial value} and \meta{final value},
3546 all of which should be integer expressions.
3547 Then for each \meta{value} from the \meta{initial value} to the
3548 \meta{final value} in turn (using a fixed stop of $1$ between each
3549 \meta{value}), the \meta{code} is evaluated,
3550 with the \meta{tl~var} defined as the current \meta{value}. Thus
3551 the \meta{code} should make use of the \meta{tl~var}.
3552 \end{function}
3553
3554 \section{Integer Conditionals}
3555
3556 \begin{function}{\intIfExist,\intIfExistT,\intIfExistF,\intIfExistTF}
3557 \begin{syntax}
3558 \cs{intIfExist} \meta{integer}
3559 \cs{intIfExistT} \meta{integer} \Arg{true code}
3560 \cs{intIfExistF} \meta{integer} \Arg{false code}
3561 \cs{intIfExistTF} \meta{integer} \Arg{true code} \Arg{false code}
3562 \end{syntax}
3563 Tests whether the \meta{integer} is currently defined. This does not
3564 check that the \meta{integer} really is an integer variable.
3565 \end{function}
3566
3567 \begin{function}{\intIfOdd,\intIfOddT,\intIfOddF,\intIfOddTF}
3568 \begin{syntax}
3569 \cs{intIfOdd} \Arg{integer expression}
3570 \cs{intIfOddT} \Arg{integer expression} \Arg{true code}
3571 \cs{intIfOddF} \Arg{integer expression} \Arg{false code}
3572 \cs{intIfOddTF} \Arg{integer expression} \Arg{true code} \Arg{false code}
3573 \end{syntax}
3574 This function first evaluates the \meta{integer expression}
3575 as described for \cs{intEval}.
3576 It then evaluates if this is odd or even, as appropriate.
3577 \end{function}
3578
3579 \begin{function}{\intIfEven,\intIfEvenT,\intIfEvenF,\intIfEvenTF}
3580 \begin{syntax}
3581 \cs{intIfEven} \Arg{integer expression}
3582 \cs{intIfEvenT} \Arg{integer expression} \Arg{true code}
3583 \cs{intIfEvenF} \Arg{integer expression} \Arg{false code}
3584 \cs{intIfEvenTF} \Arg{integer expression} \Arg{true code} \Arg{false code}
3585 \end{syntax}
3586 This function first evaluates the \meta{integer expression}
3587 as described for \cs{intEval}.
3588 It then evaluates if this is even or odd, as appropriate.
3589 \end{function}
3590
3591 \begin{function}{\intCompare,\intCompareT,\intCompareF,\intCompareTF}
3592 \begin{syntax}
3593 \cs{intCompare} \Arg{intexpr_1} \meta{relation} \Arg{intexpr_2}
3594 \cs{intCompareT} \Arg{intexpr_1} \meta{relation} \Arg{intexpr_2} \Arg{true code}
3595 \cs{intCompareF} \Arg{intexpr_1} \meta{relation} \Arg{intexpr_2} \Arg{false code}
3596 \cs{intCompareTF} \Arg{intexpr_1} \meta{relation} \Arg{intexpr_2} \Arg{true code} \Arg{false code}
3597 \end{syntax}
3598 This function first evaluates each of the \meta{integer expressions}
3599 as described for \cs{intEval}. The two results are then
3600 compared using the \meta{relation}:\par
3601 {\centering\begin{tabular}{ll}
3602 Equal & \texttt{=} \\
3603 Greater than & \texttt{>} \\
3604 Less than & \texttt{<} \\
3605 \end{tabular}\par}
3606 For example
3607 \begin{demohigh}
3608 \intCompareTF {2} > {1} {\prgReturn{Greater}} {\prgReturn{Less}}
3609 \intCompareTF {2} > {3} {\prgReturn{Greater}} {\prgReturn{Less}}
3610 \end{demohigh}
3611 \end{function}
3612
3613 \section{Integer Case Functions}
3614
3615 \begin{function}{\intCase}
3616 \begin{syntax}
3617 \cs{intCase} \Arg{test integer expression}
3618 ~ ~ \verb|{|
3619 ~ ~ ~ ~ \Arg{intexpr case_1} \Arg{code case_1}
3620 ~ ~ ~ ~ \Arg{intexpr case_2} \Arg{code case_2}
3621 ~ ~ ~ ~ $\cdots$
3622 ~ ~ ~ ~ \Arg{intexpr case_n} \Arg{code case_n}
3623 ~ ~ \verb|}|
3624 \end{syntax}
3625 This function evaluates the \meta{test integer expression} and
3626 compares this in turn to each of the
3627 \meta{integer expression cases}. If the two are equal then the
3628 associated \meta{code} is left in the input stream
3629 and other cases are discarded.
3630 \end{function}
3631
3632 \begin{function}{\intCaseT}
3633 \begin{syntax}
3634 \cs{intCaseT} \Arg{test integer expression}
3635 ~ ~ \verb|{|
3636 ~ ~ ~ ~ \Arg{intexpr case_1} \Arg{code case_1}
3637 ~ ~ ~ ~ \Arg{intexpr case_2} \Arg{code case_2}
3638 ~ ~ ~ ~ $\cdots$
3639 ~ ~ ~ ~ \Arg{intexpr case_n} \Arg{code case_n}
3640 ~ ~ \verb|}|
3641 ~ ~ \Arg{true code}
3642 \end{syntax}
3643 This function evaluates the \meta{test integer expression} and
3644 compares this in turn to each of the
3645 \meta{integer expression cases}. If the two are equal then the
3646 associated \meta{code} is left in the input stream
3647 and other cases are discarded. If any of the
3648 cases are matched, the \meta{true code} is also inserted into the
3649 input stream (after the code for the appropriate case).
3650 \end{function}
3651
3652 \begin{function}{\intCaseF}
3653 \begin{syntax}
3654 \cs{intCaseF} \Arg{test integer expression}
3655 ~ ~ \verb|{|
3656 ~ ~ ~ ~ \Arg{intexpr case_1} \Arg{code case_1}
3657 ~ ~ ~ ~ \Arg{intexpr case_2} \Arg{code case_2}
3658 ~ ~ ~ ~ $\cdots$
3659 ~ ~ ~ ~ \Arg{intexpr case_n} \Arg{code case_n}
3660 ~ ~ \verb|}|
3661 ~ ~ \Arg{false code}
3662 \end{syntax}
3663 This function evaluates the \meta{test integer expression} and
3664 compares this in turn to each of the
3665 \meta{integer expression cases}. If the two are equal then the
3666 associated \meta{code} is left in the input stream
3667 and other cases are discarded. If none
3668 match then the \meta{false code} is into the input stream
3669 (after the code for the appropriate case).
3670 For example
3671 \begin{demohigh}
3672 \IgnoreSpacesOn
3673 \intCaseF { 2 * 5 }
3674 {
3675 { 5 } { Small }
3676 { 4 + 6 } { Medium }
3677 { -2 * 10 } { Negative }
3678 }
3679 { No idea! }
3680 \IgnoreSpacesOff
3681 \end{demohigh}
3682 \end{function}
3683
3684 \begin{function}{\intCaseTF}
3685 \begin{syntax}
3686 \cs{intCaseTF} \Arg{test integer expression}
3687 ~ ~ \verb|{|
3688 ~ ~ ~ ~ \Arg{intexpr case_1} \Arg{code case_1}
3689 ~ ~ ~ ~ \Arg{intexpr case_2} \Arg{code case_2}
3690 ~ ~ ~ ~ $\cdots$
3691 ~ ~ ~ ~ \Arg{intexpr case_n} \Arg{code case_n}
3692 ~ ~ \verb|}|
3693 ~ ~ \Arg{true code}
3694 ~ ~ \Arg{false code}
3695 \end{syntax}
3696 This function evaluates the \meta{test integer expression} and
3697 compares this in turn to each of the
3698 \meta{integer expression cases}. If the two are equal then the
3699 associated \meta{code} is left in the input stream
3700 and other cases are discarded. If any of the
3701 cases are matched, the \meta{true code} is also inserted into the
3702 input stream (after the code for the appropriate case), while if none
3703 match then the \meta{false code} is inserted.
3704 \end{function}
3705
3706 \chapter{Floating Point Numbers (\texttt{Fp})}
3707
3708 \section{Constant and Scratch Floating Points}
3709
3710 \begin{variable}{\cZeroFp, \cMinusZeroFp}
3711 Zero, with either sign.
3712 \end{variable}
3713
3714 \begin{variable}{\cOneFp}
3715 One as an \texttt{fp}: useful for comparisons in some places.
3716 \end{variable}
3717
3718 \begin{variable}{\cInfFp,\cMinusInfFp}
3719 Infinity, with either sign. These can be input directly in a
3720 floating point expression as \texttt{inf} and \texttt{-inf}.
3721 \end{variable}
3722
3723 \begin{variable}{\cEFp}
3724 The value of the base of the natural logarithm, $\mathrm{e} = \exp(1)$.
3725 \end{variable}
3726
3727 \begin{variable}{\cPiFp}
3728 The value of $\pi$. This can be input directly in a floating point
3729 expression as \texttt{pi}.
3730 \end{variable}
3731
3732 \begin{variable}{\cOneDegreeFp}
3733 The value of $1^{\circ}$ in radians. Multiply an angle given in
3734 degrees by this value to obtain a result in radians. Note that
3735 trigonometric functions expecting an argument in radians or in
3736 degrees are both available. Within floating point expressions, this
3737 can be accessed as \texttt{deg}.
3738 \end{variable}
3739
3740 \begin{variable}{\lTmpaFp,\lTmpbFp,\lTmpcFp,\lTmpiFp,\lTmpjFp,\lTmpkFp}
3741 Scratch floating point numbers for local assignment. These are never used by
3742 the \verb!functional! package, and so are safe for use with any
3743 function. However, they may be overwritten by other
3744 code and so should only be used for short-term storage.
3745 \end{variable}
3746
3747 \begin{variable}{\gTmpaFp,\gTmpbFp,\gTmpcFp,\gTmpiFp,\gTmpjFp,\gTmpkFp}
3748 Scratch floating point numbers for global assignment. These are never used by
3749 the \verb!functional! package, and so are safe for use with any
3750 function. However, they may be overwritten by other
3751 code and so should only be used for short-term storage.
3752 \end{variable}
3753
3754 \section{The Syntax of Floating Point Expressions}
3755
3756 A decimal floating point number is one which is stored as a significand and a
3757 separate exponent. The module implements expandably a wide set of
3758 arithmetic, trigonometric, and other operations on decimal floating point
3759 numbers, to be used within floating point expressions. Floating point
3760 expressions support the following operations with their usual
3761 precedence.
3762 \begin{itemize}
3763 \item Basic arithmetic: addition $x+y$, subtraction $x-y$,
3764 multiplication $x*y$, division $x/y$, square root $\sqrt{x}$,
3765 and parentheses.
3766 \item Comparison operators: $x\mathop{\mathtt{<}}y$,
3767 $x\mathop{\mathtt{<=}}y$, $x\mathop{\mathtt{>?}}y$,
3768 $x\mathop{\mathtt{!=}}y$ \emph{etc.}
3769 \item Boolean logic: sign $\operatorname{sign} x$,
3770 negation $\mathop{!}x$, conjunction
3771 $x\mathop{\&\&}y$, disjunction $x\mathop{\vert\vert}y$, ternary
3772 operator $x\mathop{?}y\mathop{:}z$.
3773 \item Exponentials: $\exp x$, $\ln x$, $x^y$, $\operatorname{logb} x$.
3774 \item Integer factorial: $\operatorname{fact} x$.
3775 \item Trigonometry: $\sin x$, $\cos x$, $\tan x$, $\cot x$, $\sec
3776 x$, $\csc x$ expecting their arguments in radians, and
3777 $\operatorname{sind} x$, $\operatorname{cosd} x$,
3778 $\operatorname{tand} x$, $\operatorname{cotd} x$,
3779 $\operatorname{secd} x$, $\operatorname{cscd} x$ expecting their
3780 arguments in degrees.
3781 \item Inverse trigonometric functions: $\operatorname{asin} x$,
3782 $\operatorname{acos} x$, $\operatorname{atan} x$,
3783 $\operatorname{acot} x$, $\operatorname{asec} x$,
3784 $\operatorname{acsc} x$ giving a result in radians, and
3785 $\operatorname{asind} x$, $\operatorname{acosd} x$,
3786 $\operatorname{atand} x$, $\operatorname{acotd} x$,
3787 $\operatorname{asecd} x$, $\operatorname{acscd} x$ giving a result
3788 in degrees.
3789 %\item [\emph{(not yet)}] Hyperbolic functions and their inverse
3790 %functions: $\sinh x$, $\cosh x$, $\tanh x$, $\coth x$,
3791 %$\operatorname{sech} x$, $\operatorname{csch}$, and
3792 %$\operatorname{asinh} x$, $\operatorname{acosh} x$,
3793 %$\operatorname{atanh} x$, $\operatorname{acoth} x$,
3794 %$\operatorname{asech} x$, $\operatorname{acsch} x$.
3795 \item Extrema: $\max(x_{1},x_{2},\ldots)$, $\min(x_{1},x_{2},\ldots)$,
3796 $\operatorname{abs}(x)$.
3797 \item Rounding functions, controlled by two optional
3798 values, $n$ (number of places, $0$ by default) and
3799 $t$ (behavior on a tie, $\nan$ by default):
3800 \begin{itemize}
3801 \item $\operatorname{trunc}(x,n)$ rounds towards zero,
3802 \item $\operatorname{floor}(x,n)$ rounds towards $-\infty$,
3803 \item $\operatorname{ceil}(x,n)$ rounds towards $+\infty$,
3804 \item $\operatorname{round}(x,n,t)$ rounds to the closest value, with
3805 ties rounded to an even value by default, towards zero if $t=0$,
3806 towards $+\infty$ if $t>0$ and towards $-\infty$ if $t<0$.
3807 \end{itemize}
3808 %And \emph{(not yet)} modulo, and \enquote{quantize}.
3809 \item Random numbers: $\mathop{rand}()$, $\mathop{randint}(m,n)$.
3810 \item Constants: \texttt{pi}, \texttt{deg} (one degree in radians).
3811 \item Dimensions, automatically expressed in points, \emph{e.g.},
3812 \texttt{pc} is $12$.
3813 \item Automatic conversion (no need for \cs{intUse}, etc) of
3814 integer, dimension, and skip variables to floating point numbers,
3815 expressing dimensions in points and ignoring the stretch and
3816 shrink components of skips.
3817 \item Tuples: $(x_1,\ldots{},x_n)$ that can be stored in variables,
3818 added together, multiplied or divided by a floating point number,
3819 and nested.
3820 \end{itemize}
3821 Floating point numbers can be given either explicitly (in a form such
3822 as \verb|1.234e-34|, or \verb|-.0001|), or as a stored floating point variable,
3823 which is automatically replaced by its current value.
3824 A \enquote{floating point} is a floating point number or a tuple thereof.
3825 %See section \ref{sec:l3fp:fp-floats} for a description of what a floating point is,
3826 %section \ref{sec:l3fp:fp-precedence} for details about how an expression is
3827 %parsed, and section \ref{sec:l3fp:fp-operations} to know what the various
3828 %operations do. Some operations may raise exceptions (error messages),
3829 %described in section \ref{sec:l3fp:fp-exceptions}.
3830
3831 An example of use could be the following.
3832 \begin{demohigh}
3833 \LaTeX{} can now compute: $ \frac{\sin(3.5)}{2} + 2\cdot 10^{-3}
3834 = \fpEval {sin(3.5)/2 + 2e-3} $.
3835 \end{demohigh}
3836
3837 The operation \texttt{round} can be used to limit the result's
3838 precision. Adding $+0$ avoids the possibly undesirable output \verb|-0|,
3839 replacing it by \verb|+0|.
3840
3841 %However, the \pkg{l3fp} module is mostly meant
3842 %as an underlying tool for higher-level commands. For example, one
3843 %could provide a function to typeset nicely the result of floating
3844 %point computations.
3845 %\begin{verbatim}
3846 %\documentclass{article}
3847 %\usepackage{xparse, siunitx}
3848 %\ExplSyntaxOn
3849 %\NewDocumentCommand { \calcnum } { m }
3850 %{ \num { \fp_to_scientific:n {#1} } }
3851 %\ExplSyntaxOff
3852 %\begin{document}
3853 %\calcnum { 2 pi * sin ( 2.3 ^ 5 ) }
3854 %\end{document}
3855 %\end{verbatim}
3856 %See the documentation of \pkg{siunitx} for various options of
3857 %\cs{num}.
3858
3859 \section{Using Floating Point Expressions}
3860
3861 \begin{function}{\fpEval}
3862 \begin{syntax}
3863 \cs{fpEval} \Arg{floating point expression}
3864 \end{syntax}
3865 Evaluates the \meta{floating point expression} and returns the
3866 result as a decimal number with no
3867 exponent. Leading or trailing zeros may be inserted to compensate
3868 for the exponent. Non-significant trailing zeros are trimmed, and
3869 integers are expressed without a decimal separator. The values
3870 $\pm\infty$ and \nan{} trigger an \enquote{invalid operation}
3871 exception.
3872 For a tuple, each item is converted using \cs{fpEval} and they are combined as
3873 \verb|(|\meta{fp_1}\verb*|, |\meta{fp_2}\verb*|, |\ldots{}\meta{fp_n}\verb|)|
3874 if $n>1$ and \verb|(|\meta{fp_1}\verb|,)| or \verb|()| for fewer items.
3875 %This function is identical to \cs{fp_to_decimal:n}.
3876 For example
3877 \begin{demohigh}
3878 \fpEval {(1.2+3.4)*(5.6-7.8)/9}
3879 \end{demohigh}
3880 \end{function}
3881
3882 \begin{function}{\fpMathAdd}
3883 \begin{syntax}
3884 \cs{fpMathAdd} \Arg{fpexpr_1} \Arg{fpexpr_2}
3885 \end{syntax}
3886 Adds \Arg{fpexpr_1} and \Arg{fpexpr_2},
3887 and returns the result. For example
3888 \begin{demohigh}
3889 \fpMathAdd {2.8} {3.7}
3890 \fpMathAdd {3.8-1} {2.7+1}
3891 \end{demohigh}
3892 \end{function}
3893
3894 \begin{function}{\fpMathSub}
3895 \begin{syntax}
3896 \cs{fpMathSub} \Arg{fpexpr_1} \Arg{fpexpr_2}
3897 \end{syntax}
3898 Subtracts \Arg{fpexpr_2} from \Arg{fpexpr_1},
3899 and returns the result. For example
3900 \begin{demohigh}
3901 \fpMathSub {2.8} {3.7}
3902 \fpMathSub {3.8-1} {2.7+1}
3903 \end{demohigh}
3904 \end{function}
3905
3906 \begin{function}{\fpMathMult}
3907 \begin{syntax}
3908 \cs{fpMathMult} \Arg{fpexpr_1} \Arg{fpexpr_2}
3909 \end{syntax}
3910 Multiplies \Arg{fpexpr_1} by \Arg{fpexpr_2},
3911 and returns the result. For example
3912 \begin{demohigh}
3913 \fpMathMult {2.8} {3.7}
3914 \fpMathMult {3.8-1} {2.7+1}
3915 \end{demohigh}
3916 \end{function}
3917
3918 \begin{function}{\fpMathDiv}
3919 \begin{syntax}
3920 \cs{fpMathDiv} \Arg{fpexpr_1} \Arg{fpexpr_2}
3921 \end{syntax}
3922 Divides \Arg{fpexpr_1} by \Arg{fpexpr_2},
3923 and returns the result. For example
3924 \begin{demohigh}
3925 \fpMathDiv {2.8} {3.7}
3926 \fpMathDiv {3.8-1} {2.7+1}
3927 \end{demohigh}
3928 \end{function}
3929
3930 \begin{function}{\fpMathSign}
3931 \begin{syntax}
3932 \cs{fpMathSign} \Arg{fpexpr}
3933 \end{syntax}
3934 Evaluates the \meta{fpexpr} and returns the value
3935 using \cs{fpEval}\verb|{sign(|\meta{result}\verb|)}|: $+1$ for positive
3936 numbers and for $+\infty$, $-1$ for negative numbers and for
3937 $-\infty$, $\pm 0$ for $\pm 0$. If the operand is a tuple or is
3938 \nan{}, then \enquote{invalid operation} occurs and the result
3939 is $0$. For example
3940 \begin{demohigh}
3941 \fpMathSign {3.5}
3942 \fpMathSign {-2.7}
3943 \end{demohigh}
3944 \end{function}
3945
3946 \begin{function}{\fpMathAbs}
3947 \begin{syntax}
3948 \cs{fpMathAbs} \Arg{floating point expression}
3949 \end{syntax}
3950 Evaluates the \meta{floating point expression} as described for
3951 \cs{fpEval} and returns the absolute value.
3952 If the argument is $\pm\infty$, \nan{} or a tuple,
3953 \enquote{invalid operation} occurs. Within floating point
3954 expressions, \verb|abs()| can be used; it accepts $\pm\infty$ and \nan{}
3955 as arguments.
3956 \end{function}
3957
3958 \begin{function}{\fpMathMax,\fpMathMin}
3959 \begin{syntax}
3960 \cs{fpMathMax} \Arg{fp expression_1} \Arg{fp expression_2}
3961 \cs{fpMathMin} \Arg{fp expression_1} \Arg{fp expression_2}
3962 \end{syntax}
3963 Evaluates the \meta{floating point expressions} as described for \cs{fpEval}
3964 and returns the resulting larger (\texttt{max}) or smaller (\texttt{min}) value.
3965 If the argument is a tuple, \enquote{invalid operation} occurs,
3966 but no other case raises exceptions. Within floating point expressions,
3967 \verb|max()| and \verb|min()| can be used.
3968 \end{function}
3969
3970 \section{Creating and Using Floating Points}
3971
3972 \begin{function}{\fpNew}
3973 \begin{syntax}
3974 \cs{fpNew} \meta{fp var}
3975 \end{syntax}
3976 Creates a new \meta{fp var} or raises an error if the name is
3977 already taken. The declaration is global. The \meta{fp~var} is
3978 initially $+0$.
3979 \end{function}
3980
3981 \begin{function}{\fpConst}
3982 \begin{syntax}
3983 \cs{fpConst} \meta{fp var} \Arg{floating point expression}
3984 \end{syntax}
3985 Creates a new constant \meta{fp var} or raises an error if the name
3986 is already taken. The \meta{fp var} is set globally equal to
3987 the result of evaluating the \meta{floating point expression}.
3988 For example
3989 \begin{demohigh}
3990 \fpConst \cMyPiFp {3.1415926}
3991 \fpUse \cMyPiFp
3992 \end{demohigh}
3993 \end{function}
3994
3995 \begin{function}{\fpUse}
3996 \begin{syntax}
3997 \cs{fpUse} \meta{fp var}
3998 \end{syntax}
3999 Recovers the value of the \meta{fp var} and returns the value as a
4000 decimal number with no exponent.
4001 %Leading or trailing zeros may be inserted to compensate for the
4002 %exponent. Non-significant trailing zeros are trimmed. Integers are
4003 %expressed without a decimal separator. The values $\pm\infty$
4004 %and \nan{} trigger an \enquote{invalid operation} exception.
4005 %For a tuple, each item is converted using \cs{fp_to_decimal:n} and they are combined as
4006 %|(|\meta{fp_1}\verb*|, |\meta{fp_2}\verb*|, |\ldots{}\meta{fp_n}|)|
4007 %if $n>1$ and |(|\meta{fp_1}|,)| or |()| for fewer items.
4008 %This function is identical to \cs{fp_to_decimal:N}.
4009 \end{function}
4010
4011 \section{Viewing Floating Points}
4012
4013 \begin{function}{\fpLog}
4014 \begin{syntax}
4015 \cs{fpLog} \Arg{floating point expression}
4016 \end{syntax}
4017 Evaluates the \meta{floating point expression} and writes the
4018 result in the log file.
4019 \end{function}
4020
4021 \begin{function}{\fpVarLog}
4022 \begin{syntax}
4023 \cs{fpVarLog} \meta{fp var}
4024 \end{syntax}
4025 Writes the value of \meta{fp var} in the log file.
4026 \end{function}
4027
4028 \begin{function}{\fpShow}
4029 \begin{syntax}
4030 \cs{fpShow} \Arg{floating point expression}
4031 \end{syntax}
4032 Evaluates the \meta{floating point expression} and displays the
4033 result in the terminal.
4034 \end{function}
4035
4036 \begin{function}{\fpVarShow}
4037 \begin{syntax}
4038 \cs{fpVarShow} \meta{fp var}
4039 \end{syntax}
4040 Displays the value of \meta{fp var} in the terminal.
4041 \end{function}
4042
4043 \section{Setting Floating Point Variables}
4044
4045 \begin{function}{\fpSet}
4046 \begin{syntax}
4047 \cs{fpSet} \meta{fp var} \Arg{floating point expression}
4048 \end{syntax}
4049 Sets \meta{fp var} equal to the result of computing the
4050 \meta{floating point expression}. For example
4051 \begin{demohigh}
4052 \fpSet \lTmpaFp {4/7}
4053 \fpUse \lTmpaFp
4054 \end{demohigh}
4055 \end{function}
4056
4057 \begin{function}{\fpSetEq}
4058 \begin{syntax}
4059 \cs{fpSetEq} \meta{fp var_1} \meta{fp var_2}
4060 \end{syntax}
4061 Sets the floating point variable \meta{fp var_1} equal to the current
4062 value of \meta{fp var_2}.
4063 \end{function}
4064
4065 \begin{function}{\fpZero}
4066 \begin{syntax}
4067 \cs{fpZero} \meta{fp var}
4068 \end{syntax}
4069 Sets the \meta{fp var} to $+0$. For example
4070 \begin{demohigh}
4071 \fpSet \lTmpaFp {5.3}
4072 \fpZero \lTmpaFp
4073 \fpUse \lTmpaFp
4074 \end{demohigh}
4075 \end{function}
4076
4077 \begin{function}{\fpZeroNew}
4078 \begin{syntax}
4079 \cs{fpZeroNew} \meta{fp var}
4080 \end{syntax}
4081 Ensures that the \meta{fp var} exists globally
4082 by applying \cs{fpNew} if necessary, then applies
4083 \cs{fpZero} to leave the \meta{fp var} set to $+0$.
4084 \end{function}
4085
4086 \begin{function}{\fpAdd}
4087 \begin{syntax}
4088 \cs{fpAdd} \meta{fp var} \Arg{floating point expression}
4089 \end{syntax}
4090 Adds the result of computing the \meta{floating point expression} to
4091 the \meta{fp var}.
4092 This also applies if \meta{fp var} and \meta{floating point
4093 expression} evaluate to tuples of the same size. For example
4094 \begin{demohigh}
4095 \fpSet \lTmpaFp {5.3}
4096 \fpAdd \lTmpaFp {2.11}
4097 \fpUse \lTmpaFp
4098 \end{demohigh}
4099 \end{function}
4100
4101 \begin{function}{\fpSub}
4102 \begin{syntax}
4103 \cs{fpSub} \meta{fp var} \Arg{floating point expression}
4104 \end{syntax}
4105 Subtracts the result of computing the \meta{floating point
4106 expression} from the \meta{fp var}.
4107 This also applies if \meta{fp var} and \meta{floating point
4108 expression} evaluate to tuples of the same size. For example
4109 \begin{demohigh}
4110 \fpSet \lTmpaFp {5.3}
4111 \fpSub \lTmpaFp {2.11}
4112 \fpUse \lTmpaFp
4113 \end{demohigh}
4114 \end{function}
4115
4116 \section{Floating Point Step Functions}
4117
4118 %\begin{function}{\fpStepFunction}
4119 %\begin{syntax}
4120 %\cs{fpStepFunction} \Arg{initial value} \Arg{step} \Arg{final value} \meta{function}
4121 %\end{syntax}
4122 %This function first evaluates the \meta{initial value}, \meta{step}
4123 %and \meta{final value}, each of which should be a floating point
4124 %expression evaluating to a floating point number, not a tuple.
4125 %The \meta{function} is then placed in front of each \meta{value}
4126 %from the \meta{initial value} to the \meta{final value} in turn
4127 %(using \meta{step} between each \meta{value}). The \meta{step} must
4128 %be non-zero. If the \meta{step} is positive, the loop stops when
4129 %the \meta{value} becomes larger than the \meta{final value}. If the
4130 %\meta{step} is negative, the loop stops when the \meta{value}
4131 %becomes smaller than the \meta{final value}. The \meta{function}
4132 %should absorb one numerical argument. For example
4133 %\begin{verbatim}
4134 %\cs_set:Npn \my_func:n #1 { [I saw #1] \quad }
4135 %\fp_step_function:nnnN { 1.0 } { 0.1 } { 1.5 } \my_func:n
4136 %\end{verbatim}
4137 %would print
4138 %\begin{quote}
4139 %[I saw 1.0] \quad
4140 %[I saw 1.1] \quad
4141 %[I saw 1.2] \quad
4142 %[I saw 1.3] \quad
4143 %[I saw 1.4] \quad
4144 %[I saw 1.5] \quad
4145 %\end{quote}
4146 %\begin{texnote}
4147 %Due to rounding, it may happen that adding the \meta{step} to the
4148 %\meta{value} does not change the \meta{value}; such cases give an
4149 %error, as they would otherwise lead to an infinite loop.
4150 %\end{texnote}
4151 %\end{function}
4152
4153 \begin{function}{\fpStepInline}
4154 \begin{syntax}
4155 \cs{fpStepInline} \Arg{initial value} \Arg{step} \Arg{final value} \Arg{code}
4156 \end{syntax}
4157 This function first evaluates the \meta{initial value}, \meta{step}
4158 and \meta{final value}, all of which should be floating point
4159 expressions evaluating to a floating point number, not a tuple.
4160 Then for each \meta{value} from the \meta{initial value} to the
4161 \meta{final value} in turn (using \meta{step} between each
4162 \meta{value}), the \meta{code} is inserted into the input stream
4163 with \verb|#1| replaced by the current \meta{value}. Thus the
4164 \meta{code} should define a function of one argument (\verb|#1|).
4165 \begin{demohigh}
4166 \IgnoreSpacesOn
4167 \tlClear \lTmpaTl
4168 \fpStepInline {1} {0.1} {1.5} {
4169 \tlPutRight \lTmpaTl {[#1]}
4170 }
4171 \tlUse \lTmpaTl
4172 \IgnoreSpacesOff
4173 \end{demohigh}
4174 \end{function}
4175
4176 \begin{function}{\fpStepVariable}
4177 \begin{syntax}
4178 \cs{fpStepVariable} \Arg{initial value} \Arg{step} \Arg{final value} \meta{tl var} \Arg{code}
4179 \end{syntax}
4180 This function first evaluates the \meta{initial value}, \meta{step}
4181 and \meta{final value}, all of which should be floating point
4182 expressions evaluating to a floating point number, not a tuple.
4183 Then for each \meta{value} from the \meta{initial value} to the
4184 \meta{final value} in turn (using \meta{step} between each
4185 \meta{value}), the \meta{code} is inserted into the input stream,
4186 with the \meta{tl var} defined as the current \meta{value}. Thus
4187 the \meta{code} should make use of the \meta{tl var}.
4188 %For example
4189 %\begin{demohigh}
4190 %\IgnoreSpacesOn
4191 %\tlClear\lTmpaTl
4192 %\fpStepVariable{1}{0.1}{1.5}\lTmpiTl{
4193 % \tlPutRight\lTmpaTl{\expValue\lTmpiTl}
4194 % \tlPutRight\lTmpaTl{~}
4195 %}
4196 %\prgReturn{\expValue\lTmpaTl}
4197 %\IgnoreSpacesOff
4198 %\end{demohigh}
4199 \end{function}
4200
4201 \section{Float Point Conditionals}
4202
4203 \begin{function}{\fpIfExist,\fpIfExistT,\fpIfExistF,\fpIfExistTF}
4204 \begin{syntax}
4205 \cs{fpIfExist} \meta{fp var}
4206 \cs{fpIfExistT} \meta{fp var} \Arg{true code}
4207 \cs{fpIfExistF} \meta{fp var} \Arg{false code}
4208 \cs{fpIfExistTF} \meta{fp var} \Arg{true code} \Arg{false code}
4209 \end{syntax}
4210 Tests whether the \meta{fp var} is currently defined. This does not
4211 check that the \meta{fp var} really is a floating point variable.
4212 For example
4213 \begin{demohigh}
4214 \fpIfExistTF \lTmpaFp {\prgReturn{Yes}} {\prgReturn{No}}
4215 \fpIfExistTF \lMyUndefinedFp {\prgReturn{Yes}} {\prgReturn{No}}
4216 \end{demohigh}
4217 \end{function}
4218
4219 \begin{function}{\fpCompare,\fpCompareT,\fpCompareF,\fpCompareTF}
4220 \begin{syntax}
4221 \cs{fpCompare} \Arg{fpexpr_1} \meta{relation} \Arg{fpexpr_2}
4222 \cs{fpCompareT} \Arg{fpexpr_1} \meta{relation} \Arg{fpexpr_2} \Arg{true code}
4223 \cs{fpCompareF} \Arg{fpexpr_1} \meta{relation} \Arg{fpexpr_2} \Arg{false code}
4224 \cs{fpCompareTF} \Arg{fpexpr_1} \meta{relation} \Arg{fpexpr_2} \Arg{true code} \Arg{false code}
4225 \end{syntax}
4226 Compares the \meta{fpexpr_1} and the \meta{fpexpr_2}, and returns
4227 \texttt{true} if the \meta{relation} is obeyed. For example
4228 \begin{demohigh}
4229 \fpCompareTF {1} > {0.9999} {\prgReturn{Greater}} {\prgReturn{Less}}
4230 \fpCompareTF {1} > {1.0001} {\prgReturn{Greater}} {\prgReturn{Less}}
4231 \end{demohigh}
4232 Two floating points
4233 $x$ and $y$ may obey four mutually exclusive relations:
4234 $x<y$, $x=y$, $x>y$, or $x?y$ (\enquote{not ordered}). The last
4235 case occurs exactly if one or both operands is \nan{} or is a tuple,
4236 unless they are equal tuples. Note that a \nan{} is distinct from
4237 any value, even another \nan{}, hence $x=x$ is not true for
4238 a \nan{}. To test if a value is \nan{}, compare it to an arbitrary
4239 number with the \enquote{not ordered} relation.\par
4240 %\begin{demohigh}
4241 %\fpCompareTF{0/0}?{0}{\prgReturn{Is~a~Nan}}{\prgReturn{Isn't~a~NaN}}
4242 %\end{demohigh}
4243 Tuples are equal if they have the same number of items and items
4244 compare equal (in particular there must be no \nan{}).
4245 At present any other comparison with tuples yields \verb|?| (not ordered).
4246 This is experimental.
4247 \end{function}
4248
4249 \chapter{Dimensions (\texttt{Dim})}
4250
4251 \section{Constant and Scratch Dimensions}
4252
4253 \begin{variable}{\cMaxDim}
4254 The maximum value that can be stored as a dimension. This can also
4255 be used as a component of a skip.
4256 \end{variable}
4257
4258 \begin{variable}{\cZeroDim}
4259 A zero length as a dimension. This can also be used as a component
4260 of a skip.
4261 \end{variable}
4262
4263 \begin{variable}{\lTmpaDim,\lTmpbDim,\lTmpcDim,\lTmpiDim,\lTmpjDim,\lTmpkDim}
4264 Scratch dimensions for local assignment. These are never used by
4265 the \verb!functional! package, and so are safe for use with any
4266 function. However, they may be overwritten by other
4267 code and so should only be used for short-term storage.
4268 \end{variable}
4269
4270 \begin{variable}{\gTmpaDim,\gTmpbDim,\gTmpcDim,\gTmpiDim,\gTmpjDim,\gTmpkDim}
4271 Scratch dimensions for global assignment. These are never used by
4272 the \verb!functional! package, and so are safe for use with any
4273 function. However, they may be overwritten by other
4274 code and so should only be used for short-term storage.
4275 \end{variable}
4276
4277 \section{Dimension Expressions}
4278
4279 \begin{function}{\dimEval}
4280 \begin{syntax}
4281 \cs{dimEval} \Arg{dimension expression}
4282 \end{syntax}
4283 Evaluates the \meta{dimension expression}, expanding any
4284 dimensions and token list variables within the \meta{expression}
4285 to their content (without requiring \cs{dimUse}/\cs{tlUse})
4286 and applying the standard mathematical rules. The result of the
4287 calculation is returned as a \meta{dimension denotation}.
4288 %This is expressed in points (\texttt{pt}), and requires suitable
4289 %termination if used in a \TeX{}-style assignment as it is \emph{not}
4290 %an \meta{internal dimension}.
4291 For example
4292 \begin{demohigh}
4293 \dimEval {(1.2pt+3.4pt)/9}
4294 \end{demohigh}
4295 %\dimEval{(1.2pt+3.4pt)*(5.6-7.8)/9}
4296 \end{function}
4297
4298 \begin{function}{\dimMathAdd}
4299 \begin{syntax}
4300 \cs{dimMathAdd} \Arg{dimexpr_1} \Arg{dimexpr_2}
4301 \end{syntax}
4302 Adds \Arg{dimexpr_1} and \Arg{dimexpr_2},
4303 and returns the result. For example
4304 \begin{demohigh}
4305 \dimMathAdd {2.8pt} {3.7pt}
4306 \dimMathAdd {3.8pt-1pt} {2.7pt+1pt}
4307 \end{demohigh}
4308 \end{function}
4309
4310 \begin{function}{\dimMathSub}
4311 \begin{syntax}
4312 \cs{dimMathSub} \Arg{dimexpr_1} \Arg{dimexpr_2}
4313 \end{syntax}
4314 Subtracts \Arg{dimexpr_2} from \Arg{dimexpr_1},
4315 and returns the result. For example
4316 \begin{demohigh}
4317 \dimMathSub {2.8pt} {3.7pt}
4318 \dimMathSub {3.8pt-1pt} {2.7pt+1pt}
4319 \end{demohigh}
4320 \end{function}
4321
4322 \begin{function}{\dimMathRatio}
4323 \begin{syntax}
4324 \cs{dimMathRatio} \Arg{dimexpr_1} \Arg{dimexpr_2}
4325 \end{syntax}
4326 Parses the two \meta{dimension expressions},
4327 then calculates the ratio of the two and returns it.
4328 The result is a ratio expression
4329 between two integers, with all distances converted to scaled points.
4330 For example
4331 \begin{demohigh}
4332 \dimMathRatio {5pt} {10pt}
4333 \end{demohigh}
4334 The returned value is suitable for use inside a \meta{dimension expression}
4335 such as
4336 \begin{codehigh}
4337 \dimSet \lTmpaDim {10pt*\dimMathRatio{5pt}{10pt}}
4338 \end{codehigh}
4339 \end{function}
4340
4341 \begin{function}{\dimMathSign}
4342 \begin{syntax}
4343 \cs{dimMathSign} \Arg{dimexpr}
4344 \end{syntax}
4345 Evaluates the \meta{dimexpr} then returns $1$ or $0$ or $-1$
4346 according to the sign of the result. For example
4347 \begin{demohigh}
4348 \dimMathSign {3.5pt}
4349 \dimMathSign {-2.7pt}
4350 \end{demohigh}
4351 \end{function}
4352
4353 \begin{function}{\dimMathAbs}
4354 \begin{syntax}
4355 \cs{dimMathAbs} \Arg{dimexpr}
4356 \end{syntax}
4357 Converts the \meta{dimexpr} to its absolute value,
4358 returning the result as a \meta{dimension denotation}.
4359 For example
4360 \begin{demohigh}
4361 \dimMathAbs {3.5pt}
4362 \dimMathAbs {-2.7pt}
4363 \end{demohigh}
4364 \end{function}
4365
4366 \begin{function}{\dimMathMax,\dimMathMin}
4367 \begin{syntax}
4368 \cs{dimMathMax} \Arg{dimexpr_1} \Arg{dimexpr_2}
4369 \cs{dimMathMin} \Arg{dimexpr_1} \Arg{dimexpr_2}
4370 \end{syntax}
4371 Evaluates the two \meta{dimension expressions} and returns either the
4372 maximum or minimum value as appropriate as a \meta{dimension denotation}.
4373 For example
4374 \begin{demohigh}
4375 \dimMathMax {3.5pt} {-2.7pt}
4376 \dimMathMin {3.5pt} {-2.7pt}
4377 \end{demohigh}
4378 \end{function}
4379
4380 \section{Creating and Using Dimensions}
4381
4382 \begin{function}{\dimNew}
4383 \begin{syntax}
4384 \cs{dimNew} \meta{dimension}
4385 \end{syntax}
4386 Creates a new \meta{dimension} or raises an error if the name is
4387 already taken. The declaration is global. The \meta{dimension}
4388 is initially equal to $0$\,pt.
4389 \end{function}
4390
4391 \begin{function}{\dimConst}
4392 \begin{syntax}
4393 \cs{dimConst} \meta{dimension} \Arg{dimension expression}
4394 \end{syntax}
4395 Creates a new constant \meta{dimension} or raises an error if the
4396 name is already taken. The value of the \meta{dimension} is set
4397 globally to the \meta{dimension expression}. For example
4398 \begin{demohigh}
4399 \dimConst \cFooSomeDim {1cm}
4400 \dimUse \cFooSomeDim
4401 \end{demohigh}
4402 \end{function}
4403
4404 \begin{function}{\dimUse}
4405 \begin{syntax}
4406 \cs{dimUse} \meta{dimension}
4407 \end{syntax}
4408 Recovers the content of a \meta{dimension} and returns the value.
4409 An error is raised if the variable does not exist or if it is invalid.
4410 \end{function}
4411
4412 \section{Viewing Dimensions}
4413
4414 \begin{function}{\dimLog}
4415 \begin{syntax}
4416 \cs{dimLog} \Arg{dimension expression}
4417 \end{syntax}
4418 Writes the result of evaluating the \meta{dimension expression}
4419 in the log file. For example
4420 \begin{codehigh}
4421 \dimLog {\lFooSomeDim+1cm}
4422 \end{codehigh}
4423 \end{function}
4424
4425 \begin{function}{\dimVarLog}
4426 \begin{syntax}
4427 \cs{dimVarLog} \meta{dimension}
4428 \end{syntax}
4429 Writes the value of the \meta{dimension} in the log file. For example
4430 \begin{codehigh}
4431 \dimVarLog \lFooSomeDim
4432 \end{codehigh}
4433 \end{function}
4434
4435 \begin{function}{\dimShow}
4436 \begin{syntax}
4437 \cs{dimShow} \Arg{dimension expression}
4438 \end{syntax}
4439 Displays the result of evaluating the \meta{dimension expression}
4440 on the terminal. For example
4441 \begin{codehigh}
4442 \dimShow {\lFooSomeDim+1cm}
4443 \end{codehigh}
4444 \end{function}
4445
4446 \begin{function}{\dimVarShow}
4447 \begin{syntax}
4448 \cs{dimVarShow} \meta{dimension}
4449 \end{syntax}
4450 Displays the value of the \meta{dimension} on the terminal. For example
4451 \begin{codehigh}
4452 \dimVarShow \lFooSomeDim
4453 \end{codehigh}
4454 \end{function}
4455
4456 \section{Setting Dimension Variables}
4457
4458 \begin{function}{\dimSet}
4459 \begin{syntax}
4460 \cs{dimSet} \meta{dimension} \Arg{dimension expression}
4461 \end{syntax}
4462 Sets \meta{dimension} to the value of \meta{dimension expression}, which
4463 must evaluate to a length with units.
4464 \end{function}
4465
4466 \begin{function}{\dimSetEq}
4467 \begin{syntax}
4468 \cs{dimSetEq} \meta{dimension_1} \meta{dimension_2}
4469 \end{syntax}
4470 Sets the content of \meta{dimension_1} equal to that of
4471 \meta{dimension_2}. For example
4472 \begin{demohigh}
4473 \dimSet \lTmpaDim {10pt}
4474 \dimSetEq \lTmpbDim \lTmpaDim
4475 \dimUse \lTmpbDim
4476 \end{demohigh}
4477 \end{function}
4478
4479 \begin{function}{\dimZero}
4480 \begin{syntax}
4481 \cs{dimZero} \meta{dimension}
4482 \end{syntax}
4483 Sets \meta{dimension} to $0$\,pt. For example
4484 \begin{demohigh}
4485 \dimSet \lTmpaDim {1em}
4486 \dimZero \lTmpaDim
4487 \dimUse \lTmpaDim
4488 \end{demohigh}
4489 \end{function}
4490
4491 \begin{function}{\dimZeroNew}
4492 \begin{syntax}
4493 \cs{dimZeroNew} \meta{dimension}
4494 \end{syntax}
4495 Ensures that the \meta{dimension} exists globally by applying
4496 \cs{dimNew} if necessary, then applies
4497 \cs{dimZero} to set the \meta{dimension} to zero. For example
4498 \begin{demohigh}
4499 \dimZeroNew \lFooSomeDim
4500 \dimUse \lFooSomeDim
4501 \end{demohigh}
4502 \end{function}
4503
4504 \begin{function}{\dimAdd}
4505 \begin{syntax}
4506 \cs{dimAdd} \meta{dimension} \Arg{dimension expression}
4507 \end{syntax}
4508 Adds the result of the \meta{dimension expression} to the current
4509 content of the \meta{dimension}. For example
4510 \begin{demohigh}
4511 \dimSet \lTmpaDim {5.3pt}
4512 \dimAdd \lTmpaDim {2.11pt}
4513 \dimUse \lTmpaDim
4514 \end{demohigh}
4515 \end{function}
4516
4517 \begin{function}{\dimSub}
4518 \begin{syntax}
4519 \cs{dimSub} \meta{dimension} \Arg{dimension expression}
4520 \end{syntax}
4521 Subtracts the result of the \meta{dimension expression} from the
4522 current content of the \meta{dimension}. For example
4523 \begin{demohigh}
4524 \dimSet \lTmpaDim {5.3pt}
4525 \dimSub \lTmpaDim {2.11pt}
4526 \dimUse \lTmpaDim
4527 \end{demohigh}
4528 \end{function}
4529
4530 \section{Dimension Step Functions}
4531
4532 %\begin{function}{\dimStepFunction}
4533 %\begin{syntax}
4534 %\cs{dimStepFunction} \Arg{initial value} \Arg{step} \Arg{final value} \meta{function}
4535 %\end{syntax}
4536 %This function first evaluates the \meta{initial value}, \meta{step}
4537 %and \meta{final value}, all of which should be dimension expressions.
4538 %The \meta{function} is then placed in front of each \meta{value}
4539 %from the \meta{initial value} to the \meta{final value} in turn
4540 %(using \meta{step} between each \meta{value}). The \meta{step} must
4541 %be non-zero. If the \meta{step} is positive, the loop stops when
4542 %the \meta{value} becomes larger than the \meta{final value}. If the
4543 %\meta{step} is negative, the loop stops when the \meta{value}
4544 %becomes smaller than the \meta{final value}. The \meta{function}
4545 %should absorb one argument.
4546 %\end{function}
4547
4548 \begin{function}{\dimStepInline}
4549 \begin{syntax}
4550 \cs{dimStepInline} \Arg{initial value} \Arg{step} \Arg{final value} \Arg{code}
4551 \end{syntax}
4552 This function first evaluates the \meta{initial value}, \meta{step}
4553 and \meta{final value}, all of which should be dimension expressions.
4554 Then for each \meta{value} from the \meta{initial value} to the
4555 \meta{final value} in turn (using \meta{step} between each
4556 \meta{value}), the \meta{code} is inserted into the input stream
4557 with \verb|#1| replaced by the current \meta{value}. Thus the
4558 \meta{code} should define a function of one argument (\verb|#1|).
4559 \begin{demohigh}
4560 \IgnoreSpacesOn
4561 \tlClear \lTmpaTl
4562 \dimStepInline {1pt} {0.1pt} {1.5pt} {
4563 \tlPutRight \lTmpaTl {[#1]}
4564 }
4565 \tlUse \lTmpaTl
4566 \IgnoreSpacesOff
4567 \end{demohigh}
4568 \end{function}
4569
4570 \begin{function}{\dimStepVariable}
4571 \begin{syntax}
4572 \cs{dimStepVariable} \Arg{initial value} \Arg{step} \Arg{final value} \meta{tl var} \Arg{code}
4573 \end{syntax}
4574 This function first evaluates the \meta{initial value}, \meta{step}
4575 and \meta{final value}, all of which should be dimension expressions.
4576 Then for each \meta{value} from the \meta{initial value} to the
4577 \meta{final value} in turn (using \meta{step} between each
4578 \meta{value}), the \meta{code} is inserted into the input stream,
4579 with the \meta{tl var} defined as the current \meta{value}. Thus
4580 the \meta{code} should make use of the \meta{tl var}.
4581 %For example
4582 %\begin{demohigh}
4583 %\IgnoreSpacesOn
4584 %\tlClear\lTmpaTl
4585 %\dimStepVariable{1pt}{0.1pt}{1.5pt}\lTmpiTl{
4586 % \tlPutRight\lTmpaTl{\expValue\lTmpiTl}
4587 % \tlPutRight\lTmpaTl{~}
4588 %}
4589 %\prgReturn{\expValue\lTmpaTl}
4590 %\IgnoreSpacesOff
4591 %\end{demohigh}
4592 \end{function}
4593
4594 \section{Dimension Conditionals}
4595
4596 \begin{function}{\dimIfExist,\dimIfExistT,\dimIfExistF,\dimIfExistTF}
4597 \begin{syntax}
4598 \cs{dimIfExist} \meta{dimension}
4599 \cs{dimIfExistT} \meta{dimension} \Arg{true code}
4600 \cs{dimIfExistF} \meta{dimension} \Arg{false code}
4601 \cs{dimIfExistTF} \meta{dimension} \Arg{true code} \Arg{false code}
4602 \end{syntax}
4603 Tests whether the \meta{dimension} is currently defined. This does
4604 not check that the \meta{dimension} really is a dimension variable.
4605 For example
4606 \begin{demohigh}
4607 \dimIfExistTF \lTmpaDim {\prgReturn{Yes}} {\prgReturn{No}}
4608 \dimIfExistTF \lFooUndefinedDim {\prgReturn{Yes}} {\prgReturn{No}}
4609 \end{demohigh}
4610 \end{function}
4611
4612 \begin{function}{\dimCompare,\dimCompareT,\dimCompareF,\dimCompareTF}
4613 \begin{syntax}
4614 \cs{dimCompare} \Arg{dimexpr_1} \meta{relation} \Arg{dimexpr_2}
4615 \cs{dimCompareT} \Arg{dimexpr_1} \meta{relation} \Arg{dimexpr_2} \Arg{true code}
4616 \cs{dimCompareF} \Arg{dimexpr_1} \meta{relation} \Arg{dimexpr_2} \Arg{false code}
4617 \cs{dimCompareTF} \Arg{dimexpr_1} \meta{relation} \Arg{dimexpr_2} \Arg{true code} \Arg{false code}
4618 \end{syntax}
4619 This function first evaluates each of the \meta{dimension expressions}
4620 as described for \cs{dimEval}. The two results are then
4621 compared using the \meta{relation}:\par
4622 {\centering
4623 \begin{tabular}{ll}
4624 Equal & \verb|=| \\
4625 Greater than & \verb|>| \\
4626 Less than & \verb|<| \\
4627 \end{tabular}\par}
4628 For example
4629 \begin{demohigh}
4630 \dimCompareTF {1pt} > {0.9999pt} {\prgReturn{Greater}} {\prgReturn{Less}}
4631 \dimCompareTF {1pt} > {1.0001pt} {\prgReturn{Greater}} {\prgReturn{Less}}
4632 \end{demohigh}
4633 \end{function}
4634
4635 \section{Dimension Case Functions}
4636
4637 \begin{function}{\dimCase}
4638 \begin{syntax}
4639 \cs{dimCase} \Arg{test dimension expression}
4640 ~ ~ \verb|{|
4641 ~ ~ ~ ~ \Arg{dimexpr case_1} \Arg{code case_1}
4642 ~ ~ ~ ~ \Arg{dimexpr case_2} \Arg{code case_2}
4643 ~ ~ ~ ~ \ldots
4644 ~ ~ ~ ~ \Arg{dimexpr case_n} \Arg{code case_n}
4645 ~ ~ \verb|}|
4646 \end{syntax}
4647 This function evaluates the \meta{test dimension expression} and
4648 compares this in turn to each of the
4649 \meta{dimension expression cases}. If the two are equal then the
4650 associated \meta{code} is left in the input stream
4651 and other cases are discarded.
4652 \end{function}
4653
4654 \begin{function}{\dimCaseT}
4655 \begin{syntax}
4656 \cs{dimCaseT} \Arg{test dimension expression}
4657 ~ ~ \verb|{|
4658 ~ ~ ~ ~ \Arg{dimexpr case_1} \Arg{code case_1}
4659 ~ ~ ~ ~ \Arg{dimexpr case_2} \Arg{code case_2}
4660 ~ ~ ~ ~ \ldots
4661 ~ ~ ~ ~ \Arg{dimexpr case_n} \Arg{code case_n}
4662 ~ ~ \verb|}|
4663 ~ ~ \Arg{true code}
4664 \end{syntax}
4665 This function evaluates the \meta{test dimension expression} and
4666 compares this in turn to each of the
4667 \meta{dimension expression cases}. If the two are equal then the
4668 associated \meta{code} is left in the input stream
4669 and other cases are discarded. If any of the
4670 cases are matched, the \meta{true code} is also inserted into the
4671 input stream (after the code for the appropriate case).
4672 \end{function}
4673
4674 \begin{function}{\dimCaseF}
4675 \begin{syntax}
4676 \cs{dimCaseF} \Arg{test dimension expression}
4677 ~ ~ \verb|{|
4678 ~ ~ ~ ~ \Arg{dimexpr case_1} \Arg{code case_1}
4679 ~ ~ ~ ~ \Arg{dimexpr case_2} \Arg{code case_2}
4680 ~ ~ ~ ~ \ldots
4681 ~ ~ ~ ~ \Arg{dimexpr case_n} \Arg{code case_n}
4682 ~ ~ \verb|}|
4683 ~ ~ \Arg{false code}
4684 \end{syntax}
4685 This function evaluates the \meta{test dimension expression} and
4686 compares this in turn to each of the
4687 \meta{dimension expression cases}. If the two are equal then the
4688 associated \meta{code} is left in the input stream
4689 and other cases are discarded. If none of the cases
4690 match then the \meta{false code} is inserted.
4691 For example
4692 \begin{demohigh}
4693 \IgnoreSpacesOn
4694 \dimSet \lTmpaDim {5pt}
4695 \dimCaseF {2\lTmpaDim} {
4696 {5pt} {\prgReturn{Small}}
4697 {4pt+6pt} {\prgReturn{Medium}}
4698 {-10pt} {\prgReturn{Negative}}
4699 }{
4700 \prgReturn {No Match}
4701 }
4702 \IgnoreSpacesOff
4703 \end{demohigh}
4704 \end{function}
4705
4706 \begin{function}{\dimCaseTF}
4707 \begin{syntax}
4708 \cs{dimCaseTF} \Arg{test dimension expression}
4709 ~ ~ \verb|{|
4710 ~ ~ ~ ~ \Arg{dimexpr case_1} \Arg{code case_1}
4711 ~ ~ ~ ~ \Arg{dimexpr case_2} \Arg{code case_2}
4712 ~ ~ ~ ~ \ldots
4713 ~ ~ ~ ~ \Arg{dimexpr case_n} \Arg{code case_n}
4714 ~ ~ \verb|}|
4715 ~ ~ \Arg{true code}
4716 ~ ~ \Arg{false code}
4717 \end{syntax}
4718 This function evaluates the \meta{test dimension expression} and
4719 compares this in turn to each of the
4720 \meta{dimension expression cases}. If the two are equal then the
4721 associated \meta{code} is left in the input stream
4722 and other cases are discarded. If any of the
4723 cases are matched, the \meta{true code} is also inserted into the
4724 input stream (after the code for the appropriate case), while if none
4725 match then the \meta{false code} is inserted.
4726 %For example
4727 %\begin{demohigh}
4728 %\IgnoreSpacesOn
4729 %\dimSet\lTmpaDim{5pt}
4730 %\dimCaseTF{2\lTmpaDim}{
4731 % {5pt} {\prgReturn{Small}}
4732 % {4pt+6pt} {\prgReturn{Medium}}
4733 % {-10pt} {\prgReturn{Negative}}
4734 %}{
4735 % \prgReturn{[Some Match]}
4736 %}{
4737 % \prgReturn{[No Match]}
4738 %}
4739 %\IgnoreSpacesOff
4740 %\end{demohigh}
4741 \end{function}
4742
4743 \chapter{Comma Separated Lists (\texttt{Clist})}
4744
4745 \section{Constant and Scratch Comma Lists}
4746
4747 \begin{variable}{\cEmptyClist}
4748 Constant that is always empty.
4749 \end{variable}
4750
4751 \begin{variable}{\lTmpaClist,\lTmpbClist,\lTmpcClist,\lTmpiClist,\lTmpjClist,\lTmpkClist}
4752 Scratch comma lists for local assignment. These are never used by
4753 the \verb!functional! package, and so are safe for use with any
4754 function. However, they may be overwritten by other
4755 code and so should only be used for short-term storage.
4756 \end{variable}
4757
4758 \begin{variable}{\gTmpaClist,\gTmpbClist,\gTmpcClist,\gTmpiClist,\gTmpjClist,\gTmpkClist}
4759 Scratch comma lists for global assignment. These are never used by
4760 the \verb!functional! package, and so are safe for use with any
4761 function. However, they may be overwritten by other
4762 code and so should only be used for short-term storage.
4763 \end{variable}
4764
4765 \section{Creating and Using Comma Lists}
4766
4767 \begin{function}{\clistNew}
4768 \begin{syntax}
4769 \cs{clistNew} \meta{comma list}
4770 \end{syntax}
4771 Creates a new \meta{comma list} or raises an error if the name is
4772 already taken. The declaration is global. The \meta{comma list}
4773 initially contains no items.
4774 \begin{codehigh}
4775 \clistNew \lFooSomeClist
4776 \end{codehigh}
4777 \end{function}
4778
4779 \begin{function}{\clistConst}
4780 \begin{syntax}
4781 \cs{clistConst} \meta{clist var} \Arg{comma list}
4782 \end{syntax}
4783 Creates a new constant \meta{clist var} or raises an error
4784 if the name is already taken. The value of the
4785 \meta{clist var} is set globally to the
4786 \meta{comma list}.
4787 \begin{codehigh}
4788 \clistConst \cFooSomeClist {one,two,three}
4789 \end{codehigh}
4790 \end{function}
4791
4792 \begin{function}{\clistVarJoin}
4793 \begin{syntax}
4794 \cs{clistVarJoin} \meta{clist var} \Arg{separator}
4795 \end{syntax}
4796 Returns the contents of the \meta{clist var},
4797 with the \meta{separator} between the items.
4798 %If the comma list has a single item, it is placed in the input stream,
4799 %and a comma list with no items produces no output.
4800 %An error is raised if the variable does not exist or if it is invalid.
4801 \begin{demohigh}
4802 \clistSet \lTmpaClist { a , b , , c , {de} , f }
4803 \clistVarJoin \lTmpaClist { and }
4804 \end{demohigh}
4805 %\begin{texnote}
4806 %The result is returned within the \tn{unexpanded}
4807 %primitive (\cs{exp_not:n}), which means that the \meta{items}
4808 %do not expand further when appearing in an \texttt{x}-type
4809 %or \texttt{e}-type argument expansion.
4810 %\end{texnote}
4811 \end{function}
4812
4813 \begin{function}{\clistVarJoinExtended}
4814 \begin{syntax}
4815 \cs{clistVarJoinExtended} \meta{clist var} \Arg{separator between two} \Arg{separator between more than two} \Arg{separator between final two}
4816 \end{syntax}
4817 Returns the contents of the \meta{clist var},
4818 with the appropriate \meta{separator} between the items. Namely, if
4819 the comma list has more than two items, the \meta{separator between
4820 more than two} is placed between each pair of items except the
4821 last, for which the \meta{separator between final two} is used. If
4822 the comma list has exactly two items, then they are joined with
4823 the \meta{separator between two} and returns.
4824 %If the comma list has a single item, it is placed in the input stream,
4825 %and a comma list with no items produces no output.
4826 %An error is raised if the variable does not exist or if it is invalid.
4827 \begin{demohigh}
4828 \clistSet \lTmpaClist { a , b }
4829 \clistVarJoinExtended \lTmpaClist { and } {, } {, and }
4830 \end{demohigh}
4831 \begin{demohigh}
4832 \clistSet \lTmpaClist { a , b , , c , {de} , f }
4833 \clistVarJoinExtended \lTmpaClist { and } {, } {, and }
4834 \end{demohigh}
4835 %\begin{texnote}
4836 %The result is returned within the \tn{unexpanded}
4837 %primitive (\cs{exp_not:n}), which means that the \meta{items}
4838 %do not expand further when appearing in an \texttt{x}-type
4839 %or \texttt{e}-type argument expansion.
4840 %\end{texnote}
4841 \end{function}
4842
4843 \begin{function}{\clistJoin,\clistJoinExtended}
4844 \begin{syntax}
4845 \cs{clistJoin} \meta{comma list} \Arg{separator}
4846 \cs{clistJoinExtended} \meta{comma list} \Arg{separator between two} \Arg{separator between more than two} \Arg{separator between final two}
4847 \end{syntax}
4848 Returns the contents of the \meta{comma list},
4849 with the appropriate \meta{separator} between the items. As for
4850 \cs{clistSet}, blank items are omitted, spaces are removed from
4851 both sides of each item, then a set of braces is removed if the
4852 resulting space-trimmed item is braced. The \meta{separators} are
4853 then inserted in the same way as for \cs{clistVarJoin} and
4854 \cs{clistVarJoinExtended}, respectively.
4855 \begin{demohigh}
4856 \clistJoinExtended { a , b } { and } {, } {, and }
4857 \end{demohigh}
4858 \begin{demohigh}
4859 \clistJoinExtended { a , b , , c , {de} , f } { and } {, } {, and }
4860 \end{demohigh}
4861 %\begin{texnote}
4862 %The result is returned within the \tn{unexpanded}
4863 %primitive (\cs{exp_not:n}), which means that the \meta{items}
4864 %do not expand further when appearing in an \texttt{x}-type
4865 %or \texttt{e}-type argument expansion.
4866 %\end{texnote}
4867 \end{function}
4868
4869 \section{Viewing Comma Lists}
4870
4871 \begin{function}{\clistLog}
4872 \begin{syntax}
4873 \cs{clistLog} \Arg{tokens}
4874 \end{syntax}
4875 Writes the entries in the comma list in the log file. See also
4876 \cs{clistShow} which displays the result in the terminal.
4877 \begin{codehigh}
4878 \clistLog {one,two,three}
4879 \end{codehigh}
4880 \end{function}
4881
4882 \begin{function}{\clistVarLog}
4883 \begin{syntax}
4884 \cs{clistVarLog} \meta{comma list}
4885 \end{syntax}
4886 Writes the entries in the \meta{comma list} in the log file. See
4887 also \cs{clistVarShow} which displays the result in the terminal.
4888 \begin{codehigh}
4889 \clistSet \lTmpaClist {one,two,three}
4890 \clistVarLog \lTmpaClist
4891 \end{codehigh}
4892 \end{function}
4893
4894 \begin{function}{\clistShow}
4895 \begin{syntax}
4896 \cs{clistShow} \Arg{tokens}
4897 \end{syntax}
4898 Displays the entries in the comma list in the terminal.
4899 \begin{codehigh}
4900 \clistShow {one,two,three}
4901 \end{codehigh}
4902 \end{function}
4903
4904 \begin{function}{\clistVarShow}
4905 \begin{syntax}
4906 \cs{clistVarShow} \meta{comma list}
4907 \end{syntax}
4908 Displays the entries in the \meta{comma list} in the terminal.
4909 \begin{codehigh}
4910 \clistSet \lTmpaClist {one,two,three}
4911 \clistVarShow \lTmpaClist
4912 \end{codehigh}
4913 \end{function}
4914
4915 \section{Setting Comma Lists}
4916
4917 \begin{function}{\clistSet}
4918 \begin{syntax}
4919 \cs{clistSet} \meta{comma list} \verb|{|\meta{item_1},\ldots{},\meta{item_n}\verb|}|
4920 \end{syntax}
4921 Sets \meta{comma list} to contain the \meta{items},
4922 removing any previous content from the variable.
4923 Blank items are omitted, spaces are removed from both sides of each
4924 item, then a set of braces is removed if the resulting space-trimmed
4925 item is braced.
4926 To store some \meta{tokens} as a single \meta{item} even if the
4927 \meta{tokens} contain commas or spaces, add a set of braces:
4928 \cs{clistSet} \meta{comma list} \verb|{| \Arg{tokens} \verb|}|.
4929 \begin{demohigh}
4930 \clistSet \lTmpaClist {one,two,three}
4931 \clistVarJoin \lTmpaClist { and }
4932 \end{demohigh}
4933 \end{function}
4934
4935 \begin{function}{\clistSetEq}
4936 \begin{syntax}
4937 \cs{clistSetEq} \meta{comma list_1} \meta{comma list_2}
4938 \end{syntax}
4939 Sets the content of \meta{comma list_1} equal to that of
4940 \meta{comma list_2}. To set a token list variable equal to a comma
4941 list variable, use \cs{tlSetEq}. Conversely, setting a comma
4942 list variable to a token list is unadvisable unless one checks
4943 space-trimming and related issues.
4944 \begin{demohigh}
4945 \clistSet \lTmpaClist {one,two,three,four}
4946 \clistSetEq \lTmpbClist \lTmpaClist
4947 \clistVarJoin \lTmpbClist { and }
4948 \end{demohigh}
4949 \end{function}
4950
4951 \begin{function}{\clistSetFromSeq}
4952 \begin{syntax}
4953 \cs{clistSetFromSeq} \meta{comma list} \meta{sequence}
4954 \end{syntax}
4955 Converts the data in the \meta{sequence} into a \meta{comma list}:
4956 the original \meta{sequence} is unchanged.
4957 Items which contain either spaces or commas are surrounded by braces.
4958 \begin{demohigh}
4959 \seqPutRight \lTmpaSeq {one}
4960 \seqPutRight \lTmpaSeq {two}
4961 \clistSetFromSeq \lTmpaClist \lTmpaSeq
4962 \clistVarJoin \lTmpaClist { and }
4963 \end{demohigh}
4964 \end{function}
4965
4966 \begin{function}{\clistClear}
4967 \begin{syntax}
4968 \cs{clistClear} \meta{comma list}
4969 \end{syntax}
4970 Clears all items from the \meta{comma list}.
4971 \begin{codehigh}
4972 \clistSet \lTmpaClist {one,two,three,four}
4973 \clistClear \lTmpaClist
4974 \end{codehigh}
4975 \end{function}
4976
4977 \begin{function}{\clistClearNew}
4978 \begin{syntax}
4979 \cs{clistClearNew} \meta{comma list}
4980 \end{syntax}
4981 Ensures that the \meta{comma list} exists globally by applying
4982 \cs{clistNew} if necessary, then applies \cs{clistClear} to leave
4983 the list empty.
4984 \begin{demohigh}
4985 \clistClearNew \lFooSomeClist
4986 \clistSet \lFooSomeClist {one,two,three}
4987 \clistVarJoin \lFooSomeClist { and }
4988 \end{demohigh}
4989 \end{function}
4990
4991 \begin{function}{\clistConcat}
4992 \begin{syntax}
4993 \cs{clistConcat} \meta{comma list_1} \meta{comma list_2} \meta{comma list_3}
4994 \end{syntax}
4995 Concatenates the content of \meta{comma list_2} and \meta{comma list_3}
4996 together and saves the result in \meta{comma list_1}. The items in
4997 \meta{comma list_2} are placed at the left side of the new comma list.
4998 \begin{demohigh}
4999 \clistSet \lTmpbClist {one,two}
5000 \clistSet \lTmpcClist {three,four}
5001 \clistConcat \lTmpaClist \lTmpbClist \lTmpcClist
5002 \clistVarJoin \lTmpaClist { + }
5003 \end{demohigh}
5004 \end{function}
5005
5006 \begin{function}{\clistPutLeft}
5007 \begin{syntax}
5008 \cs{clistPutLeft} \meta{comma list} \verb|{|\meta{item_1},\ldots{},\meta{item_n}\verb|}|
5009 \end{syntax}
5010 Appends the \meta{items} to the left of the \meta{comma list}.
5011 Blank items are omitted, spaces are removed from both sides of each
5012 item, then a set of braces is removed if the resulting space-trimmed
5013 item is braced.
5014 To append some \meta{tokens} as a single \meta{item} even if the
5015 \meta{tokens} contain commas or spaces, add a set of braces:
5016 \cs{clistPutLeft} \meta{comma list} \verb|{| \Arg{tokens} \verb|}|.
5017 \begin{demohigh}
5018 \clistSet \lTmpaClist {one,two}
5019 \clistPutLeft \lTmpaClist {zero}
5020 \clistVarJoin \lTmpaClist { and }
5021 \end{demohigh}
5022 \end{function}
5023
5024 \begin{function}{\clistPutRight}
5025 \begin{syntax}
5026 \cs{clistPutRight} \meta{comma list} \verb|{|\meta{item_1},\ldots{},\meta{item_n}\verb|}|
5027 \end{syntax}
5028 Appends the \meta{items} to the right of the \meta{comma list}.
5029 Blank items are omitted, spaces are removed from both sides of each
5030 item, then a set of braces is removed if the resulting space-trimmed
5031 item is braced.
5032 To append some \meta{tokens} as a single \meta{item} even if the
5033 \meta{tokens} contain commas or spaces, add a set of braces:
5034 \cs{clistPutRight} \meta{comma list} \verb|{| \Arg{tokens} \verb|}|.
5035 \begin{demohigh}
5036 \clistSet \lTmpaClist {one,two}
5037 \clistPutRight \lTmpaClist {three}
5038 \clistVarJoin \lTmpaClist { and }
5039 \end{demohigh}
5040 \end{function}
5041
5042 \section{Modifying Comma Lists}
5043
5044 While comma lists are normally used as ordered lists, it may be
5045 necessary to modify the content. The functions here may be used
5046 to update comma lists, while retaining the order of the unaffected
5047 entries.
5048
5049 \begin{function}{\clistVarRemoveDuplicates}
5050 \begin{syntax}
5051 \cs{clistVarRemoveDuplicates} \meta{comma list}
5052 \end{syntax}
5053 Removes duplicate items from the \meta{comma list}, leaving the
5054 left most copy of each item in the \meta{comma list}. The \meta{item}
5055 comparison takes place on a token basis, as for \cs{tlIfEqTF}.
5056 \begin{demohigh}
5057 \clistSet \lTmpaClist {one,two,one,two,three}
5058 \clistVarRemoveDuplicates \lTmpaClist
5059 \clistVarJoin \lTmpaClist {,}
5060 \end{demohigh}
5061 %\begin{texnote}
5062 %This function iterates through every item in the \meta{comma list} and
5063 %does a comparison with the \meta{items} already checked. It is therefore
5064 %relatively slow with large comma lists.
5065 %Furthermore, it may fail if any of the items in the
5066 %\meta{comma list} contains \verb|{|, \verb|}|, or \verb|#|
5067 %(assuming the usual \TeX{} category codes apply).
5068 %\end{texnote}
5069 \end{function}
5070
5071 \begin{function}{\clistVarRemoveAll}
5072 \begin{syntax}
5073 \cs{clistVarRemoveAll} \meta{comma list} \Arg{item}
5074 \end{syntax}
5075 Removes every occurrence of \meta{item} from the \meta{comma list}.
5076 The \meta{item} comparison takes place on a token basis, as for
5077 \cs{tlIfEqTF}.
5078 \begin{demohigh}
5079 \clistSet \lTmpaClist {one,two,one,two,three}
5080 \clistVarRemoveAll \lTmpaClist {two}
5081 \clistVarJoin \lTmpaClist {,}
5082 \end{demohigh}
5083 %\begin{texnote}
5084 %The function may fail if the \meta{item} contains \verb|{|, \verb|}|, or \verb|#|
5085 %(assuming the usual \TeX{} category codes apply).
5086 %\end{texnote}
5087 \end{function}
5088
5089 \begin{function}{\clistVarReverse}
5090 \begin{syntax}
5091 \cs{clistVarReverse} \meta{comma list}
5092 \end{syntax}
5093 Reverses the order of items stored in the \meta{comma list}.
5094 \begin{demohigh}
5095 \clistSet \lTmpaClist {one,two,one,two,three}
5096 \clistVarReverse \lTmpaClist
5097 \clistVarJoin \lTmpaClist {,}
5098 \end{demohigh}
5099 \end{function}
5100
5101 %\begin{function}{\clistVarSort}
5102 %\begin{syntax}
5103 %\cs{clistVarSort} \meta{clist var} \Arg{comparison code}
5104 %\end{syntax}
5105 %Sorts the items in the \meta{clist var} according to the
5106 %\meta{comparison code}, and assigns the result to
5107 %\meta{clist var}. The details of sorting comparison are
5108 %described in Section \ref{sec:l3sort:mech}.
5109 %\end{function}
5110
5111 \section{Working with the Contents of Comma Lists}
5112
5113 \begin{function}{\clistCount,\clistVarCount}
5114 \begin{syntax}
5115 \cs{clistCount} \Arg{comma list}
5116 \cs{clistVarCount} \meta{comma list}
5117 \end{syntax}
5118 Returns the number of items in the \meta{comma list}
5119 as an \meta{integer denotation}. The total number of items
5120 in a \meta{comma list} includes those which are duplicates,
5121 \emph{i.e.} every item in a \meta{comma list} is counted.
5122 \begin{demohigh}
5123 \clistSet \lTmpaClist {one,two,three,four}
5124 \clistVarCount \lTmpaClist
5125 \end{demohigh}
5126 \end{function}
5127
5128 \begin{function}{\clistItem}
5129 \begin{syntax}
5130 \cs{clistItem} \Arg{comma list} \Arg{integer expression}
5131 \end{syntax}
5132 Indexing items in the \meta{comma list} from $1$ at the top (left), this
5133 function evaluates the \meta{integer expression} and returns the
5134 appropriate item from the comma list. If the
5135 \meta{integer expression} is negative, indexing occurs from the
5136 bottom (right) of the comma list. When the \meta{integer expression}
5137 is larger than the number of items in the \meta{comma list} (as
5138 calculated by \cs{clistCount}) then the function returns nothing.
5139 \begin{demohigh}
5140 \tlSet \lTmpaTl {\clistItem {one,two,three,four} {3}}
5141 \tlUse \lTmpaTl
5142 \end{demohigh}
5143 %\begin{texnote}
5144 %The result is returned within the \tn{unexpanded}
5145 %primitive (\cs{exp_not:n}), which means that the \meta{item}
5146 %does not expand further when appearing in an \texttt{x}-type
5147 %or \texttt{e}-type argument expansion.
5148 %\end{texnote}
5149 \end{function}
5150
5151 \begin{function}{\clistVarItem}
5152 \begin{syntax}
5153 \cs{clistVarItem} \meta{comma list} \Arg{integer expression}
5154 \end{syntax}
5155 Indexing items in the \meta{comma list} from $1$ at the top (left), this
5156 function evaluates the \meta{integer expression} and returns the
5157 appropriate item from the comma list. If the
5158 \meta{integer expression} is negative, indexing occurs from the
5159 bottom (right) of the comma list. When the \meta{integer expression}
5160 is larger than the number of items in the \meta{comma list} (as
5161 calculated by \cs{clistVarCount}) then the function returns nothing.
5162 \begin{demohigh}
5163 \clistSet \lTmpaClist {one,two,three,four}
5164 \tlSet \lTmpaTl {\clistVarItem \lTmpaClist {3}}
5165 \tlUse \lTmpaTl
5166 \end{demohigh}
5167 %\begin{texnote}
5168 %The result is returned within the \tn{unexpanded}
5169 %primitive (\cs{exp_not:n}), which means that the \meta{item}
5170 %does not expand further when appearing in an \texttt{x}-type
5171 %or \texttt{e}-type argument expansion.
5172 %\end{texnote}
5173 \end{function}
5174
5175 \begin{function}{\clistRandItem,\clistVarRandItem}
5176 \begin{syntax}
5177 \cs{clistRandItem} \Arg{comma list}
5178 \cs{clistVarRandItem} \meta{clist var}
5179 \end{syntax}
5180 Selects a pseudo-random item of the \meta{comma list}.
5181 If the \meta{comma list} has no item, the result is empty.
5182 \begin{demohigh}
5183 \tlSet \lTmpaTl {\clistRandItem {one,two,three,four,five,six}}
5184 \tlUse \lTmpaTl
5185 \tlSet \lTmpaTl {\clistRandItem {one,two,three,four,five,six}}
5186 \tlUse \lTmpaTl
5187 \end{demohigh}
5188 %\begin{texnote}
5189 %The result is returned within the \tn{unexpanded}
5190 %primitive (\cs{exp_not:n}), which means that the \meta{item}
5191 %does not expand further when appearing in an \texttt{x}-type
5192 %or \texttt{e}-type argument expansion.
5193 %\end{texnote}
5194 \end{function}
5195
5196 \section{Comma Lists as Stacks}
5197
5198 Comma lists can be used as stacks, where data is pushed to and popped
5199 from the top of the comma list. (The left of a comma list is the top, for
5200 performance reasons.) The stack functions for comma lists are not
5201 intended to be mixed with the general ordered data functions detailed
5202 in the previous section: a comma list should either be used as an
5203 ordered data type or as a stack, but not in both ways.
5204
5205 \begin{function}{\clistGet}
5206 \begin{syntax}
5207 \cs{clistGet} \meta{comma list} \meta{token list variable}
5208 \end{syntax}
5209 Stores the left-most item from the \meta{comma list} in the
5210 \meta{token list variable} without removing it from the
5211 \meta{comma list}. The \meta{token list variable} is assigned locally.
5212 If the \meta{comma list} is empty the
5213 \meta{token list variable} is set to the marker value \cs{qNoValue}.
5214 \begin{demohigh}
5215 \clistSet \lTmpaClist {two,three,four}
5216 \clistGet \lTmpaClist \lTmpaTl
5217 \tlUse \lTmpaTl
5218 \end{demohigh}
5219 \end{function}
5220
5221 \begin{function}{\clistGetT,\clistGetF,\clistGetTF}
5222 \begin{syntax}
5223 \cs{clistGetT} \meta{comma list} \meta{token list variable} \meta{true code}
5224 \cs{clistGetF} \meta{comma list} \meta{token list variable} \meta{false code}
5225 \cs{clistGetTF} \meta{comma list} \meta{token list variable} \meta{true code} \meta{false code}
5226 \end{syntax}
5227 If the \meta{comma list} is empty, leaves the \meta{false code} in the
5228 input stream. The value of the \meta{token list variable} is
5229 not defined in this case and should not be relied upon. If the
5230 \meta{comma list} is non-empty, stores the left-most item from the
5231 \meta{comma list} in the \meta{token list variable} without removing
5232 it from the \meta{comma list}.
5233 The \meta{token list variable} is assigned locally.
5234 \begin{demohigh}
5235 \clistSet \lTmpaClist {two,three,four}
5236 \clistGetTF \lTmpaClist \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
5237 \end{demohigh}
5238 \end{function}
5239
5240 \begin{function}{\clistPop}
5241 \begin{syntax}
5242 \cs{clistPop} \meta{comma list} \meta{token list variable}
5243 \end{syntax}
5244 Pops the left-most item from a \meta{comma list} into the
5245 \meta{token list variable}, \emph{i.e.} removes the item from the
5246 comma list and stores it in the \meta{token list variable}.
5247 The assignment of the \meta{token list variable} is local.
5248 If the \meta{comma list} is empty the
5249 \meta{token list variable} is set to the marker value \cs{qNoValue}.
5250 \begin{demohigh}
5251 \clistSet \lTmpaClist {two,three,four}
5252 \clistPop \lTmpaClist \lTmpaTl
5253 \clistVarJoin \lTmpaClist {,}
5254 \end{demohigh}
5255 \end{function}
5256
5257 \begin{function}{\clistPopT,\clistPopF,\clistPopTF}
5258 \begin{syntax}
5259 \cs{clistPopT} \meta{comma list} \meta{token list variable} \Arg{true code}
5260 \cs{clistPopF} \meta{comma list} \meta{token list variable} \Arg{false code}
5261 \cs{clistPopTF} \meta{comma list} \meta{token list variable} \Arg{true code} \Arg{false code}
5262 \end{syntax}
5263 If the \meta{comma list} is empty, leaves the \meta{false code} in the
5264 input stream. The value of the \meta{token list variable} is
5265 not defined in this case and should not be relied upon. If the
5266 \meta{comma list} is non-empty, pops the top item from the
5267 \meta{comma list} in the \meta{token list variable}, \emph{i.e.} removes
5268 the item from the \meta{comma list}.
5269 The \meta{token list variable} is assigned locally.
5270 \begin{demohigh}
5271 \clistSet \lTmpaClist {two,three,four}
5272 \clistPopTF \lTmpaClist \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
5273 \end{demohigh}
5274 \end{function}
5275
5276 \begin{function}{\clistPush}
5277 \begin{syntax}
5278 \cs{clistPush} \meta{comma list} \Arg{items}
5279 \end{syntax}
5280 Adds the \Arg{items} to the top of the \meta{comma list}.
5281 Spaces are removed from both sides of each item as for any
5282 \texttt{n}-type comma list.
5283 \begin{demohigh}
5284 \clistSet \lTmpaClist {two,three,four}
5285 \clistPush \lTmpaClist {zero,one}
5286 \clistVarJoin \lTmpaClist {|}
5287 \end{demohigh}
5288 \end{function}
5289
5290 \section{Mapping over Comma Lists}
5291
5292 %The functions described in this section apply a specified function
5293 %to each item of a comma list.
5294 %All mappings are done at the current group level, \emph{i.e.} any
5295 %local assignments made by the \meta{function} or \meta{code} discussed
5296 %below remain in effect after the loop.
5297
5298 When the comma list is given explicitly, %as an \texttt{n}-type argument,
5299 spaces are trimmed around each item.
5300 If the result of trimming spaces is empty, the item is ignored.
5301 Otherwise, if the item is surrounded by braces, one set is removed,
5302 and the result is passed to the mapped function. Thus, if the
5303 comma list that is being mapped is \verb*|{a , {{b} }, ,{}, {c},}|
5304 then the arguments passed to the mapped function are
5305 `\verb*|a|', `\verb*|{b} |', an empty argument, and `\verb*|c|'.
5306
5307 When the comma list is given as a variable, spaces
5308 have already been trimmed on input, and items are simply stripped
5309 of one set of braces if any. This case is more efficient than using
5310 explicit comma lists.
5311
5312 %\begin{function}{\clistMapFunction,\clistVarMapFunction}
5313 %\begin{syntax}
5314 %\cs{clistMapFunction} \Arg{comma list} \meta{function}
5315 %\cs{clistVarMapFunction} \meta{comma list} \meta{function}
5316 %\end{syntax}
5317 %Applies \meta{function} to every \meta{item} stored in the
5318 %\meta{comma list}. The \meta{function} receives one argument for
5319 %each iteration. The \meta{items} are returned from left to right.
5320 %The function \cs{clistMapInline} is in general more efficient
5321 %than \cs{clistMapFunction}.
5322 %\end{function}
5323
5324 \begin{function}{\clistMapInline,\clistVarMapInline}
5325 \begin{syntax}
5326 \cs{clistMapInline} \Arg{comma list} \Arg{inline function}
5327 \cs{clistVarMapInline} \meta{comma list} \Arg{inline function}
5328 \end{syntax}
5329 Applies \meta{inline function} to every \meta{item} stored
5330 within the \meta{comma list}. The \meta{inline function} should
5331 consist of code which receives the \meta{item} as \verb|#1|.
5332 The \meta{items} are returned from left to right.
5333 \begin{demohigh}
5334 \IgnoreSpacesOn
5335 \tlClear \lTmpaTl
5336 \clistMapInline {one,two,three} {
5337 \tlPutRight \lTmpaTl {(#1)}
5338 }
5339 \tlUse \lTmpaTl
5340 \IgnoreSpacesOff
5341 \end{demohigh}
5342 \end{function}
5343
5344 \begin{function}{\clistMapVariable,\clistVarMapVariable}
5345 \begin{syntax}
5346 \cs{clistMapVariable} \Arg{comma list} \meta{variable} \Arg{code}
5347 \cs{clistVarMapVariable} \meta{comma list} \meta{variable} \Arg{code}
5348 \end{syntax}
5349 Stores each \meta{item} of the \meta{comma list} in turn in the
5350 (token list) \meta{variable} and applies the \meta{code}. The
5351 \meta{code} will usually make use of the \meta{variable}, but this
5352 is not enforced. The assignments to the \meta{variable} are local.
5353 Its value after the loop is the last \meta{item} in the \meta{comma
5354 list}, or its original value if there were no \meta{item}. The
5355 \meta{items} are returned from left to right.
5356 \begin{demohigh}
5357 \IgnoreSpacesOn
5358 \clistMapVariable {one,two,three} \lTmpiTl {
5359 \tlPutRight \gTmpaTl {\expWhole {(\lTmpiTl)}}
5360 }
5361 \tlUse \gTmpaTl
5362 \IgnoreSpacesOff
5363 \end{demohigh}
5364 \end{function}
5365
5366 %\begin{function}{\clistMapTokens,\clistVarMapTokens}
5367 %\begin{syntax}
5368 %\cs{clistMapTokens} \Arg{comma list} \Arg{code}
5369 %\cs{clistVarMapTokens} \meta{clist var} \Arg{code}
5370 %\end{syntax}
5371 %Calls \meta{code} \Arg{item} for every \meta{item} stored in the
5372 %\meta{comma list}. The \meta{code} receives each \meta{item} as a
5373 %trailing brace group. If the \meta{code} consists of a single
5374 %function this is equivalent to \cs{clistMapFunction} or \cs{clistVarMapFunction}.
5375 %\end{function}
5376
5377 %\begin{function}{\clistMapBreak}
5378 %\begin{syntax}
5379 %\cs{clistMapBreak}
5380 %\end{syntax}
5381 %Used to terminate a clist map function before all
5382 %entries in the \meta{comma list} have been processed. This
5383 %normally takes place within a conditional statement, for example
5384 %\begin{verbatim}
5385 %\clist_map_inline:Nn \l_my_clist
5386 %{
5387 %\str_if_eq:nnTF { #1 } { bingo }
5388 %{ \clist_map_break: }
5389 %{
5390 %Do something useful
5391 %}
5392 %}
5393 %\end{verbatim}
5394 %Use outside of a clist map scenario leads to low
5395 %level \TeX{} errors.
5396 %\begin{texnote}
5397 %When the mapping is broken, additional tokens may be inserted
5398 %before further items are taken
5399 %from the input stream. This depends on the design of the mapping
5400 %function.
5401 %\end{texnote}
5402 %\end{function}
5403 %
5404 %\begin{function}{\clistMapBreakDo}
5405 %\begin{syntax}
5406 %\cs{clistMapBreakDo} \Arg{code}
5407 %\end{syntax}
5408 %Used to terminate a clist map function before all
5409 %entries in the \meta{comma list} have been processed, inserting
5410 %the \meta{code} after the mapping has ended. This
5411 %normally takes place within a conditional statement, for example
5412 %\begin{verbatim}
5413 %\clist_map_inline:Nn \l_my_clist
5414 %{
5415 %\str_if_eq:nnTF { #1 } { bingo }
5416 %{ \clist_map_break:n { <code> } }
5417 %{
5418 %Do something useful
5419 %}
5420 %}
5421 %\end{verbatim}
5422 %Use outside of a clist map scenario leads to low
5423 %level \TeX{} errors.
5424 %\begin{texnote}
5425 %When the mapping is broken, additional tokens may be inserted
5426 %before the \meta{code} is
5427 %inserted into the input stream.
5428 %This depends on the design of the mapping function.
5429 %\end{texnote}
5430 %\end{function}
5431
5432 \section{Comma List Conditionals}
5433
5434 \begin{function}{\clistIfExist,\clistIfExistT,\clistIfExistF,\clistIfExistTF}
5435 \begin{syntax}
5436 \cs{clistIfExist} \meta{comma list}
5437 \cs{clistIfExistT} \meta{comma list} \Arg{true code}
5438 \cs{clistIfExistF} \meta{comma list} \Arg{false code}
5439 \cs{clistIfExistTF} \meta{comma list} \Arg{true code} \Arg{false code}
5440 \end{syntax}
5441 Tests whether the \meta{comma list} is currently defined. This does
5442 not check that the \meta{comma list} really is a comma list.
5443 \begin{demohigh}
5444 \clistIfExistTF \lTmpaClist {\prgReturn{Yes}} {\prgReturn{No}}
5445 \clistIfExistTF \lFooUndefinedClist {\prgReturn{Yes}} {\prgReturn{No}}
5446 \end{demohigh}
5447 \end{function}
5448
5449 \begin{function}{\clistIfEmpty,\clistIfEmptyT,\clistIfEmptyF,\clistIfEmptyTF}
5450 \begin{syntax}
5451 \cs{clistIfEmpty} \Arg{comma list}
5452 \cs{clistIfEmptyT} \Arg{comma list} \Arg{true code}
5453 \cs{clistIfEmptyF} \Arg{comma list} \Arg{false code}
5454 \cs{clistIfEmptyTF} \Arg{comma list} \Arg{true code} \Arg{false code}
5455 \end{syntax}
5456 Tests if the \meta{comma list} is empty (containing no items).
5457 The rules for space trimming are as for other \texttt{n}-type
5458 comma-list functions, hence the comma list \verb|{ , ,, }| (without
5459 outer braces) is empty, while \verb|{ ,{},}| (without outer braces)
5460 contains one element, which happens to be empty: the comma-list
5461 is not empty.
5462 \begin{demohigh}
5463 \clistIfEmptyTF {one,two} {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
5464 \clistIfEmptyTF { , } {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
5465 \end{demohigh}
5466 \end{function}
5467
5468 \begin{function}{\clistVarIfEmpty,\clistVarIfEmptyT,\clistVarIfEmptyF,\clistVarIfEmptyTF}
5469 \begin{syntax}
5470 \cs{clistVarIfEmpty} \meta{comma list}
5471 \cs{clistVarIfEmptyT} \meta{comma list} \Arg{true code}
5472 \cs{clistVarIfEmptyF} \meta{comma list} \Arg{false code}
5473 \cs{clistVarIfEmptyTF} \meta{comma list} \Arg{true code} \Arg{false code}
5474 \end{syntax}
5475 Tests if the \meta{comma list} is empty (containing no items).
5476 \begin{demohigh}
5477 \clistSet \lTmpaClist {one,two}
5478 \clistVarIfEmptyTF \lTmpaClist {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
5479 \clistClear \lTmpaClist
5480 \clistVarIfEmptyTF \lTmpaClist {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
5481 \end{demohigh}
5482 \end{function}
5483
5484 \begin{function}{\clistIfIn,\clistIfInT,\clistIfInF,\clistIfInTF}
5485 \begin{syntax}
5486 \cs{clistIfIn} \Arg{comma list} \Arg{item}
5487 \cs{clistIfInT} \Arg{comma list} \Arg{item} \Arg{true code}
5488 \cs{clistIfInF} \Arg{comma list} \Arg{item} \Arg{false code}
5489 \cs{clistIfInTF} \Arg{comma list} \Arg{item} \Arg{true code} \Arg{false code}
5490 \end{syntax}
5491 Tests if the \meta{item} is present in the \meta{comma list}.
5492 In the case of an \texttt{n}-type \meta{comma list}, the usual rules
5493 of space trimming and brace stripping apply. For example
5494 \begin{demohigh}
5495 \clistIfInTF { a , {b} , {b} , c } {b} {\prgReturn{Yes}} {\prgReturn{No}}
5496 \clistIfInTF { a , {b} , {b} , c } {d} {\prgReturn{Yes}} {\prgReturn{No}}
5497 \end{demohigh}
5498 %\begin{texnote}
5499 %The function may fail if the \meta{item} contains \verb|{|, \verb|}|, or \verb|#|
5500 %(assuming the usual \TeX{} category codes apply).
5501 %\end{texnote}
5502 \end{function}
5503
5504 \begin{function}{\clistVarIfIn,\clistVarIfInT,\clistVarIfInF,\clistVarIfInTF}
5505 \begin{syntax}
5506 \cs{clistVarIfIn} \meta{comma list} \Arg{item}
5507 \cs{clistVarIfInT} \meta{comma list} \Arg{item} \Arg{true code}
5508 \cs{clistVarIfInF} \meta{comma list} \Arg{item} \Arg{false code}
5509 \cs{clistVarIfInTF} \meta{comma list} \Arg{item} \Arg{true code} \Arg{false code}
5510 \end{syntax}
5511 Tests if the \meta{item} is present in the \meta{comma list}.
5512 In the case of an \texttt{n}-type \meta{comma list}, the usual rules
5513 of space trimming and brace stripping apply.
5514 \begin{demohigh}
5515 \clistSet \lTmpaClist {one,two}
5516 \clistVarIfInTF \lTmpaClist {one} {\prgReturn{Yes}} {\prgReturn{No}}
5517 \clistVarIfInTF \lTmpaClist {three} {\prgReturn{Yes}} {\prgReturn{No}}
5518 \end{demohigh}
5519 %\begin{texnote}
5520 %The function may fail if the \meta{item} contains \verb|{|, \verb|}|, or \verb|#|
5521 %(assuming the usual \TeX{} category codes apply).
5522 %\end{texnote}
5523 \end{function}
5524
5525 \chapter{Sequences and Stacks (\texttt{Seq})}
5526
5527 \section{Constant and Scratch Sequences}
5528
5529 \begin{variable}{\cEmptySeq}
5530 Constant that is always empty.
5531 \end{variable}
5532
5533 \begin{variable}{\lTmpaSeq,\lTmpbSeq,\lTmpcSeq,\lTmpiSeq,\lTmpjSeq,\lTmpkSeq}
5534 Scratch sequences for local assignment. These are never used by
5535 the \verb!functional! package, and so are safe for use with any
5536 function. However, they may be overwritten by other
5537 code and so should only be used for short-term storage.
5538 \end{variable}
5539
5540 \begin{variable}{\gTmpaSeq,\gTmpbSeq,\gTmpcSeq,\gTmpiSeq,\gTmpjSeq,\gTmpkSeq}
5541 Scratch sequences for global assignment. These are never used by
5542 the \verb!functional! package, and so are safe for use with any
5543 function. However, they may be overwritten by other
5544 code and so should only be used for short-term storage.
5545 \end{variable}
5546
5547 \section{Creating and Using Sequences}
5548
5549 \begin{function}{\seqNew}
5550 \begin{syntax}
5551 \cs{seqNew} \meta{sequence}
5552 \end{syntax}
5553 Creates a new \meta{sequence} or raises an error if the name is
5554 already taken. The declaration is global. The \meta{sequence}
5555 initially contains no items.
5556 \begin{codehigh}
5557 \seqNew \lFooSomeSeq
5558 \end{codehigh}
5559 \end{function}
5560
5561 \begin{function}{\seqConstFromClist}
5562 \begin{syntax}
5563 \cs{seqConstFromClist} \meta{seq var} \Arg{comma-list}
5564 \end{syntax}
5565 Creates a new constant \meta{seq var} or raises an error if the name
5566 is already taken. The \meta{seq var} is set globally to contain the
5567 items in the \meta{comma list}.
5568 \begin{codehigh}
5569 \seqConstFromClist \cFooSomeSeq {one,two,three}
5570 \end{codehigh}
5571 \end{function}
5572
5573 \begin{function}{\seqVarJoin}
5574 \begin{syntax}
5575 \cs{seqVarJoin} \meta{seq var} \Arg{separator}
5576 \end{syntax}
5577 Returns the contents of the \meta{seq var}, with
5578 the \meta{separator} between the items. If the sequence has
5579 a single item, it is returned with no \meta{separator},
5580 and an empty sequence returns nothing. An error is raised if
5581 the variable does not exist or if it is invalid.
5582 \begin{demohigh}
5583 \seqSetSplit \lTmpaSeq {|} {a|b|c|{de}|f}
5584 \seqVarJoin \lTmpaSeq { and }
5585 \end{demohigh}
5586 %\begin{texnote}
5587 %The result is returned within the \tn{unexpanded}
5588 %primitive (\cs{exp_not:n}), which means that the \meta{items}
5589 %do not expand further when appearing in an \texttt{x}-type
5590 %or \texttt{e}-type argument expansion.
5591 %\end{texnote}
5592 \end{function}
5593
5594 \begin{function}{\seqVarJoinExtended}
5595 \begin{syntax}
5596 \cs{seqVarJoinExtended} \meta{seq var} \Arg{separator between two} \Arg{separator between more than two} \Arg{separator between final two}
5597 \end{syntax}
5598 Returns the contents of the \meta{seq var}, with
5599 the appropriate \meta{separator} between the items. Namely, if the
5600 sequence has more than two items, the \meta{separator between more
5601 than two} is placed between each pair of items except the last,
5602 for which the \meta{separator between final two} is used. If the
5603 sequence has exactly two items, then they are joined
5604 with the \meta{separator between two} and returned. If the sequence has
5605 a single item, it is returned, and an empty sequence
5606 returns nothing. An error is raised if the variable does
5607 not exist or if it is invalid.
5608 \begin{demohigh}
5609 \seqSetSplit \lTmpaSeq {|} {a|b|c|{de}|f}
5610 \seqVarJoinExtended \lTmpaSeq { and } {, } {, and }
5611 \end{demohigh}
5612 The first separator argument is not used in this case
5613 because the sequence has more than $2$ items.
5614 %\begin{texnote}
5615 %The result is returned within the \tn{unexpanded}
5616 %primitive (\cs{exp_not:n}), which means that the \meta{items}
5617 %do not expand further when appearing in an \texttt{x}-type
5618 %or \texttt{e}-type argument expansion.
5619 %\end{texnote}
5620 \end{function}
5621
5622 \section{Viewing Sequences}
5623
5624 \begin{function}{\seqVarLog}
5625 \begin{syntax}
5626 \cs{seqVarLog} \meta{sequence}
5627 \end{syntax}
5628 Writes the entries in the \meta{sequence} in the log file.
5629 \begin{codehigh}
5630 \seqVarLog \lFooSomeSeq
5631 \end{codehigh}
5632 \end{function}
5633
5634 \begin{function}{\seqVarShow}
5635 \begin{syntax}
5636 \cs{seqVarShow} \meta{sequence}
5637 \end{syntax}
5638 Displays the entries in the \meta{sequence} in the terminal.
5639 \begin{codehigh}
5640 \seqVarShow \lFooSomeSeq
5641 \end{codehigh}
5642 \end{function}
5643
5644 \section{Setting Sequences}
5645
5646 \begin{function}{\seqSetFromClist}
5647 \begin{syntax}
5648 \cs{seqSetFromClist} \meta{sequence} \meta{comma-list}
5649 \end{syntax}
5650 Converts the data in the \meta{comma list} into a \meta{sequence}:
5651 the original \meta{comma list} is unchanged.
5652 \begin{demohigh}
5653 \seqSetFromClist \lTmpaSeq {one,two,three}
5654 \seqVarJoin \lTmpaSeq { and }
5655 \end{demohigh}
5656 \end{function}
5657
5658 \begin{function}{\seqSetSplit}
5659 \begin{syntax}
5660 \cs{seqSetSplit} \meta{sequence} \Arg{delimiter} \Arg{token list}
5661 \end{syntax}
5662 Splits the \meta{token list} into \meta{items} separated
5663 by \meta{delimiter}, and assigns the result to the \meta{sequence}.
5664 Spaces on both sides of each \meta{item} are ignored,
5665 then one set of outer braces is removed (if any);
5666 this space trimming behaviour is identical to that of
5667 \pkg{Clist} functions. Empty \meta{items} are preserved by
5668 \cs{seqSetSplit}, and can be removed afterwards using
5669 \cs{seqVarRemoveAll} \meta{sequence} \verb|{}|.
5670 The \meta{delimiter} may not contain \verb|{|, \verb|}| or \verb|#|
5671 (assuming \TeX{}'s normal category code r\'egime).
5672 If the \meta{delimiter} is empty, the \meta{token list} is split
5673 into \meta{items} as a \meta{token list}.
5674 %See also \cs{seqSetSplitKeepSpaces}, which omits space stripping.
5675 \begin{demohigh}
5676 \seqSetSplit \lTmpaSeq {,} {1,2,3}
5677 \seqVarJoin \lTmpaSeq { and }
5678 \end{demohigh}
5679 \end{function}
5680
5681 \begin{function}{\seqSetEq}
5682 \begin{syntax}
5683 \cs{seqSetEq} \meta{sequence_1} \meta{sequence_2}
5684 \end{syntax}
5685 Sets the content of \meta{sequence_1} equal to that of
5686 \meta{sequence_2}.
5687 \begin{demohigh}
5688 \seqSetFromClist \lTmpaSeq {one,two,three,four}
5689 \seqSetEq \lTmpbSeq \lTmpaSeq
5690 \seqVarJoin \lTmpbSeq { and }
5691 \end{demohigh}
5692 \end{function}
5693
5694 \begin{function}{\seqClear}
5695 \begin{syntax}
5696 \cs{seqClear} \meta{sequence}
5697 \end{syntax}
5698 Clears all items from the \meta{sequence}.
5699 \begin{codehigh}
5700 \seqClear \lTmpaSeq
5701 \end{codehigh}
5702 \end{function}
5703
5704 \begin{function}{\seqClearNew}
5705 \begin{syntax}
5706 \cs{seqClearNew} \meta{sequence}
5707 \end{syntax}
5708 Ensures that the \meta{sequence} exists globally by applying
5709 \cs{seqNew} if necessary, then applies \cs{seqClear} to leave
5710 the \meta{sequence} empty.
5711 \begin{demohigh}
5712 \seqClearNew \lFooSomeSeq
5713 \seqSetFromClist \lFooSomeSeq {one,two,three}
5714 \seqVarJoin \lFooSomeSeq { and }
5715 \end{demohigh}
5716 \end{function}
5717
5718 \begin{function}{\seqConcat}
5719 \begin{syntax}
5720 \cs{seqConcat} \meta{sequence_1} \meta{sequence_2} \meta{sequence_3}
5721 \end{syntax}
5722 Concatenates the content of \meta{sequence_2} and \meta{sequence_3}
5723 together and saves the result in \meta{sequence_1}. The items in
5724 \meta{sequence_2} are placed at the left side of the new sequence.
5725 \begin{demohigh}
5726 \seqSetFromClist \lTmpbSeq {one,two}
5727 \seqSetFromClist \lTmpcSeq {three,four}
5728 \seqConcat \lTmpaSeq \lTmpbSeq \lTmpcSeq
5729 \seqVarJoin \lTmpaSeq {, }
5730 \end{demohigh}
5731 \end{function}
5732
5733 %\begin{function}{\seqSetSplitKeepSpaces}
5734 %\begin{syntax}
5735 %\cs{seqSetSplitKeepSpaces} \meta{sequence} \Arg{delimiter} \Arg{token list}
5736 %\end{syntax}
5737 %Splits the \meta{token list} into \meta{items} separated
5738 %by \meta{delimiter}, and assigns the result to the \meta{sequence}.
5739 %One set of outer braces is removed (if any) but any surrounding spaces
5740 %are retained: any braces \emph{inside} one or more spaces are
5741 %therefore kept. Empty \meta{items} are preserved by
5742 %\cs{seqSetSplitKeepSpaces}, and can be removed afterwards using
5743 %\cs{seqVarRemoveAll} \meta{sequence} \verb|{}|.
5744 %The \meta{delimiter} may not contain \verb|{|, \verb|}| or \verb|#|
5745 %(assuming \TeX{}'s normal category code r\'egime).
5746 %If the \meta{delimiter} is empty, the \meta{token list} is split
5747 %into \meta{items} as a \meta{token list}.
5748 %%See also \cs{seqSetSplit}, which removes spaces around the delimiters.
5749 %\end{function}
5750
5751 \begin{function}{\seqPutLeft}
5752 \begin{syntax}
5753 \cs{seqPutLeft} \meta{sequence} \Arg{item}
5754 \end{syntax}
5755 Appends the \meta{item} to the left of the \meta{sequence}.
5756 \begin{demohigh}
5757 \seqSetFromClist \lTmpaSeq {one,two}
5758 \seqPutLeft \lTmpaSeq {zero}
5759 \seqVarJoin \lTmpaSeq { and }
5760 \end{demohigh}
5761 \end{function}
5762
5763 \begin{function}{\seqPutRight}
5764 \begin{syntax}
5765 \cs{seqPutRight} \meta{sequence} \Arg{item}
5766 \end{syntax}
5767 Appends the \meta{item} to the right of the \meta{sequence}.
5768 \begin{demohigh}
5769 \seqSetFromClist \lTmpaSeq {one,two}
5770 \seqPutRight \lTmpaSeq {three}
5771 \seqVarJoin \lTmpaSeq { and }
5772 \end{demohigh}
5773 \end{function}
5774
5775 \section{Modifying Sequences}
5776
5777 While sequences are normally used as ordered lists, it may be
5778 necessary to modify the content. The functions here may be used
5779 to update sequences, while retaining the order of the unaffected
5780 entries.
5781
5782 \begin{function}{\seqVarRemoveDuplicates}
5783 \begin{syntax}
5784 \cs{seqVarRemoveDuplicates} \meta{sequence}
5785 \end{syntax}
5786 Removes duplicate items from the \meta{sequence}, leaving the
5787 left most copy of each item in the \meta{sequence}. The \meta{item}
5788 comparison takes place on a token basis, as for \cs{tlIfEqTF}.
5789 \begin{demohigh}
5790 \seqSetFromClist \lTmpaSeq {one,two,one,two,three}
5791 \seqVarRemoveDuplicates \lTmpaSeq
5792 \seqVarJoin \lTmpaSeq {,}
5793 \end{demohigh}
5794 %\begin{texnote}
5795 %This function iterates through every item in the \meta{sequence} and
5796 %does a comparison with the \meta{items} already checked. It is therefore
5797 %relatively slow with large sequences.
5798 %\end{texnote}
5799 \end{function}
5800
5801 \begin{function}{\seqVarRemoveAll}
5802 \begin{syntax}
5803 \cs{seqVarRemoveAll} \meta{sequence} \Arg{item}
5804 \end{syntax}
5805 Removes every occurrence of \meta{item} from the \meta{sequence}.
5806 The \meta{item} comparison takes place on a token basis, as for \cs{tlIfEqTF}.
5807 \begin{demohigh}
5808 \seqSetFromClist \lTmpaSeq {one,two,one,two,three}
5809 \seqVarRemoveAll \lTmpaSeq {two}
5810 \seqVarJoin \lTmpaSeq {,}
5811 \end{demohigh}
5812 \end{function}
5813
5814 \begin{function}{\seqVarReverse}
5815 \begin{syntax}
5816 \cs{seqVarReverse} \meta{sequence}
5817 \end{syntax}
5818 Reverses the order of the items stored in the \meta{sequence}.
5819 \begin{demohigh}
5820 \seqSetFromClist \lTmpaSeq {one,two,one,two,three}
5821 \seqVarReverse \lTmpaSeq
5822 \seqVarJoin \lTmpaSeq {,}
5823 \end{demohigh}
5824 \end{function}
5825
5826 %\begin{function}{\seqVarSort}
5827 %\begin{syntax}
5828 %\cs{seqVarSort} \meta{sequence} \Arg{comparison code}
5829 %\end{syntax}
5830 %Sorts the items in the \meta{sequence} according to the
5831 %\meta{comparison code}, and assigns the result to
5832 %\meta{sequence}. The details of sorting comparison are
5833 %described in Section \ref{sec:l3sort:mech}.
5834 %\end{function}
5835
5836 %\begin{function}{\seqVarShuffle}
5837 %\begin{syntax}
5838 %\cs{seqVarShuffle} \meta{seq var}
5839 %\end{syntax}
5840 %Sets the \meta{seq var} to the result of placing the items of the
5841 %\meta{seq var} in a random order. Each item is (roughly) as likely
5842 %to end up in any given position.
5843 %\begin{texnote}
5844 %For sequences with more than $13$ items or so, only a small
5845 %proportion of all possible permutations can be reached, because
5846 %the random seed \cs{sys_rand_seed:} only has $28$-bits. The use
5847 %of \tn{toks} internally means that sequences with more than
5848 %$32767$ or $65535$ items (depending on the engine) cannot be
5849 %shuffled.
5850 %\end{texnote}
5851 %\end{function}
5852
5853 \section{Working with the Contents of Sequences}
5854
5855 \begin{function}{\seqVarCount}
5856 \begin{syntax}
5857 \cs{seqVarCount} \meta{sequence}
5858 \end{syntax}
5859 Returns the number of items in the \meta{sequence}
5860 as an \meta{integer denotation}. The total number of items
5861 in a \meta{sequence} includes those which are empty and duplicates,
5862 \emph{i.e.} every item in a \meta{sequence} is unique.
5863 \begin{demohigh}
5864 \seqSetFromClist \lTmpaSeq {one,two,three,four}
5865 \seqVarCount \lTmpaSeq
5866 \end{demohigh}
5867 \end{function}
5868
5869 \begin{function}{\seqVarItem}
5870 \begin{syntax}
5871 \cs{seqVarItem} \meta{sequence} \Arg{integer expression}
5872 \end{syntax}
5873 Indexing items in the \meta{sequence} from $1$ at the top (left), this
5874 function evaluates the \meta{integer expression} and returns the
5875 appropriate item from the sequence. If the
5876 \meta{integer expression} is negative, indexing occurs from the
5877 bottom (right) of the sequence. If the \meta{integer expression}
5878 is larger than the number of items in the \meta{sequence} (as
5879 calculated by \cs{seqVarCount}) then the function returns nothing.
5880 \begin{demohigh}
5881 \seqSetFromClist \lTmpaSeq {one,two,three,four}
5882 \tlSet \lTmpaTl {\seqVarItem \lTmpaSeq {3}}
5883 \tlUse \lTmpaTl
5884 \end{demohigh}
5885 %\begin{texnote}
5886 %The result is returned within the \tn{unexpanded}
5887 %primitive (\cs{exp_not:n}), which means that the \meta{item}
5888 %does not expand further when appearing in an \texttt{x}-type
5889 %or \texttt{e}-type argument expansion.
5890 %\end{texnote}
5891 \end{function}
5892
5893 \begin{function}{\seqVarRandItem}
5894 \begin{syntax}
5895 \cs{seqVarRandItem} \meta{seq var}
5896 \end{syntax}
5897 Selects a pseudo-random item of the \meta{sequence}. If the
5898 \meta{sequence} is empty the result is empty.
5899 \begin{demohigh}
5900 \seqSetFromClist \lTmpaSeq {one,two,three,four,five,six}
5901 \tlSet \lTmpaTl {\seqVarRandItem \lTmpaSeq}
5902 \tlUse \lTmpaTl
5903 \tlSet \lTmpaTl {\seqVarRandItem \lTmpaSeq}
5904 \tlUse \lTmpaTl
5905 \end{demohigh}
5906 %This is not available in older versions of \XeTeX{}.
5907 %\begin{texnote}
5908 %The result is returned within the \tn{unexpanded}
5909 %primitive (\cs{exp_not:n}), which means that the \meta{item}
5910 %does not expand further when appearing in an \texttt{x}-type
5911 %or \texttt{e}-type argument expansion.
5912 %\end{texnote}
5913 \end{function}
5914
5915 \section{Sequences as Stacks}
5916
5917 Sequences can be used as stacks, where data is pushed to and popped
5918 from the top of the sequence. (The left of a sequence is the top, for
5919 performance reasons.) The stack functions for sequences are not
5920 intended to be mixed with the general ordered data functions detailed
5921 in the previous section: a sequence should either be used as an
5922 ordered data type or as a stack, but not in both ways.
5923
5924 \begin{function}{\seqGet}
5925 \begin{syntax}
5926 \cs{seqGet} \meta{sequence} \meta{token list variable}
5927 \end{syntax}
5928 Reads the top item from a \meta{sequence} into the
5929 \meta{token list variable} without removing it from the
5930 \meta{sequence}. The \meta{token list variable} is assigned locally.
5931 If \meta{sequence} is empty the \meta{token list variable} is set to
5932 the special marker \cs{qNoValue}.
5933 \begin{demohigh}
5934 \seqSetFromClist \lTmpaSeq {two,three,four}
5935 \seqGet \lTmpaSeq \lTmpaTl
5936 \tlUse \lTmpaTl
5937 \end{demohigh}
5938 \end{function}
5939
5940 \begin{function}{\seqGetT,\seqGetF,\seqGetTF}
5941 \begin{syntax}
5942 \cs{seqGetT} \meta{sequence} \meta{token list variable} \Arg{true code}
5943 \cs{seqGetF} \meta{sequence} \meta{token list variable} \Arg{false code}
5944 \cs{seqGetTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
5945 \end{syntax}
5946 If the \meta{sequence} is empty, leaves the \meta{false code} in the
5947 input stream. The value of the \meta{token list variable} is
5948 not defined in this case and should not be relied upon. If the
5949 \meta{sequence} is non-empty, stores the top item from a
5950 \meta{sequence} in the \meta{token list variable} without removing it from
5951 the \meta{sequence}. The \meta{token list variable} is assigned locally.
5952 \begin{demohigh}
5953 \seqSetFromClist \lTmpaSeq {two,three,four}
5954 \seqGetTF \lTmpaSeq \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
5955 \end{demohigh}
5956 \end{function}
5957
5958 \begin{function}{\seqPop}
5959 \begin{syntax}
5960 \cs{seqPop} \meta{sequence} \meta{token list variable}
5961 \end{syntax}
5962 Pops the top item from a \meta{sequence} into the
5963 \meta{token list variable}. the \meta{token list variable} is assigned locally.
5964 If \meta{sequence} is empty the \meta{token list variable} is set to
5965 the special marker \cs{qNoValue}.
5966 \begin{demohigh}
5967 \seqSetFromClist \lTmpaSeq {two,three,four}
5968 \seqPop \lTmpaSeq \lTmpaTl
5969 \seqVarJoin \lTmpaSeq {,}
5970 \end{demohigh}
5971 \end{function}
5972
5973 \begin{function}{\seqPopT,\seqPopF,\seqPopTF}
5974 \begin{syntax}
5975 \cs{seqPopT} \meta{sequence} \meta{token list variable} \Arg{true code}
5976 \cs{seqPopF} \meta{sequence} \meta{token list variable} \Arg{false code}
5977 \cs{seqPopTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
5978 \end{syntax}
5979 If the \meta{sequence} is empty, leaves the \meta{false code} in the
5980 input stream. The value of the \meta{token list variable} is
5981 not defined in this case and should not be relied upon. If the
5982 \meta{sequence} is non-empty, pops the top item from the \meta{sequence}
5983 in the \meta{token list variable}, \emph{i.e.} removes the item from the
5984 \meta{sequence}. The \meta{token list variable} is assigned locally.
5985 \begin{demohigh}
5986 \seqPopTF \cEmptySeq \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
5987 \end{demohigh}
5988 \end{function}
5989
5990 \begin{function}{\seqPush}
5991 \begin{syntax}
5992 \cs{seqPush} \meta{sequence} \Arg{item}
5993 \end{syntax}
5994 Adds the \Arg{item} to the top of the \meta{sequence}.
5995 \begin{demohigh}
5996 \seqSetFromClist \lTmpaSeq {two,three,four}
5997 \seqPush \lTmpaSeq {one}
5998 \seqVarJoin \lTmpaSeq {|}
5999 \end{demohigh}
6000 You can only push one item to the \meta{sequence} with \cs{seqPush},
6001 which is different from \cs{ClistPush}.
6002 \end{function}
6003
6004 \section{Recovering Items from Sequences}
6005
6006 Items can be recovered from either the left or the right of sequences.
6007 For implementation reasons, the actions at the left of the sequence are
6008 faster than those acting on the right. These functions all assign the
6009 recovered material locally.
6010
6011 \begin{function}{\seqGetLeft}
6012 \begin{syntax}
6013 \cs{seqGetLeft} \meta{sequence} \meta{token list variable}
6014 \end{syntax}
6015 Stores the left-most item from a \meta{sequence} in the
6016 \meta{token list variable} without removing it from the
6017 \meta{sequence}. The \meta{token list variable} is assigned locally.
6018 If \meta{sequence} is empty the \meta{token list variable}
6019 is set to the special marker \cs{qNoValue}.
6020 \begin{demohigh}
6021 \seqSetFromClist \lTmpaSeq {two,three,four}
6022 \seqGetLeft \lTmpaSeq \lTmpaTl
6023 \tlUse \lTmpaTl
6024 \end{demohigh}
6025 \end{function}
6026
6027 \begin{function}{\seqGetLeftT,\seqGetLeftF,\seqGetLeftTF}
6028 \begin{syntax}
6029 \cs{seqGetLeftT} \meta{sequence} \meta{token list variable} \Arg{true code}
6030 \cs{seqGetLeftF} \meta{sequence} \meta{token list variable} \Arg{false code}
6031 \cs{seqGetLeftTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
6032 \end{syntax}
6033 If the \meta{sequence} is empty, leaves the \meta{false code} in the
6034 input stream. The value of the \meta{token list variable} is
6035 not defined in this case and should not be relied upon. If the
6036 \meta{sequence} is non-empty, stores the left-most item from the
6037 \meta{sequence}
6038 in the \meta{token list variable} without removing it from the
6039 \meta{sequence}, then leaves the \meta{true code} in the input stream.
6040 The \meta{token list variable} is assigned locally.
6041 \begin{demohigh}
6042 \seqSetFromClist \lTmpaSeq {two,three,four}
6043 \seqGetLeftTF \lTmpaSeq \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
6044 \end{demohigh}
6045 \end{function}
6046
6047 \begin{function}{\seqGetRight}
6048 \begin{syntax}
6049 \cs{seqGetRight} \meta{sequence} \meta{token list variable}
6050 \end{syntax}
6051 Stores the right-most item from a \meta{sequence} in the
6052 \meta{token list variable} without removing it from the
6053 \meta{sequence}. The \meta{token list variable} is assigned locally.
6054 If \meta{sequence} is empty the \meta{token list variable}
6055 is set to the special marker \cs{qNoValue}.
6056 \begin{demohigh}
6057 \seqSetFromClist \lTmpaSeq {two,three,four}
6058 \seqGetRight \lTmpaSeq \lTmpaTl
6059 \tlUse \lTmpaTl
6060 \end{demohigh}
6061 \end{function}
6062
6063 \begin{function}{\seqGetRightT,\seqGetRightF,\seqGetRightTF}
6064 \begin{syntax}
6065 \cs{seqGetRightT} \meta{sequence} \meta{token list variable} \Arg{true code}
6066 \cs{seqGetRightF} \meta{sequence} \meta{token list variable} \Arg{false code}
6067 \cs{seqGetRightTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
6068 \end{syntax}
6069 If the \meta{sequence} is empty, leaves the \meta{false code} in the
6070 input stream. The value of the \meta{token list variable} is
6071 not defined in this case and should not be relied upon. If the
6072 \meta{sequence} is non-empty, stores the right-most item from the
6073 \meta{sequence}
6074 in the \meta{token list variable} without removing it from the
6075 \meta{sequence}, then leaves the \meta{true code} in the input stream.
6076 The \meta{token list variable} is assigned locally.
6077 \begin{demohigh}
6078 \seqSetFromClist \lTmpaSeq {two,three,four}
6079 \seqGetRightTF \lTmpaSeq \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
6080 \end{demohigh}
6081 \end{function}
6082
6083 \begin{function}{\seqPopLeft}
6084 \begin{syntax}
6085 \cs{seqPopLeft} \meta{sequence} \meta{token list variable}
6086 \end{syntax}
6087 Pops the left-most item from a \meta{sequence} into the
6088 \meta{token list variable}, \emph{i.e.} removes the item from the
6089 sequence and stores it in the \meta{token list variable}.
6090 The assignment of the \meta{token list variable} is local.
6091 If \meta{sequence} is empty the \meta{token list variable}
6092 is set to the special marker \cs{qNoValue}.
6093 \begin{demohigh}
6094 \seqSetFromClist \lTmpaSeq {two,three,four}
6095 \seqPopLeft \lTmpaSeq \lTmpaTl
6096 \seqVarJoin \lTmpaSeq {,}
6097 \end{demohigh}
6098 \end{function}
6099
6100 \begin{function}{\seqPopLeftT,\seqPopLeftF,\seqPopLeftTF}
6101 \begin{syntax}
6102 \cs{seqPopLeftT} \meta{sequence} \meta{token list variable} \Arg{true code}
6103 \cs{seqPopLeftF} \meta{sequence} \meta{token list variable} \Arg{false code}
6104 \cs{seqPopLeftTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
6105 \end{syntax}
6106 If the \meta{sequence} is empty, leaves the \meta{false code} in the
6107 input stream. The value of the \meta{token list variable} is
6108 not defined in this case and should not be relied upon. If the
6109 \meta{sequence} is non-empty, pops the left-most item from the \meta{sequence}
6110 in the \meta{token list variable}, \emph{i.e.} removes the item from the
6111 \meta{sequence}, then leaves the \meta{true code} in the input stream.
6112 The \meta{token list variable} is assigned locally.
6113 \begin{demohigh}
6114 \seqPopLeftTF \cEmptySeq \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
6115 \end{demohigh}
6116 \end{function}
6117
6118 \begin{function}{\seqPopRight}
6119 \begin{syntax}
6120 \cs{seqPopRight} \meta{sequence} \meta{token list variable}
6121 \end{syntax}
6122 Pops the right-most item from a \meta{sequence} into the
6123 \meta{token list variable}, \emph{i.e.} removes the item from the
6124 sequence and stores it in the \meta{token list variable}.
6125 The assignment of the \meta{token list variable} is local.
6126 If \meta{sequence} is empty the \meta{token list variable}
6127 is set to the special marker \cs{qNoValue}.
6128 \begin{demohigh}
6129 \seqSetFromClist \lTmpaSeq {two,three,four}
6130 \seqPopRight \lTmpaSeq \lTmpaTl
6131 \seqVarJoin \lTmpaSeq {,}
6132 \end{demohigh}
6133 \end{function}
6134
6135 \begin{function}{\seqPopRightT,\seqPopRightF,\seqPopRightTF}
6136 \begin{syntax}
6137 \cs{seqPopRightT} \meta{sequence} \meta{token list variable} \Arg{true code}
6138 \cs{seqPopRightF} \meta{sequence} \meta{token list variable} \Arg{false code}
6139 \cs{seqPopRightTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
6140 \end{syntax}
6141 If the \meta{sequence} is empty, leaves the \meta{false code} in the
6142 input stream. The value of the \meta{token list variable} is
6143 not defined in this case and should not be relied upon. If the
6144 \meta{sequence} is non-empty, pops the right-most item from the \meta{sequence}
6145 in the \meta{token list variable}, \emph{i.e.} removes the item from the
6146 \meta{sequence}, then leaves the \meta{true code} in the input stream.
6147 The \meta{token list variable} is assigned locally.
6148 \begin{demohigh}
6149 \seqPopRightTF \cEmptySeq \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
6150 \end{demohigh}
6151 \end{function}
6152
6153 \section{Mapping over Sequences}
6154
6155 %All mappings are done at the current group level, \emph{i.e.} any
6156 %local assignments made by the \meta{function} or \meta{code} discussed
6157 %below remain in effect after the loop.
6158
6159 %\begin{function}{\seqVarMapFunction}
6160 %\begin{syntax}
6161 %\cs{seqVarMapFunction} \meta{sequence} \meta{function}
6162 %\end{syntax}
6163 %Applies \meta{function} to every \meta{item} stored in the
6164 %\meta{sequence}. The \meta{function} will receive one argument for
6165 %each iteration. The \meta{items} are returned from left to right.
6166 %To pass further arguments to the \meta{function}, see \cs{seqVarMapTokens}.
6167 %The function \cs{seqVarMapInline} is faster than
6168 %\cs{seqVarMapFunction} for sequences with more than about $10$ items.
6169 %\end{function}
6170
6171 \begin{function}{\seqVarMapInline}
6172 \begin{syntax}
6173 \cs{seqVarMapInline} \meta{sequence} \Arg{inline function}
6174 \end{syntax}
6175 Applies \meta{inline function} to every \meta{item} stored
6176 within the \meta{sequence}. The \meta{inline function} should
6177 consist of code which will receive the \meta{item} as \verb|#1|.
6178 The \meta{items} are returned from left to right.
6179 \begin{demohigh}
6180 \IgnoreSpacesOn
6181 \seqSetFromClist \lTmpkSeq {one,two,three}
6182 \tlClear \lTmpaTl
6183 \seqVarMapInline \lTmpkSeq {
6184 \tlPutRight \lTmpaTl {(#1)}
6185 }
6186 \tlUse \lTmpaTl
6187 \IgnoreSpacesOff
6188 \end{demohigh}
6189 \end{function}
6190
6191 %\begin{function}{\seqVarMapTokens}
6192 %\begin{syntax}
6193 %\cs{seqVarMapTokens} \meta{sequence} \Arg{code}
6194 %\end{syntax}
6195 %Analogue of \cs{seqVarMapFunction} which maps several tokens
6196 %instead of a single function. The \meta{code} receives each item in
6197 %the \meta{sequence} as a trailing brace group. For instance,
6198 %\begin{verbatim}
6199 %\seqVarMapTokens \lMySeq { \prgReplicate { 2 } }
6200 %\end{verbatim}
6201 %expands to twice each item in the \meta{sequence}: for each item in
6202 %\verb|\l_my_seq| the function \cs{PrgReplicate} receives \verb|2| and
6203 %\meta{item} as its two arguments. The function
6204 %\cs{seqVarMapInline} is typically faster but it is not expandable.
6205 %\end{function}
6206
6207 \begin{function}{\seqVarMapVariable}
6208 \begin{syntax}
6209 \cs{seqVarMapVariable} \meta{sequence} \meta{variable} \Arg{code}
6210 \end{syntax}
6211 Stores each \meta{item} of the \meta{sequence} in turn in the (token
6212 list) \meta{variable} and applies the \meta{code}. The \meta{code}
6213 will usually make use of the \meta{variable}, but this is not
6214 enforced. The assignments to the \meta{variable} are local. Its
6215 value after the loop is the last \meta{item} in the \meta{sequence},
6216 or its original value if the \meta{sequence} is empty. The
6217 \meta{items} are returned from left to right.
6218 \begin{demohigh}
6219 \IgnoreSpacesOn
6220 \intZero \lTmpaInt
6221 \seqSetFromClist \lTmpaSeq {1,3,7}
6222 \seqVarMapVariable \lTmpaSeq \lTmpiTl {
6223 \intAdd \lTmpaInt {\lTmpiTl*\lTmpiTl}
6224 }
6225 \intUse \lTmpaInt
6226 \IgnoreSpacesOff
6227 \end{demohigh}
6228 \end{function}
6229
6230 %\begin{function}{\seqVarMapIndexedFunction}
6231 %\begin{syntax}
6232 %\cs{seqVarMapIndexedFunction} \meta{seq var} \meta{function}
6233 %\end{syntax}
6234 %Applies \meta{function} to every entry in the \meta{sequence
6235 %variable}. The \meta{function} should have signature |:nn|. It
6236 %receives two arguments for each iteration: the \meta{index} (namely
6237 %\verb|1| for the first entry, then \verb|2| and so on) and the \meta{item}.
6238 %\end{function}
6239
6240 %\begin{function}{\seqVarMapIndexedInline}
6241 %\begin{syntax}
6242 %\cs{seqVarMapIndexedInline} \meta{seq var} \Arg{inline function}
6243 %\end{syntax}
6244 %Applies \meta{inline function} to every entry in the \meta{sequence
6245 %variable}. The \meta{inline function} should consist of code which
6246 %receives the \meta{index} (namely \verb|1| for the first entry, then \verb|2|
6247 %and so on) as \verb|#1| and the \meta{item} as \verb|#2|.
6248 %\end{function}
6249
6250 %\begin{function}{\seqMapBreak}
6251 %\begin{syntax}
6252 %\cs{seqMapBreak}
6253 %\end{syntax}
6254 %Used to terminate a seq map function before all
6255 %entries in the \meta{sequence} have been processed. This
6256 %normally takes place within a conditional statement, for example
6257 %\begin{verbatim}
6258 %\seq_map_inline:Nn \l_my_seq
6259 %{
6260 %\str_if_eq:nnTF { #1 } { bingo }
6261 %{ \seq_map_break: }
6262 %{
6263 %Do something useful
6264 %}
6265 %}
6266 %\end{verbatim}
6267 %Use outside of a seq map scenario leads to low
6268 %level \TeX{} errors.
6269 %\begin{texnote}
6270 %When the mapping is broken, additional tokens may be inserted
6271 %before further items are taken
6272 %from the input stream. This depends on the design of the mapping
6273 %function.
6274 %\end{texnote}
6275 %\end{function}
6276
6277 %\begin{function}{\seqMapBreakDo}
6278 %\begin{syntax}
6279 %\cs{seqMapBreakDo} \Arg{code}
6280 %\end{syntax}
6281 %Used to terminate a seq map function before all
6282 %entries in the \meta{sequence} have been processed, inserting
6283 %the \meta{code} after the mapping has ended. This
6284 %normally takes place within a conditional statement, for example
6285 %\begin{verbatim}
6286 %\seq_map_inline:Nn \l_my_seq
6287 %{
6288 %\str_if_eq:nnTF { #1 } { bingo }
6289 %{ \seq_map_break:n { <code> } }
6290 %{
6291 %Do something useful
6292 %}
6293 %}
6294 %\end{verbatim}
6295 %Use outside of a seq map scenario leads to low
6296 %level \TeX{} errors.
6297 %\begin{texnote}
6298 %When the mapping is broken, additional tokens may be inserted
6299 %before the \meta{code} is
6300 %inserted into the input stream.
6301 %This depends on the design of the mapping function.
6302 %\end{texnote}
6303 %\end{function}
6304
6305 %\begin{function}
6306 %{\seq_set_map:NNn, \seq_gset_map:NNn}
6307 %\begin{syntax}
6308 %\cs{seq_set_map:NNn} \meta{sequence_1} \meta{sequence_2} \Arg{inline function}
6309 %\end{syntax}
6310 %Applies \meta{inline function} to every \meta{item} stored
6311 %within the \meta{sequence_2}. The \meta{inline function} should
6312 %consist of code which will receive the \meta{item} as \verb|#1|.
6313 %The sequence resulting applying \meta{inline function} to each
6314 %\meta{item} is assigned to \meta{sequence_1}.
6315 %\begin{texnote}
6316 %Contrarily to other mapping functions, \cs{seq_map_break:} cannot
6317 %be used in this function, and would lead to low-level \TeX{} errors.
6318 %\end{texnote}
6319 %\end{function}
6320
6321 %\begin{function}[added = 2020-07-16]
6322 %{\seq_set_map_x:NNn, \seq_gset_map_x:NNn}
6323 %\begin{syntax}
6324 %\cs{seq_set_map_x:NNn} \meta{sequence_1} \meta{sequence_2} \Arg{inline function}
6325 %\end{syntax}
6326 %Applies \meta{inline function} to every \meta{item} stored
6327 %within the \meta{sequence_2}. The \meta{inline function} should
6328 %consist of code which will receive the \meta{item} as \verb|#1|.
6329 %The sequence resulting from \texttt{x}-expanding
6330 %\meta{inline function} applied to each \meta{item}
6331 %is assigned to \meta{sequence_1}. As such, the code
6332 %in \meta{inline function} should be expandable.
6333 %\begin{texnote}
6334 %Contrarily to other mapping functions, \cs{seq_map_break:} cannot
6335 %be used in this function, and would lead to low-level \TeX{} errors.
6336 %\end{texnote}
6337 %\end{function}
6338
6339 \section{Sequence Conditionals}
6340
6341 \begin{function}{\seqIfExist,\seqIfExistT,\seqIfExistF,\seqIfExistTF}
6342 \begin{syntax}
6343 \cs{seqIfExist} \meta{sequence}
6344 \cs{seqIfExistT} \meta{sequence} \Arg{true code}
6345 \cs{seqIfExistF} \meta{sequence} \Arg{false code}
6346 \cs{seqIfExistTF} \meta{sequence} \Arg{true code} \Arg{false code}
6347 \end{syntax}
6348 Tests whether the \meta{sequence} is currently defined. This does not
6349 check that the \meta{sequence} really is a sequence variable.
6350 \begin{demohigh}
6351 \seqIfExistTF \lTmpaSeq {\prgReturn{Yes}} {\prgReturn{No}}
6352 \seqIfExistTF \lFooUndefinedSeq {\prgReturn{Yes}} {\prgReturn{No}}
6353 \end{demohigh}
6354 \end{function}
6355
6356 \begin{function}{\seqVarIfEmpty,\seqVarIfEmptyT,\seqVarIfEmptyF,\seqVarIfEmptyTF}
6357 \begin{syntax}
6358 \cs{seqVarIfEmpty} \meta{sequence}
6359 \cs{seqVarIfEmptyT} \meta{sequence} \Arg{true code}
6360 \cs{seqVarIfEmptyF} \meta{sequence} \Arg{false code}
6361 \cs{seqVarIfEmptyTF} \meta{sequence} \Arg{true code} \Arg{false code}
6362 \end{syntax}
6363 Tests if the \meta{sequence} is empty (containing no items).
6364 \begin{demohigh}
6365 \seqSetFromClist \lTmpaSeq {one,two}
6366 \seqVarIfEmptyTF \lTmpaSeq {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
6367 \seqClear \lTmpaSeq
6368 \seqVarIfEmptyTF \lTmpaSeq {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
6369 \end{demohigh}
6370 \end{function}
6371
6372 \begin{function}{\seqVarIfIn,\seqVarIfInT,\seqVarIfInF,\seqVarIfInTF}
6373 \begin{syntax}
6374 \cs{seqVarIfIn} \meta{sequence} \Arg{item}
6375 \cs{seqVarIfInT} \meta{sequence} \Arg{item} \Arg{true code}
6376 \cs{seqVarIfInF} \meta{sequence} \Arg{item} \Arg{false code}
6377 \cs{seqVarIfInTF} \meta{sequence} \Arg{item} \Arg{true code} \Arg{false code}
6378 \end{syntax}
6379 Tests if the \meta{item} is present in the \meta{sequence}.
6380 \begin{demohigh}
6381 \seqSetFromClist \lTmpaSeq {one,two}
6382 \seqVarIfInTF \lTmpaSeq {one} {\prgReturn{Yes}} {\prgReturn{Not}}
6383 \seqVarIfInTF \lTmpaSeq {three} {\prgReturn{Yes}} {\prgReturn{Not}}
6384 \end{demohigh}
6385 \end{function}
6386
6387 \chapter{Property Lists (\texttt{Prop})}
6388
6389 \LaTeX3 implements a \enquote{property list} data type, which contain
6390 an unordered list of entries each of which consists of a \meta{key} and
6391 an associated \meta{value}. The \meta{key} and \meta{value} may both
6392 be any \meta{balanced text}, the \meta{key} is processed using
6393 \cs{tlToStr}, meaning that category codes are ignored. It is possible to
6394 map functions to property lists such that the function is applied to every
6395 key--value pair within the list.
6396
6397 Each entry in a property list must have a unique \meta{key}: if an entry is
6398 added to a property list which already contains the \meta{key} then the new
6399 entry overwrites the existing one. The \meta{keys} are compared on a
6400 string basis, using the same method as \cs{strIfEq}.
6401
6402 %Property lists are intended for storing key-based information for use within
6403 %code. This is in contrast to key--value lists, which are a form of
6404 %\emph{input} parsed by the \pkg{l3keys} module.
6405
6406 \section{Constant and Scratch Sequences}
6407
6408 \begin{variable}{\cEmptyProp}
6409 Constant that is always empty.
6410 \end{variable}
6411
6412 \begin{variable}{\lTmpaProp,\lTmpbProp,\lTmpcProp,\lTmpiProp,\lTmpjProp,\lTmpkProp}
6413 Scratch property lists for local assignment. These are never used by
6414 the \verb!functional! package, and so are safe for use with any
6415 function. However, they may be overwritten by other
6416 code and so should only be used for short-term storage.
6417 \end{variable}
6418
6419 \begin{variable}{\gTmpaProp,\gTmpbProp,\gTmpcProp,\gTmpiProp,\gTmpjProp,\gTmpkProp}
6420 Scratch property lists for global assignment. These are never used by
6421 the \verb!functional! package, and so are safe for use with any
6422 function. However, they may be overwritten by other
6423 code and so should only be used for short-term storage.
6424 \end{variable}
6425
6426 \section{Creating and Using Property Lists}
6427
6428 \begin{function}{\propNew}
6429 \begin{syntax}
6430 \cs{propNew} \meta{property list}
6431 \end{syntax}
6432 Creates a new \meta{property list} or raises an error if the name is
6433 already taken. The declaration is global. The \meta{property list}
6434 initially contains no entries.
6435 \begin{codehigh}
6436 \propNew \lFooSomeProp
6437 \end{codehigh}
6438 \end{function}
6439
6440 \begin{function}{\propConstFromKeyval}
6441 \begin{syntax}
6442 \cs{propConstFromKeyval} \meta{prop var}
6443 ~ \{
6444 ~ ~ \meta{key1} \verb|=| \meta{value1} \verb|,|
6445 ~ ~ \meta{key2} \verb|=| \meta{value2} \verb|,| $\cdots$
6446 ~ \}
6447 \end{syntax}
6448 Creates a new constant \meta{prop var} or raises an error if the
6449 name is already taken. The \meta{prop var} is set globally to
6450 contain key--value pairs given in the second argument, processed in
6451 the way described for \cs{propSetFromKeyval}. If duplicate
6452 keys appear only the last of the values is kept.
6453 This function correctly detects the \verb|=| and \verb|,| signs provided they
6454 have the standard category code $12$ or they are active.
6455 \begin{codehigh}
6456 \propConstFromKeyval \cFooSomeProp {key1=one,key2=two,key3=three}
6457 \end{codehigh}
6458 %Notice that in contrast to most keyval lists (\emph{e.g.} those in
6459 %\pkg{l3keys}), each key here \emph{must} be followed with an \texttt{=} sign.
6460 \end{function}
6461
6462 \begin{function}{\propToKeyval}
6463 \begin{syntax}
6464 \cs{propToKeyval} \meta{property list}
6465 \end{syntax}
6466 Returns the \meta{property list} in a key--value notation. Keep in mind
6467 that a \meta{property list} is \emph{unordered}, while key--value interfaces
6468 don't necessarily are, so this can't be used for arbitrary interfaces.
6469 %\begin{texnote}
6470 %The result is returned within the \tn{unexpanded} primitive
6471 %(\cs{exp_not:n}), which means that the key--value list does not expand
6472 %further when appearing in an \texttt{x}-type or \texttt{e}-type argument expansion.
6473 %It also needs exactly two steps of expansion.
6474 %\end{texnote}
6475 \begin{codehigh}
6476 \propToKeyval \lTmpaProp
6477 \end{codehigh}
6478 \end{function}
6479
6480 \section{Viewing Property Lists}
6481
6482 \begin{function}{\propVarLog}
6483 \begin{syntax}
6484 \cs{propVarLog} \meta{property list}
6485 \end{syntax}
6486 Writes the entries in the \meta{property list} in the log file.
6487 \begin{codehigh}
6488 \propVarLog \lTmpaProp
6489 \end{codehigh}
6490 \end{function}
6491
6492 \begin{function}{\propVarShow}
6493 \begin{syntax}
6494 \cs{propVarShow} \meta{property list}
6495 \end{syntax}
6496 Displays the entries in the \meta{property list} in the terminal.
6497 \begin{codehigh}
6498 \propVarShow \lTmpaProp
6499 \end{codehigh}
6500 \end{function}
6501
6502 \section{Setting Property Lists}
6503
6504 \begin{function}{\propSetFromKeyval}
6505 \begin{syntax}
6506 \cs{propSetFromKeyval} \meta{prop var}
6507 \{
6508 ~ \meta{key1} \verb|=| \meta{value1} \verb|,|
6509 ~ \meta{key2} \verb|=| \meta{value2} \verb|,| $\cdots$
6510 \}
6511 \end{syntax}
6512 Sets \meta{prop var} to contain key--value pairs given in the second
6513 argument. If duplicate keys appear only the last of the values is kept.
6514
6515 Spaces are trimmed around every \meta{key} and every \meta{value},
6516 and if the result of trimming spaces consists of a single brace
6517 group then a set of outer braces is removed. This enables both the
6518 \meta{key} and the \meta{value} to contain spaces, commas or equal
6519 signs. The \meta{key} is then processed by \cs{tlToStr}.
6520 This function correctly detects the \verb|=| and \verb|,| signs provided they
6521 have the standard category code $12$ or they are active.
6522 \begin{codehigh}
6523 \propSetFromKeyval \lTmpaProp {key1=one,key2=two}
6524 \end{codehigh}
6525 %Notice that in contrast to most keyval lists (\emph{e.g.} those in
6526 %\pkg{l3keys}), each key here \emph{must} be followed with an \texttt{=} sign.
6527 \end{function}
6528
6529 \begin{function}{\propSetEq}
6530 \begin{syntax}
6531 \cs{propSetEq} \meta{property list_1} \meta{property list_2}
6532 \end{syntax}
6533 Sets the content of \meta{property list_1} equal to that of
6534 \meta{property list_2}.
6535 \begin{codehigh}
6536 \propSetFromKeyval \lTmpaProp {key1=one,key2=two,key3=three}
6537 \propSetEq \lTmpbProp \lTmpaProp
6538 \propVarLog \lTmpbProp
6539 \end{codehigh}
6540 \end{function}
6541
6542 \begin{function}{\propClear}
6543 \begin{syntax}
6544 \cs{propClear} \meta{property list}
6545 \end{syntax}
6546 Clears all entries from the \meta{property list}.
6547 \begin{codehigh}
6548 \propClear \lTmpaProp
6549 \end{codehigh}
6550 \end{function}
6551
6552 \begin{function}{\propClearNew}
6553 \begin{syntax}
6554 \cs{propClearNew} \meta{property list}
6555 \end{syntax}
6556 Ensures that the \meta{property list} exists globally by applying \cs{propNew}
6557 if necessary, then applies \cs{propClear} to leave the list empty.
6558 \begin{codehigh}
6559 \propClearNew \lFooSomeProp
6560 \end{codehigh}
6561 \end{function}
6562
6563 \begin{function}{\propConcat}
6564 \begin{syntax}
6565 \cs{propConcat} \meta{prop var_1} \meta{prop var_2} \meta{prop var_3}
6566 \end{syntax}
6567 Combines the key--value pairs of \meta{prop var_2} and
6568 \meta{prop var_3}, and saves the result in \meta{prop var_1}. If a
6569 key appears in both \meta{prop var_2} and \meta{prop var_3} then the
6570 last value, namely the value in \meta{prop var_3} is kept.
6571 \begin{codehigh}
6572 \propSetFromKeyval \lTmpbProp {key1=one,key2=two}
6573 \propSetFromKeyval \lTmpcProp {key3=three,key4=four}
6574 \propConcat \lTmpaProp \lTmpbProp \lTmpcProp
6575 \propVarLog \lTmpaProp
6576 \end{codehigh}
6577 \end{function}
6578
6579 \begin{function}{\propPut}
6580 \begin{syntax}
6581 \cs{propPut} \meta{property list} \Arg{key} \Arg{value}
6582 \end{syntax}
6583 Adds an entry to the \meta{property list} which may be accessed
6584 using the \meta{key} and which has \meta{value}. If the \meta{key}
6585 is already present in the \meta{property list}, the existing entry
6586 is overwritten by the new \meta{value}. Both the \meta{key} and
6587 \meta{value} may contain any \meta{balanced text}. The \meta{key} is
6588 stored after processing with \cs{tlToStr}, meaning that category
6589 codes are ignored.
6590 \begin{codehigh}
6591 \propSetFromKeyval \lTmpaProp {key1=one,key2=two}
6592 \propPut \lTmpaProp {key1} {newone}
6593 \propVarLog \lTmpaProp
6594 \end{codehigh}
6595 \end{function}
6596
6597 \begin{function}{\propPutIfNew}
6598 \begin{syntax}
6599 \cs{propPutIfNew} \meta{property list} \Arg{key} \Arg{value}
6600 \end{syntax}
6601 If the \meta{key} is present in the \meta{property list} then no
6602 action is taken. Otherwise, a new entry is added as described for
6603 \cs{propPut}.
6604 \begin{codehigh}
6605 \propSetFromKeyval \lTmpaProp {key1=one,key2=two}
6606 \propPutIfNew \lTmpaProp {key1} {newone}
6607 \propVarLog \lTmpaProp
6608 \end{codehigh}
6609 \end{function}
6610
6611 \begin{function}{\propPutFromKeyval}
6612 \begin{syntax}
6613 \cs{propPutFromKeyval} \meta{prop var}
6614 \{
6615 ~ \meta{key1} \verb|=| \meta{value1} \verb|,|
6616 ~ \meta{key2} \verb|=| \meta{value2} \verb|,| $\cdots$
6617 \}
6618 \end{syntax}
6619 Updates the \meta{prop var} by adding entries for each key--value
6620 pair given in the second argument. The addition is done through
6621 \cs{propPut}, hence if the \meta{prop var} already contains
6622 some of the keys, the corresponding values are discarded and
6623 replaced by those given in the key--value list. If duplicate keys
6624 appear in the key--value list then only the last of the values is kept.
6625 \begin{codehigh}
6626 \propSetFromKeyval \lTmpaProp {key1=one,key2=two}
6627 \propPutFromKeyval \lTmpaProp {key1=newone,key3=three}
6628 \propVarLog \lTmpaProp
6629 \end{codehigh}
6630 %The function is equivalent to storing the key--value pairs in a
6631 %temporary property variable using \cs{propSetFromKeyval}, then
6632 %combining \meta{prop var} with the temporary variable using
6633 %\cs{propConcat}. In particular, the \meta{keys} and
6634 %\meta{values} are space-trimmed and unbraced as described in
6635 %\cs{propSetFromKeyval}. This function correctly detects
6636 %the \verb|=| and \verb|,| signs provided they
6637 %have the standard category code $12$ or they are active.
6638 \end{function}
6639
6640 \begin{function}{\propVarRemove}
6641 \begin{syntax}
6642 \cs{propVarRemove} \meta{property list} \Arg{key}
6643 \end{syntax}
6644 Removes the entry listed under \meta{key} from the
6645 \meta{property list}. If the \meta{key} is
6646 not found in the \meta{property list} no change occurs,
6647 \emph{i.e} there is no need to test for the existence of a key before
6648 deleting it.
6649 \begin{codehigh}
6650 \propSetFromKeyval \lTmpaProp {key1=one,key2=two,key3=three}
6651 \propVarRemove \lTmpaProp {key2}
6652 \propVarLog \lTmpaProp
6653 \end{codehigh}
6654 \end{function}
6655
6656 \section{Recovering Values from Property Lists}
6657
6658 \begin{function}{\propVarCount}
6659 \begin{syntax}
6660 \cs{propVarCount} \meta{property list}
6661 \end{syntax}
6662 Returns the number of key--value pairs in the \meta{property list}
6663 as an \meta{integer denotation}.
6664 \begin{demohigh}
6665 \propSetFromKeyval \lTmpaProp {key1=one,key2=two,key3=three}
6666 \propVarCount \lTmpaProp
6667 \end{demohigh}
6668 \end{function}
6669
6670 \begin{function}{\propVarItem}
6671 \begin{syntax}
6672 \cs{propVarItem} \meta{property list} \Arg{key}
6673 \end{syntax}
6674 Returns the \meta{value} corresponding to the \meta{key} in
6675 the \meta{property list}. If the \meta{key} is missing,
6676 nothing is returned.
6677 \begin{demohigh}
6678 \propSetFromKeyval \lTmpaProp {key1=one,key2=two,key3=three}
6679 \tlSet \lTmpaTl {\propVarItem \lTmpaProp {key2}}
6680 \tlUse \lTmpaTl
6681 \end{demohigh}
6682 %\begin{texnote}
6683 %This function is slower than the non-expandable analogue
6684 %\cs{prop_get:NnN}.
6685 %The result is returned within the \tn{unexpanded}
6686 %primitive (\cs{exp_not:n}), which means that the \meta{value}
6687 %does not expand further when appearing in an \texttt{x}-type
6688 %or \texttt{e}-type argument expansion.
6689 %\end{texnote}
6690 \end{function}
6691
6692 \begin{function}{\propGet}
6693 \begin{syntax}
6694 \cs{propGet} \meta{property list} \Arg{key} \meta{token list variable}
6695 \end{syntax}
6696 Recovers the \meta{value} stored with \meta{key} from the \meta{property list},
6697 and places this in the \meta{token list variable}.
6698 If the \meta{key} is not found in the
6699 \meta{property list} then the \meta{token list variable} is set
6700 to the special marker \cs{qNoValue}.
6701 The assignment of the \meta{token list variable} is local.
6702 \begin{demohigh}
6703 \propSetFromKeyval \lTmpaProp {key1=one,key2=two,key3=three}
6704 \propGet \lTmpaProp {key2} \lTmpaTl
6705 \tlUse \lTmpaTl
6706 \end{demohigh}
6707 \end{function}
6708
6709 \begin{function}{\propGetT,\propGetF,\propGetTF}
6710 \begin{syntax}
6711 \cs{propGetT} \meta{property list} \Arg{key} \meta{token list variable} \Arg{true code}
6712 \cs{propGetF} \meta{property list} \Arg{key} \meta{token list variable} \Arg{false code}
6713 \cs{propGetTF} \meta{property list} \Arg{key} \meta{token list variable} \Arg{true code} \Arg{false code}
6714 \end{syntax}
6715 If the \meta{key} is not present in the \meta{property list}, leaves
6716 the \meta{false code} in the input stream. The value of the
6717 \meta{token list variable} is not defined in this case and should
6718 not be relied upon. If the \meta{key} is present in the
6719 \meta{property list}, stores the corresponding \meta{value} in the
6720 \meta{token list variable} without removing it from the
6721 \meta{property list}, then leaves the \meta{true code} in the input
6722 stream. The \meta{token list variable} is assigned locally.
6723 \begin{demohigh}
6724 \propSetFromKeyval \lTmpaProp {key1=one,key2=two,key3=three}
6725 \propGetTF \lTmpaProp {key2} \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
6726 \end{demohigh}
6727 \end{function}
6728
6729 \begin{function}{\propPop}
6730 \begin{syntax}
6731 \cs{propPop} \meta{property list} \Arg{key} \meta{token list variable}
6732 \end{syntax}
6733 Recovers the \meta{value} stored with \meta{key} from the \meta{property list},
6734 and places this in the \meta{token list variable}.
6735 If the \meta{key} is not found in the
6736 \meta{property list} then the \meta{token list variable} is set
6737 to the special marker \cs{qNoValue}.
6738 The \meta{key} and \meta{value} are then deleted from the property list.
6739 The assignment of the \meta{token list variable} is local.
6740 \begin{demohigh}
6741 \propSetFromKeyval \lTmpaProp {key1=one,key2=two,key3=three}
6742 \propPop \lTmpaProp {key2} \lTmpaTl
6743 Pop: \tlUse \lTmpaTl.
6744 Count: \propVarCount \lTmpaProp.
6745 \end{demohigh}
6746 \end{function}
6747
6748 \begin{function}{\propPopT,\propPopF,\propPopTF}
6749 \begin{syntax}
6750 \cs{propPopT} \meta{property list} \Arg{key} \meta{token list variable} \Arg{true code}
6751 \cs{propPopF} \meta{property list} \Arg{key} \meta{token list variable} \Arg{false code}
6752 \cs{propPopTF} \meta{property list} \Arg{key} \meta{token list variable} \Arg{true code} \Arg{false code}
6753 \end{syntax}
6754 If the \meta{key} is not present in the \meta{property list}, leaves
6755 the \meta{false code} in the input stream. The value of the
6756 \meta{token list variable} is not defined in this case and should
6757 not be relied upon. If the \meta{key} is present in
6758 the \meta{property list}, pops the corresponding \meta{value}
6759 in the \meta{token list variable}, \emph{i.e.} removes the item from
6760 The \meta{token list variable} is assigned locally.
6761 \begin{demohigh}
6762 \propSetFromKeyval \lTmpaProp {key1=one,key2=two,key3=three}
6763 \propPopTF \lTmpaProp {key2} \lTmpaTl {\prgReturn{Yes}} {\prgReturn{No}}
6764 \end{demohigh}
6765 \end{function}
6766
6767 \section{Mapping over property lists}
6768
6769 %All mappings are done at the current group level, \emph{i.e.} any
6770 %local assignments made by the \meta{function} or \meta{code} discussed
6771 %below remain in effect after the loop.
6772
6773 %\begin{function}{\propVarMapFunction}
6774 %\begin{syntax}
6775 %\cs{propVarMapFunction} \meta{property list} \meta{function}
6776 %\end{syntax}
6777 %Applies \meta{function} to every \meta{entry} stored in the
6778 %\meta{property list}. The \meta{function} receives two arguments for
6779 %each iteration: the \meta{key} and associated \meta{value}.
6780 %The order in which \meta{entries} are returned is not defined and
6781 %should not be relied upon.
6782 %To pass further arguments to the \meta{function}, see
6783 %\cs{prop_map_tokens:Nn}.
6784 %\end{function}
6785
6786 \begin{function}{\propVarMapInline}
6787 \begin{syntax}
6788 \cs{propVarMapInline} \meta{property list} \Arg{inline function}
6789 \end{syntax}
6790 Applies \meta{inline function} to every \meta{entry} stored
6791 within the \meta{property list}. The \meta{inline function} should
6792 consist of code which receives the \meta{key} as \verb|#1| and the
6793 \meta{value} as \verb|#2|.
6794 The order in which \meta{entries} are returned is not defined and
6795 should not be relied upon.
6796 \begin{demohigh}
6797 \IgnoreSpacesOn
6798 \propSetFromKeyval \lTmpkProp {key1=one,key2=two,key3=three}
6799 \tlClear \lTmpaTl
6800 \propVarMapInline \lTmpkProp {
6801 \tlPutRight \lTmpaTl {(#1=#2)}
6802 }
6803 \tlUse \lTmpaTl
6804 \IgnoreSpacesOff
6805 \end{demohigh}
6806 \end{function}
6807
6808 %\begin{function}{\propVarMapTokens}
6809 %\begin{syntax}
6810 %\cs{propVarMapTokens} \meta{property list} \Arg{code}
6811 %\end{syntax}
6812 %Analogue of \cs{propVarMapFunction} which maps several tokens
6813 %instead of a single function. The \meta{code} receives each
6814 %key--value pair in the \meta{property list} as two trailing brace
6815 %groups. For instance,
6816 %\begin{verbatim}
6817 %\propVarMapTokens \lMyProp { \strIfEqT { mykey } }
6818 %\end{verbatim}
6819 %expands to the value corresponding to \texttt{mykey}: for each
6820 %pair in \verb|\lMyProp| the function \cs{strIfEqT} receives
6821 %\texttt{mykey}, the \meta{key} and the \meta{value} as its three
6822 %arguments. For that specific task, \cs{propVarItem} is faster.
6823 %\end{function}
6824
6825 %\begin{function}{\propMapBreak}
6826 %\begin{syntax}
6827 %\cs{propMapBreak}
6828 %\end{syntax}
6829 %Used to terminate a prop map function before all
6830 %entries in the \meta{property list} have been processed. This
6831 %normally takes place within a conditional statement, for example
6832 %\begin{verbatim}
6833 %\prop_map_inline:Nn \l_my_prop
6834 %{
6835 %\str_if_eq:nnTF { #1 } { bingo }
6836 %{ \prop_map_break: }
6837 %{
6838 %Do something useful
6839 %}
6840 %}
6841 %\end{verbatim}
6842 %Use outside of a prop map scenario leads to low
6843 %level \TeX{} errors.
6844 %\begin{texnote}
6845 %When the mapping is broken, additional tokens may be inserted
6846 %before further items are taken
6847 %from the input stream. This depends on the design of the mapping
6848 %function.
6849 %\end{texnote}
6850 %\end{function}
6851
6852 %\begin{function}{\propMapBreakDo}
6853 %\begin{syntax}
6854 %\cs{propMapBreakDo} \Arg{code}
6855 %\end{syntax}
6856 %Used to terminate a prop map function before all
6857 %entries in the \meta{property list} have been processed, inserting
6858 %the \meta{code} after the mapping has ended. This
6859 %normally takes place within a conditional statement, for example
6860 %\begin{verbatim}
6861 %\prop_map_inline:Nn \l_my_prop
6862 %{
6863 %\str_if_eq:nnTF { #1 } { bingo }
6864 %{ \prop_map_break:n { <code> } }
6865 %{
6866 %Do something useful
6867 %}
6868 %}
6869 %\end{verbatim}
6870 %Use outside of a prop map scenario leads to low
6871 %level \TeX{} errors.
6872 %\begin{texnote}
6873 %When the mapping is broken, additional tokens may be inserted
6874 %before the \meta{code} is
6875 %inserted into the input stream.
6876 %This depends on the design of the mapping function.
6877 %\end{texnote}
6878 %\end{function}
6879
6880 \section{Property List Conditionals}
6881
6882 \begin{function}{\propIfExist,\propIfExistT,\propIfExistF,\propIfExistTF}
6883 \begin{syntax}
6884 \cs{propIfExist} \meta{property list}
6885 \cs{propIfExistT} \meta{property list} \Arg{true code}
6886 \cs{propIfExistF} \meta{property list} \Arg{false code}
6887 \cs{propIfExistTF} \meta{property list} \Arg{true code} \Arg{false code}
6888 \end{syntax}
6889 Tests whether the \meta{property list} is currently defined. This does not
6890 check that the \meta{property list} really is a property list variable.
6891 \begin{demohigh}
6892 \propIfExistTF \lTmpaProp {\prgReturn{Yes}} {\prgReturn{No}}
6893 \propIfExistTF \lFooUndefinedProp {\prgReturn{Yes}} {\prgReturn{No}}
6894 \end{demohigh}
6895 \end{function}
6896
6897 \begin{function}{\propVarIfEmpty,\propVarIfEmptyT,\propVarIfEmptyF,\propVarIfEmptyTF}
6898 \begin{syntax}
6899 \cs{propVarIfEmpty} \meta{property list}
6900 \cs{propVarIfEmptyT} \meta{property list} \Arg{true code}
6901 \cs{propVarIfEmptyF} \meta{property list} \Arg{false code}
6902 \cs{propVarIfEmptyTF} \meta{property list} \Arg{true code} \Arg{false code}
6903 \end{syntax}
6904 Tests if the \meta{property list} is empty (containing no entries).
6905 \begin{demohigh}
6906 \propSetFromKeyval \lTmpaProp {key1=one,key2=two}
6907 \propVarIfEmptyTF \lTmpaProp {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
6908 \propClear \lTmpaProp
6909 \propVarIfEmptyTF \lTmpaProp {\prgReturn{Empty}} {\prgReturn{NonEmpty}}
6910 \end{demohigh}
6911 \end{function}
6912
6913 \begin{function}{\propVarIfIn,\propVarIfInT,\propVarIfInF,\propVarIfInTF}
6914 \begin{syntax}
6915 \cs{propVarIfIn} \meta{property list} \Arg{key}
6916 \cs{propVarIfInT} \meta{property list} \Arg{key} \Arg{true code}
6917 \cs{propVarIfInF} \meta{property list} \Arg{key} \Arg{false code}
6918 \cs{propVarIfInTF} \meta{property list} \Arg{key} \Arg{true code} \Arg{false code}
6919 \end{syntax}
6920 Tests if the \meta{key} is present in the \meta{property list},
6921 making the comparison using the method described by \cs{strIfEqTF}.
6922 \begin{demohigh}
6923 \propSetFromKeyval \lTmpaProp {key1=one,key2=two}
6924 \propVarIfInTF \lTmpaProp {key1} {\prgReturn{Yes}} {\prgReturn{Not}}
6925 \propVarIfInTF \lTmpaProp {key3} {\prgReturn{Yes}} {\prgReturn{Not}}
6926 \end{demohigh}
6927 %\begin{texnote}
6928 %This function iterates through every key--value pair in the
6929 %\meta{property list} and is therefore slower than using \cs{propGetTF}.
6930 %\end{texnote}
6931 \end{function}
6932
6933 \chapter{Regular Expressions (\texttt{Regex})}
6934
6935 This module provides regular expression testing,
6936 extraction of submatches, splitting, and replacement, all acting
6937 on token lists. The syntax of regular expressions is mostly a subset
6938 of the \textsc{pcre} syntax (and very close to \textsc{posix}),
6939 with some additions
6940 due to the fact that \TeX{} manipulates tokens rather than characters.
6941 For performance reasons, only a limited set of features are implemented.
6942 Notably, back-references are not supported.
6943
6944 Let us give a few examples. The following example replace the first
6945 occurrence of \enquote{\texttt{at}} with \enquote{\texttt{is}}
6946 in the token list variable \cs{lTmpaTl}.
6947 \begin{demohigh}
6948 \tlSet \lTmpaTl {That cat.}
6949 \regexReplaceOnce {at} {is} \lTmpaTl
6950 \tlUse \lTmpaTl
6951 \end{demohigh}
6952 A more complicated example is
6953 a pattern to emphasize each word and add a comma after it:
6954 \begin{demohigh}
6955 \tlSet \lTmpaTl {That cat.}
6956 \regexReplaceAll {\w+} {\c{underline} \cB\{ \0 \cE\} ,} \lTmpaTl
6957 \tlUse \lTmpaTl
6958 \end{demohigh}
6959 The |\w| sequence represents any \enquote{word} character, and |+|
6960 indicates that the |\w| sequence should be repeated as many times as
6961 possible (at least once), hence matching a word in the input token
6962 list. In the replacement text, |\0| denotes the full match (here, a
6963 word). The command |\underline| is inserted using |\c{underline}|,
6964 and its argument |\0| is put between braces |\cB\{| and |\cE\}|.
6965
6966 If a regular expression is to be used several times,
6967 it can be compiled once, and stored in a regex
6968 variable using \cs{regexSet}. For example,
6969 \begin{codehigh}
6970 \regexNew \lFooRegex
6971 \regexSet \lFooRegex {\c{begin} \cB. (\c[^BE].*) \cE.}
6972 \end{codehigh}
6973 stores in \cs{lFooRegex} a regular expression which matches the
6974 starting marker for an environment: \cs[no-index]{begin}, followed by a
6975 begin-group token (|\cB.|), then any number of tokens which are
6976 neither begin-group nor end-group character tokens (|\c[^BE].*|),
6977 ending with an end-group token (|\cE.|). As explained later,
6978 the parentheses \enquote{capture} the result of |\c[^BE].*|,
6979 giving us access to the name of the environment when doing
6980 replacements.
6981
6982 \section{Regular Expression Variables}
6983
6984 If a regular expression is to be used several times,
6985 it is better to compile it once rather than doing it
6986 each time the regular expression is used. The compiled
6987 regular expression is stored in a variable. All
6988 of this module's functions can be given their
6989 regular expression argument either as an explicit string
6990 or as a compiled regular expression.
6991
6992 \begin{variable}{\lTmpaRegex,\lTmpbRegex,\lTmpcRegex,\lTmpiRegex,\lTmpjRegex,\lTmpkRegex}
6993 Scratch regex variables for local assignment. These are never used by
6994 \verb!function! package, and so are safe for use with any function.
6995 However, they may be overwritten by other non-kernel
6996 code and so should only be used for short-term storage.
6997 \end{variable}
6998
6999 \begin{variable}{\gTmpaRegex,\gTmpbRegex,\gTmpcRegex,\gTmpiRegex,\gTmpjRegex,\gTmpkRegex}
7000 Scratch regex variables for global assignment. These are never used by
7001 \verb!function! package, and so are safe for use with any function.
7002 However, they may be overwritten by other non-kernel
7003 code and so should only be used for short-term storage.
7004 \end{variable}
7005
7006 \begin{function}{\regexNew}
7007 \begin{syntax}
7008 \cs{regexNew} \meta{regex var}
7009 \end{syntax}
7010 Creates a new \meta{regex var} or raises an error if the
7011 name is already taken. The declaration is global. The
7012 \meta{regex var} is initially such that it never matches.
7013 \end{function}
7014
7015 \begin{function}{\regexSet}
7016 \begin{syntax}
7017 \cs{regexSet} \meta{regex var} \Arg{regex}
7018 \end{syntax}
7019 Stores a compiled version of the \meta{regular expression} in the
7020 \meta{regex var}. For instance, this function can be
7021 used as
7022 \begin{codehigh}
7023 \regexNew \lMyRegex
7024 \regexSet \lMyRegex {my\ (simple\ )? reg(ex|ular\ expression)}
7025 \end{codehigh}
7026 \end{function}
7027
7028 \begin{function}{\regexConst}
7029 \begin{syntax}
7030 \cs{regexConst} \meta{regex var} \Arg{regex}
7031 \end{syntax}
7032 Creates a new constant \meta{regex var} or raises an error if the name
7033 is already taken. The value of the \meta{regex var} is set
7034 globally to the compiled version of the \meta{regular expression}.
7035 \end{function}
7036
7037 \begin{function}{\regexLog,\regexVarLog,\regexShow,\regexVarShow}
7038 \begin{syntax}
7039 \cs{regexLog} \Arg{regex}
7040 \cs{regexVarLog} \meta{regex var}
7041 \cs{regexShow} \Arg{regex}
7042 \cs{regexVarShow} \meta{regex var}
7043 \end{syntax}
7044 Displays in the terminal or writes in the log file (respectively)
7045 how \pkg{l3regex} interprets the \meta{regex}. For instance,
7046 \cs{regexShow} \verb+{\A X|Y}+ shows
7047 \begin{codehigh}
7048 +-branch
7049 anchor at start (\A)
7050 char code 88 (X)
7051 +-branch
7052 char code 89 (Y)
7053 \end{codehigh}
7054 indicating that the anchor |\A| only applies to the first branch:
7055 the second branch is not anchored to the beginning of the match.
7056 \end{function}
7057
7058 \section{Regular Expression Matching}
7059
7060 \begin{function}{\regexMatch,\regexMatchT,\regexMatchF,\regexMatchTF}
7061 \begin{syntax}
7062 \cs{regexMatch} \Arg{regex} \Arg{token list}
7063 \cs{regexMatchT} \Arg{regex} \Arg{token list} \Arg{true code}
7064 \cs{regexMatchF} \Arg{regex} \Arg{token list} \Arg{false code}
7065 \cs{regexMatchTF} \Arg{regex} \Arg{token list} \Arg{true code} \Arg{false code}
7066 \end{syntax}
7067 Tests whether the \meta{regular expression} matches any part
7068 of the \meta{token list}. For instance,
7069 \begin{demohigh}
7070 \regexMatchTF {b [cde]*} {abecdcx} {\prgPrint{True}} {\prgPrint{False}}
7071 \regexMatchTF {[b-dq-w]} {example} {\prgPrint{True}} {\prgPrint{False}}
7072 \end{demohigh}
7073 \end{function}
7074
7075 \begin{function}{\regexVarMatch,\regexVarMatchT,\regexVarMatchF,\regexVarMatchTF}
7076 \begin{syntax}
7077 \cs{regexVarMatch} \meta{regex var} \Arg{token list}
7078 \cs{regexVarMatchT} \meta{regex var} \Arg{token list} \Arg{true code}
7079 \cs{regexVarMatchF} \meta{regex var} \Arg{token list} \Arg{false code}
7080 \cs{regexVarMatchTF} \meta{regex var} \Arg{token list} \Arg{true code} \Arg{false code}
7081 \end{syntax}
7082 Tests whether the \meta{regex var} matches any part of the \meta{token list}.
7083 \end{function}
7084
7085 \begin{function}{\regexCount,\regexVarCount}
7086 \begin{syntax}
7087 \cs{regexCount} \Arg{regex} \Arg{token list} \meta{int var}
7088 \cs{regexVarCount} \meta{regex var} \Arg{token list} \meta{int var}
7089 \end{syntax}
7090 Sets \meta{int var} within the current \TeX{} group level
7091 equal to the number of times
7092 \meta{regular expression} appears in \meta{token list}.
7093 The search starts by finding the left-most longest match,
7094 respecting greedy and lazy (non-greedy) operators. Then the search
7095 starts again from the character following the last character
7096 of the previous match, until reaching the end of the token list.
7097 Infinite loops are prevented in the case where the regular expression
7098 can match an empty token list: then we count one match between each
7099 pair of characters. For instance,
7100 \begin{demohigh}
7101 \intNew \lFooInt
7102 \regexCount {(b+|c)} {abbababcbb} \lFooInt
7103 \intUse \lFooInt
7104 \end{demohigh}
7105 \end{function}
7106
7107 \begin{function}{\regexMatchCase}
7108 \begin{syntax}
7109 \cs{regexMatchCase}
7110 ~ ~ |{|
7111 ~ ~ ~ ~ \Arg{regex_1} \Arg{code case_1}
7112 ~ ~ ~ ~ \Arg{regex_2} \Arg{code case_2}
7113 ~ ~ ~ ~ \ldots
7114 ~ ~ ~ ~ \Arg{regex_n} \Arg{code case_n}
7115 ~ ~ |}| \Arg{token list}
7116 \end{syntax}
7117 Determines which of the \meta{regular expressions} matches at the earliest
7118 point in the \meta{token list}, and leaves the corresponding \meta{code_i}.
7119 If several \meta{regex} match starting at the same point,
7120 then the first one in the list is selected and the others are discarded.
7121 Each \meta{regex} can either be given as a regex variable or as an explicit
7122 regular expression.
7123 \par
7124 In detail, for each starting position in the \meta{token list}, each
7125 of the \meta{regex} is searched in turn. If one of them matches
7126 then the corresponding \meta{code} is used and everything else is
7127 discarded, while if none of the \meta{regex} match at a given
7128 position then the next starting position is attempted. If none of
7129 the \meta{regex} match anywhere in the \meta{token list} then
7130 nothing is left in the input stream. Note that this differs from
7131 nested \cs{regexMatch} statements since all \meta{regex} are
7132 attempted at each position rather than attempting to match
7133 \meta{regex_1} at every position before moving on to \meta{regex_2}.
7134 \end{function}
7135
7136 \begin{function}{\regexMatchCaseT}
7137 \begin{syntax}
7138 \cs{regexMatchCaseT}
7139 ~ ~ |{|
7140 ~ ~ ~ ~ \Arg{regex_1} \Arg{code case_1}
7141 ~ ~ ~ ~ \Arg{regex_2} \Arg{code case_2}
7142 ~ ~ ~ ~ \ldots
7143 ~ ~ ~ ~ \Arg{regex_n} \Arg{code case_n}
7144 ~ ~ |}| \Arg{token list}
7145 ~ ~ \Arg{true code}
7146 \end{syntax}
7147 Determines which of the \meta{regular expressions} matches at the
7148 earliest point in the \meta{token list}, and leaves the
7149 corresponding \meta{code_i} followed by the \meta{true code} in the
7150 input stream. If several \meta{regex} match starting at the same
7151 point, then the first one in the list is selected and the others are
7152 discarded. Each \meta{regex} can either be given
7153 as a regex variable or as an explicit regular expression.
7154 \end{function}
7155
7156 \begin{function}{\regexMatchCaseF}
7157 \begin{syntax}
7158 \cs{regexMatchCaseF}
7159 ~ ~ |{|
7160 ~ ~ ~ ~ \Arg{regex_1} \Arg{code case_1}
7161 ~ ~ ~ ~ \Arg{regex_2} \Arg{code case_2}
7162 ~ ~ ~ ~ \ldots
7163 ~ ~ ~ ~ \Arg{regex_n} \Arg{code case_n}
7164 ~ ~ |}| \Arg{token list}
7165 ~ ~ \Arg{false code}
7166 \end{syntax}
7167 Determines which of the \meta{regular expressions} matches at the
7168 earliest point in the \meta{token list}, and leaves the
7169 corresponding \meta{code_i}.
7170 If several \meta{regex} match starting at the same
7171 point, then the first one in the list is selected and the others are
7172 discarded. If none of the \meta{regex} match, the \meta{false code}
7173 is left in the input stream. Each \meta{regex} can either be given
7174 as a regex variable or as an explicit regular expression.
7175 \end{function}
7176
7177 \begin{function}{\regexMatchCaseTF}
7178 \begin{syntax}
7179 \cs{regexMatchCaseTF}
7180 ~ ~ |{|
7181 ~ ~ ~ ~ \Arg{regex_1} \Arg{code case_1}
7182 ~ ~ ~ ~ \Arg{regex_2} \Arg{code case_2}
7183 ~ ~ ~ ~ \ldots
7184 ~ ~ ~ ~ \Arg{regex_n} \Arg{code case_n}
7185 ~ ~ |}| \Arg{token list}
7186 ~ ~ \Arg{true code} \Arg{false code}
7187 \end{syntax}
7188 Determines which of the \meta{regular expressions} matches at the
7189 earliest point in the \meta{token list}, and leaves the
7190 corresponding \meta{code_i} followed by the \meta{true code} in the
7191 input stream. If several \meta{regex} match starting at the same
7192 point, then the first one in the list is selected and the others are
7193 discarded. If none of the \meta{regex} match, the \meta{false code}
7194 is left in the input stream. Each \meta{regex} can either be given
7195 as a regex variable or as an explicit regular expression.
7196 \end{function}
7197
7198 \section{Regular Expression Submatch Extraction}
7199
7200 \begin{function}{\regexExtractOnce,\regexExtractOnceT,\regexExtractOnceF,\regexExtractOnceTF}
7201 \begin{syntax}
7202 \cs{regexExtractOnce} \Arg{regex} \Arg{token list} \meta{seq var}
7203 \cs{regexExtractOnceT} \Arg{regex} \Arg{token list} \meta{seq var} \Arg{true code}
7204 \cs{regexExtractOnceF} \Arg{regex} \Arg{token list} \meta{seq var} \Arg{false code}
7205 \cs{regexExtractOnceTF} \Arg{regex} \Arg{token list} \meta{seq var} \Arg{true code} \Arg{false code}
7206 \end{syntax}
7207 Finds the first match of the \meta{regular expression} in the
7208 \meta{token list}. If it exists, the match is stored as the first
7209 item of the \meta{seq var}, and further items are the contents of
7210 capturing groups, in the order of their opening parenthesis. The
7211 \meta{seq var} is assigned locally. If there is no match, the
7212 \meta{seq var} is cleared. The testing versions insert the
7213 \meta{true code} into the input stream if a match was found, and the
7214 \meta{false code} otherwise.
7215 \par
7216 For instance, assume that you type
7217 \begin{codehigh}
7218 \regexExtractOnce {\A(La)?TeX(!*)\Z} {LaTeX!!!} \lTmpaSeq
7219 \end{codehigh}
7220 Then the regular expression (anchored at the start with |\A| and
7221 at the end with |\Z|) must match the whole token list. The first
7222 capturing group, |(La)?|, matches |La|, and the second capturing
7223 group, |(!*)|, matches |!!!|. Thus, \cs{lTmpaSeq} contains as a result
7224 the items |{LaTeX!!!}|, |{La}|, and |{!!!}|.
7225 Note that the $n$-th item of \cs{lTmpaSeq}, as obtained using
7226 \cs{seqVarItem}, correspond to the submatch numbered $(n-1)$ in
7227 functions such as \cs{regexReplaceOnce}.
7228 \end{function}
7229
7230 \begin{function}{\regexVarExtractOnce,\regexVarExtractOnceT,\regexVarExtractOnceF,\regexVarExtractOnceTF}
7231 \begin{syntax}
7232 \cs{regexVarExtractOnce} \meta{regex var} \Arg{token list} \meta{seq var}
7233 \cs{regexVarExtractOnceT} \meta{regex var} \Arg{token list} \meta{seq var} \Arg{true code}
7234 \cs{regexVarExtractOnceF} \meta{regex var} \Arg{token list} \meta{seq var} \Arg{false code}
7235 \cs{regexVarExtractOnceTF} \meta{regex var} \Arg{token list} \meta{seq var} \Arg{true code} \Arg{false code}
7236 \end{syntax}
7237 Finds the first match of the \meta{regex var} in the
7238 \meta{token list}. If it exists, the match is stored as the first
7239 item of the \meta{seq var}, and further items are the contents of
7240 capturing groups, in the order of their opening parenthesis. The
7241 \meta{seq var} is assigned locally. If there is no match, the
7242 \meta{seq var} is cleared. The testing versions insert the
7243 \meta{true code} into the input stream if a match was found, and the
7244 \meta{false code} otherwise.
7245 \end{function}
7246
7247 \begin{function}{\regexExtractAll,\regexExtractAllT,\regexExtractAllF,\regexExtractAllTF}
7248 \begin{syntax}
7249 \cs{regexExtractAll} \Arg{regex} \Arg{token list} \meta{seq var}
7250 \cs{regexExtractAllT} \Arg{regex} \Arg{token list} \meta{seq var} \Arg{true code}
7251 \cs{regexExtractAllF} \Arg{regex} \Arg{token list} \meta{seq var} \Arg{false code}
7252 \cs{regexExtractAllTF} \Arg{regex} \Arg{token list} \meta{seq var} \Arg{true code} \Arg{false code}
7253 \end{syntax}
7254 Finds all matches of the \meta{regular expression}
7255 in the \meta{token list}, and stores all the submatch information
7256 in a single sequence (concatenating the results of
7257 multiple \cs{regexExtractOnce} calls).
7258 The \meta{seq var} is assigned locally. If there is no match,
7259 the \meta{seq var} is cleared.
7260 The testing versions insert the \meta{true code} into the input
7261 stream if a match was found, and the \meta{false code} otherwise.
7262 For instance, assume that you type
7263 \begin{codehigh}
7264 \regexExtractAll {\w+} {Hello, world!} \lTmpaSeq
7265 \end{codehigh}
7266 Then the regular expression matches twice, the resulting
7267 sequence contains the two items |{Hello}| and |{world}|.
7268 \end{function}
7269
7270 \begin{function}{\regexVarExtractAll,\regexVarExtractAllT,\regexVarExtractAllF,\regexVarExtractAllTF}
7271 \begin{syntax}
7272 \cs{regexVarExtractAll} \meta{regex var} \Arg{token list} \meta{seq var}
7273 \cs{regexVarExtractAllT} \meta{regex var} \Arg{token list} \meta{seq var} \Arg{true code}
7274 \cs{regexVarExtractAllF} \meta{regex var} \Arg{token list} \meta{seq var} \Arg{false code}
7275 \cs{regexVarExtractAllTF} \meta{regex var} \Arg{token list} \meta{seq var} \Arg{true code} \Arg{false code}
7276 \end{syntax}
7277 Finds all matches of the \meta{regex var}
7278 in the \meta{token list}, and stores all the submatch information
7279 in a single sequence (concatenating the results of
7280 multiple \cs{regexVarExtractOnce} calls).
7281 The \meta{seq var} is assigned locally. If there is no match,
7282 the \meta{seq var} is cleared.
7283 The testing versions insert the \meta{true code} into the input
7284 stream if a match was found, and the \meta{false code} otherwise.
7285 \end{function}
7286
7287 \begin{function}{\regexSplit,\regexSplitT,\regexSplitF,\regexSplitTF}
7288 \begin{syntax}
7289 \cs{regexSplit} \Arg{regular expression} \Arg{token list} \meta{seq var}
7290 \cs{regexSplitT} \Arg{regular expression} \Arg{token list} \meta{seq var} \Arg{true code}
7291 \cs{regexSplitF} \Arg{regular expression} \Arg{token list} \meta{seq var} \Arg{false code}
7292 \cs{regexSplitTF} \Arg{regular expression} \Arg{token list} \meta{seq var} \Arg{true code} \Arg{false code}
7293 \end{syntax}
7294 Splits the \meta{token list} into a sequence of parts, delimited by
7295 matches of the \meta{regular expression}. If the \meta{regular expression}
7296 has capturing groups, then the token lists that they match are stored as
7297 items of the sequence as well. The assignment to \meta{seq var} is local.
7298 If no match is found the resulting \meta{seq var} has the
7299 \meta{token list} as its sole item. If the \meta{regular expression}
7300 matches the empty token list, then the \meta{token list} is split
7301 into single tokens.
7302 The testing versions insert the \meta{true code} into the input
7303 stream if a match was found, and the \meta{false code} otherwise.
7304 For example, after
7305 \begin{codehigh}
7306 \seqNew \lPathSeq
7307 \regexSplit {/} {the/path/for/this/file.tex} \lPathSeq
7308 \end{codehigh}
7309 the sequence |\lPathSeq| contains the items |{the}|, |{path}|,
7310 |{for}|, |{this}|, and |{file.tex}|.
7311 \end{function}
7312
7313 \begin{function}{\regexVarSplit,\regexVarSplitT,\regexVarSplitF,\regexVarSplitTF}
7314 \begin{syntax}
7315 \cs{regexVarSplit} \meta{regex var} \Arg{token list} \meta{seq var}
7316 \cs{regexVarSplitT} \meta{regex var} \Arg{token list} \meta{seq var} \Arg{true code}
7317 \cs{regexVarSplitF} \meta{regex var} \Arg{token list} \meta{seq var} \Arg{false code}
7318 \cs{regexVarSplitTF} \meta{regex var} \Arg{token list} \meta{seq var} \Arg{true code} \Arg{false code}
7319 \end{syntax}
7320 Splits the \meta{token list} into a sequence of parts, delimited by
7321 matches of the \meta{regular expression}. If the \meta{regex var}
7322 has capturing groups, then the token lists that they match are stored as
7323 items of the sequence as well. The assignment to \meta{seq var} is local.
7324 If no match is found the resulting \meta{seq var} has the
7325 \meta{token list} as its sole item. If the \meta{regular expression}
7326 matches the empty token list, then the \meta{token list} is split
7327 into single tokens.
7328 The testing versions insert the \meta{true code} into the input
7329 stream if a match was found, and the \meta{false code} otherwise.
7330 \end{function}
7331
7332 \section{Regular Expression Replacement}
7333
7334 \begin{function}{\regexReplaceOnce,\regexReplaceOnceT,\regexReplaceOnceT,\regexReplaceOnceTF}
7335 \begin{syntax}
7336 \cs{regexReplaceOnce} \Arg{regular expression} \Arg{replacement} \meta{tl var}
7337 \cs{regexReplaceOnceT} \Arg{regular expression} \Arg{replacement} \meta{tl var} \Arg{true code}
7338 \cs{regexReplaceOnceF} \Arg{regular expression} \Arg{replacement} \meta{tl var} \Arg{false code}
7339 \cs{regexReplaceOnceTF} \Arg{regular expression} \Arg{replacement} \meta{tl var} \Arg{true code} \Arg{false code}
7340 \end{syntax}
7341 Searches for the \meta{regular expression} in the contents of the
7342 \meta{tl var} and replaces the first match with the
7343 \meta{replacement}. In the \meta{replacement},
7344 |\0| represents the full match, |\1| represent the contents of the
7345 first capturing group, |\2| of the second, \emph{etc.}
7346 The result is assigned locally to \meta{tl var}.
7347 \end{function}
7348
7349 \begin{function}{\regexReplaceOnce,\regexReplaceOnceT,\regexReplaceOnceT,\regexReplaceOnceTF}
7350 \begin{syntax}
7351 \cs{regexVarReplaceOnce} \meta{regex var} \Arg{replacement} \meta{tl var}
7352 \cs{regexVarReplaceOnceT} \meta{regex var} \Arg{replacement} \meta{tl var} \Arg{true code}
7353 \cs{regexVarReplaceOnceF} \meta{regex var} \Arg{replacement} \meta{tl var} \Arg{false code}
7354 \cs{regexVarReplaceOnceTF} \meta{regex var} \Arg{replacement} \meta{tl var} \Arg{true code} \Arg{false code}
7355 \end{syntax}
7356 Searches for the \meta{regex var} in the contents of the
7357 \meta{tl var} and replaces the first match with the
7358 \meta{replacement}. In the \meta{replacement},
7359 |\0| represents the full match, |\1| represent the contents of the
7360 first capturing group, |\2| of the second, \emph{etc.}
7361 The result is assigned locally to \meta{tl var}.
7362 \end{function}
7363
7364 \begin{function}{\regexReplaceAll,\regexReplaceAllT,\regexReplaceAllF,\regexReplaceAllTF}
7365 \begin{syntax}
7366 \cs{regexReplaceAll} \Arg{regular expression} \Arg{replacement} \meta{tl var}
7367 \cs{regexReplaceAllT} \Arg{regular expression} \Arg{replacement} \meta{tl var} \Arg{true code}
7368 \cs{regexReplaceAllF} \Arg{regular expression} \Arg{replacement} \meta{tl var} \Arg{false code}
7369 \cs{regexReplaceAllTF} \Arg{regular expression} \Arg{replacement} \meta{tl var} \Arg{true code} \Arg{false code}
7370 \end{syntax}
7371 Replaces all occurrences of the \meta{regex var} in the
7372 contents of the \meta{tl var}
7373 by the \meta{replacement}, where |\0| represents
7374 the full match, |\1| represent the contents of the first capturing
7375 group, |\2| of the second, \emph{etc.} Every match is treated
7376 independently, and matches cannot overlap. The result is assigned
7377 locally to \meta{tl~var}.
7378 \end{function}
7379
7380 \begin{function}{\regexVarReplaceAll,\regexVarReplaceAllT,\regexVarReplaceAllF,\regexVarReplaceAllTF}
7381 \begin{syntax}
7382 \cs{regexVarReplaceAll} \meta{regex var} \Arg{replacement} \meta{tl var}
7383 \cs{regexVarReplaceAllT} \meta{regex var} \Arg{replacement} \meta{tl var} \Arg{true code}
7384 \cs{regexVarReplaceAllF} \meta{regex var} \Arg{replacement} \meta{tl var} \Arg{false code}
7385 \cs{regexVarReplaceAllTF} \meta{regex var} \Arg{replacement} \meta{tl var} \Arg{true code} \Arg{false code}
7386 \end{syntax}
7387 Replaces all occurrences of the \meta{regular expression} in the
7388 contents of the \meta{tl var}
7389 by the \meta{replacement}, where |\0| represents
7390 the full match, |\1| represent the contents of the first capturing
7391 group, |\2| of the second, \emph{etc.} Every match is treated
7392 independently, and matches cannot overlap. The result is assigned
7393 locally to \meta{tl var}.
7394 \end{function}
7395
7396 \begin{function}{\regexReplaceCaseOnce}
7397 \begin{syntax}
7398 \cs{regexReplaceCaseOnce}
7399 ~ ~ |{|
7400 ~ ~ ~ ~ \Arg{regex_1} \Arg{replacement_1}
7401 ~ ~ ~ ~ \Arg{regex_2} \Arg{replacement_2}
7402 ~ ~ ~ ~ \ldots
7403 ~ ~ ~ ~ \Arg{regex_n} \Arg{replacement_n}
7404 ~ ~ |}| \meta{tl var}
7405 \end{syntax}
7406 Replaces the earliest match of the regular expression
7407 "(?|"\meta{regex_1}"|"\dots"|"\meta{regex_n}")" in the \meta{token list variable}
7408 by the \meta{replacement} corresponding to which \meta{regex_i} matched.
7409 If none of the \meta{regex} match, then the
7410 \meta{tl var} is not modified. Each \meta{regex} can either be given as a regex
7411 variable or as an explicit regular expression.
7412 \par
7413 In detail, for each starting position in the \meta{token list}, each
7414 of the \meta{regex} is searched in turn. If one of them matches
7415 then it is replaced by the corresponding \meta{replacement} as
7416 described for \cs{regexReplaceOnce}. This is equivalent to
7417 checking with \cs{regexMatchCase} which \meta{regex} matches,
7418 then performing the replacement with \cs{regexReplaceOnce}.
7419 \end{function}
7420
7421 \begin{function}{\regexReplaceCaseOnceT}
7422 \begin{syntax}
7423 \cs{regexReplaceCaseOnceT}
7424 ~ ~ |{|
7425 ~ ~ ~ ~ \Arg{regex_1} \Arg{replacement_1}
7426 ~ ~ ~ ~ \Arg{regex_2} \Arg{replacement_2}
7427 ~ ~ ~ ~ \ldots
7428 ~ ~ ~ ~ \Arg{regex_n} \Arg{replacement_n}
7429 ~ ~ |}| \meta{tl var}
7430 ~ ~ \Arg{true code}
7431 \end{syntax}
7432 Replaces the earliest match of the regular expression
7433 "(?|"\meta{regex_1}"|"\dots"|"\meta{regex_n}")" in the \meta{token list variable}
7434 by the \meta{replacement} corresponding to which
7435 \meta{regex_i} matched, then leaves the \meta{true code} in the
7436 input stream. If none of the \meta{regex} match, then the
7437 \meta{tl var} is not modified. Each \meta{regex} can either be given as a regex
7438 variable or as an explicit regular expression.
7439 \end{function}
7440
7441 \begin{function}{\regexReplaceCaseOnceF}
7442 \begin{syntax}
7443 \cs{regexReplaceCaseOnceF}
7444 ~ ~ |{|
7445 ~ ~ ~ ~ \Arg{regex_1} \Arg{replacement_1}
7446 ~ ~ ~ ~ \Arg{regex_2} \Arg{replacement_2}
7447 ~ ~ ~ ~ \ldots
7448 ~ ~ ~ ~ \Arg{regex_n} \Arg{replacement_n}
7449 ~ ~ |}| \meta{tl var}
7450 ~ ~ \Arg{false code}
7451 \end{syntax}
7452 Replaces the earliest match of the regular expression
7453 "(?|"\meta{regex_1}"|"\dots"|"\meta{regex_n}")" in the \meta{token list variable}
7454 by the \meta{replacement} corresponding to which
7455 \meta{regex_i} matched. If none of the \meta{regex} match, then the
7456 \meta{tl var} is not modified, and the \meta{false code} is left in
7457 the input stream. Each \meta{regex} can either be given as a regex
7458 variable or as an explicit regular expression.
7459 \end{function}
7460
7461 \begin{function}{\regexReplaceCaseOnceTF}
7462 \begin{syntax}
7463 \cs{regexReplaceCaseOnceTF}
7464 ~ ~ |{|
7465 ~ ~ ~ ~ \Arg{regex_1} \Arg{replacement_1}
7466 ~ ~ ~ ~ \Arg{regex_2} \Arg{replacement_2}
7467 ~ ~ ~ ~ \ldots
7468 ~ ~ ~ ~ \Arg{regex_n} \Arg{replacement_n}
7469 ~ ~ |}| \meta{tl var}
7470 ~ ~ \Arg{true code} \Arg{false code}
7471 \end{syntax}
7472 Replaces the earliest match of the regular expression
7473 "(?|"\meta{regex_1}"|"\dots"|"\meta{regex_n}")" in the \meta{token list variable}
7474 by the \meta{replacement} corresponding to which
7475 \meta{regex_i} matched, then leaves the \meta{true code} in the
7476 input stream. If none of the \meta{regex} match, then the
7477 \meta{tl var} is not modified, and the \meta{false code} is left in
7478 the input stream. Each \meta{regex} can either be given as a regex
7479 variable or as an explicit regular expression.
7480 \end{function}
7481
7482 \begin{function}{\regexReplaceCaseAll}
7483 \begin{syntax}
7484 \cs{regexReplaceCaseAll}
7485 ~ ~ |{|
7486 ~ ~ ~ ~ \Arg{regex_1} \Arg{replacement_1}
7487 ~ ~ ~ ~ \Arg{regex_2} \Arg{replacement_2}
7488 ~ ~ ~ ~ \ldots
7489 ~ ~ ~ ~ \Arg{regex_n} \Arg{replacement_n}
7490 ~ ~ |}| \meta{tl var}
7491 \end{syntax}
7492 Replaces all occurrences of all \meta{regex} in the \meta{token~list}
7493 by the corresponding \meta{replacement}. Every match is
7494 treated independently, and matches cannot overlap. The result is
7495 assigned locally to \meta{tl var}.
7496 \par
7497 In detail, for each starting position in the \meta{token list}, each
7498 of the \meta{regex} is searched in turn. If one of them matches
7499 then it is replaced by the corresponding \meta{replacement}, and the
7500 search resumes at the position that follows this match (and
7501 replacement). For instance
7502 %% FIXME
7503 %\begin{codehigh}
7504 %\tlSet \lTmpaTl {Hello, world!}
7505 %\regexReplaceCaseAll
7506 % {
7507 % {[A-Za-z]+} {``\0''}
7508 % {\b} {---}
7509 % {.} {[\0]}
7510 % } \lTmpaTl
7511 %\end{codehigh}
7512 \begin{codehigh}
7513 \tlSet \lTmpaTl {Hello, world!}
7514 \regexReplaceCaseAll
7515 {
7516 {[A-Za-z]+} {``\0''}
7517 {\b} {---}
7518 {.} {[\0]}
7519 } \lTmpaTl
7520 \end{codehigh}
7521 results in \cs{lTmpaTl} having the contents
7522 \verb*|``Hello''---[,][ ]``world''---[!]|. Note in particular that
7523 the word-boundary assertion |\b| did not match at the start of words
7524 because the case |[A-Za-z]+| matched at these positions. To change
7525 this, one could simply swap the order of the two cases in the
7526 argument of \cs{regexReplaceCaseAll}.
7527 \end{function}
7528
7529 \begin{function}{\regexReplaceCaseAllT}
7530 \begin{syntax}
7531 \cs{regexReplaceCaseAllT}
7532 ~ ~ |{|
7533 ~ ~ ~ ~ \Arg{regex_1} \Arg{replacement_1}
7534 ~ ~ ~ ~ \Arg{regex_2} \Arg{replacement_2}
7535 ~ ~ ~ ~ \ldots
7536 ~ ~ ~ ~ \Arg{regex_n} \Arg{replacement_n}
7537 ~ ~ |}| \meta{tl var}
7538 ~ ~ \Arg{true code}
7539 \end{syntax}
7540 Replaces all occurrences of all \meta{regex} in the \meta{token~list}
7541 by the corresponding \meta{replacement}. Every match is
7542 treated independently, and matches cannot overlap. The result is
7543 assigned locally to \meta{tl var}, and the \meta{true code}
7544 is left in the input stream if any replacement was made.
7545 \end{function}
7546
7547 \begin{function}{\regexReplaceCaseAllF}
7548 \begin{syntax}
7549 \cs{regexReplaceCaseAllF}
7550 ~ ~ |{|
7551 ~ ~ ~ ~ \Arg{regex_1} \Arg{replacement_1}
7552 ~ ~ ~ ~ \Arg{regex_2} \Arg{replacement_2}
7553 ~ ~ ~ ~ \ldots
7554 ~ ~ ~ ~ \Arg{regex_n} \Arg{replacement_n}
7555 ~ ~ |}| \meta{tl var}
7556 ~ ~ \Arg{false code}
7557 \end{syntax}
7558 Replaces all occurrences of all \meta{regex} in the \meta{token~list}
7559 by the corresponding \meta{replacement}. Every match is
7560 treated independently, and matches cannot overlap. The result is
7561 assigned locally to \meta{tl var}, and the \meta{false code} is left
7562 in the input stream if not any replacement was made.
7563 \end{function}
7564
7565 \begin{function}{\regexReplaceCaseAllTF}
7566 \begin{syntax}
7567 \cs{regexReplaceCaseAllTF}
7568 ~ ~ |{|
7569 ~ ~ ~ ~ \Arg{regex_1} \Arg{replacement_1}
7570 ~ ~ ~ ~ \Arg{regex_2} \Arg{replacement_2}
7571 ~ ~ ~ ~ \ldots
7572 ~ ~ ~ ~ \Arg{regex_n} \Arg{replacement_n}
7573 ~ ~ |}| \meta{tl var}
7574 ~ ~ \Arg{true code} \Arg{false code}
7575 \end{syntax}
7576 Replaces all occurrences of all \meta{regex} in the \meta{token
7577 list} by the corresponding \meta{replacement}. Every match is
7578 treated independently, and matches cannot overlap. The result is
7579 assigned locally to \meta{tl var}, and the \meta{true code} or
7580 \meta{false code} is left in the input stream depending on whether
7581 any replacement was made or not.
7582 \end{function}
7583
7584 \section{Syntax of Regular Expressions}
7585
7586 \subsection{Regular Expression Examples}
7587
7588 We start with a few examples, and encourage the reader to apply
7589 \cs{regexShow} to these regular expressions.
7590 \begin{itemize}
7591 \item |Cat| matches the word \enquote{Cat} capitalized in this way,
7592 but also matches the beginning of the word \enquote{Cattle}: use
7593 |\bCat\b| to match a complete word only.
7594 \item |[abc]| matches one letter among \enquote{a}, \enquote{b},
7595 \enquote{c}; the pattern \verb"(a|b|c)" matches the same three
7596 possible letters (but see the discussion of submatches below).
7597 \item |[A-Za-z]*| matches any number (due to the quantifier
7598 \verb"*") of Latin letters (not accented).
7599 \item |\c{[A-Za-z]*}| matches a control sequence made of Latin
7600 letters.
7601 \item |\_[^\_]*\_| matches an underscore, any number of characters
7602 other than underscore, and another underscore; it is equivalent to
7603 |\_.*?\_| where |.| matches arbitrary characters and the
7604 lazy quantifier |*?| means to match as few characters as
7605 possible, thus avoiding matching underscores.
7606 \item |[\+\-]?\d+| matches an explicit integer with at most one
7607 sign.
7608 \item \verb*"[\+\-\ ]*\d+\ *" matches an explicit integer with any
7609 number of $+$ and $-$ signs, with spaces allowed except within the
7610 mantissa, and surrounded by spaces.
7611 \item \verb*"[\+\-\ ]*(\d+|\d*\.\d+)\ *" matches an explicit integer or
7612 decimal number; using \verb*"[.,]" instead of \verb*"\." would allow
7613 the comma as a decimal marker.
7614 \item
7615 \verb*"[\+\-\ ]*(\d+|\d*\.\d+)\ *((?i)pt|in|[cem]m|ex|[bs]p|[dn]d|[pcn]c)\ *"
7616 \allowbreak matches an explicit dimension with any unit that \TeX{} knows, where
7617 \verb*"(?i)" means to treat lowercase and uppercase letters
7618 identically.
7619 \item \verb*"[\+\-\ ]*((?i)nan|inf|(\d+|\d*\.\d+)(\ *e[\+\-\ ]*\d+)?)\ *"
7620 matches an explicit floating point number or the special values
7621 \verb*"nan" and \verb*"inf" (with signs and spaces allowed).
7622 \item \verb*"[\+\-\ ]*(\d+|\cC.)\ *" matches an explicit integer or
7623 control sequence (without checking whether it is an integer
7624 variable).
7625 \item |\G.*?\K| at the beginning of a regular expression matches and
7626 discards (due to |\K|) everything between the end of the previous
7627 match (|\G|) and what is matched by the rest of the regular
7628 expression; this is useful in \cs{regexReplaceAll} when the
7629 goal is to extract matches or submatches in a finer way than with
7630 \cs{regexExtractAll}.
7631 \end{itemize}
7632 While it is impossible for a regular expression to match only integer
7633 expressions, \newline\verb*"[\+\-\(]*\d+\)*([\+\-*/][\+\-\(]*\d+\)*)*" matches among
7634 other things all valid integer expressions (made only with explicit
7635 integers). One should follow it with further testing.
7636
7637 \subsection{Characters in Regular Expressions}
7638
7639 Most characters match exactly themselves,
7640 with an arbitrary category code. Some characters are
7641 special and must be escaped with a backslash (\emph{e.g.}, |\*|
7642 matches a star character). Some escape sequences of
7643 the form backslash--letter also have a special meaning
7644 (for instance |\d| matches any digit). As a rule,
7645 \begin{itemize}
7646 \item every alphanumeric character (\texttt{A}--\texttt{Z},
7647 \texttt{a}--\texttt{z}, \texttt{0}--\texttt{9}) matches
7648 exactly itself, and should not be escaped, because
7649 |\A|, |\B|, \ldots{} have special meanings;
7650 \item non-alphanumeric printable ASCII characters can (and should)
7651 always be escaped: many of them have special meanings (\emph{e.g.},
7652 use |\(|, |\)|, |\?|, |\.|, |\^|);
7653 \item spaces should always be escaped (even in character
7654 classes);
7655 \item any other character may be escaped or not, without any
7656 effect: both versions match exactly that character.
7657 \end{itemize}
7658 Note that these rules play nicely with the fact that many
7659 non-alphanumeric characters are difficult to input into \TeX{}
7660 under normal category codes. For instance, |\\abc\%|
7661 matches the characters |\abc%| (with arbitrary category codes),
7662 but does not match the control sequence |\abc| followed by a
7663 percent character. Matching control sequences can be done
7664 using the |\c|\Arg{regex} syntax (see below).
7665
7666 Any special character which appears at a place where its special
7667 behaviour cannot apply matches itself instead (for instance, a
7668 quantifier appearing at the beginning of a string), after raising a
7669 warning.
7670
7671 Characters.
7672 \begin{l3regex-syntax}
7673 \item[\\x\{hh\ldots{}\}] Character with hex code \texttt{hh\ldots{}}
7674 \item[\\xhh] Character with hex code \texttt{hh}.
7675 \item[\\a] Alarm (hex 07).
7676 \item[\\e] Escape (hex 1B).
7677 \item[\\f] Form-feed (hex 0C).
7678 \item[\\n] New line (hex 0A).
7679 \item[\\r] Carriage return (hex 0D).
7680 \item[\\t] Horizontal tab (hex 09).
7681 \end{l3regex-syntax}
7682
7683 \subsection{Characters Classes}
7684
7685 Character types.
7686 \begin{l3regex-syntax}
7687 \item[.] A single period matches any token.
7688 \item[\\d] Any decimal digit.
7689 \item[\\h] Any horizontal space character,
7690 equivalent to |[\ \^^I]|: space and tab.
7691 \item[\\s] Any space character,
7692 equivalent to |[\ \^^I\^^J\^^L\^^M]|.
7693 \item[\\v] Any vertical space character,
7694 equivalent to |[\^^J\^^K\^^L\^^M]|. Note that |\^^K| is a vertical space,
7695 but not a space, for compatibility with Perl.
7696 \item[\\w] Any word character, \emph{i.e.},
7697 alphanumerics and underscore, equivalent to the explicit
7698 class |[A-Za-z0-9\_]|.
7699 \item[\\D] Any token not matched by |\d|.
7700 \item[\\H] Any token not matched by |\h|.
7701 \item[\\N] Any token other than the |\n| character (hex 0A).
7702 \item[\\S] Any token not matched by |\s|.
7703 \item[\\V] Any token not matched by |\v|.
7704 \item[\\W] Any token not matched by |\w|.
7705 \end{l3regex-syntax}
7706 Of those, |.|, |\D|, |\H|, |\N|, |\S|, |\V|, and |\W| match arbitrary
7707 control sequences.
7708
7709 Character classes match exactly one token in the subject.
7710 \begin{l3regex-syntax}
7711 \item[{[\ldots{}]}] Positive character class.
7712 Matches any of the specified tokens.
7713 \item[{[\char`\^\ldots{}]}] Negative character class.
7714 Matches any token other than the specified characters.
7715 \item[{x-y}] Within a character class, this denotes a range (can be
7716 used with escaped characters).
7717 \item[{[:\meta{name}:]}] Within a character class (one more set of
7718 brackets), this denotes the \textsc{posix} character class
7719 \meta{name}, which can be \texttt{alnum}, \texttt{alpha},
7720 \texttt{ascii}, \texttt{blank}, \texttt{cntrl}, \texttt{digit},
7721 \texttt{graph}, \texttt{lower}, \texttt{print}, \texttt{punct},
7722 \texttt{space}, \texttt{upper}, \texttt{word}, or \texttt{xdigit}.
7723 \item[{[:\char`\^\meta{name}:]}] Negative \textsc{posix} character class.
7724 \end{l3regex-syntax}
7725 For instance, |[a-oq-z\cC.]| matches any lowercase latin letter
7726 except |p|, as well as control sequences (see below for a description
7727 of |\c|).
7728
7729 In character classes, only |[|, |^|, |-|, |]|, |\| and spaces are
7730 special, and should be escaped. Other non-alphanumeric characters can
7731 still be escaped without harm. Any escape sequence which matches a
7732 single character (|\d|, |\D|, \emph{etc.}) is supported in character
7733 classes. If the first character is |^|, then
7734 the meaning of the character class is inverted; |^| appearing anywhere
7735 else in the range is not special. If the first character (possibly
7736 following a leading |^|) is |]| then it does not need to be escaped
7737 since ending the range there would make it empty.
7738 Ranges of characters
7739 can be expressed using |-|, for instance, |[\D 0-5]| and |[^6-9]| are
7740 equivalent.
7741
7742 \subsection{Structure: Alternatives, Groups, Repetitions}
7743
7744 Quantifiers (repetition).
7745 \begin{l3regex-syntax}
7746 \item[?] $0$ or $1$, greedy.
7747 \item[??] $0$ or $1$, lazy.
7748 \item[*] $0$ or more, greedy.
7749 \item[*?] $0$ or more, lazy.
7750 \item[+] $1$ or more, greedy.
7751 \item[+?] $1$ or more, lazy.
7752 \item[\{$n$\}] Exactly $n$.
7753 \item[\{$n,$\}] $n$ or more, greedy.
7754 \item[\{$n,$\}?] $n$ or more, lazy.
7755 \item[\{$n,m$\}] At least $n$, no more than $m$, greedy.
7756 \item[\{$n,m$\}?] At least $n$, no more than $m$, lazy.
7757 \end{l3regex-syntax}
7758 For greedy quantifiers the regex code will first investigate matches
7759 that involve as many repetitions as possible, while for lazy
7760 quantifiers it investigates matches with as few repetitions as
7761 possible first.
7762
7763 Alternation and capturing groups.
7764 \begin{l3regex-syntax}
7765 \item[A\char`|B\char`|C] Either one of \texttt{A}, \texttt{B},
7766 or \texttt{C}, investigating \texttt{A} first.
7767 \item[(\ldots{})] Capturing group.
7768 \item[(?:\ldots{})] Non-capturing group.
7769 \item[(?\char`|\ldots{})] Non-capturing group which resets
7770 the group number for capturing groups in each alternative.
7771 The following group is numbered with the first unused
7772 group number.
7773 \end{l3regex-syntax}
7774
7775 Capturing groups are a means of extracting information about the
7776 match. Parenthesized groups are labelled in the order of their
7777 opening parenthesis, starting at $1$. The contents of those groups
7778 corresponding to the \enquote{best} match (leftmost longest)
7779 can be extracted and stored in a sequence of token lists using for
7780 instance \cs{regexExtractOnceTF}.
7781
7782 The |\K| escape sequence resets the beginning of the match to the
7783 current position in the token list. This only affects what is reported
7784 as the full match. For instance,
7785 \begin{codehigh}
7786 \regexExtractAll {a \K .} {a123aaxyz} \lFooSeq
7787 \end{codehigh}
7788 results in \cs{lFooSeq} containing the items |{1}| and |{a}|: the
7789 true matches are |{a1}| and |{aa}|, but they are trimmed by the use of
7790 |\K|. The |\K| command does not affect capturing groups: for instance,
7791 \begin{codehigh}
7792 \regexExtractOnce {(. \K c)+ \d} {acbc3} \lFooSeq
7793 \end{codehigh}
7794 results in \cs{lFooSeq} containing the items |{c3}| and |{bc}|: the
7795 true match is |{acbc3}|, with first submatch |{bc}|, but |\K| resets
7796 the beginning of the match to the last position where it appears.
7797
7798 \subsection{Matching Exact Tokens}
7799
7800 The |\c| escape sequence allows to test the category code of tokens,
7801 and match control sequences. Each character category is represented
7802 by a single uppercase letter:
7803 \begin{itemize}
7804 \item |C| for control sequences;
7805 \item |B| for begin-group tokens;
7806 \item |E| for end-group tokens;
7807 \item |M| for math shift;
7808 \item |T| for alignment tab tokens;
7809 \item |P| for macro parameter tokens;
7810 \item |U| for superscript tokens (up);
7811 \item |D| for subscript tokens (down);
7812 \item |S| for spaces;
7813 \item |L| for letters;
7814 \item |O| for others; and
7815 \item |A| for active characters.
7816 \end{itemize}
7817 The |\c| escape sequence is used as follows.
7818 \begin{l3regex-syntax}
7819 \item[\\c\Arg{regex}] A control sequence whose csname matches the
7820 \meta{regex}, anchored at the beginning and end, so that |\c{begin}|
7821 matches exactly \cs[no-index]{begin}, and nothing else.
7822 \item[\\cX] Applies to the next object, which can be a character,
7823 escape character sequence such as |\x{0A}|, character class, or
7824 group, and forces this object to only match tokens with category
7825 |X| (any of |CBEMTPUDSLOA|. For instance, |\cL[A-Z\d]| matches
7826 uppercase letters and digits of category code letter, |\cC.|
7827 matches any control sequence, and |\cO(abc)| matches |abc| where
7828 each character has category other.\footnote{This last example also
7829 captures \enquote{\texttt{abc}} as a regex group; to avoid this
7830 use a non-capturing group \texttt{\textbackslash cO(?:abc)}.}
7831 \item[{\\c[XYZ]}] Applies to the next object, and forces it to only
7832 match tokens with category |X|, |Y|, or |Z| (each being any of
7833 |CBEMTPUDSLOA|). For instance, |\c[LSO](..)| matches two tokens of
7834 category letter, space, or other.
7835 \item[{\\c[\char`\^XYZ]}] Applies to the next object and prevents it
7836 from matching any token with category |X|, |Y|, or |Z| (each being
7837 any of |CBEMTPUDSLOA|). For instance, |\c[^O]\d| matches digits
7838 which have any category different from other.
7839 \end{l3regex-syntax}
7840 The category code tests can be used inside classes; for instance,
7841 |[\cO\d \c[LO][A-F]]| matches what \TeX{} considers as hexadecimal
7842 digits, namely digits with category other, or uppercase letters from
7843 |A| to |F| with category either letter or other. Within a group
7844 affected by a category code test, the outer test can be overridden by
7845 a nested test: for instance, |\cL(ab\cO\*cd)| matches |ab*cd| where
7846 all characters are of category letter, except |*| which has category
7847 other.
7848
7849 The |\u| escape sequence allows to insert the contents of a token list
7850 directly into a regular expression or a replacement, avoiding the need
7851 to escape special characters. Namely, |\u|\Arg{var name} matches
7852 the exact contents (both character codes and category codes) of the
7853 variable \cs[no-index]{\meta{var name}}. %,
7854 %which are obtained by applying \cs{exp_not:v} \Arg{var name} at the
7855 %time the regular expression is compiled.
7856 Within a |\c{...}|
7857 control sequence matching, the |\u| escape sequence only expands its
7858 argument once. %, in effect performing \cs{tl_to_str:v}.
7859 Quantifiers are supported.
7860
7861 The |\ur| escape sequence allows to insert the contents of a |regex|
7862 variable into a larger regular expression. For instance,
7863 |A\ur{lTmpaRegex}D| matches the tokens |A| and |D| separated by
7864 something that matches the regular expression
7865 \cs{lTmpaRegex}. This behaves as if a non-capturing group
7866 were surrounding \cs{lTmpaRegex}, and any group contained
7867 in \cs{lTmpaRegex} is converted to a non-capturing group.
7868 Quantifiers are supported.
7869
7870 For instance, if \cs{lTmpaRegex} has value \verb"B|C",
7871 then |A\ur{l_tmpa_regex}D| is equivalent to \verb"A(?:B|C)D" (matching
7872 |ABD| or |ACD|) and not to \verb"AB|CD" (matching |AB| or |CD|). To
7873 get the latter effect, it is simplest to use \TeX{}'s expansion
7874 machinery directly: if \cs{lTmpaTl} contains
7875 \verb"B|C" then the following two lines show the same result:
7876 \begin{codehigh}
7877 \regexShow {A \u{lTmpaTl} D}
7878 \regexShow {A B | C D}
7879 \end{codehigh}
7880
7881 \subsection{Miscellaneous}
7882
7883 Anchors and simple assertions.
7884 \begin{l3regex-syntax}
7885 \item[\\b] Word boundary: either the previous token is matched by
7886 |\w| and the next by |\W|, or the opposite. For this purpose,
7887 the ends of the token list are considered as |\W|.
7888 \item[\\B] Not a word boundary: between two |\w| tokens
7889 or two |\W| tokens (including the boundary).
7890 \item[\char`^ \textrm{or} \\A]
7891 Start of the subject token list.
7892 \item[\char`$\textrm{,} \\Z \textrm{or} \\z] %^^A $
7893 End of the subject token list.
7894 \item[\\G] Start of the current match. This is only different from |^|
7895 in the case of multiple matches: for instance
7896 |\regexCount {\G a} {aaba} \lTmpaInt| yields $2$, but
7897 replacing |\G| by |^| would result in \cs{lTmpaInt} holding the
7898 value $1$.
7899 \end{l3regex-syntax}
7900
7901 The option |(?i)| makes the match case insensitive (identifying
7902 \texttt{A}--\texttt{Z} with \texttt{a}--\texttt{z}; no Unicode support
7903 yet). This applies until the end of the group in which it appears, and
7904 can be reverted using |(?-i)|. For instance, in
7905 \verb"(?i)(a(?-i)b|c)d", the letters |a| and |d| are affected by the
7906 |i| option. Characters within ranges and classes are affected
7907 individually: |(?i)[Y-\\]| is equivalent to |[YZ\[\\yz]|, and
7908 |(?i)[^aeiou]| matches any character which is not a vowel. Neither
7909 character properties, nor |\c{...}| nor |\u{...}| are affected by the
7910 |i| option.
7911 %^^A \]
7912
7913 \section{Syntax of the Replacement Text}
7914
7915 Most of the features described in regular expressions do not make
7916 sense within the replacement text. Backslash introduces various
7917 special constructions, described further below:
7918 \begin{itemize}
7919 \item |\0| is the whole match;
7920 \item |\1| is the submatch that was matched by the first (capturing)
7921 group |(...)|; similarly for |\2|, \ldots{}, |\9| and
7922 |\g{|\meta{number}|}|;
7923 \item \verb*|\ | inserts a space (spaces are ignored when not
7924 escaped);
7925 \item |\a|, |\e|, |\f|, |\n|, |\r|, |\t|, |\xhh|, |\x{hhh}|
7926 correspond to single characters as in regular expressions;
7927 \item |\c|\Arg{cs name} inserts a control sequence;
7928 \item |\c|\meta{category}\meta{character} (see below);
7929 \item |\u|\Arg{tl var name} inserts the contents of the
7930 \meta{tl var} (see below).
7931 \end{itemize}
7932 Characters other than backslash and space are simply inserted in the
7933 result (but since the replacement text is first converted to a string,
7934 one should also escape characters that are special for \TeX{}, for
7935 instance use |\#|). Non-alphanumeric characters can always be safely
7936 escaped with a backslash.
7937
7938 For instance,
7939 \begin{demohigh}
7940 \tlSet \lTmpaTl {Hello, world!}
7941 \regexReplaceAll {([er]?l|o) .} {(\0--\1)} \lTmpaTl
7942 \tlUse \lTmpaTl
7943 \end{demohigh}
7944
7945 The submatches are numbered according to the order in which the
7946 opening parenthesis of capturing groups appear in the regular
7947 expression to match. The $n$-th submatch is empty if there are fewer
7948 than $n$ capturing groups or for capturing groups that appear in
7949 alternatives that were not used for the match. In case a capturing
7950 group matches several times during a match (due to quantifiers) only
7951 the last match is used in the replacement text. Submatches always keep
7952 the same category codes as in the original token list.
7953
7954 By default, the category code of characters inserted by the
7955 replacement are determined by the prevailing category code regime at
7956 the time where the replacement is made, with two exceptions:
7957 \begin{itemize}
7958 \item space characters (with character code $32$) inserted with
7959 \verb*|\ | or |\x20| or |\x{20}| have category code $10$ regardless
7960 of the prevailing category code regime;
7961 \item if the category code would be $0$ (escape), $5$ (newline),
7962 $9$ (ignore), $14$ (comment) or $15$ (invalid), it is replaced by
7963 $12$ (other) instead.
7964 \end{itemize}
7965 The escape sequence |\c| allows to insert characters
7966 with arbitrary category codes, as well as control sequences.
7967 \begin{l3regex-syntax}
7968 \item[\\cX(\ldots{})] Produces the characters \enquote{\ldots{}} with
7969 category |X|, which must be one of |CBEMTPUDSLOA| as in regular
7970 expressions. Parentheses are optional for a single character (which
7971 can be an escape sequence). When nested, the innermost category
7972 code applies, for instance |\cL(Hello\cS\ world)!| gives this text
7973 with standard category codes.
7974 \item[\\c\Arg{text}] Produces the control sequence with csname
7975 \meta{text}. The \meta{text} may contain references to the
7976 submatches |\0|, |\1|, and so on, as in the example for |\u| below.
7977 \end{l3regex-syntax}
7978
7979 The escape sequence |\u|\Arg{var name} allows to insert the
7980 contents of the variable with name \meta{var name} directly into
7981 the replacement, giving an easier control of category codes. When
7982 nested in |\c{|\ldots{}|}| and |\u{|\ldots{}|}| constructions, the
7983 |\u| and |\c| escape sequences %perform \cs{tl_to_str:v}, namely
7984 extract the value of the control sequence and turn it into a string.
7985 Matches can also be used within the arguments of |\c| and |\u|. For
7986 instance,
7987 \begin{demohigh}
7988 \tlSet \lMyOneTl {first}
7989 \tlSet \lMyTwoTl {\underline{second}}
7990 \tlSet \lTmpaTl {One,Two,One,One}
7991 \regexReplaceAll {[^,]+} {\u{lMy\0Tl}} \lTmpaTl
7992 \tlUse \lTmpaTl
7993 \end{demohigh}
7994
7995 Regex replacement is also a convenient way to produce token lists
7996 with arbitrary category codes. For instance
7997 \begin{codehigh}
7998 \tlClear \lTmpaTl
7999 \regexReplaceAll { } {\cU\% \cA\~} \lTmpaTl
8000 \end{codehigh}
8001 results in \cs{lTmpaTl} containing the percent character
8002 with category code $7$ (superscript) and an active tilde character.
8003
8004 \chapter{Token Manipulation (\texttt{Token})}
8005
8006 \begin{function}{\charLowercase,\charUppercase,\charTitlecase,\charFoldcase}
8007 \begin{syntax}
8008 \cs{charLowercase} \meta{char}
8009 \cs{charUppercase} \meta{char}
8010 \cs{charTitlecase} \meta{char}
8011 \cs{charFoldcase} \meta{char}
8012 \end{syntax}
8013 Converts the \meta{char} to the equivalent case-changed character
8014 as detailed by the function name (see %\cs{strFoldcase} and
8015 \cs{textTitlecase} for details of these terms). The case mapping
8016 is carried out with no context-dependence (\emph{cf.} \cs{textUppercase},
8017 \emph{etc.}) These functions generate characters with the category code
8018 of the \meta{char} (i.e. only the character code changes).
8019 \end{function}
8020
8021 \begin{function}{\charStrLowercase,\charStrUppercase,\charStrTitlecase,\charStrFoldcase}
8022 \begin{syntax}
8023 \cs{charStrLowercase} \meta{char}
8024 \cs{charStrUppercase} \meta{char}
8025 \cs{charStrTitlecase} \meta{char}
8026 \cs{charStrFoldcase} \meta{char}
8027 \end{syntax}
8028 Converts the \meta{char} to the equivalent case-changed character
8029 as detailed by the function name (see %\cs{strFoldcase} and
8030 \cs{textTitlecase} for details of these terms). The case mapping
8031 is carried out with no context-dependence (\emph{cf.} \cs{textUppercase},
8032 \emph{etc.}) These functions generate \enquote{other} (category code $12$)
8033 characters.
8034 \end{function}
8035
8036 \begin{function}{\charSetLccode}
8037 \begin{syntax}
8038 \cs{charSetLccode} \Arg{intexpr_1} \Arg{intexpr_2}
8039 \end{syntax}
8040 Sets up the behaviour of the \meta{character} when
8041 found inside \cs{textLowercase}, such that \meta{character_1}
8042 will be converted into \meta{character_2}. The two \meta{characters}
8043 may be specified using an \meta{integer expression} for the character code
8044 concerned. This may include the \TeX{} \verb|`|\meta{character}
8045 method for converting a single character into its character code:
8046 \begin{codehigh}
8047 \charSetLccode {`\A} {`\a} % Standard behaviour
8048 \charSetLccode {`\A} {`\A + 32}
8049 \charSetLccode {65} {97}
8050 \end{codehigh}
8051 The setting applies within the current \TeX{} group.
8052 \end{function}
8053
8054 \begin{function}{\charSetUccode}
8055 \begin{syntax}
8056 \cs{charSetUccode} \Arg{intexpr_1} \Arg{intexpr_2}
8057 \end{syntax}
8058 Sets up the behaviour of the \meta{character} when
8059 found inside \cs{textUppercase}, such that \meta{character_1}
8060 will be converted into \meta{character_2}. The two \meta{characters}
8061 may be specified using an \meta{integer expression} for the character code
8062 concerned. This may include the \TeX{} \verb|`|\meta{character}
8063 method for converting a single character into its character code:
8064 \begin{codehigh}
8065 \charSetUccode {`\a} {`\A} % Standard behaviour
8066 \charSetUccode {`\a} {`\a - 32}
8067 \charSetUccode {97} {65}
8068 \end{codehigh}
8069 The setting applies within the current \TeX{} group.
8070 \end{function}
8071
8072 \begin{function}{\charValueLccode}
8073 \begin{syntax}
8074 \cs{charValueLccode} \Arg{integer expression}
8075 \end{syntax}
8076 Returns the current lower case code of the \meta{character} with
8077 character code given by the
8078 \meta{integer expression}.
8079 \end{function}
8080
8081 \begin{function}{\charValueUccode}
8082 \begin{syntax}
8083 \cs{charValueUccode} \Arg{integer expression}
8084 \end{syntax}
8085 Returns the current upper case code of the \meta{character} with
8086 character code given by the
8087 \meta{integer expression}.
8088 \end{function}
8089
8090 %\begin{function}{\charShowValueLccode}
8091 %\begin{syntax}
8092 %\cs{charShowValueLccode} \Arg{integer expression}
8093 %\end{syntax}
8094 %Displays the current lower case code of the \meta{character} with
8095 %character code given by the \meta{integer expression} on the
8096 %terminal.
8097 %\end{function}
8098 %
8099 %\begin{function}{\charShowValueUccode}
8100 %\begin{syntax}
8101 %\cs{charShowValueUccode} \Arg{integer expression}
8102 %\end{syntax}
8103 %Displays the current upper case code of the \meta{character} with
8104 %character code given by the \meta{integer expression} on the
8105 %terminal.
8106 %\end{function}
8107
8108 \chapter{Text Processing (\texttt{Text})}
8109
8110 This module deals with manipulation of (formatted) text; such material is
8111 comprised of a restricted set of token list content. The functions provided
8112 here concern conversion of textual content for example in case changing,
8113 %generation of bookmarks and extraction to tags.
8114 Begin-group and end-group tokens in the \meta{text}
8115 are normalized and become \verb|{| and \verb|}|, respectively.
8116
8117 \section{Case Changing}
8118
8119 These case changing functions are designed to work with Unicode input only.
8120 As such, UTF-8 input is assumed for \emph{all} engines.
8121 When used with XeTeX or LuaTeX a full range of Unicode transformations
8122 are enabled. Specifically, the standard mappings
8123 here follow those defined by the \href{http://www.unicode.org}
8124 {Unicode Consortium} in \texttt{UnicodeData.txt} and
8125 \texttt{SpecialCasing.txt}. In the case of $8$-bit engines, mappings
8126 are provided for characters which can be represented in output typeset
8127 using the \verb|T1|, \verb|T2| and \verb|LGR| font encodings.
8128 Thus for example \texttt{รค} can be
8129 case-changed using pdfTeX. For pTeX only the ASCII range is
8130 covered as the engine treats input outside of this range as east Asian.
8131
8132 %Importantly, notice that these functions are intended for working with
8133 %user \emph{text for typesetting}. For case changing programmatic data see
8134 %the \pkg{Str} module and discussion there of \cs{strLowercase},
8135 %\cs{strUppercase} and \cs{strFoldcase}.
8136
8137 \begin{function}{\textExpand}
8138 \begin{syntax}
8139 \cs{textExpand} \Arg{text}
8140 \end{syntax}
8141 Takes user input \meta{text} and expands the content.
8142 Protected commands (typically formatting) are left in place,
8143 and no processing takes place of math mode material.
8144 %(as delimited by pairs given in \cs{l_text_math_delims_tl}
8145 %or as the argument to commands listed in \cs{l_text_math_arg_tl}).
8146 Commands which are neither engine- nor \LaTeX{} protected are expanded exhaustively.
8147 %Any commands listed in \cs{l_text_expand_exclude_tl},
8148 %\cs{l_text_accents_tl} and \cs{l_text_letterlike_tl} are excluded from expansion.
8149 \end{function}
8150
8151 \begin{function}{\textLowercase,\textUppercase,\textTitlecase,\textTitlecaseFirst}
8152 \begin{syntax}
8153 \cs{textLowercase} \Arg{tokens}
8154 \cs{textUppercase} \Arg{tokens}
8155 \cs{textTitlecase} \Arg{tokens}
8156 \cs{textTitlecaseFirst} \Arg{tokens}
8157 \end{syntax}
8158 Takes user input \meta{text} first applies \cs{textExpand}, then
8159 transforms the case of character tokens as specified by the
8160 function name. The category code of letters are not changed by this
8161 process (at least where they can be represented by the engine as a single
8162 token: $8$-bit engines may require active characters).
8163 \par
8164 Upper- and lowercase have the obvious meanings. Titlecasing may be regarded
8165 informally as converting the first character of the \meta{tokens} to
8166 uppercase and the rest to lowercase. However, the process is more complex
8167 than this as there are some situations where a single lowercase character
8168 maps to a special form, for example \texttt{ij} in Dutch which becomes
8169 \texttt{IJ}.
8170 \par
8171 For titlecasing, note that there are two functions available. The
8172 function \cs{textTitlecase} applies (broadly) uppercasing to the first
8173 letter of the input, then lowercasing to the remainder. In contrast,
8174 \cs{textTitlecaseFirst} \emph{only} carries out the uppercasing operation,
8175 and leaves the balance of the input unchanged.
8176 %Determining whether non-letter characters at the start of text should switch
8177 %from upper- to lowercasing is controllable.
8178 %When \cs{l_text_titlecase_check_letter_bool} is \texttt{true},
8179 %characters which are not letters (category code $11$) are
8180 %left unchanged and \enquote{skipped}: the first \emph{letter} is uppercased.
8181 %(With $8$-bit engines, this is extended to active characters which form
8182 %part of a multi-byte letter codepoint.) When
8183 %\cs{l_text_titlecase_check_letter_bool} is \texttt{false}, the first
8184 %character is uppercased, and the rest lowercased, irrespective of the nature
8185 %of the character.
8186 \par
8187 Case changing does not take place within math mode material. For example:
8188 \begin{demohigh}
8189 \textUppercase {Text $y=mx+c$ with {Braces}}
8190 \end{demohigh}
8191 \begin{demohigh}
8192 \textLowercase {Text $Y=mX+c$ with {Braces}}
8193 \end{demohigh}
8194 %The arguments of commands listed in \cs{l_text_case_exclude_arg_tl}
8195 %are excluded from case changing; the latter are entirely non-textual
8196 %content (such as labels).
8197 \end{function}
8198
8199 \begin{function}{\textLangLowercase,\textLangUppercase,\textLangTitlecase,\textLangTitlecaseFirst}
8200 \begin{syntax}
8201 \cs{textLangLowercase} \Arg{language} \Arg{tokens}
8202 \cs{textLangUppercase} \Arg{language} \Arg{tokens}
8203 \cs{textLangTitlecase} \Arg{language} \Arg{tokens}
8204 \cs{textLangTitlecaseFirst} \Arg{language} \Arg{tokens}
8205 \end{syntax}
8206 Takes user input \meta{text} first applies \cs{textExpand}, then
8207 transforms the case of character tokens as specified by the
8208 function name. The category code of letters are not changed by this
8209 process (at least where they can be represented by the engine as a single
8210 token: $8$-bit engines may require active characters).
8211 \par
8212 These conversions are language-sensitive, and follow Unicode Consortium guidelines.
8213 Currently, the languages recognised for special handling are as follows.
8214 \begin{itemize}
8215 \item Azeri and Turkish (\texttt{az} and \texttt{tr}).
8216 The case pairs I/i-dotless and I-dot/i are activated for these
8217 languages. The combining dot mark is removed when lowercasing
8218 I-dot and introduced when upper casing i-dotless.
8219 \item German (\texttt{de-alt}).
8220 An alternative mapping for German in which the lowercase
8221 \emph{Eszett} maps to a \emph{gro\ss{}es Eszett}. Since there is
8222 a \verb|T1| slot for the \emph{gro\ss{}es Eszett} in \verb|T1|, this
8223 tailoring \emph{is} available with pdfTeX as well as in the
8224 Unicode \TeX{} engines.
8225 \item Greek (\texttt{el}).
8226 Removes accents from Greek letters when uppercasing; titlecasing
8227 leaves accents in place. (At present this is implemented only
8228 for Unicode engines.)
8229 \item Lithuanian (\texttt{lt}).
8230 The lowercase letters i and j should retain a dot above when the
8231 accents grave, acute or tilde are present. This is implemented for
8232 lowercasing of the relevant uppercase letters both when input as
8233 single Unicode codepoints and when using combining accents. The
8234 combining dot is removed when uppercasing in these cases. Note that
8235 \emph{only} the accents used in Lithuanian are covered: the behaviour
8236 of other accents are not modified.
8237 \item Dutch (\texttt{nl}).
8238 Capitalisation of \texttt{ij} at the beginning of titlecased
8239 input produces \texttt{IJ} rather than \texttt{Ij}. The output
8240 retains two separate letters, thus this transformation \emph{is}
8241 available using pdfTeX.
8242 \end{itemize}
8243 \end{function}
8244
8245 \chapter{Files (\texttt{File})}
8246
8247 This module provides functions for working with external files.
8248 %Some of these functions apply to an entire file, and have prefix \cs{file},
8249 %while others are used to work with files on a line by line basis and have
8250 %prefix \cs{Ior} (reading) or \cs{Iow} (writing).
8251
8252 It is important to remember that when reading external files \TeX{}
8253 attempts to locate them using both the operating system path and entries in the
8254 \TeX{} file database (most \TeX{} systems use such a database). Thus the
8255 \enquote{current path} for \TeX{} is somewhat broader than that for other
8256 programs.
8257
8258 For functions which expect a \meta{file name} argument, this argument
8259 may contain both literal items and expandable content, which should on
8260 full expansion be the desired file name.
8261 %Active characters (as declared in \cs{l_char_active_seq}) are \emph{not}
8262 %expanded, allowing the direct use of these in file names.
8263 Quote tokens (\verb|"|) are not permitted in file names as they are reserved
8264 for internal use by some \TeX{} primitives.
8265
8266 Spaces are trimmed at the beginning and end of the file name:
8267 this reflects the fact that some file systems do not allow or interact
8268 unpredictably with spaces in these positions. When no extension is given,
8269 this will trim spaces from the start of the name only.
8270
8271 \section{File Operation Functions}
8272
8273 \begin{function}{\fileInput}
8274 \begin{syntax}
8275 \cs{fileInput} \Arg{file name}
8276 \end{syntax}
8277 Searches for \meta{file name} in the path as detailed for
8278 \cs{fileIfExistTF}, and if found reads in the file and
8279 returns the contents. All files read are recorded
8280 for information and the file name stack is updated by this
8281 function. An error is raised if the file is not found.
8282 \end{function}
8283
8284 \begin{function}{\fileIfExistInput,\fileIfExistInputF}
8285 \begin{syntax}
8286 \cs{fileIfExistInput} \Arg{file name}
8287 \cs{fileIfExistInputF} \Arg{file name} \Arg{false code}
8288 \end{syntax}
8289 Searches for \meta{file name} using the current \TeX{} search path.
8290 %and the additional paths included in \cs{l_file_search_path_seq}.
8291 If found then reads in the file and returns the contents as described
8292 for \cs{fileInput}, otherwise inserts the \meta{false code}.
8293 Note that these functions do not raise
8294 an error if the file is not found, in contrast to \cs{fileInput}.
8295 \end{function}
8296
8297 \begin{function}{\fileGet,\fileGetT,\fileGetF,\fileGetTF}
8298 \begin{syntax}
8299 \cs{fileGet} \Arg{filename} \Arg{setup} \meta{tl}
8300 \cs{fileGetT} \Arg{filename} \Arg{setup} \meta{tl} \Arg{true code}
8301 \cs{fileGetF} \Arg{filename} \Arg{setup} \meta{tl} \Arg{false code}
8302 \cs{fileGetTF} \Arg{filename} \Arg{setup} \meta{tl} \Arg{true code} \Arg{false code}
8303 \end{syntax}
8304 Defines \meta{tl} to the contents of \meta{filename}.
8305 Category codes may need to be set appropriately via the \meta{setup}
8306 argument.
8307 The non-branching version sets the \meta{tl} to \cs{qNoValue} if the file is
8308 not found. The branching version runs the \meta{true code} after the
8309 assignment to \meta{tl} if the file is found, and \meta{false code}
8310 otherwise.
8311 \end{function}
8312
8313 \begin{function}{\fileIfExist,\fileIfExistT,\fileIfExistF,\fileIfExistTF}
8314 \begin{syntax}
8315 \cs{fileIfExist} \Arg{file name}
8316 \cs{fileIfExistT} \Arg{file name} \Arg{true code}
8317 \cs{fileIfExistF} \Arg{file name} \Arg{false code}
8318 \cs{fileIfExistTF} \Arg{file name} \Arg{true code} \Arg{false code}
8319 \end{syntax}
8320 Searches for \meta{file name} using the current \TeX{} search path.
8321 %and the additional paths controlled by \cs{l_file_search_path_seq}.
8322 \end{function}
8323
8324 %\begin{function}{\fileInputStop}
8325 %\begin{syntax}
8326 %\cs{fileInputStop}
8327 %\end{syntax}
8328 %Ends the reading of a file started by \cs{file_input:n} or similar before
8329 %the end of the file is reached. Where the file reading is being terminated
8330 %due to an error, \cs{msg_critical:nn(nn)}
8331 %should be preferred.
8332 %\begin{texnote}
8333 %This function must be used on a line on its own: \TeX{} reads files
8334 %line-by-line and so any additional tokens in the \enquote{current} line
8335 %will still be read.
8336 %\par
8337 %This is also true if the function is hidden inside another function
8338 %(which will be the normal case), i.e., all tokens on the same line
8339 %in the source file are still processed. Putting it on a line by itself
8340 %in the definition doesn't help as it is the line where it is used that
8341 %counts!
8342 %\end{texnote}
8343 %\end{function}
8344
8345 \chapter{Quarks (\texttt{Quark})}
8346
8347 %One special type of constants in \LaTeX3 is \enquote{quarks}.
8348 %By convention all constants of type quark start out with \verb|\q|.
8349
8350 Quarks are control sequences (and in fact, token lists) that expand
8351 to themselves and should therefore \emph{never} be executed directly
8352 in the code. This would result in an endless loop!
8353
8354 Quarks can be used as error return values for functions that receive erroneous input.
8355 For example, in the function \cs{propGet} to retrieve a value stored
8356 in some key of a property list, if the key does not exist then the return value
8357 is the quark \cs{qNoValue}.
8358 As mentioned above, such quarks are extremely fragile and it is imperative
8359 when using such functions that code is carefully written to check for
8360 pathological cases to avoid leakage of a quark into an uncontrolled
8361 environment.
8362
8363 \section{Constant Quarks}
8364
8365 \begin{variable}{\qNoValue}
8366 A canonical value for a missing value, when one is requested from
8367 a data structure. This is therefore used as a \enquote{return} value
8368 by functions such as \cs{propGet} if there is no data to return.
8369 \end{variable}
8370
8371 \section{Quark Conditionals}
8372
8373 \begin{function}{\quarkVarIfNoValue,\quarkVarIfNoValueT,\quarkVarIfNoValueF,\quarkVarIfNoValueTF}
8374 \begin{syntax}
8375 \cs{quarkVarIfNoValue} \meta{token}
8376 \cs{quarkVarIfNoValueT} \meta{token} \Arg{true code}
8377 \cs{quarkVarIfNoValueF} \meta{token} \Arg{false code}
8378 \cs{quarkVarIfNoValueTF} \meta{token} \Arg{true code} \Arg{false code}
8379 \end{syntax}
8380 Tests if the \meta{token} is equal to \cs{qNoValue}.
8381 \begin{demohigh}
8382 \clistGet \cEmptyClist \lTmpaTl
8383 \quarkVarIfNoValueTF \lTmpaTl {\prgReturn{NoValue}} {\prgReturn{SomeValue}}
8384 \end{demohigh}
8385 \begin{demohigh}
8386 \seqPop \cEmptySeq \lTmpaTl
8387 \quarkVarIfNoValueTF \lTmpaTl {\prgReturn{NoValue}} {\prgReturn{SomeValue}}
8388 \end{demohigh}
8389 \begin{demohigh}
8390 \propSetFromKeyval \lTmpaProp {key1=one,key2=two}
8391 \propGet \lTmpaProp {key3} \lTmpaTl
8392 \quarkVarIfNoValueTF \lTmpaTl {\prgReturn{NoValue}} {\prgReturn{SomeValue}}
8393 \end{demohigh}
8394 \end{function}
8395
8396 %\begin{function}{\quarkIfNoValue,\quarkIfNoValueTF}
8397 %\begin{syntax}
8398 %\cs{quarkIfNoValue} \Arg{token list}
8399 %\cs{quarkIfNoValueTF} \Arg{token list} \Arg{true code} \Arg{false code}
8400 %\end{syntax}
8401 %Tests if the \meta{token list} contains only \cs{qNoValue}
8402 %(distinct from \meta{token list} being empty or containing
8403 %\cs{qNoValue} plus one or more other tokens).
8404 %\end{function}
8405
8406 \chapter{Legacy Concepts (\texttt{Legacy})}
8407
8408 There are a small number of \TeX{} or \LaTeXe{} concepts which are not used
8409 in \pkg{functional} code but which need to be manipulated when working as a \LaTeXe{}
8410 package. To allow these to be integrated cleanly into \pkg{functional} code, a set
8411 of legacy interfaces are provided here.
8412
8413 \begin{function}{\legacyIf,\legacyIfT,\legacyIfF,\legacyIfTF}
8414 \begin{syntax}
8415 \cs{legacyIf} \Arg{name}
8416 \cs{legacyIfT} \Arg{name} \Arg{true code}
8417 \cs{legacyIfF} \Arg{name} \Arg{false code}
8418 \cs{legacyIfTF} \Arg{name} \Arg{true code} \Arg{false code}
8419 \end{syntax}
8420 Tests if the \LaTeXe{}/plain \TeX{} conditional (generated by \tn{newif})
8421 if \texttt{true} or \texttt{false} and branches \hbox{accordingly}. The
8422 \meta{name} of the conditional should \emph{omit} the leading \texttt{if}.
8423 \begin{demohigh}
8424 \newif \ifFooBar
8425 \legacyIfTF {FooBar} {\prgReturn{True!}} {\prgReturn{False!}}
8426 \end{demohigh}
8427 \end{function}
8428
8429 \begin{function}{\legacyIfSetTrue,\legacyIfSetFalse}
8430 \begin{syntax}
8431 \cs{legacyIfSetTrue} \Arg{name}
8432 \cs{legacyIfSetFalse} \Arg{name}
8433 \end{syntax}
8434 Sets the \LaTeXe{}/plain \TeX{} conditional \verb|\if|\meta{name}
8435 (generated by \tn{newif}) to be \texttt{true} or \texttt{false}.
8436 \begin{demohigh}
8437 \newif \ifFooBar
8438 \legacyIfSetTrue {FooBar}
8439 \legacyIfTF {FooBar} {\prgReturn{True!}} {\prgReturn{False!}}
8440 \end{demohigh}
8441 \end{function}
8442
8443 \begin{function}{\legacyIfSet}
8444 \begin{syntax}
8445 \cs{legacyIfSet} \Arg{name} \Arg{boolexpr}
8446 \end{syntax}
8447 Sets the \LaTeXe{}/plain \TeX{} conditional \verb|\if|\meta{name}
8448 (generated by \tn{newif}) to the result of evaluating the
8449 \meta{boolean expression}.
8450 \begin{demohigh}
8451 \newif \ifFooBar
8452 \legacyIfSet {FooBar} {\cFalseBool}
8453 \legacyIfTF {FooBar} {\prgReturn{True!}} {\prgReturn{False!}}
8454 \end{demohigh}
8455 \end{function}
8456
8457 \chapter{The Source Code}
8458
8459 %\CodeHigh{lite}
8460 \dochighinput[language=latex/latex3]{functional.sty}
8461
8462 \end{document}