我可以用矩阵制作一个网格并在其上设置皇后。但现在我想将它们设为一个节点,因为我希望它成为树的一部分。现在网格和矩阵是分开的。这是我的代码:
\documentclass{book}
\usepackage{skak}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix of nodes, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
& & & \\
& & & \symqueen \\
\symqueen & & & \\
& & \symqueen & \\};
\end{tikzpicture}
\begin{tikzpicture}
\node {
\tikz\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\tikz\matrix[matrix of nodes, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
& & & \\
& & & \symqueen \\
\symqueen & & & \\
& & \symqueen & \\};
};
\end{tikzpicture}
\end{document}
我怎样才能让第二个看起来像第一个?
答案1
正如所建议的,其他方法也是可行的。例如,仅使用矩阵和锚点,您可以获得:
\documentclass[border=3mm]{standalone}
\usepackage{skak}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand{\Row}[4]{#1 \& #2 \& #3 \& #4 \\}
\newcommand{\vvRow}{\Row{}{}{}{}\Row{}{}{}{}}%two void rows
\newcommand{\vvvRow}{\Row{}{}{}{}\Row{}{}{}{}\Row{}{}{}{}}%three void rows
\newcommand{\myGrid}[3]{\begin{scope}[shift={#1}]
\matrix[matrix of nodes,
ampersand replacement=\&,
inner sep=0.5pt,
nodes={
inner sep=0pt,
text width=.5cm,
align=center,
text height=0.4cm,
text depth=.1cm},
nodes in empty cells]
(#2){#3};
\foreach \hvL in {1,...,3}
\draw[gray] (#2-1-\hvL.north east) -- (#2-4-\hvL.south east)
(#2-\hvL-1.south west) -- (#2-\hvL-4.south east);
\draw[gray] (#2-4-1.south west) rectangle (#2-1-4.north east);
\end{scope}
}%\myGrid{position}{name}{contents}
\begin{document}
\begin{tikzpicture}
\def\sq{\symqueen}
\def\rw{0.5}
%first grid row
\myGrid{(0,0)}{g-1-1}{\Row{\sq}{}{}{}\vvvRow}
%second grid row
\myGrid{([shift={(-9.5*\rw,-3*\rw)}]g-1-1.south)}{g-2-1}{\Row{\sq}{}{}{}\vvvRow}
\myGrid{([shift={(-3.5*\rw,-3*\rw)}]g-1-1.south)}{g-2-2}{\Row{}{\sq}{}{}\vvvRow}
\myGrid{([shift={(3.5*\rw,-3*\rw)}]g-1-1.south)}{g-2-3}{\Row{}{}{\sq}{}\vvvRow}
\myGrid{([shift={(9.5*\rw,-3*\rw)}]g-1-1.south)}{g-2-4}{\Row{}{}{}{\sq}\vvvRow}
%third grid row
\myGrid{([shift={(-9.5*\rw,-3*\rw)}]g-2-2.south)}{g-3-1}{\Row{}{\sq}{}{}\Row{\sq}{}{}{}\vvRow}
\myGrid{([shift={(-3.5*\rw,-3*\rw)}]g-2-2.south)}{g-3-2}{\Row{}{\sq}{}{}\Row{}{\sq}{}{}\vvRow}
\myGrid{([shift={(3.5*\rw,-3*\rw)}]g-2-2.south)}{g-3-3}{\Row{}{\sq}{}{}\Row{}{}{\sq}{}\vvRow}
\myGrid{([shift={(9.5*\rw,-3*\rw)}]g-2-2.south)}{g-3-4}{\Row{}{\sq}{}{}\Row{}{}{}{\sq}\vvRow}
\foreach \rs/\re/\gs in {1/2/1,2/3/2}
\foreach \gn in {1,...,4}
\draw (g-\rs-\gs.south) -- (g-\re-\gn.north);
\end{tikzpicture}
\end{document}
答案2
我终于找到了一种方法来做到这一点:在 tikzpictures 中使用 tikzpictures。
\documentclass{book}
\usepackage{skak}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[
every matrix/.append style={ampersand replacement=\&,matrix of nodes},
level distance = 3cm,
level 1/.style = {sibling distance = 3cm},
level 2/.style = {sibling distance = 2.5cm},
edge from parent path={(\tikzparentnode.south) -- (\tikzchildnode.north)},
]
\node {
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
\& \& \& \\
\& \& \& \\
\& \& \& \\
\& \& \& \\};
\end{tikzpicture}
}
child {node {
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
\symqueen \& \& \& \\
\& \& \& \\
\& \& \& \\
\& \& \& \\};
\end{tikzpicture}
}}
child {node {
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
\& \symqueen \& \& \\
\& \& \& \\
\& \& \& \\
\& \& \& \\};
\end{tikzpicture}
}
child {node {
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
\& \symqueen \& \& \\
\symqueen \& \& \& \\
\& \& \& \\
\& \& \& \\};
\end{tikzpicture}
}}
child {node {
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
\& \symqueen \& \& \\
\& \symqueen \& \& \\
\& \& \& \\
\& \& \& \\};
\end{tikzpicture}
}}
child {node {
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
\& \symqueen \& \& \\
\& \& \symqueen \& \\
\& \& \& \\
\& \& \& \\};
\end{tikzpicture}
}}
child {node {
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
\& \symqueen \& \& \\
\& \& \& \symqueen \\
\& \& \& \\
\& \& \& \\};
\end{tikzpicture}
}}
}
child {node {
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
\& \& \symqueen \& \\
\& \& \& \\
\& \& \& \\
\& \& \& \\};
\end{tikzpicture}
}}
child {node {
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix, nodes={anchor=center, inner sep=0pt, text width=.5cm,align=center,minimum height=.5cm}, nodes in empty cells]{
\& \& \& \symqueen \\
\& \& \& \\
\& \& \& \\
\& \& \& \\};
\end{tikzpicture}
}};
\end{tikzpicture}
\end{document}