# Intersections with border of matrix node

Consider the following code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix,intersections,calc}

\begin{document}

\begin{tikzpicture}
\matrix (m) [matrix of math nodes,row sep={4em,between origins},column sep={5em,between origins},nodes={anchor=base}]{
|[draw,inner sep=5pt,name path=border1]| \frac{A}{B}& E \\
C & D \\ };
\draw[->,name path=line1] (m-1-1.base west) -- (m-2-2.north east);
\fill [name intersections={of=line1 and border1},green] (intersection-1) circle (1.5pt) (intersection-2) circle (1.5pt);
\fill [red] ($(m-1-1.base)+(intersection-1)$) circle (1.5pt) ($(m-1-1.base)+(intersection-2)$) circle (1.5pt);
\end{tikzpicture}

\end{document}


with result: It aims to find (in green) the intersection of the rectangular border (drawn, and named 'border1') of the top left node (m-1-1) with the only arrow in the diagram. However, as you can see, the intersection points in green do not lie on the border of (m-1-1). Instead, only the shifts of the green points by the point (m-1-1.base), which are drawn in red, do lie on the border.

This indicates that the path named 'border1' is not placed on the top left node, but instead placed somewhere at the centre of the whole picture. More precisely, it seems that the actual border of the node (m-1-1) is 'border1' shifted by (m-1-1.base).

So a few questions relating to this, and how to solve my problem:

1. Why is 'border1' what it is, instead of being the actual border of (m-1-1)?

2. Is it possible to do something different so as to obtain the actual border of the node (instead of the border shifted to the centre of the picture), in a manner that we can then use it to calculate intersections?

3. Given the path 'border1' (after it has been drawn), how can we create a copy of it which is shifted by (m-1-1.base), which we can then use to compute intersections? (By the way, what if we don't know that the node is anchored at (m-1-1.base)?)

• In general, it's better to post one question for each problem. I guess you're also interested in anwers dealing with only one of 1., 2. and 3. - however which would be "accepted" then? We also could get a mix-up. Separate questions (linking to each other would be fine. You wouldn't have to post MWE and screenshot each time. Otherwise it might be a good idea to turn the question and answers to community wiki with the goal to create a comprehensive community answer. – Stefan Kottwitz Apr 17 '11 at 8:40
• @Stefan: Thanks for the comment. I was a bit apprehensive regarding the three different questions myself. I would not mind creating a community wiki, if you think that would allow the question as is. How do I do that, then? – Ricardo Andrade Apr 17 '11 at 8:50
• if you don't want to split the question, let's wait and see the answers you get. I don't convert to CW because I wish answerers shall get rep for their valuable answers. If it's appropriate, a single CW answer could be created summarizing partial answers. – Stefan Kottwitz Apr 17 '11 at 9:29

1. Because matrices are very complicated. I took a look once at the code and tried to follow it. It took me days before I could think straight again. The nodes have to be set up before being positioned, since their position depends on the positions of later nodes, so they're typeset and then put in boxes, measured, then positioned, and finally the boxes are rendered. But the positioning and labelling takes place when they are put in boxes so the offsets are all wrong. If you experiment with other entries in the matrix, you'll see that they are all positioned at the centre of the matrix.

2. Yes. There are two methods. One is to use the fact that after the matrix is rendered, the offsets are remembered in some global macros. If you act fast, you can read these and work out where the offsets are. I did something like this in my answer to the question on setting the background of a matrix: How can I set the background color of the rows and columns of a matrix node in Tikz?. If you really want this, it could be done. But a quicker method is to use the fit library and place a new node around the original one after it has been positioned correctly. So the following works:

\documentclass{article}
%% \url{https://tex.stackexchange.com/q/15985/86}
\usepackage{tikz}
\usetikzlibrary{matrix,intersections,calc,fit}

\begin{document}

\begin{tikzpicture}
\matrix (m) [matrix of math nodes,row sep={4em,between origins},column sep={5em,between origins},nodes={anchor=base}]{
|[draw,inner sep=5pt]| \frac{A}{B}& E \\
C & D \\ };
\node[fit=(m-1-1),name path=border1,inner sep=0pt] {};
\draw[->,name path=line1] (m-1-1.base west) -- (m-2-2.north east);
\fill [name intersections={of=line1 and border1},green] (intersection-1) circle (1.5pt) (intersection-2) circle (1.5pt);
\fill [red] ($(m-1-1.base)+(intersection-1)$) circle (1.5pt) ($(m-1-1.base)+(intersection-2)$) circle (1.5pt);
\end{tikzpicture}

\end{document} 3. I think that my answer to (2) also answers this one.

• Thanks for the answer. My next idea was indeed to look at the code for matrices. I am not so sure that is the right thing to do now... By the way, when you say "if you act fast" what exactly do you mean? – Ricardo Andrade Apr 17 '11 at 22:54
• @Ricardo: By "if you act fast", I mean that there is no guarantee that that information will not be overwritten by some later command. So if you are going to use it, you should do so straight after the matrix command (which is what I do in that background code). I really don't recommend looking at the code for matrices! – Andrew Stacey Apr 18 '11 at 18:15