In my opinion, this is a bug in TikZ. As is well-known on this site (!) the PGF \foreach
command executes its innards inside a group (actually, two groups). This means that when something is meant to be remembered from one iteration to the next, there has to be an explicit mechanism for remembering it. When the \foreach
construction is used to build a path then there are some obvious things that need to be remembered, such as the last point constructed and the last "saved" point. So when TikZ encounters a \foreach
in the path construction then it actually calls a wrapper around the PGF \foreach
which includes this "saving" apparatus.
However, it would appear that the implementation forgot to remember something. When TikZ encounters a bare node coordinate, such as (p1)
, then it can't immediately construct the path at this point. The path will lead up to a point on the node's border, but will then hop to some other point on the node's border and that hop will be determined by the next bit of the path. So TikZ remembers that there is a moveto
which is "waiting" and figures it out when it knows what the next bit of the path will be.
Unfortunately, this waiting moveto is not part of what is remembered when it goes around the \foreach
iteration. So it reverts to the previously remembered moveto which was the one set up before all the grouping started, namely (p1)
.
A solution is to make TikZ remember that moveto between groups. Here's an adaptation of \tikz@foreach
which adds in this rememberence. I have a suspicion that there are a few other things that ought to be remembered between loops as well, such as \tikzlastnode
, but to avoid overcomplicating the code I haven't put them in.
\documentclass{article}
%\url{http://tex.stackexchange.com/q/75146/86}
\usepackage{tikz}
\makeatletter
\def\tikz@foreach{%
\def\pgffor@beginhook{%
\tikz@lastx=\tikz@foreach@save@lastx%
\tikz@lasty=\tikz@foreach@save@lasty%
\tikz@lastxsaved=\tikz@foreach@save@lastxsaved%
\tikz@lastysaved=\tikz@foreach@save@lastysaved%
\let\tikz@moveto@waiting=\tikz@foreach@moveto@waiting
\setbox\tikz@figbox=\box\tikz@tempbox\expandafter\tikz@scan@next@command\pgfutil@firstofone}%
\def\pgffor@endhook{\pgfextra{%
\xdef\tikz@foreach@save@lastx{\the\tikz@lastx}%
\xdef\tikz@foreach@save@lasty{\the\tikz@lasty}%
\xdef\tikz@foreach@save@lastxsaved{\the\tikz@lastxsaved}%
\xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}%
\global\let\tikz@foreach@moveto@waiting=\tikz@moveto@waiting
\global\setbox\tikz@tempbox=\box\tikz@figbox\pgfutil@gobble}}%
\def\pgffor@afterhook{%
\tikz@lastx=\tikz@foreach@save@lastx%
\tikz@lasty=\tikz@foreach@save@lasty%
\tikz@lastxsaved=\tikz@foreach@save@lastxsaved%
\tikz@lastysaved=\tikz@foreach@save@lastysaved%
\let\tikz@moveto@waiting=\tikz@foreach@moveto@waiting
\setbox\tikz@figbox=\box\tikz@tempbox\tikz@scan@next@command}%
\global\setbox\tikz@tempbox=\box\tikz@figbox%
\xdef\tikz@foreach@save@lastx{\the\tikz@lastx}%
\xdef\tikz@foreach@save@lasty{\the\tikz@lasty}%
\xdef\tikz@foreach@save@lastxsaved{\the\tikz@lastxsaved}%
\xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}%
\global\let\tikz@foreach@moveto@waiting=\tikz@moveto@waiting
\foreach}
\makeatother
\begin{document}
\begin{tikzpicture}[scale=.5]
\foreach [count=\x] \pt in {(0,5), (0,2), (2,2), (2,0), (6,0), (6,2), (8, 2), (8,6), (6,6), (6,8), (3,8)}
\node[black, circle, draw] (p\x) at \pt {};
\draw (p1)
\foreach \x in {2,...,11} {
-- (p\x)
};
\end{tikzpicture}
\end{document}
Result:

\foreach \x [count=\xi from 2] in {1,...,10} { \draw (p\x)-- (p\xi);}