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


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.




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


% This fails here ...

\foreach \n\lab in \nodes{

\vspace{10 mm}

% ... but it works fine here.



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.

  • Though I worry about debugging, I suppose (and can imagine) that there are scenarios in which this could be very useful. – orome May 21 '12 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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