TransWikia.com

Align two trees in TiKZ (states/nodes)

TeX - LaTeX Asked by Tamar on July 6, 2021

In the following graph, i would like to align the tree with the states 11,12, 13, 14 such that 11-14 is a vertical chain, with state 11 being right of state 1, state 12 being right of state 3, state 13 being right of 6 and state 14 being right of state 10. How can I do this?

documentclass{article}

usepackage{tikz}
usepackage{amsmath}
usetikzlibrary{automata}
usetikzlibrary{calc,arrows.meta,positioning}

newcommand{iddots}{reflectbox{$ddots$}}
begin{document}

resizebox{textwidth}{!}{
begin{tikzpicture}[->,-latex,shorten >=1pt,auto,node distance=40mm,semithick, state/.style={circle, draw, minimum size=2cm}]
node[state](1)                  {$S_0$};
node[state](2)[below left  of=1] {$dcdot S_0$};
node[state](3)[below right of=1]{$u cdot S_0$};
node[state](4)[below left  of=2] {$d^2 cdot S_0$};
node[state](5)[below right of=2]{$du cdot S_0$};
node[state](6)[below right of=3]{$u^2 cdot S_0$};
node[state](7)[below left of=4]{$d^N cdot S_0$};
node[state](8)[below right of=4]{$d^k u^{N-k} cdot S_0$};
node[state](9)[below right of=5]{$d^{N-k}u^{k}cdot S_0$};
node[state](10)[below right of=6]{$u^N cdot S_0$};
node[state](14)[right of=10]{$S_1$};
node[state](13)[above of=14, right of=6]{$S_1$};
node[state](12)[above of=13, right of=3]{$S_1$};
node[state](11)[above of=12, right of=1]{$S_1$};

path (1) edge [swap] node {$1-p$}(2)
      (1) edge        node {$p$}(3)
      (2) edge [swap] node {$1-p$}(4)
      (2) edge        node {$p$}(5)
      (3) edge [swap] node {$1-p$}(5)
      (3) edge        node {$p$}(6);
       node at ($(7)!.5!(8)$) {$hdots$};
       node at ($(8)!.5!(9)$) {$hdots$};
       node at ($(9)!.5!(10)$) {$hdots$};
       
       node at ($(4)!.5!(7)$) {iddots};
       node at ($(5)!.5!(8)$) {iddots};
       node at ($(6)!.5!(9)$) {iddots};
       
       node at ($(4)!.5!(8)$) {$ddots$};
       node at ($(5)!.5!(9)$) {$ddots$};
       node at ($(6)!.5!(10)$) {$ddots$};
end{tikzpicture}
}
end{document}

What I want (more or less):
desired output

One Answer

Like I mentioned in the comments, if you have two nodes, you can 'calculate' the corner of an imaginary rectangle using -| to place a node at this position. In your case, you have nodes (14) and (1), (3), (6), where you can align the other nodes at the desired positions.

enter image description here

documentclass[border=5pt]{standalone}

usepackage{tikz}
usepackage{amsmath}
usetikzlibrary{automata}
usetikzlibrary{calc,arrows.meta,positioning}

newcommand{iddots}{reflectbox{$ddots$}}
begin{document}
    
    resizebox{textwidth}{!}{
        begin{tikzpicture}[->,-latex,shorten >=1pt,auto,node distance=40mm,semithick, state/.style={circle, draw, minimum size=2cm}]
            node[state](1)                  {$S_0$};
            node[state](2)[below left  of=1] {$dcdot S_0$};
            node[state](3)[below right of=1]{$u cdot S_0$};
            node[state](4)[below left  of=2] {$d^2 cdot S_0$};
            node[state](5)[below right of=2]{$du cdot S_0$};
            node[state](6)[below right of=3]{$u^2 cdot S_0$};
            node[state](7)[below left of=4]{$d^N cdot S_0$};
            node[state](8)[below right of=4]{$d^k u^{N-k} cdot S_0$};
            node[state](9)[below right of=5]{$d^{N-k}u^{k}cdot S_0$};
            node[state](10)[below right of=6]{$u^N cdot S_0$};
            node[state](14)[right of=10]{$S_1$};
            node[state](13) at (6-|14) {$S_1$};
            node[state](12) at (3-|14) {$S_1$};
            node[state](11) at (1-|14) {$S_1$};
            
            path (1) edge [swap] node {$1-p$}(2)
            (1) edge        node {$p$}(3)
            (2) edge [swap] node {$1-p$}(4)
            (2) edge        node {$p$}(5)
            (3) edge [swap] node {$1-p$}(5)
            (3) edge        node {$p$}(6);
            node at ($(7)!.5!(8)$) {$hdots$};
            node at ($(8)!.5!(9)$) {$hdots$};
            node at ($(9)!.5!(10)$) {$hdots$};
            
            node at ($(4)!.5!(7)$) {iddots};
            node at ($(5)!.5!(8)$) {iddots};
            node at ($(6)!.5!(9)$) {iddots};
            
            node at ($(4)!.5!(8)$) {$ddots$};
            node at ($(5)!.5!(9)$) {$ddots$};
            node at ($(6)!.5!(10)$) {$ddots$};
        end{tikzpicture}
    }
end{document}

Correct answer by Excelsior on July 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP