5

I'd like to find out if it is possible to automatically find position of a node in the picture, to use in order to draw additional objects to connect to it or from it without having to manually keep tracking of physical coordinates.

For example, I wanted to make a second x-y axis, rotate it, the make point inside this frame (relative to it) and then wanted to draw a line from (0,0) to this point. Since I rotated the second frame, it is hard to keep track myself of all the physical locations of all the objects I draw in the rotated frame.

It will be useful, if I can get TikZ to do that for me. To make it clear what I mean, here is a MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\fbox{
  \begin{tikzpicture}
   %first frame, the base frame
   \draw [->] (0,0) -- (1,0) ;
   \draw [->] (0,0) -- (0,1) ;

   %second frame, rotate it by 45 degrees relative to the base
   \draw[rotate around={-45:(2,2)}] [->] (2,2) -- (3,2) ;
   \draw[rotate around={-45:(2,2)}] [->] (2,2) -- (2,3) ;

   %draw a point, relative to the second frame
   \draw[rotate around={-45:(2,2)}] [->] (2,2) -- (2.5,2.5) ;
   \draw[rotate around={-45:(2,2)}] [fill=red] (2.5,2.5) circle(.25ex);

   %now, I want to draw an arrow from (0,0) to the circle above.
   %I can't just do this below, since frame 2 has rotated
   %\draw [->,color=red] (0,0) -- (2.5,2.5) ;    
\end{tikzpicture}
}
\end{document}

Mathematica graphics

I need now to draw an arrow from (0,0) to the red circle but I do not know the coordinates of the circle now, relative to the picture, since frame 2 is rotated. I'd have to calculate it. I know its coordinates relative to frame 2.

Is there a way to label it, and use the label to automatically find its coordinates relative the base frame (i.e., the drawing itself)?

  • 1
    use \node (<node-name>) at (<coordinate>) {}; Then you can refer to the node by name later on without having to remember the coordinates. – A.Ellett Feb 6 '15 at 19:53
  • 2
    @A.Ellett Or \coordinate (<coord name>) at (x,y); – Torbjørn T. Feb 6 '15 at 19:56
7

Yes. You can add a named coordinate, which is a special type of node, at the red dot, and use this instead of explicit coordinates.

This is described in chapter 17 Nodes and their shapes of the manual.

\documentclass[tikz,border=2mm]{standalone}
\begin{document}
  \begin{tikzpicture}
   %first frame, the base frame
   \draw [->] (0,0) -- (1,0) ;
   \draw [->] (0,0) -- (0,1) ;

   \begin{scope}[rotate around={-45:(2,2)}]
   %second frame, rotate it by 45 degrees relative to the base
   \draw[->] (2,2) -- (3,2) ;
   \draw[->] (2,2) -- (2,3) ;

   %draw a point, relative to the second frame

   \draw [->] (2,2) -- (2.5,2.5) ;
   \draw[fill=red] (2.5,2.5) circle(.25ex) coordinate (red dot);
   \end{scope}


   \draw [->,color=red] (0,0) -- (red dot) ;    
\end{tikzpicture}
\end{document}
  • great! and thanks for adding the scope construct, this is much better than having to rotate each object one by one. Tikz is looking really nice. Using named coordinates will make it much easier to make much more complex drawings now. – Nasser Feb 6 '15 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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