# positioning node with matrix inside

I wish the nodes distribute horizontally side by side! and align nodes on top! but currently, it's overlapped.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}[mymat/.style={matrix of nodes,
nodes={draw,font=\sffamily\bfseries,text width=1em,
text height=1em,baseline=center},
column sep=-\pgflinewidth/2}]
\node[mymat] (m0) {
0 \\
};
\node[mymat,right=1cm of m0] (m1) {
0 \\
1 \\
};
\node[mymat,right=1cm of m1.west,anchor=west] (m2) {
0 & 0 \\
1 & 0 \\
1 & 1 \\
0 & 1 \\
};
\node[mymat,right=1cm of m2.west,anchor=west] (m3) {
0 & 0 & 0\\
1 & 0  & 0\\
1 & 1  & 0\\
0 & 1 & 0\\
0 & 1 & 1\\
1 & 1  & 1\\
1 & 0  & 1\\
0 & 0 & 1\\
};
\end{tikzpicture}
\end{document}


Just remove the west anchors. You can set (and change) the node distance. This is easier than saying every time right=1cm of ....

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}[mymat/.style={matrix of nodes,
nodes={draw,font=\sffamily\bfseries,text width=1em,
text height=1em,baseline=center},
column sep=-\pgflinewidth/2},node distance=1cm]
\node[mymat] (m0) {
0 \\
};
\node[mymat,right=of m0] (m1) {
0 \\
1 \\
};
\node[mymat,right=of m1] (m2) {
0 & 0 \\
1 & 0 \\
1 & 1 \\
0 & 1 \\
};
\node[mymat,right=of m2] (m3) {
0 & 0 & 0\\
1 & 0  & 0\\
1 & 1  & 0\\
0 & 1 & 0\\
0 & 1 & 1\\
1 & 1  & 1\\
1 & 0  & 1\\
0 & 0 & 1\\
};
\end{tikzpicture}
\end{document}


If you want to align them at the top, use north east and north west anchors.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}[mymat/.style={matrix of nodes,
nodes={draw,font=\sffamily\bfseries,text width=1em,
text height=1em,baseline=center},
column sep=-\pgflinewidth/2},node distance=0.5cm]
\node[mymat] (m0) {
0 \\
};
\node[mymat,right=of m0.north east,anchor=north west] (m1) {
0 \\
1 \\
};
\node[mymat,right=of m1.north east,anchor=north west] (m2) {
0 & 0 \\
1 & 0 \\
1 & 1 \\
0 & 1 \\
};
\node[mymat,right=of m2.north east,anchor=north west] (m3) {
0 & 0 & 0\\
1 & 0  & 0\\
1 & 1  & 0\\
0 & 1 & 0\\
0 & 1 & 1\\
1 & 1  & 1\\
1 & 0  & 1\\
0 & 0 & 1\\
};
\end{tikzpicture}
\end{document}


• great, how to change it to align on top, I try to change anchor to north west but it doesn't work Jun 9, 2020 at 16:49
• @lucky1928 Now I think I understand why you put these anchors. You need to use north east and north west anchors, I added an example.
– user194703
Jun 9, 2020 at 16:58
• Thanks, perfect. not sure why the text not in the center of node! Jun 9, 2020 at 19:29
• @lucky1928 To have the nodes horizontally centered, use nodes={draw,font=\sffamily\bfseries,text width=1em, text height=1em,baseline=center,align=center}. To have them completely centered, use something like nodes={draw,font=\sffamily\bfseries,minimum size=1.6em,anchor=center},.
– user194703
Jun 9, 2020 at 19:31