2

I am trying to create a states tree for demonstrating the N-Queens problem. I've managed to create a tree and display the chessboards in various states however there there is no gap between the nested nodes and the lines are drawn over the chessboard. I attempted to increase node size and node distance but this doesn't seem to be having an effect.

Here is my current latex for this:

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage{chessfss}
\usepackage{xkeyval}
\usepackage{xifthen}
\usepackage{pgfcore}
\usepackage{pgfbaseshapes}
\usepackage{pst-node}
\usepackage{chessboard}
\storechessboardstyle{4x4}{maxfield=d4}
\begin{document}

\begin{figure}[h]
    \centering
    \begin{tikzpicture}[node distance={10mm}, thick, main/.style = minimum size=1em]
        \node (1) {\chessboard[tinyboard,style=4x4,setwhite={Qa1},showmover=false]}
            child {node {\chessboard[tinyboard,style=4x4,setwhite={Qa1,Qb1},showmover=false]}}
            child {node {\chessboard[tinyboard,style=4x4,setwhite={Qa1,Qb2},showmover=false]}
                child {node {\chessboard[tinyboard,style=4x4,setwhite={Qa1,Qb2,Qc1},showmover=false]}}
                child {node {\chessboard[tinyboard,style=4x4,setwhite={Qa1,Qb2,Qc2},showmover=false]}}
            }
            child {node {$\dots$}
        };
        \node (2) [right=of 1] {\chessboard[tinyboard,style=4x4,setwhite={Qb1},showmover=false]}
            child {node {$\dots$}
        };
        \node  [right=of 2] {$\dots$};
    \end{tikzpicture}
\end{figure}

\end{document}

Which results in the following output:

enter image description here

Also as an aside, do I really need all of these packages to create a chessboard? I'm using TexPad to edit this and it will no longer live compile while I'm using them all.

\usepackage[T1]{fontenc}
\usepackage{chessfss}
\usepackage{xkeyval}
\usepackage{xifthen}
\usepackage{pgfcore}
\usepackage{pgfbaseshapes}
\usepackage{pst-node}
\usepackage{chessboard}
7
  • regarding the asside: fontenc is unrelated (and you should load it) but from all the rest you only need chessboard, and you should load tikz. Nov 27, 2021 at 10:51
  • I'm not that familiar with trees, but it seems that TikZ is totally ignoring the node sizes when placing the children. The fact that they do not overlap is pure coincidence. In fact, they do overlap since they have rather large white space borders. Nov 27, 2021 at 16:46
  • @UlrikeFischer Thanks for replying. I get the errors "File `pst-node.sty' not found" and "Emergency stop. (\fi ...)" in chessboard.sty when I remove the other packages. Adding back pst-node seems to resolve this but then I can no longer use live updates, which seems a bit odd.
    – dan-frank
    Nov 27, 2021 at 17:08
  • 1
    Unrelated to the question: The main difference between right=of and right of= is that the latter is deprecated and distances are calculated between node centers. With right=of (which is recommended), the distance is by default calculated between the edges of the nodes. Adding on grid means the distance is between node centers, if you need that. Nov 27, 2021 at 17:30
  • 1
    I was was missing \usetikzlibrary{positioning} and was getting an error with right=of. Nov 28, 2021 at 14:31

1 Answer 1

3

First off, right=of ... is correct, this is the syntax supported by the positioning library. To answer your question, you can use the sibling distance and the level distance keys to increase the gaps. I also played with shorten > and shorten < to adjust the connecting edges.

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{chessboard}
\storechessboardstyle{4x4}{maxfield=d4}
\begin{document}

\begin{figure}[h]
    \centering
    \begin{tikzpicture}[sibling distance=25mm, level distance=25mm,
        thick,
        main/.style ={minimum size=1em}]        
        \path[shorten >=-1ex,shorten <=-1ex]
            node (1) {\chessboard[tinyboard,style=4x4,setwhite={Qa1},showmover=false]}
            child {node {\chessboard[tinyboard,style=4x4,setwhite={Qa1,Qb1},showmover=false]}}
            child {node {\chessboard[tinyboard,style=4x4,setwhite={Qa1,Qb2},showmover=false]}
                child {node {\chessboard[tinyboard,style=4x4,setwhite={Qa1,Qb2,Qc1},showmover=false]}}
                child {node {\chessboard[tinyboard,style=4x4,setwhite={Qa1,Qb2,Qc2},showmover=false]}}
            }
            child[shorten >=1ex,shorten <=1ex]  {node {$\dots$}
        };
        \path[shorten >=1ex,shorten <=1ex]
            node (2) [right=of 1] {\chessboard[tinyboard,style=4x4,setwhite={Qb1},showmover=false]}
            child {node {$\dots$}
        };
        \node  [right=of 2] {$\dots$};
    \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

1
  • Thank you! This is exactly what I was aiming for.
    – dan-frank
    Nov 27, 2021 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.