Debian Codegraph @

texmf-dist/source/latex/l3packages/l3keys2e/l3keys2e.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3keys2e.dtx
4 %
5 % Copyright (C) 2009-2025 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3packages bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver|package>
26 % The version of expl3 required is tested as early as possible, as
27 % some really old versions do not define \ProvidesExplPackage.
28 \RequirePackage{expl3}[2020/02/08]
29 %<package>\@ifpackagelater{expl3}{2020/02/08}
30 %<package> {}
31 %<package> {%
32 %<package> \PackageError{l3keys2e}{Support package l3kernel too old}
33 %<package> {%
34 %<package> Please install an up to date version of l3kernel\MessageBreak
35 %<package> using your TeX package manager or from CTAN.\MessageBreak
36 %<package> \MessageBreak
37 %<package> Loading l3keys2e will abort!%
38 %<package> }%
39 %<package> \endinput
40 %<package> }
41 %</driver|package>
42 %<*driver>
43 \documentclass[full]{l3doc}
44 \usepackage{amstext}
45 \begin{document}
46 \DocInput{\jobname.dtx}
47 \end{document}
48 %</driver>
49 % \fi
50 %
51 % \title{^^A
52 % The \pkg{l3keys2e} package\\
53 % \LaTeXe{} option processing using \LaTeX3 keys^^A
54 % }
55 %
56 % \author{^^A
57 % The \LaTeX{} Project\thanks
58 % {^^A
59 % E-mail:
60 % \href{mailto:[email protected]}
61 % {[email protected]}^^A
62 % }^^A
63 % }
64 %
65 % \date{Released 2025-10-09}
66 %
67 % \maketitle
68 %
69 % \begin{documentation}
70 %
71 % \section{Introduction}
72 %
73 % \begin{center}
74 % \bfseries This package is obsolete with the June 2022 \LaTeX{} release.
75 % \end{center}
76 %
77 % With new formats, \cs{ProcessKeyOptions} is available as a more powerful
78 % version of \cs{ProcessKeysOptions}. In particular, it is built directly
79 % into the \LaTeX{} kernel, uses `raw' options (and therefore does not
80 % suffer from space stripping and expansion issues), and can avoid
81 % option clash warnings. Package authors should transition their
82 % code to the new approach.
83 %
84 % \section{Old introduction}
85 %
86 % The key--value method for optional arguments is very popular, as it
87 % allows the class or package author to define a large number of
88 % options with a simple interface. The \pkg{expl3} bundle of \LaTeX3
89 % base code includes the module \pkg{l3keys} for defining keys, but
90 % to use these when loading \LaTeXe\ packages and classes requires
91 % extra support. That support is provided by this small package, which
92 % is intended to enable \LaTeXe\ packages to benefit from the power of
93 % the \LaTeX3 key--value system.
94 %
95 % \subsection{Creating and using keyval options}
96 %
97 % As with any key--value input, using key--value pairs as package or
98 % class options has two parts. The first stage is to define one or
99 % more keys, using the \cs{keys_define:nn} function. For example, an
100 % option which simply stores a value would be created using:
101 % \begin{verbatim}
102 % \keys_define:nn { module }
103 % { option .tl_set:N = \l_module_variable_tl }
104 % \end{verbatim}
105 % On its own, this will not make the key an option for the package or
106 % class containing the definition. The second stage is therefore to
107 % process the current options, searching for applicable keys.
108 %
109 % \begin{function}{\ProcessKeysOptions}
110 % \begin{syntax}
111 % \cs{ProcessKeysOptions} \Arg{module}
112 % \end{syntax}
113 % The \cs{ProcessKeysOptions} function is used to check the current
114 % option list against the keys defined for \Arg{module}. Global (class)
115 % options and local (package) options are checked when this function
116 % is called in a package. Each option which does match a key name is
117 % then used to attempt to set the appropriate key using
118 % \cs{keys_set:nn}. For example, the option defined earlier would be
119 % processed by the line
120 % \begin{verbatim}
121 % \ProcessKeysOptions { module }
122 % \end{verbatim}
123 % \end{function}
124 %
125 % \begin{function}{\ProcessKeysPackageOptions}
126 % \begin{syntax}
127 % \cs{ProcessKeysPackageOptions} \Arg{module}
128 % \end{syntax}
129 % This function works in a similar manner to \cs{ProcessKeysOptions}.
130 % When used in a \LaTeXe{} package, \cs{ProcessKeysPackageOptions}
131 % will not examine any class options available. In contrast,
132 % \cs{ProcessKeysOptions} does parse class options (in common with the
133 % \LaTeXe{} kernel function \cs{ProcessOptions}).
134 % \end{function}
135 %
136 % When passing unknown keys to other packages, the standard \LaTeX{}
137 % \tn{CurrentOption} command is available and should be used. In contrast
138 % to \cs{l_keys_key_str}, \cs{CurrentOption} is a token list and thus retains
139 % category code information. Depending on how options are used by third-party
140 % packages, this may be essential for the option to be recognized.
141 %
142 %\end{documentation}
143 %
144 %\begin{implementation}
145 %
146 %\subsection{\pkg{l3keys2e} implementation}
147 %
148 % \begin{macrocode}
149 %<*package>
150 % \end{macrocode}
151 %
152 % \begin{macrocode}
153 %<@@=keys>
154 % \end{macrocode}
155 %
156 % \begin{macrocode}
157 \ProvidesExplPackage{l3keys2e}{2025-10-09}{}
158 {LaTeX2e option processing using LaTeX3 keys}
159 % \end{macrocode}
160 %
161 % Allow for a kernel-based implementation
162 % \begin{macrocode}
163 \cs_if_exist:NT \ProcessKeysOptions
164 { \file_input_stop: }
165 % \end{macrocode}
166 %
167 % Non-standard variants.
168 % \begin{macrocode}
169 \cs_generate_variant:Nn \clist_put_right:Nn { Nv }
170 \cs_generate_variant:Nn \keys_if_exist:nnT { nx }
171 \cs_generate_variant:Nn \keys_if_exist:nnTF { nx }
172 % \end{macrocode}
173 %
174 % \begin{macro}{\l_@@_latexe_options_clist}
175 % A single list is used for all options, into which they are collected
176 % before processing.
177 % \begin{macrocode}
178 \clist_new:N \l_@@_latexe_options_clist
179 % \end{macrocode}
180 % \end{macro}
181 %
182 % \begin{macro}{\l_@@_process_class_bool}
183 % A flag to indicate that class options should be processed for
184 % packages.
185 % \begin{macrocode}
186 \bool_new:N \l_@@_process_class_bool
187 % \end{macrocode}
188 % \end{macro}
189 %
190 % \begin{macro}{\@@_latexe_options:n}
191 % The main function calls functions to collect up the global and local
192 % options into \cs{l_@@_latexe_options_clist} before calling the
193 % underlying functions to actually do the processing. So that a suitable
194 % message is produced if the option is unknown, the special
195 % \texttt{unknown} key is set if it does not already exist for the
196 % current module.
197 % \begin{macrocode}
198 \cs_new_protected:Npn \@@_latexe_options:n #1
199 {
200 \clist_clear:N \l_@@_latexe_options_clist
201 \@@_latexe_options_global:n {#1}
202 \@@_latexe_options_local:
203 \keys_if_exist:nnF {#1} { unknown }
204 {
205 \keys_define:nn {#1}
206 {
207 unknown .code:n =
208 {
209 \msg_error:nnee { keyvalue } { option-unknown }
210 { \l_keys_key_str } { \@currname }
211 }
212 }
213 \AtEndOfPackage
214 { \keys_define:nn {#1} { unknown .undefine: } }
215 }
216 \clist_map_inline:Nn \l_@@_latexe_options_clist
217 {
218 \tl_set:Nn \CurrentOption {##1}
219 \keys_set:nn {#1} {##1}
220 }
221 \AtEndOfPackage { \cs_set_eq:NN \@unprocessedoptions \scan_stop: }
222 }
223 % \end{macrocode}
224 % \end{macro}
225 %
226 % \begin{macro}{\@@_latexe_options_global:n}
227 % Global (class) options are handled differently for \LaTeXe{} packages
228 % and classes. Hence this function is essentially a check on the current
229 % file type. The initial test is needed as \LaTeXe{} allows variables to
230 % be equal to \cs{scan_stop:}, which is forbidden in \LaTeX3 code.
231 % \begin{macrocode}
232 \cs_new_protected:Npn \@@_latexe_options_global:n #1
233 {
234 \cs_if_eq:NNF \@classoptionslist \scan_stop:
235 {
236 \cs_if_eq:NNTF \@currext \@clsextension
237 { \@@_latexe_options_class:n {#1} }
238 {
239 \bool_if:NT \l_@@_process_class_bool
240 { \@@_latexe_options_package:n {#1} }
241 }
242 }
243 }
244 % \end{macrocode}
245 % \end{macro}
246 %
247 % \begin{macro}{\@@_latexe_options_class:n}
248 % For classes, each option (stripped of any content after |=|)
249 % is checked for existence as a key. If found, the option is added to
250 % the combined list for processing. On the other hand, unused options
251 % are stored up in \cs{@unusedoptionlist}. Before any of that, though,
252 % there is a simple check to see if there is an |unknown| key. If there
253 % is, then \emph{everything} will match and the mapping can be skipped.
254 % \begin{macrocode}
255 \cs_new_protected:Npn \@@_latexe_options_class:n #1
256 {
257 \cs_if_free:cF { opt@ \@currname . \@currext }
258 {
259 \keys_if_exist:nnTF {#1} { unknown }
260 {
261 \clist_put_right:Nv \l_@@_latexe_options_clist
262 { opt@ \@currname . \@currext }
263 }
264 {
265 \clist_map_inline:cn { opt@ \@currname . \@currext }
266 {
267 \keys_if_exist:neTF {#1} { \@@_latexe_remove_equals:n {##1} }
268 { \clist_put_right:Nn \l_@@_latexe_options_clist {##1} }
269 { \clist_put_right:Nn \@unusedoptionlist {##1} }
270 }
271 }
272 }
273 }
274 % \end{macrocode}
275 % \end{macro}
276 %
277 % \begin{macro}{\@@_latexe_options_package:n}
278 % For global options when processing a package, the tasks are slightly
279 % different from those for a class. The check is the same, but here
280 % there is nothing to do if the option is not applicable. Each valid
281 % option also needs to be removed from \cs{@unusedoptionlist}.
282 % \begin{macrocode}
283 \cs_new_protected:Npn \@@_latexe_options_package:n #1
284 {
285 \clist_map_inline:Nn \@classoptionslist
286 {
287 \keys_if_exist:neT {#1} { \@@_latexe_remove_equals:n {##1} }
288 {
289 \clist_put_right:Nn \l_@@_latexe_options_clist {##1}
290 \clist_remove_all:Nn \@unusedoptionlist {##1}
291 }
292 }
293 }
294 % \end{macrocode}
295 % \end{macro}
296 %
297 % \begin{macro}{\@@_latexe_options_local:}
298 % If local options are found, the are added to the processing list.
299 % \LaTeXe{} stores options for each file in a macro which may or may not
300 % exist, hence the need to use \cs{cs_if_exist:c}.
301 % \begin{macrocode}
302 \cs_new_protected:Npn \@@_latexe_options_local:
303 {
304 \cs_if_eq:NNF \@currext \@clsextension
305 {
306 \cs_if_exist:cT { opt@ \@currname . \@currext }
307 {
308 \exp_args:NNc \clist_put_right:NV \l_@@_latexe_options_clist
309 { opt@ \@currname . \@currext }
310 }
311 }
312 }
313 % \end{macrocode}
314 % \end{macro}
315 %
316 % \begin{macro}{\@@_latexe_remove_equals:n}
317 % \begin{macro}{\@@_latexe_remove_equals:w}
318 % As the name suggests, this is a simple function to remove an equals
319 % sign from the input. This is all wrapped up in an \texttt{n} function
320 % so that there will always be a sign available.
321 % \begin{macrocode}
322 \cs_new:Npn \@@_latexe_remove_equals:n #1
323 { \@@_latexe_remove_equals:w #1 = \s_@@_stop }
324 \cs_new:Npn \@@_latexe_remove_equals:w #1 = #2 \s_@@_stop { \exp_not:n {#1} }
325 % \end{macrocode}
326 % \end{macro}
327 % \end{macro}
328 %
329 % \begin{macro}{\ProcessKeysOptions}
330 % \begin{macro}{\ProcessKeysPackageOptions}
331 % The user macro are simply wrappers around the internal process. In
332 % contrast to other similar packages, the module name is always required
333 % here.
334 % \begin{macrocode}
335 \cs_new_protected:Npn \ProcessKeysOptions #1
336 {
337 \bool_set_true:N \l_@@_process_class_bool
338 \@@_latexe_options:n {#1}
339 }
340 \cs_new_protected:Npn \ProcessKeysPackageOptions #1
341 {
342 \bool_set_false:N \l_@@_process_class_bool
343 \@@_latexe_options:n {#1}
344 }
345 \@onlypreamble \ProcessKeysOptions
346 \@onlypreamble \ProcessKeysPackageOptions
347 % \end{macrocode}
348 %\end{macro}
349 %\end{macro}
350 %
351 % One message to give.
352 % \begin{macrocode}
353 \msg_new:nnnn { keyvalue } { option-unknown }
354 { Unknown~option~'#1'~for~package~#2. }
355 {
356 LaTeX~has~been~asked~to~set~an~option~called~'#1'~
357 but~the~#2~package~has~not~created~an~option~with~this~name.
358 }
359 % \end{macrocode}
360 %
361 % \begin{macrocode}
362 %</package>
363 % \end{macrocode}
364 %
365 % \end{implementation}
366 %
367 % \PrintIndex