2

I want to make a tree structure by tikz. I am not a professional latex user and I am just trying to combine examples to make it work but first I would like that the edges have direction and then T 3 and T 4 overlap and then I would like to be able to make the space between nodes in each layer being more flexible to change. This in my code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{shapes,arrows,calc}
\usetikzlibrary{graphs}
\tikzstyle{line} = [draw, -latex']
\begin{document}
\begin{tikzpicture}[sibling distance=10em,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=white!20}]]
  \node {P}
    child { node (ch1){char1} 
        child { node {T 1}
        child { node {comment} } }
        child { node {T 2}
        child { node {comment} } }
        child { node {T 3}
        child { node {comment} } } node [left=2cm of char 1] (s1){script 1}}
    child { node (ch2){char 2} 
      child { node {T 2}
        child { node {comment} } }
        child { node {T 4}
        child { node {comment} } }
        child { node {T 5}
        child { node {comment} } } node [right=2cm of char 2] (s2){script 2}};
   %Draw edges
   \path [line] (s2) -- node {}(ch2);
   \path [line] (s1) -- node {}(ch1);
\end{tikzpicture}

\end{document}

There is a circle created on the edges between char 1 and script 1. I can not figure why? Any suggestion would be appreciate!!

  • Also, there are many packages, which allow you to construct trees which are easier to use. – Viktor Glombik Feb 5 '19 at 22:25
  • @ViktorGlombik If you are referring to forest, I agree, but giving a list of packages you have in mind might help here. – user121799 Feb 5 '19 at 23:06
5
  1. The circles appear because you have node{} in \path [line] (s2) -- node {}(ch2);
  2. You can add arrows by saying edge from parent/.style={draw,-latex'}.
  3. You were loading graphdrawing but it is not obvious to me you were using it.
  4. left=2cm of char 1 should probably be left=2cm of ch1.
  5. You can change the dimensions/distances at any level using e.g. level 1/.style={sibling distance=50mm},level 2/.style={sibling distance=15mm},level 3/.style={text width=1cm,font=\tiny}.
  6. To debug you may temporarily set opacity=0.5 to better understand where some nodes went.
  7. The rest of your statements I do not understand. Could you perhaps consider making them clearer?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,calc,positioning}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}[sibling distance=10em,%opacity=0.5,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=white!20},
    line/.style={draw, -latex'},
    edge from parent/.style={draw,-latex'},
    level 1/.style={sibling distance=50mm}, 
    level 2/.style={sibling distance=15mm},
    level 3/.style={text width=1cm,font=\tiny}]
  \node {P}
    child { node (ch1){char1} 
        child { node {T 1}
        child { node {comment} } }
        child { node {T 2}
        child { node {comment} } }
        child { node {T 3}
        child { node {comment} } } node [left=2cm of ch1] (s1){script 1}}
    child { node (ch2){char 2} 
      child { node {T 2}
        child { node {comment} } }
        child { node {T 4}
        child { node {comment} } }
        child { node {T 5}
        child { node {comment} } } node [right=2cm of ch2] (s2){script 2}};
   %Draw edges
   \path [line] (s2) -- (ch2);
   \path [line] (s1) -- (ch1);
\end{tikzpicture}
\end{document}

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{shapes,arrows,calc}
\usetikzlibrary{graphs}
\tikzstyle{line} = [draw, -latex']
\begin{document}
\begin{tikzpicture}[sibling distance=10em,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=white!20}]]
  \node {P}
    child { node (ch1){char1} 
        child { node {T 1}
        child { node {comment} } }
        child { node {T 2}
        child { node {comment} } }
        child { node {T 3}
        child { node {comment} } } node [left=2cm of char 1] (s1){script 1}}
    child { node (ch2){char 2} 
      child { node {T 2}
        child { node {comment} } }
        child { node {T 4}
        child { node {comment} } }
        child { node {T 5}
        child { node {comment} } } node [right=2cm of char 2] (s2){script 2}};
   %Draw edges
   \path [line] (s2) -- node {}(ch2);
   \path [line] (s1) -- node {}(ch1);
\end{tikzpicture}

\end{document}

enter image description here

I am confused by your comments. Something like this?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,calc,positioning}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}[sibling distance=10em,%opacity=0.5,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=white!20},
    line/.style={draw, -latex'},
    edge from parent/.style={draw,-latex'},
    level 1/.style={sibling distance=30mm}, 
    level 2/.style={sibling distance=15mm},
    level 3/.style={text width=1cm,font=\tiny}]
  \node {P}
    child { node (ch1){char1} 
        child { node {T 1}
        child { node {comment} } }
        child { node {T 3}
        child { node {comment} } }
        child { node {T 2}
        child { node {comment} } } node [left=2cm of ch1] (s1){script 1}}
    child { node (ch2){char 2} 
        child { node {T 2}
        child { node {comment} } }
        child { node {T 4}
        child { node {comment} } }
        child { node {T 5}
        child { node {comment} } } node [right=2cm of ch2] (s2){script 2}};
   %Draw edges
   \path [line] (s2) -- (ch2);
   \path [line] (s1) -- (ch1);
\end{tikzpicture}
\end{document}

enter image description here

Note that this is just to clarify your request, and not a proposal for really do this. (And this is not a tree.)

| improve this answer | |
  • Thanks for the answer but I still do not see the leave corresponding to T 3. where is it? – Dalek Feb 5 '19 at 22:52
  • @Dalek It is where you put it: under T4. You can check this by adding opacity=0.5 to the options of \begin{tikzpicture}. May I ask where you want to have it? – user121799 Feb 5 '19 at 22:55
  • but as I said in my question I want to be able to play with the distances of leaves from each other in order to avoid this overlap. How can I include it in my code? – Dalek Feb 5 '19 at 22:57
  • @Dalek I edited my answer accordingly. (Your question contains a sentence that runs over four lines, I apologize if I missed some things when trying to parse it.) – user121799 Feb 5 '19 at 23:03
  • @mamot Thanks. Is it possible in the case of shared lower level node, both the higher level char1 and char2 be connected to the same node for instance, T2 instead of having two nodes? – Dalek Feb 5 '19 at 23:11

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.