[Tex/LaTex] In \DeclareStringOption, what is the difference between and

kvoptionspackage-optionspackage-writing

In the kvoptions package the following macro is defined:

\DeclareStringOption [⟨init⟩] {⟨key⟩} [⟨default⟩]

Following is the related documentation:

A macro is created that remembers the value of the key ⟨key⟩. The name
of the macro consists of the option name ⟨key⟩ that is prefixed by the
prefix (see 2.1.3). The initial contents of the macro can be given by
the first optional argument ⟨init⟩. The default is empty. The the
option ⟨key⟩ is defined. The option code just stores its value in the
macro. If the optional argument at the end of \DeclareStringOption is
given, then option ⟨key⟩ is defined with the default ⟨default⟩.

I fail to understand what is the difference between the default value and the init one. Can someone please explain?

Best Answer

In common with many keyval methods, the default value is the one which will be used if only the key name is given at point of use. Thus with

\DeclareStringOption{foo}[bar]

giving foo with no value/= sign in the option list is equivalent to giving foo = bar.

In contrast, the initial value is used to ensure that some value has been stored before option processing. Thus

\ProvidesPackage{mypkg}
\DeclareStringOption[baz]{foo}

will store the value baz for key foo 'now'. This is equivalent to giving

\ProvidesPackage{mypkg}
\DeclareStringOption{foo}
\newcommand*{\mypkg@foo}{baz}

If the user then sets the foo option, the initial value will be 'thrown away' and has no more influence on the outcome. In contrast, the default value is still important as in the case

\ProvidesPackage{mypkg}
\DeclareStringOption[baz]{foo}[bar]

If the user does not give the foo option, then \mypkg@foo will have value baz. If the user gives the foo option with no value, the default is used and \mypkg@foo will have value bar. Finally, if the user gives foo = <some value> then of course \mypkg@foo will contain <some value>.

Related Question