# Inspirated

January 6, 2010

## (GUI, Mathematical Equations, Scientific Plotting) = (GTK+, LaTeX, Matplotlib)

Filed under: Blog — krkhan @ 10:16 am

GTK+ needs no introduction. LaTeX is the first thing that pops in anyone’s mind if mathematical equations’ typesetting is under consideration. Matplotlib — while not as well-known as the former two — is the super easy and elegant solution for scientific plotting on *nix platforms.

For an application demo, I required all three. Past experience has taught me that the most straightforward way of “gluing” things together is Python. GTK+ therefore = PyGTK. Next up was LaTeX, and a previous solution of mine for embedding LaTeX in PyGTK came to the rescue. The final requirement of Matplotlib was fulfilled without any hassle since the library was already written in Python.

The collective result was pretty:

(Click on the image for larger version.)

The linked tarball contains the Python scripts for the application. For everything to run smoothly, LaTeX and Matplotlib packages need to be installed on your system. If you encounter any issues running the code, feel free to flame your distribution for the apparent lack of sanity regarding package management.

Tags: , , , , , , , , , , ,

September 10, 2009

## HOWTO: Use LaTeX mathematical expressions in PyGTK

Filed under: Blog — krkhan @ 10:04 pm

I had never really laid my hands on LaTeX until I required it in one of the helper applications for my graduation project. Unfortunately, the requirement wasn’t as simple as producing some documents as I had to embed mathematical expressions on the fly in my PyGTK apps. Googling around for the solution, I found GtkMathView which accomplished something similar to this albeit using MathML. However, my luck ran out on me again as the widget lacked Python bindings. The other solution was to generate transparent PNGs on the fly and include them as GtkImages. This worked rather well, as the final code allowed easy modifications to the generated expressions.

Requirements for the code were:

Final results:

And the simple code behind it:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #!/usr/bin/env python """An example demonstrating usage of latexmath2png module for embedding math equations in PyGTK   Author: Kamran Riaz Khan <krkhan@inspirated.com> """   import gtk import os import latexmath2png   pre = 'gtktex_' eqs = [ r'$\alpha_i > \beta_i$', r'$\sum_{i=0}^\infty x_i$', r'$\left(\frac{5 - \frac{1}{x}}{4}\right)$', r'$s(t) = \mathcal{A}\sin(2 \omega t)$', r'$\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$' ] latexmath2png.math2png(eqs, os.getcwd(), prefix = pre)   def window_destroy(widget): for i in range(0, len(eqs)): os.unlink(os.path.join(os.getcwd(), '%s%d.png' % (pre, i + 1))) gtk.main_quit()   window = gtk.Window() window.set_border_width(10) window.set_title('LaTeX Equations in GTK') window.connect('destroy', window_destroy) vbox = gtk.VBox(spacing = 10) window.add(vbox)   images = [None] * len(eqs) for i in range(len(eqs)): images[i] = gtk.image_new_from_file('%s%d.png' % (pre, i + 1)) vbox.pack_start(images[i])   window.show_all() gtk.main()
Tags: , , , , , , , , , ,