7

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?

1 Answer 1

8

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.

4
  • Wow, I didn't know options descended like that. That makes this trick virtually painless. But why are the boxes in the nested tikz larger than the ones outside?
    – Ryan Reich
    Commented Nov 23, 2010 at 15:59
  • @Ryan: that is a good question. I can only guess that by using inner sep and outer sep in the right scopes you can get the boxes in the second line to be the same size as those in the first line. Commented Nov 23, 2010 at 16:14
  • Hmm, but that is the sort of ugliness I want to avoid. I mean, what is a good inner sep? Tests show that setting inner sep = 0.3333em (the default) changes neither the bounding box of the first row nodes nor of the nested nodes in the second row. That is, somehow, tikz thinks that both the small and the large boxes have the same inner sep! I believe this is a legitimate bug.
    – Ryan Reich
    Commented Nov 23, 2010 at 23:03
  • It turns out that for my application, I am setting inner sep = 0 globally anyway, so this (bug or not) is not a problem with your solution for me. I will wait a little while before accepting it in case someone else comes up with something surprising.
    – Ryan Reich
    Commented Nov 24, 2010 at 0:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .