# What is the scope of elements of TikZ pictures?

I understand that named elements of a tikzpicture cannot be referenced before they are defined; but I'm confused about how "long" a named element persists. It appears that once named, an element continues to exist, even in (subsequent) pictures that do not contain the element.

For example, the code

\draw[bluearrow](e)--(r);


generates the expected "undefined element" error if used prior to the definition of 'e' and 'r', but once I create a picture containing code that defines these elements, subsequent figures containing exactly the code above will no longer produce an error, and will instead draw the requested arrow, between the locations in that figure corresponding to the locations of 'e' and 'r' in the original figure in which they were defined:

What is the default scope of tikzpicture element names? Is there a way to control their scope? The (apparent) default behavior seems likely to produce hard to debug behavior in figures.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{petri}

\begin{document}

\tikzset{
bluearrow/.style={->, blue, fill=none,>=stealth', thick},
state/.style={circle,draw=black,inner sep=0pt,minimum size=7mm,label=center:$#1$,name=#1}}

\begin{center}

% This fails here ...
%\begin{tikzpicture}
%\draw[bluearrow](e)--(a);\draw[bluearrow](a)--(a^2);\draw[bluearrow](a^2)--(a^3);\draw[bluearrow](a^3)--(e);
%\end{tikzpicture}

\begin{tikzpicture}
\def\nodes{0/{e},1/{r},2/{r^2},3/{r^3}};\def\dim{4};
\foreach \n\lab in \nodes{
\node[state={\lab}]at({cos(90+\n*(360/\dim))},{sin(90+\n*(360/\dim))}){};}
\draw[bluearrow](e)--(r);\draw[bluearrow](r)--(r^2);\draw[bluearrow](r^2)--(r^3);\draw[bluearrow](r^3)--(e);
\end{tikzpicture}

\vspace{10 mm}

% ... but it works fine here.
\begin{tikzpicture}
\draw[bluearrow](e)--(r);\draw[bluearrow](r)--(r^2);\draw[bluearrow](r^2)--(r^3);\draw[bluearrow](r^3)--(e);
\end{tikzpicture}

\end{center}

\end{document}

-

TikZ itself adds a lot of internal groups, e.g. every \path ...; command is a TeX group. Because \node is basically just \path node the node name can not be defined locally because it would not survive it's own definition scope! While there are some techniques getting a macro out of a scope using such for every node (which is defined as a set of several macro) at every group inside a tikzpicture is just not doable. Therefore nodes etc. are defined globally, which makes them accessible for the rest of the document, even in other tikzpictures. The position of the node relative to the current origin should still be valid, so you could define \coordinate (my offset) at (1,1); in one tikzpicture and still use it in other ones. If you however want to draw directly to the original coordinate/node then remember picture,overlay is required.
No \node is not just \path [node]; node is a path operation and the definition is \path... node ... without [..]. It's not exactly a path operation like to , arc etc.. because the node is not exactly a part of a path but an object placed along a path. – Alain Matthes May 21 '12 at 20:50
@Altermundus: Thanks, I corrected that. I confused \path [node] and \path node. – Martin Scharrer May 21 '12 at 21:19