3

I'm trying to create a tree-diagram in TikZ using the following code

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

and what I get is this:

enter image description here

I tried to put the arrow in the background with the 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}

This results in

enter image description here

Can anyone help me to get a result that cuts the arrow at the bouding box (like the second solution) but overlays the black line (like the first one)?

Thanks, Nico

  • Can you tell it not to draw an edge there when drawing the tree? (The equivalent of Forest's no edge.) – cfr May 31 '16 at 20:37
2

In order to make the on background layer do what you want, you just need to add draw=none to the options for the relevant edge from parent. In this example, I've also replaced the deprecated arrows library with the current arrows.meta and adjusted the syntax appropriately.

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

adjusted tree

  • For this (minimal) example, your answer works very well, so thank you for that. I should have mentioned that my real diagram has got much more elements (about 200) and therefore it would take a great manual effort to implement your solution. I hoped for a more generic approach, e.g. changing global options in the tikzset, avoiding the manual switch-off of edges. – Nico Jun 1 '16 at 13:36
  • @Nico Well, how would TikZ know when to switch the edges off, given that the arrows are not drawn until later? You could do it with Forest more easily, perhaps, but only if there's an algorithm for deciding when to do what. As presented in your minimal example, it is impossible to tell whether this is the case or not since there is only one case of switching off. – cfr Jun 1 '16 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.