2

Consider the following short .sty package:

%% This is file `mypackage.sty'
\NeedsTeXFormat{LaTeX2e}[1997/06/01]
\ProvidesPackage{mypackage}
\DeclareOption{myoptionone}{Do something}
\DeclareOption{myoptiontwo}{Do some other things}
\DeclareOption{myoptionthree}{Do something awesome}
\ExecuteOptions{myoptionone,myoptiontwo}
\ProcessOptions\relax
\endinput

When loaded using \usepackage{mypackage}, the options myoptionone and myoptiontwo are executed as default.

Now, I want to add a key value option foo=<value> to mypackage.sty using the kvoptions package, with the following default settings: foo=bar and maintaining the original myoptionone and myoptiontwo.

So far, I got

%% This is file `mypackage.sty'
\NeedsTeXFormat{LaTeX2e}[1997/06/01]
\ProvidesPackage{mypackage}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{
  family=hmm,
  prefix=hmm@
}
\DeclareVoidOption{myoptionone}{Do something}
\DeclareVoidOption{myoptiontwo}{Do some other things}
\DeclareVoidOption{myoptionthree}{Do something awesome}
\DeclareStringOption[bar]{foo}[bar]
%\ExecuteOptions{myoptionone,myoptiontwo}% This doesn't work.
\ProcessKeyvalOptions*
\endinput

What’s the equivalence of \ExecuteOptions when kvoptions is used? I don’t think I understand this discussion on Google Groups.

4

The package kvoptions is based on keyval, which provides \setkeys. You have to pass the name of the family and the options:

%% This is file `mypackage.sty'
\NeedsTeXFormat{LaTeX2e}[1997/06/01]
\ProvidesPackage{mypackage}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{
  family=hmm,
  prefix=hmm@
}
\DeclareVoidOption{myoptionone}{Do something}
\DeclareVoidOption{myoptiontwo}{Do some other things}
\DeclareVoidOption{myoptionthree}{Do something awesome}
\DeclareStringOption[bar]{foo}[bar]
\setkeys{hmm}{myoptionone,myoptiontwo}% This does work.
\ProcessKeyvalOptions*
\endinput

The package kvoptions also loads kvsetkeys which provides the alternative \kvsetkeys. It is not used by default with kvoptions but you can enable it with the setkeys option in \SetupKeyvalOptions:

%% This is file `mypackage.sty'
\NeedsTeXFormat{LaTeX2e}[1997/06/01]
\ProvidesPackage{mypackage}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{
  family=hmm,
  prefix=hmm@,
  setkeys=\kvsetkeys
}
\DeclareVoidOption{myoptionone}{Do something}
\DeclareVoidOption{myoptiontwo}{Do some other things}
\DeclareVoidOption{myoptionthree}{Do something awesome}
\DeclareStringOption[bar]{foo}[bar]
\kvsetkeys{hmm}{myoptionone,myoptiontwo}
\ProcessKeyvalOptions*
\endinput

All details about \kvsetkeys can be found in the kvsetkeys manual but in this context the most important advantage of \kvsetkeys is being reentrant:

This means while \kvsetkeys is executed(e.g. during execution of the code given in \DeclareVoidOption), other commands using \kvsetkeys(perhaps internally) can still be used. This gives more freedom to the code in \Declare...Option.

  • Thank you! Heiko Oberdiek, the author of kvoptions stated in this thread that \kvsetkeys is preferable over \setkeys. Would you please elaborate? – Ruixi Zhang Jul 8 '18 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.