5

I drew the below picture to illustrate the backtracking tree on the k-queen problem with k = 4. enter image description here

It's a bit ugly... and I don't know how to solve the problem : the connecting lines from the child operation do not reach children properly. We can for instance see that on the far left node on level 1. Setting the inner sep to 0pt has close to no effect.

Here's the code:

\documentclass{standalone}

\usepackage[x11names,dvipsnames,table]{xcolor}
\usepackage{tikz}
\usepackage{skak}

\newcommand{\chessnode}[4]{ %rows cols pos border
    \begin{tikzpicture}    
    \foreach \rows in {1,...,#1}
        \foreach \cols in {1,...,#2}
        {
            \pgfmathparse{mod(\rows+\cols,2) ? "white" : "black!30"}
            \edef\colour{\pgfmathresult}
            
            \filldraw[\colour] (\rows-1,\cols-1) rectangle ++ (1,1);
        }
        
    \foreach \x/\y in {#3}
        \node at (\y-0.5,\x-0.5) {\scalebox{2}{\symqueen}};
        
    \draw[ultra thick,#4] (0,0) rectangle (#1,#2);
    \end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}
[level distance=6cm,every node/.style={inner sep=0pt},
level 1/.style={sibling distance=8cm},
level 2/.style={sibling distance=5cm},
level 3/.style={sibling distance=5cm}
]
\node {\chessnode{4}{4}{}{black}}
    child[line width=6pt] { node{ \chessnode{4}{4}{4/1}{black} }
        child { node {\chessnode{4}{4}{4/1,3/3}{red!60}}}
        child { node {\chessnode{4}{4}{4/1,3/4}{black}}
            child { node {\chessnode{4}{4}{4/1,3/4,2/2}{red!60}}}
        }
    }
    child[line width=6pt] { node{ \chessnode{4}{4}{4/2}{black} }
        child { node {\chessnode{4}{4}{4/2,3/4}{black}}
            child { node {\chessnode{4}{4}{4/2,3/4,2/1}{black}}
                child { node {\chessnode{4}{4}{4/2,3/4,2/1,1/3}{Green}}}
            }
        }
    }
    child[line width=6pt] { node{ \chessnode{4}{4}{4/3}{black} }
        child { node {\chessnode{4}{4}{4/3,3/1}{black}}
            child { node {\chessnode{4}{4}{4/3,3/1,2/4}{black}}
                child { node {\chessnode{4}{4}{4/3,3/1,2/4,1/2}{Green}}}
            }
        }
    }
    child[line width=6pt] { node{ \chessnode{4}{4}{4/4}{black} }
        child { node {\chessnode{4}{4}{4/4,3/1}{black}}
            child { node {\chessnode{4}{4}{4/4,3/1,2/3}{red!60}}}
        }
        child { node {\chessnode{4}{4}{4/4,3/2}{red!60}}}
    };
\end{tikzpicture}
\end{document}
7
  • 2
    Nesting tikzpictures is usually not recommended. Using a tikz \pic might be the safer option. Commented Oct 26, 2024 at 13:06
  • While it isn't clear that \pic is going to solve the problem, the syntax of \chessnode is too complex to be put inside of a \pic, so it isn't going to help anyway. @cfr, where? I set inner sep=0pt as an option of the root tikzpicture, so there shouldn't be any space added Commented Oct 26, 2024 at 17:02
  • the syntax of \chessnode cannot be made to work inside a pic (the grammar isn't going to recognise it). I have several \foreach distinct loops, for instance. Commented Oct 26, 2024 at 20:57
  • 1
    I see nothing in \chessnode that wouldn't wor0k in a pic. What went wrong when you tried it? Commented Oct 27, 2024 at 9:34
  • The CFG of the \pic command doesn't allow multiple non nested loops Commented Nov 4, 2024 at 15:40

4 Answers 4

5

Instead of nesting TikZ pictures I use the next best thing: a matrix. Instead of the child path operation of TikZ, I'll be using the forest package.

To make the edges connect better to the nodes, I'll place them behind the nodes and make them extend a bit by using a rect line end. (We also could just extend them to the center of the nodes.)

The q key adds a queen and also to all the children. This means you don't have to repeat positions of queens.

Code

\documentclass[tikz]{standalone}
\usepackage{forest}
\usepackage{skak}
\usetikzlibrary{backgrounds}
\ExplSyntaxOn\let\TikzChessAlphToInt\int_from_alph:n\ExplSyntaxOff
\newcommand*\tikzchessset{\pgfqkeys{/tikz/chess}}
\tikzchessset{
  every chess board/.style={x=+1em, y=+1em},
  rows/.initial=4, columns/.initial=4,
  background color odd/.initial=black!30,
  background color even/.initial=white,
  node/.style 2 args={
    black, anchor=center, inner sep=+0pt,
    at={(\TikzChessAlphToInt{#1}-.5,.5-#2)}}}
\forestset{
  chess/.style={
    /tikz/every outer matrix/.append style={
      inner sep=+.5\pgflinewidth, draw, shape=rectangle, thick},
    content=\chessnode, matrix},
  declare toks={list}{},
  chessboard/.style={
    for tree={% edges on background
      +edge path=\noexpand\pgfonlayer{background},
      edge path+=\noexpand\endpgfonlayer,
      edge+={line cap=rect, ultra thick}},
    q/.style={list+={,##1}, for descendants={list+={,##1}}, chess},
    bad/.style=red, good/.style=green}}
\newcommand{\chessnode}{%
  \pgfpositionnodelater\relax % disable forest-ing
  \tikzset{chess/every chess board}%
  \foreach[count=\iRow from 0] \row in {1, ..., \pgfkeysvalueof{/tikz/chess/rows}}
    \foreach[count=\iCol from 0] \col in {1, ..., \pgfkeysvalueof{/tikz/chess/columns}}
       \fill[fill=\pgfkeysvalueof{/tikz/chess/background color
         \ifodd\pgfintmod{\iCol+\iRow}{2} odd\else even\fi}]
           (\iCol,-\iRow) rectangle + (1,-1);
  \foreach[expand list] \v in {\forestoption{list}}
    \path\if,\v,\else node[chess/node/.expanded=\v]{\symqueen}\fi;
  \pgfmatrixendrow}
\begin{document}
\begin{forest} chessboard
[, chess
  [, q=a1 [, q=c2, bad] [, q=d2        [, q=b3, bad ]]]
  [, q=b1 [, q=d2       [, q=a3        [, q=c4, good]]]]
  [, q=c1 [, q=a2       [, q=d3        [, q=b4, good]]]]
  [, q=d1 [, q=a2       [, q=c3, bad]] [, q=b2, bad ]]
]
\end{forest}
\end{document}

Output

enter image description here


Here's a version that keeps track of places where a queen is still allowed and marks non-allowed ones with an X.

Keeping track of that also allows for an automatic version, unfortunately, this only works for sizes smaller than 6 (pdfLaTeX) or 7 (LuaLaTeX).

Code

\documentclass[tikz]{standalone}
\usepackage{forest}
\usepackage{skak}
\usetikzlibrary{backgrounds}
\ExplSyntaxOn\let\TikzChessAlphToInt\int_from_alph:n\ExplSyntaxOff
\newcommand*\tikzchessset{\pgfqkeys{/tikz/chess}}
\tikzchessset{
  every chess board/.style={/utils/exec=\pgfpositionnodelater\relax, x=+1em, y=+1em},
  rows/.initial=4, columns/.initial=4,
  background color odd/.initial=black!30,
  background color even/.initial=white,
  node/.style args={#1/#2}{black, anchor=center, inner sep=+0pt, at={(#1-.5,.5-#2)}},
  not allowed node/.style args={#1/#2}{chess/node={#1}/{#2}, gray, node contents=$\times$},
  not allowed/.style 2 args={insert path={node[chess/not allowed node=#1/#2]}}}
\newcommand{\chessnode}[2]{%
  \tikzset{chess/every chess board}%
  \foreach[count=\iRow from 0] \row in {1, ..., \pgfkeysvalueof{/tikz/chess/rows}}
    \foreach[count=\iCol from 0] \col in {1, ..., \pgfkeysvalueof{/tikz/chess/columns}}
       \fill[fill=\pgfkeysvalueof{/tikz/chess/background color
         \ifodd\pgfintmod{\iCol+\iRow}{2} odd\else even\fi}]
           (\iCol,-\iRow) rectangle + (1,-1) [chess/for rectangle={\col}{\row}];
  \foreach[expand list] \v in {#1} \path\if,\v,\else node[chess/node/.expanded=\v]{\symqueen}\fi;}
\makeatletter
\forestset{
  remove from/.code 2 args={%
    \expanded{\noexpand\pgfutil@in@{#1}{\forestoption{#2}}}%
    \ifpgfutil@in@
      \def\forest@temp##1#1##2\relax{\forestset{#2={##1##2}}}%
      \expanded{\noexpand\forest@temp\forestoption{#2}}\relax
    \fi},
  if in/.code n args={4}{%
    \expanded{\noexpand\pgfutil@in@{#1}{\forestoption{#2}}}%
    \ifpgfutil@in@\expandafter\pgfutil@firstoftwo\else\expandafter\pgfutil@secondoftwo\fi{\pgfkeysalso{#3}}{\pgfkeysalso{#4}}}}
\makeatother
\forestset{
  declare toks={listofqueens}{},
  declare toks={listofallowedqueens}{},
  % one of these as the option of forest
  manual chessboard tree/.style={
    chessboard tree={#1},
    for tree={
      chess/init={#1},
      delay={if n children={0}{if level={#1}{/forest/chess/good}{/forest/chess/bad}}{}}}},
  auto chessboard tree/.style={chessboard tree={#1}, chess/auto={#1}{1}},
  chessboard tree/.style={
    /tikz/chess/rows={#1}, /tikz/chess/columns={#1},
    /tikz/chess/for rectangle/.style 2 args={%
      /forest/if in={,##1/##2}{listofallowedqueens}{}{
        /forest/if in={,##1/##2}{listofqueens}{}{
          chess/not allowed={##1}{##2}}}},
    chess/init={#1},
    before drawing tree={for tree={ % edges on background:
      +edge path=\noexpand\pgfonlayer{background},
      edge path+=\noexpand\endpgfonlayer,
      edge+={line cap=rect, ultra thick}}},
    q'/.style 2 args={chessboard, chess/do={##1}{##2}},
    q/.style 2 args={q'/.expanded={\TikzChessAlphToInt{##1}}{##2}},
  },
  chess/bad/.style=red, chess/good/.style=green,
  chessboard/.style={
    /tikz/every outer matrix/.append style={
      inner sep=+.5\pgflinewidth, draw, shape=rectangle, thick}, matrix,
    content=\chessnode{\forestoption{listofqueens}}{\forestoption{listofallowedqueens}}\pgfmatrixendrow},
  %
  chess/init/.style={% start with all positions allowed
    listofallowedqueens=,
    chess/@init/.style={
      chess/@@init/.style={listofallowedqueens+={,##1/####1}},
      chess/@@init/.list={1, ..., #1}},
    chess/@init/.list={1, ..., #1}},
  chess/do/.style 2 args={
    for tree={
      listofqueens+={,#1/#2}, remove from={,#1/#2}{listofallowedqueens},
      chess/do row={#1}{#2}, chess/do col={#1}{#2}, chess/do dia={#1}{#2}}},
  % these might try to remove something that was never in the list to begin with
  chess/do row/.style 2 args={
    chess/@do/.style={remove from={,##1/#2}{listofallowedqueens}},
    chess/@do/.list={1, ..., \pgfkeysvalueof{/tikz/chess/columns}}},
  chess/do col/.style 2 args={
    chess/@do/.style={remove from={,#1/##1}{listofallowedqueens}},
    chess/@do/.list/.expanded={\inteval{#2+1}, ..., \pgfkeysvalueof{/tikz/chess/rows}}},
  chess/do dia/.style 2 args={
    chess/@do/.style={% might try to remove something in non-existing rows/cols
      remove from/.expanded={,\inteval{#1-##1}/\inteval{#2+##1}}{listofallowedqueens},
      remove from/.expanded={,\inteval{#1+##1}/\inteval{#2+##1}}{listofallowedqueens}},
    chess/@do/.list/.expanded={1, ..., \inteval{\pgfkeysvalueof{/tikz/chess/rows}-#2}}},
  % automatically placing queens
  chess/auto/.style n args={2}{% #1: options, #2: size, #3: level
    chess/@auto/.estyle={
      if in={,##1/#2}{listofallowedqueens}{
        append={[,
          listofqueens={\forestoption{listofqueens}},
          listofallowedqueens={\forestoption{listofallowedqueens}},
          q'={##1}{#2}, chess/auto={#1}{\inteval{#2+1}}]}}{}},
    delay={if n children={0}{if level={#1}{/forest/chess/good}{/forest/chess/bad}}{}},
    chess/@auto/.list={1, ..., #1}}}
\begin{document}
\begin{forest} manual chessboard tree=4
[, chessboard
  [, q=a1
    [, q=c2]
    [, q=d2
      [, q=b3]]]
  [, q=b1
    [, q=d2
      [, q=a3
        [, q=c4]]]]
  [, q=c1
    [, q=a2
      [, q=d3
        [, q=b4]]]]
  [, q=d1
    [, q=a2
      [, q=c3]]
    [, q=b2]]
]
\end{forest}
\begin{forest} auto chessboard tree=3
[, chessboard]
\end{forest}
\begin{forest} auto chessboard tree=4
[, chessboard]
\end{forest}
\begin{forest} auto chessboard tree=5
[, chessboard]
\end{forest}
\end{document}

Output

enter image description here

3
  • Even if it's not in the question, it might be useful to add crosses on forbidden positions for each new line explored. PS: I really like the way you code in TikZ. Commented Oct 27, 2024 at 9:37
  • 1
    @projetmbc Good idea! Commented Oct 27, 2024 at 17:40
  • Man, your solution is really awesome. Looks good, solves the problem, with elegant code. Commented Oct 28, 2024 at 10:05
5

Please respect the one-question-per-question rule. This answers your first question. I suggest removing your second and asking it separately, linking here as appropriate.

It is a tree, so I would use forest. I would also use orthogonol or 'forked' edges. Otherwise, you will have to mess around with arrow tips etc. and I still don't think it will look aesthetically pleasing.

It is safe to use boxes containing tikzpictures inside tikzpictures because the boxes are typeset separately, so that's what I do here. An alternative would be pics, but you will likely still need boxes anyway.

Here's the basic forest configuration:

\forestset{%
  declare count register={board no},
  board no'=0,

We use a forest count register to ensure a unique number which we can use to name the box for each node. Then we create a chess node style taking 4 arguments, which wraps your code in a box, ensures a unique box for each board and sets the node content to that box.

  chess node/.style n args=4{%
    board no'+=1,

Increment our counter.

    TeX={%

Execute the following code immediately and directly in TeX i.e. not inside the picture.

      \expandafter\newbox\csname boardbox\foresteregister{board no}\endcsname

Make a new box with a unique name.

      \expandafter\setbox\csname boardbox\foresteregister{board no}\endcsname=\hbox{%

Set the box to the desired tikzpicture, which is just a copy of what you had in the question.

        \begin{tikzpicture}
          \foreach \rows in {1,...,#1}
          \foreach \cols in {1,...,#2}
          {
            \pgfmathparse{mod(\rows+\cols,2) ? "white" : "black!30"}
            \edef\colour{\pgfmathresult}
            \filldraw[\colour] (\rows-1,\cols-1) rectangle ++ (1,1);
          }
          \foreach \xchess/\ychess in {#3}
          \node at (\ychess-0.5,\xchess-0.5) {\scalebox{2}{\symqueen}};
          \draw[ultra thick,#4] (0,0) rectangle (#1,#2);
        \end{tikzpicture}% comment line endings to avoid spaces
      }%
    },

Set the content of the node to the box, expanding now so we get the right one and not just the last one created when forest actually comes to use it.

    content/.expanded=\expandafter\box\csname boardbox\foresteregister{board no}\endcsname,
  },
}

forest version with forked edges and boxed boards

Aside from the above, we use forked edges, zero inner sep and thicker lines.

If you don't want forked edges, you can modify the code to create something like

uglier version

which uses 5pt for the width of the edges and is, imho, quite ugly. However, that is partly to make the effect of the changes clear. Doubtless you would choose something subtler.

  for tree={
    inner sep=0pt,
    edge path={%
      \noexpand\scoped[on background layer]{\noexpand\path[\forestoption{edge}] (!u.parent anchor)--(.child anchor)\forestoption{edge label};}%

This puts the edges on the background layer defined by the backgrounds library. Aside from that, it is just the standard definition forest uses by default for edge path.

    },
    edge+={shorten >=-2pt,shorten <=-2pt,line width=5pt},

This extends the edge by 2pt at the beginning and end of the path. Depending on the size of your nodes and the thickness of your lines, you may adjust this as appropriate. Just don't try this with non-opaque nodes as the results will not be pleasing.

  },

This produces the appearance of a mitred end where the edge joins a node at an angle, without having to mess around figuring out which angle is required. It is the equivalent of hemming: you just hide the mess under something neater.

Complete code:

\documentclass[border=10pt]{standalone}
% ateb: https://tex.stackexchange.com/a/729516/
% addaswyd o gwestiwn R. Absil: https://tex.stackexchange.com/q/729489/
\usepackage[x11names,dvipsnames]{xcolor}
\usepackage[edges]{forest}
\usepackage{skak}
\usetikzlibrary{backgrounds}
\forestset{%
  declare count register={board no},
  board no'=0,
  chess node/.style n args=4{%
    board no'+=1,
    TeX={%
      \expandafter\newbox\csname boardbox\foresteregister{board no}\endcsname
      \expandafter\setbox\csname boardbox\foresteregister{board no}\endcsname=\hbox{%
        \begin{tikzpicture}
          \foreach \rows in {1,...,#1}
          \foreach \cols in {1,...,#2}
          {
            \pgfmathparse{mod(\rows+\cols,2) ? "white" : "black!30"}
            \edef\colour{\pgfmathresult}
            \filldraw[\colour] (\rows-1,\cols-1) rectangle ++ (1,1);
          }
          \foreach \xchess/\ychess in {#3}
          \node at (\ychess-0.5,\xchess-0.5) {\scalebox{2}{\symqueen}};
          \draw[ultra thick,#4] (0,0) rectangle (#1,#2);
        \end{tikzpicture}%
      }%
    },
    content/.expanded=\expandafter\box\csname boardbox\foresteregister{board no}\endcsname,
  },
}
\begin{document}
\begin{forest}
  for tree={
    inner sep=0pt,
  },
  forked edges,
  edge+={ultra thick},
  [,chess node={4}{4}{}{black}
    [,chess node={4}{4}{4/1}{black}
      [,chess node={4}{4}{4/1,3/3}{red!60}]
      [,chess node={4}{4}{4/1,3/4}{black}
        [,chess node={4}{4}{4/1,3/4,2/2}{red!60}]
      ]
    ]
    [,chess node={4}{4}{4/2}{black}
      [,chess node={4}{4}{4/2,3/4}{black}
        [,chess node={4}{4}{4/2,3/4,2/1}{black}
          [,chess node={4}{4}{4/2,3/4,2/1,1/3}{Green}
          ]
        ]
      ]
    ]
    [,chess node={4}{4}{4/3}{black}
      [,chess node={4}{4}{4/3,3/1}{black}
        [,chess node={4}{4}{4/3,3/1,2/4}{black}
          [,chess node={4}{4}{4/3,3/1,2/4,1/2}{Green}]
        ]
      ]
    ]
    [,chess node={4}{4}{4/4}{black}
      [,chess node={4}{4}{4/4,3/1}{black}
        [,chess node={4}{4}{4/4,3/1,2/3}{red!60}]
      ]
      [,chess node={4}{4}{4/4,3/2}{red!60}]
    ]
  ]
\end{forest}
\begin{forest}
  for tree={
    inner sep=0pt,
    edge path={%
      \noexpand\scoped[on background layer]{\noexpand\path[\forestoption{edge}] (!u.parent anchor)--(.child anchor)\forestoption{edge label};}%
    },
    edge+={shorten >=-2pt,shorten <=-2pt,line width=5pt},
  },
  [,chess node={4}{4}{}{black}
    [,chess node={4}{4}{4/1}{black}
      [,chess node={4}{4}{4/1,3/3}{red!60}]
      [,chess node={4}{4}{4/1,3/4}{black}
        [,chess node={4}{4}{4/1,3/4,2/2}{red!60}]
      ]
    ]
    [,chess node={4}{4}{4/2}{black}
      [,chess node={4}{4}{4/2,3/4}{black}
        [,chess node={4}{4}{4/2,3/4,2/1}{black}
          [,chess node={4}{4}{4/2,3/4,2/1,1/3}{Green}
          ]
        ]
      ]
    ]
    [,chess node={4}{4}{4/3}{black}
      [,chess node={4}{4}{4/3,3/1}{black}
        [,chess node={4}{4}{4/3,3/1,2/4}{black}
          [,chess node={4}{4}{4/3,3/1,2/4,1/2}{Green}]
        ]
      ]
    ]
    [,chess node={4}{4}{4/4}{black}
      [,chess node={4}{4}{4/4,3/1}{black}
        [,chess node={4}{4}{4/4,3/1,2/3}{red!60}]
      ]
      [,chess node={4}{4}{4/4,3/2}{red!60}]
    ]
  ]
\end{forest}
\end{document}
3
  • (I removed the other question) While your code looks nicer, it only does because connecting lines are orthogonal to the sides of the connected nodes. I could have used something like the |- shape for arrows with a similar result. I don't want it to look that way. I want it to "look like I did", with properly connected nodes. Commented Oct 26, 2024 at 20:48
  • 1
    @R.Absil well, obviously that's why. if you connect a thick, blunt line at an angle, what do you expect? if you don't want gaps, don't add spaces into your definitions and, if you must have angles, either use thinner lines or hide their ends. Commented Oct 27, 2024 at 0:10
  • I expected there was a "nice way" of filling the connection points. As @MS-SPO pointed out in his post, it comes from the fact that as soon as tikz detects an intersection between the line and the edge of the node, it stops drawing. I would like it to "finish the remaining triangle". An unelegant solution would be to drop a coordinate in each of my chessnode, and have the child instruction connect these coordinates on background (with the background library). This will work... but the code will be so ugly... Commented Oct 27, 2024 at 8:33
3

Some analysis, on the route towards a solution. Suggestions below.

Let's minimize your code to a silly version, i.e. bypassing \chessnode, to see relevant features: it holds some surprises:

  • it's asymmetric
  • the thick lines look strange ... at first glance

result

ROOT:

  • dotted line is the top nodes shape
  • line stops being drawn, once the middle intersects the shape

root

Does this hold for the others, too? Well, it's similar:

  • as soon, as the incoming line ...
  • ... touches/intersects the nodes shape
  • ... it stops being drawn (so to say: a different intersection point is calculated)

left


\documentclass{standalone}

\usepackage[x11names,dvipsnames,table]{xcolor}
\usepackage{tikz}
\usepackage{skak}

\newcommand{\chessnode}[4]{ %rows cols pos border
    \begin{tikzpicture}    
    \foreach \rows in {1,...,#1}
        \foreach \cols in {1,...,#2}
        {
            \pgfmathparse{mod(\rows+\cols,2) ? "white" : "black!30"}
            \edef\colour{\pgfmathresult}
            
            \filldraw[\colour] (\rows-1,\cols-1) rectangle ++ (1,1);
        }
        
    \foreach \x/\y in {#3}
        \node at (\y-0.5,\x-0.5) {\scalebox{2}{\symqueen}};
        
    \draw[ultra thick,#4] (0,0) rectangle (#1,#2);
    \end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}
[level distance=6cm,every node/.style={inner sep=0pt},
level 1/.style={sibling distance=8cm},
level 2/.style={sibling distance=5cm},
level 3/.style={sibling distance=5cm}
]
\node[dotted,draw] {node[draw] {X}}%\chessnode{4}{4}{}{black}}
    child[line width=6pt] {node[draw] {X}}% node{ \chessnode{4}{4}{4/1}{black} }
        child {node[draw] {X}}% node {\chessnode{4}{4}{4/1,3/3}{red!60}}}
%        child { node {\chessnode{4}{4}{4/1,3/4}{black}}
%            child { node {\chessnode{4}{4}{4/1,3/4,2/2}{red!60}}}
%        }
 %   }
%    child[line width=6pt] { node{ \chessnode{4}{4}{4/2}{black} }
%        child { node {\chessnode{4}{4}{4/2,3/4}{black}}
%            child { node {\chessnode{4}{4}{4/2,3/4,2/1}{black}}
%                child { node {\chessnode{4}{4}{4/2,3/4,2/1,1/3}{Green}}}
%            }
%        }
%    }
%    child[line width=6pt] { node{ \chessnode{4}{4}{4/3}{black} }
%        child { node {\chessnode{4}{4}{4/3,3/1}{black}}
%            child { node {\chessnode{4}{4}{4/3,3/1,2/4}{black}}
%                child { node {\chessnode{4}{4}{4/3,3/1,2/4,1/2}{Green}}}
%            }
%        }
%    }
%    child[line width=6pt] { node{ \chessnode{4}{4}{4/4}{black} }
%        child { node {\chessnode{4}{4}{4/4,3/1}{black}}
%            child { node {\chessnode{4}{4}{4/4,3/1,2/3}{red!60}}}
%        }
%        child { node {\chessnode{4}{4}{4/4,3/2}{red!60}}}
%    }
;
\end{tikzpicture}
\end{document}

Suggestions:

  • try getting a balanced (symmetric) tree design first
  • perhaps this will be easier to achieve via forest
  • some of your probles seem to arise from using line width >> 1pt

For the white queen on black fields:

  • looks like it's simply transparent, where it shouldn't be (you'd like a white fill of the figure(s))
3

A solution with \pic:

\documentclass{standalone}

\usepackage[x11names,dvipsnames,table]{xcolor}
\usepackage{tikz}
\usepackage{skak}
\usetikzlibrary {positioning,backgrounds}
\tikzset{
pics/chessnode/.style n args={4}{code={%
\begin{scope}[shift={(-#1/2,-#2/2)}]
    \foreach \rows in {1,...,#1}
    \foreach \cols in {1,...,#2}
    {
        \pgfmathparse{mod(\rows+\cols,2) ? "white" : "black!30"}
        \edef\colour{\pgfmathresult}
        
        \filldraw[\colour] (\rows-1,\cols-1) rectangle ++ (1,1);
    }
    
    \foreach \x/\y in {#3}
    \node at (\y-0.5,\x-0.5) {\scalebox{2}{\symqueen}};
    
    \draw[ultra thick,#4] (0,0) rectangle (#1,#2);
\end{scope}
 }}}

\begin{document}
\begin{tikzpicture}
\def\cbw{4cm} %chessboard width
\def\ld{6cm} %level distance
\def\sd{8cm} %sibling distance 1
\def\sdd{5cm} %sibling distance 2
\def\sddd{5cm} %sibling distance 3

\coordinate (l0) at (0,0);

\coordinate[below right=\ld and -1.5*\sd of l0] (l0-1); 
\coordinate[below right=\ld and -0.5*\sd of l0] (l0-2);
\coordinate[below right=\ld and 0.5*\sd of l0] (l0-3);
\coordinate[below right=\ld and 1.5*\sd of l0] (l0-4);

\coordinate[below right=\ld and -0.5*\sdd of l0-1] (l0-1-1);
\coordinate[below right=\ld and 0.5*\sdd of l0-1] (l0-1-2);
\coordinate[below=\ld of l0-2] (l0-2-1);
\coordinate[below=\ld of l0-3] (l0-3-1);
\coordinate[below right=\ld and -0.5*\sdd of l0-4] (l0-4-1);
\coordinate[below right=\ld and 0.5*\sdd of l0-4] (l0-4-2);

\coordinate[below=\ld of l0-1-2] (l0-1-2-1);
\coordinate[below=\ld of l0-2-1] (l0-2-1-1);
\coordinate[below=\ld of l0-3-1] (l0-3-1-1);
\coordinate[below=\ld of l0-4-1] (l0-4-1-1);

\node[below=\ld of l0-2-1-1] (l0-2-1-1-1) {r11};
\node[below=\ld of l0-3-1-1] (l0-3-1-1-1) {r12};
%chessboards
\pic at (l0){chessnode={4}{4}{}{black}};

\pic at (l0-1){chessnode={4}{4}{4/1}{black}};
\pic at (l0-2){chessnode={4}{4}{4/2}{black}};
\pic at (l0-3){chessnode={4}{4}{4/3}{black}};
\pic at (l0-4){chessnode={4}{4}{4/4}{black}};

\pic at (l0-1-1){chessnode={4}{4}{4/1,3/3}{red!60}};
\pic at (l0-1-2){chessnode={4}{4}{4/1,3/4}{black}};
\pic at (l0-2-1){chessnode={4}{4}{4/2,3/4}{black}};
\pic at (l0-3-1){chessnode={4}{4}{4/3,3/1}{black}};
\pic at (l0-4-1){chessnode={4}{4}{4/4,3/1}{black}};
\pic at (l0-4-2){chessnode={4}{4}{4/4,3/2}{red!60}};

\pic at (l0-1-2-1){chessnode={4}{4}{4/1,3/4,2/2}{red!60}};
\pic at (l0-2-1-1){chessnode={4}{4}{4/2,3/4,2/1}{black}};
\pic at (l0-3-1-1){chessnode={4}{4}{4/3,3/1,2/4}{black}};
\pic at (l0-4-1-1){chessnode={4}{4}{4/4,3/1,2/3}{red!60}};

\pic at (l0-2-1-1-1){chessnode={4}{4}{4/2,3/4,2/1,1/3}{black}};
\pic at (l0-3-1-1-1){chessnode={4}{4}{4/3,3/1,2/4,1/2}{black}};
%connections
\begin{scope}[on background layer]
\draw[line width=6pt]   (l0) -- (l0-1) 
                        (l0) -- (l0-2) 
                        (l0) -- (l0-3) 
                        (l0) -- (l0-4);
\draw[line width=6pt]   (l0-1) -- (l0-1-1) 
                        (l0-1) -- (l0-1-2) 
                        (l0-2) -- (l0-2-1)
                        (l0-3) -- (l0-3-1)
                        (l0-4) -- (l0-4-1) 
                        (l0-4) -- (l0-4-2);
\draw[line width=6pt]   (l0-1-2) -- (l0-1-2-1)
                        (l0-2-1) -- (l0-2-1-1)
                        (l0-3-1) -- (l0-3-1-1)
                        (l0-4-1) -- (l0-4-1-1);
\draw[line width=6pt]   (l0-2-1-1) -- (l0-2-1-1-1)
                        (l0-3-1-1) -- (l0-3-1-1-1);
\end{scope}
\end{tikzpicture}
\end{document}
1
  • Nice. Can you please add a screenshot? Thank you Commented Oct 27, 2024 at 17:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.