我想画一个 2 或 3 周期二项树。我无法像图片那样设计。我尝试的第一个模型,代码如下。其中“文本”没有像图片那样对齐。“\vspace”或“Itemize”不起作用。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzstyle{tree} = [text width=2em, text centered]
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=1cm,
]
{
& & \\
& { Stock price =\$B Option price =\$c}& \\
Stock price = \$A & & \\
& { Stock price =\$B Option price =\$c} & \\
& & \\
};
\draw[->] (tree-3-1) -- (tree-2-2) node [midway,above] {$P$};
\draw[->] (tree-3-1) -- (tree-4-2) node [midway,below] {$(1-p)$};
\end{tikzpicture}
\end{document}
答案1
有许多高级软件包(例如 forest)使树的绘制变得非常方便。但是,在这个答案中,我将遵循您所走的路径并提出一些小建议。您可以使用通过 实现的节点样式nodes={...}
来使您的生活更轻松。并且您可以通过定位添加项目符号。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\tikzset{bullet/.style={circle,fill,inner sep=2pt}}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=1cm,nodes={text width=8em}
]
{
& & \\
& {Stock price =\$B\\ Option price =\$c} & \\
Stock price = \$A & & \\
& {Stock price =\$B\\ Option price =\$c} & \\
& & \\
};
\node[bullet,right=0mm of tree-3-1.east](b-3-1){};
\node[bullet,left=0mm of tree-2-2.west](b-2-2){};
\node[bullet,left=0mm of tree-4-2.west](b-4-2){};
\draw[->] (b-3-1) -- (b-2-2) node [midway,above] {$p$};
\draw[->] (b-3-1) -- (b-4-2) node [midway,below] {$(1-p)$};
\end{tikzpicture}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=2.5cm,
row sep=1cm,nodes={text width=8em}
]
{
& & D\\
& B & \\
A & & E\\
& C & \\
& & F\\
};
\node[bullet,left=3.14mm of tree-3-1.west](b-3-1){};
\node[bullet,left=3.14mm of tree-2-2.west,label=above:22,label=below:2.057](b-2-2){};
\node[bullet,left=3.14mm of tree-4-2.west](b-4-2){};
\node[bullet,left=3.14mm of tree-1-3.west](b-1-3){};
\node[bullet,left=3.14mm of tree-3-3.west](b-3-3){};
\node[bullet,left=3.14mm of tree-5-3.west](b-5-3){};
\draw[->] (b-3-1) -- (b-2-2);
\draw[->] (b-2-2) -- (b-1-3);
\draw[->] (b-2-2) -- (b-3-3);
\draw[->] (b-3-1) -- (b-4-2);
\draw[->] (b-4-2) -- (b-3-3);
\draw[->] (b-4-2) -- (b-5-3);
\end{tikzpicture}
\end{document}
在第二个例子中,我还展示了如何为项目符号添加标签,但当然我只为一个项目符号添加标签(而不是从屏幕截图中输入所有数字)。
正如上面提到的,当然还有更优雅的方式,但这种方式当然有一个好处,就是你可以随意放置额外的东西。