title: Evaluating Latex Math in Javascript
body: <p>I am working on a html-based calculator, and I want to render an expression, then evaluate it, and all of this must be done in Javascript. I would prefer for the expression to be written in LaTeX, and for it to be possible to edit the rendered expression interactively, but other languages could work.</p>
  
  <p>What I've tried before is to render the expression, interactively, with MathQuill, and then evaluate it with MathJS. This works, to some extent, however, since the two packages are designed with different goals (MathQuill renders LaTeX, while MathJS has a custom math syntax), it doesn't work perfectly well (e.g. if a user types in <code>\frac{5}{17*x}</code> and assumes that it is the correct syntax, then evaluates it and gets an error from MathJS).</p>
  
  <p>Currently, I see three ways to proceed:</p>
  
  <ul>
  <li>Continue with what I have (or maybe switch to MathJax- which is better? ).</li>
  <li>Find a tool that does both rendering and evaluating (whether it is in latex or some other language)</li>
  <li>Find two tools for rendering and evaluating with close (if not the same) syntax</li>
  <li>Write my own rendering and evaluating tool, as recommended <a href="https://stackoverflow.com/questions/16604660/how-to-evaluate-latex-formula-in-javascript">here</a>. I would prefer not to do this, because it seems kind of tedious, and I don't want to reinvent the wheel.</li>
  </ul>
  
  <p>What is the bast way to do this?</p>
  
  <p><strong>Update 1:</strong> After looking through the list of alternative languages available at <a href="http://alt.js.org/" rel="noreferrer">altJS</a>, I think I could use Python, Ruby, or Basic as a language for user input. If I did do that, it would open up so many possibilities for rendering it (technically, it's not "rendering," but just think about how many syntax highlighters there are out there). However, I don't want users to have access to the more advanced elements of these languages, because it would add confusion to my users, and it could also be unsafe (like using <code>eval</code>). I only want users to be able to use math operations, variables (only those that I specify that they can, and also user-created), and functions (only those that I specify that they can, and also user-defined).  Is there a way to restrict the subset of the language that can be used?</p>
  
  <p><strong>Update 2:</strong> I looked around some more on the altJS page, and realized that almost all of the languages were designed to compile language XYZ to JavaScript, then run the JavaScript on the page, instead of running language XYZ <em>on the page</em>. I haven't looked into each and every single one, but most of them seem to be like this. Others, like Brython, seem to be designed to run language XYZ in the browser, but as a replacement/complement to JavaScript instead of as an input language for the user. Is there some workaround so I can use these methods as input?</p>
  
  <p><strong>Update 3:</strong> Thank you to @SpaceDog for that information, and for pointing out that I haven't been specific in what users should be able to achieve. So, here is the functionality that I want to be achieved:</p>
  
  <ol>
  <li>Basic Operations (addition, subtraction, multiplication, division/fractions, exponentiation, roots, modulus)</li>
  <li>Functions (trigonometric functions, logs, etc.)</li>
  <li>Constants (e, pi, etc.)</li>
  <li>Variables (assignment, reassignment, and using them)</li>
  <li>User defined functions</li>
  </ol>
  
  <p><a href="http://latexcalc.sourceforge.net/" rel="noreferrer">LaTeXCalc</a> seems to meet all of these requirements, except for the last one. Also, it doesn't appear to be in Javascript.</p>
  
  <p>This brings me to another idea I've thought of: use something like MathJax for presentation, then use <a href="http://products.wolframalpha.com/api/" rel="noreferrer">Wolfram Alpha API</a> to actually handle the computation. The benefits are that it would have much more advanced options for both input and output. However, the application would be rendered useless (why not just use wolframalpha.com, if the calculator is practically just a clone?), it costs money, and the app wouldn't work offline (it is a desktop app, written in HTML/CSS/JS). Thoughts on that?</p>