This is a "feature" of how TeX parses the TikZ syntax. When TeX finds the keyword node
it starts looking for what comes next (up to the end of the node
) and it only "knows" about a certain number of things, such as the [optional settings]
the (label)
and the {text}
. It doesn't know how to deal with the \foreach
command. It also doesn't seem too keen on the foreach
key-word, but I may not have gotten the syntax right for that so someone else might have a solution using that.
However, what you want to do is eminently possible using the power of the \pgfkeys
mechanism. This has inbuilt a method for iterating over a list (imaginatively called .list
). Here's some code that works for your example.
\documentclass{article}
% \url{http://tex.stackexchange.com/q/24948/86}
\usepackage{tikz}
\tikzset{
repeated preaction/.style={%
repeat preaction/.list={#1}
},
repeat preaction/.code args={#1/#2}{%
\tikzset{
preaction={
fill,
transform canvas={xscale=#1,yscale=#1},
#2
}
}
}
}
\begin{document}
\begin{tikzpicture}
\draw node
[preaction={fill,transform canvas={xscale=1.3,yscale=1.3},red}]
[preaction={fill,transform canvas={xscale=1.2,yscale=1.2},green}]
[preaction={fill,transform canvas={xscale=1.1,yscale=1.1},blue}]
[text width=10em, text height=10em,circle,fill=white] {} ;
\end{tikzpicture}
\begin{tikzpicture}
\draw node
[repeated preaction={1.3/red, 1.2/green, 1.1/blue}]
[text width=10em, text height=10em,circle,fill=white] {} ;
\end{tikzpicture}
\end{document}
What this does is define a few extra keys. I'll take them in the order in which they are called.
repeated preaction
: This takes one argument, which is a list. Its sole job is to pass that argument as a list to the next key. If you were prepared to write repeat preaction/.list={...}
in the [...]
bit then you could skip this part. (Perhaps a better name for this key would be repeat preaction with
.)
repeat preaction
: This is the code that is iterated over by the list syntax. So it gets passed each element of the list in turn. As the list is in the format scale/colour
, we specify the argument syntax accordingly rather than just taking a list of arguments. This executes its code on each iteration, and what it does is simply to set the corresponding key. As we are now in "code" form (necessary to handle the arguments), we have to jump back in to the option-setting mode with the \tikzset
command.
With a little more finesse, the actual preaction could also be passed to the initial option (though one would have to be careful about macro parameters).
(I scaled up your scales a bit so that they were easier to see. Also, the sides of picture got cropped by the cropper, that's because of the use of scaling confusing it about bounding boxes. That wouldn't happen in a real document.)
This is just a longish comment. I might be wrong in understanding the workflow and hopefully I'll be corrected if so.
In pgfplots
things do not happen all at once. First your plot commands together with the axis options are parsed and stored as PGF paths. Then a visualization scheme kicks in (and I'm not 100% sure about the order) and path colorings, legend styles etc. are assigned. Then they are drawn with low level TikZ/PGF commands.
In order to select a cycle list, the plots need to be collected already such that they are counted. But that would probably be too late to change the drawing options. I don't think this functionality is supported off-the-shelf.
Note that this is a very rough description, there are many things checked in each layer so don't take this too literal.
But if you are willing to provide the number of plot items then it's a matter of defining a style with an argument that selects the predefined plot cycle list.
Best Answer
\foreach \x/\y in {..}
. You forgotin
.By the way, it's a bit shorter with
rectangle
: