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.

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?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
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

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.