6

xkeyval provides \DeclareOptionX which effectively defines a key like \define@key does but that can be used as a package option.

How can I \DeclareOptionXs that behave like \define@choicekey or \define@boolkey would?

I've done the following, but it isn't ideal.

\documentclass{article}
\usepackage{xkeyval}
\newcounter{testcounter}
\usepackage{filecontents}
\begin{filecontents}{testpackage.sty}
\define@boolkey{testpackage}{ffoo}{}
\DeclareOptionX{foo}{\setkeys{testpackage}{ffoo=#1}}
\ProcessOptionsX
\ifKV@testpackage@ffoo
\def\foo{Foo}
\else
\def\foo{Fail}
\fi
\end{filecontents}
\usepackage[foo=true]{testpackage}
\begin{document}
\foo
\end{document}

This isn't great since the error doesn't refer to the key it should refer to, that is, to the key that the user sets.

Is there some obvious way of doing this that I am missing?

Seamus
  • 73,242

1 Answers1

6

You do not have to use \DeclareOptionX to set up options using xkeyval. As with any other keyval package for LaTeX, anything which is set up as a key will work as an option for the appropriate family. Thus you can use \define@choicekey or \define@boolkey directly:

\define@choicekey*{fam}{key}{val1,Val2,VAL3}{You chose: #1}
\ProcessOptionsX<fam>

makes key available as a choice key when loading the package or class the code is contained in.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 1
    Huh. I was fairly sure I tried exactly this and it didn't work! It does work now. I wonder what I was doing wrong... [Also, isn't it \DeclareOptionX not \DefineOptionX?] – Seamus Jul 18 '11 at 14:46