To that end, I've written a python function that will generate the URL for the Latex image-generation script, get the image data using HTTP and write it to disk as a file of your choosing. It uses the python requests module (available via pip) and ImageMagick:
import os, requests def formula_as_file( formula, file, negate=False ): tfile = file if negate: tfile = 'tmp.png' r = requests.get( 'http://latex.codecogs.com/png.latex?\dpi{300} \huge %s' % formula ) f = open( tfile, 'wb' ) f.write( r.content ) f.close() if negate: os.system( 'convert tmp.png -channel RGB -negate -colorspace rgb %s' %file )
The code is appallingly simple for how much time it has saved me; it allows me to regenerate formulas for conference posters by simply running a python script and since most vector-graphics packages simply link to bitmap images allows the resulting layouts to update automatically. Even better is that CodeCogs image generation script produces transparent PNGs so you can drop them on top of lightly colored backgrounds. By setting the negate flag you will get a white on transparent PNG that can be used over dark backgrounds.
Here are some examples:
formula_as_file( r'\Gamma_{Levin}(x) = \| \nabla p(x) \|_2^{0.8} + \sum_i |\frac{\partial^2 p(x)}{\partial x_i^2}|^{0.8}', 'reg_levin.png', True )
formula_as_file( r'\Gamma_{MTV}(x) = \| \sum_{i=1}^3 \left( \| \nabla p^{(i)}(x)\|_2^2 \right) \|_2', 'reg_MTV.png' )
Finally no more ugly power-point presentations!
4 comments:
Very useful. Thanks James
Can it be Vice Versa?!
Thank you
I'm search for 2 week with Excel VBA but i'm no found
and then
using python for one day and see your blog
and solved
you're excellent programer
Thank you
very useful. Thank you!
Post a Comment