Code: Select all
\begin{myenvironment}[myoptiona=blue, myoptionb=20pt]
Code: Select all
\mycommand[myoptiona=blue]
\mycommand[myoptiona=green]
\mycommand
is implemented without the user setting myoptiona
, that key reverts to the preset value in \presetkeys{mycommand}{<preset values>}
instead of keeping the value set globally by the user at the beginning of the environment. However, I want the second bit of code to work exactly the way it does, so there need to be preset values of some sort. What is the proper way to fix this? (I don't want an awkward workaround.)Code: Select all
\documentclass{minimal}
\usepackage{tikz}
\usepackage{color}
\usepackage{xkeyval}
\makeatletter
\define@key{myenvironment}{myoptiona}{\def\myoptionadef{#1}}
\define@key{myenvironment}{myoptionb}{\def\myoptionbdef{#1}}
\define@key{mycommand}{myoptiona}{\def\myoptionadef{#1}}
\savekeys{myenvironment}{myoptiona, myoptionb}
\savekeys{mycommand}{myoption}
\presetkeys{myenvironment}{myoptiona=red, myoptionb=10pt}{}
\presetkeys{mycommand}{myoptiona=red}{}
\makeatother
\newenvironment{myenvironment}[1][]{%
\begin{tikzpicture}
\setkeys{myenvironment}{#1}
\draw (2,0) coordinate circle (\myoptionbdef);
}{\end{tikzpicture}}
\newcommand{\mycommand}[1][]{%
\setkeys{mycommand}{#1}
\draw[color=\myoptionadef] (0,0) -- (1,0);
}
\begin{document}
This example produces a red line, but I want it to produce a blue line:
\begin{myenvironment}[myoptiona=blue, myoptionb=20pt] % myoptiona has been globally set to blue.
\mycommand % myoptiona gets set back to the preset value red because no key value was set by user.
\end{myenvironment}
This example works the way I want it to:
\begin{myenvironment}[myoptionb=20pt] % I want the user to be able to omit setting myoptiona.
\mycommand
\end{myenvironment}
\end{document}