1

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}

enter image description here

1 Answer 1

1

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}

enter image description here

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}

enter image description here

4
  • great, how to change it to align on top, I try to change anchor to north west but it doesn't work
    – lucky1928
    Jun 9, 2020 at 16:49
  • 1
    @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!
    – lucky1928
    Jun 9, 2020 at 19:29
  • 1
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.