<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="favicon.png">
<title></title>
<meta name="description" content="" />
<link href="style.css" rel="stylesheet" type="text/css" />
<style>
div {
padding: 1rem;
}
</style>
</head>
<body>
<div>
Input String: <input type="text" style="width: 20rem;" value="3+\int_a^b f(x) \mathrm{d}x=\int_{a}^{b} f(x) dx+3" oninput="outputEl.textContent = latexReplace(this.value);" onchange="outputEl.textContent = latexReplace(this.value);" />
</div>
<div>
Output: <span id="output"></span>
</div>
<div>
Enter a string in the input above and some common integral LaTeX syntax will be parsed.
</div>
<script src="script.js"></script>
<script>
var outputEl = document.getElementById('output');
</script>
</body>
</html>
function parseLatexInt(input){
var sub = "";
var sup = "";
var inside = "";
var rest = "";
var dx = "x";
var subi = 0;
var supi = 0;
if (input[0] == "_"){
if (input[1] == "{"){
var openPar = 1;
sub = "";
for (var i=2;i<input.length;i++){
if (input[i] == "{"){
openPar++;
sub += input[i];
}
else if (input[i] == "}"){
openPar--;
if (openPar == 0){
subi = i+1;
break;
}
sub += input[i];
}
else {
sub += input[i];
}
}
}
else {
sub = input[1];
subi = 2;
}
if (input[subi] != "^"){
supi = subi;
}
else {
subi++;
if (input[subi] == "{"){
var openPar = 1;
sup = "";
for (var i=subi+1;i<input.length;i++){
if (input[i] == "{"){
openPar++;
sup += input[i];
}
else if (input[i] == "}"){
openPar--;
if (openPar == 0){
supi = i+1;
break;
}
sup += input[i];
}
else {
sup += input[i];
}
}
}
else {
sup = input[subi];
supi = subi+1;
}
}
}
var dindex = input.substring(supi).indexOf('\\mathrm{d}');
if (dindex < 0){
dindex = input.substring(supi).indexOf('d');
if (dindex < 0){
inside = input.substring(supi).replace(/\\\,/g,"").replace(/\\\!/g,"");
dx = "x";
}
else {
inside = input.substring(supi,dindex+supi).replace(/\\\,/g,"").replace(/\\\!/g,"");
rest = input.substring(dindex+supi+2);
dx = input.substring(supi)[dindex+1];
}
}
else {
inside = input.substring(supi,dindex+supi).replace(/\\\,/g,"").replace(/\\\!/g,"");
rest = input.substring(dindex+supi+11);
dx = input.substring(supi)[dindex+10];
}
var out = "int("+inside+","+dx;
if (sub != "" || sup != ""){
out += ","+sub+","+sup;
}
out += ")";
return out+rest;
}
function latexReplace(input){
input = input.replace(/\s/g,"");
for (var i=0;i<input.length;i++){
if (input[i] == "t"){
if (i >= 3 && input.substring(i-3,i+1) == "\\int"){
input = input.substring(0,i-3)+parseLatexInt(input.substring(i+1));
}
}
}
return input;
}
About
This repl parses common LaTeX integral syntax and outputs a string that is easier to postfix.
To view this on TripleLog, click here.
HTML
The HTML is a simple input/output to change a string of text.
CSS
Add CSS by editing the style.css file that is linked to in the head section or inline edit the style section in the head.
JavaScript
The script.js file parses some LaTeX like integrals. It should handle \mathrm{d}x and dx as well as bounds in either a^b or {a+1}^{b+1} form.