尝试在 beamer 中创建一个简单的二叉树

尝试在 beamer 中创建一个简单的二叉树

按照以下示例 在 beamer 中使用 tikz 矩阵时出现“单个 & 符号与错误的 catcode 一起使用”错误 我删除了收到的错误,但树仍然无法正确生成。我尝试在 Beamer 中重现我在此线程中看到的内容: http://www.latex-community.org/forum/viewtopic.php?f=45&t=7846&sid=2129f08cf0ff8d6a5473794a64bdf5ac

提前致谢。

\documentclass[xcolor=pdflatex,dvipsnames,table]{beamer}

\usepackage{tikz}
\usetikzlibrary{matrix}

\mode<presentation>

\usetheme{JuanLesPins}

\setbeamercovered{transparent=30}

\title[Investment Science II]{ Investment Science II \\  Introduction to Binomial Trees }
\author[ Anonymous]

{ Investment Science II }

\institute{ Blah University }

\begin{document}

\frame{\maketitle}

\begin{frame}

  \begin{tikzpicture}

    \matrix (tree) [matrix of nodes,ampersand replacement=\&]

          {
          \&   \& F \\

          \& C \&   \\

      \$A \&   \& E \\

          \& B \&   \\

          \&   \& D \\
    };

    \draw[->] (tree-3-1) -- (tree-2-2) node [midway,above] {$p$};

    \draw[->] (tree-3-1) -- (tree-4-2) node [midway,below] {$(1-p)$};

    \draw[->] (tree-2-2) -- (tree-1-3) node [midway,above] {$p^2$};

    \draw[->] (tree-2-2) -- (tree-3-3) node [midway,below] {$(1-p)p$};

    \draw[->] (tree-4-2) -- (tree-3-3) node [midway,above] {$(1-p)p$};

    \draw[->] (tree-4-2) -- (tree-5-3) node [midway,below] {$(1-p)^2$};

  \end{tikzpicture}

  \end{frame}
\end{document}

答案1

你的代码中有太多空行。空行会导致段落中断。至少删除author矩阵开始后和之后的空行。最好多删除一些。

它没有正确生成,因为您忘记指定节点大小、列分隔符和行分隔符。

这可以正常工作:

\documentclass[xcolor=pdflatex,dvipsnames,table]{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\mode<presentation>
\usetheme{JuanLesPins}
\setbeamercovered{transparent=30}
\title[Investment Science II]{ Investment Science II \\
  Introduction to Binomial Trees }
\author[ Anonymous]{ Investment Science II }
\institute{ Blah University }
\begin{document}
\frame{\maketitle}
\begin{frame}
  \begin{tikzpicture}[>=stealth,sloped]
    \matrix (tree) [%
      matrix of nodes,
      minimum size=1cm,
      column sep=3.5cm,
      row sep=1cm,ampersand replacement=\&
    ]
    {
          \&   \& F \\
          \& C \&   \\
      \$A \&   \& E \\
          \& B \&   \\
          \&   \& D \\
    };
    \draw[->] (tree-3-1) -- (tree-2-2) node [midway,above] {$P$};
    \draw[->] (tree-3-1) -- (tree-4-2) node [midway,below] {$(1-p)$};
    \draw[->] (tree-2-2) -- (tree-1-3) node [midway,above] {$P^2$};
    \draw[->] (tree-2-2) -- (tree-3-3) node [midway,below] {$(1-p)p$};
    \draw[->] (tree-4-2) -- (tree-3-3) node [midway,above] {$(1-p)p$};
    \draw[->] (tree-4-2) -- (tree-5-3) node [midway,below] {$(1-p)^2$};
  \end{tikzpicture}
  \end{frame}
\end{document}

输出树

相关内容