Accepting key-value input can be done using a number of packages, and the general approach is the same for all of them: I covered this in some detail in a TUGboat article. Essentially, there are three things you need to do
- Define one or more keys;
- Tell LaTeX to process package options using these keys;
- Provide a macro for setting keys after package loading.
In the question, you've mentioned the xkeyval package, with others including kvoptions, pgfkeys (plus pgfopts) and the LaTeX3 keys system l3keys (plus l3keys2e). I have used all of these in the past, and I would favour pgfkeys (if you do not want to use LaTeX3) or the LaTeX3 keys implementation (if you are happy using expl3). The reason is that these two have in my opinion the best overall method for defining keys. (I should add that I wrote most of the LaTeX3 keys system, and this was based initially on the pgfkeys approach.)
As the question asks for an xkeyval approach, I will sketch one out here. First, of course, you'll need to load the package.
\usepackage{xkeyval}
This also loads the parent keyval package, which provides some of the basic mechaism. To define keys, the basic macro is \define@key:
\define@key{mypkg}{optA}{<code for optA>} % 'mypkg' is the 'family' for the keys
\define@key{mypkg}{optB}{<code for optB>}
Within the code, #1 will be the value passed to the key. You can define key types with richer validation (for example Boolean keys) using the various xkeyval macros. As I say, the xkeyval approach is rather dense, and I think 'one question per key type' might be best if you want more information!
The second stage is to process the package options. To do this, in place of \ProcessOptions you use \ProcessOptionsX<mypkg>. This will work through the package options, looking for a defined key for each one and executing the code it finds.
Finally, to define a macro to use the keys after package loading, you need \setkeys:
\newcommand\mymacro[1]{\setkeys{mypkg}{#1}}
What you should notice here is that key-value package options are just keys that are defined when the \ProcessOptionsX macro is used. So it is possible to define keys only as package options, then disable them by doing \defin@key again. It's also possible to define options that are only available after package loading, by simply placing \define@key after \ProcessOptionsX.
\define@key<mypkg>{optA}{<code for optA>}and\ProcessOptionsX<mypkg>– Leo Liu Mar 12 '11 at 08:01xkeyval: I'll update that. – Joseph Wright Mar 12 '11 at 08:21\define@key, as the syntax comes from thekeyvalpackage! – Joseph Wright Mar 12 '11 at 08:22