tikz:节点与图上的边重叠

tikz:节点与图上的边重叠

我正在尝试使用 Tikz 重现论文中的图表,我尝试重现的图表如下:

我想要的是

但没有左子树上的水平曲线和括号。到目前为止,我得到的结果如下:

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}

\tikzset{
    treenode/.style = {align=center, inner sep=0pt, text centered,
        font=\sffamily\Large\bfseries, text width=5cm},
    burned/.style = {treenode, circle, white, draw=red, fill=red, text width=1.5em, very thick},
    saved/.style = {treenode, circle, white, draw=green, fill=green, text width=1.5em, very thick},
    untouch/.style = {treenode, circle, white, draw=gray, fill=gray, text width=1.5em, very thick}
}

\begin{document}

\begin{tikzpicture}[auto, node distance=2cm, thick]
% nodes
\node[burned](1){F};
    \node[saved](2) [below left of=1] {S};
    \node[untouch](3) [below left of=2] {};
    \node[untouch](4) [below right of=2] {};
    \node[burned](5) [below of=1] {B};
    \node[burned](6) [below right of=1] {B};
    \node[saved](7) [below of=5] {S};
    \node[burned](8) [below of=6] {B};
    \node[untouch](9) [below of=7] {};
    \node[saved](10) [below of=8] {S};
    \node[untouch](11) [below of=9] {};
    \node[untouch](12) [below of=11] {};
    \node[untouch](13) [below of=10] {};
    \node[untouch](14) [below of=13] {};

%edges
\path
    (1) edge node [right] {} (2)
        edge node [below] {} (5)
        edge node [left] {} (6)
    (2) edge node [right] {} (3)
        edge node [right] {} (4)
    (5) edge node [below] {} (7)
        edge node [left] {} (8)
    (7) edge node [below] {} (9)
    (9) edge node [below] {} (11)
    (11) edge node [below] {} (12)
    (6) edge node [right] {} (7)
        edge node [below] {} (8)
    (8) edge node [below] {} (10)
    (10) edge node [below] {} (13)
    (13) edge node [below] {} (14);
\end{tikzpicture}

\end{document}

生成以下图表:

我得到了什么

所以,基本上我有三个问题:

  • 有什么方法可以避免边和节点之间的重叠?
  • 另外,有没有什么办法可以让同一级别的节点对齐?(就像论文中的图表一样)
  • 是否有其他 LaTex 包或 tikz 库具有更直观的语法来绘制图形(即带有边/节点的图形)?也许 Graphviz 点语法类似。

答案1

可能有人会建议使用forest这种图表。这将是一个很好的建议。但在等待它的同时,您可以开始使用 positioning库及其=of(而不是of=)语法。

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
    treenode/.style = {align=center, inner sep=0pt, text centered,
        font=\sffamily\Large\bfseries, text width=5cm},
    burned/.style = {treenode, circle, white, draw=red, fill=red, text width=1.5em, very thick,sibling distance=30mm},
    saved/.style = {treenode, circle, white, draw=green, fill=green, text width=1.5em, very thick,sibling distance=15mm},
    untouch/.style = {treenode, circle, white, draw=gray, fill=gray, text width=1.5em, very thick,sibling distance=7.5mm}
}

\begin{document}

\begin{tikzpicture}[auto, thick, on grid, ]
% nodes
\node[burned](1){F};
    \node[saved](2) [below left = 1 and 2 of 1] {S};
    \node[untouch](3) [below left = 1 and 1 of 2] {};
    \node[untouch](4) [below right = 1 and 1 of 2] {};
    \node[burned](5) [below = 1 of 1] {B};
    \node[burned](6) [below right = 1 and 2 of 1] {B};
    \node[saved](7) [below = of 5] {S};
    \node[burned](8) [below = of 6] {B};
    \node[untouch](9) [below = of 7] {};
    \node[saved](10) [below = of 8] {S};
    \node[untouch](11) [below = of 9] {};
    \node[untouch](12) [below =of 11] {};
    \node[untouch](13) [below = of 10] {};
    \node[untouch](14) [below = of 13] {};

%edges
\path
    (1) edge node [right] {} (2)
        edge node [below] {} (5)
        edge node [left] {} (6)
    (2) edge node [right] {} (3)
        edge node [right] {} (4)
    (5) edge node [below] {} (7)
        edge node [left] {} (8)
    (7) edge node [below] {} (9)
    (9) edge node [below] {} (11)
    (11) edge node [below] {} (12)
    (6) edge node [right] {} (7)
        edge node [below] {} (8)
    (8) edge node [below] {} (10)
    (10) edge node [below] {} (13)
    (13) edge node [below] {} (14);
\end{tikzpicture}

\end{document}

在此处输入图片描述

更新:forest版本。

\documentclass[a4paper,12pt]{article}
\usepackage{forest}

\tikzset{
    treenode/.style = {align=center, inner sep=0pt, text centered,
        font=\sffamily\Large\bfseries, text width=5cm},
    burned/.style = {treenode, circle, white, draw=red, fill=red, text width=1.5em, very thick,sibling distance=30mm},
    saved/.style = {treenode, circle, white, draw=green, fill=green, text width=1.5em, very thick,sibling distance=15mm},
    untouch/.style = {treenode, circle, white, draw=gray, fill=gray, text width=1.5em, very thick,sibling distance=7.5mm}
}

\begin{document}

\begin{forest}
[F,burned
    [S, saved, , for descendants={untouch}
        [][]]
    [B, burned, name=B1
        [S, saved, name=S1, for descendants={untouch} [[[]]]]]
    [B, burned, name=B2
        [B, burned, name=B3 [S, saved, , for descendants={untouch} [[]]]]]]
    \draw (B1)--(B3) (B2)--(S1);
\end{forest}
\end{document}

在此处输入图片描述

相关内容