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.


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):

% or

   \expandafter\ifx\csname pgf@sh@pi@#1\endcsname\pgfpictureid
| improve this answer | |
  • 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

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.