Embed Python Inside L TEX: 1 Install 4 Example
Embed Python Inside L TEX: 1 Install 4 Example
Tiago Maluta
tiago.maluta@gmail.com
1 Install 4 Example
You can embedded Python code inside LATEX file by just Suppose that you had a code to generate some graphic. I used
installing python.sty file. The process is straightforward. pylab and scipy to generate it. Saved on disk as image.png
The example below was tested on Gentoo Linux distribution, and standard LATEX graphicx package to insert figure on text.
other distros may change TeX directory.
\begin{python}
# mkdir /usr/share/texmf-dist/tex/latex/python from pylab import *
# cd /usr/share/texmf-dist/tex/latex/python
# curl www.imada.sdu.dk/∼ehmsen/python.sty > python.sty
from scipy import signal
# texhash
cycles = 10
t = arange(0,2*pi*cycles, pi/10)
waveforms = [ ’sawtooth’ , ’square’ ]
2 Use
for i, waveform in enumerate(waveforms):
Just define the package.
subplot(2,2,i+1)
exec ’y = signal.’ + waveform + ’(t)’
\usepackage{python} plot(t,y)
title(waveform)
And create a normal block statement. axis([0, 2*pi*cycles, -1.1, 1.1])
savefig(’image.png’)
\begin{python} \end{python}
# python code
print "Hello World" \begin{center}
for i in range(10): \includegraphics[scale=0.3]{image}
print i \end{center}
\end{python}
Output on pdf file:
Pay attention on identation. I used four spaces instead tab.
3 Compile
You need pass -shell-escape parameter to enable the
\write18{command} construct used on python.sty. The com-
mand can be any shell command. This construct is normally
disallowed for security reasons1 .