Fork me on GitHub

Evaluatex.js

Latex and ASCIIMath evaluator for Javascript.

Install with NPM:

$ npm install evaluatex

Use a minified version in a webapp:

<script src="node_modules/evaluatexin/evaluatex.min.js"></script>
<script type="text/javascript">
    evaluatex();
<script>

Get zipped releases here.

Check it on Github.

Try it!

Evaluatex is a parser that reads and evaluates LaTeX and ASCII math. It can safely resolve math expressions, without relying on Javascript's eval, which is evil. It's especially powerful when combined with math markup tools such as Mathquill.

API

const fn = evaluatex(expression, constants = {}, options = {});
const result = fn(variables = {});

Demo

Change the math below and see the result on the right. You may use any functions and objects in Javascript's Math.

sqrt(3^2 + 4^2) + log(PI) / log(SQRT2)

Manual

Evaluatex takes a string containing a math expression and returns a compiled function. The compiled function evaluates the math and returns the result.

Throughout this manual, you may change the underlined math in any of the examples to explore how Evaluatex works.

1 + 2 * 3 / 4

A one-liner:

const result = evaluatex("1 + 2 * 3 / 4")()

Evaluatex returns a function because you can invoke it multiple times with different variables.

fn = evaluatex("");
result = [ fn({ magic: 2 }), fn({ magic: 99 }) ];

Evaluatex is a calculator

Evaluatex supports all of the features you should expect from a simple calculator, including order of operations. You can use parentheses, square brackets, or curly braces interchangeably. Exponents use the ^ operator.

(1 + 2) * [3 ^ 4] - {5 + 6}

You can use named values in expressions that are visible to the expression only. This avoids the need for eval(). See the Constants and variables section for more.

1 + a / b

Implicit multiplication with brackets or variables is not a problem.

4a(1 + b)

Be careful with associativity: 1 / 2a is (1 / 2) * a.

1 / 2a

You have full access to all functions and constants in Javascript's Math object.

sin(PI / 2) + LN2 ^ E + hypot(3, 4)

You can omit brackets from simple functions, but be careful with implicit multiplication.

sin 2PI
sin PI^2

When in doubt, use parentheses.

Functions

You can define custom functions as constants.

incr(4)

Multi-argument functions work just as well, but they require parentheses.

hypot(3, 4)
min(5, 4, 3, -2, 1)
sum3(10, 20, 30)

There are several functions built in to Evaluatex to help with logs, roots, and trigonometry.

logn(81, 3)
rootn(8, 3)
csc(PI/4)

Absolute values work like a charm, as do factorials, which round to the nearest integer before performing the calculation.

|5 - 20|
3.6!

Constants and variables

You can refer to symbols, such as x, in a math expression. These symbols can be constants or variables.

Constants are specified in the call to evaluatex() as the second parameter. Their values are compiled by Evaluatex into the resultant equation. Use constants if you know that they won't change between invocations. Constants may be numeric values or functions, but not expressions like PI/2.

100 + HALF_PI

Variables are specified when calling the compiled function. Their values can be changed between invocations. If you compile an expression with a variable, you must give a value for that variable, otherwise Evaluatex will complain. Variables may only be numeric values, and not functions.

100 + x

You can combine constants and variables. Constants have priority over variables - if you define a constant, you can't change it without re-compiling the expression.

100 + x + HALF_PI

LaTeX

LaTeX parses math a little differently. Turn on LaTeX behaviour with a flag:

x^24

In the previous example, the power takes only the first symbol after it, so it reads like x^2 * 4. Use curly braces to fix that.

x^{24}

LaTeX commands are also supported, like \frac. The following example is interpreted as 1 / 20 * 3.

\\frac 1{20}3

Issues

Report issues on Github. Evaluatex does its best to be correct, but unanticipated bugs features might exist.

License

Evaluatex is licensed under the MIT license. This means that you can probably use it in your project, as long as you don't pass it off as your own or blame the author for the inevitable heat death of the universe.

fn = evaluatex(
  
"",
 
,
 
);
result = fn();
Result: