Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

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}
share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

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.

share|improve this answer
    
Though I worry about debugging, I suppose (and can imagine) that there are scenarios in which this could be very useful. –  raxacoricofallapatorius May 21 '12 at 20:27
    
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
    
But I agree with the big part of your answer –  Alain Matthes May 21 '12 at 20:54
    
@Altermundus: Thanks, I corrected that. I confused \path [node] and \path node. –  Martin Scharrer May 21 '12 at 21:19
    
When you say 'If you however want to draw directly to the original coordinate/node than remember picture,overlay is required.', you mean '... THEN remember picture...', right? –  rvf0068 May 21 '12 at 23:16
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.