DISCLAIMER: This is not an answer. Actually, I would be really interested in an answer that explains why that happens.
I quickly glanced through tikz.code.tex and found the comment
Comment by TT: I hope I fixed the \tikz \foreach problem. The new
version will take a conservative approach and will only do fancy stuff
when the next keyword after \tikz is one of the following: \draw,
\fill, \filldraw, \graph, \matrix,
I agree that this does not tell us why the second code example does not work, but one message which one may, or may not, take from this is that foreach
is tricky. The reason why I do not agree with koleygr's answer, or the way I read it, is that IMHO it does not explain why this happens, and, what is IMHO even a bit more worrisome, suggests that you need to have a complete path in the second argument of foreach. Of course, since I do not really understand what's going on in detail, I may very well be wrong. Nevertheless I'd like to present a short snippet that does sort of what is intended in the second, nonworking foreach loop. It is a foreach loop that is not too elegant but works.
\documentclass[tikz]{standalone}
\begin{document}
\tikzset{my foreach code/.code={\xdef\Lst{}\foreach #1 }}
\begin{tikzpicture}[]
\draw [my foreach code={\a in {0,30,...,330} {\xdef\Lst{\Lst (\a:2) -- }}}]
\Lst cycle;
\end{tikzpicture}
\end{document}

It is instructive to compare this to the following nonworking example.
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\xdef\Lst{}
\draw foreach \a in {0,30,...,330} {\xdef\Lst{\Lst (\a:2) -- }} \Lst cycle;
\end{tikzpicture}
\end{document}
One would expect that this works, but it doesn't. According to how I read koleygr's answer, the first code should not work either, but it does. (Those who may want to object that \foreach
is not foreach
: the pgfmanual states in section 14.14: "For historical reasons, you can also write \foreach instead of foreach.")
BOTTOMLINE: I do not really have a simple message other than this is tricky and it might well be that in the future there might be a foreach
version that works with the syntax.
pgf
has to pass first the coordinates to get to the other one(s), so simpy-- (…)
otherwise it doesn't pass them as mentioned. Cycle is just the first point the path, so just[…] -- cycle;
. This is actually a really bad answer, but this is just a quick one, so sorry for that …foreach
is a (TeX) group. You can't write\draw {(0,0) --} (0,1);
but you can write\draw (0,0) { -- (0,1)};
.