Debian Codegraph @

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

raw
1 \genericekv\expkvd{-def}
2
3 Since the trend for the last couple of years goes to defining keys for a \kv\
4 interface using a \kv\ interface, I thought that maybe providing such an
5 interface for \expkv\ will make it more attractive for actual use. But at the
6 same time I didn't want to broaden \expkv's initial scope. So here is \expkvd,
7 go define \kv\ interfaces with \kv\ interfaces.
8
9 Unlike many of the other established \kv\ interfaces to define keys, \expkvd\
10 works using prefixes instead of suffixes (\emph{e.g.}, |.tl_set:N| of
11 \pkg{l3keys}) or directory like handlers (\emph{e.g.}, |/.store in| of
12 \pkg{pgfkeys}). This was decided as a personal preference, more over in \TeX\
13 parsing for the first spaces is way easier than parsing for the last one, so
14 this should also turn out to be faster. \expkvd's prefixes are sorted into two
15 categories: \prefixes, which are equivalent to \TeX's prefixes like |\long| and
16 of which a \key\ can have multiple, and \types\ defining the basic behaviour of
17 the \key\ and of which a \key\ must have one. For a description of the available
18 \prefixes\ take a look at \autoref{sec:d:prefixes}, the \types\ are described in
19 \autoref{sec:d:types}.
20
21
22 \subsection{Macros\label{sec:d:macros}}
23
24 The number of user-facing macros is quite manageable:
25
26 \begin{function}{\ekvdefinekeys}
27 \begin{syntax}
28 \cs{ekvdefinekeys}\marg{set}\kvarg
29 \end{syntax}
30 In \meta{set}, define \key\ to have definition \val. The general syntax for
31 \key\ should be
32 \begin{quote}
33 \ttfamily
34 \meta{prefix} \meta{name}
35 \end{quote}
36 where \meta{prefix} is a space separated list of optional \prefixes\ followed
37 by one \type. The syntax of \val\ is dependent on the used \type.
38 \end{function}
39
40 \begin{function}{\ekvdDate,\ekvdVersion}
41 These two macros store the version and date of the package.
42 \end{function}
43
44
45 \subsection{Prefixes}
46
47 As already said, prefixes are separated into two groups, \prefixes\ and \types.
48 Not every \prefix\ is allowed for all \types.
49
50
51 \subsubsection{\textit{Prefixes}\label{sec:d:prefixes}}
52
53 \begin{function}[module=expkv-def prefix]{new}
54 The following \key\ must be new (so previously undefined). An error is thrown
55 if it is already defined and the new definition is ignored. |new| only asserts
56 that there are no conflicts between \Nkey{}s and other \Nkey{}s or \Vkey{}s
57 and other \Vkey{}s.
58 \end{function}
59 \begin{example}{The effects of the \texttt{new} \prefix}
60 You can test the following (lines throwing an error are marked by a comment,
61 error messages are printed in red for this example):
62 \expkvdocPrintErrors[\par]%
63 \begin{enverb}[below]
64 \ekvdefinekeys{new-example}
65 {
66 new code key = \domystuffwitharg{#1}
67 ,new noval KEY = \domystuffwithoutarg
68 ,new bool key = \mybool % Error!
69 ,new bool KEY = \mybool % Error!
70 ,new meta key = {KEY} % Error!
71 ,new nmeta KEY = {key} % Error!
72 }
73 \end{enverb}
74 \end{example}
75
76 \begin{function}[module=expkv-def prefix]{also}
77 The following key \type\ will be \emph{added} to an existing \key's
78 definition. You can't add a \type\ taking an argument at use time to an
79 existing \key\ which doesn't take an argument and vice versa. Also you'll get
80 an error if you try to add an action which isn't allowed to be either |\long|
81 or |\protected| to a \key\ which already is |\long| or |\protected| (the
82 opposite order would be suboptimal as well, but can't be really captured with
83 the current code).
84
85 A \key\ already defined as |\long| or |\protected| will stay that way, but you
86 can add |\long| or |\protected| to a \key\ which isn't by using |also|.
87 \end{function}
88 \begin{example}
89 {Overload a key \type\ with another with the \texttt{also} \prefix}
90 Suppose you want to create a boolean \key, but additionally to setting a
91 boolean value you want to execute some more code as well. For this you can use
92 the following:
93 \begin{enverb}[no-tcb]
94 \ekvdefinekeys{also-example}
95 {
96 bool key = \ifmybool
97 ,also code key = \domystuff{#1}
98 }
99 \end{enverb}
100 \end{example}
101
102 If you use |also| on a |choice|, |bool|, |invbool|, or |boolpair| \key\ it is
103 tried to determine if the key already is of one of those types. If this test is
104 true the declared choices will be added to the possible choices but the key's
105 definition will not be changed other than that. If that wouldn't have been done,
106 the callbacks of the different choices could get called multiple times.
107
108 \begin{function}[module=expkv-def prefix]{protected,protect}
109 The following \key\ will be defined |\protected|. Note that \types\ which
110 can't be defined expandable will always use |\protected|. This only affects
111 the key at use time not the \key\ definition.
112 \end{function}
113
114 \begin{function}[module=expkv-def prefix]{long}
115 The following \key\ will be defined |\long| (so can take an explicit |\par|
116 token in its \val). Please note that this only changes the \key\ at use time.
117 |long| being present or not doesn't limit you to use |\par| inside of the
118 \key's definition (if the \type\ allows this).
119 \end{function}
120
121
122 \subsubsection{\textit{Types}\label{sec:d:types}}
123
124 Since the \prefixes\ apply to some of the \types\ automatically but sometimes
125 one might be disallowed we need some way to highlight this behaviour. In the
126 following an enforced \prefix\ will be printed black
127 (\texttt{\enfprefix{protected}}), allowed \prefixes\ will be grey
128 (\texttt{\allprefix{protected}}), and disallowed \prefixes\ will be red
129 (\texttt{\notprefix{protected}}). This will be put flush-right in the syntax
130 showing line.
131
132 \begin{function}[module=expkv-def type]{code,ecode}
133 \begin{syntax}
134 code \key\ = \marg{definition} \prefixes2222
135 \end{syntax}
136 Define \key\ to be a \Vkey\ expanding to \meta{definition}. You can use |#1|
137 inside \meta{definition} to access the \key's \val. The |ecode| variant will
138 fully expand \meta{definition} inside an |\edef|.
139 \end{function}
140 \ekvset{enverb}{no-tcb,store}% affect all the next examples.
141 \begin{example}
142 {Defining a \Vkey\ with arbitrary effect with the \texttt{code} \type}
143 The following defines the key |foo|, that'll count the number of tokens passed
144 to it (we'll borrow a function from \pkg{expl3} for this). It'll accept
145 explicit |\par| tokens. Also it'll flip the \TeX-if \cs[no-index]{iffoo} to
146 true. The result of the counting will be stored in a count register. (Don't
147 get confused, all the next examples are part of this |\ekvdefinekeys| call, so
148 there is no closing brace here.)
149 \begin{enverb}
150 \ExplSyntaxOn
151 \cs_new_eq:NN \exampleCount \tl_count_tokens:n
152 \ExplSyntaxOff
153 \newcount\examplefoocount
154 \newif\iffoo
155 \ekvdefinekeys{example}
156 {
157 protected long code foo =
158 \footrue
159 \examplefoocount=\exampleCount{#1}\relax
160 \end{enverb}
161 \end{example}
162
163 \begin{function}[module=expkv-def type]{noval,enoval}
164 \begin{syntax}
165 noval \key\ = \marg{definition} \prefixes2223
166 \end{syntax}
167 The |noval| \type\ defines \key\ as a \Nkey\ expanding to \meta{definition}.
168 |enoval| fully expands \meta{definition} inside an |\edef|.
169 \end{function}
170 \begin{example}{An arbitrary \Nkey\ action with the \texttt{noval} \type}
171 The following defines the \Nkey\ |foo| to toggle the \TeX-if
172 \cs[no-index]{iffoo} to false and set |\examplecount| to |0|. It'll be
173 |\protected| and mustn't override any existing key.
174 \begin{enverb}
175 ,new protected noval foo = \foofalse\examplefoocount=0\relax
176 \end{enverb}
177 \end{example}
178
179 \begin{function}[module=expkv-def type]{default,odefault,fdefault,edefault}
180 \begin{syntax}
181 default \key\ = \marg{definition} \prefixes2223
182 \end{syntax}
183 This serves to place a default \val\ for a \Vkey. Afterwards if you use \key\
184 as a \Nkey\ it will be the same as if \key\ got passed \meta{definition} as
185 its \val. The |odefault| variant will expand the key-macro once, so will be
186 slightly quicker, but not change if you redefine the \Vkey\ afterwards. The
187 |fdefault| version will expand the key-code until a non-expandable token or a
188 space is found, a space would be gobbled.\footnotemark{}
189 The |edefault| on the other hand fully expands the key-code with
190 \meta{definition} as its argument in |\expanded|. The \prefix\ |new|
191 means that there should be no \Nkey\ of that name yet.
192 \end{function}%
193 \footnotetext{For those familiar with \TeX-coding: This uses a
194 \cs[no-index]{romannumeral}-expansion}
195 \begin{example}
196 {Setting a default value for a \Vkey\ with the \texttt{default} \type}
197 We later decide that the above behaviour isn't what we need any more and
198 instead redefine the \Nkey\ |foo| to pass some default value to the \Vkey\
199 |foo|.
200 \begin{enverb}
201 ,default foo = {Some creative default text}
202 \end{enverb}
203 \end{example}
204
205 \begin{function}[module=expkv-def type]{initial,oinitial,finitial,einitial}
206 \begin{syntax}
207 initial \key\ = \marg{value} \prefixes3333
208 initial \key
209 \end{syntax}
210 With |initial| you can set an initial \val\ for an already defined \key. It'll
211 just call the \key\ and pass it \val. The |einitial| variant will expand \val\
212 using |\expanded| prior to passing it to the \key\ and the |oinitial|
213 variant will expand the first token in \val\ once. |finitial| will expand
214 \val\ until a non-expandable token or a space is found, a space would be
215 gobbled.\footnotemark
216
217 If you don't provide a \val\ (and no equals sign) the \Nkey\ of the same name
218 is called once (or, if you specified a |default| for a \Vkey\ that would be
219 used).
220 \end{function}%
221 \footnotetext{Again using \cs[no-index]{romannumeral}}
222 \begin{example}{Specifying initial values with the \texttt{initial} \type}
223 We want to get a defined initial behaviour for our |foo|. So we count
224 0~tokens.
225 \begin{enverb}
226 ,initial foo = {}
227 \end{enverb}
228 \end{example}
229
230 \begin{function}[module=expkv-def type]{bool,gbool,boolTF,gboolTF}
231 \begin{syntax}
232 bool \key\ = \meta{cs} \prefixes2223
233 \end{syntax}
234 \singlecs{iffoo}
235 This will define \key\ to be a boolean key, which only takes the values |true|
236 or |false| and will throw an error for other values. If the \key\ is used as
237 a \Nkey\ it'll have the same effect as if you use |true|. |bool| and |gbool|
238 will behave like \TeX-ifs, so either be \cs[no-index]{iftrue} or
239 \cs[no-index]{iffalse}. The \meta{cs} in the |boolTF| and |gboolTF| variants
240 will take two arguments and if true the first will be used else the second, so
241 they are always either |\@firstoftwo| or |\@secondoftwo|. The variants with a
242 leading |g| will set the \meta{cs} globally, the other locally. If \meta{cs}
243 is not yet defined it'll be initialised as the |false| version. Note that the
244 initialisation is \emph{not} done with |\newif|, so you will not be able to do
245 |\footrue| outside of the \kv\ interface, but you could use |\newif| yourself.
246 Even if the \key\ will not be |\protected| the commands which execute the
247 |true| or |false| choice will be, so the usage should be safe in an expansion
248 context (\emph{e.g.}, you can use \texttt{edefault \key\ = false} without an
249 issue to change the default behaviour to execute the |false| choice).
250 Internally a |bool| is the same as a |choice| \type\ which is set up to handle
251 |true| and |false| as choices. |new| will assert that neither the \Vkey\ nor
252 the \Nkey\ are already defined.
253 \end{function}
254 \begin{example}{Defining Boolean keys with the \texttt{bool} \type}
255 Also we want to have a direct way to set our \cs[no-index]{iffoo}, now that
256 the \Nkey\ doesn't toggle it any longer.
257 \begin{enverb}
258 ,bool dofoo = \iffoo
259 \end{enverb}
260 \end{example}
261
262 \begin{function}[module=expkv-def type]{invbool,ginvbool,invboolTF,ginvboolTF}
263 \begin{syntax}
264 invbool \key\ = \meta{cs} \prefixes2223
265 \end{syntax}
266 These are inverse boolean keys, they behave like |bool| and friends but set
267 the opposite meaning to the macro \meta{cs} in each case. So if |key=true| is
268 used |invbool| will set \meta{cs} to \cs[no-index]{iffalse} and vice versa.
269 \end{function}
270 \begin{example}
271 {Inversing the logic of a Boolean with the \texttt{invbool} \type}
272 And since traditional interfaces lacked \kv\ support for packages, often a
273 negated boolean key was used as well.
274 \begin{enverb}
275 ,invbool nofoo = \iffoo
276 \end{enverb}
277 \end{example}
278
279 \begin{function}[module=expkv-def type]{boolpair,gboolpair,boolpairTF,gboolpairTF}
280 \begin{syntax}
281 boolpair \key\ = \meta{cs_1}\meta{cs_2} \prefixes2223
282 \end{syntax}
283 The |boolpair| \type\ behaves like both |bool| and |invbool|, the \meta{cs_1}
284 will be set to the meaning according to the rules of |bool|, and \meta{cs_2}
285 will be set to the opposite.
286 \end{function}
287
288 \begin{function}[module=expkv-def type]{store,estore,gstore,xstore}
289 \begin{syntax}
290 store \key\ = \meta{cs} \prefixes2212
291 \end{syntax}
292 \singlecs{foo}
293 This will define a \Vkey\ to store \val\ inside of the control sequence. If
294 \meta{cs} isn't yet defined it will be initialised as empty. The variants
295 behave similarly to their |\def|, |\edef|, |\gdef|, and |\xdef| counterparts,
296 but will allow you to store macro parameters inside them without needing to
297 double them. So |estore foo = \foo, initial foo = #1| will not result in a low
298 level \TeX\ error.
299 \end{function}
300 \begin{example}
301 {Also store the \val\ of an existing \key\ in a macro using the
302 \texttt{also} \prefix\ and the \texttt{store} \type}
303 Not only do we want to count the tokens handed to |foo|, but we want to also
304 store them inside of a macro (and we don't need to specify |long| here, since
305 |foo| is already |\long| from our |code| definition above).
306 \begin{enverb}
307 ,also store foo = \examplefoostore
308 \end{enverb}
309 \end{example}
310
311 \begin{function}[module=expkv-def type]{data,edata,gdata,xdata}
312 \begin{syntax}
313 data \key\ = \meta{cs} \prefixes2212
314 \end{syntax}
315 \singlecs{foo}
316 This will define a \Vkey\ to store \val\ inside of the control sequence. But
317 unlike the |store| \type\ the macro \meta{cs} will be a switch at the same
318 time, it'll take two arguments and if \meta{key} was used expands to the first
319 argument followed by \val\ in braces, if \key\ was not used \meta{cs} will
320 expand to the second argument (so behave like |\@secondoftwo|). The idea is
321 that with this type you can define a key which should be typeset formatted.
322 The |edata| and |xdata| variants will fully expand \val, the |gdata| and
323 |xdata| variants will store \val\ inside \meta{cs} globally. Just like with
324 |store| you can use macro parameters without having to double them. The
325 \prefixes\ only affect the key-macro, \meta{cs} will always be expandable and
326 |\long|.
327 \end{function}
328 \begin{example}{Define a key using the \texttt{data} \type}
329 Next we start to define other keys, now that our |foo| is pretty much
330 exhausted. The following defines a key |bar| to be a |data| key.
331 \begin{enverb}
332 ,data bar = \examplebar
333 \end{enverb}
334 \end{example}
335
336 \begin{function}[module=expkv-def type]{dataT,edataT,gdataT,xdataT}
337 \begin{syntax}
338 dataT \key\ = \meta{cs} \prefixes2212
339 \end{syntax}
340 Just like |data|, but instead of \meta{cs} grabbing two arguments it'll only
341 grab one, so by default it'll behave like |\@gobble|, and if \val\ was given
342 to \key\ the \meta{cs} will behave like |\@firstofone| appended by
343 \marg{value}.
344 \end{function}
345 \begin{example}{Define a key using the \texttt{dataT} \type}
346 Another key we want to use is |baz|.
347 \begin{enverb}
348 ,dataT baz = \examplebaz
349 \end{enverb}
350 \end{example}
351
352 \begin{function}[module=expkv-def type]{int,eint,gint,xint}
353 \begin{syntax}
354 int \key\ = \meta{cs} \prefixes2212
355 \end{syntax}
356 \singlecs{foo}
357 An |int| key will be a \Vkey\ setting a \TeX\ count register. If \meta{cs}
358 isn't defined yet, |\newcount| will be used to initialise it. The |eint| and
359 |xint| variants will use |\numexpr| to allow basic computations in their \val.
360 The |gint| and |xint| variants set the register globally.
361 \end{function}
362
363 \begin{function}[module=expkv-def type]{dimen,edimen,gdimen,xdimen}
364 \begin{syntax}
365 dimen \key\ = \meta{cs} \prefixes2212
366 \end{syntax}
367 \singlecs{foo}
368 This is just like |int| but uses a dimen register, |\newdimen|, and |\dimexpr|
369 instead.
370 \end{function}
371
372 \begin{function}[module=expkv-def type]{skip,eskip,gskip,xskip}
373 \begin{syntax}
374 skip \key\ = \meta{cs} \prefixes2212
375 \end{syntax}
376 \singlecs{foo}
377 This is just like |int| but uses a skip register, |\newskip|, and |\glueexpr|
378 instead.
379 \end{function}
380 \begin{example}
381 {Define keys that use \TeX\ registers, here a skip with the \texttt{eskip}
382 \type}
383 Exemplary for the different register keys, the following defines |distance| so
384 that we can store some distance.
385 \begin{enverb}
386 ,eskip distance = \exampledistance
387 \end{enverb}
388 \end{example}
389
390 \begin{function}[module=expkv-def type]{toks,gtoks,apptoks,gapptoks,pretoks,gpretoks}
391 \begin{syntax}
392 toks \key\ = \meta{cs} \prefixes2212
393 \end{syntax}
394 \singlecs{foo}
395 Store \val\ inside of a toks-register. The |g| variants use |\global|, the
396 |app| variants append \val\ to the contents of that register, the |pre|
397 variants will prepend \val. If \meta{cs} is not yet defined it will be
398 initialised with |\newtoks|.
399 \end{function}
400
401 \begin{function}[module=expkv-def type]{box,gbox}
402 \begin{syntax}
403 box \key\ = \meta{cs} \prefixes2212
404 \end{syntax}
405 \singlecs{foo}
406 Typesets \val\ into a |\hbox| and stores the result in a box register. The
407 boxes are colour safe. \expkvd\ currently doesn't provide a |vbox| type.
408 \end{function}
409
410 \begin{function}[module=expkv-def type]{meta}
411 \begin{syntax}
412 meta \key\ = \kvarg \prefixes2222
413 \end{syntax}
414 This key \type\ can set other keys, you can access the \val\ given to the
415 created \Vkey\ inside the \kv\ list using |#1|. This works by injecting the
416 \kv\ list into the currently parsed list, so behaves just as if the \kv\ list
417 was directly used instead of \key.
418 \end{function}
419 \begin{example}
420 {Define a \Vkey\ as a shortcut to set multiple other keys with the
421 \texttt{meta} \type}
422 And we want to set a full set of keys with just this single one called |all|.
423 \begin{enverb}
424 ,meta all =
425 {distance=5pt,baz=cheese cake,bar=cocktail bar,foo={#1}}
426 \end{enverb}
427 \end{example}
428
429 \begin{function}[module=expkv-def type]{nmeta}
430 \begin{syntax}
431 nmeta \key\ = \kvarg \prefixes2223
432 \end{syntax}
433 This \type\ sets other keys, but unlike |meta| this defines a \Nkey, so the
434 \kv\ list is static.
435 \end{function}
436 \begin{example}
437 {Set multiple other keys from a \Nkey\ with the \texttt{nmeta} \type}
438 and if |all| is set without a value we want to do something about it as well.
439 \begin{enverb}
440 ,nmeta all =
441 {distance=10pt,baz=nothing,bar=Waikiki bar,foo}
442 \end{enverb}
443 \end{example}
444
445 \begin{function}[module=expkv-def type]{smeta}
446 \begin{syntax}
447 smeta \key\ = \marg{set}\kvarg \prefixes2222
448 \end{syntax}
449 Yet another |meta| variant. |smeta| will define a \Vkey, you can access the
450 given \val\ in the provided \kv\ list using |#1|. Unlike |meta| this will
451 process that \kv\ list inside of \meta{set} using a nested |\ekvset| call, so
452 this is equal to \texttt{\cs[no-index]{ekvset}\marg{set}\kvarg}. As a
453 result you can't use |\ekvsneak| using keys or similar macros in the way you
454 normally could.
455 \end{function}
456
457 \begin{function}[module=expkv-def type]{snmeta}
458 \begin{syntax}
459 snmeta \key\ = \marg{set}\kvarg \prefixes2223
460 \end{syntax}
461 And the last |meta| variant. |snmeta| combines |smeta| and |nmeta|, so parses
462 the \kv\ list inside of \meta{set} and defines a \Nkey\ with a static list.
463 \end{function}
464
465 \begin{function}[module=expkv-def type]{alias}
466 \begin{syntax}
467 alias \key\ = \meta{key_2} \prefixes2333
468 \end{syntax}
469 Copy the definition of \meta{key_2} to \key. \key\ will inherit being |long|
470 or |protected| from \meta{key_2} as well. If a \Vkey\ named \meta{key_2}
471 exists its definition is copied, and if an accordingly named \Nkey\ exists its
472 definition is copied to the new name as well. At least one of the two keys
473 must exist or else this throws an error. If \meta{key_2} is later redefined
474 the definition of \key\ will stay the same.
475 \end{function}
476
477 \begin{function}[module=expkv-def type]{set}
478 \begin{syntax}
479 set \key\ = \marg{set} \prefixes2233
480 set \key
481 \end{syntax}
482 This will define a \Nkey\ that will change the current set to \meta{set}. If
483 you give no value to this definition (omit |= |\marg{set}) the set name will
484 be the same as \key\ so
485 |set |\key\
486 is equivalent to
487 |set |\key| = |\marg{key}.
488 Note that just like in \expkv\ it'll not be checked whether \meta{set} is
489 defined and you'll get a low-level \TeX\ error if you use an undefined
490 \meta{set}.
491 \end{function}
492
493 \begin{function}[module=expkv-def type]{choice}
494 \begin{syntax}
495 choice \key\ = \{\val=\meta{definition}, \ldots\} \prefixes2223
496 \end{syntax}
497 |choice| defines a \Vkey\ that will only accept a limited set of values. You
498 should define each possible \meta{value} inside of the \val=\meta{definition}
499 list. If a defined \meta{value} is passed to \meta{key} the \meta{definition}
500 will be left in the input stream. You can make individual values |protected|
501 inside the \val=\meta{definition} list by using that \prefix. To also allow
502 choices that shouldn't be |\protected| but which start with the word
503 |protected| you can also use |unprotected| as a special \prefix. By default a
504 |choice| key and all its choices are expandable, an undefined \meta{value}
505 will throw an error in an expandable way. You can add additional choices after
506 the \meta{key} was created by using |choice| again for the same \key,
507 redefining choices is possible the same way, but there is no interface to
508 remove certain choices. To change the behaviour of unknown choices see also
509 the |unknown-choice| \type.
510 \end{function}
511 \begin{example}
512 {Define a choice with arbitrary code using the \texttt{choice} \type}
513 We give the users a few choices.
514 \begin{enverb}
515 ,choice choose =
516 {
517 protected lemonade = \def\exampledrink{something sour}
518 ,protected water = \def\exampledrink{something boring}
519 }
520 \end{enverb}
521 \end{example}
522
523 \begin{function}[module=expkv-def type]{choice-store}
524 \begin{syntax}
525 choice-store \key\ = \meta{cs}\{\val=\meta{definition}, \ldots\} \prefixes2223
526 \end{syntax}
527 \singlecs{foo}
528 This is a special \type\ of the |choice| \type\ that'll store the given choice
529 inside the macro \meta{cs}. Since storing inside a macro can't be done
530 expandably every choice-code is |\protected|, and you might define the
531 |choice-store| key itself as |\protected| as well if you want. Inside the
532 \val|=|\meta{definition} list the |=|\meta{definition} part is optional, if
533 you omit it the \val\ will be stored as given during define-time inside of
534 \meta{cs} (during use-time the \val\ needs to be matched |\detokenize|d), and
535 if you specify |=|\meta{definition} that \meta{definition} will be stored
536 inside of \meta{cs} instead. If \meta{cs} doesn't yet exist it's initialised
537 as empty.
538 \end{function}
539 \begin{example}
540 {Show the equivalent setup for a \texttt{choice} \type\ to mimic a
541 \texttt{choice-store} \type}
542 The following keys |key1| and |key2| are equivalent at use time (this doesn't
543 continue the |\ekvdefinekeys|-call for the set |example| above):
544 \begin{enverb}[no-store,no-tcb]
545 \newcommand*\mya{}% initialise \mya
546 \ekvdefinekeys{choice-store-example}
547 {
548 choice key1 =
549 {
550 protected a = \def\mya{a}
551 ,protected b = \def\mya{b}
552 ,protected c = \def\mya{c}
553 ,protected d = \def\mya{FOO}
554 }
555 ,choice-store key2 = \myb{a,b,c,d=FOO}
556 }
557 \end{enverb}
558 \end{example}
559 \begin{example}
560 {Store the user's choices in a macro with the \texttt{choice-store} \type}
561 (this continues the |\ekvdefinekeys|-call for the set |example| from above)
562 After the above drinks we define a few more choices which are directly stored.
563 \begin{enverb}
564 ,choice-store choose = \exampledrink{beer,wine}
565 \end{enverb}
566 One might notice that the entire setup of the |choose| key could've been done
567 using only |choice-store|.
568 \end{example}
569
570 \begin{function}[module=expkv-def type]{choice-enum}
571 \begin{syntax}
572 choice-enum \key\ = \meta{cs}\{\val, \ldots\} \prefixes2223
573 \end{syntax}
574 \singlecs{foo}
575 This is similar to |choice-store|, the differences are: \meta{cs} should be a
576 count register or is initialised as such using |\newcount|; instead of the
577 \val\ itself being stored its position in the list of choices is stored
578 (zero-based). It is not possible to specify a \meta{definition} to store
579 something else than the numerical position inside the list.
580 \end{function}
581 \begin{example}
582 {Show the equivalent setup for a \texttt{choice} \type\ to mimic a
583 \texttt{choice-enum} \type}
584 The following keys |key1| and |key2| are equivalent at use time (another
585 example not using the |example| set of above's |\ekvdefinekeys|):
586 \begin{enverb}[no-store,no-tcb]
587 \newcount\myc
588 \ekvdefinekeys{choice-enum-example}
589 {
590 choice key1 =
591 {
592 protected a={\myc=0 }
593 ,protected b={\myc=1 }
594 ,protected c={\myc=2 }
595 }
596 ,choice-enum key2 = \myd{a,b,c}
597 }
598 \end{enverb}
599 \end{example}
600
601 \begin{function}[module=expkv-def type]{unknown-choice}
602 \begin{syntax}
603 unknown-choice \key\ = \marg{definition} \prefixes2323
604 \end{syntax}
605 By default an unknown \val\ passed to a |choice| or |bool| \type\ (and all
606 their variants) will throw an error. However, with this prefix you can define
607 an alternative action which should be executed if \key\ received an unknown
608 choice. In \meta{definition} you can refer to the given invalid choice with
609 |#1|.
610 \end{function}
611 \begin{example}
612 {Handle unknown choices without throwing an error with the
613 \texttt{unknown-choice} \type}
614 If a drink was chosen with |choose| that's not defined we don't want to throw
615 an error, but store something else instead.
616 \begin{enverb}
617 ,protected unknown-choice choose =
618 \def\exampledrink{something unavailable}
619 }% closing brace for \ekvdefinekeys
620 \end{enverb}
621 \end{example}
622
623 \begin{function}[module=expkv-def type]{choice-aliases}
624 \begin{syntax}
625 choice-aliases \key\ = \{\meta{new}=\meta{old}, \ldots\} \prefixes2333
626 \end{syntax}
627 Copy the definition of the choice \meta{old} of \key\ to a \meta{new} choice
628 for the same \key. The \key\ must be an existing |choice| key. Inside the
629 \meta{new}=\meta{old} list all elements must get a value. If used the
630 |new|-prefix will apply to each individual \meta{new} choice name (so if any
631 already exists it'll throw an error, and the current element will be ignored).
632 The \meta{new} choice will inherit being |protected| from the \meta{old} one.
633 The \meta{old} choice must be defined obviously.
634
635 This works for a |choice| or |bool| \type\ as well as all their variants. If
636 something redefines some of the choices later on the aliases will keep the
637 original definition.
638 \end{function}
639 \begin{example}
640 {Create shortcuts for choices using the \texttt{choice-aliases} \type}
641 With the following we create a key that accepts some choices, and since our
642 keyboard is only designed to handle a finite number of keystrokes we also
643 allow for shorter names of those choices. (This is not part of the ongoing
644 example using the |example| set above.)
645 \begin{enverb}[no-store,no-tcb]
646 \ekvdefinekeys{choice-alias-example}
647 {
648 choice-store key = { long-name, short-name }
649 ,choice-aliases key = { ln = long-name, sn = short-name }
650 }
651 \end{enverb}
652 \end{example}
653
654 \begin{function}[module=expkv-def type]{unknown code}
655 \begin{syntax}
656 unknown code = \marg{definition} \prefixes2322
657 \end{syntax}
658 By default \expkv\ throws errors when it encounters unknown keys in a set.
659 With the |unknown| \type\ you can define handlers that deal with undefined
660 keys, instead of a \key\ name you have to specify a subtype for this, here the
661 subtype is |code|.
662
663 With |unknown code| the \meta{definition} is used for unknown keys which were
664 provided a value (so corresponds to |\ekvdefunknown|), you can access the
665 unknown \key\ name with |#1| (|\detokenize|d), the given \val\ with |#2|, and
666 the unprocessed \key\ name with |#3| (in case you want to further expand
667 it).\footnotemark
668 \end{function}%
669 \footnotetext{There is some trickery involved to get this more intuitive
670 argument order without any performance hit if you compare this to
671 \cs[no-index]{ekvdefunknown} directly}
672
673 \begin{function}[module=expkv-def type]{unknown noval}
674 \begin{syntax}
675 unknown noval = \marg{definition} \prefixes2323
676 \end{syntax}
677 This is like |unknown code| but uses \meta{definition} for unknown keys to
678 which no value was passed (so corresponds to |\ekvdefunknownNoVal|). You can
679 access the |\detokenize|d \key\ name with |#1| and the unprocessed one with
680 |#2|.
681 \end{function}
682
683 \begin{function}[module=expkv-def type]{unknown redirect-code}
684 \begin{syntax}
685 unknown redirect-code = \marg{set-list} \prefixes2331
686 \end{syntax}
687 This uses a predefined action for |unknown code|. Instead of throwing an
688 error, it is tried to find the \meta{key} in each \meta{set} in the comma
689 separated \meta{set-list}. The first found match will be used and the
690 remaining options from the list discarded. If the \meta{key} isn't found in
691 any \meta{set} an expandable error will be thrown eventually. Internally
692 \expkv's |\ekvredirectunknown| will be used.
693 \end{function}
694
695 \begin{function}[module=expkv-def type]{unknown redirect-noval}
696 \begin{syntax}
697 unknown redirect-noval = \marg{set-list} \prefixes2333
698 \end{syntax}
699 This behaves just like |unknown redirect-code| but will set up means to
700 forward keys for |unknown noval|. Internally \expkv's
701 |\ekvredirectunknownNoVal| will be used.
702 \end{function}
703
704 \begin{function}[module=expkv-def type]{unknown redirect}
705 \begin{syntax}
706 unknown redirect = \marg{set-list} \prefixes2333
707 \end{syntax}
708 This is a short cut to apply both, |unknown redirect-code| and
709 |unknown redirect-noval|, as a result you might get doubled error messages,
710 one from each.
711 \end{function}
712
713 \bigskip
714 Time to use all those keys defined in the different examples as part of the
715 |example| set!
716 % undo the global no-tcb setting
717 \ekvset{enverb}{no-store,undo-no-tcb}%
718 \begin{enverb}[restore,below,inter=Which results in three paragraphs of text:]
719 \newcommand\defexample[1][]
720 {%
721 \begingroup % keep the values set local to this call
722 \ekvset{example}{#1}%
723 After walking \the\exampledistance\space we finally reached
724 \examplebar{\emph}{no particular place}.
725 There I ordered
726 \iffoo
727 a drink called \examplefoostore\space (that has
728 \the\examplefoocount\space tokens in it)%
729 \else
730 nothing of particular interest%
731 \fi
732 \examplebaz{ and ate \emph}.
733 Then a friend of mine also chose \exampledrink.
734 \par
735 \endgroup
736 }
737 \defexample[nofoo]
738 \defexample[all,choose=lemonade]
739 \defexample
740 [all=wheat beer,bar=Biergarten,baz=pretzel,choose=champagne]
741 \end{enverb}
742
743 \subsection{Another Example}
744
745 This picks up the standard use case from \autoref{sec:expkv:standard}, but
746 defines the keys using |\ekvdefinekeys|.
747
748 \begin{enverb}
749 \makeatletter
750 \ekvdefinekeys{myrule}
751 {
752 store ht = \myrule@ht
753 ,initial ht = 1ex
754 ,store wd = \myrule@wd
755 ,initial wd = 0.1em
756 ,store raise = \myrule@raise
757 ,initial raise = \z@
758 ,meta lower = {raise={-#1}}
759 }
760 \ekvsetdef\myruleset{myrule}
761 \newcommand*\myrule[1][]
762 {%
763 \begingroup
764 \myruleset{#1}%
765 \rule[\myrule@raise]{\myrule@wd}{\myrule@ht}%
766 \endgroup
767 }
768 \makeatother
769 a\myrule\par
770 a\myrule[ht=2ex,lower=.5ex]\par
771 \myruleset{wd=5pt}
772 a\myrule
773 \end{enverb}