-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatexalign.html
252 lines (210 loc) · 6.7 KB
/
latexalign.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html>
<head>
<title>Constraint aligner</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>
<style>
input:not([type='checkbox']), textarea {
margin-bottom: .7em;
width: 100%;
}
.box {position: relative}
.container {
border: 1px solid black;
padding: 10px;
}
/* loader from here: https://loading.io/css/ */
.lds-dual-ring {
display: inline-block;
width: 80px;
height: 80px;
}
.lds-dual-ring:after {
content: " ";
display: block;
width: 64px;
height: 64px;
margin: 8px;
border-radius: 50%;
border: 6px solid #000;
border-color: #000 transparent #000 transparent;
animation: lds-dual-ring 1.2s linear infinite;
}
@keyframes lds-dual-ring {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<script>
// only call when MathJax has loaded
function typeset(code) {
MathJax.startup.promise = MathJax.startup.promise
.then(() => {code(); return MathJax.typesetPromise()})
.catch((err) => console.log('Typeset failed: ' + err.message));
return MathJax.startup.promise;
}
/* Replace "x1" with "x_{1}"
* Replace < with \\leq and > with \\geq
* Replace "+" with "& +{} &"
*/
function replaceChars(txt, surroundOperatorsWithAmpersands) {
txt = txt.replace(/([a-zA-Z])(\d+)/g, '$1_{$2}')
.replace(/<=?/g, '\\leq')
.replace(/>=?/g, '\\geq');
if(surroundOperatorsWithAmpersands) {
txt = txt.replace(/([+-])/g, '& $1{} &')
.replace(/(?=\\leq|\\geq|=)/g, '& ')
}
return txt;
}
/* Add missing spaces to each index such that every column has the same length.
* If trailing, spaces are added at the end, otherwise at the beginning
*
* Input:
* [
* [ '123', 'a', 'bc', '1', 'somethingreallylong' ],
* [ '1', 'abc', '', '123', 'short' ]
* ]
*
* Output:
* [
* [ '123', ' a', 'bc', ' 1', 'somethingreallylong' ],
* [ ' 1', 'abc', ' ', '123', ' short' ]
* ]
*/
function makeColumnsSameLength(splits, trailing = false) {
// find longest strings
var longestElements = [];
for(var i = 0; i < splits.length; i++) {
for(var j = 0; j < splits[i].length; j++) {
if(splits[i][j].length > (longestElements[j] || 0))
longestElements[j] = splits[i][j].length;
}
}
// add spaces
for(var i = 0; i < splits.length; i++) {
for(var j = 0; j < splits[i].length; j++) {
var diff = longestElements[j] - splits[i][j].length;
if(diff > 0) {
if(trailing)
splits[i][j] = splits[i][j] + ' '.repeat(diff);
else
splits[i][j] = ' '.repeat(diff) + splits[i][j];
}
}
}
}
// TODO rearranging vars
function rearrangeVars(splits) {
var vars = {}
for(var r = 0; r < splits.length; r++) {
for(var c = 0; c < splits[r].length; c++) {
var newVar = /(?:[a-zA-Z]+)(?:_\{\d+\})?/.exec(splits[r][c]);
if(newVar === null)
continue;
if(!vars.hasOwnProperty(newVar))
vars[newVar[0]] = [ [r, c] ];
else
vars[newVar[0]].push([r, c]);
}
}
var sortedKeys = Object.keys(vars).sort();
for(var i = 0; i < sortedKeys.length; i++) {
// AAAAH
}
}
function alignAmpersands(subjectTo, addMissingAmp, alignVertically, alignWith = '&') {
var lines = subjectTo.split('\n');
var splits = [];
var max = 0;
// split &, find line with most &
for(var i = 0; i < lines.length; i++) {
splits.push(lines[i].split(alignWith));
if(splits[i].length > max)
max = splits[i].length;
}
// add missing &
if(addMissingAmp) {
for(var i = 0; i < splits.length; i++) {
var missing = max - splits[i].length;
if(missing > 0) {
var ampersands = [];
for(var amp = 0; amp < missing; amp++) {
// note & is readded when joining
ampersands.push('');
}
splits[i].splice.apply(splits[i], [splits[i].length - 1, 0].concat(ampersands));
}
}
rearrangeVars(splits);
}
// find longest strings between ampersands
if(alignVertically)
makeColumnsSameLength(splits);
// join with ampersands
for(var i = 0; i < splits.length; i++) {
lines[i] = splits[i].join(alignWith);
}
return lines.join('\n');
}
function updateMath() {
var mathParts = [
'\\begin{align*}\n\\max &\\; ', // objective function
' \\\\\n\\text{subject to} &\\;\n\\begin{alignedat}[t]{', // number of variables
'}\n', // subject to
'\n\\end{alignedat}\n\\end{align*}'
];
var objFunction = replaceChars(document.querySelector('#MathInputObjective').value, false);
var subjectTo = replaceChars(document.querySelector('#subjectTos').value, true);
subjectTo = alignAmpersands(subjectTo, document.querySelector('#addmissing').checked, document.querySelector('#alignvertically').checked);
subjectTo = subjectTo.replace(/(?=\n)/g, ' \\\\');
var lines = subjectTo.split('\n');
var max = Math.floor(subjectTo.split('\n')[0].split('&').length / 2);
var amath = mathParts[0] + objFunction +
mathParts[1] + max +
mathParts[2] + subjectTo +
mathParts[3];
document.querySelector('#latexcode').innerHTML = amath;
// make sure MathJax has loaded
if(window.MathJax) {
typeset(() => {
var out = document.querySelector('#MathOutput');
out.innerHTML = '$$' + amath + '$$';
return out;
});
}
}
// render math part as soon as MathJax has loaded
window.addEventListener('load', function() {
var out = document.querySelector('#MathOutput');
out.classList.remove('lds-dual-ring');
window.updateMath();
});
</script>
<div class="container">
<p>
Maximize<br />
<input id="MathInputObjective" size="80" value="x1 + x2 + x3" onkeyup="updateMath()" /><br />
Subject to<br />
<textarea name="MathInputSubjectTo" id="subjectTos" cols="30" rows="10" onkeyup="updateMath()">2x1 + x2 + x3 < 5
2.5x1 - 3x2 + x3 = 6
x1 + x2 - 1.4x3 > 12</textarea>
</p>
<label><input type="checkbox" name="addmissing" id="addmissing" checked onchange="updateMath()">Add missing &</label><br />
<label><input type="checkbox" name="vercicalalign" id="alignvertically" checked onchange="updateMath()">Vertically align &</label><br />
<pre>
<code id="latexcode"></code>
</pre>
<div id="MathOutput" class="output lds-dual-ring"></div>
</div>
</body>
</html>