First of all,
\tikzstyle{<key>}=[<value>]
is the same as
\tikzset{<key>/.style={<value>}}
From this point I will only use the \tikzset
syntax (→ Should \tikzset or \tikzstyle be used to define TikZ styles?). But you would've had the same problem with \tikzset
.
The assignment is not a problem, the style TIKZStyle
contains simply your argment to styleA
. But when TikZ executes this style (say when \draw[TIKZSTYLE]
) it sees firstly \DEFstyle
and its definition and tries it as a key, but a key named top color=green,bottom color=red
(for TikZ this is just one “string” at this moment) does not exist.
Even top color=green
won’t work because, again, TikZ does not see the =
hidden in \DEFstyle
.
The only thing that would work is is a style without a parameter, say
\setstyles{styleA={rounded corners}}%
\tikz \draw[TIKZSTYLE] (0,0) rectangle (1em,2ex);
which results in
. But even that is not safe enough:
\setstyles{styleA={rounded corners}}%
\def\DEFstyle{fill}%
\tikz \draw[TIKZSTYLE] (0,0) rectangle (1em,2ex);
(But then again, throw some @
in the \DEFstyle
macro’s name …).
These are the reasons we need to expand \DEFstyle
before assigning it to the TIKZSTYLE
style:
\tikzset{TIKZSTYLE/.style/.expand once=\DEFstyle}%
In your definition of the \setstyles
macro are some spurious spaces:
I do not know much about your project, but as a reminder, you can do this all with PGF keys. See the second Code block.
Code
\documentclass[tikz]{standalone}
\usepackage{xkeyval}
\makeatletter
\def\DEFstyle{}
\define@key{styles}{styleA}{\def\DEFstyle{#1}}
\newcommand{\setstyles}[1]{%
\setkeys{styles}{styleA={}}%
\setkeys{styles}{#1}%
\tikzset{TIKZSTYLE/.style/.expand once=\DEFstyle}%
}
\makeatother
\begin{document}
\setstyles{styleA={top color=green,bottom color=red}}
\tikz \draw[TIKZSTYLE] (0,0) rectangle (1em,2ex);
\end{document}
Code (sans-xkeyval
)
\documentclass[tikz]{standalone}
\pgfkeys{
/no xkeyval/styleA/.style={
/tikz/TIKZSTYLE/.style={#1}
}
}
\newcommand*{\setstyles}[1]{%
\pgfqkeys{/no xkeyval}{#1}%
}
\begin{document}%
\setstyles{styleA={top color=green,bottom color=red}}%
\tikz \draw[TIKZSTYLE] (0,0) rectangle (1em,2ex);
\end{document}
\tikzset{TIKZSTYLE/.style/.expand once=\DEFstyle}
seems to work! Thanks a lot! May you consider to explain your solution? Otherwise I can answer this question myself. Also: thanks for the link.