3
\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{trees,positioning,fit}

\begin{document}

$$
\begin{tikzpicture}[sloped]
\node (a) at ( 0,0) [bag] {$C(0, 1)$\\\vspace{5pt}$S(0, 1)$};
\node (b1) at ( 4,1.5) [bag] {$C(\Delta t, 1)$\\\vspace{5pt}$S(\Delta t, 1)$};
\node (b2) at ( 4,-1.5) [bag] {$C(\Delta t, 2)$\\\vspace{5pt}$S(\Delta t, 2)$};
\node (c1) at ( 8,3) [bag] {$C(2\Delta t, 1)$\\\vspace{5pt}$S(2\Delta t, 1)$};
\node (c2) at ( 8,0) [bag] {$C(2\Delta t, 2)$\\\vspace{5pt}$S(2\Delta t, 2)$};
\node (c3) at ( 8,-3) [bag] {$C(2\Delta t, 3)$\\\vspace{5pt}$S(2\Delta t, 3)$};
\draw [->] (a) to node [above]{} (b1);
\draw [->] (a) to node [below]{} (b2);
\draw [->] (b1) to node [above]{} (c1);
\draw [->] (b1) to node [above]{} (c2);
\draw [->] (b2) to node [above]{} (c2);
\draw [->] (b2) to node [above]{} (c3);
\end{tikzpicture}
$$

$$
 \begin{tikzpicture}[sloped]
   \node (a) at ( 0,0) [bag] {$j\Delta S$};
   \node (b1) at ( 3,2) [bag] {$(j-1)\Delta S$};
   \node (b2) at ( 3,0) [bag]  {$j\Delta S$};
   \node (b3) at ( 3,-2) [bag] {$(j+1)\Delta S$};   
   \draw [->] (a) to node [above] {$p$} (b1);
   \draw [->] (a) to node [above] {$1-p-q$} (b2);
   \draw [->] (a) to node [below] {$q$} (b3);

\end{tikzpicture}
$$

\end{document}

The pictures are shown below. There are three problems:

  1. In the first picture, arrows overlap the formula in the middle.

  2. To spread the first line and second line in the formula, I use very naive way $C(\Delta t, 2)$\\\vspace{5pt}$S(\Delta t, 2)$, is there any effective way?

  3. In the second picture, why the formula $(j-1)\Delta S$ automatically change the line.

enter image description here

enter image description here

dexteritas
  • 9,161
A.Oreo
  • 319

2 Answers2

3

Explanation

Both your first and last problem has the same cause: the definition of bag. You haven't shown us the exact definition, but I'm guessing you have something like

bag/.style={text width=0.5cm}

Setting text width means that the node becomes a minipage like box with the given width. That in turn means that LaTeX will automatically add line breaks if the node text becomes longer, but if no break points are found, you'll end up with an overfull box, i.e. the text will protrude out of the box on the right side. However, the size of the node will not be influenced by such an overflow.

You can see how this causes your problem 1 and 3 if you add draw to the bag style, in which case you get this:

bag,draw

(You may also have noticed that you get a lot of overfull hbox warnings from this.)

Possible solution

Point 1 and 3

bag/.style={align=left}

Setting align will allow for line breaks (\\), but the width of the node will be the length of the longest line.

