Debian Codegraph @

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

raw
1 \genericekv\expkv{}
2
3 This package supports two different front ends to parse a \kv\ list. The first
4 (|\ekvset|) is similar to \pkg{keyval}'s |\setkeys|, it parses the list and
5 executes defined actions based on the encountered \key{}s. The second
6 (|\ekvparse|) is more versatile, it only splits the list into \key{}s and
7 \val{}s and then runs user-provided code on the result.
8
9 The first is described in subsections~\ref{sec:expkv:setup}
10 to~\ref{sec:expkv:set}, the latter is described in \autoref{sec:expkv:parse}.
11
12 Unlike the other packages in the bundle, if you load \expkv\ as a \LaTeXe\
13 package there is a single option available:
14 \begin{function}{all}
15 \begin{syntax}
16 \cs[no-index]{usepackage}[all]\{expkv\}
17 \end{syntax}
18 Loads all the packages of \expkvbundle.
19 \end{function}
20
21 \subsection{General Parsing Rules}
22
23 \expkv\ parses a \kv\ list by first splitting the elements on commas (active or
24 other), then looking for an equals sign (active or other). If there is one the
25 \kv\ pair will be split at the first. From both \key\ and \val\ (if there was a
26 \val) one set of outer spaces is stripped, and afterwards one set of outer
27 braces (meaning braces which are around the complete remainder after space
28 stripping if there are any).
29
30 So the syntax looks something like the following pseudo-input:
31 \begin{syntaxexample}
32 \vissp{ \marg{key} = \marg{value} }
33 \end{syntaxexample}
34 with the displayed spaces and braces being optional and removed if found. Note
35 that if you want either \key\ or \val\ to include a comma the braces become
36 mandatory, the same is true if \key\ should contain an equals sign.
37
38 \subsubsection{Expansion Control\label{sec:expkv:expansion}}
39
40 \expkv\ provides a mechanism to specify expansions of a \key\ and/or \val. For
41 those familiar with \pkg{pgfkeys} this is similar to its |.expand once| or
42 |.expanded| handlers. This concept will be called \expnotation\ or \expansion\
43 throughout this documentation.
44
45 The syntax for this notation is a leading list of \expansion-rules followed by a
46 colon that is immediately followed by a space. Also the \expansion-rules must
47 not contain any spaces outside of braces, and the remainder on the right hand
48 side of the colon must not be blank, else it is not considered an \expnotation\
49 but just a weirdly formed \key-name.
50
51 The entire syntax of a \kv\ pair is
52 \begin{syntaxexample}
53 \vissp{ \{\expansion: \asciispace\marg{key} \} = \marg{value} }
54 \end{syntaxexample}
55 Note that the \expansion\ prefix is right delimited by \verb*|: | so the space
56 after the colon is only optional in the sense that the entire \expansion\ prefix
57 is optional. Else all displayed spaces and braces are optional, the inner set of
58 spaces and braces around \key\ only being optional if the optional \expansion\
59 prefix (\expansion\verb*|: |) was present. If that part was present the list of
60 \expansion-rules will be executed, which might change the contents of both \key\
61 and \val. For |\ekvparse| this is always true, however in |\ekvset| it is only
62 parsed for the \expnotation\ if there is no \key\ matching the given input (so
63 this notation doesn't impose a restriction on key names, though \key-names
64 actually containing what would otherwise be an \expansion\ prefix should be
65 pretty rare in practice).
66
67 All packages in \expkvbundle\ support this notation (most of them internally use
68 |\ekvset| or |\ekvparse|). Please note however that while \expkvo\ fully
69 supports them, reinsertion via the |\r| \expansion-rule might affect the unused
70 global options list if used in the class options.
71
72 An \expansion-rule consists of a single token. In a \Vkey\ they work on the
73 \val\ (but you can use the |\key| rule to also affect the \key\ there) while in
74 a \Nkey\ they work on the \key. The following rules are available (those
75 familiar with \pkg{expl3} will notice that the first six are identical to its
76 argument types):
77
78 \begingroup
79 \let\bigskip\medskip
80 \begin{function}[module=Expansion]{o}
81 Expands the first token once.
82 \end{function}
83
84 \begin{function}[module=Expansion]{e}
85 Expands the entire \val\ inside of |\expanded|.
86 \end{function}
87
88 \begin{function}[module=Expansion]{c}
89 Builds a |\csname| from the contents.
90 \end{function}
91
92 \begin{function}[module=Expansion]{f}
93 Expands the contents until a space or an unexpandable token is found (the
94 space would be removed).
95 \end{function}
96
97 \begin{function}[module=Expansion]{V}
98 The \val\ should be a single token, either defined as a parameterless macro or
99 as a register (via |\newcount| etc.). This expands to the value of the
100 register or the macro's replacement text. If the token in \val\ has the
101 |\meaning| of |\relax| an error is thrown and the result is empty.
102 \end{function}
103
104 \begin{function}[module=Expansion]{v}
105 This is a combination of |c| and |V|, meaning the \val\ is turned into a
106 single control sequence via |\csname|, and then expanded to its value. The
107 control sequence will only be built if it's defined.
108 \end{function}
109 \begin{example}{The difference between \texttt{co} and \texttt{v} expansion}
110 Say we want to hand the contents of a macro as the value to our key, but the
111 actual macro name depends on user input. For this we have two options which
112 behave slightly different. One is to use |v| the other is to combine the |co|
113 \expansion-rules. The following demonstrates both (I modified the way errors
114 are thrown to instead output them in red for this; you'll learn about
115 |\ekvparse| in a few pages, for now just stick with me):
116 \expkvdocPrintErrors
117 \makeatletter
118 \begin{enverb}[below]
119 \newcommand\mypair[2]{Arg: `\detokenize{#2}'. }%
120 \newcommand\myvalue{Value}%
121 \ekvparse\@firstofone\mypair
122 {
123 co: key = myvalue, v: key = myvalue, \par
124 ,co: key = myValue, v: key = myValue, \par
125 }
126 \end{enverb}
127 The difference is that in |co| the variable is implicitly initialised as
128 |\relax| by |c| if it doesn't exist and then doesn't expand in |o|. On the
129 other hand |v| will check whether the variable would exist and throw an error
130 if it doesn't (and will not set it to |\relax| by blindly using |\csname|).
131 \end{example}
132
133 \begin{function}[module=Expansion]{s}
134 Strips one set of outer spaces and outer braces.
135 \end{function}
136
137 \begin{function}[module=Expansion]{b}
138 Adds one set of outer braces.
139 \end{function}
140
141 \begin{function}[module=Expansion]{p}
142 \begin{syntax}
143 p\marg{contents}
144 \end{syntax}
145 Places \meta{contents} before the \val.
146 \end{function}
147
148 \begin{function}[module=Expansion]{P}
149 \begin{syntax}
150 P\marg{contents}
151 \end{syntax}
152 Places \meta{contents} after the \val.
153 \end{function}
154
155 \begin{function}[module=Expansion]{g}
156 Gobbles the first token or balanced group on the left (leads to a low-level
157 \TeX-error if the \val\ is empty).
158 \end{function}
159
160 \begin{function}[module=Expansion]{\r}
161 In a \Vkey\ reinserts the contents of \val\ after all the \expansion-rules
162 were executed (the \key-name needs to be empty). In a \Nkey\ the contents of
163 \key\ are reinserted after all the \expansion-rules were executed (the \val\
164 needs to be empty, which is an easy to fulfil rule as there was no \val).
165 Normal \kv\ parsing is aborted afterwards for the current \kv\ list element.
166 \end{function}
167 \begin{example}{Parse the contents of a macro as additional \kv\ input}
168 Say we want to store a list of common settings in a macro, then we want to
169 parse a few keys, insert the contents of the macro, and parse a few more keys.
170 The following does exactly that (|\ekvset| is analogue to |\setkeys| of the
171 \pkg{keyval} package if you're familiar with it, else you'll learn about
172 |\ekvset| a few pages down the road so be patient):
173 \begin{enverb}[no-tcb]
174 \newcommand*\mykeylist{color=red,height=5cm}
175 \ekvset{mypkg}{key=value, o\r: \mykeylist, other key=other value}
176 \end{enverb}
177 You could also use the following with the same outcome, but this looks more
178 complicated so the other form should be preferred:
179 \begin{enverb}[no-tcb]
180 \ekvset{mypkg}{key=value, o\r: {}=\mykeylist, other key=other value}
181 \end{enverb}
182 \end{example}
183
184 \begin{function}[module=Expansion]{\key}
185 \begin{syntax}
186 \cs[module=Expansion]{key}\marg{expansion}
187 \end{syntax}
188 This is the only supported way to change the contents of \key\ for a \Vkey\ in
189 the \expnotation. All the rules in \expansion\ are applied to \key\ instead of
190 \val.
191 \end{function}
192
193 \begin{function}[module=Expansion]{R}
194 This is the same as if you used |V\r|. So it expects a single token, retrieves
195 its value, and reinserts this as additional \kv\ input.
196 \end{function}
197
198 \begin{function}[module=Expansion]{r}
199 This is the same as if you used |v\r|. So it builds a |\csname| if that is
200 defined, retrieves its value, and reinserts this as additional \kv\ input.
201 \end{function}
202 \begin{example}
203 {Parse the contents of a macro as additional \kv\ input (revisited)}
204 Now that we also know the |R| and |r| rule, the example above can be input
205 even simpler:
206 \begin{enverb}[no-tcb]
207 \ekvset{mypkg}{key=value, R: \mykeylist, other key=other value}
208 \end{enverb}
209 or
210 \begin{enverb}[no-tcb]
211 \ekvset{mypkg}{key=value, r: mykeylist, other key=other value}
212 \end{enverb}
213 \end{example}
214 \endgroup
215
216 \subsection{Setting up Keys\label{sec:expkv:setup}}
217
218 \expkv\ provides a rather simple approach to setting up keys, similar to
219 \pkg{keyval}. If you're looking for a more sophisticated interface similar to
220 those of \pkg{l3keys} or \pkg{pgfkeys} take a look at \expkvd\ described in
221 \autoref{sec:expkv-def} or for a simple interface that defines expandable macros
222 at \expkvc\ described in \autoref{sec:expkv-cs}.
223
224 Keys in \expkv\ (as in many other \kv\ implementations) belong to a \emph{set},
225 so that different sets can contain keys of the same name. Unlike many other
226 implementations \expkv\ doesn't provide means to set a default value, instead we
227 have keys that take a value (we call those \Vkey) and keys that don't (which are
228 called \Nkey\ by \expkv), but both can share the same name on the user level,
229 the only difference for the user is whether =\val\ was used or not.
230
231 The following macros are available to define new keys. Those macros containing
232 \enquote{\texttt{def}} in their name can be prefixed by anything allowed to
233 prefix |\def| (but \emph{don't} use |\outer|, keys defined with it won't ever be
234 usable). And prefixes allowed for |\let| can prefix those macros with
235 \enquote{\texttt{let}} in their name, accordingly. Neither \set\ nor \key\ are
236 allowed to be empty for new keys. \set\ will be used as is inside of
237 |\csname ...\endcsname| and \key\ will get |\detokenize|d. Also \set\ should not
238 contain an explicit |\par| token.
239
240 \begin{function}{\ekvdef}
241 \begin{syntax}
242 \cs{ekvdef}\marg{set}\marg{key}\marg{code}
243 \end{syntax}
244 Defines a \Vkey\ in a \set\ to expand to \meta{code}. In \meta{code} you can
245 use |#1| to refer to the given \val.
246 \end{function}
247 \begin{example}{Define a single \Vkey}
248 Define |text| in |foo| to store the \val\ inside |\foo@text|:
249 \begin{enverb}[no-tcb]
250 \protected\long\ekvdef{foo}{text}{\def\foo@text{#1}}
251 \end{enverb}
252 \end{example}
253
254 \begin{function}{\ekvdefNoVal}
255 \begin{syntax}
256 \cs{ekvdefNoVal}\marg{set}\marg{key}\marg{code}
257 \end{syntax}
258 Defines a \Nkey\ in \set\ to expand to \meta{code}.
259 \end{function}
260 \begin{example}{Define a single \Nkey}
261 Define |bool| in |foo| to set |\iffoo@bool| to |true|:
262 \begin{enverb}[no-tcb]
263 \protected\ekvdefNoVal{foo}{bool}{\foo@booltrue}
264 \end{enverb}
265 \end{example}
266
267 \begin{function}{\ekvlet}
268 \begin{syntax}
269 \cs{ekvlet}\marg{set}\marg{key}\meta{cs}
270 \end{syntax}
271 Let the \Vkey\ in \set\ to \meta{cs}. There are no checks on \meta{cs}
272 enforced, but the code should expect the \val\ as a single braced argument
273 directly following it.
274 \end{function}
275 \begin{example}{Copy a macro to define a \Vkey}
276 Let |cmd| in |foo| do the same as |\foo@cmd|:
277 \begin{enverb}[no-tcb]
278 \ekvlet{foo}{cmd}\foo@cmd
279 \end{enverb}
280 \end{example}
281
282 \begin{function}{\ekvletNoVal}
283 \begin{syntax}
284 \cs{ekvletNoVal} \marg{set} \marg{key} \meta{cs}
285 \end{syntax}
286 Let the \Nkey\ in \set\ to \meta{cs}. Again no checks on \meta{cs} are done.
287 It shouldn't expect any provided argument.
288 \end{function}
289 \begin{example}{Copy a macro to define a \Nkey}
290 See above.
291 \end{example}
292
293 \begin{function}{\ekvletkv}
294 \begin{syntax}
295 \cs{ekvletkv}\marg{set}\marg{key}\marg{set2}\marg{key2}
296 \end{syntax}
297 Copies the definition such that \Vkey\ in \set\ behaves like \meta{key2} of
298 \meta{set2}. It is not checked whether that second key exists!
299 \end{function}
300 \begin{example}{Copy an existing \Vkey}
301 Let |B| in |bar| do the same as |A| in |foo|:
302 \begin{enverb}[no-tcb]
303 \ekvletkv{bar}{B}{foo}{A}
304 \end{enverb}
305 \end{example}
306
307 \begin{function}{\ekvletkvNoVal}
308 \begin{syntax}
309 \cs{ekvletkvNoVal}\marg{set}\marg{key}\marg{set2}\marg{key2}
310 \end{syntax}
311 And this lets the \Nkey\ in \set\ to the definition of the |NoVal|-\meta{key2}
312 in \meta{set2}. Again, it is not checked whether the second key exists.
313 \end{function}
314 \begin{example}{Copy an existing \Nkey}
315 See above.
316 \end{example}
317
318 \subsection{Handle Unknown Keys}
319
320 By default \expkv\ throws an error message if it encounters an undefined \key.
321 You can change this behaviour with the macros listed here. Just like in the
322 section above, prefixes for |\def| are allowed if the macro has |def| in its
323 name, and |\let| prefixes are allowed if the macro is named something with
324 |let|.
325
326 \begin{function}{\ekvdefunknown}
327 \begin{syntax}
328 \cs{ekvdefunknown}\marg{set}\marg{code}
329 \end{syntax}
330 Execute \meta{code} if an undefined \Vkey\ is encountered while parsing in
331 \set. You can refer to the given \val\ with |#1|, the unknown \key's name
332 with |#2| (will be |\detokenize|d), and to the \key's name without
333 |\detokenize| applied with |#3| in \meta{code} (this order is chosen for
334 performance reasons).
335
336 \cs{ekvdefunknown} and \cs{ekvredirectunknown} are mutually exclusive, you
337 can't use both.
338 \end{function}
339 \begin{example}{Search undefined \Vkey{}s in another \set}
340 Also search |bar| for undefined keys of set |foo| (and use the not yet
341 |\detokenize|d \key's name in case the undefined key handler of |bar| needs
342 that):
343 \begin{enverb}[no-tcb]
344 \long\ekvdefunknown{foo}{\ekvset{bar}{{#3}={#1}}}
345 \end{enverb}
346 This example differs from using |\ekvredirectunknown{foo}{bar}| (see below) in
347 that also the unknown-key handler of the |bar| set will be triggered, error
348 messages for undefined keys will look different, and this is slower than using
349 \cs{ekvredirectunknown}.
350 \end{example}
351
352 \begin{function}{\ekvdefunknownNoVal}
353 \begin{syntax}
354 \cs{ekvdefunknownNoVal}\marg{set}\marg{code}
355 \end{syntax}
356 With this you can let \expkv\ execute \meta{code} if an unknown \Nkey\ was
357 encountered. You can refer to the given \key\ with |#1| (will be
358 |\detokenize|d), and to the not |\detokenize|d \key's name with |#2|.
359
360 \cs{ekvdefunknownNoVal} and \cs{ekvredirectunknownNoVal}
361 are mutually exclusive, you can't use both.
362 \end{function}
363 \begin{example}{Search undefined \Nkey{}s in another \set}
364 Adding to the above also handling of \Nkey{}s in |foo|:
365 \begin{enverb}[no-tcb]
366 \ekvdefunknownNoVal{foo}{\ekvset{bar}{{#2}}}
367 \end{enverb}
368 \end{example}
369
370 \begin{function}{\ekvredirectunknown}
371 \begin{syntax}
372 \cs{ekvredirectunknown}\marg{set}\marg{set-list}
373 \end{syntax}
374 This is a short cut to set up a special \cs{ekvdefunknown}-rule for \set\ that
375 will check each set in the comma separated \meta{set-list} for an unknown
376 \Vkey. The resulting unknown-key handler will always be |\long| and \emph{not}
377 |\protected|. The first set in \meta{set-list} has highest priority, once the
378 \Vkey\ is found in one of the sets the remainder of the list is discarded. If
379 \meta{key} isn't found in any of the sets an error will be thrown eventually.
380 Note that the error message looks different than a normal key-not-found error,
381 in particular no unwanted-value message can be thrown (it will not be checked
382 if a \Nkey\ of the same name does exist), and the error message will contain
383 all sets.
384
385 \cs{ekvdefunknown} and \cs{ekvredirectunknown}
386 are mutually exclusive, you can't use both.
387 \end{function}
388 \begin{example}{Search an undefined \Vkey\ in a list of other \set{}s}
389 For every undefined \Vkey\ in |foo| also search the sets |bar| and |baz|:
390 \begin{enverb}[no-tcb]
391 \ekvredirectunknown{foo}{bar, baz}
392 \end{enverb}
393 \end{example}
394
395 \begin{function}{\ekvredirectunknownNoVal}
396 \begin{syntax}
397 \cs{ekvredirectunknownNoVal}\marg{set}\marg{set-list}
398 \end{syntax}
399 This behaves just like \cs{ekvredirectunknown}, it does the same but for
400 \Nkey{}s. Again no prefixes are supported (the result will neither be |\long|
401 nor |\protected|). Note that the error messages will not check whether a
402 missing-value error should be thrown.
403
404 \cs{ekvdefunknownNoVal} and \cs{ekvredirectunknownNoVal}
405 are mutually exclusive, you can't use both.
406 \end{function}
407 \begin{example}{Search an undefined \Nkey\ in a list of other \set{}s}
408 See above.
409 \end{example}
410
411 \begin{function}{\ekvletunknown}
412 \begin{syntax}
413 \cs{ekvletunknown}\marg{set}\meta{cs}
414 \end{syntax}
415 This lets the handler for unknown \Vkey{}s to \meta{cs}. \meta{cs} should
416 expect three arguments, the first will be the \val\ the second the
417 |\detokenize|d \key-name, the third the unprocessed \key-name. No conditions
418 on \meta{cs} are enforced.
419 \end{function}
420 \begin{example}
421 {Do the same as an already defined macro if an unknown \Vkey\ is found}
422 Let the set |foo| do the same as the macro |\foo@unknown| whenever an unknown
423 \Vkey\ is encountered:
424 \begin{enverb}[no-tcb]
425 \ekvletunknown{foo}\foo@unknown
426 \end{enverb}
427 \end{example}
428
429 \begin{function}{\ekvletunknownNoVal}
430 \begin{syntax}
431 \cs{ekvletunknownNoVal}\marg{set}\meta{cs}
432 \end{syntax}
433 This does the same as \cs{ekvletunknown} but for \Nkey{}s. The \meta{cs}
434 should expect two arguments, namely the |\detokenize|d \key\ and the
435 unprocessed \key.
436 \end{function}
437 \begin{example}{Silently ignore unknown \Nkey{}s}
438 Let the set |foo| ignore unknown \Nkey{}s by gobbling the \key-name:
439 \begin{enverb}[no-tcb]
440 \ekvletunknownNoVal{foo}\@gobbletwo
441 \end{enverb}
442 \end{example}
443
444 \subsection{Helpers in Actions}
445
446 \begin{function}{\ekvifdefined,\ekvifdefinedNoVal}
447 \begin{syntax}
448 \cs{ekvifdefined}\marg{set}\marg{key}\marg{true}\marg{false}
449 \cs{ekvifdefinedNoVal}\marg{set}\marg{key}\marg{true}\marg{false}
450 \end{syntax}
451 These two macros test whether there is a \key\ in \set. It is false if either
452 a hash table entry doesn't exist for that key or its meaning is |\relax|.
453 \end{function}
454 \begin{example}{Check if a \Vkey\ is already defined}
455 Check whether the key |special| is already defined in set |foo|, if it isn't
456 input a file that contains more key definitions:
457 \begin{enverb}[no-tcb]
458 \ekvifdefined{foo}{special}{}{\input{foo.morekeys.tex}}
459 \end{enverb}
460 \end{example}
461
462 \begin{function}{\ekvifdefinedset}
463 \begin{syntax}
464 \cs{ekvifdefinedset}\marg{set}\marg{true}\marg{false}
465 \end{syntax}
466 This macro tests whether \set\ is defined (which it is if at least one key was
467 defined for it). If it is \meta{true} will be run, else \meta{false}.
468 \end{function}
469 \begin{example}{Check if a \set\ is already defined}
470 Check whether the set |VeRyUnLiKeLy| is already defined, if so throw an error,
471 else do nothing:
472 \begin{enverb}[no-tcb]
473 \ekvifdefinedset{VeRyUnLiKeLy}
474 {\errmessage{VeRyUnLiKeLy already defined}}{}
475 \end{enverb}
476 \end{example}
477
478 \begin{function}{\ekvsneak,\ekvsneakPre}
479 \begin{syntax}
480 \cs{ekvsneak}\marg{after}
481 \end{syntax}
482 Puts \meta{after} after the effects of \cs{ekvset} (without cancelling the
483 current \cs{ekvset} call). The first variant will put \meta{after} after any
484 other tokens which might have been sneaked before, while \cs{ekvsneakPre} will
485 put \meta{after} before other smuggled stuff. After \cs{ekvset} has parsed the
486 entire \kv\ list everything that has been \cs{ekvsneak}ed will be left in the
487 input stream.
488 \end{function}
489 \begin{example}{Execute code after \cs[no-index]{ekvset} if a \Nkey\ was used}
490 Define a key |secret| in the set |foo| that will sneak out
491 |\foo@secretly@sneaked|:
492 \begin{enverb}[no-tcb]
493 \ekvdefNoVal{foo}{secret}{\ekvsneak{\foo@secretly@sneaked}}
494 \end{enverb}
495 \end{example}
496 A more elaborate usage example is shown in \autoref{sec:expkv:sneak}.
497
498 \begin{function}{\ekvbreak,\ekvbreakPreSneak,\ekvbreakPostSneak}
499 \begin{syntax}
500 \cs{ekvbreak}\marg{after}
501 \end{syntax}
502 Gobbles the remainder of the current \cs{ekvset} call and its argument list
503 and inserts \meta{after}. So this can be used to break out of \cs{ekvset}. The
504 first variant will also gobble anything that has been sneaked out using
505 \cs{ekvsneak} or \cs{ekvsneakPre}, while \cs{ekvbreakPreSneak} will put
506 \meta{after} before anything that has been smuggled and \cs{ekvbreakPostSneak}
507 will put \meta{after} after the stuff that has been sneaked out.
508 \end{function}
509 \begin{example}{Stop parsing a \kv\ list if a specific \Nkey\ was used}
510 Define a key |abort| that will stop key parsing inside the set |foo| and
511 execute |\foo@aborted|, or if it got a value |\foo@aborted@with|:
512 \begin{enverb}[no-tcb]
513 \ekvdefNoVal{foo}{abort}{\ekvbreak{\foo@aborted}}
514 \ekvdef{foo}{abort}{\ekvbreak{\foo@aborted@with{#1}}}
515 \end{enverb}
516 \end{example}
517
518 \begin{function}{\ekvmorekv}
519 \begin{syntax}
520 \cs{ekvmorekv}\kvarg
521 \end{syntax}
522 Adds the contents of the \kv\ list to the list processed by the current call
523 of |\ekvset|.
524 \end{function}
525 \begin{example}{Use one \key\ to set multiple other keys}
526 Define a \Nkey\ |style| that sets the keys |border|, |width|, and |height| as
527 a shortcut:
528 \begin{enverb}[no-tcb]
529 \ekvdefNoVal{foo}{style}{\ekvmorekv{border, width=2cm, height=1.5ex}}
530 \end{enverb}
531 \end{example}
532
533 \begin{function}{\ekvchangeset}
534 \begin{syntax}
535 \cs{ekvchangeset}\marg{new-set}
536 \end{syntax}
537 Replaces the current \set\ with \meta{new-set}, so for the rest of the current
538 \cs{ekvset} call that call behaves as if it was called with
539 \cs{ekvset}\marg{new-set}. It is comparable to using \key|/.cd| in
540 \pkg{pgfkeys}.
541 \end{function}
542 \begin{example}{Change the current \set}
543 Define a key |cd| in set |foo| that will change to another set as specified in
544 the \val. If the set is undefined it'll stop the parsing and throw an error as
545 defined in the macro |\foo@cd@error|:
546 \begin{enverb}[no-tcb]
547 \ekvdef{foo}{cd}
548 {\ekvifdefinedset{#1}{\ekvchangeset{#1}}{\ekvbreak{\foo@cd@error}}}
549 \end{enverb}
550 \end{example}
551
552 \subsection{Parsing Keys in Sets}\label{sec:expkv:set}
553
554 \begin{function}{\ekvset}
555 \begin{syntax}
556 \cs{ekvset}\marg{set}\kvarg
557 \end{syntax}
558 This macro parses the \kv\ list and checks for defined \key{}s that are in
559 \set. Unlike the generic \cs{ekvparse} this macro uses |\detokenize| on the
560 \key\ before checking whether it is a defined key.
561
562 \cs{ekvset} is nestable, and fully expandable. But it is \emph{not} alignment
563 safe. As a result \key\ names and \val{}s that contain an |&| must be wrapped
564 in braces if \cs{ekvset} is used inside an alignment (like \LaTeXe's |tabular|
565 environment) or alternatively you have to create a wrapper that ensurs an
566 alignment safe context.
567 \end{function}
568 \begin{example}{Set defined keys using \cs[no-index]{ekvset}}
569 Parse |key=arg, key| in set |foo|:
570 \begin{enverb}[no-tcb]
571 \ekvset{foo}{key=arg, key}
572 \end{enverb}
573 \end{example}
574
575 \begin{function}{\ekvsetSneaked}
576 \begin{syntax}
577 \cs{ekvsetSneaked}\marg{set}\marg{sneak}\kvarg
578 \end{syntax}
579 This behaves like \cs{ekvset} in which \cs{ekvsneak} was immediately called.
580 \end{function}
581 \begin{example}
582 {Set defined keys and execute code afterwards using
583 \cs[no-index]{ekvsetSneaked}}
584 Parse |key=arg, key| in the set |foo| with |\afterwards| sneaked out:
585 \begin{enverb}[no-tcb]
586 \ekvsetSneaked{foo}{\afterwards}{key=arg, key}
587 \end{enverb}
588 \end{example}
589
590 \begin{function}{\ekvsetdef}
591 \begin{syntax}
592 \cs{ekvsetdef}\meta{cs}\marg{set}
593 \end{syntax}
594 Defines the macro \meta{cs} to be a shortcut for \cs{ekvset}\marg{set}. You
595 can use any \TeX-prefix allowed to prefix |\def| for \cs{ekvsetdef} (so
596 |\long|, |\protected|, or |\global| -- don't use |\outer|).
597 The resulting macro is faster than but else equivalent to the idiomatic
598 definition:\par
599 |\def|\meta{cs}|#1{|\cs{ekvset}\marg{set}|{#1}}|
600 \end{function}
601 \begin{example}
602 {Define a setup command for a defined \set\ using \cs[no-index]{ekvsetdef}}
603 Define the macro |\foosetup| to parse keys in the set |foo| and use it to
604 parse |key=arg, key|:
605 \begin{enverb}[no-tcb]
606 \ekvsetdef\foosetup{foo}
607 \foosetup{key=arg, key}
608 \end{enverb}
609 \end{example}
610
611 \begin{function}{\ekvsetSneakeddef}
612 \begin{syntax}
613 \cs{ekvsetSneakeddef}\meta{cs}\marg{set}
614 \end{syntax}
615 Just like \cs{ekvsetdef} this defines a shorthand macro \meta{cs}, but this
616 will make it a shorthand for \cs{ekvsetSneaked}, meaning \meta{cs} will take
617 two arguments (first the \cs{ekvsneak} argument, then the \kv\ list). Hence
618 the result is a faster version of:\par
619 |\long\def|\meta{cs}|#1#2{|\cs{ekvsetSneaked}\marg{set}|{#1}{#2}}|
620 \end{function}
621 \begin{example}
622 {Define a setup command that will also require code to execute after all
623 keys were processed using \cs[no-index]{ekvsetSneakeddef}}
624 Define the macro |\foothings| to parse keys in the set |foo| and accept a
625 sneaked argument, then use it to parse |key=arg, key| and sneak |\afterwards|:
626 \begin{enverb}[no-tcb]
627 \ekvsetSneakeddef\foothings{foo}
628 \foothings{\afterwards}{key=arg, key}
629 \end{enverb}
630 \end{example}
631
632 \begin{function}{\ekvsetdefSneaked}
633 \begin{syntax}
634 \cs{ekvsetdefSneaked}\meta{cs}\marg{set}\marg{sneaked}
635 \end{syntax}
636 This macro behaves like \cs{ekvsetSneakeddef}, but with a fixed \meta{sneaked}
637 argument. So the resulting macro is faster than but else equivalent to\par
638 |\long\def|\meta{cs}|#1{|\cs{ekvsetSneaked}\marg{set}\marg{sneaked}|{#1}}|
639 \end{function}
640 \begin{example}
641 {Define a setup command that will execute codes after all keys were
642 processed using \cs[no-index]{ekvsetdefSneaked}}
643 Define the macro |\barthing| to parse keys in the set |bar| and always execute
644 |\afterwards| afterwards, then use it to parse |key=arg, key|:
645 \begin{enverb}[no-tcb]
646 \ekvsetdefSneaked\barthing{bar}{\afterwards}
647 \barthing{key=arg, key}
648 \end{enverb}
649 \end{example}
650
651 \begin{function}{\ekvcompile}
652 \begin{syntax}
653 \cs{ekvcompile}\meta{*}\meta{cs}\meta{parameters}\marg{set}\kvarg
654 \end{syntax}
655 This macro defines \meta{cs} to be a \emph{fast} way to set the given \kv\
656 list in \meta{set}. The meaning of the keys is frozen if you don't give the
657 optional |*| (if the star is present the stored content will be the key-macros
658 and later redefinitions of keys will affect them, otherwise the key-macros are
659 expanded once, hence the key-code is stored). This does support the unknown
660 key handlers set up with |\ekvdefunknown| and |\ekvdefunknownNoVal| and also
661 the redirection of unknown keys (the latter will not be expanded exhaustively
662 though, so the key-search is done on every later call of \meta{cs}). Any
663 prefix allowed for |\def| might prefix \cs{ekvcompile}. The list is not
664 entirely fixed, as you might use \meta{parameters} in a \val\ (this is not a
665 single token but a parameter text as you'd use it with |\def|). They can not
666 be part of a \key-name (the names are indeed fixed). If you need a |#| in a
667 \val\ you'll need to double it just as you'd do in |\def|. Internally
668 \cs{ekvcompile} uses |\ekvparse| and no |\ekvset| variant, because of this the
669 \expnotation\ is handled slightly differently; in case you're using a
670 \key-name that starts with something that looks like \expnotation\ you'll have
671 to explicitly add an empty \expansion\ prefix.
672 \end{function}
673 \begin{example}
674 {Compile a \kv\ list into a macro that will quickly set that list}
675 Define the macro |\foo| to set some keys in the set |foo|. Since one key has a
676 strange name we need to add an empty \expansion\ prefix. Also we'd like |\foo|
677 to take one parameter which is part of the \val\ of |bar| (since the list is
678 parsed now and not when |\foo| is used we don't need to put braces around that
679 value, even if at use time |#1| contains commas):
680 \begin{enverb}[no-tcb]
681 \ekvcompile\foo#1{foo}
682 {
683 bar = #1baz
684 ,: part-of-key: name = strange
685 ,NoVal
686 }
687 \end{enverb}
688 After this using |\foo{VAL}| will be the same as but faster than
689 \begin{enverb}[no-tcb]
690 \ekvset{foo}{bar={VALbaz},part-of-key: name=strange,NoVal}
691 \end{enverb}
692 \end{example}
693
694 \subsection{Generic Key Parsing}\label{sec:expkv:parse}
695
696 \begin{function}{\ekvparse}
697 \begin{syntax}
698 \cs{ekvparse}\marg{code1}\marg{code2}\kvarg
699 \end{syntax}
700 This macro parses the \kv\ list and provides \Nkey{}s to \meta{code1} as a
701 single argument and \Vkey{}s with their corresponding \val\ as two arguments
702 to \meta{code2}.
703
704 \cs{ekvparse} is fully expandable and alignment safe, meaning that you don't
705 have to take any extra precautions if it is used inside an alignment context
706 (like \LaTeXe's |tabular| environment) and any \key\ or \val\ can contain an
707 |&|. \cs{ekvparse} expands in exactly two steps, the result is provided inside
708 |\unexpanded| (so doesn't expand further in an |\edef| or |\expanded|
709 context).
710
711 \cs{ekvbreak}, \cs{ekvsneak}, \cs{ekvmorekv}, \emph{etc.}\ don't work
712 in \cs{ekvparse}. \cs{ekvparse} does not throw an error if multiple
713 unprotected equals signs are found (it just splits at the first), and doesn't
714 throw an error if a \key\ is empty. If something looks like \expnotation\ (has
715 a colon followed but not preceded by a space and with non-blank material
716 following it) it'll be parsed as such (which might throw errors due to
717 undefined \expansion-rules if that wasn't the intended input). If you for some
718 reason need to input a \key-name that would match that pattern you'll need to
719 precede it by \verb*|: | (an empty \expansion\ prefix).
720 \end{function}
721 \begin{example}
722 {Parse a \kv\ list and execute arbitrary code for each element using
723 \cs[no-index]{ekvparse}}
724 \begin{enverb}[no-tcb]
725 \ekvparse{\handlekey{S}}{\handlekeyval{S}}{foo = bar, key, baz={zzz}}
726 \end{enverb}
727 would be equivalent to
728 \begin{enverb}[no-tcb]
729 \handlekeyval{S}{foo}{bar}\handlekey{S}{key}\handlekeyval{S}{baz}{zzz}
730 \end{enverb}
731 and afterwards |\handlekey| and |\handlekeyval| would have to further handle
732 the keys. No such macros are contained in \expkv, but I hope you get the idea.
733 Because it expands in two steps and doesn't expand any further both
734 \begin{enverb}[no-tcb]
735 \expandafter\parse\expanded{\ekvparse\k\kv{foo = bar, key, baz={zzz}}}
736 \end{enverb}
737 and
738 \begin{enverb}[no-tcb]
739 \expandafter\expandafter\expandafter
740 \parse\ekvparse\k\kv{foo={bar}, key, baz = zzz}
741 \end{enverb}
742 expand to
743 \begin{enverb}[no-tcb]
744 \parse\kv{foo}{bar}\k{key}\kv{baz}{zzz}
745 \end{enverb}
746 \end{example}
747
748 \subsection{Other Useful Macros}
749
750 \begin{function}{\ekvoptarg}
751 \begin{syntax}
752 \cs{ekvoptarg}\marg{next}\marg{default}
753 \end{syntax}
754 This macro will expandably check for a following optional argument in brackets
755 (|[]|). After the optional argument there has to be a mandatory one (or else
756 this might have unwanted side effects). The code in \meta{next} should expect
757 two arguments (or more), namely the processed optional argument and the
758 mandatory one that followed it. If there was an optional argument the result
759 will be \meta{next}\marg{optional}\meta{mandatory} (so the optional argument
760 will be wrapped in braces, the mandatory argument will be untouched). If there
761 was no optional argument the result will be
762 \meta{next}\marg{default}\marg{mandatory} (so the default will be used and the
763 mandatory argument will be wrapped in braces after it was read once -- if it
764 was already wrapped it is effectively unchanged).
765
766 \cs{ekvoptarg} expands in exactly two steps, grabs all the arguments only at
767 the second expansion step, and is alignment safe. It has its limitations
768 however. It can't tell the difference between |[| and |{[}|, so it doesn't
769 work if the mandatory argument is a single bracket. Also if the optional
770 argument should contain a nested closing bracket it has to be nested in braces
771 like so: |[{arg[u]ment}]| (or else the result would be |arg[u| with a trailing
772 |ment]|).
773 \end{function}
774 \begin{example}{Expandably search for an optional argument with a default value}
775 Say we have a macro that should take an optional argument defaulting to |1|,
776 we could program it like this:
777 \makeatletter
778 \begin{enverb}
779 \newcommand\foo{\ekvoptarg\@foo{1}}
780 \newcommand\@foo[2]{Mandatory: #2\par Optional: #1}
781 \foo{5}\par
782 \foo[4]{5}\par
783 \end{enverb}
784 \end{example}
785
786 \begin{function}{\ekvoptargTF}
787 \begin{syntax}
788 \cs{ekvoptargTF}\marg{true}\marg{false}
789 \end{syntax}
790 This macro is similar to \cs{ekvoptarg} but will result in
791 \meta{true}\marg{optional}\meta{mandatory} or \meta{false}\marg{mandatory}
792 instead of placing a default value.
793
794 \cs{ekvoptargTF} expands in exactly two steps, grabs all the arguments only at
795 the second expansion step, and is alignment safe. It has the same limitations
796 as \cs{ekvoptarg}.
797 \end{function}
798 \begin{example}
799 {Expandably search for an optional argument and behave differently if it's
800 found or not}
801 Say we have a macro that should behave differently depending on whether there
802 was an optional argument or not. This could be done with:
803 \makeatletter
804 \begin{enverb}
805 \newcommand\foo{\ekvoptargTF\foo@a\foo@b}
806 \newcommand\foo@a[2]{Mandatory: #2\par Optional: #1}
807 \newcommand\foo@b[1]{Mandatory: #1\par No optional.}
808 \foo{5}\par
809 \foo[4]{5}\par
810 \end{enverb}
811 \end{example}
812
813 \begin{function}{\ekvcsvloop}
814 \begin{syntax}
815 \cs{ekvcsvloop}\marg{code}\marg{csv-list}
816 \end{syntax}
817 This loops over the comma separated items in \meta{csv-list} and, after
818 stripping spaces from either end of \meta{item} and removing at most one set
819 of outer braces, leaves |\unexpanded{|\meta{code}\marg{item}|}| for each list
820 item in the input stream. Blank elements are ignored (if you need a blank
821 element it should be given as \verb*|{ }|). It supports both active commas and
822 commas of category other. \cs{ekvcsvloop} is not alignment safe, but you could
823 make it so by nesting it in |\expanded| (since the braces around the argument
824 of |\expanded| will hide alignment characters from \TeX's parsing).
825 \end{function}
826 \begin{example}
827 {Loop over a comma separated list and execute arbitrary code for each
828 element}
829 The following splits a comma separated list and prints it in a typewriter font
830 with parentheses around each element:
831 \begin{enverb}
832 \newcommand*\myprocessor[1]{\texttt{(#1)}}
833 \ekvcsvloop\myprocessor{abc,def,ghi}\par
834 \ekvcsvloop\myprocessor{1,,2,,3,,4}\par
835 \end{enverb}
836 \end{example}
837
838 \begin{function}{\ekverr}
839 \begin{syntax}
840 \cs{ekverr}\marg{package}\marg{message}
841 \end{syntax}
842 This macro will throw an error fully expandably.\footnotemark\ The error
843 length is limited to a total length of 69~characters, and since ten characters
844 will be added for the formatting (\verb*|! | and \verb*| Error: |) that leaves
845 us with a total length of \meta{package} plus \meta{message} of 59~characters.
846 If the message gets longer \TeX\ will only display the first 69~characters and
847 append |\ETC.| to the end.
848
849 Neither \meta{package} nor \meta{message} expand any further. Also
850 \meta{package} must not contain an explicit |\par| token or the token
851 |\thanks@jfbu|. No such restriction applies to \meta{message}.
852
853 If |^^J| is set up as the |\newlinechar| (which is the case in \LaTeXe\ but
854 not in \hologo{plainTeX} by default) you can use that to introduce line breaks
855 in your error message. However that doesn't change the message length limit.
856 \end{function}
857 \footnotetext{The used mechanism was to the best of my knowledge first
858 implemented by Jean-François Burnol.}
859 After your own error message some further text will be placed. The formatting
860 of that text will look good if |^^J| is the |\newlinechar|, else not so much.
861 That text will read:
862 \begin{verbatim}
863 ! Paragraph ended before \<an-expandable-macro>
864 completed due to above exception. If the error
865 summary is not comprehensible see the package
866 documentation.
867 I will try to recover now. If you're in inter-
868 active mode hit <return> at the ? prompt and I
869 continue hoping recovery was complete.
870 \end{verbatim}
871 Any clean up has to be done by you, |\ekverr| will expand to nothing after
872 throwing the error message.
873
874 In \ConTeXt\ this macro works differently. While still being fully expandable,
875 it doesn't have the character count limitation and doesn't impose restrictions
876 on \meta{package}. It will not display the additional text and adding line
877 breaks is not possible.
878 \begin{example}{Expandably throw error messages using \cs[no-index]{ekverr}}
879 Say we set up a macro that takes as mandatory argument a simple equation which
880 must not be empty and if it's not empty it displays it and calculates the
881 result:
882 \begin{enverb}[no-tcb]
883 \newcommand*\mycalc[1]
884 {%
885 \the\numexpr
886 \if\relax\detokenize{#1}\relax
887 \ekverr{my}{Empty equation not allowed, leaving -2147483647}%
888 -2147483647%
889 \else
890 #1%
891 \fi
892 \relax
893 }
894 Using \mycalc{} wrong.
895 \end{enverb}
896 If that code gets executed the following will be the terminal output
897 \begin{verbatim}
898 Runaway argument?
899 ! my Error: Empty equation not allowed, leaving -2147483647
900 ! Paragraph ended before \<an-expandable-macro>
901 completed due to above exception. If the error
902 summary is not comprehensible see the package
903 documentation.
904 I will try to recover now. If you're in inter-
905 active mode hit <return> at the ? prompt and I
906 continue hoping recovery was complete.
907 <to be read again>
908 \par
909 l.17 Using \mycalc{}
910 wrong.
911 ?
912 \end{verbatim}
913 and the output would contain
914 \exres{Using -2147483647 wrong}
915 if we continued the \TeX\ run at the prompt.
916 \end{example}
917
918
919 \subsection{Other Macros}
920
921 \begin{variable}{\ekvDate,\ekvVersion}
922 These two macros store \expkv's date and version.
923 \end{variable}
924
925 \begin{function}{\ekv@name,\ekv@name@set,\ekv@name@key}
926 \begin{syntax}
927 \cs{ekv@name}\marg{set}\marg{key}
928 \cs{ekv@name@set}\marg{set}
929 \cs{ekv@name@key}\marg{key}
930 \end{syntax}
931 The names of the macros storing the code of \Vkey{}s are built with these
932 macros. The name is built from two blocks, one that is formatting the \set\
933 name, and on for formatting the \key\ name. To get the actual name the
934 argument to \cs{ekv@name@key} must be |\detokenize|d. Both blocks are put
935 together (with the necessary |\detokenize|) by \cs{ekv@name}. For \Nkey{}s an
936 additional |N| gets appended, so their name is
937 \cs{ekv@name}\marg{set}\marg{key}|N|.
938
939 You can use these macros to implement additional functionality or access
940 key-macros outside of \expkv, but \emph{don't} change them! \expkv\ relies on
941 their exact definitions internally.
942 \end{function}
943 \begin{example}{Directly call key code without parsing a \kv\ list}
944 Execute the key-macro of the \Nkey\ named |bar| in set |foo|:
945 \begin{enverb}[no-tcb]
946 \csname\ekv@name{foo}{bar}N\endcsname
947 \end{enverb}
948 \end{example}
949
950
951 \subsection{Examples}
952 \subsubsection{Standard Use-Case\label{sec:expkv:standard}}
953
954 \begin{example}{A \kv\ based replacement for \LaTeXe's \cs[no-index]{rule}}
955 Because I keep forgetting the correct order of \LaTeXe's |\rule| command I
956 want to create a \kv\ interface to it. For this I define the keys |ht| to
957 specify the rule's height, |wd| to specify its width, and to give a
958 displacement I use two keys (because who can remember whether the rule is
959 moved upwards or downwards?).
960
961 First the internals storing the values are initialised
962 \begin{enverb}[no-tcb,store]
963 \makeatletter
964 \newcommand*\myrule@ht{1ex}
965 \newcommand*\myrule@wd{0.1em}
966 \newcommand*\myrule@raise{\z@}
967 \end{enverb}
968 then the keys are defined. We could use |\dimen| registers instead of defining
969 macros, but macros have the advantage that the font dependent dimensions are
970 evaluated at use time.
971 \begin{enverb}[no-tcb,store]
972 \protected\ekvdef{myrule}{ht}{\def\myrule@ht{#1}}
973 \protected\ekvdef{myrule}{wd}{\def\myrule@wd{#1}}
974 \protected\ekvdef{myrule}{raise}{\def\myrule@raise{#1}}
975 \protected\ekvdef{myrule}{lower}{\def\myrule@raise{-#1}}
976 \end{enverb}
977 We also want a way to change the initial values without outputting a rule
978 (since there are unexpandable keys involved it's a good idea to define this
979 |\protected|)
980 \begin{enverb}[no-tcb,store]
981 \protected\ekvsetdef\myruleset{myrule}
982 \end{enverb}
983 and we need an actual frontend that does the job:
984 \begin{enverb}[no-tcb,store]
985 \newcommand*\myrule[1][]
986 {%
987 \begingroup
988 \myruleset{#1}%
989 \rule[\myrule@raise]{\myrule@wd}{\myrule@ht}%
990 \endgroup
991 }
992 \makeatother
993 \end{enverb}
994 Now we can use it:
995 \begin{enverb}[same-line=0.5,restore]
996 a\myrule\par
997 a\myrule[ht=2ex,lower=.5ex]\par
998 \myruleset{wd=5cm}
999 a\myrule
1000 \end{enverb}
1001 \end{example}
1002
1003
1004 \subsubsection{An Expandable \kv\ Macro Using
1005 \cs[no-index]{ekvsneak}\label{sec:expkv:sneak}}
1006
1007 \begin{example}{An expandable \kv\ macro using \cs[no-index]{ekvsneak}}
1008 Let's set up an expandable macro that uses a \kv\ interface. The problems
1009 we'll face for this are:
1010 \begin{enumerate}
1011 \item ignoring duplicate keys
1012 \item default values for keys which weren't used
1013 \item providing the values as the correct argument to a macro (ordered)
1014 \end{enumerate}
1015
1016 First we need to decide which \kv\ parsing macro we want to do this with,
1017 \cs{ekvset} or \cs{ekvparse}. For this example we also want to show the usage
1018 of \cs{ekvsneak}, hence we'll choose \cs{ekvset}. And we'll have to use
1019 \cs{ekvset} such that it builds a parsable list for our macro internals. To
1020 gain back control after \cs{ekvset} is done we have to put an internal of our
1021 macro at the start of that list, so we use an internal key that uses
1022 \cs{ekvsneakPre} after any user input.
1023
1024 To ignore duplicates will be easy if the value of the key used last will be
1025 put first in the list, so we'll use \cs{ekvsneakPre} for the real values as
1026 well. If for some reason we wanted a key for which the first usage was the
1027 binding one we'd use \cs{ekvsneak} for that one.
1028
1029 Providing default values can be done in different ways. We'll use a simple
1030 approach in which we'll just put the outcome of our keys if they were used
1031 with default values before our end marker.
1032
1033 Ordering the keys can be done simply by searching for a specific token for
1034 each argument (that token acts as a flag), so our sneaked out values will
1035 include these specific tokens acting as markers.
1036
1037 Now we got an answer to each of our initial problems. Everything that's left
1038 is deciding what our macro should actually do. For this example we'll define a
1039 macro that calculates the sine of a number rounded to a specified precision.
1040 The macro should also understand input in radian and degree, and we could also
1041 decide to evaluate a different function. For the real hard part of this
1042 (expandably calculating trigonometric functions) we'll use \pkg{xfp}.
1043
1044 First we set up our keys according to our earlier considerations and set up
1045 the user facing macro |\sine|. The end marker of the parsing list will be a
1046 |\sine@stop| token (which we don't need to define) and we put our default
1047 values right before it. The user macro |\sine| uses |\ekvoptargTF| to check
1048 for the optional argument short cutting a bit if no optional argument was
1049 found. If you'd so prefer you could use \pkg{ltcmd}'s
1050 |\NewExpandableDocumentCommand| to expandably get an optional argument as
1051 well.
1052 \begin{enverb}[no-tcb,store]
1053 \RequirePackage{xfp}
1054 \makeatletter
1055 \ekvdef{sine}{f}{\ekvsneakPre{\f{#1}}}
1056 \ekvdef{sine}{round}{\ekvsneakPre{\rnd{#1}}}
1057 \ekvdefNoVal{sine}{degree}{\ekvsneakPre{\deg{d}}}
1058 \ekvdefNoVal{sine}{radian}{\ekvsneakPre{\deg{}}}
1059 \ekvdefNoVal{sine}{internal}{\ekvsneakPre{\sine@rnd}}
1060 \newcommand*\sine{\ekvoptargTF\sine@args{\sine@final{sin}{d}{3}}}
1061 \newcommand*\sine@args[2]
1062 {\ekvset{sine}{#1,internal}\rnd{3}\deg{d}\f{sin}\sine@stop{#2}}
1063 \end{enverb}
1064
1065 Now we need to define some internal macros to extract the value of each key's
1066 last usage (remember that this will be the argument after the first matching
1067 flag). For that we use one delimited macro per key.
1068 \begin{enverb}[no-tcb,store]
1069 \def\sine@rnd#1\rnd#2#3\sine@stop{\sine@deg#1#3\sine@stop{#2}}
1070 \def\sine@deg#1\deg#2#3\sine@stop{\sine@f#1#3\sine@stop{#2}}
1071 \def\sine@f#1\f#2#3\sine@stop{\sine@final{#2}}
1072 \end{enverb}
1073 After the macros |\sine@rnd|, |\sine@deg|, and |\sine@f| the macro
1074 |\sine@final| will see
1075 |\sine@final|\marg{f}\marg{degree/radian}\marg{round}\marg{num}. Now
1076 |\sine@final| has to expandably deal with those arguments such that the
1077 |\fpeval| macro of \pkg{xfp} gets the correct input. Luckily this part is
1078 pretty easy after the build up we've done until now. In |\fpeval| the
1079 trigonometric functions have names such as |sin| or |cos|, and the degree
1080 taking alternatives just have an appended |d| (so |sind| or |cosd|). So
1081 putting \meta{f} and \meta{degree/radian} together will form the correct
1082 names.
1083 \begin{enverb}[no-tcb,store]
1084 \newcommand*\sine@final[4]{\fpeval{round(#1#2(#4),#3)}}
1085 \makeatother
1086 \end{enverb}
1087 Let's give our macro a test:
1088 \begin{enverb}[restore,inter=\def\RequirePackage#1{}]
1089 \sine{60}\par
1090 \sine[round=10]{60}\par
1091 \sine[f=cos,radian]{pi}\par
1092 \edef\myval{\sine[f=tan]{1}}\texttt{\meaning\myval}
1093 \end{enverb}
1094 \end{example}
1095
1096 Please note that setting this up a lot more user friendly is easily possible by
1097 utilizing \expkvc\ (see \autoref{sec:expkv-cs}).