右上方等距节点 =

右上方等距节点 =

我正在尝试绘制决策树。由于树向右分支,因此我需要在向右移动时减少节点之间的垂直距离。但是,我想保持水平距离不变。

我知道以下职位的可能性:

  • above right = of nodeA:将保持垂直距离不变,不能更多地向右使用
  • above right = 0.5cm of nodeA:允许我减少垂直距离,但也会同样缩小水平距离
  • above right = 0.5cm and 1cm of nodeA:允许我减少垂直距离并控制水平距离。

在所有这些可能性中,最后一种似乎是最好的。但是,我仍然需要控制1cm所有不同分支的距离 - 我宁愿让所有节点等距分布,而不必选择使节点完全适合页面的距离。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzstyle{block} = [rectangle, draw, 
text width=3em, text centered, rounded corners, minimum height=3em]

\begin{document}

    \begin{frame}   
    \begin{tikzpicture}
    \node [block] (A) [] {A};
    \node [block] [above right = of A] (B1) {B1};
    \node [block] [below right = of A] (B2) {B2};

    \node [block] [above right = 0.1cm and 1cm of B1] (C1) {C1};
    \node [block] [below right = 0.1cm and 1cm of B1] (C2) {C2};

    \node [block] [above right = 0.1cm and 1cm of B2] (C3) {C3};
    \node [block] [below right = 0.1cm and 1cm of B2] (C4) {C4};

    \end{tikzpicture}

    \end{frame}
\end{document}

答案1

使用forest选项吗?我当然不是专家,但我认为它对于制作树木来说相当流行。

在此处输入图片描述

\documentclass{beamer}
\usepackage{forest}
\tikzset{
block/.style = {rectangle, draw, 
text width=3em, text centered, rounded corners, minimum height=3em},
}
\begin{document}
\begin{frame}
\begin{forest}
for tree={grow=east, block}
[A
  [B1
    [C1] [C2]
  ]
  [B2
    [C3] [C4]
  ]
]
\end{forest}
\end{frame}
\end{document}

答案2

一个小的变化Torbjørn T. 回答

\documentclass{beamer}
\usepackage{forest}

\begin{document}
\begin{frame}
\begin{forest}
for tree = {
    draw, rounded corners, minimum size=3em, % nodes style
    grow=0
            }
[A
  [B1
    [C1] 
    [C2]
  ]
  [B2
    [C3] 
    [C4]
  ]
]
\end{forest}
\end{frame}
\end{document}

结果与 Torbjørn T. 回答

如果您只希望节点之间具有所需的距离,那么以下用法可能˙forest会有所帮助:

\documentclass{beamer}
\usepackage{forest}

\begin{document}
\begin{frame}
\begin{forest}
for tree = {
    draw, rounded corners, minimum size=3em, % nodes style
    grow=0,
    no edge,                                 % n edges between nodes
    s sep=1mm,                               % (vertical/sibling) separation between nodes
    l sep=10mm,                              % (horizontal/level separation between nodes
            }
[A
  [B1
    [C1] 
    [C2]
  ]
  [B2
    [C3] 
    [C4]
  ]
]
\end{forest}
\end{frame}
\end{document}

在此处输入图片描述

答案3

node distance在块样式定义中添加了值为 的选项0.5cm and 1cm。在我的代码中,block2是我的块,block1是你的块。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
block1/.style = {rectangle, draw, 
text width=3em, text centered, rounded corners, minimum height=3em},
block2/.style = {rectangle, draw, 
text width=3em, text centered, rounded corners, minimum height=3em,node distance=0.5cm and 1cm}
}

\begin{document}

    \begin{frame}   
    \begin{tikzpicture}
    \node [block1] (A) [] {A};
    \node [block1] [above right = of A] (B1) {B1};
    \node [block1] [below right = of A] (B2) {B2};

    \node [block1] [above right = 0.1cm and 1cm of B1] (C1) {C1};
    \node [block1] [below right = 0.1cm and 1cm of B1] (C2) {C2};

    \node [block1] [above right = 0.1cm and 1cm of B2] (C3) {C3};
    \node [block1] [below right = 0.1cm and 1cm of B2] (C4) {C4};

    \end{tikzpicture}

    \end{frame}

     \begin{frame}   
    \begin{tikzpicture}
    \node [block2] (A) [] {A};
    \node [block2] [above right = of A] (B1) {B1};
    \node [block2] [below right = of A] (B2) {B2};

    \node [block2] [above right = -0.5cm and 1cm of B1] (C1) {C1};
    \node [block2] [below right = -0.5cm and 1cm of B1] (C2) {C2};

    \node [block2] [above right = -0.5cm and 1cm of B2] (C3) {C3};
    \node [block2] [below right = -0.5cm and 1cm of B2] (C4) {C4};

    \end{tikzpicture}

    \end{frame}
\end{document}

在此处输入图片描述

相关内容