Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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