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}|
53 % {1993/10/13}{Initial version}
54 % \changes{v1.04}{1993/11/15}
55 % {Upgrade after comments from Timothy van Zandt}
56 % \changes{v1.05}{1993/11/17}
57 % {Further small improvements}
58 % \changes{v1.06}{1994/02/01}
60 % \changes{v1.07}{1994/03/15}
62 % \changes{v1.08}{1994/09/12}
63 % {Improve docstrip handling}
64 % \changes{v1.09}{1995/09/21}
65 % {Move a comma for graphics/1698}
66 % \changes{v1.14}{2014/04/25}
67 % {Make most definitions \cs{long} to allow \cs{par} in values: graphics/3446}
68 % \changes{v1.14}{2014/04/25}
69 % {Only strip a single brace pair from values: graphics/3446}
70 % \changes{v1.15}{2014/05/08}
71 % {revert this change: Only strip a single brace pair from values: graphics/3446}
74 % This package implements a system of defining and using sets of
75 % parameters, which are set using the syntax \meta{key}=\meta{value}.
77 % For each keyword in such a set, there exists a function which is
78 % called whenever the parameter appears in a parameter list. For
79 % instance if the set |dpc| is to have the keyword |scale| then I
81 % | \define@key{dpc}{scale}{scale ({\tt\string#1})\\}|\\
82 % The first argument of |\define@key| is the set of keywords being
83 % used, the second is the keyword, and the third is the function to
84 % call. This function will be given as |#1| the \meta{value} specified
87 % Normally it is an error to omit the `=\meta{value}' however if an
88 % optional \meta{value} is supplied when the keyword is defined, then
89 % just the keyword need be supplied.\\
90 % |\define@key{dpc}{clip}[true]{...}|\\
91 % For `|clip|' you can go `|clip = true|' or `|clip = false|' or
92 % just `|clip|', which is the same as `|clip = true|'
94 % To use these keywords, just call `|\setkeys|' with a comma
95 % separated list of settings, each of the form
96 % \meta{key}=\meta{value}, or just \meta{key}. Any white space around
97 % the `|=|' and `|,|' is ignored.
99 % As the \meta{key} is passed as a macro argument, if it consists
100 % entirely of a |{ }| group, the outer braces are stripped off. Thus
101 % |,key=foo,| and |,key={foo},| are equivalent. This fact enables one to
102 % `hide' any commas or equals signs that must appear in the value. i.e.\
103 % in |foo={1,2,3},bar=4|, |foo| gets the value |1,2,3|, the comma after
104 % |1| does not terminate the keyval pair, as it is `hidden' by the
107 % Empty entries, with nothing between the commas, are silently ignored.
108 % This means that it is not an error to have a comma after the last
109 % term, or before the first.
113 % We may extend the examples above to give a `fake' graphics
114 % inclusion macro, with a syntax similar to that used in the psfig
118 % \def\dpcgraphics{\@ifnextchar[\@dpcgraphics{\@dpcgraphics[]}}
120 % |\dpcgraphics| has one optional argument which is passed through
121 % |\setkeys|, and one mandatory argument, the filename. It actually
122 % just typesets its arguments, for demonstration.
124 % \def\@dpcgraphics[#1]#2{{\setkeys{dpc}{#1}INPUT: #2}}%
126 % \define@key{dpc}{scale}{scale ({\tt\string#1\relax})\\}
127 % \define@key{dpc}{height}{height ({\tt#1})\\}
128 % \define@key{dpc}{width}{width ({\tt#1})\\}
129 % \define@key{dpc}{bb}{bounding box ({\tt#1})\\}
130 % \define@key{dpc}{clip}[true]{clip ({\tt\string#1\relax})\\}
133 % The declared keys are: |scale|, |height|, |width|, |bb|,
134 % and |clip|. Except for the last, they must all be given a value if
137 % Note how in the following, any white space around |=| or |,| is
138 % ignored, as are the `empty' arguments caused by extra commas. Note
139 % also that each macro receives \emph{exactly} the tokens that you
140 % specify as arguments, no premature expansion is done.
143 % \def\dpcgraphics{\@ifnextchar[\@dpcgraphics{\@dpcgraphics[]}}
144 % \def\@dpcgraphics[#1]#2{{\setkeys{dpc}{#1}INPUT: #2}}
146 % \define@key{dpc}{scale}{scale ({\tt\string#1\relax})\\}
147 % \define@key{dpc}{clip}[true]{clip ({\tt\string#1\relax})\\}
150 % \begin{minipage}{.4\textwidth}
156 % scale = \scalemacro,
157 % bb = 20 20 300 400 ,
163 % \begin{minipage}{.4\textwidth}
168 % scale = \scalemacro ,
169 % bb = 20 20 300 400 ,
175 % \section{The Internal Interface}
176 % A declaration of the form:\\
177 % |\define@key{family}{key}{...}|\\
178 % Defines a macro |\KV@prefix@key| with one argument. When used in a
179 % keyval list, the macro receives the value as its argument.
181 % A declaration of the form:\\
182 % |\define@key{family}{key}[default]{...}|\\
183 % Defines a macro |\KV@family@key| as above, however it also defines the
184 % macro |\KV@family@key@default| as a macro with no arguments, and
186 % |\KV@family@key{default}|.
188 % Thus if macros are defined using |\define@key|, the use of a key with
189 % no value \ldots|,foo,|\ldots\ is always equivalent to the use of the
190 % key with some value, \ldots|,foo=default,|\ldots. However a package
191 % writer may wish that the `default' behaviour for some key is not
192 % directly equivalent to using that key with a value. (In particular, as
193 % pointed out to me by Timothy Van Zandt, you may wish to omit error
194 % checking on the default value as you know it is correct.) In these
195 % cases one simply needs to define the two macros
196 % |\KV@|\meta{family}|@key| and |\KV@|\meta{family}|@key@default|
197 % directly using |\def| (or |\newcommand|). I do not supply a user
198 % interface for this type of definition, but it is supported in the
199 % sense that I will try to ensure that any future upgrades of this
200 % package do not break styles making use of these `low level'
205 % \section{The Macros}
207 % From version~1.05, all `internal' macros associated to keys have names
209 % |\KV@|\meta{family}|@|\meta{key} or
210 % |\KV@|\meta{family}|@|\meta{key}|@|\meta{default}
216 % \begin{macro}{\setkeys}
217 % The top level macro. |#2| should be a comma separated values of the
218 % form \meta{key} |=| \meta{value} or just simply \meta{key}.
219 % The macro associated with this key in the `family' |#1| is called with
220 % argument \meta{value}. The second form is only allowed if the key was
221 % declared with a default value.
223 \long\def\setkeys#1#2{%
225 % Save the `family' for later. Then begin acting on the comma
227 % \changes{v1.11}{1998/06/05}
228 % {Make \cs{@tempc} safe (in case it is an \cs{if}.}
230 \def\KV@prefix{KV@#1@}%
236 % \begin{macro}{\KV@do}
237 % Iterate down the list of comma separated argument pairs.
238 % \changes{v1.14}{2014/04/25}
239 % {Add \cs{@empty} not \cs{empty}}
242 \ifx\relax#1\@empty\else
244 \expandafter\KV@do\fi}
248 % \begin{macro}{\KV@split}
249 % Split up the keyword and value, and call the appropriate command.
250 % This macro was slightly reorganised for version 1.04, after some
251 % suggestions from Timothy Van Zandt.
253 \long\def\KV@split#1=#2=#3\relax{%
254 \KV@@sp@def\@tempa{#1}%
255 \ifx\@tempa\@empty\else
256 \expandafter\let\expandafter\@tempc
257 \csname\KV@prefix\@tempa\endcsname
261 {\@tempa\space undefined}%
266 \KV@@sp@def\@tempb{#2}%
267 \expandafter\@tempc\expandafter{\@tempb}\relax
274 % \begin{macro}{\KV@default}
275 % Run the default code, or raise an error.
278 \expandafter\let\expandafter\@tempb
279 \csname\KV@prefix\@tempa @default\endcsname
281 \KV@err{No value specified for \@tempa}%
288 % \begin{macro}{\KV@err}
289 % \changes{v1.10}{1997/11/10}
290 % {Use \cs{PackageError}}
291 % \changes{v1.12}{1998/11/18}
292 % {Option system added}
295 %<plain>\def\KV@err#1{\errmessage{key-val: #1}}
297 \DeclareOption{unknownkeysallowed}{%
298 \def\KV@errx#1{\PackageInfo{keyval}{#1}}}
299 \DeclareOption{unknownkeyserror}{%
300 \def\KV@errx#1{\PackageError{keyval}{#1}\@ehc}}
301 \ExecuteOptions{unknownkeyserror}
308 % \begin{macro}{\KV@@sp@def}
309 % \changes{v1.10}{1997/11/10}
310 % {Reorganise to not require doubled hash tokens, and to not
311 % lose initial brace groups.}
312 % \begin{macro}{\KV@@sp@b}
313 % \begin{macro}{\KV@@sp@c}
314 % \begin{macro}{\KV@@sp@d}
315 % |\KV@@sp@def|\meta{cmd}\meta{token list} is like |\def|, except that
316 % a space token at the beginning or end of \meta{token list} is
317 % removed before making the assignment. \meta{token list} may not
318 % contain the token |\@nil|, unless it is within a brace group.
319 % The names of these commands were changed at version~1.05 to ensure
320 % that they do not clash with `internal' macros in a key family `sp'.
322 % Since v1.10, |#| may appear in the second argument without it
323 % needing to be doubled as |##|. Also earlier versions would
324 % drop any initial brace group, so |{abc}d| would incorrectly
325 % be treated as |abcd|. The current version only removes brace
326 % groups that surround the entire value, so |{abcd}| \emph{is} treated
327 % correctly as |abcd|. Prior to v1.14, two levels of bracing are removed, if
328 % you require the entire argument to be a single brace group, you had
329 % use |{{{abcd}}}|, from v1.14 exactly one brace group is removed, so to make
330 % the entire value be a brace group you need |{{abc}}|.
337 \long\def\KV@@sp@def##1##2{%
338 \futurelet\KV@tempa\KV@@sp@d##2\@nil\@nil#1\@nil\relax##1}%
341 % Early release removed initial space by having an `extra' argument
342 % in |\KV@@sp@b| but that removed too many braces, so now make
343 % |\KV@@sp@b| explicitly remove a single space token. That unfortunately
344 % means we need the new |\KV@@sp@d| command to add a space token if
345 % one was not there before.
348 \ifx\KV@tempa\@sptoken
349 \expandafter\KV@@sp@b
351 \expandafter\KV@@sp@b\expandafter#1%
355 % \changes{v1.14}{2014/04/25}
356 % {Add \cs{@empty} to avoid dropping a brace pair: graphics/3446}
357 % \changes{v1.15}{2014/05/08}
358 % {revert last change}
360 \long\def\KV@@sp@b#1##1 \@nil{\KV@@sp@c##1}%
364 %<plain>\def\@sptoken{#1}%
367 % Make the above definitions, inserting the space token where needed.
373 % \changes{v1.14}{2014/04/25}
374 % {Add \cs{expandafter} to remove the extra \cs{@empty} token: graphics/3446}
375 % \changes{v1.15}{2014/05/08}
376 % {revert last change}
378 \long\def\KV@@sp@c#1\@nil#2\relax#3{\KV@toks@{#1}\edef#3{\the\KV@toks@}}
385 % \begin{macro}{\KV@toks@}
386 % Macro register used above to prevent |#| doubling.
387 % Avoid uding one of the normal scratch registers, as this code
388 % is not in a local group.
394 % \begin{macro}{\define@key}
395 % Define the command associated to the key |#2| in the family |#1|.
396 % First looks for a default argument (the default value for the
399 \def\define@key#1#2{%
400 \@ifnextchar[{\KV@def{#1}{#2}}{\long\@namedef{KV@#1@#2}####1}}
404 % \begin{macro}{\KV@def}
405 % Make the definitions of the command, and the default value.
407 \def\KV@def#1#2[#3]{%
408 \long\@namedef{KV@#1@#2@default\expandafter}\expandafter
409 {\csname KV@#1@#2\endcsname{#3}}%
410 \long\@namedef{KV@#1@#2}##1}