9

I would like to take two TikZ matrix, possibly of different dimensions and stack them. My demo code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes,row sep=-\pgflinewidth,column 2/.style={nodes={rectangle,draw,minimum width=3em}}]
{
0   & 6 \\
};
\matrix [matrix of nodes,row sep=-\pgflinewidth,column 2/.style={nodes={rectangle,draw,minimum width=3em}}]
{
1   & 3 \\
};
\end{tikzpicture}
\end{document}

There should be some space between the matrices and they should be aligned. I've tried to use below of but it doesn't seem to work...

enter image description here

2 Answers2

11

A matrix is just really a node, so if you name the top matrix (Top) then you can use below of=Top in the other matrix to position it:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix  [matrix of nodes,row sep=-\pgflinewidth,column 2/.style={nodes={rectangle,draw,minimum width=3em}}] (Top)
{
0   & 6 \\
};
\matrix [matrix of nodes,row sep=-\pgflinewidth,column 2/.style={nodes={rectangle,draw,minimum width=3em}}, below of=Top]
{
1   & 3 \\
};
\end{tikzpicture}
\end{document}
Peter Grill
  • 223,288
  • What happened to good ol' at (3,2)? :P – percusse Nov 17 '12 at 00:11
  • As I pointed out, I tried to use below of and it doesn't work. The matrices actually overlap. – JonSlaughter Nov 17 '12 at 00:17
  • @JonSlaughter: Is that comment directed at me or percusse? If so, and this solution does not work for you, it would be helpful if you posted a more realistic example – Peter Grill Nov 17 '12 at 00:19
  • @JonSlaughter As Peter showed it works without any complication. What is it that doesn't work? – percusse Nov 17 '12 at 00:21
  • Well, the example is more complex. Why it doesn't work I have no idea. Harish's method worked so I'll go with that. I don't like necessarily hard coding the offset but it will do. My matricies are just normal matrices not much different than above except they use math nodes. – JonSlaughter Nov 17 '12 at 00:21
  • Ok, I guess that shows how important it is to set up the MWE as realistic as possible. Glad you found a solution that works for you. – Peter Grill Nov 17 '12 at 00:23
  • @JonSlaughter If you look at the error that TikZ gives then you know why. manual shift would be very difficult as the examples get more complex. Also you can use the label option instead of using a matrix. – percusse Nov 17 '12 at 00:23
  • Hey, I think that this question gets to the problem with using below of, and solves it pretty well. – bright-star Jan 14 '14 at 22:02
  • Doesn't solve my issue with multiple widths and inconsistent left alignment, though :/ – bright-star Jan 14 '14 at 22:04
  • @TrevorAlexander: Difficult to help without more detail. I would recommend that you post a new question, with a complete MWE including \documentclass and the appropriate packages that sets up the problem. – Peter Grill Jan 14 '14 at 22:37
8

You can also use a scope and shift the entire content.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes,row sep=-\pgflinewidth,column 2/.style={nodes={rectangle,draw,minimum width=3em}}]
{
0   & 6 \\
};
\begin{scope}[yshift=-1cm]
\matrix [matrix of nodes,row sep=-\pgflinewidth,column 2/.style={nodes={rectangle,draw,minimum width=3em}}]
{
1   & 3 \\
};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

  • is there a way to make this work if the column styles include anchor=base west? If one scoped node is wider than the other scoped nodes, the aligned positions are different. – bright-star Jan 14 '14 at 21:54
  • @TrevorAlexander It is possible by choosing proper shift distances and anchors. It is hard to say without some code though. –  Jan 14 '14 at 22:23