1

I am looking for something that is the equivalent of \providecoordinate in TikZ/PGFplots. This would be easy if I knew how to test if a coordinate is already defined.

There are similar questions about how to implement something which effectively implements providetikzstyle and how to implement provideenvironment.

1 Answer 1

3

A coordinate in TikZ is a special form of node. The coordinate x/y etc. values of a node is stored in a macro named \pgf@sh@np@<name> which existence you can test. The following code should do it and support most variations of the \coordinate syntax. Supporting the \path coordinate syntax, i.e. allow for \path providecoordinate is not possible without extensively hacking into the path parsing code.

\documentclass{article}

\usepackage{tikz}

\makeatletter
\newcommand{\providecoordinate}[1][]{%
    \@providecoordinate[#1]%
}
\def\@providecoordinate[#1]#2(#3)#4;{%
    \ifcsname pgf@sh@np@#3\endcsname\else
        \coordinate[#1]#2(#3)#4;
    \fi
}
\makeatother

\begin{document}

\begin{tikzpicture}
   \providecoordinate [] (A) at (0,0);
   \coordinate (B) at (0,0);

   \providecoordinate (B) at (1,0);

   % Small test: Both at the same location? YES!
   \draw [red]   (A) circle (1pt);
   \draw [green] (B) circle (2pt);
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .