# Py2tex extension example notebook¶

The py2tex extension can be found at https://gist.github.com/4032651. The extension defines new magic functions to convert easy python formulas to a nice LaTeX representation.
The extension supports the python unum class to provide unit aware calculation. The combination of ipython notebook with unit aware calculation make it perfect for engineering calculation worksheets.

In [1]:
In [2]:
from math import *

## Normal usage¶

In [3]:
%%tex a=1.0
b = -1.0
c = -1.0
x_1=(-b+sqrt(b**2-4*a*c))/(2*a)
x_2=(-b-sqrt(b**2-4*a*c))/(2*a)
varphi = x_1
$$\mbox{a} = 1.0$$
$$\mbox{b} = -1.0$$
$$\mbox{c} = -1.0$$
$$\mbox{x}_{1} = \frac{- b + \sqrt{b^{2} - 4 \cdot a \cdot c}}{2 \cdot a} = 1.618$$
$$\mbox{x}_{2} = \frac{- b - \sqrt{b^{2} - 4 \cdot a \cdot c}}{2 \cdot a} = -0.618$$
$$\varphi = x_{1} = 1.618$$
In [4]:
%texnr x_1_2=x_1+x_2
$$\mbox{x}_{1,2} = x_{1} + x_{2}$$
In [5]:
%texformat %.3e
In [6]:
%%tex alpha = pi/3
cos(alpha)**2+sin(alpha)**2
sin(alpha)
$$\alpha = \frac{\pi}{3} = 1.047$$
$$\cos\left(\alpha\right)^{2} + \sin\left(\alpha\right)^{2} = 1.000$$
$$\sin\left(\alpha\right) = 8.660 \cdot 10^{-1}$$

## Unit example¶

In [7]:
from unum.units import *
In [8]:
%texformat %.3f
In [9]:
%%texnr t = 3.0*s
a = 4.5*(m/s**2)
v_0 = 2*(m/s)
$$\mbox{t} = 3.0 \cdot s$$
$$\mbox{a} = 4.5 \cdot \frac{m}{s^{2}}$$
$$\mbox{v}_{0} = 2 \cdot \frac{m}{s}$$
In [10]:
%tex x = v_0*t+(a*t**2)/2
$$\mbox{x} = v_{0} \cdot t + \frac{a \cdot t^{2}}{2} = 26.250\quad \mbox{m}$$