Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

Embed Python Inside L TEX: 1 Install 4 Example

This document discusses how to embed Python code within LaTeX documents. It explains that the python.sty file needs to be installed, then Python code can be included within \begin{python} and \end{python} blocks. The code will be executed and output can be included. When compiling, the -shell-escape parameter must be used to enable execution of the Python code. An example is provided that generates a graphic from Python and includes it in the LaTeX document.

Uploaded by

tmaluta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Embed Python Inside L TEX: 1 Install 4 Example

This document discusses how to embed Python code within LaTeX documents. It explains that the python.sty file needs to be installed, then Python code can be included within \begin{python} and \end{python} blocks. The code will be executed and output can be included. When compiling, the -shell-escape parameter must be used to enable execution of the Python code. An example is provided that generates a graphic from Python and includes it in the LaTeX document.

Uploaded by

tmaluta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Embed Python inside LATEX

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 .

$ pdflatex -shell-escape file.tex

Output on pdf file:


More information: www.coding.com.br
Hello World 0 1 2 3 4 5 6 7 8 9

1 Note This text is licensed under Public Domain.


that running Python code you are able to do anything
whitin user’s permissions.

You might also like