TikZ: alignment with number grids arranged in a graph

tikz-pgf

This tikzpicture organizes square grids on a graph. The numbers inside the grid do not align very well (too far to the left on the left of the graph, too far to the right on the right of the graph). How can I get that right? Thanks!

Any other improvements in the code style and/or overall esthetics always appreciated (the arrows are not the prettiest). I tried to use a matrix of nodes, which would have simplified and clarified the code, but couldn't get it to work inside a \newcommand, so I resorted to placing nodes manually instead.

\documentclass[class=article,border=5pt,tikz]{standalone}
\usetikzlibrary{backgrounds, scopes, positioning}
\usetikzlibrary{matrix}

\newcommand{\nodeA}{\tikz{%
    \draw[step=1,color=gray] (0,0)  grid (3,3);
    \node at (1/2,5/2) {1};
    \node at (1/2,3/2) {2};
    \node at (5/2,1/2) {9};
}}%

\newcommand{\nodeB}{\tikz{%
    \draw[step=1,color=gray] (0,0)  grid (3,3);
    \node at (1/2,5/2) {1};
    \node at (1/2,3/2) {2};
    \node at (1/2,1/2) {3};
    \node at (3/2,5/2) {4};
    \node at (5/2,1/2) {9};
}}%

\newcommand{\nodeC}{\tikz{%
    \draw[step=1,color=gray] (0,0)  grid (3,3);
    \node at (1/2,5/2) {1};
    \node at (1/2,3/2) {2};
    \node at (3/2,5/2) {3};
    \node at (5/2,1/2) {9};
}}%

\newcommand{\nodeD}{\tikz{%
    \draw[step=1,color=gray] (0,0)  grid (3,3);
    \node at (1/2,5/2) {1};
    \node at (1/2,3/2) {2};
    \node at (1/2,1/2) {3};
    \node at (3/2,5/2) {4};
    \node at (3/2,3/2) {5};
    \node at (5/2,1/2) {9};
}}%

\newcommand{\nodeE}{\tikz{%
    \draw[step=1,color=gray] (0,0)  grid (3,3);
    \node at (1/2,5/2) {1};
    \node at (1/2,3/2) {2};
    \node at (1/2,1/2) {3};
    \node at (3/2,5/2) {4};
    \node at (5/2,5/2) {5};
    \node at (3/2,3/2) {6};
    \node at (5/2,1/2) {9};
}}%

\newcommand{\nodeF}{\tikz{%
    \draw[step=1,color=gray] (0,0)  grid (3,3);
    \node at (1/2,5/2) {1};
    \node at (1/2,3/2) {2};
    \node at (3/2,5/2) {3};
    \node at (1/2,1/2) {4};
    \node at (5/2,1/2) {9};
}}%

\newcommand{\nodeG}{\tikz{%
    \draw[step=1,color=gray] (0,0)  grid (3,3);
    \node at (1/2,5/2) {1};
    \node at (1/2,3/2) {2};
    \node at (3/2,5/2) {3};
    \node at (3/2,3/2) {4};
    \node at (1/2,1/2) {5};
    \node at (5/2,1/2) {9};
}}%

\newcommand{\nodeH}{\tikz{%
    \draw[step=1,color=gray] (0,0)  grid (3,3);
    \node at (1/2,5/2) {1};
    \node at (1/2,3/2) {2};
    \node at (3/2,5/2) {3};
    \node at (5/2,5/2) {4};
    \node at (5/2,1/2) {9};
}}%

\begin{document}

\begin{tikzpicture}[node distance = 10mm and 20mm]
\node (A)                    {\nodeA};
\node (B) [below  left=of A] {\nodeB};
\node (C) [below right=of A] {\nodeC};
\node (D) [below left=of B]  {\nodeD};
\node (E) [below =of B]      {\nodeE};
\node (F) [below left=of C]  {\nodeF};
\node (G) [below =of C]      {\nodeG};
\node (H) [below right=of C] {\nodeH};

\scoped[on background layer]
    \draw[->] (A) edge (B) (A) to (C);
    \draw[->] (B) edge (D) (B) to (E);
    \draw[->] (C) edge (F) (C) to (G) (C) to (H);
\end{tikzpicture}

\end{document}

graph of grids

Best Answer

You are using nested tikzpictures, which is not recommended. In this particular case, inner picture is using the anchor defined by its positioning.

An alternative without nesting picture could be to use matrix nodes. Something like this:

\documentclass[class=article,border=5pt,tikz]{standalone}
\usetikzlibrary{backgrounds, scopes, positioning}
\usetikzlibrary{matrix}

\tikzset{
    mygrid/.style={
        matrix,
        inner sep=1pt,
        column sep=-\pgflinewidth,
        row sep=-\pgflinewidth,
        matrix of nodes,
        nodes in empty cells,
        nodes={draw=gray, anchor=center, inner sep=.3333em, minimum size=5mm}
    }
}

\begin{document}

\begin{tikzpicture}[node distance = 10mm and 20mm]
\node[mygrid] (A) {1&&\\2&&\\&&9\\};
\node[mygrid, below left=of A] (B) {1&4&\\2&&\\3&&9\\};
\node[mygrid, below right=of A] (C) {1&3&\\2&&\\&&9\\};
\node[mygrid, below left=of B] (D) {1&4&\\2&5&\\3&&9\\};
\node[mygrid, below =of B] (E) {1&4&5\\2&6&\\3&&9\\};
\node[mygrid, below left=of C] (F) {1&3&\\2&&\\4&&9\\};
\node[mygrid, below =of C] (G) {1&3&\\2&4&\\5&&9\\};
\node[mygrid, below right=of C] (H) {1&3&4\\2&&\\&&9\\};

\scoped[on background layer]
    \draw[->] (A) edge (B) (A) to (C);
    \draw[->] (B) edge (D) (B) to (E);
    \draw[->] (C) edge (F) (C) to (G) (C) to (H);
\end{tikzpicture}

\end{document}

enter image description here

Update: using trees

Following SebGlav suggestion I've managed to use a TiKZ-tree for such a graphic. I was not able to do it with forest.

\documentclass[class=article,border=5pt,tikz]{standalone}
\usetikzlibrary{matrix}

\tikzset{
    mygrid/.style={
        matrix,
        inner sep=1pt,
        column sep=-\pgflinewidth,
        row sep=-\pgflinewidth,
        matrix of nodes,
        nodes in empty cells,
        nodes={draw=gray, anchor=center, inner sep=.3333em, minimum size=5mm}
    },
    mygridfortree/.style={mygrid, ampersand replacement=\&}
}

\begin{document}

\begin{tikzpicture}[
    level distance=2cm, 
    level 1/.style={sibling distance=6cm},
    level 2/.style={sibling distance=2cm}
]
\node[mygrid] {1&&\\2&&\\&&9\\}
    child{node[mygridfortree] {1\&4\&\\2\&\&\\3\&\&9\\}
        child{node[mygridfortree] {1\&4\&\\2\&5\&\\3\&\&9\\}}
        child{node[mygridfortree] {1\&4\&5\\2\&6\&\\3\&\&9\\}}}
    child{node[mygridfortree] {1\&3\&\\2\&\&\\\&\&9\\}
        child {node[mygridfortree] {1\&3\&\\2\&\&\\4\&\&9\\}}
        child {node[mygridfortree] {1\&3\&\\2\&4\&\\5\&\&9\\}}
        child {node[mygridfortree] {1\&3\&4\\2\&\&\\\&\&9\\}}};
\end{tikzpicture}

\end{document}

enter image description here

Related Question