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.

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.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

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

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}

See also this question: Programming in TikZ

share|improve this answer
    
Like I wrote, the macro needs to expand to text like "(x,y)" or (name) if "name" is a node –  Alain Matthes Jun 6 '11 at 18:57
add comment

Perhaps the discussion and cross referenced links here might help: Use macro as coordinate in pgfplots plot.

share|improve this answer
    
Thank you! I'll check it. –  pmav99 Jun 6 '11 at 18:46
    
this should be a comment rather than an answer... –  Vivi Jun 6 '11 at 19:34
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.