# How to nest xymatrix in Xy-pic

This is a bit of a long post, so apologies in advance. I would like to nest xymatrices in Xy-pic so that entire xymatrices would be treated as single entries in another xymatrix.

I tried the obvious method of putting an xymatrix as an entry in another, which didn't work.

I then did some search and read a few manuals, and found one method which only deals with two matrices side by side, with the starting point of the arrows not at the centers of the two matrices, as in the code below:

$\xymatrix{ & R_1 \ar[r] \ar[d] & P \\ R_2 \ar[r] \ar[d] & B & \\ Q && } \qquad\rightarrow\qquad \xymatrix{a}$


I then found on a thread on the Xy official site someone asking the same question as me; and he was told to use xygraph, and that there is an xymovie about this type of construction.

I couldn't find the movie; and couldn't find much information on how to use xygraph either ( the section in Xy-pic RM didn't include many examples).

Eventually I tried this:

\xy
\xygraph{!M

\xymatrix{
& R_1 \ar[r] \ar[d] & P \\
R_2 \ar[r] \ar[d] & B & \\
Q &&
} ([u]X : ?, [l]Y : ?)

}
\endxy


which still didn't do what I wanted.

So is there a way to nest xymatrices? Or maybe I need to use some other graphics packages? Thank you very much for you help! (And your time for reading this.)

-
Your question "Or maybe I need to use some other graphics packages?" is dangerous! It'll be interesting to see how long you have to wait before getting an answer starting "This is really easy using TikZ ...". –  Loop Space Aug 30 '10 at 7:34
@Andrew: I’m tempted. –  Caramdir Aug 30 '10 at 9:22
Actually it is not possible to nest TikZ matrices. –  Caramdir Aug 30 '10 at 9:47
@Caramdir: I'm surprised at that, but there's 19minutes between your comments so I guess you tried quite a few possibilities. However, I suspect that "nesting matrices" is a means to an end and if user1025 could clarify that end, it might be easier to help. I'll leave a comment to that end ... –  Loop Space Aug 30 '10 at 10:33
@user1025: could you explain what you want to achieve by nesting xy-matrices? In particular, do you want the ability to nest to arbitrary depth, or just one Big Matrix for positioning and then Sub Matrices for the actual information? Presumably you're wanting to draw arrows between the sub-matrices, is that the main functionality you want? –  Loop Space Aug 30 '10 at 10:36

You could use several xy matrices inside an xy environment and refer to them. Here's an example, using pieces of your code, which prints two xy matrices and connects them by an arrow:

\documentclass{article}
\usepackage[matrix,arrow]{xy}
\begin{document}
$\xy \xymatrix"m"{ & R_1 \ar[r] \ar[d] & P \\ R_2 \ar[r] \ar[d] & B & \\ Q && } \POS+(42,0) \xymatrix{ \ar@{<-}["m"rr] A \ar[r] & B } \endxy$
\end{document}


The first matrix is named "m". The command \ar@{<-}["m"rr]in the second matrix draws an arrow from the current point to the node P of the matrix "m" using the relative positioning rr from the upper left corner. I used @{<-} to revert the arrow.

Output:

This way you could use several matrices and connect them by arrows. Regarding actual nesting: the xy-pic reference manual says:

Matrix nesting is not safe.

-
Thanks. How did you find the 42 in \POS+(42,0), was it try and error or is it possible to work out from the matrix "m"? Also I would really like to have the starting and end points of arrows the centers of matrices they connect if that's possible. –  user1025 Aug 30 '10 at 22:06
I've just tested the value and 42 looked well. It should be possible to connect the centers, perhaps look at the reference manual: ctan.tug.org/tex-archive/macros/generic/diagrams/xypic/xy/doc/… –  Stefan Kottwitz Sep 1 '10 at 16:51

Your second example wouldn't compile because you shouldn't add the \xymatrix command after M. The documentation states that

M<matrix> will be insert the result of \xymatrix<matrix> as the node

So

\xy
\xygraph{!M{
& R_1 \ar[r] \ar[d] & P \\
R_2 \ar[r] \ar[d] & B & \\
Q &&
} ([u]X : ?, [l]Y : ?)
}
\endxy


will compile. On the other hand, reading the manual, it states that for an \xymatrix

the entire matrix is an object itself with reference point its top left entry

arrows going to and from it won't center correctly. Which can be seen if you compile the above code.

-
Thanks, but I would really like a way to center arrows. –  user1025 Aug 30 '10 at 22:01
I noticed. But I can't find a way to do it semi-automatically with Xy-pic. You can always position/draw arrows by hand (or through references as Stefan describes), but I suspect you don't want that. I'll edit if I think of something. –  Willie Wong Aug 30 '10 at 22:05