I am trying to use the xkeyval package to include key arguments in a new environment. It seems to work except for default values. Here's my best try so far:
\documentclass{article}
\usepackage{xkeyval}
\makeatletter
\define@key{Mystuff}{foo}[what]{\def\foo{#1}}
\define@key{Mystuff}{bar}[whatever]{\def\bar{#1}}
\savekeys{Mystuff}{foo,bar}
\makeatother
\newenvironment{myenv}[1][]%
{\setkeys{Mystuff}{#1}BEGIN \foo}
{END \bar}
\begin{document}
\begin{myenv}[bar=BAAAR]
Uh?
\end{myenv}
\end{document}
But this will cause LaTeX to complain about
\foo being undefined:
! Undefined control sequence.
\\myenv [#1]->\setkeys {Mystuff}{#1}BEGIN \foo
l.18 \begin{myenv}[bar=BAAAR]
And the output will be something like "BEGIN Uh? END BAAAR" (without the default "what" value to \foo).
What am I doing wrong there, and how can I change this example so the default value for \foo will be used?
xkeyvalskeys are evaluated by\setkeyseven if they are not specified explicitly in the key list if you preset them with\presetkeys. – Ulrike Fischer Jun 09 '12 at 12:49\presetkeys, and if I want to be able to specify the defaults in each\newcommandor\newenvironment, I would use\setkeyswith them in the new defined command, as @egreg showed. Sounds really good (very flexible)! :-) – Jay Jun 09 '12 at 15:06\setkeys{Mystuff}{foo=what,bar=whatever,#1}will most likely result in setting the same keys twice: once by the hardwiredfoo=what,bar=whateverand again by#1.\presetkeyswill not set from the preset list the keys whose values have been supplied by the user (in#1in the above example). – Ahmed Musa Jun 09 '12 at 20:43\setkeys{Mystuff}{foo=what,bar=whatever,#1}still works if I want these defaults for only one command or environment (and not for all that use the Mystuff family), right? – Jay Jun 10 '12 at 00:39\presetkeys, since your keys may eventually grow in number. There will be no point setting them twice needlessly. There are facilities in ltxkeys package that will automatically set up the defaults for you and the syntaxes there look like those of xkeyval package. For example, look at\ltxkeys@declarekeys. – Ahmed Musa Jun 10 '12 at 17:25