TikZ:在子节点的边界框处切割箭头

TikZ:在子节点的边界框处切割箭头

我正在尝试使用以下代码在 TikZ 中创建树形图

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[
    start/.style={circle, minimum size=18.5mm, minimum size=18.5mm, fill=green!50, draw=black, text centered, anchor=north},
    part/.style={rectangle, rounded corners=1mm, fill=gray!10, minimum size=6mm, draw=black, text centered, anchor=north},
    node/.style={circle, minimum size=18.5mm, fill=blue!9, draw=black, text centered, anchor=north},
    growth parent anchor=south, 
    level 1/.style={sibling distance=2cm, level distance = 1.5cm},
    level 2/.style={sibling distance=2cm, level distance = 0.1cm}, 
]

%draw tree
\node (start) [start] {\textbf{Start}} [-]
    child{node (part1) [part] {Part1}                 
        child{node (node1) [node] {1}
             }
         } 
    child{node (part2) [part] {Part2}                 
        child{node (node2) [node] {2}
             }                  
         };

%draw line and arrow
\draw[line width=5pt, color = blue!60, line cap=butt] (node1) to (part1);    
\draw[-{triangle 45},line width=5pt, color = blue!60, line cap=butt] (part1.center) to (start); 
\end{tikzpicture}
\end{document}

我得到的是:

在此处输入图片描述

我尝试使用 backgrounds-tikzlibrary 将箭头放在背景中。

\usetikzlibrary{arrows, backgrounds}
\begin{scope}[on background layer]     
\draw[-{triangle 45},line width=5pt, color = blue!60, line cap=butt] (part1.center) to (start); 
\end{scope}

这导致

在此处输入图片描述

有人能帮我得到一个结果吗,让箭头在边界框处切割(像第二种解决方案)但覆盖黑线(像第一个)?

谢谢,尼科

答案1

为了使 执行on background layer您想要的操作,您只需添加draw=none到相关的选项中edge from parent。在此示例中,我还用arrows当前库替换了已弃用的库arrows.meta,并适当调整了语法。

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{arrows.meta,backgrounds}
\begin{document}
\begin{tikzpicture}
  [
    start/.style={circle, minimum size=18.5mm, minimum size=18.5mm, fill=green!50, draw=black, text centered, anchor=north},
    part/.style={rectangle, rounded corners=1mm, fill=gray!10, minimum size=6mm, draw=black, text centered, anchor=north},
    node/.style={circle, minimum size=18.5mm, fill=blue!9, draw=black, text centered, anchor=north},
    growth parent anchor=south,
    level 1/.style={sibling distance=2cm, level distance = 1.5cm},
    level 2/.style={sibling distance=2cm, level distance = 0.1cm},
  ]

  %draw tree
  \node (start) [start] {\textbf{Start}} [-]
   child{node (part1) [part] {Part1}
    child{node (node1) [node] {1}
    }
    edge from parent [draw=none]
  }
  child{node (part2) [part] {Part2}
    child{node (node2) [node] {2}
    }
  };

  %draw line and arrow
  \draw[line width=5pt, color = blue!60] (node1) to (part1);
  \scoped[on background layer]{\draw[-{Triangle[]},line width=5pt, color = blue!60] (part1.center) to (start);}
\end{tikzpicture}
\end{document}

调整树

相关内容