There are two ways (afaik) to parse traditional math string to an expression:

toExpr = ToExpression[#, TraditionalForm] &
interpreter = Interpreter["MathExpression"]


The former is faster but less capable. I would like to know what are the differences between those approaches because I would like to avoid Interpreter unless I have to use it.

For a start:

toExpr@"sin^(-1)(x)"
interpreter@"sin^(-1)(x)"


x/sin

ArcSin[x]

toExpr["x(x)"]
interpreter["x(x)"]


x[x]

x^2

I would like to know other similar differences.

• Somewhat related: mathematica.stackexchange.com/q/134081/1871 Apr 17, 2021 at 4:40
• I don't think you'll ever get there with these tools. Traditional math is a mess of inconsistent notations that are highly context-dependent. For example, $1/ax$ means 1/(a*x), (1/a)*x, or 1/ax depending on the mood of the author and the field of study (math, physics, econ, etc.). $sin^{-1}x$ means ArcSin[x] or 1/Sin[x] depending on relative humidity. As you show below, $a=b$ means a==b or a=b depending on context. There's lots of experience, intuition, and fault-tolerance going into parsing real math written by real people. Apr 17, 2021 at 5:36
• @Roman You certainly can't get everywhere but you can get somewhere and it is good to know where exactly. My use case allows me to require reasonable conventions from 'input source' plus I can tweak the input if needed. I just want to have a robust, even if limited, tool.
– Kuba
Apr 17, 2021 at 5:49
• I wonder if the test suite in the answer(s) is likely to become quite large, random, and lacking structure. Examples: "sin t^2"/"sin t^(2)"/"sin t^(-1)", "f^(-1)(x)", "sin^(-2)(x)" Apr 17, 2021 at 17:23

under construction, feel free to contribute

examples in form of input => toExpr[input] | interpreter[input]

1. ToExpression does not parse "e" as E.

2. ToExpression does not parse "sqrt" as Sqrt

"sqrt(e)"  =>  sqrt[e] | Sqrt[E]

3. ToExpression does not understand inverse function notation

"sin^(-1)(x)" =>   x/sin | Arcsin[x]

4. ToExpression is strict about requiring multiplication operator in ambiguous cases:

"x(x)" =>  x[x]  | x^2


But it is better to be consistent:

"y(y(x)+x)" =>  y[x + y[x]] | y (x + y[x])

5. ToExpression is strict about * vs . wrt matrix multiplication. It parses * as elementwise multiplication.

"{{2,2}}*{{2},{3}}" => {{2, 2}} {{2}, {3}} (*+msg*)    | {{10}}


6. Interpreter is picky about case and symbol names in general

"x+C" => x+C | Failure[..]


This is likely a feature and was already reported.

7. ToExpression is strict about = vs. ==. Interpreter parses = as ==.

"f(x) = x^2"  => x^2 | f[x]==x^2


Notice how the former set f[x] definition so interpreter would return True if evaluated in the same session as f[x] == x^2 would return True.

Anticipating = you can use ToExpression[_, TraditionalForm, Hold] /. Set -> Equal