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.

In the following diagram, I'm trying to draw a line from the center of the red dot (inside the blue node) to the green node. The coordinate of the red dot is labeled "thepoint", so this should be easy. However the line seems to go from the origin (0,0) of the blue node instead...

Does anyone know what I'm doing wrong? How can I refer to a labeled coordinate that's inside a node? (Or is there a better way of doing this?)

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

%first node
\node [fill=blue!20, matrix] (firstNode)
{
    %some dots
    \fill[red] coordinate (thepoint) (0.4,0.2) circle (3pt);
    \fill[black] (0.0,0.2) circle (3pt);
    \fill[black] (0.0,-0.2) circle (3pt);\\
};

%another node
\node[fill=green!20](anotherNode) at (3,0) {2nd node};

%the line
\draw[-latex] (thepoint) to [out=30,in=180] (anotherNode.west);

\end{tikzpicture}
\end{document}

Thanks!

EDIT: Answered my own question! (sigh...)

I had a small (but important) syntax mistake. The correct way to define the coordinate is:

\fill[red] (0.4,0.2) coordinate (thepoint) circle (3pt);
share|improve this question

1 Answer 1

up vote 7 down vote accepted

I must admit that when I first read the question, my reaction was "Don't do that!".

The "that" which I was thinking of was putting stuff inside a node. But then I saw that you were actually using a matrix node which is explicitly designed for putting stuff inside! However, once I'd seen that you were using a matrix node, I was puzzled as to why you were giving explicit coordinates to the entries as the matrix is meant to deal with all the alignment.

So although you've answered your immediate question, I'd like to offer a couple of reformulations of your code which make use of the matrix alignment. The first is your code (with your correction). The second simply uses the same basic code but uses the matrix to put the sub-objects in their place. The third replaces the sub-objects by nodes. The difference here is subtle: it allows you to put your style options in a different place (and thus organise them slightly differently, and make it easier to modify them all in one go), and the base of the arrow is now the border of the circle, not its centre. To restore it to its centre, make the reference firstNode-1-2.center. Once again, this gives greater flexibility.

Here's the code. The TikZ libraries are only needed for the last version.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix,shapes.geometric}
\begin{document}
\begin{tikzpicture}

%first node
\node [fill=blue!20, matrix] (firstNode)
{
    %some dots
    \fill[red] (0.4,0.2) coordinate (thepoint) circle (3pt);
    \fill[black] (0.0,0.2) circle (3pt);
    \fill[black] (0.0,-0.2) circle (3pt);\\
};

%another node
\node[fill=green!20](anotherNode) at (3,0) {2nd node};

%the line
\draw[-latex] (thepoint) to [out=30,in=180] (anotherNode.west);

\end{tikzpicture}

\begin{tikzpicture}

%first node
\matrix [fill=blue!20,row sep={0.4cm,between origins},column sep={0.4cm,between origins}] (firstNode)
{
    \fill[black] circle (3pt); &
    \fill[red] circle (3pt) coordinate (thepoint); \\
    \fill[black] circle (3pt);\\
};

%another node
\node[fill=green!20](anotherNode) at (3,0) {2nd node};

%the line
\draw[-latex] (thepoint) to [out=30,in=180] (anotherNode.west);

\end{tikzpicture}

\begin{tikzpicture}

%first node
\matrix [
  matrix of nodes,
  every cell/.style={
    ellipse,
    minimum size=6pt,
    inner sep=0pt,
  },
  fill=blue!20,
  row sep={%
    0.4cm,%
    between origins,%
  },
  column sep={%
    0.4cm,%
    between origins,%
  }
] (firstNode)
{
    |[fill=black]| {} &
    |[fill=red]| {} \\
    |[fill=black]| {} \\
};

%another node
\node[fill=green!20](anotherNode) at (3,0) {2nd node};

%the line
\draw[-latex] (firstNode-1-2) to [out=30,in=180] (anotherNode.west);

\end{tikzpicture}
\end{document}

referring to coordinates in TikZ matrices

share|improve this answer
    
Very nice answer! For my own figure, I don't think your matrix approach will work. I probably slightly over-simplified what I really need to draw inside that node for the purposes of my "Minimal Working Example" here. In reality, it's like this: \draw[help lines, line width=0.5, color=black, step=0.2cm] (-0.9,-0.7) grid (0.9,0.7); \fill[black] (0.4,0.2) coordinate (thepoint) circle (3pt); \fill[black] (0.0,0.2) circle (3pt); \fill[black] (0.0,-0.2) circle (3pt);\\ I'll give you the accepted answer though :) –  Spacecookies Sep 19 '11 at 10:19
    
@Spacecookies: I guessed it might be more complicated (it's always dangerous assuming things from a MWE). I was partly writing this for anyone else who stumbles across your question and might want to do something similar. Even so, I find myself nervous at putting actual TikZ stuff inside a node. Is the entire purpose of the outer node to put the background rectangle there? (But now I come to think about it, I'm not sure why I'm so nervous. Certainly putting the stuff inside an inner tikzpicture is Bad, but you're not doing that.) –  Loop Space Sep 19 '11 at 10:26
    
(Incidentally, I don't answer these questions for the number of "accepted"s! I do it to learn more so that I can improve my own documents. You should take Leo's advice and write your own answer which you should accept.) –  Loop Space Sep 19 '11 at 10:29
    
@LoopSpace It's 20.1 for me, but yes, it's there. Thanks, don't know why/how I missed that section yesterday! –  Raphael Sep 26 at 8:17

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.