将 TeX 转换为纯字符串以用于 http://api.mathjs.org/v4

Convert TeX to a plain string for use on http://api.mathjs.org/v4

我对 TeX 非常陌生,实际上我是在过去一个小时内发现它的。我想知道我是否可以这样转换:

$$ (3\times10^{-9})\times(7\times10^{3})  $$

变成这样:

(8*10^−9)*(7*10^3)

我一直在研究有关 MathJax 的一些内容,但我不确定它是否适合我。我计划使用 math.js REST API 给我一个 TeX 的答案,但是我不确定如何将 TeX 转换成纯字符串。希望这是有道理的。

它需要在 Javascript.

谢谢。

这很丑陋,但它完成了简单表达式所需的功能:

var myString = '$$ (3\times10^{4})\times(7\times10^{2})  $$';
var myRegexp = /\^{([^}]*)}/g;

var myOutput = myString.replace(/\times/g, 'x');
myOutput = myOutput.replace(/$$/g, '');

myOutput = myOutput.replace(myRegexp, "^");

console.log(myOutput);

如果你的Tex表达式会比较复杂,你可以试试math-expressions。它具有 fromLatex 功能,但我尚未对其进行测试。