## LaTeX forum ⇒ General ⇒ Global and Individual Definition of Key Options

LaTeX specific issues not fitting into one of the other forums of this category.
Posts: 3
Joined: Wed Aug 28, 2013 11:16 pm

### Global and Individual Definition of Key Options

I am using xkeyval to write a TikZ-like environment and set of commands to be used in that environment. I would like the user to be able to set an option globally, i.e.
\begin{myenvironment}[myoptiona=blue, myoptionb=20pt]

or when using each instance of a certain command, i.e.
\mycommand[myoptiona=blue]\mycommand[myoptiona=green]

In the MWE below, I ideally want the first bit of code to produce a blue line, but it in fact produces a red line instead. The problem is that when \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.)

\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}
Last edited by localghost on Tue Jan 28, 2014 10:47 am, edited 1 time in total.
Reason: Source code has to be tagged as such to keep a post clear and legible (see Board Rules).

Tags: