0

I can make a grid and set queens on it with a matrix. But now I want to make them a node, because I want it to be part of tree. And now the grid and the matrix are separated. Here is my code :

\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}

enter image description here

How can I have the second one looks like the first ?

  • @MS-SPO I would love to know how to place the queens symbol for a given child, because it is exactly what I am struggling to do ! – PoustouFlan Jul 30 '21 at 17:45
  • 1
    Did you have a look at the minimal introduction and the PGF manual here: https://ctan.org/pkg/pgf?lang=en ? – MS-SPO Jul 30 '21 at 17:47
  • @MS-SPO I did look at the minimal introduction, I might be wrong but there is nothing related to tree in, and my problem is I have no idea how to place the queens in the tree. – PoustouFlan Jul 30 '21 at 17:52
  • 2
    Right, that's what the huge manual is for. Don't be afraid, just navigate to e.g. ch 6 "tutorial", ch. 17 "nodes and edges", ch 20.4 "anchoring a matrix", ch. 21 "making trees grow" etc. Prefer scanning over strict reading, at least in your first pass ;-) – MS-SPO Jul 30 '21 at 17:56
  • May be this is where you want to end up with? https://tex.stackexchange.com/questions/133332/to-draw-a-chessboard/133383#133383 . You can also downsize the chessboard with this package, e.g. to 4x4. – MS-SPO Jul 30 '21 at 19:45

2 Answers2

2

As suggested, even other approaches are possible. For example using only matrix and anchors you can get:

\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}

grid whit queens

vi pa
  • 3,394
1

I finally figured out a way to do this : using tikzpictures inside of 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}

enter image description here