Call Windows Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 15 October 2013

PythonTeX

Posted on 14:33 by Unknown
I write technical documents in LaTeX, and my programming language of choice is (currently) Python.  I’m about to start writing a large LaTeX document that will have lots of figures drawn with Python’s matplotlib.  So, I was wondering, wouldn’t it be nice if there was some support for including these more directly in the document.  I went surfing, and came across PythonTeX, by Geoffrey M. Poore.

PythonTeX doesn’t just allow you to put figures in your LaTeX, it allows whole chunks of Python, with the results embedded in the output.  After watching a YouTube video on the capabilities, I thought it looked interesting, so I decide to give it a go: it might well be useful, and anyway it’s a great displacement activity from actually starting writing that document.

It wasn’t a particularly onerous installation process, as LaTeX and Python installations go.  In order to get the test document, pythontex_gallery.tex, to format properly, I just had to:
  1. download PythonTeX
  2. run LaTeX on the provided example file pythontex_gallery.tex (I use TeXnicCenter)
  3. give LaTeX permission install several macro packages (as warned by the PythonTeX installation documentation)
  4. from the command line, run pythontex.py pythontex_gallery.tex
  5. scrutinise the error messages to see I needed pygments.style, sympy, and scipy  (again, as warned by the PythonTeX installation documentation)
  6. go to the pygments page, and see that it recommends installation via ez_setup
  7. go to the ez_setup page, and download it
  8. from the command line, run python ez_setup.py
  9. download the relevant pygments “egg”
  10. from the command line, run easy_install Pygments-1.6-py2.7.egg
  11. download and install sympy
  12. download and install scipy (hence discovering that it is pronounced “sigh pie”, not “skippy”)
  13. from the command line, run pythontex.py pythontex_gallery.tex
  14. run LaTeX on pythontex_gallery.tex again
At this point out popped a LaTeX pdf with figures, equations, integrals, and expression derivations all produced from the embedded python!

Now that I have everything installed, all I have to do to generate this pdf from scratch is:
  1. run LaTeX on pythontex_gallery.tex
  2. from the command line, run pythontex.py pythontex_gallery.tex
  3. run LaTeX on pythontex_gallery.tex again
Simples!

There is also a handy utility to convert the LaTeX file with embedded python into a stand-alone LaTeX document, suitable for other people to process.  (This is essential if the document needs to be sent off for publication, for example.)
  1. edit the LaTeX file to include the depythontex=true option in the pythontex package
  2. run LaTeX on pythontex_gallery.tex again
  3. run pythontex.py pythontex_gallery.tex again
  4. run depythontex.py --graphicspath pythontex_gallery.tex final.tex
Then anyone can run LaTeX on final.tex without needing PythonTeX.

Having installed PythonTeX, and checked that it can at least process the supplied test file, I next needed to check that I can get it to produce the kind of diagrams I want.

So I wrote a short LaTeX document with the body:
\begin{pylabcode}
n = 16
m = 6
figure(figsize=(n*0.2, m*0.2))
gca().axison = False
x = 8
y = 4
fill( (x,x+1,x+1,x), (y,y,y+1,y+1), 'r', linewidth=0)
for i in range(0,n+1):
ii = (i,i)
jj = (0,m)
plot(ii,jj,'0.4', linewidth=0.2)
for j in range(0,m+1):
ii = (0,n)
jj = (j,j)
plot(ii,jj,'0.4', linewidth=0.2)
savefig('myplot.pdf', bbox_inches='tight')
\end{pylabcode}

\includegraphics{myplot.pdf}

This is a \pylab{'${0} \\times {1} = {2}$'.format(n, m, n * m)}
square grid, with a red block at $(\pylab{x},\pylab{y})$.

The top bit is some python code to draw a grid and a red square, and the bottom bit is some explanatory LaTeX text.  Going through the LaTeX-PythonTex-LaTeX process gives:
Excellent.  I can use it to draw diagrams in situ.  The great advantage this approach has is that I can keep all my code for the figures in the same file as the LaTeX text, so there will be much less chance for fragments to wander off and get lost.

As shown in the code above, the LaTeX can include references to python variables, which is how the caption part is generated.  Then, if I decide that I want a slightly different figure, I just edit this single file, change only the values assigned to n,m,x,y in the python part, and get something like:

The new variable values have changed the drawn figure, and these changes have also been propagated to the caption, including the calculation of the product.  This gives a much more sophisticated form of cross referencing.

I’m sold!


Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in LaTeX, python | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • hyperbolic hyperbole
    What's with hyperbolic discounting? It's everywhere ! I first consciously noticed the term at a workshop about six weeks ago, and n...
  • better use seaweed
    As Neils Bohr is alleged to have said , “prediction is very difficult, especially about the future”. My smartphone has a weather app on it t...
  • "Windows support" -- not
    Just had another scam phone call -- someone with a strong Indian accent claiming to be calling from "Windows Technical Support" (o...
  • national stereotypes
    I've just got back from a very productive three day meeting in Paris. Just around the corner from where I was working, there was a marv...
  • retrospective holiday diary day 1: travelling north
    We went to the Lake District last “summer” ; this “summer” it was time for touring the other side of the country: Northumbria. The holiday s...
  • retrospective holiday diary day 5: trains
    Monday 24 September, and the long-threatened rain finally arrived. So this was the ideal day for the planned Carlisle-Settle rail trip . Bu...
  • oh dear
    We have a garden pond to help encourage frogs and other amphibians. Hedgehogs may suffer, however. :-(
  • funfair mirror trees
    One of the trees in our garden has died.  It died last summer in the drought, but we gave it a year to prove to us it really was dead.  It i...
  • retrospective holiday diary day 3: Lindisfarne
    Saturday 22 September, and the weather was still fine, sunny holiday weather so we decided to take advantage of the sunshine, and do Lindisf...
  • more scammers
    So not long after the scam phone call , the phone rings again. It's British Gas -- they get to call me because I'm actually a custo...

Categories

  • 3D printer
  • algorithm
  • astronomy
  • birds
  • Bonnie Tyler
  • books
  • cognition
  • computer
  • conference
  • Doctor Who
  • driving
  • ducks
  • duodecimal
  • education
  • electricity
  • estimation
  • Evernote
  • evolution
  • font
  • food
  • fractals
  • game
  • garden
  • graphics
  • grimoire
  • history
  • holiday
  • humour
  • language
  • LaTeX
  • lego
  • lol
  • mathematics
  • medicine
  • money
  • music
  • obituary
  • pedantry
  • politics
  • probability
  • psychology
  • publishing
  • python
  • quotations
  • research
  • robots
  • science
  • science fiction
  • space flight
  • statistics
  • TPS
  • trains
  • tree
  • TV
  • weather
  • web

Blog Archive

  • ▼  2013 (119)
    • ►  December (1)
    • ►  November (17)
    • ▼  October (12)
      • back from Lyon
      • snail central
      • sequestering carbon, several books at a time XI
      • 90 minutes later
      • apple picking time
      • PythonTeX
      • too much tech!
      • mushrooms gonna mushroom
      • sequestering carbon, several books at a time X
      • Martin Magnus on Kindle
      • a sky full of shepherd's delight
      • mellow fruitfulness
    • ►  September (10)
    • ►  August (9)
    • ►  July (8)
    • ►  June (10)
    • ►  May (19)
    • ►  April (10)
    • ►  March (9)
    • ►  February (4)
    • ►  January (10)
  • ►  2012 (103)
    • ►  December (16)
    • ►  November (8)
    • ►  October (14)
    • ►  September (6)
    • ►  August (13)
    • ►  July (8)
    • ►  June (6)
    • ►  May (9)
    • ►  April (10)
    • ►  March (7)
    • ►  February (5)
    • ►  January (1)
  • ►  2011 (79)
    • ►  December (7)
    • ►  November (5)
    • ►  October (10)
    • ►  September (7)
    • ►  August (6)
    • ►  July (5)
    • ►  June (6)
    • ►  May (6)
    • ►  April (9)
    • ►  March (9)
    • ►  February (3)
    • ►  January (6)
Powered by Blogger.

About Me

Unknown
View my complete profile