TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
possible duplicate of How to write a class that accepts an optional key-value pair? – egreg Apr 17 '12 at 16:55
up vote 24 down vote accepted

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.

share|improve this answer
An introduction to implementing key--value input, written by Joseph Wright, can be found here. – lockstep Mar 4 '11 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 Mar 6 '11 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. – Martin Scharrer Mar 6 '11 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 Mar 6 '11 at 2:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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