tikz 树垂直空间和“悬挂”树枝

tikz 树垂直空间和“悬挂”树枝

我正在尝试使用 Beamer 创建幻灯片。如下所示:

\documentclass[t]{beamer} %
\usepackage{tikz} %
\usetikzlibrary{trees} %

\setbeamertemplate{navigation symbols}{} %
\setbeamertemplate{footline}{} %
\setbeamertemplate{headline}{} %

\begin{document}

\begin{frame}{}
  \bigskip
  \begin{tikzpicture}[ %
    blue/.style={rectangle,draw,fill=blue!20}, %
    blue2/.style={rectangle,draw,fill=blue!20,text width=2.9cm,text centered,grow=south}, %
    red/.style={rectangle,draw,fill=red!20,text width=2.3cm,text centered}, %
    ] %
    \footnotesize \node [blue] {Here's a basic question for our subject} %
    [edge from parent fork down] %
    [sibling distance=2cm] %
    child {node[red,anchor=east] {Answer 1} %
      [sibling distance=0.3cm] %
      child {node[blue2,anchor=east] {Answer 1 may lead to} %
        child {node[red,anchor=south] {Theory A}} %
      } %
      child {node[blue2,anchor=west] {Answer 1 may also lead to} %
        child {node[red,anchor=south] {Theory B}} %
      }} %
    child{node[red,anchor=west] {Answer 2} %
      child {node[blue2,anchor=west] {Answer 2 leads to another question} %
        [sibling distance=5.5cm] %
        child {node[red,anchor=west]{Answer 3} %
          [sibling distance=0.8cm] %
          child {node[red,anchor=east]{Theory C}} %
          child {node[red,anchor=west]{Theory D}} %
        } %
        child {node[red,anchor=east]{Answer 4}}}}; %
  \end{tikzpicture}
\end{frame}
\end{document}

问题是,我还有“理论 E”和“理论 F”,它们位于“答案 4”下。我想增加“答案 2 引出另一个问题”和“答案 3”和“答案 4”下的垂直空间;将“答案 3”推到左侧,位于“答案 1”的分支下,为“答案 4”的子项腾出空间。我不知道该怎么做。

在此处输入图片描述

编辑:也许我应该强调一下,我正在寻找一种能够移动“答案 3”及其子项的解决方案在下面“理论 A”/“理论 B”,使其与“答案 1”垂直对齐。然后我可以将“答案 4”直接放在“答案 2 引出另一个问题”下,并添加“理论 E”和“理论 F”作为其子项。我无法在幻灯片中添加任何水平空间(它具有固定尺寸),也无法将字体调小,因为我希望人们能够阅读它(它已经是 \footnotesize)。

答案1

使用 forest 包很简单:

\documentclass[t]{beamer}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}{}
\setbeamertemplate{headline}{}

\usepackage[edges]{forest}

\begin{document}

\begin{frame}
\frametitle{solution with the \texttt{forest} package}
\begin{center} 
    \begin{forest}
    for tree={          % style of nodes in the tree
         font = \sffamily\scriptsize\linespread{0.8}\selectfont,
        align = center,%   text centered,
         fill = red!20, draw,
                        % style of tree (edges, distances, direction)
         grow = south,
    forked edge,        % for forked edge
        s sep = 2mm,    % sibling distance
        l sep = 8mm,    % level distance
     fork sep = 4mm,    % distance from parent to branching point
               }% end for tree
[Here's a basic question for our subject, fill=blue!20
    [Answer 1
        [Answer 1\\ may lead to, fill=blue!20
            [Theory A]
        ]
         [Answer 1 may\\ also lead to, fill=blue!20
            [Theory B]
        ]
    ]
    [Answer 2
        [Answer 2 leads to\\ another question, fill=blue!20
            [Answer 3
                [Theory C]
                [Theory D]
            ]
            [Answer 4
                [Theory E]
                [Theory F]
            ]
        ]
    ]
]
  \end{forest}
