Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

First time using Tikz. Haven't found any documentation on drawing graphs like these, so I have been reading other examples and trying to modify their code to fit my example.

I am trying to reproduce something like this Markov Chain (please excuse my very crude drawing!):

enter image description here

with edges labeled with probabilities which I've tried to incorporate into my code below:

\documentclass[letter,10pt]
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{automata}
\begin{document}

\begin{center}
\begin{tikzpicture}[->,>=stealth',auto,semithick,node distance=3cm]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=1]
\node[state]    (A)                     {$A$};
\node[state]    (B)[above right of=A]   {$B$};
\node[state]    (C)[below right of=A]   {$C$};
\node[state]    (D)[below right of=B]   {$D$};
\path
(A) edge[loop left]     node{$p^2$}         (A);
    edge[bend left]     node{$(1-p)^2$}     (B);
    edge                node{$p(1-p)$}      (D);
    edge[bend right]    node{$p(1-p)$}      (C);
(B) edge                node{$1$}           (D);
(C) edge                node{$1$}           (D);
(D) edge[loop right]    node{$(1-q)^2$}     (D);
    edge[bend right]    node{$q(1-q)$}      (B);
    edge[bend left]     node{$q(1-q)$}      (C);
    edge                node{$q^2$}         (A);
\end{tikzpicture}
\end{center}

Even though I think my structure is correct, most of the paths don't show up and the labels are jumbled and unreadable. If it's also possible, I'd like to group together states $B$, $C$ and $D$ as a patch called $H$, and call state $A$ patch $G$.

Thank you very much for your help!

share|improve this question

1 Answer 1

up vote 7 down vote accepted

This is an attempt. Only one ; is needed at the end of \path command. Also an oval is added to circle the Patch H and the single line between (A) and (D) is changed to depict lines in the crude image.

enter image description here

Code

\documentclass[letter,10pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{automata,arrows,positioning,calc}
\begin{document}

\begin{center}
\begin{tikzpicture}[->, >=stealth', auto, semithick, node distance=3cm]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=1]
\node[state]    (A)                     {$A$};
\node[state]    (B)[above right of=A]   {$B$};
\node[state]    (C)[below right of=A]   {$C$};
\node[state]    (D)[below right of=B]   {$D$};
\path
(A) edge[loop left]     node{$p^2$}         (A)
    edge[bend left]     node{$(1-p)^2$}     (B)
    edge[bend left,below]      node{$p(1-p)$}      (D)
    edge[bend right]    node{$p(1-p)$}      (C)
(B) edge                node{$1$}           (D)
(C) edge                node{$1$}           (D)
(D) edge[loop right]    node{$(1-q)^2$}     (D)
    edge[bend right,right]    node{$q(1-q)$}      (B)
    edge[bend left]     node{$q(1-q)$}      (C)
    edge[bend left,above]     node{$q^2$}         (A);
\node[above=0.5cm] (A){Patch G};
\draw[red] ($(D)+(-1.5,0)$) ellipse (2cm and 3.5cm)node[yshift=3cm]{Patch H};
\end{tikzpicture}
\end{center}
\end{document}
share|improve this answer
    
Thank you so much! I didn't know what the semicolons did. That must be why none of my edges showed up. Thank you so much for your time! –  EthanAlvaree Jul 29 at 5:18
    
You are welcome. Indeed, keep in mind, only the last ; is required. –  Jesse Jul 29 at 5:20
    
Also, if you don't want to "hardcode" the position and dimensions of the ellipse, you can \usetikzlibrary{fit} and: \node[draw=red,ellipse, inner sep=0pt, fit=(B) (C) (D)] (patch) {}; \node[below=5mm of patch.north, red] {Patch H}; –  JLDiaz Jul 29 at 8:20
    
Thanks @JLDiaz, did think of that. But thinking that mine was quicker/easier, IMHO. –  Jesse Jul 29 at 8:30
    
A very great example which helped me a lot! Thank you for posting this! –  math12 Oct 13 at 17:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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