3 %% keyval.dtx Copyright (C) 1993 1994 1995 1997 1998 1999 David Carlisle
4 %% Copyright (C) 2000--2015 David Carlisle, LaTeX3 Project
6 %% This file is part of the Standard LaTeX `Graphics Bundle'.
7 %% It may be distributed under the terms of the LaTeX Project Public
8 %% License, as described in lppl.txt in the base LaTeX distribution.
9 %% Either version 1.3c or, at your option, any later version.
13 \ProvidesFile{keyval.dtx}
16 %<package&!plain>\NeedsTeXFormat{LaTeX2e}
17 %<package&!plain>\ProvidesPackage{keyval}
18 %<driver> \ProvidesFile{keyval.drv}
20 % \ProvidesFile{keyval.dtx}
21 [2014/10/28 v1.15 key=value parser (DPC)]
26 \documentclass{ltxdoc}
34 % \GetFileInfo{keyval.dtx}
35 % \title{The \textsf{keyval} package\thanks{This file
36 % has version number \fileversion, last
37 % revised \filedate.}}
38 % \author{David Carlisle}
40 % \MaintainedByLaTeXTeam{graphics}
44 % A \LaTeX\ package implementing a system allowing the setting
45 % of parameters (or `named arguments' with a
46 % \meta{key}${}={}$\meta{value} syntax.
48 % Eg: |\foo[height=3in, shadow = true ]{bar}|
54 % {1993/10/13}{Initial version}
55 % \changes{v1.04}{1993/11/15}
56 % {Upgrade after comments from Timothy van Zandt}
57 % \changes{v1.05}{1993/11/17}
58 % {Further small improvements}
59 % \changes{v1.06}{1994/02/01}
61 % \changes{v1.07}{1994/03/15}
63 % \changes{v1.08}{1994/09/12}
64 % {Improve docstrip handling}
65 % \changes{v1.09}{1995/09/21}
66 % {Move a comma for graphics/1698}
67 % \changes{v1.14}{2014/04/25}
68 % {Make most definitions \cs{long} to allow \cs{par} in values: graphics/3446}
69 % \changes{v1.14}{2014/04/25}
70 % {Only strip a single brace pair from values: graphics/3446}
71 % \changes{v1.15}{2014/05/08}
72 % {revert this change: Only strip a single brace pair from values: graphics/3446}
75 % This package implements a system of defining and using sets of
76 % parameters, which are set using the syntax \meta{key}=\meta{value}.
78 % For each keyword in such a set, there exists a function which is
79 % called whenever the parameter appears in a parameter list. For
80 % instance if the set |dpc| is to have the keyword |scale| then I
82 % | \define@key{dpc}{scale}{scale ({\tt\string#1})\\}|\\
83 % The first argument of |\define@key| is the set of keywords being
84 % used, the second is the keyword, and the third is the function to
85 % call. This function will be given as |#1| the \meta{value} specified
88 % Normally it is an error to omit the `=\meta{value}' however if an
89 % optional \meta{value} is supplied when the keyword is defined, then
90 % just the keyword need be supplied.\\
91 % |\define@key{dpc}{clip}[true]{...}|\\
92 % For `|clip|' you can go `|clip = true|' or `|clip = false|' or
93 % just `|clip|', which is the same as `|clip = true|'
95 % To use these keywords, just call `|\setkeys|' with a comma
96 % separated list of settings, each of the form
97 % \meta{key}=\meta{value}, or just \meta{key}. Any white space around
98 % the `|=|' and `|,|' is ignored.
100 % As the \meta{key} is passed as a macro argument, if it consists
101 % entirely of a |{ }| group, the outer braces are stripped off. Thus
102 % |,key=foo,| and |,key={foo},| are equivalent. This fact enables one to
103 % `hide' any commas or equals signs that must appear in the value. i.e.\
104 % in |foo={1,2,3},bar=4|, |foo| gets the value |1,2,3|, the comma after
105 % |1| does not terminate the keyval pair, as it is `hidden' by the
108 % Empty entries, with nothing between the commas, are silently ignored.
109 % This means that it is not an error to have a comma after the last
110 % term, or before the first.
114 % We may extend the examples above to give a `fake' graphics
115 % inclusion macro, with a syntax similar to that used in the psfig
119 % \def\dpcgraphics{\@ifnextchar[\@dpcgraphics{\@dpcgraphics[]}}
121 % |\dpcgraphics| has one optional argument which is passed through
122 % |\setkeys|, and one mandatory argument, the filename. It actually
123 % just typesets its arguments, for demonstration.
125 % \def\@dpcgraphics[#1]#2{{\setkeys{dpc}{#1}INPUT: #2}}%
127 % \define@key{dpc}{scale}{scale ({\tt\string#1\relax})\\}
128 % \define@key{dpc}{height}{height ({\tt#1})\\}
129 % \define@key{dpc}{width}{width ({\tt#1})\\}
130 % \define@key{dpc}{bb}{bounding box ({\tt#1})\\}
131 % \define@key{dpc}{clip}[true]{clip ({\tt\string#1\relax})\\}
134 % The declared keys are: |scale|, |height|, |width|, |bb|,
135 % and |clip|. Except for the last, they must all be given a value if
138 % Note how in the following, any white space around |=| or |,| is
139 % ignored, as are the `empty' arguments caused by extra commas. Note
140 % also that each macro receives \emph{exactly} the tokens that you
141 % specify as arguments, no premature expansion is done.
144 % \def\dpcgraphics{\@ifnextchar[\@dpcgraphics{\@dpcgraphics[]}}
145 % \def\@dpcgraphics[#1]#2{{\setkeys{dpc}{#1}INPUT: #2}}
147 % \define@key{dpc}{scale}{scale ({\tt\string#1\relax})\\}
148 % \define@key{dpc}{clip}[true]{clip ({\tt\string#1\relax})\\}
151 % \begin{minipage}{.4\textwidth}
157 % scale = \scalemacro,
158 % bb = 20 20 300 400 ,
164 % \begin{minipage}{.4\textwidth}
169 % scale = \scalemacro ,
170 % bb = 20 20 300 400 ,
176 % \section{The Internal Interface}
177 % A declaration of the form:\\
178 % |\define@key{family}{key}{...}|\\
179 % Defines a macro |\KV@prefix@key| with one argument. When used in a
180 % keyval list, the macro receives the value as its argument.
182 % A declaration of the form:\\
183 % |\define@key{family}{key}[default]{...}|\\
184 % Defines a macro |\KV@family@key| as above, however it also defines the
185 % macro |\KV@family@key@default| as a macro with no arguments, and
187 % |\KV@family@key{default}|.
189 % Thus if macros are defined using |\define@key|, the use of a key with
190 % no value \ldots|,foo,|\ldots\ is always equivalent to the use of the
191 % key with some value, \ldots|,foo=default,|\ldots. However a package
192 % writer may wish that the `default' behaviour for some key is not
193 % directly equivalent to using that key with a value. (In particular, as
194 % pointed out to me by Timothy Van Zandt, you may wish to omit error
195 % checking on the default value as you know it is correct.) In these
196 % cases one simply needs to define the two macros
197 % |\KV@|\meta{family}|@key| and |\KV@|\meta{family}|@key@default|
198 % directly using |\def| (or |\newcommand|). I do not supply a user
199 % interface for this type of definition, but it is supported in the
200 % sense that I will try to ensure that any future upgrades of this
201 % package do not break styles making use of these `low level'
206 % \section{The Macros}
208 % From version~1.05, all `internal' macros associated to keys have names
210 % |\KV@|\meta{family}|@|\meta{key} or
211 % |\KV@|\meta{family}|@|\meta{key}|@|\meta{default}
217 % \begin{macro}{\setkeys}
218 % The top level macro. |#2| should be a comma separated values of the
219 % form \meta{key} |=| \meta{value} or just simply \meta{key}.
220 % The macro associated with this key in the `family' |#1| is called with
221 % argument \meta{value}. The second form is only allowed if the key was
222 % declared with a default value.
224 \long\def\setkeys#1#2{%
226 % Save the `family' for later. Then begin acting on the comma
228 % \changes{v1.11}{1998/06/05}
229 % {Make \cs{@tempc} safe (in case it is an \cs{if}.}
231 \def\KV@prefix{KV@#1@}%
237 % \begin{macro}{\KV@do}
238 % Iterate down the list of comma separated argument pairs.
239 % \changes{v1.14}{2014/04/25}
240 % {Add \cs{@empty} not \cs{empty}}
243 \ifx\relax#1\@empty\else
245 \expandafter\KV@do\fi}
249 % \begin{macro}{\KV@split}
250 % Split up the keyword and value, and call the appropriate command.
251 % This macro was slightly reorganised for version 1.04, after some
252 % suggestions from Timothy Van Zandt.
254 \long\def\KV@split#1=#2=#3\relax{%
255 \KV@@sp@def\@tempa{#1}%
256 \ifx\@tempa\@empty\else
257 \expandafter\let\expandafter\@tempc
258 \csname\KV@prefix\@tempa\endcsname
262 {\@tempa\space undefined}%
267 \KV@@sp@def\@tempb{#2}%
268 \expandafter\@tempc\expandafter{\@tempb}\relax
275 % \begin{macro}{\KV@default}
276 % Run the default code, or raise an error.
279 \expandafter\let\expandafter\@tempb
280 \csname\KV@prefix\@tempa @default\endcsname
282 \KV@err{No value specified for \@tempa}%
289 % \begin{macro}{\KV@err}
290 % \changes{v1.10}{1997/11/10}
291 % {Use \cs{PackageError}}
292 % \changes{v1.12}{1998/11/18}
293 % {Option system added}
296 %<plain>\def\KV@err#1{\errmessage{key-val: #1}}
298 \DeclareOption{unknownkeysallowed}{%
299 \def\KV@errx#1{\PackageInfo{keyval}{#1}}}
300 \DeclareOption{unknownkeyserror}{%
301 \def\KV@errx#1{\PackageError{keyval}{#1}\@ehc}}
302 \ExecuteOptions{unknownkeyserror}
309 % \begin{macro}{\KV@@sp@def}
310 % \changes{v1.10}{1997/11/10}
311 % {Reorganise to not require doubled hash tokens, and to not
312 % lose initial brace groups.}
313 % \begin{macro}{\KV@@sp@b}
314 % \begin{macro}{\KV@@sp@c}
315 % \begin{macro}{\KV@@sp@d}
316 % |\KV@@sp@def|\meta{cmd}\meta{token list} is like |\def|, except that
317 % a space token at the beginning or end of \meta{token list} is
318 % removed before making the assignment. \meta{token list} may not
319 % contain the token |\@nil|, unless it is within a brace group.
320 % The names of these commands were changed at version~1.05 to ensure
321 % that they do not clash with `internal' macros in a key family `sp'.
323 % Since v1.10, |#| may appear in the second argument without it
324 % needing to be doubled as |##|. Also earlier versions would
325 % drop any initial brace group, so |{abc}d| would incorrectly
326 % be treated as |abcd|. The current version only removes brace
327 % groups that surround the entire value, so |{abcd}| \emph{is} treated
328 % correctly as |abcd|. Prior to v1.14, two levels of bracing are removed, if
329 % you require the entire argument to be a single brace group, you had
330 % use |{{{abcd}}}|, from v1.14 exactly one brace group is removed, so to make
331 % the entire value be a brace group you need |{{abc}}|.
338 \long\def\KV@@sp@def##1##2{%
339 \futurelet\KV@tempa\KV@@sp@d##2\@nil\@nil#1\@nil\relax##1}%
342 % Early release removed initial space by having an `extra' argument
343 % in |\KV@@sp@b| but that removed too many braces, so now make
344 % |\KV@@sp@b| explicitly remove a single space token. That unfortunately
345 % means we need the new |\KV@@sp@d| command to add a space token if
346 % one was not there before.
349 \ifx\KV@tempa\@sptoken
350 \expandafter\KV@@sp@b
352 \expandafter\KV@@sp@b\expandafter#1%
356 % \changes{v1.14}{2014/04/25}
357 % {Add \cs{@empty} to avoid dropping a brace pair: graphics/3446}
358 % \changes{v1.15}{2014/05/08}
359 % {revert last change}
361 \long\def\KV@@sp@b#1##1 \@nil{\KV@@sp@c##1}%
365 %<plain>\def\@sptoken{#1}%
368 % Make the above definitions, inserting the space token where needed.
374 % \changes{v1.14}{2014/04/25}
375 % {Add \cs{expandafter} to remove the extra \cs{@empty} token: graphics/3446}
376 % \changes{v1.15}{2014/05/08}
377 % {revert last change}
379 \long\def\KV@@sp@c#1\@nil#2\relax#3{\KV@toks@{#1}\edef#3{\the\KV@toks@}}
386 % \begin{macro}{\KV@toks@}
387 % Macro register used above to prevent |#| doubling.
388 % Avoid uding one of the normal scratch registers, as this code
389 % is not in a local group.
395 % \begin{macro}{\define@key}
396 % Define the command associated to the key |#2| in the family |#1|.
397 % First looks for a default argument (the default value for the
400 \def\define@key#1#2{%
401 \@ifnextchar[{\KV@def{#1}{#2}}{\long\@namedef{KV@#1@#2}####1}}
405 % \begin{macro}{\KV@def}
406 % Make the definitions of the command, and the default value.
408 \def\KV@def#1#2[#3]{%
409 \long\@namedef{KV@#1@#2@default\expandafter}\expandafter
410 {\csname KV@#1@#2\endcsname{#3}}%
411 \long\@namedef{KV@#1@#2}##1}