Making LaTeX Equations

On Mac, there is a program called LaTeXiT, which will create images of LaTeX equations that can be easily imported into other documents. Linux has a similar program called klatexformula. These allow you to avoid writing an entire document just to get the image of a single formula.

I’ve created a very compact program to do something similar, but without a lot of the fancy features like drawing the image or saving history. It’s a barebones version that will just create an image in one of a number of file formats. Here it is:

#! /usr/bin/python
from Tkinter import *
import ScrolledText
import signal
import sys
import os
class Dialog:
  def __init__(self,parent):
    self.filelist = []
    cur_row = 0
    root = self.root = parent
## File name
    Label(root, text="File name (no extension)").grid(padx=5,row=cur_row, sticky=W)
    self.filename = Entry(root)
    self.filename.grid(padx=5,row=cur_row,column=1,sticky=W+E)
## Format
    cur_row = cur_row+1
    Label(root,text="Format").grid(row=cur_row,sticky=W,padx=5)
    self.form = StringVar(root)
    self.form.set("png")
    option=OptionMenu(root,self.form,"png","pdf","ps","eps","jpg","svg",
"tif")
    option.grid(padx=5,row=cur_row,column=1,sticky=W+E)
## Font size
    cur_row = cur_row+1
    Label(root,text="Font Size").grid(row=cur_row,sticky=W,padx=5)
    self.fontsize = StringVar(root)
    self.fontsize.set("normalsize")
    sizeopt=OptionMenu(root,self.fontsize,"tiny","scriptsize",
"footnotesize","small","normalsize","large","Large","LARGE","huge","Huge")
    sizeopt.grid(padx=5,row=cur_row,column=1,sticky=W+E)
#Environment
    cur_row = cur_row+1
    Label(root,text="Environment").grid(row=cur_row,sticky=W,padx=5)
    self.enviro = StringVar(root)
    self.enviro.set("Equation")
    envopt=OptionMenu(root,self.enviro,"Equation","Align")
    envopt.grid(padx=5,row=cur_row,column=1,sticky=W+E)
## Equation
    cur_row = cur_row+1
    Label(root, text="Write Equation in LaTeX Format").grid(row=cur_row,padx=5,columnspan=3)
    cur_row = cur_row+1
    self.equation = ScrolledText.ScrolledText(root,wrap=WORD,width=40,height=10)
    self.equation.grid(padx=5,columnspan=3,row=cur_row,sticky=W+E+N+S)
    root.grid_rowconfigure(cur_row,weight=1)
    root.grid_columnconfigure(0,weight=1)
# Run button
    b = Button(root,text="RUN",command=self.run)
    cur_row = cur_row+1
    b.grid(row=cur_row,sticky=W+E)
# Dialog
    self.errvar = StringVar()
    self.rundialog = Label(root,textvariable=self.errvar)
    self.rundialog.grid(padx=5,row=cur_row,column=1,sticky=W+E)
# Quit
    quit = Button(root,text="QUIT",command=self.quit)
    quit.grid(padx=5,row=cur_row,column=2,sticky=W+E)
  def quit(self):
    self.cleanup()
    self.root.destroy()
  def run(self):
    theEquation = self.equation.get("1.0",END+'-1c')
    if theEquation is "":
      self.errvar.set("Set Equation!")
      return
    theFile = self.filename.get()
    if theFile is "":
      self.errvar.set("Set Filename!")
      return
    extension = self.form.get()
    environment = self.enviro.get()
    size = self.fontsize.get()
    texfile = open(theFile+".tex","w")
    texfile.write("\\documentclass[varwidth=true,border=10pt]{standalone}\n")
    texfile.write("\\usepackage{amsmath}\n")
    texfile.write("\\usepackage{amssymb}\n")
    texfile.write("\\begin{document}\n")
    texfile.write("\\"+size+"\n")
    texfile.write("$$\n")
    if environment == "Align":
      texfile.write("\\begin{aligned}\n")
    texfile.write(theEquation)
    if environment == "Align":
      texfile.write("\\end{aligned}\n")
    texfile.write("$$\n")
    texfile.write("\end{document}")
    texfile.close()
    if theFile not in self.filelist:
      self.filelist.append(theFile)
    err = os.system("pdflatex -halt-on-error -interaction=nonstopmode "+theFile+".tex")
    if err:
      self.errvar.set("LaTeX Error!")
      return
    if extension != "pdf":
      if extension == "jpg":
        extopt = "jpeg"
      elif extension == "tif":
        extopt = "tiff"
      else:
        extopt = extension
    os.system("pdftocairo -"+extopt+" "+theFile+".pdf "+theFile)
    os.system("mv "+theFile+"-1."+extension+" "+theFile+"."+extension)
    os.system("rm -f "+theFile+".pdf")
    self.quit()
  def cleanup(self):
    for f in self.filelist:
      os.system("rm -f " + f+".tex "+f+".aux "+f+".log")
if __name__== "__main__":
  root = Tk()
  root.title("Latex Equation Creator")
  root.geometry("400x400")
  d = Dialog(root)
  root.wait_window(d.root)

It works using Ubuntu and TeX Live 2014 and is written in python 2.7 with the TkInter package. You might need to correct some indentation and line-wrapping problems if you try to use this html version.

Advertisements