编辑

编辑

目前我正在尝试创建一个树形图。我需要将其放入投影仪幻灯片中。这是我目前所做的:

\fbox{
\begin{tikzpicture}[level/.style={sibling distance=60mm/#1}]
\node[circle,draw] (0){$0$}
  child {node[circle,draw] (A) {$A$}
    child {node[circle,draw] (B) {$B$}
      child {node[circle,draw](C) {C}
        child {node[circle,draw] (D) {$D$}}
        child {node[circle,draw] (E) {$E$}}
            } 
      child {node[circle,draw](F){$F$}}
          }
    child {node[circle,draw](G) {$G$}
      child {node[circle,draw](H) {$H$}}
      child {node[circle,draw](I) {$I$}}
          }
        }
  child {node[circle,draw](J) {$J$}
    child {node[circle,draw](K) {$K$}
      child {node[circle,draw](L) {$L$}}
      child {node[circle,draw](M){$M$}}
          }
  child {node [circle,draw] (N) {$N$}
   % child {node[circle,draw](O) {$O$}}
    child {node[circle,draw](P){$P$}
      child {node [circle,draw](Q) {$Q$}}
      child {node [circle,draw](R) {$R$}
% ----------------------------------------
%text on the right -----------------------
       child [grow=right] {node (q) {\void} edge from parent[draw=none]
          child [grow=right] {node (q) {4. Stufe} edge from parent[draw=none]
            child [grow=up] {node (r) {3. Stufe} edge from parent[draw=none]
              child [grow=up] {node (s) {2. Stufe} edge from parent[draw=none]
                child [grow=up] {node (t) {1. Stufe} edge from parent[draw=none]
                  child [grow=up] {node (u) {Start} edge from parent[draw=none]}
% ----------------------------------------
                }
              }
            }
          }
        }
      }
    }
  }
};

% Wahrscheinlichkeiten der Pfade
\path (0) -- (A) node [midway, left]  {\; $\frac{o}{a}$ \;};
\path (0) -- (J) node [midway, right] {\; $\frac{o}{j}$ \;};

\path (A) -- (B) node [midway, left] {\; $\frac{a}{b}$ \;};

\path (B) -- (C) node [midway, left] {\; $\frac{b}{c}$ \;};
\path (B) -- (F) node [midway, right] {\; $\frac{b}{f}$ \;};
 \path (C) -- (D) node [midway, left] {\; $\frac{c}{d}$ \;};
 \path (C) -- (E) node [midway, right] {\; $\frac{c}{e}$ \;};

\path (A) -- (G) node [midway, right] {\; $\frac{a}{g}$ \;};
 \path (G) -- (H) node [midway, left] {\; $\frac{g}{h}$ \;};
 \path (G) -- (I) node [midway, right] {\; $\frac{g}{i}$ \;};

\path (J) -- (K) node [midway, left] {\; $\frac{j}{k}$ \;};
 \path (K) -- (L) node [midway, left] {\; $\frac{k}{l}$ \;};
 \path (K) -- (M) node [midway, right] {\; $\frac{k}{m}$ \;};

\path (J) -- (N) node [midway, right] {\; $\frac{j}{n}$ \;};
 \path (N) -- (P) node [midway, right] {\; $\frac{n}{p}$ \;};
   \path (P) -- (Q) node [midway, left] {\; $\frac{p}{q}$ \;};
   \path (P) -- (R) node [midway, right] {\; $\frac{p}{r}$ \;};    

\end{tikzpicture}
}

现在我有两个问题:如何向图表添加第五级以及它如何适合投影仪幻灯片?第二个问题:如何用不同的颜色突出显示图表的某些分支?

答案1

可以像添加其他级别一样添加第五级。当然,它必须适合幻灯片!

forest尤其擅长绘制紧凑树。以下是使用此包的版本:

密林

这实际上使用了forest我作为实验编写的一种样式。示例代码如下

\documentclass{beamer}

\usepackage{etex,prooftrees}% remove etex if unneeded

\begin{document}
\begin{frame}[fragile]
  \fbox{\begin{forest}
    proof tree,
    no line numbering,
    single branches,
    for tree={
      circle,
      draw,
      math content,
      minimum size=2em,
      inner sep=1.5pt,
      before drawing tree={
        if level=0{}{
          if n'=1{
            edge label/.wrap 2 pgfmath args={node [midway, right=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          }{
            edge label/.wrap 2 pgfmath args={node [midway, left=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          },
        }{}
      },
      l sep+=3mm,
    },
    [, phantom
      [0, name=0, just=Start
        [A, name=A, just={1. Stufe}
          [B, name=B, just={2. Stufe}
            [C, name=C, just={3. Stufe}
              [D, name=D, just={4. Stufe}]
              [E, name=E]
            ]
            [F, name=F]
          ]
          [G, name=G
            [H, name=H]
            [I, name=I]
          ]
        ]
        [J, name=J
          [K, name=K
            [L, name=L]
            [M, name=M]
          ]
          [N, name=N
            [P, name=P
              [Q, name=Q
                [S, name=S, just={5. Stufe}]
                [T, name=T]
              ]
              [R, name=R]
            ]
          ]
        ]
      ]
    ]
  \end{forest}}
\end{frame}

\end{document}

etex除非您的 TeX 安装非常新,否则您不需要它,而且几天后当错误修复通过 CTAN 传播时,您将不需要它。如果您有更新的 TeX Live 2015,您将暂时需要它。

该包的代码prooftrees这里. 另存为prooftrees.sty与您的文件位于同一目录中.tex

要为分支着色,可以使用for tree={<colour>, edge={<colour>}}。例如,如果我A按如下方式添加到节点

, for tree={green!50!black, edge={green!50!black}}

K如下

, for tree={blue, edge={blue}}

将分支恢复G为黑色

, for tree={black, edge={black}}

然后我得到

着色树枝

完整代码(不包括prooftrees.sty):

\documentclass{beamer}

\usepackage{etex,prooftrees}% remove etex if unneeded

\begin{document}
\begin{frame}[fragile]
  \fbox{\begin{forest}
    proof tree,
    no line numbering,
    single branches,
    for tree={
      circle,
      draw,
      math content,
      minimum size=2em,
      inner sep=1.5pt,
      before drawing tree={
        if level=0{}{
          if n'=1{
            edge label/.wrap 2 pgfmath args={node [midway, right=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          }{
            edge label/.wrap 2 pgfmath args={node [midway, left=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          },
        }{}
      },
      l sep+=3mm,
    },
    [, phantom
      [0, name=0, just=Start
        [A, name=A, just={1. Stufe}, for tree={green!50!black, edge={green!50!black}}
          [B, name=B, just={2. Stufe}
            [C, name=C, just={3. Stufe}
              [D, name=D, just={4. Stufe}]
              [E, name=E]
            ]
            [F, name=F]
          ]
          [G, name=G, for tree={black, edge={black}}
            [H, name=H]
            [I, name=I]
          ]
        ]
        [J, name=J
          [K, name=K, for tree={blue, edge={blue}}
            [L, name=L]
            [M, name=M]
          ]
          [N, name=N
            [P, name=P
              [Q, name=Q
                [S, name=S, just={5. Stufe}]
                [T, name=T]
              ]
              [R, name=R]
            ]
          ]
        ]
      ]
    ]
  \end{forest}}
\end{frame}

\end{document}

编辑

如果您不希望边源自父级上的单个点,则可以添加parent anchor=,调用for tree={...}

...
for tree={
  circle,
  draw,
  parent anchor=,
  math content,
  minimum size=2em,
  inner sep=1.5pt,
  ...

要得到

来自父级不同点的边

类似的方法可以使用 的justtrees简化版本prooftrees。代码如下这里. 将其保存为justtrees.sty与您的文件相同的目录.tex

输出基本相同,但包使用的代码稍微简单一些,并且可能编译得更快一些(我不确定)。

\documentclass{beamer}

\usepackage{etex,justtrees}% remove etex if unneeded

\begin{document}
\begin{frame}[fragile]
  \fbox{\begin{forest}
    just tree,
    for tree={
      circle,
      draw,
      parent anchor=,
      math content,
      minimum size=2em,
      inner sep=1.5pt,
      before drawing tree={
        if level=0{}{
          if n'=1{
            edge label/.wrap 2 pgfmath args={node [midway, right=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          }{
            edge label/.wrap 2 pgfmath args={node [midway, left=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          },
        }{}
      },
      l sep+=3mm,
    },
    [, phantom
      [0, name=0, right just=Start
        [A, name=A, right just={1. Stufe}, for tree={green!50!black, edge={green!50!black}}
          [B, name=B, right just={2. Stufe}
            [C, name=C, right just={3. Stufe}
              [D, name=D, right just={4. Stufe}]
              [E, name=E]
            ]
            [F, name=F]
          ]
          [G, name=G, for tree={black, edge={black}}
            [H, name=H]
            [I, name=I]
          ]
        ]
        [J, name=J
          [K, name=K, for tree={blue, edge={blue}}
            [L, name=L]
            [M, name=M]
          ]
          [N, name=N
            [P, name=P
              [Q, name=Q
                [S, name=S, right just={5. Stufe}]
                [T, name=T]
              ]
              [R, name=R]
            ]
          ]
        ]
      ]
    ]
  \end{forest}}
\end{frame}

\end{document}

正确对齐的树

编辑

回应评论中的讨论,\scalebox{<scale>}{\begin{forest}...\end{forest}}可用于缩放树。但是,将其缩小一半会使观众难以阅读或无法阅读。删除边缘上的分数会有所帮助。如果其中一些分数确实很重要,则可以直接使用它们添加,edge node或者可以创建样式my label以在需要时根据节点内容创建分数。

例如:

\documentclass{beamer}

\usepackage{etex,justtrees}% remove etex if unneeded

\begin{document}
\begin{frame}[fragile]
  \fbox{\scalebox{.5}{\begin{forest}
    my label/.style={
      if n'=1{
        edge label/.wrap 2 pgfmath args={node [midway, right=.5em] {\ensuremath{\frac{##2}{##1}}}}{content()}{content("!u")},
      }{
        edge label/.wrap 2 pgfmath args={node [midway, left=.5em] {\ensuremath{\frac{##2}{##1}}}}{content()}{content("!u")},
      },
    },
    just tree,
    for tree={
      circle,
      draw,
      parent anchor=,
      math content,
      minimum size=2em,
      inner sep=1.5pt,
      l sep+=3mm,
    },
    [, phantom
      [0, name=0, right just=Start
        [A, name=A, right just={1. Stufe}, for tree={green!50!black, edge={green!50!black}}
          [B, name=B, right just={2. Stufe}, my label
            [C, name=C, right just={3. Stufe}
              [D, name=D, right just={4. Stufe}]
              [E, name=E]
            ]
            [F, name=F]
          ]
          [G, name=G, for tree={black, edge={black}}
            [H, name=H]
            [I, name=I]
          ]
        ]
        [J, name=J
          [K, name=K, for tree={blue, edge={blue}}, my label
            [L, name=L]
            [M, name=M]
          ]
          [N, name=N
            [P, name=P
              [Q, name=Q
                [S, name=S, my label, right just={5. Stufe}]
                [T, name=T, my label]
              ]
              [R, name=R]
            ]
          ]
        ]
      ]
    ]
  \end{forest}}}
\end{frame}

迷你树

请注意我确实不是建议把树做这么小,除非你想惹恼你的观众,或者你知道你的观众将完全由眼睛明亮的鹰组成。

相关内容