Graphics, Figures & TablesMarkov Latex

Information and discussion about graphics, figures & tables in LaTeX documents.
Estabilo
Posts: 12
Joined: Tue May 12, 2015 11:08 am

Markov Latex

Post by Estabilo »

Hello people

how to do this Markov diagram in Latex?

Thankss
Attachments
markov latex.png
markov latex.png (30.08 KiB) Viewed 13622 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10330
Joined: Mon Mar 10, 2008 9:44 pm

Markov Latex

Post by Stefan Kottwitz »

Like this?

Code: Select all

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{calc, automata, chains, arrows.meta}
\begin{document}
\begin{tikzpicture}[start chain = going right,
  -Triangle, every loop/.append style = {-Triangle}]
  \foreach \i in {0,...,4} 
    \node[state, on chain]  (\i) {\i};
  \foreach \i in {0,...,3} {
    \draw let \n1 = { int(\i+1) } in
      (\i)  edge[bend left] (\n1)
      (\n1) edge[bend left] (\i);
  }
  \foreach \i in {1,...,3}
    \draw  (\i) edge[loop below] (\i);
  \draw    (0)  edge[loop left]   (0);
  \draw    (4)  edge[loop right]  (4);
\end{tikzpicture}
\end{document}
tikz-chain-diagram.png
tikz-chain-diagram.png (9.02 KiB) Viewed 13612 times
Stefan
LaTeX.org admin
Estabilo
Posts: 12
Joined: Tue May 12, 2015 11:08 am

Re: Markov Latex

Post by Estabilo »

yesssssss is perfect!!!!!

But one thing more, I would to writte text or number above the arrows.

I do not know how to do it.

Can you help me?

Thanks
User avatar
Stefan Kottwitz
Site Admin
Posts: 10330
Joined: Mon Mar 10, 2008 9:44 pm

Re: Markov Latex

Post by Stefan Kottwitz »

Sure, this is easy with nodes on the edges.

I just need to go to sleep now, I will write again tomorrow.

Stefan
LaTeX.org admin
Estabilo
Posts: 12
Joined: Tue May 12, 2015 11:08 am

Re: Markov Latex

Post by Estabilo »

Okey Stefan ,I wait :) :)
Estabilo
Posts: 12
Joined: Tue May 12, 2015 11:08 am

Re: Markov Latex

Post by Estabilo »

all okey Stefan?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10330
Joined: Mon Mar 10, 2008 9:44 pm

Markov Latex

Post by Stefan Kottwitz »

I just have few time. Don't worry, I like TikZ questions.

It's actually simple: the new quotes library, which comes with TikZ 3.0 or later, brings an easy syntax instead of adding nodes:

Code: Select all

\draw  (0)  edge[loop left, "left"]   (0);
\draw  (4)  edge[loop right, "right"]  (4);
A user variable can also be added for text or numbers in a for loop:

Code: Select all

\foreach \i/\txt in {1/here,2/there,3/here too}
  \draw  (\i) edge[loop below, "\txt"] (\i);
Complete example - note that I added the quotes library at the beginning:

Code: Select all

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{calc, automata, chains, arrows.meta, quotes}
\begin{document}
\begin{tikzpicture}[start chain = going right,
  -Triangle, every loop/.append style = {-Triangle}]
  \foreach \i in {0,...,4} 
    \node[state, on chain]  (\i) {\i};
  \foreach \i in {0,...,3} {
    \draw let \n1 = { int(\i+1) } in
      (\i)  edge[bend left] (\n1)
      (\n1) edge[bend left] (\i);
  }
  \foreach \i/\txt in {1/here,2/there,3/here too}
    \draw  (\i) edge[loop below, "\txt"] (\i);
  \draw    (0)  edge[loop left, "left"]   (0);
  \draw    (4)  edge[loop right, "right"]  (4);
\end{tikzpicture}
\end{document}
markov-chain.png
markov-chain.png (16.76 KiB) Viewed 13572 times
Stefan
LaTeX.org admin
Estabilo
Posts: 12
Joined: Tue May 12, 2015 11:08 am

Markov Latex

Post by Estabilo »

Sorry Stefan, I have seen the code during all day and I have a problem that I don`t know solve :(
If you run/execute this code, you will se the strange problem

Code: Select all

\documentclass[12pt,a4paper,tikz,border=10pt]{article}
\usepackage[spanish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{listings}
\usepackage{fancybox}
\usepackage{fancyvrb}
\usepackage{enumerate} 
\usepackage{pstricks}
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{parskip}


\usepackage{tikz}
\usetikzlibrary{calc, automata, chains, arrows.meta, quotes}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=3cm,right=2cm,top=1cm,bottom=2cm]{geometry}

\title{Cadenes de Markov}
\date{}

%%%%%%%%%%%%%%%%%%%%% COLORES %%%%%%%%%%%%%%%%%%%%%%%

\usepackage{color}
\definecolor{gray97}{gray}{.97}
\definecolor{gray75}{gray}{.75}
\definecolor{gray45}{gray}{.45}
\usepackage{mdframed}
\usepackage{subfig}
\usepackage{epsfig}
\usepackage{enumitem}
\usepackage{listings}
 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\maketitle


\noindent \large{\textbf{Ejercicio 1.}}
\\
\begin{tikzpicture}[start chain = going right,
  -Triangle, every loop/.append style = {-Triangle}]
  \foreach \i in {0,...,4} 
    \node[state, on chain]  (\i) {\i};
  \foreach \i in {0,...,3} {
    \draw let \n1 = { int(\i+1) } in
      (\i)  edge[bend left] (\n1)
      (\n1) edge[bend left] (\i);
  }
  \foreach \i/\txt in {1/here,2/there,3/here too}
    \draw  (\i) edge[loop below, "\txt"] (\i);
  \draw    (0)  edge[loop left, "left"]   (0);
  \draw    (4)  edge[loop right, "right"]  (4);
\end{tikzpicture}



\end{document}
thanks
User avatar
Stefan Kottwitz
Site Admin
Posts: 10330
Joined: Mon Mar 10, 2008 9:44 pm

Markov Latex

Post by Stefan Kottwitz »

It's caused by the shorthand " which is a spanish babel command. You could disable it by

Code: Select all

\shorthandoff{"}
Either globally in the preamble, if you don't use it, or locally in the TikZ picture, at the beginning.

Stefan
LaTeX.org admin
Estabilo
Posts: 12
Joined: Tue May 12, 2015 11:08 am

Markov Latex

Post by Estabilo »

oh yes!! thanks Stefan !!!
Sorry but I need the last one thing, I don´t know to writte above translation.
I tried this but it does not run

Code: Select all

\draw (\i)  edge[bend left,"\txt"] (\n1)
\draw (0)  edge[bend left,"a"] (1)
but not compilation :S

I need this: (photo)



thanks
Attachments
markov-writte.png
markov-writte.png (17.55 KiB) Viewed 13540 times
Post Reply