5

I am trying to make a simple tree using Tikz, but I have some problems with the right position (level) of the right side of the tree. How can this be fixed?

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[%
    sibling distance=10em,
    every node/.style = {%
        shape=rectangle,
        rounded corners,
        draw,
        align=center,
        top color=white,
        bottom color=blue!20
   }]]

\node {root}
    child { node {A (level 1)}
        child { node {A.A (level 2)}
            child { node {A.A.A (level 3)} }
            child { node {A.A.B (level 3)} }
        }
        child { node {A.B (level 2)} }
        child { node {A.C (level 2)} }
    }
    child { node {B (level 1)}
        child { node {B.A (level 2)} }
        child { node {B.B (level 2)} }
        child { node {B.C (level 2)} }
    %} % comment, as it wouldn't typeset otherwise
    };

\end{tikzpicture}
\end{document}

What it looks like at the moment:error

  • 1
    Please add a compilable MWE. – OSjerick Jul 1 '16 at 18:22
  • 1
    @OSjerick I have added the missing lines. – epR8GaYuh Jul 1 '16 at 18:29
  • 2
    If you need to draw more trees, may be you could consider forest, it will solve this problem without your intervention: tex.stackexchange.com/q/60571/1952 – Ignasi Jul 2 '16 at 10:44
7

Like this;

enter image description here For this you need to increase sibling distance at level 1, and slightly reduce in level 2:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[%
level 1/.style = {sibling distance=24em},   % <-- added
level 2/.style = {sibling distance=8em},    % <-- added
level 3/.style = {sibling distance=9em},    % <-- added
    every node/.style = {%
        shape=rectangle,
        rounded corners,
        draw,
        align=center,
        top color=white,
        bottom color=blue!20
   }]]

\node {root}
    child { node {A (level 1)}
        child { node {A.A (level 2)}
            child { node {A.A.A (level 3)} }
            child { node {A.A.B (level 3)} }
        }
        child { node {A.B (level 2)} }
        child { node {A.C (level 2)} }
    }
    child { node[red] {B (level 1)}
        child { node {B.A (level 2)} }
        child { node {B.B (level 2)} }
        child { node {B.C (level 2)} }
    };
    \end{tikzpicture}
\end{document}
  • Yes, thank you. This is what I was looking for. – epR8GaYuh Jul 1 '16 at 18:40

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.