将森林树叶子的边标记为自由节点

将森林树叶子的边标记为自由节点

我当前的代码如下,直接从森林包文档样式制定。

\documentclass{article} 
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={circle,draw, s sep=1cm, l sep=15mm, anchor=north west}
[A
    [B,edge label={node[midway,left, xshift=2 mm, fill=white] {1}} 
      [D,name=leaf1,edge label={node[midway,left,xshift=2 mm, fill=white] {5}} ] 
      [E,name=leaf2,edge label={node[midway,left,xshift=2 mm, fill=white] {7}} ] 
      [F,name=leaf3,edge label={node[midway,left,xshift=3 mm, fill=white] {12}} ]
    ]
    [C,edge label={node[midway,left,xshift=3 mm, fill=white] {27}}
      [H,name=leaf4,edge label={node[midway,left,xshift=2 mm, fill=white] {8}} ] 
      [I,name=leaf5,edge label={node[midway,left,xshift=2 mm, fill=white] {2}} ] 
      [J,name=leaf6,edge label={node[midway,left,xshift=2 mm, fill=white] {9}} ]
    ] 
]
\node at (current bounding box.south)
   [below=15mm,draw,circle, name=Target]
   {T};
\draw[-] (leaf1) to (Target);
\draw[-] (leaf2) to (Target);
\draw[-] (leaf3) to (Target);
\draw[-] (leaf4) to (Target);
\draw[-] (leaf5) to (Target);
\draw[-] (leaf6) to (Target);
\end{forest}
\end{document}

我想要做的是用 6 个绘图命令生成的边缘进行与上述标签类似的标记。

答案1

很晚了...但这个问题并没有留下悬而未决的我详细说明成本加运费稍作修改的评论:

  • forest对于使用tikz库外部的行的标签quotes
  • 为树中的标签定义了新样式EL(如边缘标签),这使得标签书写更短

    \documentclass{article}
    \usepackage{forest}
    \usetikzlibrary{positioning, quotes}
    
    \begin{document}
    \begin{forest}
    for tree = {circle, draw,
                s sep+ = 6mm,
                l sep+ = 12mm,
             EL/.style = {edge label={node[midway, fill=white, inner sep=2pt,
                                            anchor=center]{#1}},},
              }
    [A
        [B,EL=1
          [D,name=leaf1,EL=5 ]
          [E,name=leaf2,EL=7 ]
          [F,name=leaf3,EL=12 ]
        ]
        [C,EL=27
          [H,name=leaf4,EL=8 ]
          [I,name=leaf5,EL=2 ]
          [J,name=leaf6,EL=9 ]
        ]
    ]
    \tikzset{every edge quotes/.style = {fill=white, inner sep=2pt}}
    \node[below=20mm of current bounding box.south,
          circle, draw, name=Target] {T};
    \draw   (leaf1) edge ["A"] (Target)
            (leaf2) edge ["B"] (Target)
            (leaf3) edge ["C"] (Target)
            (leaf4) edge ["D"] (Target)
            (leaf5) edge ["E"] (Target)
            (leaf6) edge ["F"] (Target);
    \end{forest}
    \end{document}
    

在此处输入图片描述

相关内容