Skip to content

Python script to create a simple LaTeX graph using a Markov chain matrix

Notifications You must be signed in to change notification settings

jonathanberthias/LaTeX-Markov-chain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

LaTeX Markov chain

Python script to create a simple LaTeX graph using a Markov chain matrix

Usage

From the folder containing the Python script and your matrix as a *.txt file:

$ python graphy.py your_matrix.txt

Only put the matrix in your_matrix.txt. Don't put any other LaTeX code such as \begin{matrix}.

Example 'matrix.txt' file:

0   & 1   & 0   \\
0   & 1/3 & 2/3 \\
1/2 & 1/2 & 0

Default behavior

By default, nodes are numbered starting from 1 and are placed to the right of each other. Edges are bent to the left, and loops are drawn above the node.

The Result isn't perfect!

This script's purpose is to auto-generate a code sample for your LaTeX document. You may have to tweak it for the resulting graph to look nice.

Example

Code generated by the script given the above matrix:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata, positioning}
\begin{document}

\begin{center}
\begin{tikzpicture}

\node[state] (1) {1};
\node[state, right=of 1] (2) {2};
\node[state, right=of 2] (3) {3};

\draw[every loop, >=latex]
(1) edge[bend left, auto=left] node {1} (2)
(2) edge[loop above] node {1/3} (2)
(2) edge[bend left, auto=left] node {2/3} (3)
(3) edge[bend left, auto=left] node {1/2} (1)
(3) edge[bend left, auto=left] node {1/2} (2);

\end{tikzpicture}
\end{center}

\end{document}

After tweaking it a little bit, it looks like:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata, positioning}
\begin{document}

\begin{center}
\begin{tikzpicture}

\node[state] (1) {1};
\node[state, below left=of 1] (2) {2};
\node[state, below right=of 1] (3) {3};

\draw[every loop, >=latex]
(1) edge[bend right, auto=right] node {1} (2)
(2) edge[loop left] node {1/3} (2)
(2) edge[bend left, auto=left] node {2/3} (3)
(3) edge[bend right, auto=right] node {1/2} (1)
(3) edge[bend left, auto=left] node {1/2} (2);

\end{tikzpicture}
\end{center}

\end{document}

The only changes are some left that became right and the positioning of the nodes, yet the result is a clean Markov chain graph.

About

Python script to create a simple LaTeX graph using a Markov chain matrix

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages