5

so far i have the following code which gives me a ugly TIKZ picture (twe level tree). Is there a comfortable way to get a nice spacing for second level nodes? Thank you very much in advance!

\documentclass[a4paper,12pt,titlepage]{scrreprt}
 % \documentclass[a4paper,11pt, twoside]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english, ngerman]{babel}
\usepackage{graphicx}
% \usepackage[pdftex]{graphicx}
% \usepackage{epstopdf}
\usepackage{latexsym}
\usepackage{amsmath,amssymb, amsthm}
\usepackage{tikz}
\usetikzlibrary{external}
%\tikzexternalize % activate!
\usepackage{lscape}
\usepackage{fullpage}
\usetikzlibrary{positioning,shadows,arrows,trees,shapes,fit}

\begin{document}
\pagestyle{empty}
\begin{landscape}
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
[level distance=2.5cm,
level 1/.style={sibling distance=5cm},
level 2/.style={sibling distance=2cm}, auto,transform shape ]
\tikzstyle{every node}=[rectangle,draw]
\node(Root) {Gesamtdaten}
    child { node (Root-0){$RR=0$} }
    child {
        node (Root-1){$RR\neq 0$}
        child { node(Root-1-1) {$0<RR\leq 0.25$} }
        child { node (Root-1-2){$0.25<RR\leq 0.65$} }
        child { node (Root-1-3){$0.65 < RR \leq 1$} }
        child { node(Root-1-4) {$1 < RR \leq 1.2$} }
        child { node (Root-1-5){$1.2 < RR \leq 1.35$} }
        child { node (Root-1-6){$1.35 < RR \leq 1.5$} }
        child { node (Root-1-7){$1.5 < RR \leq 1.7$} }
        child { node (Root-1-8){$1.7 < RR \leq 1.95$} }
        child { node (Root-1-9){$1.95 < RR \leq 2.4$} }
        child { node (Root-1-10){$2.4 < RR \leq 3$} }
        }
;
\begin{scope}[every node/.style={right}]
 \path (Root-0    -| Root-1)  ++(2cm,0) node[color=red] {SVM 1};
 \path (Root-1-1  -| Root-1-10) ++(2cm,0) node [color=red] {SVM 2};
 \end{scope}
\end{tikzpicture}

\end{center}
\end{figure}
\end{landscape}

\end{document}

enter image description here

  • Your MWE cannot get compiled as it. – Clément Jul 10 '14 at 14:21
  • using pdflatex i can compile my code... i do not know why it may not work – user58885 Jul 10 '14 at 14:25
  • I commented \tikzexternalize and now I can compile. Before that, the error message was ! Package tikz Error: Sorry, the system call 'pdflatex -halt-on-error -interact ion=batchmode -jobname "temp-figure0" etc. – Clément Jul 10 '14 at 14:27
  • @Clément Did you turn on the shell escape? – percusse Jul 10 '14 at 14:28
  • (@percusse : no) Why don't you just increase the sibling distance of the second level to, for instance, 3.5cm? That is enough for the nodes not to collapse, but your figure becomes too large for the A4 format with a 12pt font: is that a problem? – Clément Jul 10 '14 at 14:30
9

This is a variation on smoneck's answer which implements the east-growing tree in forest.

I begin by demonstrating forest's ability to space the children automatically even without additional hints.

Initial conversion

forest uses the bracket syntax which simplifies the definition of the tree somewhat:

\documentclass[tikz,12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english, ngerman]{babel}
\usepackage{forest}
\usetikzlibrary{positioning}

\begin{document}
  \begin{forest}
    for tree={
      draw,
    }
    [Gesamtdaten, name=Root
      [{$RR=0$}, name=Root-0
      ]
      [{$RR\neq 0$}, name=Root-1
        [{$0<RR\leq 0.25$}, name=Root-1-1
        ]
        [{$0.25<RR\leq 0.65$}, name=Root-1-2
        ]
        [{$0.65 < RR \leq 1$}, name=Root-1-3
        ]
        [{$1 < RR \leq 1.2$}, name=Root-1-4
        ]
        [{$1.2 < RR \leq 1.35$}, name=Root-1-5
        ]
        [{$1.35 < RR \leq 1.5$}, name=Root-1-6
        ]
        [{$1.5 < RR \leq 1.7$}, name=Root-1-7
        ]
        [{$1.7 < RR \leq 1.95$}, name=Root-1-8
        ]
        [{$1.95 < RR \leq 2.4$}, name=Root-1-9
        ]
        [{$2.4 < RR \leq 3$}, name=Root-1-10
        ]
      ]
    ]
    \begin{scope}[every node/.style={right}]
      \path (Root-0    -| Root-1)  ++(2cm,0) node[color=red] {SVM 1};
      \path (Root-1-1  -| Root-1-10) ++(2cm,0) node [color=red] {SVM 2};
    \end{scope}
  \end{forest}

\end{document}

initial conversion

This spaces the nodes but unfortunately does not guarantee that the connecting lines will not cross nodes, especially when the nodes' borders are drawn.

Tweaking the appearance

We add to l to increase the distance between levels, thicken the lines drawn throughout, and use -| type line construction to draw the connecting lines.

\documentclass[tikz,12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english, ngerman]{babel}
\usepackage{forest}
\usetikzlibrary{positioning}

