Using Tikzmagic

TIKZ is a great language for producing vector graphics. It is however a bit tedious to go over the whole $\LaTeX$-like compilation when you get used to an ipython notebooks work-flow.

I describe here how to use a cell magic implemented by http://www2.ipp.mpg.de/~mkraus/python/tikzmagic.py and a hack to use euclide within the graph (as implemented in https://github.com/laurentperrinet/ipython_magics).

Installation

In [1]:
%install_ext https://raw.githubusercontent.com/laurentperrinet/ipython_magics/master/tikzmagic.py
Installed tikzmagic.py. To use it, type:
  %load_ext tikzmagic
/usr/local/lib/python3.5/site-packages/IPython/core/magics/extension.py:47: UserWarning: %install_ext` is deprecated, please distribute your extension(s)as a python packages.
  "as a python packages.", UserWarning)
In [2]:
%load_ext tikzmagic

### basic usage
In [3]:
%tikz \draw (0,0) rectangle (1,1);
No description has been provided for this image
In [4]:
%%tikz -s 400,400 -sc 1.2 -f png
\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);
No description has been provided for this image

now with euclide

In [5]:
%%tikz -s 800,400 -sc 1.2 -f png

\tkzInit[xmax=13, ymax=8]

\tkzDefPoint(2.95, 2.25){A}
\tkzDefPoint(11,5.25){B}
\tkzLabelPoints[above left](A,B)
\tkzDefPoint(10, 2.25){C}

% draw red dots at the center of edges
\tkzDrawPoints[size=10, color=red, fill=red](A,B)


%----------------------------------------------------------
% Drawing the lines and segments
%----------------------------------------------------------
\tkzDrawLine[color=red,line width=2pt, add=-1.15 and -.15 ](C,A)
\tkzDrawLine[color=red,line width=2pt, add=-1.3 and -.3 ](C,B)

\tkzDrawLine(A,B)
\tkzDrawLines[dashed](A,C B,C)

% drawing arcs for angles  
\tkzMarkAngle[size=2.5,mkpos=.2](C,A,B)
\tkzLabelAngle[pos=3.5,circle](C,A,B){$\mathsf{\phi}$}

\tkzDefPointWith[linear,K=1.5](A,C)
\tkzGetPoint{D}
\tkzDefPointWith[linear,K=.75](B,C)
\tkzGetPoint{E}
\tkzMarkAngle[size=1,mkpos=.2](D,C,E)
\tkzLabelAngle[pos=1.75,circle](D,C,E){$\mathsf{\theta}$}
No description has been provided for this image

To crop the image, I use the \pgfresetboundingbox directive

In [6]:
%%tikz -s 800,400 -sc 1.2 -f png

\tkzInit[xmax=13, ymax=8]

\tkzDefPoint(2.95, 2.25){A}
\tkzDefPoint(11,5.25){B}
\tkzLabelPoints[above left](A,B)
\tkzDefPoint(10, 2.25){C}

% draw red dots at the center of edges
\tkzDrawPoints[size=10, color=red, fill=red](A,B)


%----------------------------------------------------------
% Drawing the lines and segments
%----------------------------------------------------------
\tkzDrawLine[color=red,line width=2pt, add=-1.15 and -.15 ](C,A)
\tkzDrawLine[color=red,line width=2pt, add=-1.3 and -.3 ](C,B)

\tkzDrawLine(A,B)
\tkzDrawLines[dashed](A,C B,C)

% drawing arcs for angles  
\tkzMarkAngle[size=2.5,mkpos=.2](C,A,B)
\tkzLabelAngle[pos=3.5,circle](C,A,B){$\mathsf{\phi}$}

\tkzDefPointWith[linear,K=1.5](A,C)
\tkzGetPoint{D}
\tkzDefPointWith[linear,K=.75](B,C)
\tkzGetPoint{E}
\tkzMarkAngle[size=1,mkpos=.2](D,C,E)
\tkzLabelAngle[pos=1.75,circle](D,C,E){$\mathsf{\theta}$}


\pgfresetboundingbox
\path(5, 1) rectangle (13, 8);
No description has been provided for this image

My full diagram

with as an option, a way to save it in SVG format:

In [7]:
%%tikz -f svg -s 800,500 --save /tmp/diagram.svg

%\fill[white,fill opacity=.9] (0, 1.2) rectangle (13, 8);
%\tkzInit[xmax=13, ymax=8]

%----------------------------------------------------------
% Defining coordinates
%----------------------------------------------------------

\tkzDefPoint(2.95, 2.25){A}
\tkzDefPoint(11,5.25){B}
\tkzLabelPoints[above left](A,B)
%   \tkzLabelPoints[below right](B)
\tkzDefPoint(10, 2.25){C}

% draw red dots at the center of edges
\tkzDrawPoints[size=10, color=red, fill=red](A,B)

%----------------------------------------------------------
% Drawing the lines and segments
%----------------------------------------------------------
\tkzDrawLine[color=red,line width=2pt, add=-1.15 and -.15 ](C,A)
\tkzDrawLine[color=red,line width=2pt, add=-1.3 and -.3 ](C,B)

\tkzDrawLine(A,B)
\tkzDrawLines[dashed](A,C B,C)

% drawing arcs for angles  
\tkzMarkAngle[size=2.5,mkpos=.2](C,A,B)
\tkzLabelAngle[pos=3.5,circle](C,A,B){$\mathsf{\phi}$}

\tkzDefPointWith[linear,K=1.5](A,C)
\tkzGetPoint{D}
\tkzDefPointWith[linear,K=.75](B,C)
\tkzGetPoint{E}
\tkzMarkAngle[size=1,mkpos=.2](D,C,E)
\tkzLabelAngle[pos=1.75,circle](D,C,E){$\mathsf{\theta}$}

%----------------------------------------------------------
% Drawing normals
%----------------------------------------------------------

\tkzDefLine[perpendicular=through A, K=-.75](C,A)
\tkzGetPoint{a1}
\tkzDefLine[perpendicular=through B, K=2.5](C,B)
\tkzGetPoint{b1}
\tkzInterLL(a1,A)(b1,B)  \tkzGetPoint{H}
\tkzMarkRightAngle[size=.5](H,A,C)
\tkzMarkRightAngle[size=.5](H,B,C)
\tkzDrawLines[dashed,dash phase=1.5pt](a1,A)
\tkzDrawLines[dashed,dash phase=0.5pt](b1,B)

%----------------------------------------------------------
% Drawing mediator and psi line
%----------------------------------------------------------
%    \tkzDefLine[mediator](A,B)          \tkzGetPoints{m1}{M}
\tkzDefMidPoint(A,B)
\tkzGetPoint{M}
\tkzDefLine[perpendicular=through M, K=.4](A,B)
\tkzGetPoint{m1}
\tkzMarkRightAngle[size=.5](B,M,m1)    

\tkzFillAngle[size=1.4,fill=blue!40](m1,M,H)
\tkzLabelAngle[pos=2,circle](m1,M,H){$\mathsf{\psi}$}%= \phi -\theta/2
\tkzDrawLines[](m1,M M,H)    

\pgfresetboundingbox
\path(1, 1) rectangle (13, 9);
In [8]:
from IPython.display import Image, SVG
SVG(filename="/tmp/diagram.svg")
Out[8]:

Beautiful!