How can I know if a node is already defined?

I think I've seen this question before, but I can't find it. The problem is simple; before I assign a name to a node I would like to know if the name is already used.

I think when a node is created \node (name) {label}; some macros are defined from name.

-
Upped and starred. This has bothered me for a while. –  qubyte Dec 9 '11 at 13:43
Related Question: Checking whether or not a node has been previously defined. –  Peter Grill Dec 4 '12 at 19:58

A PGF/TikZ node with the name <name> defines the macro \pgf@sh@ns@<name> (e.g. \csname pgf@sh@ns@#1\endcsname where #1 is the name). This macro defines the node shape (ns), e.g. rectangle. Other defined macros are \pgf@sh@np@<name> (Node Point), \pgf@sh@nt@<name> (Node Transformation matrix) and \pgf@sh@pi@<name> (node Picture Id).

You can use e.g. \@ifundefined{pgf@sh@ns@#1}{<node does not exists>}{<node exists>} to test if a node with this name got already define in the document (node are declared globally).

If you want to test if the node was already used in the current picture, you better test (also) the picture id if it matches the current picture id (\pgfpictureid, e.g. pgfid1, i.e. use \ifx for comparision).

Here some possible implementations of such macros (not tested yet):

\def\@nodeundefined#1{%
\@ifundefined{pgf@sh@ns@#1}%
}
% or
\long\def\@nodedefined#1#2#3{%
\@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}

\def\@nodedefinedcurrpic#1{%
\expandafter\ifx\csname pgf@sh@pi@#1\endcsname\pgfpictureid
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}

-
Thanks I forgot the part of the name pgf@ns@... thanks also for the abbreviations ns, np, etc. very useful. Do you know what is the signification of sh ? Now with your help, I look at the sources and I remark : \pgfutil@ifundefined{pgf@sh@ns@#1} . –  Alain Matthes Dec 9 '11 at 15:15
@Altermundus: I would say sh stands for 'shape'. Nodes are defined mostly over their shape. –  Martin Scharrer Dec 9 '11 at 15:21
To place a node with the anchor of a given named node, the sources give : \def\pgfpointanchor#1#2{% % Ok, check whether #1 is known! \pgfutil@ifundefined{pgf@sh@ns@#1} {\pgfutil@ifundefined{pgf@sh@ns@not yet positionedPGFINTERNAL#1}{%... –  Alain Matthes Dec 9 '11 at 15:22