Debian Codegraph @

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

raw
1 \latexekv\expkvo{-opt}
2
3 \expkvo\ allows to parse \LaTeXe\ class and package options as \kv\ lists using
4 sets of \expkv.
5
6 With the 2021-05-01 release of \LaTeXe\ there were some very interesting changes
7 to the package and class options code. It is now possible to use braces inside
8 the options, and we can access options without them being preprocessed. As a
9 result, some but not all restrictions were lifted from the possible option
10 usage. What will still fail is things that aren't save from an |\edef| expansion
11 (luckily, the \expnotation\ can be used to get around that as well). One feature
12 of \expkvo\ that doesn't work any more is the possibility to parse the unused
13 option list, because that one doesn't contain the full information any more.
14 \expkvo\ will fall back to v0.1 if the kernel is older than 2021-05-01.
15
16 Another very interesting change in \LaTeXe\ was the addition of \pkg{ltkeys} and
17 its |\ProcessKeyOptions| with the possibility to parse future options with it
18 instead of getting the dreaded |Option clash| error. The idea is brilliant and
19 changes made in the 2022-10-22 version allow us to provide the same feature
20 without having to hack any kernel internals, so starting with kernel version
21 2022-11-01 \expkvo\ supports this as well.
22
23 \expkvo\ shouldn't place any restrictions on the keys, historic shortcomings of
24 the kernel cannot be helped though, so the supported things vary with the kernel
25 version (see above). The one thing that \expkvo\ doesn't support, which \expkv\
26 alone would, is active commas or equals signs. But there is no good reason why
27 any of the two should be active in the preamble.
28
29 You can use \LaTeXe's rollback support, so to load v0.1 explicitly use:
30 \begin{enverb}[no-tcb]
31 \usepackage{expkv-opt}[=v0.1]
32 \end{enverb}
33 which will load the last version of \expkvo\ that doesn't use the raw option
34 lists (this shouldn't be done by a package author, but only by a user on a
35 single-document basis if there are some incompatibilities, which is unlikely).
36
37
38 \subsection{Macros\label{sec:o:macros}}
39
40 \subsubsection{Option Processors\label{sec:o:processors}}
41
42 \expkvo's behaviour if it encounters a defined or an undefined \key\ depends on
43 which list is being parsed and whether the current file is a class or not. Of
44 course in every case a defined \key's callback will be invoked but an
45 additional action might be executed. For this reason the rule set of every macro
46 will be given below the short description which list it will parse.
47
48 During each of the processing macros the current list element (not processed in
49 any way) is stored within the macro |\CurrentOption|.
50
51 \begin{function}{\ekvoProcessOptions}
52 \begin{syntax}
53 \cs{ekvoProcessOptions}\marg{set}
54 \end{syntax}
55 This runs |\ekvoProcessGlobalOptions|, then |\ekvoProcessLocalOptions|, and
56 finally |\ekvoProcessFutureOptions|. If you're using |\ekvoUseUnknownHandlers|
57 it'll affect all three option processors. Else the respective default
58 unknown-rules are used.
59 \end{function}
60
61 \begin{function}{\ekvoProcessLocalOptions}
62 \begin{syntax}
63 \cs{ekvoProcessLocalOptions}\marg{set}
64 \end{syntax}
65 This parses the options which are directly passed to the current class or
66 package for an \expkv\ \set.
67 \end{function}
68 \expkvorules
69 {%
70 cd = {remove the option from the list of unused global options if the local
71 option list matches the option list of the main class and the unused
72 global options list is not empty; else \emph{nothing}}
73 ,cu = add the key to the list of unused global options (if the local option
74 list matches the option list of the main class)
75 ,pu = throw an error
76 }
77
78 \begin{function}{\ekvoProcessGlobalOptions}
79 \begin{syntax}
80 \cs{ekvoProcessGlobalOptions}\marg{set}
81 \end{syntax}
82 In \LaTeXe\ the options given to |\documentclass| are global options. This
83 macro processes the global options for an \expkv\ \set.
84 \end{function}
85 \expkvorules{d = remove the option from the list of unused global options}
86
87 \begin{function}{\ekvoProcessFutureOptions}
88 \begin{syntax}
89 \cs{ekvoProcessFutureOptions}\marg{set}
90 \end{syntax}
91 This parses the option list of every future call of the package with
92 |\usepackage| or similar with an \expkv\ \set, circumventing the
93 |Option clash| error that'd be thrown by \LaTeXe. It is only available for
94 kernel versions starting with 2022-11-01. It is mutually exclusive with
95 \LaTeXe's |\ProcessKeyOptions| (which ever comes last defines how future
96 options are parsed).
97 \end{function}
98 \expkvorules{u = throw an error}
99
100 \begin{function}{\ekvoProcessOptionsList}
101 \begin{syntax}
102 \cs{ekvoProcessOptionsList}\meta{list}\marg{set}
103 \end{syntax}
104 Process the \kv\ list stored in the macro \meta{list}.
105 \end{function}
106 \expkvorules{}
107
108
109 \subsubsection{Other Macros\label{sec:o:others}}
110
111 \begin{function}{\ekvoUseUnknownHandlers}
112 \begin{syntax}
113 \cs{ekvoUseUnknownHandlers}\meta{cs_1}\meta{cs_2}\quad{\normalfont\itshape or}
114 \cs{ekvoUseUnknownHandlers}*
115 \end{syntax}
116 With this macro you can change the action \expkvo\ executes if it encounters
117 an undefined \key\ for the next (and only the next) list processing macro. The
118 macro \meta{cs_1} will be called if an undefined \Nkey\ is encountered and get
119 one argument being the \key\ (without being |\detokenize|d). Analogous the
120 macro \meta{cs_2} will be called if an undefined \Vkey\ was parsed and get two
121 arguments, the first being the \key\ (without being |\detokenize|d) and the
122 second the \val.
123 \par
124 If you use the starred variant, it'll not take further arguments. In this case
125 the undefined handlers defined via |\ekvdefunknown| and |\ekvdefunknownNoVal|
126 in the parsing set get used, and if those aren't available they'll simply do
127 nothing.
128 \end{function}
129
130 \begin{function}{\ekvoVersion,\ekvoDate}
131 These two macros store the version and date of the package.
132 \end{function}
133
134
135 \subsection{Examples}
136
137 \begin{example}{A package using \expkvo}
138 Let's say we want to create a package that changes the way footnotes are
139 displayed in \LaTeX. For this it will essentially just redefine |\thefootnote|
140 and we'll call this package \pkg{ex-footnote}. First we report back which
141 package we are:
142 \begin{enverb}[no-tcb]
143 \ProvidesPackage{ex-footnote}[2020-02-02 v1 change footnotes]
144 \end{enverb}
145 Next we'll need to provide the options we want the package to have.
146 \begin{enverb}[no-tcb]
147 \RequirePackage{color}
148 \RequirePackage{expkv-opt}% also loads expkv
149 \ekvdef{ex-footnote}{color}{\def\exfn@color{#1}}
150 \ekvdef{ex-footnote}{format}{\def\exfn@format{#1}}
151 \end{enverb}
152 We can provide initial values just by defining the two macros storing the
153 value.
154 \begin{enverb}[no-tcb]
155 \newcommand*\exfn@color{}
156 \newcommand*\exfn@format{arabic}
157 \end{enverb}
158 Next we need to process the options given to the package. The package should
159 only obey options directly passed to it, so we're using
160 |\ekvoProcessLocalOptions| and |\ekvoProcessFutureOptions|:
161 \begin{enverb}[no-tcb]
162 \ekvoProcessLocalOptions {ex-footnote}
163 \ekvoProcessFutureOptions{ex-footnote}
164 \end{enverb}
165 Now everything that's still missing is actually changing the way footnotes
166 appear:
167 \begin{enverb}[no-tcb]
168 \renewcommand*\thefootnote
169 {%
170 \ifx\exfn@color\@empty
171 \csname\exfn@format\endcsname{footnote}%
172 \else
173 \textcolor{\exfn@color}{\csname\exfn@format\endcsname{footnote}}%
174 \fi
175 }
176 \end{enverb}
177 So the complete code of the package would look like this:
178 \begin{enverb}[no-tcb]
179 \ProvidesPackage{ex-footnote}[2020-02-02 v1 change footnotes]
180
181 \RequirePackage{color}
182 \RequirePackage{expkv-opt}% also loads expkv
183
184 \ekvdef{ex-footnote}{color}{\def\exfn@color{#1}}
185 \ekvdef{ex-footnote}{format}{\def\exfn@format{#1}}
186 \newcommand*\exfn@color{}
187 \newcommand*\exfn@format{arabic}
188
189 \ekvoProcessLocalOptions {ex-footnote}
190 \ekvoProcessFutureOptions{ex-footnote}
191
192 \renewcommand*\thefootnote
193 {%
194 \ifx\exfn@color\@empty
195 \csname\exfn@format\endcsname{footnote}%
196 \else
197 \textcolor{\exfn@color}{\csname\exfn@format\endcsname{footnote}}%
198 \fi
199 }
200 \end{enverb}
201 And it could be used with one (or thanks to |\ekvoProcessFutureOptions| all)
202 of the following lines:
203 \begin{enverb}[no-tcb]
204 \usepackage{ex-footnote}
205 \usepackage[format=fnsymbol]{ex-footnote}
206 \usepackage[color=green]{ex-footnote}
207 \usepackage[color=red,format=roman]{ex-footnote}
208 \end{enverb}
209 \end{example}
210
211 \begin{example}{Parsing the global options}
212 This document was compiled with the global options
213 \texttt{[\detokenize\expandafter\expandafter\expandafter{\csname @raw@classoptionslist\endcsname]}}
214 in use. If we define the following keys
215 \begin{enverb}[store,no-tcb]
216 \ekvdef{optexample}{exfoo}
217 {Global option \texttt{exfoo} got \texttt{\detokenize{#1}}.\par}
218 \ekvdefNoVal{optexample}{exbar}
219 {Global option \texttt{exbar} set.\par}
220 \end{enverb}
221 we can use those options to control the result of the following:
222 \begin{enverb}[restore,same-line=.55]
223 \ekvoProcessGlobalOptions{optexample}
224 \end{enverb}
225 Please note that under normal conditions
226 \cs[no-index]{ekvoProcessGlobalOptions} is only useable in the preamble; this
227 example is only for academic purposes, you'll not be able to reproduce this
228 with the exact code shown above.
229 \end{example}