Point 2

  • \\ takes an optional argument to specify additional distance, so you could do \\[5pt] instead of the more verbose \vspace.

  • Another option would be to add \usepackage{setspace} to the preamble, and define the bag style as

     bag/.style={align=left,font=\onehalfspacing}
    

    (or \doublespacing, if \onehalfspacing isn't enough).

  • A third option would be to first define a new macro:

    \newcommand\minimathtab[1]{%
      \renewcommand\arraystretch{1.4}% % modify 1.4 to suit your need
      \begin{tabular}{@{}>{$}l<{$}@{}}
      #1
      \end{tabular}
    }
    

    In the diagram, don't use the bag style (or in fact any style), but apply that macro:

    \node (a)  {\minimathtab{C(0, 1) \\ S(0, 1)}};
    

Other things to note

  • Don't use $$ .. $$ in LaTeX. For unnumbered displayed equations, use \[ ... \]. See Why is \[ ... \] preferable to $$ ... $$?

    In this case though, I would probably just use a center environment.

  • You've loaded the positioning library, but you're not using its features. It allows for positioning nodes relative to other nodes, at a distance that can be set for the entire diagram. This means you can position everything without using explicit coordinates, and it's very easy to adjust the spacing, as you only need to adjust the node distance

Complete code

\documentclass{book}
\usepackage{setspace}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{bag/.style={align=left,font=\onehalfspacing}}

\newcommand\minimathtab[1]{%
  \renewcommand\arraystretch{1.2}%
   \begin{tabular}{@{}>{$}l<{$}@{}}
   #1
   \end{tabular}
}
\begin{document}

\begin{center}

\begin{tikzpicture}[
  % first value is vertical separation, second value is horizontal 
  node distance=2mm and 1.5cm
]

\begin{scope}[
  % apply the bag style to all the nodes inside the scope environment
  every node/.style=bag
]
\node                     (a)  {$C(0, 1) $ \\ $ S(0, 1)$};
\node [above right=of a]  (b1) {$C(\Delta t, 1) $ \\ $ S(\Delta t, 1)$};
\node [below right=of a]  (b2) {$C(\Delta t, 2) $ \\ $ S(\Delta t, 2)$};
\node [above right=of b1] (c1) {$C(2\Delta t, 1) $ \\ $ S(2\Delta t, 1)$};
\node [below right=of b1] (c2) {$C(2\Delta t, 2) $ \\ $ S(2\Delta t, 2)$};
\node [below right=of b2] (c3) {$C(2\Delta t, 3) $ \\ $ S(2\Delta t, 3)$};
\end{scope}

\draw [->] (a) -- (b1);
\draw [->] (a) -- (b2);
\draw [->] (b1) -- (c1);
\draw [->] (b1) -- (c2);
\draw [->] (b2) -- (c2);
\draw [->] (b2) -- (c3);
\end{tikzpicture}

\begin{tikzpicture}[sloped,node distance=1cm and 2cm]
   \node (a) {$j\Delta S$};
   \node [above right=of a] (b1) {$(j-1)\Delta S$};
   \node [right=of a] (b2)  {$j\Delta S$};
   \node [below right=of a] (b3) {$(j+1)\Delta S$};   

   \draw [->] (a) to node [above] {$p$}     (b1.south west);
   \draw [->] (a) to node [above] {$1-p-q$} (b2);
   \draw [->] (a) to node [below] {$q$}     (b3.north west);
\end{tikzpicture}



\begin{tikzpicture}[node distance=2mm and 1.5cm]
\node                     (a)  {\minimathtab{C(0, 1) \\ S(0, 1)}};
\node [above right=of a]  (b1) {\minimathtab{C(\Delta t, 1) \\ S(\Delta t, 1)}};
\node [below right=of a]  (b2) {\minimathtab{C(\Delta t, 2) \\ S(\Delta t, 2)}};
\node [above right=of b1] (c1) {\minimathtab{C(2\Delta t, 1) \\ S(2\Delta t, 1)}};
\node [below right=of b1] (c2) {\minimathtab{C(2\Delta t, 2) \\ S(2\Delta t, 2)}};
\node [below right=of b2] (c3) {\minimathtab{C(2\Delta t, 3) \\ S(2\Delta t, 3)}};


\draw [->] (a) -- (b1);
\draw [->] (a) -- (b2);
\draw [->] (b1) -- (c1);
\draw [->] (b1) -- (c2);
\draw [->] (b2) -- (c2);
\draw [->] (b2) -- (c3);
\end{tikzpicture}
\end{center}
\end{document}
Torbjørn T.
  • 206,688
1

see, it this is what you looking for:

  • instead $$ ... $$ i use \begin{center} ... \end{center} for two reasons:

    1. $$ belong to tex syntax and not to latex
    2. i don't see any benefits to has tikzpicture images in math environment
  • i omit use of bag style. it seems that it is source of your problem, but its purpose is not clear. it is not used for automatic break nodes content into two lines,

  • i remove manual adding vertical space between lines in nodes and replace it by following image style definition:

      \begin{tikzpicture}[sloped, 
align = left, 
 font = \linespread{1.2}\selectfont
                         ]
...
\end{tikzpicture}
  • mwe below also contain simple solution with automatic break longer text into two lines
  • second image can be also drawn as simple tree as can be seen in mwe below

\documentclass{article}
\usepackage[margin=20mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
    \begin{center}
      \begin{tikzpicture}[sloped, 
align = left, 
 font = \linespread{1.2}\selectfont
                         ]
\node (a)               {$C(0,1)$\\         $S(0, 1)$};
\node (b1) at (4, 1.5)  {$C(\Delta t,1)$\\  $S(\Delta t, 1)$};
\node (b2) at (4,-1.5)  {$C(\Delta t,2)$\\  $S(\Delta t, 2)$};
\node (c1) at (8, 3.0)  {$C(2\Delta t,1)$\\ $S(2\Delta t, 1)$};
\node (c2) at (8, 0.0)  {$C(2\Delta t,2)$\\ $S(2\Delta t, 2)$};
\node (c3) at (8,-3.0)  {$C(2\Delta t,3)$\\ $S(2\Delta t, 3)$};
\draw [->]  (a)  edge (b1)
            (a)  edge (b2)
            (b1) edge (c1)
            (b1) edge (c2)
            (b2) edge (c2)
            (b2) edge (c3);
\end{tikzpicture}
    \end{center}

% first image with automatic breaking cells content into more lines
    \begin{center}
      \begin{tikzpicture}[sloped,
     align = left,
      font = \linespread{1.2}\selectfont,
text width = 9ex 
                         ]
\node[align=right]  (a) {$C(0,1)$         $S(0, 1)$};
\node (b1) at (4, 1.5)  {$C(\Delta t,1)$  $S(\Delta t, 1)$};
\node (b2) at (4,-1.5)  {$C(\Delta t,2)$  $S(\Delta t, 2)$};
\node (c1) at (8, 3.0)  {$C(2\Delta t,1)$ $S(2\Delta t, 1)$};
\node (c2) at (8, 0.0)  {$C(2\Delta t,2)$ $S(2\Delta t, 2)$};
\node (c3) at (8,-3.0)  {$C(2\Delta t,3)$ $S(2\Delta t, 3)$};
\draw [->]  (a)  edge (b1)
            (a)  edge (b2)
            (b1) edge (c1)
            (b1) edge (c2)
            (b2) edge (c2)
            (b2) edge (c3);
\end{tikzpicture}
    \end{center}

    \begin{center}
 \begin{tikzpicture}[sloped,
    node distance=2cm and 3cm]
   \node (a)  at (0, 0) {$j\Delta S$};
   \node (b1) at (3, 2) {$(j-1)\Delta S$};
   \node (b2) at (3, 0) {$j\Delta S$};
   \node (b3) at (3,-2) {$(j+1)\Delta S$};
   \draw [->] (a) to node [above] {$p$} (b1.south west);
   \draw [->] (a) to node [above] {$1-p-q$} (b2.west);
   \draw [->] (a) to node [below] {$q$} (b3.north west);
\end{tikzpicture}
    \end{center}

% second image drawn as tree
    \begin{center}
\begin{tikzpicture}[sloped,
    level distance=30mm,
    sibling distance=15mm,
    grow=right,
    edge from parent/.style = {draw, semithick, ->},
    edge from parent path=
{(\tikzparentnode) -- (\tikzchildnode.west)}
                    ]
\node  {$j\Delta S$}
    child{node {$(j-1)\Delta S$}    edge from parent node[above] {$p$}}
    child{node {$j\Delta S$}        edge from parent node[above] {$1-p-q$}}
    child{node {$(j+1)\Delta S$}    edge from parent node[above] {$q$}};
\end{tikzpicture}
    \end{center}
\end{document}

enter image description here

Zarko
  • 296,517