#!/usr/bin/wish -f

wm title . "Show Math"
entry .latex -textvariable latex
button .show -text Show -command show
button .quit -text Quit -command exit
pack .latex .show .quit -side left

bind . <Return> show

proc show {} {
    global latex
    set filename "showmath"
    set texfile $filename.tex
    set dvifile $filename.dvi
    set pngfile $filename.png

    set header \
        {\documentclass{article}
            \usepackage{amsmath}
            \begin{document}
            \pagestyle{empty}
            \begin{displaymath}}
    set footer \
        {\end{displaymath}
            \end{document}}

    set f [open $texfile w]
    puts $f $header
    puts $f "$latex"
    puts $f $footer
    close $f
    exec latex -halt-on-error $texfile
    exec dvipng -D 300 -T tight -q* -o $pngfile $dvifile
    exec firefox -remote "openURL(file:///tmp/$pngfile,new-tab)"
}

cd "/tmp"