If you love math, TeX and code, here's a treasure trove for you:
1. There is a JS library called math.js.
2. It has a very advanced mathematical expression compiler which can transform an expression into a computed value, AST (node tree) or optimised JavaScript function.
3. Every node has a method toTex() that will convert that sub-expression into TeX.
Now:
1. Implement your math code in math.js.
2. Add KaTeX.
3. Instantly render your code in sexy typeset math notation.