Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I know I can declare and execute an option for my package using

\DeclareOption{myoption}{\typeout{I did it!}}
\ProcessOptions

but now I'd like to give the option a value:

\DeclareOption{type}{\typeout{You typed: \CurrentOptionValue}}

which would be used as follows:

\usepackage[type="hello, world!"]{mypackage}

Requirements on the value string is that it can contain at least some nonalphanumeric characters, like spaces, commas and dots (and maybe some others...).

share|improve this question
    
Do you mean just any option or specifically a key-value one? –  Joseph Wright Nov 12 '10 at 15:11

2 Answers 2

up vote 14 down vote accepted

For handling key-value input as package options, there are a few choices:

  1. xkeyval
  2. kvoptions
  3. pgfopts

Of these, kvoptions is probably the most robust approach. The way that options are declared is to use \define@key (or \pgfkeys in the case of pgfopts) before processing the package options. With kvoptions you get some nicer wrappers, such as \DeclareBoolOption.

However, the LaTeX2e kernel does various bits of processing before the input gets anywhere near the key-value processor. As a result, I'd strongly advice considering using a post-loading macro to set keys rather than a load-time set of options. This is easy enough to implement:

\def\mysetupmacro#1{\setkeys{mypackage}{#1}}

which will then only need a basic key-value package: probably I would choose kvsetkeys, which provides \kvsetkeys as a more robust version of \setkeys.

share|improve this answer
    
Forgot about pgfopts and didn't know about kvoptions. Thanks for those. –  Matthew Leingang Nov 12 '10 at 16:08
    
"However, the LaTeX2e kernel does various bits of processing before..." In particular, the kernel gobbles punctuation, causing arguments like type="hello, world!" to fail. Not all is lost. Should you really need to pass spaces via package options, you can escape them like this: type="hello,\ world!" However, other punctuation can't be escaped (your ! will lead to trouble). Joseph is spot-on about this. PS, xkeyval more or less supersets kvoptions, is feature-rich, and is certainly equally robust. pgfopts exposes pgfkeys for package writers (for which, see the PGF\TikZ manual). –  Geoffrey Jones Nov 13 '10 at 0:18

The xkeyval package is your friend.

share|improve this answer

Your Answer

 
discard

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.