tikz 中的树和标签问题

tikz 中的树和标签问题

我想在树的边缘放置一个标签,使其打破边缘。我尝试了一些愚蠢的方法,将背景设置为白色,但随后标签覆盖了其他附近的边缘:

例子

另外,我似乎有一些节点的标签比 2 位数字大。我怎样才能使所有内容统一?

这是我的 MWE:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{shapes.geometric}
\begin{document}
    \begin{tikzpicture}[level/.style={sibling distance=15mm/#1}]
        \node [circle,draw] {0}
        child[very thick,red] {node [thin, black,circle,draw] {$1$} edge from parent node [fill=white] {$s_1$}}
        child[very thick,blue] {node [thin, black,circle,draw] {$2$}
            child[very thick, red] {node [thin, black,circle,draw] {$3$}}
            child[very thick, blue] {node [thin, black,circle,draw] {$4$}}
            child[very thick, orange] {node [thin, black,circle,draw] {$8$}}
            edge from parent node [fill=white] {$s_2$} }
        child[very thick,orange] {node [thin, black,circle,draw] {$6$}}
        child[very thick,orange] {node [thin, black,circle,draw] {$7$}}
        child[very thick,blue] {node [thin, black,circle,draw] {$11$}
            child[very thick, red] {node [thin, black,circle,draw] {$10$}}
            child[very thick, blue] {node [thin, black,circle,draw] {$9$}}
            child[very thick, orange] {node [thin, black,circle,draw] {$5$}}
        }
        child[very thick,red] {node [thin, black,circle,draw] {$12$}};
    \end{tikzpicture}
\end{document}

答案1

使用该forest包后树代码更短:

\documentclass{article}
\usepackage{forest}

\begin{document}
\forestset{
  EL/.style={% abbreviation for Edge Label
             edge label={node[midway, font=\small, text=black,
                         fill=white, inner sep=2pt]{$#1$}} 
             }
          }% end of forestset
  
\begin{forest}
for tree = {
% node style
    circle,
    draw, 
    minimum size = 1.5em,
    inner sep=0pt,
    math content,
% tree style
    edge={very thick},
    s sep=3mm,
    l sep=12mm,
           }
[0,
    [1, edge=red, EL=s_1]
    [2, edge=blue,EL=s_2,fit=band
        [3, edge=orange]
        [4, edge=blue]
        [8, edge=orange]
    ]
    [6,edge=orange]
    [7,edge=orange]
    [11,edge=blue,fit=band
        [10,edge=red]
        [9, edge=blue]
        [5, edge=orange]
    ]
    [12, edge=red]
]
    \end{forest}
\end{document}

在此处输入图片描述

附录(1): 如果你只想增加节点“6”和“7”之间的距离,最简单的方法是在它们之间插入幻影节点:

\documentclass{article}
\usepackage{forest}

\begin{document}
\forestset{
  EL/.style={% shortens for (my) edge label
             edge label={node[midway, font=\small, text=black,
                         fill=white, inner sep=2pt]{$#1$}}
             }
          }% end of forestset

\begin{forest}
for tree = {
% node style
    circle,
    draw,
    minimum size = 1.5em,
    inner sep=0pt,
    math content,
% tree style
    edge={very thick},
    s sep=3mm,
    l sep=12mm,
           }
[0,
    [1, edge=red, EL=s_1]
    [2, edge=blue,EL=s_2, fit=band
        [3, edge=orange]
        [4, edge=blue]
        [8, edge=orange]
    ]
    [6,edge=orange]
    [, phantom, no edge]  % <---
    [7,edge=orange]
    [11,edge=blue,fit=band
        [10,edge=red]
        [9, edge=blue]
        [5, edge=orange]
    ]
    [12, edge=red]
]
    \end{forest}
\end{document}

在此处输入图片描述

附录(2): 如果您希望树的图像更窄,您可以设置:

  • s sep=1mms
  • 删除fit=band树底层的选项
  • 将边缘标签的字体大小减小至font=\footnotesize
  • 将边缘标签位置推低,用midway替换pos=0.65
  • font=\small通过在\for tree设置中添加选项来减少树节点的字体大小
  • 将节点大小减小到`最小大小=1.2em,新的 MWE 为:
\documentclass{article}
\usepackage{forest}

\begin{document}
\forestset{
  EL/.style={% abbreviation for Edge Label
             edge label={node[pos=0.65, font=\footnotesize, text=black,
                         fill=white, inner sep=2pt]{$#1$}}
             }
          }% end of forestset

\begin{forest}
for tree = {
% node style
    circle,
    draw,
    font=\small,
    minimum size = 1.2em,
    inner sep=0pt,
    math content,
% tree style
    edge={very thick},
    s sep=2mm,
    l sep=12mm,
           }
[0,
    [1, edge=red, EL=s_1]
    [2, edge=blue,EL=s_2,
        [3, edge=orange]
        [4, edge=blue]
        [8, edge=orange]
    ]
    [6,edge=orange]
    [7,edge=orange]
    [11,edge=blue,
        [10,edge=red]
        [9, edge=blue]
        [5, edge=orange]
    ]
    [12, edge=red]
]
    \end{forest}
\end{document}

其生产成果为:

在此处输入图片描述

答案2

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{shapes.geometric}
\begin{document}
    \begin{tikzpicture}[
        every node/.style={% standard style for all nodes no repetition required every time
            thin, 
            black,
            draw,
            circle,
            inner sep=1pt,% space around the text in the circle
            text width=10pt,%uniform circles minimum size
            text centered
        },
    level/.style={
        sibling distance=15mm/#1
    },
        level distance=5em% can vary to suit 
]
        \node [] {0}
        child[very thick,red] {node [] {$1$} edge from parent node [draw=none,fill=white] {$s_1$}}
        child[very thick,blue] {node [] {$2$}
            child[very thick, red] {node [] {$3$}}
            child[very thick, blue] {node [] {$4$}}
            child[very thick, orange] {node [] {$8$}}
            edge from parent node [fill=white,draw=none] {$s_2$} }
        child[very thick,orange] {node [] {$6$}}
        child[very thick,orange] {node [] {$7$}}
        child[very thick,blue] {node [] {$11$}
            child[very thick, red] {node [] {$10$}}
            child[very thick, blue] {node [] {$9$}}
            child[very thick, orange] {node [] {$5$}}
        }
        child[very thick,red] {node [] {$12$}};
    \end{tikzpicture}
\end{document}

相关内容