我正在使用二项式树,如下所示(代码位于帖子末尾)。
如何在每个节点内创建新行?我想在节点中的每个数字下方添加第二个数字。
\documentclass{article}
\usepackage{amssymb,mathtools}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of math nodes,
minimum size=1cm,
column sep=2.5cm,
row sep=0.4cm,
column 1/.style={nodes={draw,rounded corners,
top color=white, bottom color=blue!20}},
column 2/.style={nodes={draw,fill=green!30,rounded corners}},]
{
& 2800 \\
2500 & \\
& 2700 \\
};
\draw[->] (tree-2-1) -- (tree-1-2) node [midway,above] {$p$};
\draw[->] (tree-2-1) -- (tree-3-2) node [midway,below] {$1-p$};
\end{tikzpicture}
\end{document}
答案1
请参阅 pgfmanual 中的第 57.2 节“节点矩阵中的行尾字符和行尾字符”。
在matrix of nodes
(matrix of math nodes
虽然我不知道为什么)中,您可以添加text width
和align
选项,并将\\
节点内容放置在换行符中。多行节点内容必须在括号内{ ... \\ ... }
。
\documentclass{article}
\usepackage{amssymb,mathtools}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=2.5cm,
row sep=0.4cm,
column 1/.style={nodes={draw,rounded corners,
top color=white, bottom color=blue!20}},
column 2/.style={nodes={draw,fill=green!30,rounded corners, text width=1cm, align=center}},
]
{
& {2800 \\ 2700} \\
2500 & \\
& 2700 \\
};
\draw[->] (tree-2-1) -- (tree-1-2) node [midway,above] {$p$};
\draw[->] (tree-2-1) -- (tree-3-2) node [midway,below] {$1-p$};
\end{tikzpicture}
\end{document}