GeneralGlobal and Individual Definition of Key Options

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

Global and Individual Definition of Key Options

Post by Lady Jamcrackers »

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.

Code: Select all

\begin{myenvironment}[myoptiona=blue, myoptionb=20pt]
or when using each instance of a certain command, i.e.

Code: Select all

\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.)

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}
Last edited by localghost on Tue Jan 28, 2014 10:47 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Post Reply