I use the following code to draw a line to connect two nodes in my graphic:

\draw ( p-\prow-\col) -- (p-\row-\col);

At the edge of the figure, however, not every node will have a predecessor from which the line can originate.

What I would like to do is only draw the line conditionally, depending on whether the node ( p-\prow-\col) already exists or not.

I have seen this question, but the implementation and usage aren't clear to me (and it also seems to be handling the opposite case, executing when the node is undefined).

I'm simply looking for the correct syntax for the command if node is defined: do this

  • @koleygr Thanks for the find, albeit 5 years later.
    – nbrooks
    Oct 21, 2017 at 7:24
  • Sorry.... I just show you had linked the question too... There just was more ways for the solution and thought it would be better to link the original for people that looking for the answer... But it is not a duplicate and I suggest to voters to vote not to close... Sorry!
    – koleygr
    Oct 21, 2017 at 7:42
  • @koleygr But the answer to this question essentially says it took exactly the definition from the linked one. So it seems quite duplicated.
    – TeXnician
    Oct 21, 2017 at 7:47
  • @TeXnician we can change the question to "How to use this answer:..."... Just kiding... But the question is about usage of that solution... I don't know if it could be a comment there the time that asked... usually we advice to open a new question and not to place an answer (with their question on the original answer)... Some times we close the question after that... Anyway... If the question get closed I suggest an edit to the original answer with the usage or something simmilar
    – koleygr
    Oct 21, 2017 at 7:55
  • Well, actually the second paragraph of the answer deals with the usage (if you find the line of code anywhere in the code; it's not really hidden). But let's wait what the close voters say.
    – TeXnician
    Oct 21, 2017 at 8:00

1 Answer 1


The definition of \@nodedefined in Martin Scharrer's answer to the question you linked to seems to do exactly what you ask: The command takes three arguments: The first is the node name to check, the second is the code that's executed if the node exists, and the third is the code that's executed if it doesn't exist (in this case, you'd just leave it empty).




\node (p-1-1) {P-1-1};
\node (p-2-2) at (2,2) {P-2-2};
    \draw (p-1-1) -- (p-2-2);

    \draw (p-1-2) -- (p-2-2);
  • 1
    Thanks for your answer Jake. When I do this, I still get an error Package pgf Error: No shape named p-2-1 is known.
    – nbrooks
    Dec 4, 2012 at 19:56
  • 1
    Sorry, typo in the error message. I meant p-1-2, I simply ran the code from your answer. The error message comes from the draw line, so the conditional seems not to work. I'm not sure how to check the versions of TikZ or PGF...I'm using version 2.43 of TexShop as my editor, if that helps.
    – nbrooks
    Dec 4, 2012 at 20:05
  • There's an error there, 'at's for sure. Dec 4, 2012 at 20:12
  • Ah, ok thanks. It's version 2.10 of PGF
    – nbrooks
    Dec 4, 2012 at 20:12
  • 1
    @AndrewStacey Yes. Hopefully, one day I'll read what I'm writing. Thanks for the correction
    – percusse
    Dec 4, 2012 at 21:09

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