[Tex/LaTex] How to make tikz center the cells of a matrix

matricestikz-pgf

I want to construct complicated things in tikz matrices (but don't we all), and when I'm done, I want the rows and columns of the matrix to align along the centers of these complicated things. Here is an example of what goes wrong:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
 \matrix
 {
  \path node{a} node[right] {b}; \\
  \node{c};\\
 };
\end{tikzpicture}
\end{document}

You will see that the second row is aligned at the left of the first row. The reason for this is explained quite clearly in the tikz manual: rows and columns are aligned along the "origins" of the cells.

What this means is that each cell is considered a tiny picture with an origin (that is, (0,0)) and all those points are put on a grid. It's clear from this that what has happened in the above picture is that the \path begins at (0,0) and moves right, and likewise the \node begins at its (0,0) and moves right, and (as claimed) those two origins are put in a line.

Unfortunately, that's not what I want and there's no obvious way to change it. I want to tell tikz that the "origin" of the top cell is the center of its bounding box. If it were a single node that would be easy: just write [anchor = center] as an option.

But I can't put paths in nodes, so there's no way to do that directly. And although it is possible to pass an [xshift = <dimen>] to the \path, I would have to know what that shift is before starting, whereas (since it is such a complicated figure) I can't know that until the end, when it is too late to perform a coordinate transformation.

How can I do this? Basically, I want to give an entirely relative construction and then slap a coordinate system on it afterwards. Alternatively, I want to know if there is any matrix option that does this that I have overlooked. Another alternative: is there a different way to achieve this alignment without using matrices?

Best Answer

You can't put paths in nodes, but you can put paths in tikzpictures in nodes! Compare the first and second lines. I added some styling as in Section 17.3 of the PGF (2.10) manual.alt text

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={draw=black},font=\Huge]
 \matrix[draw=red]
 {
  \node{a} node[right] {b}; \fill[blue] (0,0) circle (2pt); \\
  \node{\tikz{\node{a} node[right] {b};}}; \fill[blue] (0,0) circle (2pt); \\
 \node{c}; \fill[blue] (0,0) circle (2pt);\\
 };
\end{tikzpicture}
\end{document}

Note how the options in the outer tikzpicture environment descend into the \tikz group inside the node.