\end{center}
\end{frame}
\end{document}

在此处输入图片描述

  • 使用的只是基本功能forest
  • 该树可以适合框架大小,使用的字体大小减小到\small。这样就不需要在节点下推送分支答案 2 引出了另一个问题在左边的树枝下。通过这个,我认为树更清晰)
  • 最小节点大小未定义,其大小取决于其内容大小
  • 可以添加更复杂的可能性,forest根据节点在三者中的位置对节点进行着色。然而,我估计这会使代码在开始forest使用时不太清晰

附录: 如果我正确理解了你的问题的编辑,那么你正在寻找以下树:

在此处输入图片描述

该框架的代码是:

\begin{frame}[fragile]
\frametitle{solution with the \texttt{forest} package}
\begin{center}
    \begin{forest}
    for tree={draw, 
             font = \sffamily\small\linespread{0.8}\selectfont,
             fill = red!20,
            align = center,
    % style of tree (edges, distances, direction)
             grow = south,
        forked edge,        % for forked edge
            s sep = 4mm,    % sibling distance
            l sep = 6mm,    % level distance
         fork sep = 3mm,    % distance from parent to branching point
              }
[Here's a basic question for our subject, fill=blue!20
    [Answer 1, 
        [Answer 1\\ may lead to, fill=blue!20
            [Theory A]
            [,phantom
                [Answer 3, no edge,
                 tier=answer, name=answ3
                    [Theory C]
                    [Theory D]
                ]
            ]
            [Theory B]
        ]
     ]
    [Answer 2
        [Answer 2 leads to\\ another question, fill=blue!20,
         s sep+=12mm 
            [,coordinate,name=answ2]
            [,coordinate
                [Answer 4,tier=answer
                    [Theory E]
                    [Theory F]
                ]
            ]
        ]
    ]
]
    \draw (answ2) |- ([yshift=2.5mm] answ3.north) -- (answ3);
\end{forest}
\end{center}
\end{frame}

答案2

[level distance=XXcm]您可以使用和移动子项[sibling distance=XXcm]

\documentclass[t,aspectratio=169]{beamer} %
\usepackage{tikz} %
\usetikzlibrary{trees} %

\setbeamertemplate{navigation symbols}{} %
\setbeamertemplate{footline}{} %
\setbeamertemplate{headline}{} %

\begin{document}

\begin{frame}{}
  \bigskip
  \begin{tikzpicture}[ %
    blue/.style={rectangle,draw,fill=blue!20}, %
    blue2/.style={rectangle,draw,fill=blue!20,text width=2.9cm,text centered,grow=south}, %
    red/.style={rectangle,draw,fill=red!20,text width=2.3cm,text centered}, %
    ] %
    \footnotesize \node [blue] {Here's a basic question for our subject} %
    [edge from parent fork down] %
    [sibling distance=2cm] %
    child {node[red,anchor=east] {Answer 1} %
      [sibling distance=0.3cm] %
      child {node[blue2,anchor=east] {Answer 1 may lead to} %
        child {node[red,anchor=south] {Theory A}} %
      } %
      child {node[blue2,anchor=west] {Answer 1 may also lead to} %
        child {node[red,anchor=south] {Theory B}} %
      }} %
    child{node[red,anchor=west] {Answer 2} %
      child{node[blue2,anchor=west] {Answer 2 leads to another question} %
        [sibling distance=8cm] %
        child[level distance=2cm,sibling distance=8cm]{node[red,anchor=west]{Answer 3} %
          [sibling distance=0.8cm] %
          child[level distance=2cm]{node[red,anchor=east]{Theory C}} %
          child[level distance=2cm]{node[red,anchor=west]{Theory D}} %
        } %
        child[level distance=2cm]{node[red,anchor=east]{Answer 4}}}}; %
  \end{tikzpicture}
\end{frame}
\end{document}

暗示:我将其添加[aspectratio=169]到文档类,以适应更宽的屏幕。

在此处输入图片描述

相关内容