5

I'm trying to draw a decision tree. Since the tree branches out to the right, I need to decrease the vertical distance between nodes as I go to the right. However, I want to keep the horizontal distance constant.

I'm aware of the following position possibilities:

  • above right = of nodeA: Will keep vertical distance constant, can't use this more to the right
  • above right = 0.5cm of nodeA: Allows me to reduce vertical distance, but will equally shrink horizontal distance
  • above right = 0.5cm and 1cm of nodeA: Allows me to reduce vertical distance, and control horizontal distance.

Of all these possibilities, the last one seems best. However, I still need to control the 1cm for all of the different branches - I would rather have all the nodes to space out equidistantly without having to pick a distance that fits the node exactly onto the page.

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

Is using forest an option? I'm certainly not an expert, but I think it is quite popular for making trees.

enter image description here

\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

A small variation of the Torbjørn T. answer:

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

the result is the same as at Torbjørn T. answer.

in case that you like to have only nodes with desired distances bewtween them, then may the following use of ˙forest be helpful:

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

enter image description here

0

I added the node distance option in block style definition with values are 0.5cm and 1cm. In my code, block2 is mine block while block1 is yours.

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

enter image description here

  • 1
    That's useful, thanks. But you're still manually controlling horizontal distance in every single node, which means that they don't optimally use the space and spread horizontally.. – FooBar Jul 26 '19 at 9:35

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.