如何使用 tikzpicture 和棋盘在嵌套节点之间正确绘制线条

如何使用 tikzpicture 和棋盘在嵌套节点之间正确绘制线条

我正在尝试创建一个状态树来演示 N-Queens 问题。我已成功创建了一棵树并显示了不同状态下的棋盘,但嵌套节点之间没有间隙,并且线条绘制在棋盘上。我尝试增加节点大小和节点距离,但这似乎没有效果。

这是我目前的乳胶:

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

输出结果如下:

在此处输入图片描述

另外,我真的需要所有这些包来创建棋盘吗?我正在使用 TexPad 来编辑它,当我使用它们时它将不再实时编译。

\usepackage[T1]{fontenc}
\usepackage{chessfss}
\usepackage{xkeyval}
\usepackage{xifthen}
\usepackage{pgfcore}
\usepackage{pgfbaseshapes}
\usepackage{pst-node}
\usepackage{chessboard}

答案1

首先,right=of ...是正确的,这是库支持的语法positioning。要回答您的问题,您可以使用sibling distancelevel distance键来增加间隙。我还使用shorten >shorten <来调整连接边缘。

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

在此处输入图片描述

相关内容