<!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="" 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 matrices in LaTeX syntax (\begin{matrix}1 & 2 & 3\\4 & 5 & 6\end{matrix}) will be converted to a multi-dimensional array.
</div>
<script src="script.js"></script>
<script>
var outputEl = document.getElementById('output');
</script>
</body>
</html>
function parseLatexMatrix(input){
var key = "";
var openPar = 1;
var idx = 0;
var val = "";
for (var i=0;i<input.length;i++){
if (input[i] == "{"){
return false;
}
else if (input[i] == "}"){
key = input.substring(0,i);
idx = i+1;
break;
}
}
var end = input.indexOf("\\end{"+key+"}");
var begin = input.indexOf("\\begin{"+key+"}",idx);
if (end < 0){
return false;
}
if (begin == -1 || begin > end){
val = input.substring(idx,end);
}
else {
return false;
}
if (key.indexOf("matrix") > -1){
var rows = val.trim().split("\\\\");
var out = "[";
for (var i=0;i<rows.length;i++){
if (rows[i].trim().length == 0){continue;}
var row = "[";
row += rows[i].trim().replace(/&/g,", ")+'';
row += "]";
if (i > 0){
out += ", ";
}
out += row;
}
out += "]";
out += input.substring(end+6+key.length);
return out;
}
return false;
}
function latexReplace(input){
for (var i=0;i<input.length;i++){
if (input[i] == "{"){
if (i >=6 && input.substring(i-6,i) == "\\begin"){
var matrix = parseLatexMatrix(input.substring(i+1));
if (matrix){
input = input.substring(0,i-6)+matrix;
}
}
}
}
return input;
}
About
This repl parses (some) LaTeX matrices.
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 matrices in LaTeX syntax (\begin{matrix}1 & 2 & 3\4 & 5 & 6\end{matrix}) to be converted into a multi-dimensional array.
The parsing is quite simple. Add support for more types of matrices?