# pgf macro that returns a node

How can I write a macro that returns a coordinate (or a node). The node must be usable within a tikzpicture environment and preferably inside a path declaration.

I need something like the following (it doesn't compile). I am not sure if pgfcoordinate is the correct way to go...

\documentclass{article}
\usepackage{tikz}
\usepackage{pgf}

\begin{document}

\newcommand{\mycoord}[2]{
\pgfmathsetmacro{\x}{\pgfmathparse{#1} \pgfmathresult} % actually do some calculations
\pgfmathsetmacro{\y}{\pgfmathparse{#2} \pgfmathresult} % actually do some calculations
\pgfcoordinate{\x}{\y}} % I want this node to be returned.

\begin{tikzpicture}

\node at \mycoord{1}{2} {some text}; % I want to use the calculated node

\end{tikzpicture}
\end{document}

the error message is

Package tikz Error: Cannot parse this coordinate.

-

This a normal error. Tikz waits coordinates something like (3,0) or (a) where a is the name of a node. I think you do not use correctly \pgfcoordinate I never use this macro so perhaps I'm wrong but this macro defines quickly a name of a node associates with coordinates. You can define your macro outside the tikzpicture.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\newcommand{\mycoord}[2]{
\pgfcoordinate{#1}{#2}}
\mycoord{a}{\pgfpoint{3cm}{0cm}}

\begin{tikzpicture}
\node[draw] at (a) {good};
\end{tikzpicture}
\end{document}

The next code is interesting only if you want to modify \xand \y

\documentclass{article}
\usepackage{tikz}

\begin{document}

\newcommand{\mycoord}[2]{
\pgfmathsetmacro\x{5} % or what you want
\pgfmathsetmacro\y{-2}}
\begin{tikzpicture}
\node[draw](d){deb};
\path \pgfextra \mycoord{1}{2} \endpgfextra node[draw] (f) at (\x,\y) {end}; %
\draw (d)--(f) ;
\end{tikzpicture}
\end{document}
-
Thank you. Unfortunately I can't quite get it to work. The example I gave is rather simplified. My actual case is more complicated and I need to call mycoord within the tikzpicture environment. Preferably within a path. – pmav99 Jun 6 '11 at 16:33
@pmav99 perhaps my answer is not clear but you use pgfcoordinate in wrong way. The definition is \pgfcoordinate{⟨name ⟩}{⟨coordinate ⟩}. The first argument must be a name. Then when you use \node at you need to give something that can be parsed like coordinate. I add another example in my answer – Alain Matthes Jun 6 '11 at 18:48

I think the easiest way to go is simply to have the macro expand to text like "(x,y)" and use e.g. calc or other standard transformations to manipulate that. For example, you can do straightforward things like:

\newcommand{\mycoord}[2]{([xshift=2cm] #1,#2)}

And then embed the macro call in paths just like you'd expect.

\draw (0,0) -- \mycoord{0}{0};

Here's a minimal example that shows using calc. Note that this does something fairly random (I just took an example from the manual and modified it), it is just intended to show how the syntax would work rather than really being useful:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\newcommand{\mycoordB}[3]{(${2*(#3-3)}*(#1,#2)$)}

\begin{tikzpicture}
\fill [black] \mycoordB{0cm}{0cm}{4} circle (2pt);
\fill [blue] \mycoordB{1cm}{2cm}{5} circle (2pt);
\fill [green] \mycoordB{2cm}{2cm}{2} circle (2pt);
\end{tikzpicture}

\end{document}