3

I want to (1) draw an arrow from child to parent [illustration in green color on the graph], (2) draw and outer arrow with text on it as shown in the figure below [illustration in red color].

enter image description here Here is my code:

\documentclass{article} 
\usepackage{tikz-qtree}

\begin{document}

\begin{figure}[!h]
\centering
\scalebox{0.9}{
\begin{tikzpicture}
[every tree node/.style={draw,circle},
   level distance=1.25cm,sibling distance=.5cm, 
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node[label=right:n,red] {nn}; 
    [.\node[label=left:{$C_{O5}=C_{O1}*C_{O2}*C_{P5}$}] {5};         
      [.\node[label=left:{$C_{O1}=C_{P1}$}] {1}; ] [.\node[label=right:{$C_{O2}=C_{P2}$}] {2}; ] 
    ]
    [.\node[label=right:{$C_{O6}=C_{O3}*C_{O4}*C_{P6}$}] {6};
      [.\node[label=left:{$C_{O3}=C_{P3}$}] {3}; ] [.\node[label=right:{$C_{O4}=C_{P4}$}] {4}; ] 
    ] ]
\end{tikzpicture}
}
\end{figure}

\end{document}
4
  • 3
    edge from parent path={[<-,line width=2pt,green!50!black](\tikzparentnode) -- (\tikzchildnode)}
    – Salim Bou
    Jun 3, 2017 at 20:00
  • Do you really want to draw over the equations to the left of the nodes?
    – cfr
    Jun 3, 2017 at 21:29
  • Thank you Salim, but it does not work. Jun 4, 2017 at 9:54
  • than you cfr, no actually what you did is perfect Jun 4, 2017 at 9:55

1 Answer 1

3

You already have an answer for tikz-qtree in the comments, but here's an alternative using Forest, which I would use here because it is a tree.

By default, setting a tree with much larger labels and node content is problematic, because Forest ignores the labels when packing the tree. However, if we put the labels into more nodes, then we don't have to worry about setting the sibling separation manually to make room for them, as Forest will take care of the spacing for us.

Despite your image, I've avoided drawing the red arrow over the labelling, but you can adjust the placement if you really want that.

\documentclass{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{forest}
  for tree={
    draw,
    circle,
    edge+={Stealth-, green!75!black, thick},
  },
  before typesetting nodes={
    for tree={
      split option={content}{:}{content,label me},
    },
  },
  label me/.style={
    if level=0{
      label=right:{$#1$},
    }{
      if n'=1{
        insert after={[{#1}, math content, no edge]},
      }{
        insert before={[{#1}, math content, no edge]},
      },
    },
  },
  tikz+={
    \draw [thick, red!75!black, -Stealth] (!r22.south) ++(-15pt,0) -- ([shift=(135:2.5pt)]!r2.north west) -- ([shift=(135:2.5pt)]!r.north west) node [midway, sloped, above] {text here};
  },
  [nn:n, red
    [5:{C_{O5}=C_{O1}*C_{O2}*C_{P5}}
      [1:{C_{O1}=C_{P1}}]
      [2:{C_{O2}=C_{P2}}]
    ]
    [6:{C_{O6}=C_{O3}*C_{O4}*C_{P6}}
      [3:{C_{O3}=C_{P3}}]
      [4:{C_{O4}=C_{P4}}]
    ]
  ]
\end{forest}
\end{document}

Forest solution

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.