2

I am working with a binomial tree as shown below (code at end of post).

How can I make a new line within each node? I would like to add a second number below each of the numbers in the node.

enter image description here

\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}  
4

Look at section 57.2 End-of-Lines and End-of-Row Characters in Matrices of Nodes in pgfmanual.

In a matrix of nodes (not matrix of math nodes although I don't know why) you can add text width and align options and place \\ in node contents to break lines. Multiline node contents must be inside braces { ... \\ ... }.

\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}  

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.