初始转换

初始转换

到目前为止,我有以下代码,它给了我一个丑陋的 TIKZ 图片(二级树)。有没有一种舒适的方法可以为二级节点获得良好的间距?提前非常感谢!

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

在此处输入图片描述

答案1

这是 smoneck 答案的一个变体,它实现了向东生长的树forest

我首先要展示的forest是无需额外提示就能自动隔开子项的能力。

初始转换

forest使用括号语法,稍微简化了树的定义:

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

初始转换

这会使节点间隔开,但不幸的是,这不能保证连接线不会穿过节点,尤其是在绘制节点的边界时。

调整外观

我们添加了一些东西l来增加级别之间的距离,加粗了整个绘制的线条,并使用-|类型线构造来绘制连接线。

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

改良树

这不仅对于横向 A4 来说太大了,而且考虑到最后一条线的长度与树的高度相比,它看起来也有点被挤压了。因此,值得看看 smoneck 建议的生长方向对我们有什么作用。

最后一棵树

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

最终树

结果是 107x97mm,可以轻松适合 A4。

答案2

把它放在一边怎么样?我基本上添加了growedge from parent path选项并调整了距离。你也可以使用font和/或scale

您只需要调整scope-section。

正确成长

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

相关内容