<!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="sin^2(x) = \cos^{3}{x}" 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 powers should get moved to a more infixy location.
</div>
<script src="script.js"></script>
<script>
var outputEl = document.getElementById('output');
</script>
</body>
</html>
function shiftPower(input) {
var openPar = 0;
var power = "";
var inside = "";
for (var i=1;i<input.length;i++){
if (input[i] == "(" || input[i] == "{" || input[i] == "["){
openPar++;
}
else if (input[i] == ")" || input[i] == "}" || input[i] == "]"){
openPar--;
}
if (openPar == 0){
if (power == ""){
power = input.substring(1,i+1);
continue;
}
else {
inside = input.substring(1+power.length,i+1);
break;
}
}
}
return inside + "^"+power+input.substring(1+power.length+inside.length);
}
function latexReplace(input){
for (var i=0;i<input.length;i++){
if (input[i] == "^"){
if (i >= 3 && input.substring(i-3,i+1) == "sin^"){
input = input.substring(0,i)+shiftPower(input.substring(i));
}
else if (i >= 3 && input.substring(i-3,i+1) == "cos^"){
input = input.substring(0,i)+shiftPower(input.substring(i));
}
else if (i >= 3 && input.substring(i-3,i+1) == "tan^"){
input = input.substring(0,i)+shiftPower(input.substring(i));
}
else if (i >= 3 && input.substring(i-3,i+1) == "csc^"){
input = input.substring(0,i)+shiftPower(input.substring(i));
}
else if (i >= 3 && input.substring(i-3,i+1) == "sec^"){
input = input.substring(0,i)+shiftPower(input.substring(i));
}
else if (i >= 3 && input.substring(i-3,i+1) == "cot^"){
input = input.substring(0,i)+shiftPower(input.substring(i));
}
}
}
return input;
}
About
This repl handles a few scenarios where a trig function is raised to a power.
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 "^" preceded by a trig function and tries to move the power to after the () or {}.