This approach re-defined the .style handler by appending (in this case)
\expandafter\let\csname ifqrr@trace@pgfk@/tikz/Hollow\endcsname\iftrue
to it. Now, /tikz/Hollow is stored in the macro \pgfkeyscurrentpath, so we need to expand it once, because, of course, \pgfkeyscurrentpath will be different when the style is called.
This is the reason I used .ecode instead of .code for the redefinition of the .style handler:
\pgfkeys{
/handlers/.style/.code=
\pgfkeys{
\pgfkeyscurrentpath/.ecode=
\noexpand\expandafter\noexpand\let\noexpand
\csname ifqrr@trace@pgfk@\pgfkeyscurrentpath\noexpand\endcsname\noexpand\iftrue
\noexpand\pgfkeysalso{\unexpanded{#1}}%
}
}
This has the following consequences:
- Currently, it only works with
.style, not with .estyle, .style args, .estyle args, .style 2 args or .style n args. It also does not work with the handler .append style (which is often used) with a previous undefined style, i.e. .append style without a previous .style.
- It does not work with previous defined styles.
I also have defined a two-argument expecting .if handler that tests if the key is given, but sadly, it cannot be used inside a path. Therefore I have defined a very simple \iftikzstyle macro that only works for keys in the /tikz/ path but it can be used inside a path.
Further Improvements
Code
\documentclass{article}
\usepackage{tikz}
\pgfkeys{/handlers/.style/.code=\pgfkeys{\pgfkeyscurrentpath/.ecode=\noexpand\expandafter\noexpand\let\noexpand\csname ifqrr@trace@pgfk@\pgfkeyscurrentpath\noexpand\endcsname\noexpand\iftrue\noexpand\pgfkeysalso{\unexpanded{#1}}}}
\pgfkeys{
/handlers/.if/.code 2 args={% works not in the middle of a path
\expandafter\ifx\csname ifqrr@trace@pgfk@\pgfkeyscurrentpath\endcsname\iftrue#1\else#2\fi}}
\makeatletter
\newcommand*{\iftikzstyle}[1]{% only for /tikz/ styles
\expandafter\ifx\csname ifqrr@trace@pgfk@/tikz/#1\endcsname\iftrue
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother
\tikzset{Hollow/.style={red}}
\tikzset{Solid/.style={green}}
\newcommand{\TikzPicture}{%
\begin{tikzpicture}
\draw [ultra thick, blue, ->, My Options] (0,0) -- (1,0)
\iftikzstyle{Hollow}{node[right] {Error: Option Hollow specified.}}{node[opacity=.5,right] {Option Hollow not specified}}
;
\end{tikzpicture}%
}%
\begin{document}
\tikzset{My Options/.style={Hollow}}
\TikzPicture
\tikzset{My Options/.style={}}
\TikzPicture
\tikzset{Hollow/.if={Hollow is specified.}{Hollow is not specified.}}
\tikzset{Hollow}
\tikzset{Hollow/.if={Hollow is specified.}{Hollow is not specified.}}
\end{document}
Output

/.tryit at the first instance then it's gonna stay there. – percusse Mar 05 '13 at 23:09/.try, will have to look that up.. – Peter Grill Mar 05 '13 at 23:31