Code
%load_ext tikzmagic
December 24, 2021
I’ve been using the graphviz library to create flowcharts and diagrams for these posts. After some time getting used to it I’m now quite happy with it. The problem with this is that it requires quite a lot of code to create a graph and sometimes the layout is not as I want.
I write these posts in JupyterLab and there is a built in latex rendering engine. Maybe I could use that instead?
Looking it up it seems that tikz is used to display flowcharts in latex documents. This uses a syntax such as:
\begin{tikzpicture}
% draw rectangle node
\node[draw,
minimum width=2cm,
minimum height=1cm] at (0,0) {Process 1};
% Change line color
\node[draw=red,
minimum width=2cm,
minimum height=1cm] at (3,0) {Process 2};
% Change filling color
\node[draw,
fill=cyan!50,
minimum width=2cm,
minimum height=1cm] at (0,-2) {Process 2};
% Change text color
\node[draw,
text=red,
minimum width=2cm,
minimum height=1cm] at (3,-2) {Process 2};
\end{tikzpicture}
The begin and end stanzas rarely work in Jupyter and this is no exception.
To get this working in Jupyter I have to install the ipython-tikzmagic
package and then I can write special tikz cells. The installation is annoying as the package is not on pypi. I’ve had to install it with:
poetry add 'git+ssh://github.com/mkrphys/ipython-tikzmagic.git'
A bunch of additional dependencies are also required as this needs a working latex installation, image magik and pdf2svg. I have image magic installed already, so I needed to install the following:
sudo apt-get install \
texlive-latex-base texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra \
pdf2svg
Let’s see how easy it is to use.
I’m going to use one of the fancier examples from the notebook. To get this to work I had to change the format to svg from png. The full code is:
%%tikz --size 400,400 --scale 1.2 --format svg
\draw [style=help lines, step=2] (-1,-1) grid (+7,+7);
\draw [line width=0.5mm, fill=blue!40!white] (+2,+2) rectangle (+4,+4);
\draw [blue!60!white] (2, 2) node[anchor=north east] {$(i ,j )$};
\draw [blue!60!white] (4, 2) node[anchor=north west] {$(i+1,j )$};
\draw [blue!60!white] (4, 4) node[anchor=south west] {$(i+1,j+1)$};
\draw [blue!60!white] (2, 4) node[anchor=south east] {$(i ,j+1)$};
\filldraw [color=gray] (0,0) circle (.1);
\filldraw [color=gray] (0,2) circle (.1);
\filldraw [color=gray] (0,4) circle (.1);
\filldraw [color=gray] (0,6) circle (.1);
\filldraw [color=gray] (2,0) circle (.1);
\filldraw [color=black] (2,2) circle (.1);
\filldraw [color=black] (2,4) circle (.1);
\filldraw [color=gray] (2,6) circle (.1);
\filldraw [color=gray] (4,0) circle (.1);
\filldraw [color=black] (4,2) circle (.1);
\filldraw [color=black] (4,4) circle (.1);
\filldraw [color=gray] (4,6) circle (.1);
\filldraw [color=gray] (6,0) circle (.1);
\filldraw [color=gray] (6,2) circle (.1);
\filldraw [color=gray] (6,4) circle (.1);
\filldraw [color=gray] (6,6) circle (.1);
This renders really nicely however it is a lot more work than the graphviz approach. I might use this when I need greater control over the specific layout.