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}
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:Update: using
trees
Following
SebGlav
suggestion I've managed to use aTiKZ-tree
for such a graphic. I was not able to do it withforest
.