从纯文本到 TEX - Javascript
From plain text to TEX - Javascript
我正在构建一个数学应用程序,我想在 javascript (nodejs) 中展示用良好设计编写的数学公式。所以我会将纯文本转换为 TEX 格式。
从这样的公式:
(25*5)/6
公式如下:
\frac{(25 \cdot 5)}{6}
为了好的设计:
如果还有其他已知方法请指教。
非常感谢!
我已将 this 表达式解析器从 java 重写为 java 脚本。不是最直接的方法。请注意,这没有经过广泛测试。
function texFromExpression(str){
var pos = -1, ch;
function nextChar(){
ch = (++pos < str.length) ? str.charAt(pos) : -1;
}
function eat(charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
function parse(){
nextChar();
var x = parseExpression();
if (pos < str.length) throw `Unexpected: ${ch}`
return x;
}
function parseExpression() {
var x = parseTerm();
for (;;) {
if (eat('+')) x = `${x} + ${parseTerm()}` // addition
else if (eat('-')) x = `${x} - ${parseTerm()}` // subtraction
else return x;
}
}
function parseTerm() {
var x = parseFactor();
for (;;) {
if (eat('*')) x=`${x} \cdot ${parseTerm()}`; // multiplication
else if (eat('/')) x= `\frac{${x}}{${parseTerm()}}`; // division
else return x;
}
}
function parseFactor() {
if (eat('+')) return `${parseFactor()}`; // unary plus
if (eat('-')) return `-${parseFactor()}`; // unary minus
var x;
var startPos = pos;
if (eat('(')) { // parentheses
x = `{(${parseExpression()})}`
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = str.substring(startPos, pos);
} else if (ch >= 'a' && ch <= 'z') { // variables
while (ch >= 'a' && ch <= 'z') nextChar();
x= str.substring(startPos, pos);
if(x.length>1){
x = `\${x} {${parseFactor()}}`;
}
} else {
throw `Unexpected: ${ch}`
}
if (eat('^')) x = `${x} ^ {${parseFactor()}}` //superscript
if(eat('_')) x = `${x}_{${parseFactor()}}`;
return x;
}
return `$${parse()}$`;
}
我正在构建一个数学应用程序,我想在 javascript (nodejs) 中展示用良好设计编写的数学公式。所以我会将纯文本转换为 TEX 格式。 从这样的公式:
(25*5)/6
公式如下:
\frac{(25 \cdot 5)}{6}
为了好的设计:
如果还有其他已知方法请指教。 非常感谢!
我已将 this 表达式解析器从 java 重写为 java 脚本。不是最直接的方法。请注意,这没有经过广泛测试。
function texFromExpression(str){
var pos = -1, ch;
function nextChar(){
ch = (++pos < str.length) ? str.charAt(pos) : -1;
}
function eat(charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
function parse(){
nextChar();
var x = parseExpression();
if (pos < str.length) throw `Unexpected: ${ch}`
return x;
}
function parseExpression() {
var x = parseTerm();
for (;;) {
if (eat('+')) x = `${x} + ${parseTerm()}` // addition
else if (eat('-')) x = `${x} - ${parseTerm()}` // subtraction
else return x;
}
}
function parseTerm() {
var x = parseFactor();
for (;;) {
if (eat('*')) x=`${x} \cdot ${parseTerm()}`; // multiplication
else if (eat('/')) x= `\frac{${x}}{${parseTerm()}}`; // division
else return x;
}
}
function parseFactor() {
if (eat('+')) return `${parseFactor()}`; // unary plus
if (eat('-')) return `-${parseFactor()}`; // unary minus
var x;
var startPos = pos;
if (eat('(')) { // parentheses
x = `{(${parseExpression()})}`
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = str.substring(startPos, pos);
} else if (ch >= 'a' && ch <= 'z') { // variables
while (ch >= 'a' && ch <= 'z') nextChar();
x= str.substring(startPos, pos);
if(x.length>1){
x = `\${x} {${parseFactor()}}`;
}
} else {
throw `Unexpected: ${ch}`
}
if (eat('^')) x = `${x} ^ {${parseFactor()}}` //superscript
if(eat('_')) x = `${x}_{${parseFactor()}}`;
return x;
}
return `$${parse()}$`;
}