[Tex/LaTex] pgf macro that returns a node

macrosnodestikz-pgf

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.

Best Answer

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}; 
\node[draw] at (0,0) {bad};
\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}