From ElecWeb

Jump to: navigation, search



SympyTex is a LaTeX package that allows you to embed sympy commands within a LaTeX document.


Using sympy within your \LaTeX document is as easy as $2+2=\sympy{2+2}$. 

You can write a block, and then use the variables defined later in your code!
x = sympy.Symbol('x')
h = sympy.integrate(1+x,x)
The variable $h$, how can be called using {\verb \sympy{h} }, and you will get $h =  
\sympy{h}$. Similarly, the integral of $1+x^4$ is $\sympy{sympy.integrate(1+x**4,x)}$.

Here is an example of the output generated.

Example output.png


Creating graphs is easy too. Here we're using matplotlib to create a figure.

 import matplotlib
 import matplotlib.pyplot as plt
 plt.matplotlib.rc('text', usetex = True)
 import pylab
 fig = plt.figure()
 ax = fig.add_subplot(111)
 t = pylab.linspace(0,10,400)
 ax.plot(t, sin(3*t), '-',
     t, sin(0.3*t**2), '--',
     t, cos(t), '-.')
 ax.legend((r'$A^{\omega}$', r'$A^{2\omega}$', r'$A^{3\omega}$'),
     shadow = False, loc = (0.75, 0.1))
 ax.set_xlabel(r'$\gamma_1 + \gamma_2$', {'fontsize'   : 20 })
 ax.set_ylabel(r'$A^{n\omega}$ (dB)', {'fontsize'   : 20 })

The resulting image is inserted with:

 \caption{The graph generated... }

Example figure output.png


Source code and instructions for using and building SympyTeX are available from github.


To use SympyTeX, include the line


In the preamble of your latex document (let's call it 'example.tex'). Then add some sympytex commands inside your document (as above). Then build your document using the following commands.

 latex example.tex
 python example.sympy
 latex example.tex

If you have an integrated LaTeX environment (eg, Kile) you can configure it to run Python on the .sympy file automatically.