logo4 Evolution is progress—                          
progress is creativity.        
vline

Latex Formula Macro

While running the Test page on an other server I realized that formulas are shown only in matplotlib if LaTex is properly installed. By searching the web I came across an other way to render mathematical formulas to png files that even looks prettier.

The software has a rather long history which btw demonstrates faucer evolution. Different people have used the initial piece of software which is rather an idea to go from latex through dvi to png.

The two followingexamples are from that website.

<<mathequation>>y = \int_0^\infty \gamma^2 \cos(x) dx <</mathequation>>

y = \int_0^\infty \gamma^2 \cos(x) dx

<<mathequation>>y^2=x^2+\alpha^2<</mathequation>>

y^2=x^2+\alpha^2

Next step was mt-math whose original webpage doesn't exist any more but it still can be googled. That software added data:URL functionality to encode images into the HTML page. But that feature is not supported by all browsers.

htmlatex provided additional functionality but is also not available any more.

scihtmlatex provides an interesting new feature. It hooks into mod_python and directs HTML pages that contain some predefined HTML tags to an LaTex interpreter that creates an image file and replaces the formula HTML by an img tag.

This is an interesting approach in projects that receive HTML from different sources and the server has to interprets all of these pages identically. As I employ a single source, my Creole text, such flexibility is not required.

Moreover this approach has more drawbacks.

  1. It would add unnecessary overhead by parsing my text twice. First, when I create HTML from Creole text and, Second, when I get my specific formula tags replaced.
  2. Also I'm not sure whether this approach would work with my django project as django uses mod_wsgi instead of mod_python.
  3. The software and execution time can be significantly reduced, if latex is directly interpreted not bypassing span and div attributes.

My approaches forks from scihtmlatex I call it mathlatex. I provides a single class that can be called from a Creole macro which instantly replaces the macro by an img tag.

def mathequation(macro,environ,title=None):
    ltx = macro.body.strip()
    eq = Equation(ltx)
    if not title:
        title = ltx 
    src = eq.pngurl
    del(eq)
    
    return bldr.tag.img(src=src,title=title,alt=ltx)

Setup

Linux

To get this application work on your linux platform you have to install latex

$ sudo apt-get install texlive

and dvipng

$ sudo apt-get install dvipng

Windows

You have to download MiKTeX and run the installer.

Closing bell

Last but not least all these applications require proper permissions granted to the web server to write to those directories.


Tags: Software


Categories: Software

 
   

(c) Mato Nagel, Weißwasser 2004-2013, Disclaimer