1

I want to plot this graph, but I don't know how to code the words in the nodes.

enter image description here

Here is my code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{titletoc}
\usepackage{tikz}
\usetikzlibrary{trees,positioning,fit}

\begin{document}{\allowdisplaybreaks[4]}

\tikzstyle{bag} = [text width=2em, text centered]
\tikzstyle{end} = []

\begin{center}
\begin{tikzpicture}[sloped]
\node (a) at ( 0,0) [bag] {$\ln r_0$};
\node (b1) at ( 3,1.5) [bag] {$\ln r_{10} + \sigma_1\sqrt{\Delta t_1}$};
\node (b2) at ( 3,-1.5) [bag] {$\ln r_{10}$};
\node (c1) at ( 6,3) [bag] {$\ln r_{20} + 2\sigma_2\sqrt{\Delta t_2}$};
\node (c2) at ( 6,1.5) [bag] {$\textrm{Mean} = \ln r_{20} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2}$\\$\textrm{Mean Change} = \mu_{11}\Delta t_2 = \ln\dfrac{r_{20}}{r_{10}} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2} - \sigma_1\sqrt{\Delta t_1}$};
\node (c3) at ( 6,0) [bag] {$\ln r_{20} + \sigma_2\sqrt{\Delta t_2}$};
\node (c4) at ( 6,-1.5) [bag] {$\textrm{Mean} = \ln r_{20} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2}$\\$\textrm{Mean Change} = \mu_{11}\Delta t_2 = \ln\dfrac{r_{20}}{r_{10}} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2} - \sigma_1\sqrt{\Delta t_1}$};
\node (c5) at ( 6,-3) [bag] {$\ln r_{20}$};
\draw [->] (a) to node [below] {} (b1);
\draw [->] (a) to node [above] {} (b2);
\draw [->] (b1) to node [below] {} (c1);
\draw [->] (b1) to node [below] {} (c2);
\draw [->] (b1) to node [below] {} (c3);
\draw [->] (b2) to node [below] {} (c3);
\draw [->] (b2) to node [below] {} (c4);
\draw [->] (b2) to node [below] {} (c5);
\end{tikzpicture}
\end{center}

\end{document}    

Update code use

\draw [->] (a) to node [below] {} (b1.south east);
\draw [->] (a) to node [above] {} (b2.north east);
\draw [->] (b1.south east) to node [below] {} (c1.west);
\draw [->] (b1.south east) to node [below] {} (c2.west);
\draw [->] (b1.south east) to node [below] {} (c3.west);
\draw [->] (b2.north east) to node [below] {} (c3.west);
\draw [->] (b2.north east) to node [below] {} (c4.west);
\draw [->] (b2.north east) to node [below] {} (c5.west);
2
  • If I run your code on my computer, I get a very different output from what is shown in your post. In particular, the words are upright, as they should be. However, the figure is also jammed up. Is it possible that you produced it with some additional settings that are not shown in your MWE?
    – user121799
    Nov 6, 2017 at 15:29
  • @marmot yes, my question is exactly how to put the right position of those words. They overlap together and generate a very bad graph.
    – A.Oreo
    Nov 6, 2017 at 15:34

1 Answer 1

2

Your biggest problem is that you've set text width=2em. That means the node is set in a minipage-like box 2em wide, and when the text in the node becomes longer than that, it is broken across multiple lines.

A more useful definition of the style is

\tikzset{
  bag/.style={
     align=left,
     font=\footnotesize % to reduce the fontsize
  }
}

(Or possibly align=center.) When you set align, you can use \\, but automatic line breaking doesn't happen.

The next thing you'd likely want to change, is to add right (or equivalently anchor=west) to the options for all the nodes in the last level, i.e. all the c nodes.

None of that solves the problem with the first two levels of course. For those you could use e.g. \coordinate [label=above left:text] (foo) at (x,y); instead. You could use \coordinate for the last level as well for that matter.

The complete code below demonstrates both of these options. In the first tikzpicture I haven't used \coordinates for the first two levels.

output of code

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\tikzset{
  bag/.style={
    align=left,
    font=\footnotesize
  },
  end/.style={}
}
\begin{document}

\begin{center}
\begin{tikzpicture}[sloped]
\node (a)  at (0,0)    [bag] {$\ln r_0$};
\node (b1) at (3,1.5)  [bag] {$\ln r_{10} + \sigma_1\sqrt{\Delta t_1}$};
\node (b2) at (3,-1.5) [bag] {$\ln r_{10}$};

% note [bag,right], not just [bag] for the following nodes
\node (c1) at (6,3)    [bag,right] {$\ln r_{20} + 2\sigma_2\sqrt{\Delta t_2}$};
\node (c2) at (6,1.5)  [bag,right] {$\textrm{Mean} = \ln r_{20} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2}$\\$\textrm{Mean Change} = \mu_{11}\Delta t_2 = \ln\dfrac{r_{20}}{r_{10}} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2} - \sigma_1\sqrt{\Delta t_1}$};
\node (c3) at (6,0)    [bag,right] {$\ln r_{20} + \sigma_2\sqrt{\Delta t_2}$};
\node (c4) at (6,-1.5) [bag,right] {$\textrm{Mean} = \ln r_{20} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2}$\\$\textrm{Mean Change} = \mu_{11}\Delta t_2 = \ln\dfrac{r_{20}}{r_{10}} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2} - \sigma_1\sqrt{\Delta t_1}$};
\node (c5) at (6,-3)   [bag,right] {$\ln r_{20}$};

\draw [->] (a) to node [below] {} (b1);
\draw [->] (a) to node [above] {} (b2);
\draw [->] (b1) to node [below] {} (c1.west);
\draw [->] (b1) to node [below] {} (c2.west);
\draw [->] (b1) to node [below] {} (c3.west);
\draw [->] (b2) to node [below] {} (c3.west);
\draw [->] (b2) to node [below] {} (c4.west);
\draw [->] (b2) to node [below] {} (c5.west);
\end{tikzpicture}


\begin{tikzpicture}[
  sloped,
  % reduce the fontsize of all the labels
  every label/.append style={font=\footnotesize}
]
\coordinate [label=left:$\ln r_0$] (a) at (0,0);
\coordinate [label=above left:$\ln r_{10} + \sigma_1\sqrt{\Delta t_1}$] (b1) at (3,1.5);
\coordinate [label=below left:$\ln r_{10}$] (b2) at (3,-1.5);
\coordinate [label=right:$\ln r_{20} + 2\sigma_2\sqrt{\Delta t_2}$] (c1) at (6,3);
\coordinate [
   label={[align=left]right:%
      $\textrm{Mean} = \ln r_{20} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2}$ \\
      $\textrm{Mean Change} = \mu_{11}\Delta t_2 = \ln\dfrac{r_{20}}{r_{10}} + 
             \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2} - \sigma_1\sqrt{\Delta t_1}$}
]
  (c2) at (6,1.5);
\coordinate [label=right:$\ln r_{20} + \sigma_2\sqrt{\Delta t_2}$] (c3) at (6,0);
\coordinate [
   label={[align=left]right:%
        $\textrm{Mean} = \ln r_{20} + \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2}$ \\
        $\textrm{Mean Change} = \mu_{11}\Delta t_2 = \ln\dfrac{r_{20}}{r_{10}} + 
             \dfrac{3}{2}\sigma_2\sqrt{\Delta t_2} - \sigma_1\sqrt{\Delta t_1}$}
]
  (c4) at ( 6,-1.5);
\coordinate [label=right:$\ln r_{20}$] (c5) at (6,-3);

\draw [->] (a) to node [below] {} (b1);
\draw [->] (a) to node [above] {} (b2);
\draw [->] (b1) to node [below] {} (c1);
\draw [->] (b1) to node [below] {} (c2);
\draw [->] (b1) to node [below] {} (c3);
\draw [->] (b2) to node [below] {} (c3);
\draw [->] (b2) to node [below] {} (c4);
\draw [->] (b2) to node [below] {} (c5);
\end{tikzpicture}
\end{center}

\end{document}  
6
  • The diagram is at the moment a bit too wide (wider than the textblock in a standard article), I haven't done anything about that. Nov 8, 2017 at 8:47
  • thanks a lot, how to use the first style of code \node (b2) at (3,-1.5) [bag] {$\ln r_{10}$}; to achieve the result in the second style of code \coordinate [label=below left:$\ln r_{10}$] (b2) at (3,-1.5); i.e don't put the words in the node. I tried \node (b2) at (3,-1.5) [bag, left] {$\ln r_{10}$}; but it doesn't work. And how to put another word 11 below the same node?
    – A.Oreo
    Nov 8, 2017 at 9:21
  • @A.Oreo Why do you need to use the first style of code? You do \node (b2) ... [bag,below left] .., and when you draw the arrows, use (b2.north east) instead of (b2) . Nov 8, 2017 at 9:37
  • @A.Oreo For the number, you can add multiple labels to the same node or coordinate, so \coordinate [label=below:11,label=above left:...] (b2) at ... Nov 8, 2017 at 9:38
  • pls see my update code, when I use ` (b1.south east)` and (b2.north east) instead of b1 and b2, then arrows from of nodes b1 and b2 are no longer at same vertical line.
    – A.Oreo
    Nov 8, 2017 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .