Debian Codegraph @

texmf-dist/doc/latex/expkv-bundle/pkg-cs.tex

raw
1 \genericekv\expkvc{-cs}
2
3 \expkvc\ aids in creating fully expandable macros that take a \kv\ argument. It
4 implements somewhat efficient solutions to expandable \kv\ parsing without the
5 user having to worry too much about the details.
6
7 The package supports two main approaches for this. The first is splitting the
8 keys up into individual arguments, the second preparses the \kv\ list into a
9 single argument in which accessing the value of individual keys is fast. The
10 behaviour of the second type is similar to a hash table, so we call that variant
11 |Hash|, the first type is called |Split|. Both these variants support a number
12 of so called \emph{primary keys} (a primary key matches an argument, roughly
13 speaking).
14
15 In addition to these methods there is a structured way to define additional keys
16 which might build upon the primary keys but not directly relate to an argument.
17 These keys are called \emph{secondary keys}. Primary and secondary keys belong
18 to a specific macro (the macro name serves as the \emph{set}).
19
20 A word of advice you should consider: Macros defined with \expkvc\ are simple to
21 create, and there might be good use cases for them (for instance since they
22 don't work by assignments but only by argument forwarding logic they have no
23 issues with implicit or explicit groups whatsoever). But they don't scale as
24 well as established \kv\ interfaces (think of the idiomatic key definitions with
25 \pkg{keyval}, or \pkg{l3keys}, or \expkv\ with or without \expkvd), and they are
26 slower than idiomatic key definitions in packages with fast \kv\ parsing.
27
28
29 \subsection{Defining Macros and Primary Keys}
30
31 All macros defined with \expkvc\ have to be previously undefined (or have the
32 |\meaning| of |\relax|). There is no way to automatically undefine keys once
33 they are set up -- so to make sure there are no conflicts only new definitions
34 are allowed. The \emph{set} name (as used by \cs[no-index]{ekvset}) will be
35 \cs[no-index]{string}\cs[no-index]{\meta{macro}}.
36
37
38 \subsubsection{Primary Keys\label{sec:c:primary}}
39
40 The notion of primary keys needs a bit of explanation, or better, the input
41 syntax for the argument \meta{primary keys} in the following explanations. The
42 \meta{primary keys} argument should be a \kv\ list in which each \key\ will be
43 one primary key and \val\ the initial value of said \key\ (and that value is
44 mandatory, even if you leave it blank that's fine, but you have to explicitly
45 state it). By default all keys are defined short, but you can define |\long|
46 keys by prefixing \key\ with |long| (\emph{e.g.},
47 |long name=Jonathan P. Spratte| to define a |\long| key called |name|). Due to
48 some internal implementations it's worth noting that |\long| keys are a
49 microscopic grain faster. The \meta{cs} will only be defined |\long| if at least
50 one of the keys was |\long|. For obvious reasons there is no interface in place
51 to define something as |\protected|.
52
53 To allow keys to start with the word |long| even if you don't want them to be
54 |\long| you can also prefix them with |short|. The first found prefix of the two
55 will stop the parsing for prefixes and what remains becomes the \key.
56
57 These rules culminate in the following:
58
59 \begin{enverb}[no-tcb]
60 \ekvcSplit\foo
61 {
62 long short = abc\par
63 ,short long = def
64 }
65 {#1#2}
66 \end{enverb}
67 will define a macro |\foo| that knows two primary keys, |short| which is defined
68 |\long| (so will accept explicit |\par| tokens inside its value at use time),
69 and |long| which doesn't accept explicit |\par| tokens (leading to a low level
70 \TeX\ error). The description of |\ekvcSplit| follows shortly.
71
72 There is one exception to the rule that each \key\ in \meta{primary keys} needs
73 to get a value: If you include a key named |...| without a value this will be a
74 primary key in which every unknown key will be collected -- and \meta{cs} will
75 be defined |\long|. The unknown keys will be stored in a way that \emph{most}
76 \kv\ parsers will parse them correctly (but this is no general guarantee, for
77 instance \pkg{pgfkeys} can accidentally strip multiple sets of braces at the
78 wrong moment). See some examples in \autoref{sec:c:unknown}.
79
80 At the moment \expkvc\ doesn't require any internal keys, but I can't foresee
81 whether this will be the case in the future as well, as it might turn out that
82 some features I deem useful can't be implemented without such internal keys.
83 Because of this, please don't use key names starting with "EKVC|" as that
84 should be the private name space.
85
86
87 \subsubsection{Split\label{sec:c:split}}
88
89 The split variants will provide the key values as separate arguments. This
90 limits the number of keys for which this is truly useful.
91
92 \begin{function}{\ekvcSplit}
93 \begin{syntax}
94 \cs{ekvcSplit}\meta{cs}\marg{primary keys}\marg{definition}
95 \end{syntax}
96 This defines \meta{cs} to be a macro taking one mandatory argument which
97 should contain a \kv\ list. The \meta{primary keys} will be defined for this
98 macro (see \autoref{sec:c:primary}). The \meta{definition} is the code that
99 will be executed. You can access the \val\ of a \key\ by using a macro
100 parameter from |#1| to |#9|. The order of the macro parameters will be the
101 order provided in the \meta{primary keys} list (so |#1| is the \val\ of the
102 \key\ defined first). With \cs{ekvcSplit} you can define macros using at most
103 nine primary keys.
104 \end{function}
105 \begin{example}{Simple macro with \cs[no-index]{ekvcSplit}}
106 The following defines a macro |\foo| that takes the keys |a| and |b|
107 and outputs their values in a textual form:
108 \begin{enverb}
109 \ekvcSplit\foo{a=a,b=b}{a is #1.\par b is #2.\par}
110 \foo{}
111 \foo{b=e}
112 \end{enverb}
113 \end{example}
114
115 \begin{function}{\ekvcSplitAndForward}
116 \begin{syntax}
117 \cs{ekvcSplitAndForward}\meta{cs}\marg{after}\marg{primary keys}
118 \end{syntax}
119 This defines \meta{cs} to be a macro taking one mandatory argument which
120 should contain a \kv\ list. You can use as many primary keys as you want with
121 this. The primary keys will be forwarded to \meta{after} as braced arguments
122 (as many as necessary for your primary keys). The order of the braced
123 arguments will be the order of your primary key definitions. In \meta{after}
124 you can use just a single control sequence, or some arbitrary stuff which will
125 be left in the input stream before your braced values (with one set of braces
126 stripped from \meta{after}), so both of the following would be fine:
127 \begin{enverb}[no-tcb]
128 \ekvcSplitAndForward\foo\foo@aux{keyA = A, keyB = B}
129 \ekvcSplitAndForward\foo{\foo@aux{more args}}{keyA = A, keyB = B}
130 \end{enverb}
131 \end{function}
132 \noindent
133 In the first case |\foo@aux| should take at least two arguments (|keyA| and
134 |keyB|), in the second case at least three (|more args|, |keyA|, and |keyB|).
135
136 \begin{function}{\ekvcSplitAndUse}
137 \begin{syntax}
138 \cs{ekvcSplitAndUse}\meta{cs}\marg{primary keys}
139 \end{syntax}
140 This will roughly do the same as \cs{ekvcSplitAndForward}, but instead of
141 specifying what will be used after splitting the keys, \meta{cs} will use what
142 follows the \kv\ list. So its syntax will be
143 \end{function}
144 \begin{quote}
145 \ttfamily
146 \meta{cs}\kvarg\marg{after}
147 \end{quote}
148 and the code in |after| should expect at least as many arguments as the number
149 of keys defined for \meta{cs}.
150
151
152 \subsubsection{Hash\label{sec:c:hash}}
153
154 The hash variants will provide the key values as a single argument in which you
155 can access specific values using a special macro. The implementation might be
156 more convenient and scale better, \emph{but} it is slower (for a rudimentary
157 macro with a single key benchmarking was almost 1.7~times slower, the root of
158 which being the key access with \cs{ekvcValue}, not the parsing, and for a key
159 access using \cs{ekvcValueFast} it was still about 1.2~times slower). Still to
160 be future proof, considering the hash variants is a good idea, and to get best
161 performance but less maintainable code you can resort to the split approach.
162
163 \begin{function}{\ekvcHash}
164 \begin{syntax}
165 \cs{ekvcHash}\meta{cs}\marg{primary keys}\marg{definition}
166 \end{syntax}
167 This defines \meta{cs} to be a macro taking one mandatory argument which
168 should contain a \kv\ list. You can use as many primary keys as you want. The
169 primary keys will be forwarded as a single argument containing every key to
170 the underlying macro. The underlying macro is defined as \meta{definition}, in
171 which you can access the \val\ of a \key\ by using
172 \cs{ekvcValue}\marg{key}|{#1}| (or similar).
173 \end{function}
174 \begin{example}{Simple macro with \cs[no-index]{ekvcHash}}
175 This defines an equivalent macro to the |\foo| defined with \cs{ekvcSplit}
176 earlier:
177 \begin{enverb}
178 \ekvcHash\foo{a=a,b=b}{a is \ekvcValue{a}{#1}.\par
179 b is \ekvcValue{b}{#1}.\par}
180 \foo{}
181 \foo{b=e}
182 \end{enverb}
183 \end{example}
184
185 \begin{function}{\ekvcHashAndForward}
186 \begin{syntax}
187 \cs{ekvcHashAndForward}\meta{cs}\marg{after}\marg{primary keys}
188 \end{syntax}
189 This defines \meta{cs} to be a macro taking one mandatory argument which
190 should contain a \kv\ list. You can use as many primary keys as you want. The
191 primary keys will be forwarded as a single argument containing every key to
192 \meta{after}. You can use a single macro for \meta{after} or use some
193 arbitrary stuff, which will be left in the input stream before the hashed \kv\
194 list with one set of braces stripped. In the macro called in \meta{after} you
195 can access the \val\ of a \key\ by using \cs{ekvcValue}\marg{key}|{#1}| (or
196 whichever argument the hashed \kv\ list will be in).
197 \end{function}
198 \begin{example}{Using \cs[no-index]{ekvcHashAndForward}}
199 This defines a macro |\foo| processing two keys, and passing the result to
200 |\foobar|:
201 \begin{enverb}
202 \ekvcHashAndForward\foo\foobar{a=a,b=b}
203 \newcommand*\foobar[1]{a is \ekvcValue{a}{#1}.\par
204 b is \ekvcValue{b}{#1}.\par}
205 \foo{}
206 \foo{b=e}
207 \end{enverb}
208 \end{example}
209
210 \begin{function}{\ekvcHashAndUse}
211 \begin{syntax}
212 \cs{ekvcHashAndUse}\meta{cs}\marg{primary keys}
213 \end{syntax}
214 This will roughly do the same as \cs{ekvcHashAndForward}, but instead of
215 specifying what will be used after hashing the keys during the definition,
216 \meta{cs} will use what follows the \kv\ list. So its syntax will be
217 \begin{quote}
218 \ttfamily
219 \meta{cs}\kvarg\marg{after}
220 \end{quote}
221 \end{function}
222
223 \begin{function}{\ekvcValue}
224 \begin{syntax}
225 \cs{ekvcValue}\marg{key}\marg{key list}
226 \end{syntax}
227 This is a safe way to access your keys in a hash variant. \key\ is the key
228 which's \val\ you want to use out of the \meta{key list}. \meta{key list}
229 should be the key list argument forwarded to your underlying macro by
230 \cs{ekvcHash}, \cs{ekvcHashAndForward}, or \cs{ekvcHashAndUse}. It will be
231 tested whether the hash function to access that \key\ exists, the \key\
232 argument is not empty, and that the \meta{key list} really contains a \val\ of
233 that \key. This macro needs exactly two steps of expansion and if used inside
234 of an |\edef| or |\expanded| context will protect the \val\ from further
235 expanding.
236 \end{function}
237
238 \begin{function}{\ekvcValueFast}
239 \begin{syntax}
240 \cs{ekvcValueFast}\marg{key}\marg{key list}
241 \end{syntax}
242 This behaves similar to \cs{ekvcValue}, but \emph{without any} safefy tests.
243 As a result this is about 1.4~times faster \emph{but} will throw low level
244 \TeX\ errors eventually if the hash function isn't defined or the \key\ isn't
245 part of the \meta{key list} (\emph{e.g.}, because it was defined as a key for
246 another macro -- all macros share the same hash function per \meta{key} name).
247 Note that this will not only throw low level errors but result in undefined
248 behaviour as well! This macro needs exactly three steps of expansion in the
249 no-error case.
250 \end{function}
251
252 \begin{function}{\ekvcValueSplit}
253 \begin{syntax}
254 \cs{ekvcValueSplit}\marg{key}\marg{key list}\marg{next}
255 \end{syntax}
256 If you need a specific \key\ from a \meta{key list} more than once, it'll be a
257 good idea to only extract it once and from then on keep it as a separate
258 argument (or if you want to forward this value to another macro). Hence the
259 macro \cs{ekvcValueSplit} will extract one specific \key's \val\ from the list
260 and forward it as an argument to \meta{next}, so the result of this will be
261 \meta{next}\marg{value}. This is roughly as fast as \cs{ekvcValue} and runs
262 the same tests.
263 \end{function}
264 \begin{example}
265 {Splitting of a key from a hash list using \cs[no-index]{ekvcValueSplit}}
266 The following defines a macro |\foo| which will take three keys. Since the
267 next parsing step will need the value of one of the keys multiple times we
268 split that key off the list (in this example the next step doesn't use the key
269 multiple times for simplicity though), and the entire list is forwarded as the
270 second argument:
271 \begin{enverb}
272 \ekvcHash\foo{a=a,b=b,c=c}
273 {\ekvcValueSplit{a}{#1}\foobar{#1}}
274 \newcommand*\foobar[2]{a is #1.\par
275 b is \ekvcValue{b}{#2}.\par
276 c is \ekvcValue{c}{#2}.\par}
277 \foo{}
278 \end{enverb}
279 \end{example}
280
281 \begin{function}{\ekvcValueSplitFast}
282 \begin{syntax}
283 \cs{ekvcValueSplitFast}\marg{key}\marg{key list}\marg{next}
284 \end{syntax}
285 This behaves just like \cs{ekvcValueSplit}, but it won't run the safety tests,
286 hence it is faster but more error prone, just like the relation between
287 \cs{ekvcValue} and \cs{ekvcValueFast}.
288 \end{function}
289
290
291 \subsection{Secondary Keys\label{sec:c:secondary}}
292
293 To lift some limitations of each primary key matching one argument or hash
294 entry, you can define secondary keys. Those have to be defined for each macro
295 individually but it doesn't matter whether that macro was set up as a split or
296 hash variant.
297
298 Secondary keys can have a \prefix\ (|long|), and must have a \type\
299 (like |meta|). Some \types\ might require some \prefix\ while other
300 \types\ might forbid the usage of a specific \prefix.
301
302 Please keep in mind that key names shouldn't start with "EKVC|".
303
304 \begin{function}{\ekvcSecondaryKeys}
305 \begin{syntax}
306 \cs{ekvcSecondaryKeys}\meta{cs}\kvarg
307 \end{syntax}
308 This is the front facing macro to define secondary keys. For the macro
309 \meta{cs} define \key\ to have definition \val. The general syntax for \key\
310 should be
311 \begin{quote}\ttfamily
312 \meta{prefix} \meta{name}
313 \end{quote}
314 Where \meta{prefix} is a space separated list of optional \prefixes\
315 followed by one \type. The syntax of \meta{value} is dependent on the
316 used \type.
317 \end{function}
318
319
320 \subsubsection{\textit{Prefixes}}
321
322 Currently there is only one \prefix\ available, which is
323 \begin{function}[module=expkv-cs prefix]{long}
324 The following key will be defined |\long|.
325 \end{function}
326
327
328 \subsubsection{\textit{Types}\label{sec:c:secondarytypes}}
329
330 Compared to \expkvd\ you might notice that the \types\ here are much fewer.
331 Unfortunately the expansion only concept doesn't allow for great variety in the
332 common key \types.
333
334 The syntax examples of the \types\ will show which \prefix\ will be
335 automatically used by printing those black (\texttt{\enfprefix{long}}), which
336 will be available in grey (\texttt{\allprefix{long}}), and which will be
337 disallowed in red (\texttt{\notprefix{long}}). This will be put flush right next
338 to the syntax line.
339
340 If a secondary key references another key it doesn't matter whether that other
341 key is a primary or secondary key (unless explicitly stated otherwise).
342
343 \begin{function}[module=expkv-cs type]{meta}
344 \begin{syntax}
345 meta \key\ = \kvarg \prefixes2
346 \end{syntax}
347 With a |meta| key you can set other keys. Whenever \meta{key} is used the keys
348 in the \kv\ list will be set to the values given there. You can use the \val\
349 given to \key\ by using |#1| in the \kv\ list.
350 \end{function}
351
352 \begin{function}[module=expkv-cs type]{nmeta}
353 \begin{syntax}
354 nmeta \key\ = \kvarg \prefixes3
355 \end{syntax}
356 An |nmeta| key is like a |meta| key, but it doesn't take a value at use time,
357 so the \kv\ list is static.
358 \end{function}
359
360 \begin{function}[module=expkv-cs type]{alias}
361 \begin{syntax}
362 alias \meta{key} = \marg{key_2} \prefixes3
363 \end{syntax}
364 This assigns the definition of \meta{key_2} to \key. As a result \key\ is an
365 alias for \meta{key_2} behaving just the same. Both the \Vkey\ and the \Nkey\
366 will be copied if they are defined when |alias| is used. Of course,
367 \meta{key_2} has to be defined as at least one of \Nkey\ or \Vkey.
368 \end{function}
369
370 \begin{function}[module=expkv-cs type]{default}
371 \begin{syntax}
372 default \key\ = \marg{default} \prefixes3
373 \end{syntax}
374 If \key\ is defined as a \Vkey\ you can define a \Nkey\ version with this. The
375 \Nkey\ will behave as if \key\ was given \meta{default} as its \val. Note that
376 this doesn't change the initial values of primary keys set at definition time
377 (see \cs{ekvcChange} in \autoref{sec:c:change} for this). If \key\ isn't yet
378 defined this results in an error.
379 \end{function}
380
381 \begin{function}[module=expkv-cs type]{enum}
382 \begin{syntax}
383 enum \key\ = \marg{key_2}\{\val, \ldots\} \prefixes2
384 \end{syntax}
385 This defines \key\ to only accept the values given in the list of the second
386 argument of its definition. It forwards the position of \val\ in that list to
387 \meta{key_2} (zero-based) as a string of digits (so as |0|, |1|, \ldots). The
388 \meta{key_2} has to already be defined by the time an |enum| key is set up.
389 Each \val\ in the list (and at use time) is |\detokenize|d, so no expansion
390 takes place here.
391
392 If you use |enum| twice on the same \key\ the new values will again start at
393 zero (so it is possible to define multiple values with the same outcome),
394 however since you can't skip values you'll have to use the same as in the
395 first call for values with just a single variant (or use the |choice| \type\
396 as an |enum| is just a specialised |choice| and the two use the same internal
397 structure). There is no interface to delete existing values.
398 \end{function}
399 \begin{example}{Enumerating choices with the \texttt{enum} \type}
400 First a small example that might give you an idea of what the description
401 above could mean:
402 \begin{enverb}
403 \ekvcSplit\foo{k-internal=-1}{#1}
404 \ekvcSecondaryKeys\foo
405 {enum k = {k-internal}{a,b,c}}
406 \foo{}\foo{k=a}\foo{k=b}\foo{k=c}
407 \end{enverb}
408 \end{example}
409 \begin{example}{A slightly more complicated usage of the \texttt{enum} \type}
410 We can define a choice setup that might do different things based on the
411 choice encountered, and the numeric value is easy to parse using
412 \cs[no-index]{ifcase}:
413 \begin{enverb}
414 \ekvcSplit\foo{k-internal=-1}
415 {%
416 \ifcase#1
417 is\or
418 This\or
419 easy%
420 \else
421 .%
422 \fi
423 }
424 \ekvcSecondaryKeys\foo
425 {enum k = {k-internal}{a,b,c}}
426 \foo{k=b} \foo{k=a} \foo{k=c}\foo{}
427 \end{enverb}
428 \end{example}
429
430 \begin{function}[module=expkv-cs type]{choice}
431 \begin{syntax}
432 choice \key\ = \marg{key_2}\kvarg \prefixes2
433 \end{syntax}
434 This is pretty similar to an |enum|, but unlike with |enum| the forwarded
435 \val\ will not be numeric, instead the \val\ as given during the definition
436 time will be forwarded (inside the \kv\ list argument if you omit \val\ the
437 same as the \key\ will be used). While the user input has to match in a
438 |\detokenize|d form, the \val\ might still expand further during your macro's
439 expansion.
440 \end{function}
441 \begin{example}{Filtering possible values with the \texttt{choice} \type}
442 We could use this to filter out the possible vertical placements of a \LaTeXe\
443 |tabular|:
444 \begin{enverb}
445 \ekvcSplit\foo{v-internal=c,a=t,b=c,c=b}
446 {%
447 \begin{tabular}[#1]{@{} c @{:} c @{}}
448 a & #2\\
449 b & #3\\
450 c & #4\\
451 \end{tabular}%
452 }
453 \ekvcSecondaryKeys\foo
454 {choice v = {v-internal}{t,c,b}}
455 \foo{} \foo{v=t} \foo{v=c} \foo{v=b}
456 \end{enverb}
457 \end{example}
458 \begin{example}{Setting up a Boolean key with the \texttt{choice} \type}
459 We could also set up a Boolean key with a |choice| key by forwarding
460 |\@firstoftwo| or |\@secondoftwo|:
461 \begin{enverb}
462 \makeatletter
463 \ekvcSplit\foo{bool-internal=\@secondoftwo}
464 {bool was #1{true}{false}.\par}
465 \ekvcSecondaryKeys\foo
466 {
467 choice bool = {bool-internal}
468 {true=\@firstoftwo, false=\@secondoftwo}
469 ,nmeta bool = {bool-internal=\@firstoftwo}
470 }
471 \foo{}\foo{bool}\foo{bool=false}\foo{bool=true}
472 \makeatother
473 \end{enverb}
474 \end{example}
475
476 \begin{function}[module=expkv-cs type]{aggregate,e-aggregate}
477 \begin{syntax}
478 aggregate \key\ = \marg{primary}\marg{definition} \prefixes3
479 \end{syntax}
480 While other key \types\ replace the current value of the associated
481 primary key, with |aggregate| you can create keys that append or prepend (or
482 whatever you like) the new value to the current one. Your definition of an
483 |aggregate| key must be exactly two \TeX\ arguments, where \meta{primary}
484 should be the name of a primary key, and \meta{definition} the way you want to
485 store the current and the new value. Inside \meta{definition} you can use |#1|
486 for the current, and |#2| for the new value. The \meta{definition} will not
487 expand any further during the entire parsing for |aggregate|, whereas in
488 |e-aggregate| everything that ends up in \meta{definition} (so whatever you
489 provide including the values in |#1| and |#2|) will be fully expanded (using
490 the |\expanded| primitive), so use |\noexpand| and |\unexpanded| to protect
491 what shouldn't be expanded.
492 The resulting \meta{key} will inherit being either |short| or |long| from the
493 \meta{primary} key.
494 \end{function}
495 \begin{example}{Building a list with the \texttt{aggregate} \type}
496 The following defines an internal key (|k-internal|), which is used to build a
497 comma separated list from each call of the user facing key (|k|):
498 \begin{enverb}
499 \ekvcSplit\foo
500 {k-internal=0,color=red}
501 {\textcolor{#2}{#1}}
502 \ekvcSecondaryKeys\foo
503 {aggregate k = {k-internal}{#1,#2}}
504 \foo{}\par
505 \foo{k=1,k=2,k=3,k=4}
506 \end{enverb}
507 \end{example}
508 \begin{example}{Building a convoluted list with the \texttt{aggregate} \type}
509 But also more strange stuff could end there, like macros or using the same
510 value multiple times:
511 \begin{enverb}[no-tcb]
512 \ekvcSecondaryKeys\foo
513 {aggregate k = {k-internal}{\old{#1}\new{#2\old{#1}}}}
514 \end{enverb}
515 \end{example}
516
517 \begin{function}[module=expkv-cs type]{flag-bool}
518 \begin{syntax}
519 flag-bool \key\ = \meta{cs} \prefixes3
520 \end{syntax}
521 This is a secondary \type\ that doesn't involve any of the primary or other
522 secondary keys. This defines \key\ to take a value, which should be either
523 |true| or |false|, and set the flag called \meta{cs} accordingly as a boolean.
524 If \meta{cs} isn't defined yet it will be initialised as a flag. Note that the
525 flag will not be set to a specific state automatically so a flag set in one
526 macro might affect every other macro in the current scope. Please also read
527 \autoref{sec:c:flags}.
528 \end{function}
529 \begin{example}{Using a Boolean flag with the \texttt{flag-bool} \type}
530 Provide a key |bold| to turn the output of our macro bold if the associated
531 flag is |true|.
532 \begin{enverb}
533 \ekvcSplit\foo{a=a,b=b}
534 {%
535 \ekvcFlagIf\fooFlag
536 {\textbf{a is #1 and b is #2}\par}
537 {a is #1 and b is #2\par}%
538 }
539 \ekvcSecondaryKeys\foo{flag-bool bold = \fooFlag}
540 \foo{}\foo{bold=true}\foo{}\foo{bold=false}\foo{}
541 \end{enverb}
542 \end{example}
543
544 \begin{function}[module=expkv-cs type]{flag-true,flag-false}
545 \begin{syntax}
546 flag-true \key\ = \meta{cs} \prefixes3
547 \end{syntax}
548 This is similar to |flag-bool|, but the \meta{key} will be a \Nkey\ and if
549 used will set the flag to either |true| or |false|. If \meta{cs} isn't defined
550 yet it will be initialised as a flag. Note that the flag will not be set to a
551 specific state automatically. Please also read \autoref{sec:c:flags}.
552 \end{function}
553
554 \begin{function}[module=expkv-cs type]{flag-raise}
555 \begin{syntax}
556 flag-raise \key\ = \meta{cs} \prefixes3
557 \end{syntax}
558 This defines \key\ to be a \Nkey\ that will raise the flag called \meta{cs} on
559 usage. If \meta{cs} isn't defined yet it will be initialised as a flag. Note
560 that the flag will not be set to a specific state automatically. Please also
561 read \autoref{sec:c:flags}.
562 \end{function}
563
564
565 \subsection{Changing the Initial Values\label{sec:c:change}}
566
567 \begin{function}{\ekvcChange}
568 \begin{syntax}
569 \cs{ekvcChange}\meta{cs}\kvarg
570 \end{syntax}
571 This processes the \kv\ list for the macro \meta{cs} to set new defaults for
572 it (meaning the initial values used if you don't provide anything at use time,
573 not those specified with the |default| \type). \meta{cs} should be defined
574 with \expkvc\ (but it doesn't matter if it's a split or hash variant). Inside
575 the \kv\ list both primary and secondary keys can be used. If \meta{cs} was
576 defined |\long| earlier it will still be |\long|, every other \TeX\ prefix
577 will be stripped (but \expkvc\ doesn't support them anywhere else so that
578 should be fine). The resulting new defaults will be stored inside the
579 \meta{cs} locally (just as the original initial values were). If there was an
580 unknown key forwarding added to \meta{cs} (see \autoref{sec:c:unknown}) any
581 unknown key will be stored inside the list of unknown keys as well.
582 \cs{ekvcChange} is not expandable!
583 \end{function}
584 \begin{example}
585 {Changing the values for future calls using \cs[no-index]{ekvcChange}}
586 With \cs{ekvcChange} we can now do the following:
587 \begin{enverb}
588 \ekvcSplit\foo{a=a,b=b}{a is #1.\par b is #2.\par}
589 \begingroup
590 \ekvcChange\foo{b=B}
591 \foo{}
592 \ekvcSecondaryKeys\foo{meta c={a={#1},b={#1}}}
593 \ekvcChange\foo{c=c}
594 \foo{}
595 \endgroup
596 \foo{}
597 \end{enverb}
598 \end{example}
599 \begin{example}{A typical setup macro for \expkvc\ macros}
600 As a result with this the typical setup macro could be implemented:
601 \begin{enverb}[no-tcb]
602 \ekvcHashAndUse\fooKV{keyA=a,keyB=b}
603 \def\fooA#1{\fooKV{#1}\fooAinternal}
604 \def\fooB#1{\fooKV{#1}\fooBinternal}
605 \protected\def\foosetup{\ekvcChange\fooKV}
606 \end{enverb}
607 Of course the usage is limited to a single macro |\fooKV|, hence this might
608 not be as powerful as similar macros used with other \kv\ interfaces. But at
609 least a few similar macros could be grouped using the same key parsing macro
610 internally like |\fooA| and |\fooB| do in this example.
611 \end{example}
612
613
614 \subsection{Handling Unknown Keys\label{sec:c:unknown}}
615
616 If your macro should handle unknown keys without directly throwing an error you
617 can use the special |...| marker in the \meta{primary keys} list. Since those
618 keys will be processed once by \expkv\ they will be forwarded normalised: The
619 \key\ and the \val\ will be forwarded with one set of surrounding spaces and
620 braces, so a \kv\ pair will result in \texttt{\vissp{ \marg{key} = \marg{val} }}
621 and a \Nkey\ is forwarded as \texttt{\vissp{ \marg{key} }} (this way most other
622 \kv\ implementations should parse the correct input).
623
624 The exact behaviour differs slightly between the two variants (as all primary
625 keys do). The behaviour inside the split variants will be similar to normal
626 primary keys, the $n$-th argument (corresponding to the position of |...| inside
627 the primary keys list) will contain any unknown key encountered while parsing
628 the argument. And inside the split variant you can use a primary key named |...|
629 at the same time (since only the position in the list determines the argument,
630 not the name).
631 \begin{example}{Using unknown key handlers to wrap another \kv\ enabled macro}
632 The following will forward any unknown key to |\includegraphics| to control
633 the appearance while processing its own keys:
634 \begin{enverb}[same-line=0.6]
635 \newcommand*\foo{\ekvoptarg\fooKV{}}
636 \ekvcSplitAndForward\fooKV\fooOUT
637 {
638 a=a
639 ,...
640 ,b=b
641 ,...={}
642 }
643 \newcommand\fooOUT[5]
644 {%
645 a is #1 and b is #3.\par
646 \includegraphics[{#2}]{#5}\par
647 \texttt{...} is #4.\par
648 }
649 \foo[width=.5\linewidth, b=c,
650 ...={a stupid key name, but works}]
651 {example-image-duck}
652 \end{enverb}
653 \end{example}
654
655 Inside the hash variants the unknown keys list will be put inside the hash named
656 |...| (we have to use some name, and this one seems reasonable). As a
657 consequence a primary key named |...| would clash with the unknown key handler.
658 If you still used such a key it would remove any unknown key stored there until
659 that point and replace the list with its value.
660 \begin{example}{Wrapping an existing macro, but with a hash variant}
661 The following is more or less equivalent to the above example, but with the
662 hash variant, and it will not contain the primary |...| key. We have to make
663 sure that |\includegraphics| sees the \kv\ list, so need to expand
664 |\ekvcValue{...}{#1}| before |\includegraphics| parses it.
665 \begin{enverb}[same-line=0.76]
666 \newcommand*\foo{\ekvoptarg\fooKV{}}
667 \ekvcHashAndForward\fooKV\fooOUT
668 {a=a, b=b, ...}
669 \newcommand\fooOUT[2]
670 {%
671 a is \ekvcValue{a}{#1} and
672 b is \ekvcValue{b}{#1}.\par
673 \ekvcValueSplit{...}{#1}{\includegraphics[}]%
674 {#2}\par
675 }
676 \foo[width=\linewidth, b=c]
677 {example-image-duck-portrait}
678 \end{enverb}
679 \end{example}
680
681
682 \subsection{Flags\label{sec:c:flags}}
683
684 The idea of flags is taken from \pkg{expl3}. They provide a way to store
685 numerical information expandably, however only incrementing and accessing works
686 expandably, decrementing is unexpandable. A flag has a height, which is a
687 numerical value, and which can be raised by 1. Flags come at a high
688 computational cost (accessing them is slow and they require more memory than
689 normal \TeX\ data types like registers, both issues getting linearly worse with
690 the height), so don't use them if not necessary.
691
692 The state of flags is always changed locally to the current group, but not to
693 the current macro, so if you're using one of the \types\ involving flags
694 bear in mind that they can affect other macros using the same flags at the
695 current scope!
696
697 \expkvc\ provides some macros to access, alter, and use flags. Flags of \expkvc\
698 don't share a name space with the flags of \pkg{expl3}.
699
700 \begin{function}{\ekvcFlagNew}
701 \begin{syntax}
702 \cs{ekvcFlagNew}\meta{flag}
703 \end{syntax}
704 This initialises the macro \meta{flag} as a new flag. It isn't checked whether
705 the macro \meta{flag} is currently undefined. A \meta{flag} will expand to the
706 flag's current height with a trailing space (so you can use it directly with
707 \cs[no-index]{ifnum} for example and it will terminate the number scanning on
708 its own).
709
710 All other macros dealing with flags take as a parameter a macro defined as a
711 \meta{flag} with \cs{ekvcFlagNew}.
712 \end{function}
713
714 \begin{function}{\ekvcFlagHeight}
715 \begin{syntax}
716 \cs{ekvcFlagHeight}\meta{flag}
717 \end{syntax}
718 This expands to the current height of \meta{flag} in a single step of
719 expansion (without a trailing space).
720 \end{function}
721
722 \begin{function}{\ekvcFlagRaise}
723 \begin{syntax}
724 \cs{ekvcFlagRaise}\meta{flag}
725 \end{syntax}
726 This expandably raises the height of \meta{flag} by 1.
727 \end{function}
728
729 \begin{function}{\ekvcFlagSetTrue,\ekvcFlagSetFalse}
730 \begin{syntax}
731 \cs{ekvcFlagSetTrue}\meta{flag}
732 \end{syntax}
733 By interpreting an even value as false and an odd value as true we can use a
734 flag as a boolean. This expandably sets \meta{flag} to true or false,
735 respectively, by raising it if necessary.
736 \end{function}
737
738 \begin{function}{\ekvcFlagIf}
739 \begin{syntax}
740 \cs{ekvcFlagIf}\meta{flag}\marg{true}\marg{false}
741 \end{syntax}
742 This interprets a \meta{flag} as a boolean and expands to either \meta{true}
743 or \meta{false}.
744 \end{function}
745
746 \begin{function}{\ekvcFlagIfRaised}
747 \begin{syntax}
748 \cs{ekvcFlagIfRaised}\meta{flag}\marg{true}\marg{false}
749 \end{syntax}
750 This tests whether the \meta{flag} is raised, meaning it has a height greater
751 than zero, and if so expands to \meta{true} else to \meta{false}.
752 \end{function}
753
754 \begin{function}{\ekvcFlagReset,\ekvcFlagResetGlobal}
755 \begin{syntax}
756 \cs{ekvcFlagReset}\meta{flag}
757 \end{syntax}
758 This resets a flag (so restores its height to 0). This operation is \emph{not}
759 expandable and done locally for \cs{ekvcFlagReset} and globally for
760 \cs{ekvcFlagResetGlobal}. If you really intend to use flags you can reset them
761 every now and then to keep the performance hit low.
762 \end{function}
763
764 \begin{function}{\ekvcFlagGetHeight}
765 \begin{syntax}
766 \cs{ekvcFlagGetHeight}\meta{flag}\marg{next}
767 \end{syntax}
768 This retrieves the current height of the \meta{flag} and provides it as a
769 braced argument to \meta{next}, leaving \meta{next}\marg{height} in the input
770 stream.
771 \end{function}
772
773 \begin{function}{\ekvcFlagGetHeights}
774 \begin{syntax}
775 \cs{ekvcFlagGetHeights}\marg{flag-list}\marg{next}
776 \end{syntax}
777 This retrieves the current height of each \meta{flag} in the \meta{flag-list}
778 and provides them as a single argument to \meta{next}. Inside that argument
779 each height is enclosed in a set of braces individually. The \meta{flag-list}
780 is just a single argument containing the \meta{flag}s. So a usage like
781 |\ekvcFlagGetHeights{\myflagA\myflagB}{\stuff}| will expand to
782 |\stuff{|\marg{height-A}\marg{height-B}|}|.
783 \end{function}
784
785
786 \subsection{Further Examples}
787
788 \begin{example}{Defining an expandable \kv\ macro with an optional argument}
789 Using \cs[no-index]{NewExpandableDocumentCommand} or \expkv's \cs{ekvoptarg}
790 or \cs{ekvoptargTF} and forwarding arguments one can easily define \kv\ macros
791 with actual optional and mandatory arguments as well. A small nonsense
792 example:
793 \begin{enverb}[below,inter=resulting in]
794 \makeatletter
795 \newcommand*\nonsense{\ekvoptarg\nonsense@a{}}
796 \ekvcHashAndForward\nonsense@a\nonsense@b
797 {
798 keyA = A,
799 keyB = B,
800 keyC = c,
801 keyD = d,
802 }
803 \newcommand*\nonsense@b[2]
804 {%
805 \begin{tabular}{lll|}
806 key & A & \ekvcValue{keyA}{#1} \\
807 & B & \ekvcValue{keyB}{#1} \\
808 & C & \ekvcValue{keyC}{#1} \\
809 & D & \ekvcValue{keyD}{#1} \\
810 \multicolumn{2}{l}{mandatory} & #2 \\
811 \end{tabular}%
812 }
813 \makeatother
814 \nonsense{} % do nonsense
815 \nonsense[keyA=hihi]{haha}
816 \nonsense[keyA=hihi, keyB=A]{hehe}
817 \nonsense[keyC=huhu, keyA=hihi, keyB=A]{haha}
818 \end{enverb}
819 \end{example}
820
821 \begin{example}{The \cs[no-index]{sine} example revisited}
822 In \autoref{sec:expkv:sneak} I presented an expandable macro to calculate the
823 sine of some user input with a few keys, and there I hinted to \expkvc, so
824 here's the same function implemented with |\ekvcSplitAndForward|. There is a
825 small difference here, we need to use an internal key to store whether degrees
826 or radians will be used, but we don't need to use an internal key to collect
827 the values of our individual keys in the correct order.
828 \begin{enverb}
829 \makeatletter
830 \newcommand\sine{\ekvoptarg\sine@kv{}}
831 \ekvcSplitAndForward\sine@kv\sine@do
832 {
833 f = sin
834 ,internal = d
835 ,round = 3
836 }
837 \ekvcSecondaryKeys\sine@kv
838 {
839 nmeta degree = internal=d
840 ,nmeta radian = internal={}
841 }
842 \newcommand*\sine@do[4]{\fpeval{round(#1#2(#4),#3)}}
843 \makeatother
844 \sine{60}\par
845 \sine[round=10]{60}\par
846 \sine[f=cos,radian]{pi}\par
847 \edef\myval{\sine[f=tan]{1}}\texttt{\meaning\myval}
848 \end{enverb}
849 \end{example}
850
851
852 \subsection{Freedom for Keys!}
853
854 If this had been the \TeX{}book this subsection would have had a double bend
855 sign. Not because it is overly complicated, but because it shows things which
856 could break \expkvc's expandability and its alignment safety. This is for
857 experienced users wanting to get the most flexibility and knowing what they are
858 doing.
859
860 In case you're wondering, it is possible to define other keys than the primaries
861 and the secondary key \types\ listed in \autoref{sec:c:secondary} for a
862 macro defined with \expkvc\ by using the low-level interface of \expkv\ or even
863 the interface provided by \expkvd. The set name used for \expkvc's keys is the
864 macro name, including the leading backslash, or more precisely the result of
865 |\string|\meta{cs} is used. This can be exploited to define additional keys with
866 arbitrary code. Consider the following \emph{bad} example:
867 \begin{enverb}[no-tcb]
868 \ekvcSplit\foo{a=A,b=B}{a is #1.\par b is #2.\par}
869 \protected\ekvdef{\string\foo}{c}{\def\fooC{#1}}
870 \end{enverb}
871 This would define a key named |c| that will store its \val\ inside a macro. The
872 issue with this is that this can't be done expandably. As a result, the macro
873 |\foo| isn't always expandable any more (not that bad if this was never
874 required; killjoy if it was) and as soon as the key |c| is used it is also no
875 longer alignment safe\footnote{This means that the \kv\ list can't contain
876 alignment markers that are not inside an additional set of braces if used
877 inside a \TeX\ alignment}
878 (might be bad depending on the usage).
879
880 So why do I show you this? Because we could as well do something useful like
881 creating a key that pre-parses the input and after that passes the parsed value
882 on. This parsing would have to be completely expandable though (and we could
883 perhaps also implement this using the |e-aggregate| \type). For the
884 pass-on part we can use the following function:
885
886 \begin{function}{\ekvcPass}
887 \begin{syntax}
888 \cs{ekvcPass}\meta{cs}\marg{key}\marg{value}
889 \end{syntax}
890 This passes \val\ on to \key\ for the \expkvc-macro \meta{cs}. It should be
891 used inside the key parsing of a macro defined with \expkvc, else this most
892 likely results in a low level \TeX\ error. You can't forward anything to the
893 special unknown key handler |...| as that is no defined key.
894 \end{function}
895 \begin{example}
896 {Forwarding pre-parsed keys to an \expkvc\ key with \cs[no-index]{ekvcPass}}
897 With this we could for example split the value of a key at a hyphen and pass
898 the parts to different keys:
899 \begin{enverb}
900 \ekvcSplit\foo{a=A,b=B}{a is #1.\par b is #2.\par}
901 \ekvdef{\string\foo}{c}{\fooSplit#1\par}
902 \def\fooSplit#1-#2\par
903 {\ekvcPass\foo{a}{#1}\ekvcPass\foo{b}{#2}}
904 \foo{}
905 \foo{c=1-2}
906 \end{enverb}
907 \end{example}
908
909 Additionally, there is a more general version of the |aggregate| secondary key
910 type, namely the |process| key type:
911
912 \begin{function}[module=expkv-cs type]{process}
913 \begin{syntax}
914 process \key\ = \marg{primary}\marg{definition} \prefixes2
915 \end{syntax}
916 This will grab the current value of a \meta{primary} key as |#1| (without
917 changing the current value) and the new value as |#2| and leave all the
918 processing to \meta{definition}. You should use \cs{ekvcPass} to forward the
919 values afterwards. Unlike |aggregate| you can specify whether the \meta{key}
920 should be long or not, this isn't inherited from the \meta{primary} key. Keep
921 in mind that you could easily break things here if your code does not work by
922 expansion.
923 \end{function}
924 \begin{example}{Filtering out values with the \texttt{process} \type}
925 We could define a key that only accepts values greater than the current value
926 with this:
927 \begin{enverb}
928 \ekvcSplit\foo{internal=5}{a is #1.\par}
929 \ekvcSecondaryKeys\foo
930 {
931 process a={internal}
932 {\ifnum#1<#2 \ekvcPass\foo{internal}{#2}\fi}
933 }
934 \foo{a=1}
935 \foo{a=5}
936 \foo{a=9}
937 \end{enverb}
938 \end{example}
939 \begin{example}{Filtering out values with the \texttt{e-aggregate} \type}
940 The same is possible with an |e-aggregate| key as well though:
941 \begin{enverb}[no-tcb]
942 \ekvcSplit\foo{internal=5}{a is #1.\par}
943 \ekvcSecondaryKeys\foo
944 {
945 e-aggregate a={internal}
946 {\ifnum#1<#2 \unexpanded{#2}\else\unexpanded{#1}\fi}
947 }
948 \end{enverb}
949 \end{example}
950
951 \subsection{Useless Macros}
952
953 These macros are most likely of little to no interest to users.
954
955 \begin{function}{\ekvcDate,\ekvcVersion}
956 These two macros store the version and date of the package/generic code.
957 \end{function}