<!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+\frac{\mathrm{d}}{\mathrm{d}x}[x^2]" 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 any derivativess in LaTeX syntax (\frac{d}{dt}[t^2] or \frac{\mathrm{d}}{\mathrm{d}x}[x^2]) will be converted to syntax that is easier to postfix (der(t^2,t)).
</div>
<script src="script.js"></script>
<script>
var outputEl = document.getElementById('output');
</script>
</body>
</html>
function parseLatexDerivatives(input){
var openPar = 1;
var parCount = 0;
var bottomStr = "";
var dx = "x";
var insideStr = "";
for (var i=0;i<input.length;i++){
if (input[i] == "{" || input[i] == "["){
openPar++;
}
else if (input[i] == "}" || input[i] == "]"){
openPar--;
if (openPar == 0){
if (parCount > 0){
bottomStr = bottomStr.substring(0,i-parCount);
if (bottomStr.substring(0,10) == "\\mathrm{d}"){
dx = bottomStr.substring(10);
insideStr = input.substring(i+2);
openPar = 0;
parCount = -1*(i+2);
}
else if (bottomStr[0] == "d"){
dx = bottomStr.substring(1);
insideStr = input.substring(i+2);
openPar = 0;
parCount = -1*(i+2);
}
else {
return "\\frac{"+input;
}
}
else if (parCount < 0){
insideStr = insideStr.substring(0,i+parCount);
input = "der("+insideStr+","+dx+")"+input.substring(i+1);
return input;
}
else {
if (input.substring(0,i) == "d" || input.substring(0,i) == "\\mathrm{d}"){
bottomStr = input.substring(i+2);
openPar = 0;
parCount = i+2;
}
else {
return "\\frac{"+input;
}
}
}
}
}
return "\\frac{"+input;
}
function latexReplace(input){
for (var i=0;i<input.length;i++){
if (input[i] == "{"){
if (i >=5 && input.substring(i-5,i) == "\\frac"){
input = input.substring(0,i-5)+parseLatexDerivatives(input.substring(i+1));
}
}
}
return input;
}
About
This repl parse LaTeX derivatives.
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 looks for any fractions in LaTeX syntax and checks if the insides match derivative notation. More possible formats can be parsed.