Debian Codegraph @

texmf-dist/source/latex/graphics/keyval.dtx

raw
1 %
2 % \iffalse
3 %% keyval.dtx Copyright (C) 1993 1994 1995 1997 1998 1999 David Carlisle
4 %% Copyright (C) 2000-2025 David Carlisle, LaTeX Project
5 %% The LaTeX Project and any individual authors listed elsewhere
6 %% in this file.
7 %%
8 %% This file is part of the Standard LaTeX `Graphics Bundle'.
9 %% It may be distributed under the terms of the LaTeX Project Public
10 %% License, as described in lppl.txt in the base LaTeX distribution.
11 %% Either version 1.3c or, at your option, any later version.
12 %%
13 %
14 %<*dtx>
15 \ProvidesFile{keyval.dtx}
16 %</dtx>
17 %<*!plain>
18 %<package&!plain>\NeedsTeXFormat{LaTeX2e}
19 %<package&!plain>\ProvidesPackage{keyval}
20 %<driver> \ProvidesFile{keyval.drv}
21 % \fi
22 % \ProvidesFile{keyval.dtx}
23 [2022/05/29 v1.15 key=value parser (DPC)]
24 %
25 % \iffalse
26 %</!plain>
27 %<*driver>
28 \documentclass{ltxdoc}
29 \usepackage{keyval}
30 \begin{document}
31 \DocInput{keyval.dtx}
32 \end{document}
33 %</driver>
34 % \fi
35 %
36 % \GetFileInfo{keyval.dtx}
37 % \title{The \textsf{keyval} package\thanks{This file
38 % has version number \fileversion, last
39 % revised \filedate.}}
40 % \author{David Carlisle}
41 % \date{\filedate}
42 % \MaintainedByLaTeXTeam{graphics}
43 % \maketitle
44 %
45 % \begin{abstract}
46 % A \LaTeX\ package implementing a system allowing the setting
47 % of parameters (or `named arguments' with a
48 % \meta{key}${}={}$\meta{value} syntax.
49 %
50 % Eg: |\foo[height=3in, shadow = true ]{bar}|
51 % \end{abstract}
52 %
53 %
54 % \changes{v1.03}
55 % {1993/10/13}{Initial version}
56 % \changes{v1.04}{1993/11/15}
57 % {Upgrade after comments from Timothy van Zandt}
58 % \changes{v1.05}{1993/11/17}
59 % {Further small improvements}
60 % \changes{v1.06}{1994/02/01}
61 % {Update to LaTeX2e}
62 % \changes{v1.07}{1994/03/15}
63 % {New style ltxdoc}
64 % \changes{v1.08}{1994/09/12}
65 % {Improve docstrip handling}
66 % \changes{v1.09}{1995/09/21}
67 % {Move a comma for graphics/1698}
68 % \changes{v1.14}{2014/04/25}
69 % {Make most definitions \cs{long} to allow \cs{par} in values: graphics/3446}
70 % \changes{v1.14}{2014/04/25}
71 % {Only strip a single brace pair from values: graphics/3446}
72 % \changes{v1.15}{2014/05/08}
73 % {revert this change: Only strip a single brace pair from values: graphics/3446}
74 %
75 %
76 % This package implements a system of defining and using sets of
77 % parameters, which are set using the syntax \meta{key}=\meta{value}.
78 %
79 % For each keyword in such a set, there exists a function which is
80 % called whenever the parameter appears in a parameter list. For
81 % instance if the set |dpc| is to have the keyword |scale| then I
82 % would define.\\
83 % | \define@key{dpc}{scale}{scale ({\tt\string#1})\\}|\\
84 % The first argument of |\define@key| is the set of keywords being
85 % used, the second is the keyword, and the third is the function to
86 % call. This function will be given as |#1| the \meta{value} specified
87 % by the user.
88 %
89 % Normally it is an error to omit the `=\meta{value}' however if an
90 % optional \meta{value} is supplied when the keyword is defined, then
91 % just the keyword need be supplied.\\
92 % |\define@key{dpc}{clip}[true]{...}|\\
93 % For `|clip|' you can go `|clip = true|' or `|clip = false|' or
94 % just `|clip|', which is the same as `|clip = true|'
95 %
96 % To use these keywords, just call `|\setkeys|' with a comma
97 % separated list of settings, each of the form
98 % \meta{key}=\meta{value}, or just \meta{key}. Any white space around
99 % the `|=|' and `|,|' is ignored.
100 %
101 % As the \meta{key} is passed as a macro argument, if it consists
102 % entirely of a |{ }| group, the outer braces are stripped off. Thus
103 % |,key=foo,| and |,key={foo},| are equivalent. This fact enables one to
104 % `hide' any commas or equals signs that must appear in the value. i.e.\
105 % in |foo={1,2,3},bar=4|, |foo| gets the value |1,2,3|, the comma after
106 % |1| does not terminate the keyval pair, as it is `hidden' by the
107 % braces.
108 %
109 % Empty entries, with nothing between the commas, are silently ignored.
110 % This means that it is not an error to have a comma after the last
111 % term, or before the first.
112 %
113 % \section{Example}
114 %
115 % We may extend the examples above to give a `fake' graphics
116 % inclusion macro, with a syntax similar to that used in the psfig
117 % macros.
118 %
119 % \makeatletter
120 % \def\dpcgraphics{\@ifnextchar[\@dpcgraphics{\@dpcgraphics[]}}
121 %
122 % |\dpcgraphics| has one optional argument which is passed through
123 % |\setkeys|, and one mandatory argument, the filename. It actually
124 % just typesets its arguments, for demonstration.
125 %
126 % \def\@dpcgraphics[#1]#2{{\setkeys{dpc}{#1}INPUT: #2}}%
127 %
128 % \define@key{dpc}{scale}{scale ({\tt\string#1\relax})\\}
129 % \define@key{dpc}{height}{height ({\tt#1})\\}
130 % \define@key{dpc}{width}{width ({\tt#1})\\}
131 % \define@key{dpc}{bb}{bounding box ({\tt#1})\\}
132 % \define@key{dpc}{clip}[true]{clip ({\tt\string#1\relax})\\}
133 % \makeatother
134 %
135 % The declared keys are: |scale|, |height|, |width|, |bb|,
136 % and |clip|. Except for the last, they must all be given a value if
137 % used.
138 %
139 % Note how in the following, any white space around |=| or |,| is
140 % ignored, as are the `empty' arguments caused by extra commas. Note
141 % also that each macro receives \emph{exactly} the tokens that you
142 % specify as arguments, no premature expansion is done.
143 %
144 % \begin{verbatim}
145 % \def\dpcgraphics{\@ifnextchar[\@dpcgraphics{\@dpcgraphics[]}}
146 % \def\@dpcgraphics[#1]#2{{\setkeys{dpc}{#1}INPUT: #2}}
147 %
148 % \define@key{dpc}{scale}{scale ({\tt\string#1\relax})\\}
149 % \define@key{dpc}{clip}[true]{clip ({\tt\string#1\relax})\\}
150 % \end{verbatim}
151 %
152 % \begin{minipage}{.4\textwidth}
153 % \begin{verbatim}
154 % \def\scalemacro{9}
155 % \dpcgraphics
156 % [ height =4in, ,
157 % width = 3in,
158 % scale = \scalemacro,
159 % bb = 20 20 300 400 ,
160 % clip,
161 % ]{aaa}
162 % \end{verbatim}
163 % \end{minipage}
164 % \hfill
165 % \begin{minipage}{.4\textwidth}
166 % \def\scalemacro{9}
167 %\dpcgraphics
168 % [ height =4in, ,
169 % width = 3in,
170 % scale = \scalemacro ,
171 % bb = 20 20 300 400 ,
172 % clip ,
173 % ]{aaa}
174 % \end{minipage}
175 %
176 %
177 % \section{The Internal Interface}
178 % A declaration of the form:\\
179 % |\define@key{family}{key}{...}|\\
180 % Defines a macro |\KV@family@key| with one argument. When used in a
181 % keyval list, the macro receives the value as its argument.
182 %
183 % A declaration of the form:\\
184 % |\define@key{family}{key}[default]{...}|\\
185 % Defines a macro |\KV@family@key| as above, however it also defines the
186 % macro |\KV@family@key@default| as a macro with no arguments, and
187 % definition\\
188 % |\KV@family@key{default}|.
189 %
190 % Thus if macros are defined using |\define@key|, the use of a key with
191 % no value \ldots|,foo,|\ldots\ is always equivalent to the use of the
192 % key with some value, \ldots|,foo=default,|\ldots. However a package
193 % writer may wish that the `default' behaviour for some key is not
194 % directly equivalent to using that key with a value. (In particular, as
195 % pointed out to me by Timothy Van Zandt, you may wish to omit error
196 % checking on the default value as you know it is correct.) In these
197 % cases one simply needs to define the two macros
198 % |\KV@|\meta{family}|@key| and |\KV@|\meta{family}|@key@default|
199 % directly using |\def| (or |\newcommand|). I do not supply a user
200 % interface for this type of definition, but it is supported in the
201 % sense that I will try to ensure that any future upgrades of this
202 % package do not break styles making use of these `low level'
203 % definitions.
204 %
205 % \MaybeStop{}
206 %
207 % \section{The Macros}
208 %
209 % From version~1.05, all `internal' macros associated to keys have names
210 % of the form:\\
211 % |\KV@|\meta{family}|@|\meta{key} or
212 % |\KV@|\meta{family}|@|\meta{key}|@|\meta{default}
213 %
214 % \begin{macrocode}
215 %<*package>
216 % \end{macrocode}
217 %
218 % \begin{macro}{\setkeys}
219 % The top level macro. |#2| should be a comma separated values of the
220 % form \meta{key} |=| \meta{value} or just simply \meta{key}.
221 % The macro associated with this key in the `family' |#1| is called with
222 % argument \meta{value}. The second form is only allowed if the key was
223 % declared with a default value.
224 % \begin{macrocode}
225 \long\def\setkeys#1#2{%
226 % \end{macrocode}
227 % Save the `family' for later. Then begin acting on the comma
228 % separated list.
229 % \changes{v1.11}{1998/06/05}
230 % {Make \cs{@tempc} safe (in case it is an \cs{if}.}
231 % \begin{macrocode}
232 \def\KV@prefix{KV@#1@}%
233 \let\@tempc\relax
234 \KV@do#2,\relax,}
235 % \end{macrocode}
236 % \end{macro}
237 %
238 % \begin{macro}{\KV@do}
239 % Iterate down the list of comma separated argument pairs.
240 % \changes{v1.14}{2014/04/25}
241 % {Add \cs{@empty} not \cs{empty}}
242 % \begin{macrocode}
243 \long\def\KV@do#1,{%
244 \ifx\relax#1\@empty\else
245 \KV@split#1==\relax
246 \expandafter\KV@do\fi}
247 % \end{macrocode}
248 % \end{macro}
249 %
250 % \begin{macro}{\KV@split}
251 % Split up the keyword and value, and call the appropriate command.
252 % This macro was slightly reorganised for version 1.04, after some
253 % suggestions from Timothy Van Zandt.
254 % \begin{macrocode}
255 \long\def\KV@split#1=#2=#3\relax{%
256 \KV@@sp@def\@tempa{#1}%
257 \ifx\@tempa\@empty\else
258 \expandafter\let\expandafter\@tempc
259 \csname\KV@prefix\@tempa\endcsname
260 \ifx\@tempc\relax
261 %<plain> \KV@err
262 %<!plain> \KV@errx
263 {\@tempa\space undefined}%
264 \else
265 \ifx\@empty#3\@empty
266 \KV@default
267 \else
268 \KV@@sp@def\@tempb{#2}%
269 \expandafter\@tempc\expandafter{\@tempb}\relax
270 \fi
271 \fi
272 \fi}
273 % \end{macrocode}
274 % \end{macro}
275 %
276 % \begin{macro}{\KV@default}
277 % Run the default code, or raise an error.
278 % \begin{macrocode}
279 \def\KV@default{%
280 \expandafter\let\expandafter\@tempb
281 \csname\KV@prefix\@tempa @default\endcsname
282 \ifx\@tempb\relax
283 \KV@err{No value specified for \@tempa}%
284 \else
285 \@tempb\relax
286 \fi}
287 % \end{macrocode}
288 % \end{macro}
289 %
290 % \begin{macro}{\KV@err}
291 % \changes{v1.10}{1997/11/10}
292 % {Use \cs{PackageError}}
293 % \changes{v1.12}{1998/11/18}
294 % {Option system added}
295 % Error messages.
296 % \begin{macrocode}
297 %<plain>\def\KV@err#1{\errmessage{key-val: #1}}
298 %<*!plain>
299 \DeclareOption{unknownkeysallowed}{%
300 \def\KV@errx#1{\PackageInfo{keyval}{#1}}}
301 \DeclareOption{unknownkeyserror}{%
302 \def\KV@errx#1{\PackageError{keyval}{#1}\@ehc}}
303 \ExecuteOptions{unknownkeyserror}
304 \let\KV@err\KV@errx
305 \ProcessOptions
306 %</!plain>
307 % \end{macrocode}
308 % \end{macro}
309 %
310 % \begin{macro}{\KV@@sp@def}
311 % \changes{v1.10}{1997/11/10}
312 % {Reorganise to not require doubled hash tokens, and to not
313 % lose initial brace groups.}
314 % \begin{macro}{\KV@@sp@b}
315 % \begin{macro}{\KV@@sp@c}
316 % \begin{macro}{\KV@@sp@d}
317 % |\KV@@sp@def|\meta{cmd}\meta{token list} is like |\def|, except that
318 % a space token at the beginning or end of \meta{token list} is
319 % removed before making the assignment. \meta{token list} may not
320 % contain the token |\@nil|, unless it is within a brace group.
321 % The names of these commands were changed at version~1.05 to ensure
322 % that they do not clash with `internal' macros in a key family `sp'.
323 %
324 % Since v1.10, |#| may appear in the second argument without it
325 % needing to be doubled as |##|. Also earlier versions would
326 % drop any initial brace group, so |{abc}d| would incorrectly
327 % be treated as |abcd|. The current version only removes brace
328 % groups that surround the entire value, so |{abcd}| \emph{is} treated
329 % correctly as |abcd|. Prior to v1.14, two levels of bracing are removed, if
330 % you require the entire argument to be a single brace group, you had
331 % use |{{{abcd}}}|, from v1.14 exactly one brace group is removed, so to make
332 % the entire value be a brace group you need |{{abc}}|.
333 %
334 % \begin{macrocode}
335 \def\@tempa#1{%
336 % \end{macrocode}
337 %
338 % \begin{macrocode}
339 \long\def\KV@@sp@def##1##2{%
340 \futurelet\KV@tempa\KV@@sp@d##2\@nil\@nil#1\@nil\relax##1}%
341 % \end{macrocode}
342 %
343 % Early release removed initial space by having an `extra' argument
344 % in |\KV@@sp@b| but that removed too many braces, so now make
345 % |\KV@@sp@b| explicitly remove a single space token. That unfortunately
346 % means we need the new |\KV@@sp@d| command to add a space token if
347 % one was not there before.
348 % \begin{macrocode}
349 \def\KV@@sp@d{%
350 \ifx\KV@tempa\@sptoken
351 \expandafter\KV@@sp@b
352 \else
353 \expandafter\KV@@sp@b\expandafter#1%
354 \fi}%
355 % \end{macrocode}
356 %
357 % \changes{v1.14}{2014/04/25}
358 % {Add \cs{@empty} to avoid dropping a brace pair: graphics/3446}
359 % \changes{v1.15}{2014/05/08}
360 % {revert last change}
361 % \begin{macrocode}
362 \long\def\KV@@sp@b#1##1 \@nil{\KV@@sp@c##1}%
363 % \end{macrocode}
364 %
365 % \begin{macrocode}
366 %<plain>\def\@sptoken{#1}%
367 % \end{macrocode}
368 %
369 % Make the above definitions, inserting the space token where needed.
370 % \begin{macrocode}
371 }
372 \@tempa{ }
373 % \end{macrocode}
374 %
375 % \changes{v1.14}{2014/04/25}
376 % {Add \cs{expandafter} to remove the extra \cs{@empty} token: graphics/3446}
377 % \changes{v1.15}{2014/05/08}
378 % {revert last change}
379 % \begin{macrocode}
380 \long\def\KV@@sp@c#1\@nil#2\relax#3{\KV@toks@{#1}\edef#3{\the\KV@toks@}}
381 % \end{macrocode}
382 % \end{macro}
383 % \end{macro}
384 % \end{macro}
385 % \end{macro}
386 %
387 % \begin{macro}{\KV@toks@}
388 % Macro register used above to prevent |#| doubling.
389 % Avoid uding one of the normal scratch registers, as this code
390 % is not in a local group.
391 % \begin{macrocode}
392 \newtoks\KV@toks@
393 % \end{macrocode}
394 % \end{macro}
395 %
396 % \begin{macro}{\define@key}
397 % Define the command associated to the key |#2| in the family |#1|.
398 % First looks for a default argument (the default value for the
399 % key)
400 % \begin{macrocode}
401 \def\define@key#1#2{%
402 \@ifnextchar[{\KV@def{#1}{#2}}{\long\@namedef{KV@#1@#2}####1}}
403 % \end{macrocode}
404 % \end{macro}
405 %
406 % \begin{macro}{\KV@def}
407 % Make the definitions of the command, and the default value.
408 % \begin{macrocode}
409 \def\KV@def#1#2[#3]{%
410 \long\@namedef{KV@#1@#2@default\expandafter}\expandafter
411 {\csname KV@#1@#2\endcsname{#3}}%
412 \long\@namedef{KV@#1@#2}##1}
413 % \end{macrocode}
414 % \end{macro}
415 %
416 % \begin{macrocode}
417 %</package>
418 % \end{macrocode}
419 % \Finale
420 %
421 \endinput