\begin{document}
  \begin{forest}
    for tree={
      draw,
      thick,
      l sep=40pt,
      edge path={
        \noexpand\path[thick, -, \forestoption{edge}]
          (!u.parent anchor) -- +(0,-25pt) -|
          (.child anchor)\forestoption{edge label};
      },
    }
    [Gesamtdaten, name=Root
      [{$RR=0$}, name=Root-0
      ]
      [{$RR\neq 0$}, name=Root-1
        [{$0<RR\leq 0.25$}, name=Root-1-1
        ]
        [{$0.25<RR\leq 0.65$}, name=Root-1-2
        ]
        [{$0.65 < RR \leq 1$}, name=Root-1-3
        ]
        [{$1 < RR \leq 1.2$}, name=Root-1-4
        ]
        [{$1.2 < RR \leq 1.35$}, name=Root-1-5
        ]
        [{$1.35 < RR \leq 1.5$}, name=Root-1-6
        ]
        [{$1.5 < RR \leq 1.7$}, name=Root-1-7
        ]
        [{$1.7 < RR \leq 1.95$}, name=Root-1-8
        ]
        [{$1.95 < RR \leq 2.4$}, name=Root-1-9
        ]
        [{$2.4 < RR \leq 3$}, name=Root-1-10
        ]
      ]
    ]
    \begin{scope}[every node/.style={right}]
      \path (Root-0    -| Root-1)  ++(2cm,0) node[color=red] {SVM 1};
      \path (Root-1-1  -| Root-1-10) ++(2cm,0) node [color=red] {SVM 2};
    \end{scope}
  \end{forest}

\end{document}

improved tree

Not only is this much too large for landscape A4, it also looks a bit squashed given the length of the final line compared with the height of the tree. So it seems worth seeing what the growth direction suggested by smoneck might do for us.

The final tree

\documentclass[tikz,12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english, ngerman]{babel}
\usepackage{forest}
\usetikzlibrary{positioning}

\begin{document}
  \begin{forest}
    for tree={
      draw,
      thick,
      l sep=40pt,
      edge path={
        \noexpand\path[thick, -, \forestoption{edge}]
          (!u.parent anchor) -- +(20pt,0) |-
          (.child anchor)\forestoption{edge label};
      },
      grow'=east,
      child anchor=west,
      parent anchor=east,
      anchor=west,
    }
    [Gesamtdaten, name=Root
      [{$RR=0$}, name=Root-0
      ]
      [{$RR\neq 0$}, name=Root-1
        [{$0<RR\leq 0.25$}, name=Root-1-1
        ]
        [{$0.25<RR\leq 0.65$}, name=Root-1-2
        ]
        [{$0.65 < RR \leq 1$}, name=Root-1-3
        ]
        [{$1 < RR \leq 1.2$}, name=Root-1-4
        ]
        [{$1.2 < RR \leq 1.35$}, name=Root-1-5
        ]
        [{$1.35 < RR \leq 1.5$}, name=Root-1-6
        ]
        [{$1.5 < RR \leq 1.7$}, name=Root-1-7
        ]
        [{$1.7 < RR \leq 1.95$}, name=Root-1-8
        ]
        [{$1.95 < RR \leq 2.4$}, name=Root-1-9
        ]
        [{$2.4 < RR \leq 3$}, name=Root-1-10
        ]
      ]
    ]
    \begin{scope}[every node/.style={red}]
      \node [above=5pt of Root-0] {SVM 1};
      \node [above=5pt of Root-1-1] {SVM 2};
    \end{scope}
  \end{forest}

\end{document}

final tree

The result is 107x97mm which will easily fit A4.

  • excellent! This solution i like the most! Great! Thank you very very much! – user58885 Jul 11 '14 at 4:30
8

What about putting it sideways? I basically added the grow and edge from parent path options and adjusted the distances. You could also play with the font and/or scale.

You just need to adjust the scope-section.

grow-right

\begin{tikzpicture}
    [grow=right, scale=1.,font=\normalsize,
    edge from parent path={(\tikzparentnode.east) |- (\tikzchildnode.west)},
    level distance=3.5cm,
    level 1/.style={sibling distance=6cm},
    level 2/.style={sibling distance=1.2cm, level distance=3.5cm},
    auto,transform shape]
\tikzstyle{every node}=[rectangle,draw]
\node(Root) {Gesamtdaten}
child { node (Root-0){$RR=0$} }
child {
node (Root-1){$RR\neq 0$}
child { node(Root-1-1) {$0<RR\leq 0.25$} }
child { node (Root-1-2){$0.25<RR\leq 0.65$} }
child { node (Root-1-3){$0.65 < RR \leq 1$} }
child { node(Root-1-4) {$1 < RR \leq 1.2$} }
child { node (Root-1-5){$1.2 < RR \leq 1.35$} }
child { node (Root-1-6){$1.35 < RR \leq 1.5$} }
child { node (Root-1-7){$1.5 < RR \leq 1.7$} }
child { node (Root-1-8){$1.7 < RR \leq 1.95$} }
child { node (Root-1-9){$1.95 < RR \leq 2.4$} }
child { node (Root-1-10){$2.4 < RR \leq 3$} }
};
%\begin{scope}[every node/.style={right}]
%\path (Root-0-| Root-1)++(2cm,0) node[color=red] {SVM 1};
%\path (Root-1-1-| Root-1-10) ++(2cm,0) node [color=red] {SVM 2};
%\end{scope}
\end{tikzpicture}
  • That's much better, an elegant solution. – Clément Jul 10 '14 at 21:10

Your Answer

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