A ruby parser for TeX equations

Calculus is utility library which allow to parse some subset of latex equations and store them in postfix notation It also allows translate it to abstract syntax tree and calculate (implemented for simple expressions).


 gem install calculus 


 001:0> require 'calculus'
 002:0> exp ="2 + 3 * x")
 #<Expression:f46e77a9377ed2d5a9da768496a7e1c20be51bfe postfix_notation=[2, 3, "x", :mul, :plus] variables={"x"=>nil}>
 003:0> exp.postfix_notation
 [2, 3, "x", :mul, :plus]
 004:0> exp.abstract_syntax_tree
 [:plus, 2, [:mul, 3, "x"]]
 005:0> exp.variables
 006:0> exp.unbound_variables
 007:0> exp["x"] = 5
 008:0> exp.unbound_variables
 009:0> exp.calculate

You can also render expression to PNG image if you have latex and dvipng installed.

 010:0>"2 + 3 \\cdot x").to_png

Also you can skip parser if you need only png generation

 011:0>"\\hat{f}(\\xi) = \\int_{-\\infty}^{\\infty} f(x)\\ e^{- 2\\pi i x \\xi}\\,dx", :parse => false).to_png

Don’t forget to cleanup file after using.


  1. Fork it (
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request