How to define key values for a newcommand function?
You do not need keyval if you have the (IMHO more powerful) pgf keys at your disposal. You can just define your own directory and store all the keys there.
\documentclass{standalone}
\usepackage{tikz}
% EE/.cd serves two purposes: define a directory EE and switch to it
% h/.initial=1 just means we introduce a key "h" and assign it the
% initial value 1
\tikzset{EE/.cd,h/.initial=1}
\newcommand{\circleDraw}[1]{%
\begin{tikzpicture}
% this says switch to the EE directory and put the argument, #1, there
% #1 can contain arbitrarily many keys (here we only have one, but in principle it can)
\tikzset{EE/.cd,#1}
% \pgfkeysvalueof{/tikz/EE/h} is the value of h in the /tikz/EE/ directory
% the non-deprecated syntax for the circle path construction is
% \draw circle[radius=<value>];
\draw (0,0) circle [radius={0.5*\pgfkeysvalueof{/tikz/EE/h}*1pt}];
\end{tikzpicture}%
}
\begin{document}
\centering
\circleDraw{h=40}
\end{document}
Of course,
\documentclass{standalone}
\usepackage{tikz}
\tikzset{EE/.cd,h/.initial=1}
\newcommand{\circleDraw}[1]{%
\begin{tikzpicture}
\tikzset{EE/.cd,#1}
\draw (0,0) circle [radius={0.5*{\pgfkeysvalueof{/tikz/EE/h}}[0]*1pt}];
\end{tikzpicture}%
}
\begin{document}
\centering
\circleDraw{h=40}
\end{document}
also works, but at this point you have a list of only one element.
ADDENDUM: Here is a more complex example with more than one key.
\documentclass{article}
\usepackage{tikz}
% EE/.cd serves two purposes: define a directory EE and switch to it
% h/.initial=1 just means we introduce a key "h" and assign it the
% initial value 1, the other keys are analogous
\tikzset{EE/.cd,h/.initial=10,r/.initial=5,color 1/.initial=black,color 2/.initial=black,}
\newcommand{\circleDraw}[1]{%
\begin{tikzpicture}
% this says switch to the EE directory and put the argument, #1, there
% #1 can contain arbitrarily many keys
\tikzset{EE/.cd,#1}%
% it is often more convenient to have a shortcut for \pgfkeysvalueof{...}
% (of course, this macro is *local*)
\def\pv##1{\pgfkeysvalueof{/tikz/EE/##1}}%
% \pgfkeysvalueof{/tikz/EE/h} is the value of h in the /tikz/EE/ directory
% the non-deprecated syntax for the circle path construction is
% \draw circle[radius=<value>];
\draw[color=\pv{color 1}] (0,0) circle [radius={0.5*\pv{h}*1pt}];
\draw[color=\pv{color 2}] (0,0) circle [radius={0.5*\pv{r}*1pt}];
\end{tikzpicture}%
}
\begin{document}
\subsection*{Basic example}
For most of the values the initial values are taken, except for \texttt{h}.
\circleDraw{h=40}
\subsection*{Somewhat more complex example}
Now we change all values.
\circleDraw{h=50,r=20,color 1=blue,color 2=red}
\subsection*{Overriding the initial values}
Assume you want to have the circle of radius \texttt{h} always to be purple from
now on. Then you could do
\tikzset{EE/color 1=purple}
\circleDraw{h=40}~\circleDraw{h=50,r=20,color 2=red}
\dots but still the local argument ``wins''.
\circleDraw{h=50,r=20,color 1=blue,color 2=red}
\end{document}
Let me stress, though, that this only highlights a very small subset of the features of pgf keys. If you start using them, eventually you will really love more advanced keys like /.search also
, /.try
, /.expanded
and many more.
You have a misconception there. keyval
doesn't alter the arguments provided to some macro as #1
, but only uses that argument. So inside the code of \define@key
you can use the value provided to some key as #1
, but this is only usable inside \define@key
, outside of it, #1
has still the meaning it'd have without it.
What \define@key
does is, it creates a macro that takes one argument and you specify what that macro should do with its argument. That macro definition will later be called by \setkeys
with the value given to the defined key as the argument.
So if you want to define a h
key you'd have that key define some temporary macro that stores the value which was given to it, so that this value can then be used in the rest of the macro.
\documentclass{standalone}
\usepackage{tikz}
\usepackage{keyval}
\makeatletter
% make sure \enthusiastic@h has an initial value and exists, even if the h key
% isn't used later.
\newcommand*\enthusiastic@h{0}
\define@key{enthusiastic}{h}{\pgfmathsetmacro\enthusiastic@h{#1}}
\newcommand{\circleDraw}[1]
{%
\begin{tikzpicture}
\setkeys{enthusiastic}{#1}% this will parse #1 and call the key-macros
\draw (0,0) circle ({0.5*\enthusiastic@h});
\end{tikzpicture}%
}
\makeatother
\begin{document}
\centering
\circleDraw{h=40}
\end{document}