-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathtex.html
94 lines (86 loc) · 3.2 KB
/
mathtex.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LaTeX Math Formula Renderer</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.0/es5/tex-mml-chtml.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #121212;
color: #e0e0e0;
}
#inputBox {
width: 100%;
padding: 10px;
font-size: 16px;
margin-bottom: 20px;
background-color: #333;
color: #e0e0e0;
border: 1px solid #555;
}
#output {
padding: 20px;
border: 1px solid #555;
background-color: #1e1e1e;
color: #e0e0e0;
white-space: pre-wrap;
}
</style>
</head>
<body>
<h1>LaTeX Math Formula Renderer</h1>
<input type="text" id="inputBox" placeholder="Enter LaTeX formula here">
<div id="output"></div>
<script>
const inputBox = document.getElementById('inputBox');
const output = document.getElementById('output');
const colors = ['#ffcccc', '#ccffcc', '#ccccff', '#ffffcc', '#ffccff', '#ccffff'];
function colorizeBrackets(formula) {
let colorIndex = 0;
const stack = [];
let result = '';
for (let i = 0; i < formula.length; i++) {
const char = formula[i];
if (char === '(') {
const color = colors[colorIndex % colors.length];
result += `\\textcolor{${color}}{(}`;
stack.push(color);
colorIndex++;
} else if (char === ')') {
const color = stack.pop();
result += `\\textcolor{${color}}{)}`;
} else if (char === '=' && (i + 1 < formula.length) && /^[+-]$/.test(formula[i + 1])) {
result += `${char}\\textcolor{red}{${formula[i + 1]}}`;
i++; // Skip the next character as it has been processed
} else {
result += char;
}
}
return result;
}
function colorizeFirstSign(formula) {
const firstSignMatch = formula.match(/^([+-])/);
if (firstSignMatch) {
const firstSign = firstSignMatch[0];
const restOfFormula = formula.slice(1);
return `\\textcolor{red}{${firstSign}}${restOfFormula}`;
}
return formula;
}
function processFormula(formula) {
formula = colorizeFirstSign(formula.trim());
formula = colorizeBrackets(formula);
return formula;
}
inputBox.addEventListener('input', () => {
const formula = inputBox.value;
const processedFormula = processFormula(formula);
output.innerHTML = `$$${processedFormula}$$`;
MathJax.typesetPromise([output]);
});
</script>
</body>
</html>