I am writing a package that has a bunch of values that are going to be the same most of the time, but that I might want to change in select cases. I want to do this by giving this package an optional argument, the way geometry does it:


How would I go about doing this?


1 Answer 1


The kvoptions package gives you the possibility to define key=value style options. It "connects package keyval with LATEX's package and class options" (quote from the manual).

You can then declare options like margin using:

\DeclareStringOption [<init>]{<key>}[<default>]

were <init> is the initial value (also used when the option is not used), <key> stands for the option name and <default> for the default value used when the option is used without an value (foobar instead of foobar=<value>). If no default value is given the option requires a value, which makes sense for an option like margin for example.

The pgfopts package is an alternative and uses the pgfkeys package, i.e. the key-value engine of PGF/TikZ, which is IMHO more powerful and flexible than keyval or xkeyval.

Please note that keys with spaces are not supported as package or class options because the internal LaTeX macros remove all spaces from the options before the above package receive them.

  • 10
    An introduction to implementing key--value input, written by Joseph Wright, can be found here.
    – lockstep
    Commented Mar 4, 2011 at 14:50
  • ... keys with spaces are not supported as package or class options. Really? Load kvoptions-patch, xkvltxp or catoptions before \documentclass.
    – Ahmed Musa
    Commented Mar 6, 2011 at 0:11
  • @Ahmed: Thanks for mentioning these packages. But how can you load these packages before \documentclass inside a package which is loaded afterwards? The question is about programming your own package with kv options. Commented Mar 6, 2011 at 0:20
  • Thanks for the explanation, but I thought he might consider loading his package before \documentclass, something that is indeed possible. Put forget it: maybe he doesn't really need that functionality. But he might also want to pass options with expandable values (eg, myoption=Mr J\"avier).
    – Ahmed Musa
    Commented Mar 6, 2011 at 2:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .