5

I started using Tikz, took a sample of a code and tried to do my own. I'm almost there but I'm struggling on a couple of things.
First, here is my code:

\documentclass{article}  
\usepackage{tikz}  
\usepackage{amssymb}
\usetikzlibrary{arrows}  
\begin{document}  

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.5cm,  
                thick,main node/.style={rectangle,draw,font=\sffamily\Large\bfseries}]  

\node[main node] (1) {1};  
\node[main node] (2) [below of=1] {2};   
\node[main node] (3) [below of=2] {3};   
\node[main node] (4) [right of=1] {$\varnothing$};  
\node[main node] (5) [right of=2] {$\varnothing$};  
\node[main node] (6) [right of=3] {$\varnothing$};  

\path[every node/.style={font=\sffamily\small}]  
(1) edge node [left] {$p_1$} (2)  
    edge node [above] {$1-p_1$} (4)  
(2) edge node [left] {$p_2$} (3)  
    edge node [above] {$1-p_2$} (5)  
(3) edge [bend left] node [left] {$p_3$} (1)  
    edge node [above] {$1-p_3$} (6);
\end{tikzpicture}  
\end{document}

enter image description here

What I would like to change is:
1/ get rid of the rectangle on the empty set nodes
2/ the arrow with p3 on, make it rectangular. Is it possible to adjust how far away it goes on the left to ensure it doesn't clash with something else?
3/ change the distance between the number 1, 2, 3 with the empty set nodes. So basically, expand it to the right.

If anyone could explain me how to change any/all of them, that'll be great!
Cheers

user88595
  • 257

2 Answers2

5

There are almost certainly better ways to do this. (I've been learning for more than an hour but am an extremely slow learner!)

\documentclass[tikz]{standalone}
\usepackage{amssymb}
\usetikzlibrary{arrows,positioning}
\begin{document}

\begin{tikzpicture}
  [ ->,
    >=stealth',
    shorten >=1pt,
    auto,
    node distance=2.5cm,
    thick,
    main node/.style={rectangle,draw,font=\sffamily\Large\bfseries},
    empty node/.style={font=\sffamily\Large\bfseries, xshift=50pt}
    ]% adjust xshift depending on the amount of expansion to the right you require; empty node sets a non-box style for the empty set nodes

  \node[main node] (1) {1};
  \node[main node] (2) [below of=1] {2};
  \node[main node] (3) [below of=2] {3};
  \node[empty node] (4) [right of=1] {$\varnothing$};
  \node[empty node] (5) [right of=2] {$\varnothing$};
  \node[empty node] (6) [right of=3] {$\varnothing$};

  % This node is only here to show how to make sure you go around something: increase 1pt if you want a greater separation
  \node[shape=circle,draw,outer sep=1pt] (extra7) [left of=2] {something else};

  \path[every node/.style={font=\sffamily\small}]
    (1) edge node [left] {$p_1$} (2)
        edge node [above] {$1-p_1$} (4)
    (2) edge node [left] {$p_2$} (3)
        edge node [above] {$1-p_2$} (5)
    (3) edge node [above] {$1-p_3$} (6);

  % this will go to the left (west) of the new node extra7 and will go in straight lines rather than curving
  \path[every node/.style={font=\sffamily\small}, draw]
    (3.west) -- +(0,0) -| (extra7.west)  -- +(0,0) |- node[near start, left] {$p_3$} (1);

\end{tikzpicture}
\end{document}

Diversion around something else

Update

This avoids the syntax which HarishKumar notes can be problematic. Note that it also changes the spacing quite significantly:

\documentclass[tikz]{standalone}
\usepackage{amssymb}
\usetikzlibrary{arrows,positioning}
\begin{document}

\begin{tikzpicture}
  [ ->,
    >=stealth',
    shorten >=1pt,
    auto,
    node distance=2.5cm,
    thick,
    main node/.style={rectangle,draw,font=\sffamily\Large\bfseries},
    empty node/.style={font=\sffamily\Large\bfseries, xshift=50pt}
    ]

  \node[main node] (1) {1};
  \node[main node] (2) [below=of 1] {2};
  \node[main node] (3) [below=of 2] {3};
  \node[empty node] (4) [right=of 1] {$\varnothing$};
  \node[empty node] (5) [right=of 2] {$\varnothing$};
  \node[empty node] (6) [right=of 3] {$\varnothing$};

  \node[shape=circle,draw,outer sep=1pt] (extra7) [left=of 2] {something else};

  \path[every node/.style={font=\sffamily\small}]
    (1) edge node [left] {$p_1$} (2)
        edge node [above] {$1-p_1$} (4)
    (2) edge node [left] {$p_2$} (3)
        edge node [above] {$1-p_2$} (5)
    (3) edge node [above] {$1-p_3$} (6);

  \path[every node/.style={font=\sffamily\small}, draw]
    (3.west) -- +(0,0) -| (extra7.west)  -- +(0,0) |- node[near start, left] {$p_3$} (1);

\end{tikzpicture}
\end{document}

Larger diversion around something else

cfr
  • 198,882
  • Exactly what I had in mind, thanks a lot. Something else node is a nice bonus I can learn from and makes it easier to understand how it works. – user88595 Apr 23 '14 at 23:17
  • @TAllieri You're welcome. I keep trying to learn this... [I'm really surprised there were not half a dozen answers by the time I'd finished!] – cfr Apr 23 '14 at 23:22
  • @HarishKumar OK. Is there a list of Stuff Not To Be Used somewhere? Because the tikz manual itself seems a less than perfectly reliable source of information on this. See, for example, page 237 where the code includes above right of= and below right of=. I'm not sure what the disadvantages of using this syntax are (other than its being 'obsolete') but it would be nice if the documentation at least declared its own examples null and void if that's what they are :(. I think I've learnt the new syntax but I certainly didn't know to 'update' the syntax in the OP's MWE. – cfr Apr 23 '14 at 23:32
  • @HarishKumar But is there a list somewhere? You said that it was 'obsolete'. If so, presumably that's documented somewhere? – cfr Apr 23 '14 at 23:36
  • @TAllieri See HarishKumar's comments. I was going to change this in the code I posted but it changes the spacing so it is apparently not merely an alternative, updated syntax but a more substantive change... – cfr Apr 23 '14 at 23:40
  • @cfr: will take it into account thanks. That manual will be a good addition to my library, is it the TikZ and PGF by Till Tantau you talk about? 880 pages long? – user88595 Apr 23 '14 at 23:42
  • @HarishKumar OK. Thanks. [What's wrong with tikzset?] I have read that. I think that's why I use the new syntax (except when I've got a working example right above as in this case). – cfr Apr 23 '14 at 23:46
  • @TAllieri If I type texdoc tikz, I get the manual. Mine is 1,165 pages long! But it is indeed by Till Tantau. Mine is for version 3.0.0. – cfr Apr 23 '14 at 23:47
  • @cfr: the one I found was version 2.10. Cheers for everything! – user88595 Apr 23 '14 at 23:50
  • @cfr: the pgfmanual is not the sole source for TikZ documentation; I think some Q/A here extend and keep up to date the evolution. For example, see Difference between “right of=” and “right=of” in PGF/TikZ and Should \tikzset or \tikzstyle be used to define TikZ styles? – Claudio Fiandrino Apr 24 '14 at 06:31
  • @ClaudioFiandrino Thanks for the links. I realise it is not the sole source. I'm just not clear how mere mortals are supposed to figure out what is and isn't kosher. Also, doesn't the second thread say that \tikzset should be preferred to tikzstyle rather than the other way around? – cfr May 03 '14 at 14:06
  • @cfr: don't worry, it takes time, but following Q/A here will improve a lot your skills. And yes, the second link I posted, says it is better to use tikzset rather than tikzstyle. – Claudio Fiandrino May 05 '14 at 07:01
5
\documentclass{article}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{arrows,positioning}
\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.5cm,
                thick,main node/.style={rectangle,draw,font=\sffamily\Large\bfseries}]   %% Use draw = none to get rid of border on all nodes.

\node[main node] (1) {1};
\node[main node] (2) [below = of 1] {2};
\node[main node] (3) [below = of 2] {3};
\node[main node,draw = none] (4) [right = 2in of 1] {$\varnothing$};
\node[main node,draw = none] (5) [right = 2in of 2] {$\varnothing$};
\node[main node,draw = none] (6) [right = 2in of 3] {$\varnothing$};

\path[every node/.style={font=\sffamily\small}]
(1) edge node [left] {$p_1$} (2)
    edge node [above] {$1-p_1$} (4)
(2) edge node [left] {$p_2$} (3)
    edge node [above] {$1-p_2$} (5)
(3) edge [bend left,distance=1.5in] node [left] {$p_3$} (1)
    edge node [above] {$1-p_3$} (6);
\end{tikzpicture}
\end{document}

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{arrows,positioning}
\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.5cm,
                thick,main node/.style={rectangle,draw,font=\sffamily\Large\bfseries}]   %% Use draw = none to get rid of border on all nodes.

\node[main node] (1) {1};
\node[main node] (2) [below = of 1] {2};
\node[main node] (3) [below = of 2] {3};
\node[main node,draw = none] (4) [right = 2in of 1] {$\varnothing$};
\node[main node,draw = none] (5) [right = 2in of 2] {$\varnothing$};
\node[main node,draw = none] (6) [right = 2in of 3] {$\varnothing$};

\path[every node/.style={font=\sffamily\small}]
(1) edge node [left] {$p_1$} (2)
    edge node [above] {$1-p_1$} (4)
(2) edge node [left] {$p_2$} (3)
    edge node [above] {$1-p_2$} (5)
(3)   edge node [above] {$1-p_3$} (6);
\path[draw,every node/.style={font=\sffamily\small}]
(3.west) -- ([xshift=-2cm]3.west) -- ([xshift=-2cm]1.west) node [midway,left] {$p_3$} -- (1.west);    %% change xshift as you wish
\end{tikzpicture}
\end{document}

enter image description here