## HOWTO: Use LaTeX mathematical expressions in PyGTK

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 `GtkImage`

s. 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() |

[…] HOWTO: Use LaTeX mathematical expressions in PyGTK […]

Pingback by (GUI, Mathematical Equations, Scientific Plotting) = (GTK+, LaTeX, Matplotlib) | Inspirated — January 6, 2010 @ 10:16 am

at first, it didn’t work on my pc. then i noticed that the directory name where i extracted file had non-ascii chars in it and it stalls because of os.path etc. :)) anyway, good work

Comment by Mustafa YÄ±lmaz — April 14, 2010 @ 5:18 am