I need to draw this picture using tikz.

enter image description here

I use tikz rarely so I find it difficult to accomplish this task. This is what I tried using some code from the internet but I can not find how to draw the curved arrows.

\foreach \i in {0,...,4}
    \draw (-1+5*\i,0) -- (0+5*\i,1) -- (1+5*\i,0) -- (0+5*\i,-1) --cycle;
\node  at (5*\i,0) {\huge$\i$};
  • Where did you copy the code from? That code is very bad. – user156344 May 18 at 14:49
  • Yes it's bad:-) – user296113 May 18 at 14:50
  • 1
    What I mean is, well, you should find another source – user156344 May 18 at 14:51
  • It would be easier to see what the image is explaining if the diamonds were shaded. – Benjamin McKay May 18 at 20:23
\foreach \i in {0,...,4} \node[draw,diamond] (\i) at (2*\i,0) {$\i$};
\foreach \i [count=\j] in {0,...,3} {
    \draw[->] (\i) to[bend left] (\j);
    \draw[->] (\i) to[bend right] (\j);
\path[->] (1) to[bend left] node[midway,above,inner sep=2pt] {1/4} (2);
\path[->] (0) to[bend right] node[midway,below,inner sep=2pt] {1/4} (1);
\draw[->] (0) to[out=-150,in=150,loop] node[midway,left,inner sep=2pt] {1} ();
\draw[->] (1) to[out=-60,in=-120,loop] node[midway,below,inner sep=2pt] {1/2} ();
\draw[->] (2) to[out=-60,in=-120,loop] ();
\draw[->] (3) to[out=-60,in=-120,loop] ();
\draw[->] (4) to[out=30,in=-30,loop] node[midway,right,inner sep=2pt] {1} ();

enter image description here


A version using tikz-cd.

\[\tikzset{loop below/.style={out=-60,in=-120,loop},
loop left/.style={out=210,in=150,loop},
loop right/.style={out=30,in=-30,loop},
loop above/.style={out=60,in=120,loop}}
 0 \ar[r,bend left] \ar[loop left,"1" ] & 
 1 \ar[r,bend left,"1/4"] \ar[l,bend left,"1/4"] \ar[loop below,"1/2"] & 
 2 \ar[r,bend left] \ar[l,bend left] \ar[loop below] &
 3 \ar[r,bend left] \ar[l,bend left] \ar[loop below] & 
 4 \ar[l,bend left] \ar[loop right]

enter image description here

And this is similar to Joule V's answer but everything in one loop and label placement with auto.

\foreach \X [remember=\X as \LastX] in {0,...,4}
 {\node[diamond,draw] (d\X) at (2*\X,0) {\X};
  \draw[-latex] (d\X) to[out=150,in=210,loop] 
    node[auto,swap]{\pgfmathparse{{\lstLoop}[\X]}$\pgfmathresult$} (d\X) ;
  \draw[-latex] (d\LastX) to[bend left] 
        node[auto]{\pgfmathparse{{\lstAbove}[\X]}$\pgfmathresult$}  (d\X);
  \draw[-latex] (d\X) to[bend left] 
   \draw[-latex] (d\X) to[out=-60,in=-120,loop] 
    node[auto]{\pgfmathparse{{\lstLoop}[\X]}$\pgfmathresult$} (d\X);
   \draw[-latex] (d\X) to[out=30,in=-30,loop] 
   node[auto]{\pgfmathparse{{\lstLoop}[\X]}$\pgfmathresult$} (d\X) ;

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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