这tikzpicture
会在图表上组织方形网格。网格内的数字对齐不太好(图表左侧太靠左,图表右侧太靠右)。我该如何做才能正确?谢谢!
代码风格和/或整体美观方面的任何其他改进总是值得赞赏的(箭头不是最漂亮的)。我尝试使用matrix of nodes
,这本来可以简化和澄清代码,但无法让它在 内工作\newcommand
,所以我改用手动放置节点。
\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}
答案1
您正在使用嵌套的 tikzpictures,这是不推荐的。在此特定情况下,内部图片使用其定位定义的锚点。
不使用嵌套图片的另一种方法是使用matrix
节点。如下所示:
\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}
更新:使用trees
根据SebGlav
建议,我已成功使用 来TiKZ-tree
制作此类图形。我无法使用 来做到这一点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}