l3keys in LaTeX3

.default:n / .initial:n

请看如下一段代码:

1
2
3
4
5
6
7
\keys_define:nn {mymodule}{
keya .code:n = {Enter~ keya},
keya .initial:n = {value-a},
keyb .code:N = {Enter~ keyb},
keyb .default:n = {value-b},
}
\keys_set:nn {mymodule}{keyb}

在上述的例子中一个是.initial:n, 一个是 .default:n, 都表示设置其默认值. 在命令 \keys_set:nn 中:

  • 如果没有出现 keyb, 对应的 .default 语句便不会被执行. 如果出现了 keyb, 但没有赋值,此时其对应的(默认)语句便会执行.
  • 无论其中是否出现 keya, 对应的 .initial 语句均会执行.

choice key

.choice:

when set key of this type:

  • it accepts only one choice(belong to a set of valid choices) in a times.
1
2
3
4
5
6
7
8
9
10
11
12
13
\keys_define:nn { moduleA }
{
key .choice:,
key / subA .code:n = {subA's~code\par},
key / subB .code:n = {subB's~code\par},
}
\keys_set:nn { moduleA }
{
key = subA,
key = subB,
% key = {subA, subB}, % --> ERROR
% key = subC % --> ERROR
}

output:

1
2
subA's code
subB's code

.choices:nn

when set key of this type:

  • (B). all the choices share the same code.
1
2
3
4
5
6
7
8
9
10
11
12
13
\keys_define:nn { moduleB }
{
key .choices:nn =
{ subA, subB }
{ KeyA~and~KeyB~share~the~same~code\par }
}
\keys_set:nn { moduleB }
{
key = subA,
key = subB,
% key = {subA, subB}, % --> ERROR
% key = subC % --> ERROR
}

output:

1
2
KeyA and KeyB share the same code
KeyA and KeyB share the same code

.multichoice:

when set key of this type:

  • (A). it accepts a set of choices in a time(multiple choice).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
\keys_define:nn { moduleC }
{
key .multichoice:,
key/subA .code:n = { subA's~code\par },
key/subB .code:n = { subB's~code\par },
key/subC .code:n = { subC's~code\par },
}
\keys_set:nn { moduleC }
{
key = subA,
key = subB,
key = {subA, subB},
% key = subC --> ERROR
}

output:

1
2
3
4
subA's code
subB's code
subA's code
subB's code

.multichoices:nn

when set key of this type:

  • (A). it accepts a set of choices in a time(multiple choice), and,
  • (B). all the choices share the same code.
1
2
3
4
5
6
7
8
9
10
11
12
13
\keys_define:nn { moduleD }
{
key .multichoices:nn =
{ subA, subB }
{ KeyA~and~KeyB~share~the~same~code\par }
}
\keys_set:nn { moduleD }
{
key = subA,
key = subB,
key = {subA, subB},
% key = subC --> ERROR
}

output:

1
2
3
4
KeyA and KeyB share the same code
KeyA and KeyB share the same code
KeyA and KeyB share the same code
KeyA and KeyB share the same code

meta-元键

介绍

其实就是你传入的参数,比如{keya=valuea, keyb=valueb} 解释为键值对,然后再提取 value 给对应的 key 进行赋值. 详细的说明请参见下图:

meta-key

案例

一个简单的使用案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
\keys_define:nn { metakey }
{
ikeyA .code:n = {(inner~ikeyA)~being~set~to:#1\par},
ikeyB .code:n = {(inner~ikeyB)~being~set~to:#1\par},
ikeyC / subC .code:n = {(inner~ikeyC/subC)~being~set~to:#1\par},
% meta key inner
MkeyA .meta:n = { ikeyA = #1, ikeyB = #1, ikeyC/subC = #1 },
% meta key outer
MkeyB .meta:nn = { metakey / outside }{#1},
}
% meta key define outside
\keys_define:nn { metakey / outside }
{
okeyA .code:n = {(outside~okeyA)~being~set~to:#1\par},
okeyB .code:n = {(outside~okeyB)~being~set~to:#1\par},
}
\keys_set:nn { metakey }
{
MkeyA = { inner~ikeyA/B/C-val },
MkeyB = { okeyA=okeyA-val, okeyB=okeyB-val },
% MkeyA = { ikeyA=ikeyA-val, keyB=ikeyB-val }, % --> ERROR
% MkeyB = { outer~okeyA/B-val }, % --> ERROR
}

结果输出:

1
2
3
4
5
(inner ikeyA) being set to:inner ikeyA/B/C-val
(inner ikeyB) being set to:inner ikeyA/B/C-val
(inner ikeyC/subC) being set to:inner ikeyA/B/C-val
(outside okeyA) being set to:okeyA-val
(outside okeyB) being set to:okeyB-val

key’s group

An simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
\keys_define:nn { groupkey }
{
keyA .code:n = { keyA's~code\par },
keyA .groups:n = { GroupA },
keyB .code:n = { keyB's~code\par },
keyB .groups:n = { GroupB },
keyC / subC .code:n = { subC's~code\par },
keyC / subC .groups:n = { GroupA, GroupB },
}
% 1. set keys belong to groups
\keys_set_groups:nnn { groupkey }{ GroupA }{ keyA, keyB, keyC/subC }
% 2. set keys does NOT belong to groups
\dotfill\par
\keys_set_filter:nnn { groupkey }{ GroupA }{ keyA, keyB, keyC/subC }
% 3. only set the known keys
\dotfill\par
\keys_set_known:nn { groupkey }{ keyA, keyAA }

The output:

1
2
3
4
5
6
keyA's code
subC's code
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
keyB's code
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
keyA's code

inhert

example I - intro

An simple example:

1
2
3
4
5
6
7
8
9
10
11
12
\keys_define:nn { ParentModuleA }
{
pkeyA .code:n = { ParentA-keyA's~code\par },
pkeyB .code:n = { ParentA-keyB's~code\par },
pkeyC .choice:,
pkeyC / psubC .code:n = { ParentA-keyC's~choice~--~subC's~code\par },
}
\keys_define:nn {}{ childkeyA .inherit:n = { ParentModuleA } }
\keys_set:nn { childkeyA }{ pkeyA, pkeyB, pkeyC=psubC }
\dotfill\par
\keys_define:nn { ParentModuleB }{ childkeyA .inherit:n = { ParentModuleA } }
\keys_set:nn { ParentModuleB/childkeyA }{ pkeyA, pkeyB }

The output:

1
2
3
4
5
6
ParentA-keyA's code
ParentA-keyB's code
ParentA-keyC's choice – subC's code
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
ParentA-keyA's code
ParentA-keyB's code

example II - add key

Other example, see: Inheritance of keys in expl3:

1
2
3
4
5
6
7
8
9
10
11
\keys_define:nn { ParentA } {
keyA .code:n = { ParentA-keyA's~code\par },
}
\keys_define:nn { } {
ParentB .inherit:n = ParentA,
}
\keys_define:nn { ParentB } {
keyB .code:n = { ParentB-keyB's~code\par },
}

\keys_set:nn { ParentB } { keyA = keyA-val, keyB = keyB-val }

The output:

1
2
ParentA-keyA's code
ParentB-keyB's code

example III complicate inherit

The code as follows:

1
2
3
4
5
6
7
8
9
10
11
\keys_define:nn { XXX/ParentA } {
keyA .code:n = { ParentA-keyA's~code\par },
}
\keys_define:nn { XXX } {
ParentB .inherit:n = XXX/ParentA,
}
\keys_define:nn { XXX/ParentB } {
keyB .code:n = { ParentB-keyB's~code\par },
}

\keys_set:nn { XXX/ParentB } { keyA = keyA-val, keyB = keyB-val }

The output:

1
2
ParentA-keyA's code
ParentB-keyB's code

example IV - delete key

The code as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\keys_define:nn { ParentModuleA }
{
pkeyA .code:n = { ParentA-keyA's~code\par },
unknown .code:n = { ParentA-unkown's~code\par },
}
\keys_define:nn {}{ childkeyA .inherit:n = { ParentModuleA } }
\keys_define:nn { childkeyA }
{
unknown .code:n = { childkeyA-unkown's~code\par }
}
% delete keys
\dotfill\par
\keys_show:nn { childkeyA }{ pkeyA }
\keys_define:nn { ParentModuleA }{ pkeyA .undefine: }
\keys_set:nn { ParentModuleA }{ pkeyA }
\keys_set:nn { childkeyA }{ pkeyA }

the shell log is:

1
2
3
4
5
6
7
The key childkeyA/pkeyA is undefined.
> .
<recently read> }

l.157 \keys_show:nn { childkeyA }{ pkeyA }

?

And the final output is:

1
2
ParentA-unkown's code
childkeyA-unkown's code

The above results shows that pkeyA is not in childkeyA, then if you use:

1
2
3
\keys_define:nn { childkeyA }{ pkeyA .undefine: }
\keys_set:nn { ParentModuleA }{ pkeyA }
\keys_set:nn { childkeyA }{ pkeyA }

the output will be:

1
2
ParentA-keyA's code
ParentA-keyA's code

temp variables

For each key processed, information of the full path of the key, the name of the key and the value of the key is available within two string and one token list variables.

  • \l_keys_path_str: “full” description of the key, for example: mymodule/subset/key-a.
  • \l_keys_key_str: part of the path after the last /, by the above example, the value is key-a.
  • \l_keys_value_tl: everything after the =.

Inside the code block for a choice generated using .choices:nn, the variables:

  • \l_keys_choice_tl: the name of the current choice,
  • \l_keys_choice_int: its position in the comma list.

others

usage

usage, see example:

1
2
3
4
5
6
7
\keys_define:nn { module }
{
keyA .code:n = keyA's~code\par,
keyA .usage:n = preamble, % only available in preamble
keyB .code:n = keyA's~code\par,
keyB .usage:n = load, % only available when loading
}

the Scope of use stored in variable \l_keys_usage_<load|preamble>_prop:

1
2
3
4
5
\prop_map_inline:Nn \l_keys_usage_load_prop
{ #1=#2.~ } \par
\prop_map_inline:Nn \l_keys_usage_preamble_prop
{ #1=#2.~ } \par
\keys_set:nn { my } { A, B }

other command

some low level interface to handle key-value data:

  • \keyval_parse:nnn,
  • \keys_precompile:nnN,
  • \keys_if_exist:nnTF,
  • \keys_if_choice_exist:nnnTF,

source code

The source code of this post:

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
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{comment}
\parindent0pt


\begin{document}
\ExplSyntaxOn
\section{choice key}
% ==> '.choice:'
\dotfill\par
% when set key of this type:
% 0. it accepts only one choice(among a set of valid choices) in a times.
\keys_define:nn { moduleA }
{
key .choice:,
key / subA .code:n = {subA's~code\par},
key / subB .code:n = {subB's~code\par},
}
\keys_set:nn { moduleA }
{
key = subA,
key = subB,
% key = {subA, subB}, % --> ERROR
% key = subC % --> ERROR
}


% ==> '.choices:nn'
% when set key of this type:
% 2. all the choices share the same code.
\dotfill\par
\keys_define:nn { moduleB }
{
key .choices:nn =
{ subA, subB }
{ KeyA~and~KeyB~share~the~same~code\par }
}
\keys_set:nn { moduleB }
{
key = subA,
key = subB,
% key = {subA, subB}, % --> ERROR
% key = subC % --> ERROR
}


% ==> '.multichoice:'
% when set key of this type:
% 1. it accepts a set of choices in a time(multiple choice).
\dotfill\par
\keys_define:nn { moduleC }
{
key .multichoice:,
key/subA .code:n = { subA's~code\par },
key/subB .code:n = { subB's~code\par },
key/subC .code:n = { subC's~code\par },
}
\keys_set:nn { moduleC }
{
key = subA,
key = subB,
key = {subA, subB},
% key = subC --> ERROR
}


% ==> '.multichoices:nn'
% when set key of this type:
% 1. it accepts a set of choices in a time(multiple choice), and,
% 2. all the choices share the same code.
\dotfill\par
\keys_define:nn { moduleD }
{
key .multichoices:nn =
{ subA, subB }
{ KeyA~and~KeyB~share~the~same~code\par }
}
\keys_set:nn { moduleD }
{
key = subA,
key = subB,
key = {subA, subB},
% key = subC --> ERROR
}



\section{meta key}
% ==> meta key:
% a key that set another key.
\keys_define:nn { metakey }
{
ikeyA .code:n = {(inner~ikeyA)~being~set~to:#1\par},
ikeyB .code:n = {(inner~ikeyB)~being~set~to:#1\par},
ikeyC / subC .code:n = {(inner~ikeyC/subC)~being~set~to:#1\par},
% meta key inner
MkeyA .meta:n = { ikeyA = #1, ikeyB = #1, ikeyC/subC = #1 },
% meta key outer
MkeyB .meta:nn = { metakey / outside }{#1},
}
% meta key define outside
\keys_define:nn { metakey / outside }
{
okeyA .code:n = {(outside~okeyA)~being~set~to:#1\par},
okeyB .code:n = {(outside~okeyB)~being~set~to:#1\par},
}
\keys_set:nn { metakey }
{
MkeyA = { inner~ikeyA/B/C-val },
MkeyB = { okeyA=okeyA-val, okeyB=okeyB-val },
% MkeyA = { ikeyA=ikeyA-val, keyB=ikeyB-val }, % --> ERROR
% MkeyB = { outer~okeyA/B-val }, % --> ERROR
}



\section{key group}
\keys_define:nn { groupkey }
{
keyA .code:n = { keyA's~code\par },
keyA .groups:n = { GroupA },
keyB .code:n = { keyB's~code\par },
keyB .groups:n = { GroupB },
keyC / subC .code:n = { subC's~code\par },
keyC / subC .groups:n = { GroupA, GroupB },
}
% 1. set keys within groups
\keys_set_groups:nnn { groupkey }{ GroupA }{ keyA, keyB, keyC/subC }
% 2. set keys outside groups
\dotfill\par
\keys_set_filter:nnn { groupkey }{ GroupA }{ keyA, keyB, keyC/subC }
% 3. only set the known keys
\dotfill\par
\keys_set_known:nn { groupkey }{ keyA, keyAA }


\section{inhert}
% ==> '.inherit:'
\keys_define:nn { ParentModuleA }
{
pkeyA .code:n = { ParentA-keyA's~code\par },
pkeyB .code:n = { ParentA-keyB's~code\par },
pkeyC .choice:,
pkeyC / psubC .code:n = { ParentA-keyC's~choice~--~subC's~code\par },
unknown .code:n = { ParentA-unkown's~code\par },
}
\keys_define:nn {}{ childkeyA .inherit:n = { ParentModuleA } }
\keys_define:nn { childkeyA }{ unknown .code:n = { childkeyA-unkown's~code\par } }
\keys_set:nn { childkeyA }{ pkeyA, pkeyB, pkeyC=psubC }
\dotfill\par
\keys_define:nn { ParentModuleB }{ childkeyA .inherit:n = { ParentModuleA } }
\keys_set:nn { ParentModuleB/childkeyA }{ pkeyA, pkeyB }
% delete keys
\dotfill\par
\keys_define:nn { childkeyA }{ pkeyA .undefine: }
% \keys_show:nn { childkeyA }{ pkeyA }
\keys_set:nn { ParentModuleA }{ pkeyA }
\keys_set:nn { childkeyA }{ pkeyA }


% other example
\dotfill\par
\keys_define:nn { ParentA } {
keyA .code:n = { ParentA-keyA's~code\par },
}
\keys_define:nn { } {
ParentB .inherit:n = ParentA,
}
\keys_define:nn { ParentB } {
keyB .code:n = { ParentB-keyB's~code\par },
}

\keys_set:nn { ParentB } { keyA = keyA-val, keyB = keyB-val }
\ExplSyntaxOff
\end{document}

the output of this source code:

reference


l3keys in LaTeX3
https://zongpingding.github.io/2024/05/29/l3keyval/
Author
Eureka
Posted on
May 29, 2024
Licensed under