-
Notifications
You must be signed in to change notification settings - Fork 0
/
expkv.dtx
3044 lines (3037 loc) · 115 KB
/
expkv.dtx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% \iffalse meta-comment
%
% File: expkv.dtx Copyright (C) 2020-2021 Jonathan P. Spratte
%
% This work may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this license or
% (at your option) any later version. The latest version of this license is in
% the file:
%
% http://www.latex-project.org/lppl.txt
%
% ------------------------------------------------------------------------------
%
%<*driver>^^A>>=
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
\expandafter\begingroup
\fi
\input l3docstrip.tex
\askforoverwritefalse
\preamble
--------------------------------------------------------------
expkv -- an expandable key=val implementation
E-mail: [email protected]
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
--------------------------------------------------------------
Copyright (C) 2020-2021 Jonathan P. Spratte
This work may be distributed and/or modified under the conditions of the
LaTeX Project Public License (LPPL), either version 1.3c of this license or
(at your option) any later version. The latest version of this license is in
the file:
http://www.latex-project.org/lppl.txt
This work is "maintained" (as per LPPL maintenance status) by
Jonathan P. Spratte.
This work consists of the file expkv.dtx
and the derived files expkv.pdf
expkv.sty
expkv.tex
t-expkv.tex
\endpreamble
% stop docstrip adding \endinput
\postamble
\endpostamble
\generate{\file{expkv.sty}{\from{expkv.dtx}{pkg}}}
\generate{\file{expkv.tex}{\from{expkv.dtx}{tex}}}
\generate{\file{t-expkv.tex}{\from{expkv.dtx}{ctx}}}
\ifx\fmtname\nameofplainTeX
\expandafter\endbatchfile
\else
\expandafter\endgroup
\fi
%
\IfFileExists{expkv.tex}{\input{expkv.tex}}{}
\ProvidesFile{expkv.dtx}
[\csname ekvDate\endcsname\ an expandable key=val implementation]
\PassOptionsToPackage{full}{textcomp}
\documentclass{l3doc}
\RequirePackage[oldstylenums,nott]{kpfonts}
\input{glyphtounicode}
\pdfgentounicode=1
\RequirePackage{xfp} ^^A required for an example
\RequirePackage{listings}
\RequirePackage{booktabs}
\RequirePackage{array}
\RequirePackage{collcell}
\RequirePackage{siunitx}
\DeclareSIUnit\ops{ops}
\RequirePackage{xcolor}
\RequirePackage{caption}
\RequirePackage{microtype}
\RequirePackage{accsupp}
\RequirePackage{enumitem}
\RequirePackage{tcolorbox}
\newtcolorbox{exresult}[2][]
{colback=expkvgrey!10!white,colframe=expkvgrey,fontupper=\small,width={#2},#1}
\newtcbox\exres[1][]
{
colback=expkvgrey!10!white
,colframe=expkvgrey
,size=small
,nobeforeafter
,tcbox raise base
,fontupper=\small
,#1
}
\def\mylstwd{.55em}
\lstdefinelanguage{expkv}[primitive]{TeX}
{
,moretexcs=[4]^^A e-TeX
{
expanded,
numexpr,
protected,
unexpanded,
}
,moretexcs=[5]^^A plain/LaTeX/ConTeXt
{
approx,
begin,
empty,
item,
LaTeX,
makeatletter,makeatother,
newcommand,newdimen,
RequirePackage,
rule,
TeX,
textit,texttt,
usepackage,
usemodule,
@gobble,@gobbletwo,
}
,moretexcs=[6]^^A used packages
{
^^A xfp
fpeval,
^^A keyval
setkeys,
^^A yax
defactiveparameter,storevalue,setparameterlist
}
,moretexcs=[2]^^A expkv macros
{
ekvdef,ekvdefNoVal,^^A
ekvlet,ekvletNoVal,ekvletkv,ekvletkvNoVal,^^A
ekvdefunknown,ekvdefunknownNoVal,^^A
ekvletunknown,ekvletunknownNoVal,^^A
ekvredirectunknown,ekvredirectunknownNoVal,^^A
ekvset,ekvsetSneaked,^^A
ekvsetdef,ekvsetSneakeddef,ekvsetdefSneaked,^^A
ekvparse,^^A
ekvVersion,ekvDate,^^A
ekvifdefined,ekvifdefinedNoVal,ekvifdefinedset,^^A
ekvbreak,ekvbreakPreSneak,ekvbreakPostSneak,^^A
ekvsneak,ekvsneakPre,^^A
ekvchangeset,^^A
ekvoptarg,ekvoptargTF,^^A
ekvcsvloop,^^A
ekverr,^^A
ekv@name,^^A
}
,moretexcs=[3]^^A expkv-pkg macros
{
^^A expkv-cs
ekvcSecondaryKeys,ekvcSplitAndForward,
^^A expkv-def
ekvdefinekeys
}
}
\colorlet{codeparam}{cyan!65!black}
\lstset
{
,language=expkv
,flexiblecolumns=false
,basewidth=\mylstwd
,gobble=2
,basicstyle=\fontfamily{jkp}\itshape
,texcsstyle=*[1]{\bfseries\color{expkvgrey}} ^^A primitives
,texcsstyle=*[2]{\bfseries\color{expkvred}} ^^A expkv
,texcsstyle=*[3]{\color{expkvred}} ^^A expkv-pkg
,texcsstyle=*[4]{\bfseries\color{expkvgrey}} ^^A e-TeX
,texcsstyle=*[5]{\bfseries\color{expkvgrey}} ^^A plain/LaTeX macros
,texcsstyle=*[6]{} ^^A macros of other packages
,commentstyle=\color[gray]{0.4}
,literate=
{\{} {{\CodeUpBf\{}}{1}
{\}} {{\CodeUpBf\}}}{1}
{$} {{\CodeUpBf\$}}{1}
{[} {{\CodeUp[}}{1}
{]} {{\CodeUp]}}{1}
{(} {{\CodeUp(}}{1}
{)} {{\CodeUp)}}{1}
{*} {{$*$}}{1}
{1} {{\CodeUp{1}}}{1}
{2} {{\CodeUp{2}}}{1}
{3} {{\CodeUp{3}}}{1}
{4} {{\CodeUp{4}}}{1}
{5} {{\CodeUp{5}}}{1}
{6} {{\CodeUp{6}}}{1}
{7} {{\CodeUp{7}}}{1}
{8} {{\CodeUp{8}}}{1}
{9} {{\CodeUp{9}}}{1}
{0} {{\CodeUp{0}}}{1}
{##} {{\CodeColored{codeparam}{1}{\#}}}{1}
{##1}{{\CodeColored{codeparam}{2}{\#1}}}{2}
{##2}{{\CodeColored{codeparam}{2}{\#2}}}{2}
{##3}{{\CodeColored{codeparam}{2}{\#3}}}{2}
{##4}{{\CodeColored{codeparam}{2}{\#4}}}{2}
{##5}{{\CodeColored{codeparam}{2}{\#5}}}{2}
{##6}{{\CodeColored{codeparam}{2}{\#6}}}{2}
{##7}{{\CodeColored{codeparam}{2}{\#7}}}{2}
{##8}{{\CodeColored{codeparam}{2}{\#8}}}{2}
{##9}{{\CodeColored{codeparam}{2}{\#9}}}{2}
{<key>}{{$\langle$}key{$\rangle$}}{5}
{<set>}{{$\langle$}set{$\rangle$}}{5}
{<set1>}{{$\langle$}set1{$\rangle$}}{6}
{<set2>}{{$\langle$}set2{$\rangle$}}{6}
^^A,literate=*{<key>}{\key}{4}{<set>}{\set}{4}
}
\newcommand*\CodeColored[3]{\textcolor{#1}{\makebox[\dimexpr\mylstwd*#2]{#3}}}
\newcommand*\CodeUpBf[1]{\makebox[\mylstwd]{\textup{\textbf{#1}}}}
\newcommand*\CodeUp[1]{\makebox[\mylstwd]{\textup{#1}}}
\RequirePackage{randtext}
\let\metaORIG\meta
\protected\def\meta #1{\texttt{\metaORIG{#1}}}
\renewcommand*\thefootnote{\fnsymbol{footnote}}
\definecolor{expkvred}{HTML}{9F393D}
\colorlet{expkvgrey}{black!75}
\makeatletter
\newcommand*\example{\par\smallskip\noindent\textit{Example:} \ignorespaces}
\newcommand*\expFormat
{^^A
{^^A
\rmfamily
\bfseries
{\color{expkvgrey}e\kern-.05em x\kern-.05em}^^A
\lower.493ex\hbox{{\color{expkvgrey}P}}^^A
}^^A
}
\newcommand*\kvstyle
{^^A
\normalfont\rmfamily\bfseries\color{expkvred}^^A
}
\newcommand*\expkv
{^^A
\texorpdfstring
{^^A
\mbox
{^^A
\BeginAccSupp{ActualText=expkv}^^A
\href{https://github.com/Skillmon/tex_expkv}
{^^A
\rmfamily
\bfseries
{\color{expkvgrey}e\kern-.05em x\kern-.05em}^^A
\lower.493ex
\hbox{{\color{expkvgrey}P}\kern-.1em{\color{expkvred}k}}^^A
\kern-.18em{\color{expkvred}v}^^A
}^^A
\EndAccSupp{}^^A
}^^A
}
{expkv}^^A
}
\newcommand*\expkvpkg[1]
{^^A
\texorpdfstring
{^^A
\mbox
{^^A
\BeginAccSupp{ActualText=expkv-#1}^^A
\href{https://github.com/Skillmon/tex_expkv-#1}
{^^A
\rmfamily
\bfseries
{\color{expkvgrey}e\kern-.05em x\kern-.05em}^^A
\lower.493ex
\hbox{{\color{expkvgrey}P}\kern-.1em{\color{expkvred}k}}^^A
\kern-.18em{\color{expkvred}v}^^A
{\color{expkvgrey}^^A
\kern.05em\rule[-.1ex]{.08em}{1.2ex}\kern.05em\textsc{#1}^^A
}^^A
}^^A
\EndAccSupp{}^^A
}^^A
}
{expkv-#1}^^A
}
\newcommand*\expkvd{\expkvpkg{def}}
\newcommand*\expkvc{\expkvpkg{cs}}
\newcommand*\expkvo{\expkvpkg{opt}}
\ExplSyntaxOn
\newcommand*\pkglogo[1]
{
\texorpdfstring
{
\mbox
{
\BeginAccSupp{ActualText=#1}
\rmfamily
\str_case:nn {#1}
{
{yax}
{
Y\kern-.1em
\raise.15em\hbox{\scshape a}
\kern-.1em \lower.15em\hbox{X}%
}
}
\EndAccSupp{}
}
}
{#1}
}
\newcommand*\yax{\pkglogo{yax}}
\ExplSyntaxOff
\newcommand\kv{\meta{key}=\meta{value}}
\newcommand\key{\meta{key}}
\newcommand\val{\meta{value}}
\newcommand\set{\meta{set}}
\hypersetup{linkcolor=red!80!black,urlcolor=purple!80!black}
\DoNotIndex{\directlua}
\DoNotIndex{\def,\edef,\,,\=,\begingroup,\catcode,\chardef,\csname,\endcsname}
\DoNotIndex{\endgroup,\endinput,\errmessage,\expandafter,\input,\let,\long}
\DoNotIndex{\protected,\ProvidesFile,\ProvidesPackage,\relax,\space}
\DoNotIndex{\^,\@,\unexpanded,\string,\expanded,\detokenize,\meaning}
\DoNotIndex{\lastnamedcs,\romannumeral,\z@}
\DoNotIndex{\noexpand,\normalexpanded,\normalunexpanded}
\DoNotIndex{\par,\protect}
\DoNotIndex{\unprotect}
\DoNotIndex{\writestatus}
\DoNotIndex{\ifcsname}
\DoNotIndex{\ifx}
\DoNotIndex{\iffalse}
\DoNotIndex{\ifnum}
\DoNotIndex{\else}
\DoNotIndex{\fi}
\@gobble\fi ^^A ignoring \ifx etc., but only one \fi
\@gobble\fi ^^A ignoring \ifx etc., but only one \fi
\@gobble\fi ^^A ignoring \ifx etc., but only one \fi
\@ifdefinable\gobbledocstriptag{\def\gobbledocstriptag#1>{}}
\newcommand*\pmso[1] ^^A poor man's strike out
{^^A
\leavevmode
\begingroup
\sbox0{#1}^^A
\rlap{\vrule height .6ex depth -.5ex width \wd0\relax}^^A
\usebox0\relax
\endgroup
}
\makeatother
\begin{document}
\title
{^^A
\texorpdfstring
{^^A
\huge\expkv
\\[\medskipamount]
\Large an {\expFormat}andable
\meta{{\kvstyle k}\kern-.05em ey}=^^A
\meta{{\kvstyle v}\kern-.05em alue}
implementation^^A
}{expkv - an expandable <key>=<value> implementation}^^A
}
\date{\ekvDate\space v\ekvVersion}
\author{Jonathan P. Spratte\thanks{\protect\randomize{[email protected]}}}
\DocInput{expkv.dtx}
\end{document}
%</driver>^^A=<<
% \fi
%
% \maketitle
% \renewcommand*\thefootnote{\arabic{footnote}}
%
% \begin{abstract}
% \noindent\parfillskip=0pt
% \expkv\ provides a small interface for \kv\ parsing. The parsing macro is
% \emph{fully expandable}, the \meta{code} of your keys might be not. \expkv\ is
% \emph{swift}, close to the fastest \kv\ implementation. However it is the
% fastest which copes with active commas and equal signs and doesn't strip
% braces accidentally.
% \end{abstract}
%
% \tableofcontents
%
% \begin{documentation}^^A>>=
%
% \section{Documentation}
%
% \expkv\ provides an expandable \kv\ parser. The \kv\ pairs should be given as
% a comma separated list and the separator between a \key\ and the associated
% \val\ should be an equal sign. Both, the commas and the equal signs, might be
% of category 12 (other) or 13 (active). To support this is necessary as for
% example \pkg{babel} turns characters active for some languages, for instance
% the equal sign is turned active for Turkish.
%
% \expkv\ is usable as generic code, as a \LaTeX\ package or as a \ConTeXt\
% module. To use it, just use one of:
% \begin{lstlisting}
% \input expkv % plainTeX
% \usepackage{expkv} % LaTeX
% \usemodule[expkv] % ConTeXt
% \end{lstlisting}
% Both the \LaTeX\ package and the \ConTeXt\ module don't do more than
% \file{expkv.tex}, except calling |\ProvidesPackage| and setting things up such
% that \file{expkv.tex} will use |\ProvidesFile|, or printing some status
% information. The \ConTeXt\ support is not thoroughly tested, though (since I
% don't use \ConTeXt\ myself I don't know if there are additional pitfalls I
% wasn't aware of).
%
% In the \expkv\ family are other packages contained which provide additional
% functionality. Those packages currently are:
% \begin{description}[format=\expkvpkg]
% \item[def]
% a key-defining frontend for \expkv\ using a \kv\ syntax
% \item[cs]
% define expandable \kv\ macros using \expkv
% \item[opt]
% parse package and class options with \expkv
% \end{description}
% Note that while the package names are stylised with a vertical rule, their
% names are all lower case with a hyphen (\emph{e.g.}, \file{expkv-def}).
%
% A list of concise comparisons to other \kv\ packages is contained in
% \autoref{sec:cmp}.
%
% \subsection{Setting up Keys}\label{sec:define}
% \expkv\ provides a rather simple approach to setting up keys, similar to
% \pkg{keyval}. However there is an auxiliary package named \expkvd\ which
% provides a more sophisticated interface, similar to well established packages
% like \pkg{pgfkeys} or \pkg{l3keys}.
%
% Keys in \expkv\ (as in almost all other \kv\ implementations) belong to a
% \emph{set} such that different sets can contain keys of the same name. Unlike
% many other implementations \expkv\ doesn't provide means to set a default
% value, instead we have keys that take values and keys that don't (the latter
% are called |NoVal| keys by \expkv), but both can have the same name (on the
% user level).
%
% The following macros are available to define new keys. Those macros containing
% ``|def|'' in their name can be prefixed by anything allowed to prefix |\def|
% (but \emph{don't} use |\outer|, keys defined with it won't ever be usable),
% prefixes allowed for |\let| can prefix those with ``|let|'' in their name,
% accordingly. Neither \set\ nor \key\ are allowed to be empty for new keys.
% \set\ will be used as is inside of |\csname ...\endcsname| and \key\ will get
% |\detokenize|d. Also \set\ should not contain an explicit |\par| token.
%
% \begin{function}{\ekvdef}
% \begin{syntax}
% \cs{ekvdef}\marg{set}\marg{key}\marg{code}
% \end{syntax}
% Defines a \key\ taking a value in a \set\ to expand to \meta{code}. In
% \meta{code} you can use |#1| to refer to the given value.
% \end{function}
% \example Define |text| in |foo| to store the value inside |\foo@text|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \protected\long\ekvdef{foo}{text}{\def\foo@text{#1}}
% \end{lstlisting}
%
% \begin{function}{\ekvdefNoVal}
% \begin{syntax}
% \cs{ekvdefNoVal}\marg{set}\marg{key}\marg{code}
% \end{syntax}
% Defines a no value taking \key\ in a \set\ to expand to \meta{code}.
% \end{function}
% \example Define |bool| in |foo| to set |\iffoo@bool| to |true|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \protected\ekvdefNoVal{foo}{bool}{\foo@booltrue}
% \end{lstlisting}
%
% \begin{function}{\ekvlet}
% \begin{syntax}
% \cs{ekvlet}\marg{set}\marg{key}\meta{cs}
% \end{syntax}
% Let the value taking \key\ in \set\ to \meta{cs}, there are no checks on
% \meta{cs} enforced, but the code should expect the value as a single braced
% argument directly following it.
% \end{function}
% \example Let |cmd| in |foo| do the same as |\foo@cmd|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvlet{foo}{cmd}\foo@cmd
% \end{lstlisting}
%
% \begin{function}{\ekvletNoVal}
% \begin{syntax}
% \cs{ekvletNoVal}\marg{set}\marg{key}\meta{cs}
% \end{syntax}
% Let the no value taking \key\ in \set\ to \meta{cs}, it is not checked
% whether \meta{cs} exists or that it takes no parameter.
% \end{function}
% \example See above.
%
% \begin{function}{\ekvletkv}
% \begin{syntax}
% \cs{ekvletkv}\marg{set}\marg{key}\marg{set2}\marg{key2}
% \end{syntax}
% Let the \key\ in \set\ to \meta{key2} in \meta{set2}, it is not checked
% whether that second key exists (but take a look at
% \cs[no-index]{ekvifdefined}).
% \end{function}
% \example Let |B| in |bar| be an alias for |A| in |foo|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvletkv{bar}{B}{foo}{A}
% \end{lstlisting}
%
% \begin{function}{\ekvletkvNoVal}
% \begin{syntax}
% \cs{ekvletkvNoVal}\marg{set}\marg{key}\marg{set2}\marg{key2}
% \end{syntax}
% Let the \key\ in \set\ to \meta{key2} in \meta{set2}, it is not checked
% whether that second key exists (but take a look at
% \cs[no-index]{ekvifdefinedNoVal}).
% \end{function}
% \example See above.
%
% \begin{function}{\ekvdefunknown}
% \begin{syntax}
% \cs{ekvdefunknown}\marg{set}\marg{code}
% \end{syntax}
% By default an error will be thrown if an unknown \key\ is encountered. With
% this macro you can define \meta{code} that will be executed for a given
% \set\ when an unknown \key\ with a \val\ was encountered instead of throwing
% an error. You can refer to the given \val\ with |#1| and to the unknown
% \key's name with |#2| in \meta{code}.\footnotemark{}
% |\ekvdefunknown| and |\ekvredirectunknown| are mutually exclusive,
% you can't use both.
% \end{function}
% \footnotetext{That order is correct, this way the code is faster.}
% \example Also search |bar| for undefined keys of set |foo|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \long\ekvdefunknown{foo}{\ekvset{bar}{#2={#1}}}
% \end{lstlisting}
% This example differs from using |\ekvredirectunknown{foo}{bar}| (see below) in
% that also the unknown-key handler of the |bar| set will be triggered, error
% messages for undefined keys will look different, and this is slower than using
% |\ekvredirectunknown|.
%
% \begin{function}{\ekvdefunknownNoVal}
% \begin{syntax}
% \cs{ekvdefunknownNoVal}\marg{set}\marg{code}
% \end{syntax}
% As already explained for \cs[no-index]{ekvdefunknown}, \expkv\ would throw
% an error when encountering an unknown \key. With this you can instead let it
% execute \meta{code} if an unknown |NoVal| \key\ was encountered. You can
% refer to the given \key\ with |#1| in \meta{code}.
% |\ekvdefunknownNoVal| and |\ekvredirectunknownNoVal| are mutually exclusive,
% you can't use both.
% \end{function}
% \example Also search |bar| for undefined keys of set |foo|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvdefunknownNoVal{foo}{\ekvset{bar}{#1}}
% \end{lstlisting}
%
% \begin{function}{\ekvletunknown}
% \begin{syntax}
% \cs{ekvletunknown}\marg{set}\meta{cs}
% \end{syntax}
% This has the same effect as \cs[no-index]{ekvdefunknown} (so suppresses
% errors) but instead of defining a new macro the unknown handler is let to
% \meta{cs} which should take two arguments (the first being the \val\ the
% second the \key).
% \end{function}
% \example Silently ignore unknown keys which were given a value in the set
% |foo|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvletunknown{foo}\@gobbletwo
% \end{lstlisting}
%
% \begin{function}{\ekvletunknownNoVal}
% \begin{syntax}
% \cs{ekvletunknownNoVal}\marg{set}\meta{cs}
% \end{syntax}
% This will let the unknown handler used when only a \key\ without a \val\ was
% used to \meta{cs}. \meta{cs} should be a macro taking one argument which
% will be the \key\ name.
% \end{function}
% \example Treat every unknown \key\ in the set |foo| without a value as if
% |bar=|\key\ was used.
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \expanded
% {%
% \unexpanded{\ekvletunknownNoVal{foo}}%
% \unexpanded\expandafter{\csname\ekv@name{foo}{bar}\endcsname}%
% }
% \end{lstlisting}
% If you want to redefine the |bar| key at some point the above won't work
% (it'll keep the definition |bar| had when this was used), so you could as well
% use the slightly slower:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvdefunknownNoVal{foo}{\csname\ekv@name{foo}{bar}\endcsname{#1}}
% \end{lstlisting}
% In both cases the \key\ would be |\detokenize|d at this point, so macros
% wouldn't work idiomatically.
%
% \begin{function}{\ekvredirectunknown}
% \begin{syntax}
% \cs{ekvredirectunknown}\marg{set}\marg{set-list}
% \end{syntax}
% This is a short cut to set up a special |\ekvdefunknown| for \set\ that will
% check each set in the comma separated \meta{set-list} for the unknown \key.
% You can't use prefixes (so no |\long| or |\protected|) with this macro, the
% resulting unknown-key handler will always be |\long|. The first set in the
% \meta{set-list} has highest priority. Once the \key\ is found the remaining
% sets are discarded, if the \key\ isn't found in any set an error will be
% thrown eventually. Note that the error messages are affected by the use of
% this macro, in particular, it isn't checked whether a |NoVal| key of the
% same name is defined in order to throw an |unwanted value| error.
% |\ekvdefunknown| and |\ekvredirectunknown| are mutually exclusive,
% you can't use both.
% \end{function}
% \example For every key not defined in the set |foo| also search the sets |bar|
% and |baz|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvredirectunknown{foo}{bar, baz}
% \end{lstlisting}
%
% \begin{function}{\ekvredirectunknownNoVal}
% \begin{syntax}
% \cs{ekvredirectunknownNoVal}\marg{set}\marg{set-list}
% \end{syntax}
% This behaves just like |\ekvredirectunknown| and does the same but for the
% |NoVal| keys. Again no prefixes are supported. Note that the error messages
% are affected by the use of this macro, in particular, it isn't checked
% whether a normal key of the same name is defined in order to throw a
% |missing value| error.
% |\ekvdefunknownNoVal| and |\ekvredirectunknownNoVal| are mutually exclusive,
% you can't use both.
% \end{function}
% \example See above.
%
% \subsection{Parsing Keys}
%
% \begin{function}{\ekvset}
% \begin{syntax}
% \cs{ekvset}\marg{set}\{\kv,\ldots\}
% \end{syntax}
% Splits \kv\ pairs on commas. From both \key\ and \val\ up to one space is
% stripped from both ends, if then only a braced group remains the braces are
% stripped as well. So |\ekvset{foo}{bar=baz}| and
% |\ekvset{foo}{ {bar}= {baz} }| will both do
% \texttt{\cs[no-index]{\meta{foobarcode}}\{baz\}}, so you can hide commas,
% equal signs and spaces at the ends of either \key\ or \val\ by putting
% braces around them. If you omit the equal sign the code of the key created
% with the |NoVal| variants described in \autoref{sec:define} will be
% executed. If \kv\ contains more than a single unhidden equal sign, it will
% be split at the first one and the others are considered part of the value.
% |\ekvset| should be nestable.
%
% |\ekvset| is currently \emph{not} alignment safe.\footnotemark\ As a result,
% key names and values that contain an |&| must be wrapped in braces when
% |\ekvset| is used inside an alignment (like \LaTeXe's |tabular| environment)
% or you have to create a wrapper that ensures an alignment safe context.
% \end{function}
% \footnotetext{This might change in the future, I've not decided yet.}
% \example Parse |key=arg, key| in the set |foo|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvset{foo}{key=arg, key}
% \end{lstlisting}
%
% \begin{function}{\ekvsetSneaked}
% \begin{syntax}
% \cs{ekvsetSneaked}\marg{set}\marg{sneak}\{\kv,\ldots\}
% \end{syntax}
% Just like \cs[no-index]{ekvset}, this macro parses the \kv\ pairs within the
% given \set. But \cs[no-index]{ekvsetSneaked} will behave as if
% \cs[no-index]{ekvsneak} has been called with \meta{sneak} as its argument as
% the first action.
% \end{function}
% \example Parse |key=arg, key| in the set |foo| with |\afterwards| sneaked out:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvsetSneaked{foo}{\afterwards}{key=arg, key}
% \end{lstlisting}
%
% \begin{function}{\ekvsetdef}
% \begin{syntax}
% \cs{ekvsetdef}\meta{cs}\marg{set}
% \end{syntax}
% With this function you can define a shorthand macro \meta{cs} to parse keys
% of a specified \set. It is always defined |\long|, but if you need to you
% can also prefix it with |\global|. The resulting macro is faster than but
% else equivalent to the idiomatic definition:\par
% \texttt
% {\string\long\string\def\meta{cs}\#1\{\string\ekvset\marg{set}\{\#1\}\}}
% \end{function}
% \example Define the macro |\foosetup| to parse keys in the set |foo| and use
% it to parse |key=arg, key|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvsetdef\foosetup{foo}
% \foosetup{key=arg, key}
% \end{lstlisting}
%
% \begin{function}{\ekvsetSneakeddef}
% \begin{syntax}
% \cs{ekvsetSneakeddef}\meta{cs}\marg{set}
% \end{syntax}
% Just like \cs[no-index]{ekvsetdef} this defines a shorthand macro \meta{cs},
% but this macro will make it a shorthand for \cs[no-index]{ekvsetSneaked},
% meaning that \meta{cs} will take two arguments, the first being stuff that
% should be given to \cs[no-index]{ekvsneak} and the second the \kv\ list. The
% resulting macro is faster than but else equivalent to the idiomatic
% definition:\par
% \texttt
% {%
% \string\long\string\def\meta{cs}\#1\#2\{^^A
% \string\ekvsetSneaked\marg{set}\{\#1\}\{\#2\}\}^^A
% }
% \end{function}
% \example Define the macro |\foothings| to parse keys in the set |foo| and
% accept a sneaked argument, then use it to parse |key=arg, key| and sneak
% |\afterwards|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvsetSneakeddef\foothings{foo}
% \foothings{\afterwards}{key=arg, key}
% \end{lstlisting}
%
% \begin{function}{\ekvsetdefSneaked}
% \begin{syntax}
% \cs{ekvsetdefSneaked}\meta{cs}\marg{set}\marg{sneaked}
% \end{syntax}
% And this one behaves like \cs[no-index]{ekvsetSneakeddef} but with a fixed
% \meta{sneaked} argument. So the resulting macro is faster than but else
% equivalent to the idiomatic definition:\par
% \texttt
% {%
% \string\long\string\def\meta{cs}\#1\{^^A
% \string\ekvsetSneaked\marg{set}\marg{sneaked}\{\#1\}\}^^A
% }
% \end{function}
% \example Define the macro |\barthing| to parse keys in the set |bar| and
% always execute |\afterwards| afterwards, then use it to parse |key=arg, key|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvsetdefSneaked\barthing{bar}{\afterwards}
% \barthing{key=arg, key}
% \end{lstlisting}
%
% \begin{function}{\ekvparse}
% \begin{syntax}
% \cs{ekvparse}\marg{code1}\marg{code2}\{\kv,\ldots\}
% \end{syntax}
% This macro parses the \kv\ pairs and provides those list elements which are
% only keys as an argument to \meta{code1}, and those which are a \kv\ pair
% to \meta{code2} as two arguments. It is fully expandable as well and returns
% each element of the parsed list in |\unexpanded|, which has no effect
% outside of an |\expanded| or |\edef| context.
% Also |\ekvparse| expands in exactly two steps of expansion.
% You can use multiple tokens in \meta{code1} and \meta{code2} or just a
% single control sequence name. In both cases the found \key\ and \val\ are
% provided as a brace group following them.
%
% |\ekvparse| is alignment safe, meaning that you don't have to take any
% precautions if it is used inside an alignment context (like \LaTeXe's
% |tabular| environment) and any key or value can contain an |&|.
%
% |\ekvbreak|, |\ekvsneak|, and |\ekvchangeset| and their relatives don't work
% in |\ekvparse|. It is analogue to \pkg{expl3}'s |\keyval_parse:NNn|, but
% not with the same parsing rules -- |\keyval_parse:NNn| throws an error on
% multiple equal signs per \kv\ pair and on empty \key\ names in a \kv\ pair,
% both of which |\ekvparse| doesn't deal with.
% \end{function}
% \example
% \begin{lstlisting}
% \ekvparse{\handlekey{S}}{\handlekeyval{S}}{foo = bar, key, baz={zzz}}
% \end{lstlisting}
% would be equivalent to
% \begin{lstlisting}
% \handlekeyval{S}{foo}{bar}\handlekey{S}{key}\handlekeyval{S}{baz}{zzz}
% \end{lstlisting}
% and afterwards |\handlekey| and |\handlekeyval| would have to further handle
% the \key. There are no macros like these two contained in \expkv, you have
% to set them up yourself if you want to use |\ekvparse| (of course the names
% might differ). If you need the results of |\ekvparse| as the argument for
% another macro, you should use |\expanded|, or expand |\ekvparse| twice, as
% only then the input stream will contain the output above:
% \begin{lstlisting}
% \expandafter\parse\expanded{\ekvparse\k\kv{foo = bar, key, baz={zzz}}}
% \end{lstlisting}
% or
% \begin{lstlisting}
% \expandafter\expandafter\expandafter
% \parse\ekvparse\k\kv{foo = bar, key, baz={zzz}}
% \end{lstlisting}
% would both expand to
% \begin{lstlisting}
% \parse\kv{foo}{bar}\k{key}\kv{baz}{zzz}
% \end{lstlisting}
%
% \subsection{Other Macros}
%
% \expkv\ provides some other macros which might be of interest.
%
% \begin{function}{\ekvVersion,\ekvDate}
% These two macros store the version and date of the package.
% \end{function}
%
% \begin{function}{\ekvifdefined,\ekvifdefinedNoVal}
% \begin{syntax}
% \cs{ekvifdefined}\marg{set}\marg{key}\marg{true}\marg{false}
% \cs{ekvifdefinedNoVal}\marg{set}\marg{key}\marg{true}\marg{false}
% \end{syntax}
% These two macros test whether there is a \key\ in \set. It is false if
% either a hash table entry doesn't exist for that key or its meaning is
% |\relax|.
% \end{function}
% \example Check whether the key |special| is already defined in set |foo|, if
% it isn't input a file that contains more key definitions:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvifdefined{foo}{special}{}{\input{foo.morekeys.tex}}
% \end{lstlisting}
%
% \begin{function}{\ekvifdefinedset}
% \begin{syntax}
% \cs{ekvifdefinedset}\marg{set}\marg{true}\marg{false}
% \end{syntax}
% This macro tests whether \set\ is defined (which it is if at least one key
% was defined for it). If it is \meta{true} will be run, else \meta{false}.
% \end{function}
% \example Check whether the set |VeRyUnLiKeLy| is already defined, if so throw
% an error, else do nothing:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvifdefinedset{VeRyUnLiKeLy}
% {\errmessage{VeRyUnLiKeLy already defined}}{}
% \end{lstlisting}
%
% \begin{function}{\ekvbreak,\ekvbreakPreSneak,\ekvbreakPostSneak}
% \begin{syntax}
% \cs{ekvbreak}\marg{after}
% \end{syntax}
% Gobbles the remainder of the current |\ekvset| macro and its argument list
% and reinserts \meta{after}. So this can be used to break out of |\ekvset|.
% The first variant will also gobble anything that has been sneaked out using
% |\ekvsneak| or |\ekvsneakPre|, while |\ekvbreakPreSneak| will put
% \meta{after} before anything that has been smuggled and |\ekvbreakPostSneak|
% will put \meta{after} after the stuff that has been sneaked out.
% \end{function}
% \example Define a key |abort| that will stop key parsing inside the set |foo|
% and execute |\foo@aborted|, or if it got a value |\foo@aborted@with|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvdefNoVal{foo}{abort}{\ekvbreak{\foo@aborted}}
% \ekvdef{foo}{abort}{\ekvbreak{\foo@aborted@with{#1}}}
% \end{lstlisting}
%
% \begin{function}{\ekvsneak,\ekvsneakPre}
% \begin{syntax}
% \cs{ekvsneak}\marg{after}
% \end{syntax}
% Puts \meta{after} after the effects of |\ekvset|. The first variant will put
% \meta{after} after any other tokens which might have been sneaked before,
% while |\ekvsneakPre| will put \meta{after} before other smuggled stuff.
% This reads and reinserts the remainder of the current |\ekvset| macro and
% its argument list to do its job. After |\ekvset| has parsed the entire \kv\
% list everything that has been |\ekvsneak|ed will be left in the input
% stream. A small usage example is shown in \autoref{sec:sneakex}.
% \end{function}
% \example Define a key |secret| in the set |foo| that will sneak out
% |\foo@secretly@sneaked|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvdefNoVal{foo}{secret}{\ekvsneak{\foo@secretly@sneaked}}
% \end{lstlisting}
%
% \begin{function}{\ekvchangeset}
% \begin{syntax}
% \cs{ekvchangeset}\marg{new-set}
% \end{syntax}
% Replaces the current set with \meta{new-set}, so for the rest of the current
% |\ekvset| call, that call behaves as if it was called with
% \texttt{\cs[no-index]{ekvset}\marg{new-set}}. It is comparable to using
% \texttt{\meta{key}/.cd} in \pkg{pgfkeys}.
% \end{function}
% \example Define a key |cd| in set |foo| that will change to another set as
% specified in the value, if the set is undefined it'll stop the parsing and
% throw an error as defined in the macro |\foo@cd@error|:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \ekvdef{foo}{cd}
% {\ekvifdefinedset{#1}{\ekvchangeset{#1}}{\ekvbreak{\foo@cd@error}}}
% \end{lstlisting}
%
% \begin{function}{\ekvoptarg}
% \begin{syntax}
% \cs{ekvoptarg}\marg{next}\marg{default}
% \end{syntax}
% This macro will check for a following optional argument in brackets (|[]|)
% expandably. After the optional argument there has to be a mandatory one. The
% code in \meta{next} should expect two arguments (the processed optional
% argument and the mandatory one). If there was an optional argument the
% result will be \meta{next}\marg{optional}\meta{mandatory} (so the optional
% argument will be wrapped in braces, the mandatory argument will be
% untouched). If there was no optional argument the result will be
% \meta{next}\marg{default}\marg{mandatory} (so the default will be used and
% the mandatory argument will be wrapped in braces after being read once -- if
% it was already wrapped it is effectively unchanged).
% \end{function}
% |\ekvoptarg| expands in exactly two steps, grabs all the arguments only at the
% second expansion step, and is alignment safe. It has its limitations however.
% It can't tell the difference between |[| and |{[}|, so it doesn't work if the
% mandatory argument is a single bracket. Also if the optional argument should
% contain a nested closing bracket, the optional argument has to use nested
% braces like so: |[{arg]ument}]|.
% \example Say we have a macro that should take an optional argument defaulting
% to 1:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \newcommand\foo{\ekvoptarg\@foo{1}}
% \newcommand\@foo[2]{Mandatory: #2\par Optional: #1}
% \end{lstlisting}
%
% \begin{function}{\ekvoptargTF}
% \begin{syntax}
% \cs{ekvoptargTF}\marg{true}\marg{false}
% \end{syntax}
% This macro is similar to |\ekvoptarg|, but will result in
% \meta{true}\marg{optional}\meta{mandatory} or \meta{false}\marg{mandatory}
% instead of placing a default value.
% \end{function}
% |\ekvoptargTF| expands in exactly two steps, grabs all the arguments only at
% the second expansion step, and is alignment safe. It has the same limitations
% as |\ekvoptarg|.
% \example Say we have a macro that should behave differently depending on
% whether there was an optional argument or not. This could be done with:
% \begin{lstlisting}[aboveskip=0pt,belowskip=0pt]
% \newcommand\foo{\ekvoptargTF\foo@a\foo@b}
% \newcommand\foo@a[2]{Mandatory: #2\par Optional: #1}
% \newcommand\foo@b[1]{Mandatory: #1\par No optional.}
% \end{lstlisting}
%
% \begin{function}{\ekvcsvloop}
% \begin{syntax}
% \cs{ekvcsvloop}\marg{code}\marg{csv-list}
% \end{syntax}
% This loops over the comma separated items in \meta{csv-list} and, after
% stripping spaces from either end of \meta{item} and removing at most one set
% of outer braces, leaves
% \texttt
% {^^A
% \cs[no-index]{unexpanded}\hskip0pt
% \{\hskip0pt
% \meta{code}\hskip0pt
% \marg{item}\hskip0pt
% \}^^A
% }
% for each list item in the input stream. Blank elements are ignored (if you
% need a blank element it should be given as |{}|). It supports both active
% commas and commas of category other. You could consider it as a watered down
% version of |\ekvparse|. However it is not alignment safe, which you could
% achieve by nesting it in |\expanded| (since the braces around the argument
% of |\expanded| will hide |&|s from \TeX's alignment parsing).
% \end{function}
% \example The following splits a comma separated list and prints it in a
% typewriter font with parentheses around each element.\\
% \begin{minipage}{.75\linewidth}
% \begin{lstlisting}
% \newcommand*\myprocessor[1]{\texttt{(#1)}}
% \ekvcsvloop\myprocessor{abc,def,ghi}\par
% \ekvcsvloop\myprocessor{1,,2,,3,,4}\par
% \end{lstlisting}
% \end{minipage}^^A
% \begin{exresult}[nobeforeafter,box align=center]{.25\linewidth}
% \newcommand*\myprocessor[1]{\texttt{(#1)}}
% \ekvcsvloop\myprocessor{abc,def,ghi}\par
% \ekvcsvloop\myprocessor{1,,2,,3,,4}\par
% \end{exresult}
%
% \begin{function}{\ekverr}
% \begin{syntax}
% \cs{ekverr}\marg{package}\marg{message}
% \end{syntax}
% This macro will throw an error fully expandably.\footnotemark\ The error
% length is limited to a total length of 69~characters, and since ten
% characters will be added for the formatting (\verb*|! | and
% \verb*| Error: |) that leaves us with a total length for \meta{package} plus
% \meta{message} of 59~characters. If the message gets longer \TeX\ will only
% display the first 69~characters and append |\ETC.| to the end.
%
% Neither \meta{package} nor \meta{message} expand any further. Also
% \meta{package} must not contain an explicit |\par| token or the token
% |\thanks@jfbu|. No such restriction applies to \meta{message}.
%
% If |^^J| is set up as the |\newlinechar| (which is the case in \LaTeXe\ but
% not in plain \TeX\ by default) you can use that to introduce line breaks in
% your error message. However that doesn't change the message length limit.
% \end{function}
% \footnotetext{The used mechanism was to the best of my knowledge first
% implemented by Jean-François Burnol.}
% After your own error message some further text will be placed. The
% formatting of that text will look good if |^^J| is the |\newlinechar|, else
% not so much. That text will read:
% \begin{verbatim}
% ! Paragraph ended before \<an-expandable-macro>
% completed due to above exception. If the error
% summary is not comprehensible see the package
% documentation.
% I will try to recover now. If you're in inter-
% active mode hit <return> at the ? prompt and I
% continue hoping recovery was complete.
% \end{verbatim}
% Any clean up has to be done by you, |\ekverr| will expand to nothing after
% throwing the error message.
%
% In \ConTeXt\ this macro works differently. While still being fully expandable,
% it doesn't have the character count limitation and doesn't impose restrictions
% on \meta{package}. It will not display the additional text and adding line
% breaks is not possible.
% \example Say we set up a small calculation which works with user input. In our
% calculation we need a division, so have to watch out for division by zero. If
% we detect such a case we throw an error and do the recovery by using the
% biggest integer allowed in \TeX\ as the result.
% \begin{lstlisting}
% \newcommand*\mydivision[2]
% {%
% \the\numexpr
% \ifnum\numexpr#2=0 % space here on purpose
% \ekverr{my}{division by 0. Setting result to 2147483647.}%
% 2147483647%
% \else