-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadLatex.js
190 lines (157 loc) · 5.37 KB
/
loadLatex.js
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
function setupEquationBlock(tex)
{
tex = createP();
tex.style('font-size', '12px');
tex.position(50, height-350);
// let linkStr = String.raw`\htmlClass{EQDIV}{\href{https://katex.org/}{\KaTeX}} other`
let simpleOptions = {
trust:true,
}
katex.render(eqStr, tex.elt,simpleOptions)
// attach color-class specific mouseOver functions
let redEq = selectAll('.CRClass',tex);
console.log(redEq)
redEq.forEach( (eq) =>{
eq.mouseOver(eigOverFn)
eq.mouseOut(eigOutFn)
});
let dynEq = selectAll('.CYClass',tex);
console.log(dynEq)
dynEq.forEach( (eq) =>{
eq.mouseOver(dynOverFn)
eq.mouseOut(dynOutFn)
});
let initEq = selectAll('.CLClass',tex);
initEq.forEach( (eq) =>{
eq.mouseOver(x0OverFn)
eq.mouseOut(x0OutFn)
});
let xtEq = selectAll('.XTClass',tex);
xtEq.forEach( (eq) =>{
eq.mouseOver(xtOverFn)
eq.mouseOut(xtOutFn)
});
let realEq = selectAll('.RealClass',tex);
realEq.forEach( (eq) =>{
eq.mouseOver(realOverFn)
eq.mouseOut(realOutFn)
});
let imagEq = selectAll('.ImagClass',tex);
imagEq.forEach( (eq) =>{
eq.mouseOver(imagOverFn)
eq.mouseOut(imagOutFn)
});
/*
*/
return tex;
}
function setupLatexColors(cText,cHigh,cLow,c1,c2,c3)
{
//automate this with a dictionary: {tag:"CH",color:c1,mouseOverFun:eigMouseOver()}
eqStr = ""
//cMain, cBracket, cInner must be defined, either here or in eq1.txt
eqStr += buildColorDef("cMain", pColorToHexStr(cText))
eqStr += buildColorMacro("CH", pColorToHexStr(cHigh))
eqStr += buildColorMacro("CL", pColorToHexStr(cLow))
eqStr += buildColorMacro("CR", pColorToHexStr(c1))
eqStr += buildColorMacro("CB", pColorToHexStr(c2))
eqStr += buildColorMacro("CY", pColorToHexStr(c3))
eqStr += "\\color{\\cMainDef}"
return eqStr
}
function pColorToHexStr(pcolor)
{
return pcolor.toString('#rrggbb')
}
//https://katex.org/docs/supported.html
function buildColorMacro(macroName, macroColorStr)
{
//expects macroColor to be a string formatted like "#AABBCC"
defStr = buildColorDef(macroName,macroColorStr)
//old functional def
//return defStr+"\\newcommand{\\"+macroName+"}[1]{\\textcolor{"+macroColorStr.substring(1)+"}{#1}} \n"
//twist: add html class in with macro defintion so we can find these later
return defStr+"\\newcommand{\\"+macroName+"}[1]{\\htmlClass{"+macroName+"Class}{\\textcolor{"+macroColorStr.substring(1)+"}{#1}}} \n"
//return defStr+String.raw`\\newcommand{\\${macroName}}[1]{\\textcolor{${macroColorStr.substring(1)}}{#1}} \n`
}
function buildColorDef(macroName, macroColorStr)
{
//expects macroColor to be a string formatted like "#AABBCC"
//return "\\newcommand{\\"+macroName+"}[1]{ \\textcolor{"+macroColorStr.substring(1)+"}{#1} } "
return "\\def \\"+macroName+"Def {"+macroColorStr.substring(1)+"}\n"
}
// function doubleSlash(strIn)
// {
// var strOut = strIn.replace(/\\/g, "||")
// return strOut;
// }
/* Notes down here */
//https://katex.org/docs/supported.html
// https://discourse.processing.org/t/latex-in-processing/19691
// https://cs.nyu.edu/~kapp/cs101/processing_on_the_web/
//
function showFile(input) {
let file = input.files[0];
alert(`File name: ${file.name}`); // e.g my.png
//alert(`Last modified: ${file.lastModified}`); // e.g 1552830408824
var fileReader = new FileReader();
fileReader.onload = function(event) {
console.log(event.target.result);
}
// var file2 = event.target.files[0];
fileReader.readAsText(file);
}
// Try more regexes here: https://regex101.com/r/0XhJre/1
// TRY this regex structure instead!!
// https://stackoverflow.com/questions/46029392/search-and-replace-outer-tag-in-atom-using-regex
// <span class="klass">(.*?)</span>
// <code>$1</code>
function repCMDandDef(strArray, findPre, repPre)
{
let findCmdPre = `\\\\${findPre}{`
let repCmdPre = `\\${repPre}{`
let findDefPre = `\\\\LBC{\\\\${findPre}Def}`
let repDefPre = `\\LBC{\\${repPre}Def}`
//e.g. replace ( \CY{xyz} with \CR{xyz} )
//let textRepd = repEachBracket(strArray,findCmdPre,`}`,repCmdPre,`}`);
//e.g. replace ( \LBC{\CYDef}x\RBC{} with \LBC{\CRDef}x\RBC{} )
//textRepd = repEachBracket(textRepd,findDefPre,'\RBC{}',repDefPre,'\RBC{}');
textRepd = strArray;
return textRepd;
}
function repEachBracket(strArray,findPre,findPost, repPre,repPost)
{
//finds between "\fStr{" and "}"
let regp = new RegExp(`${findPre}(.+?)${findPost}`,"gms");
console.log(regp)
//let regp = /\\CR{(.+?)}/g;//'\\CR{.+?}/gm'
//let regp = new RegExp(`\LBC`,"gms");
// let regp = new RegExp(`${findPre}(.+?)${findPost}`,"gs");
//
//
if (Array.isArray(strArray))
for (let i=0; i<strArray.length; i++)
{
let s = strArray[i];
//s = s.replace(fStr,repStr);
let middle = s.match(regp)
if (middle){
console.log("a match!")
console.log(middle)
}
s = s.replaceAll(regp, `${repPre}$1${repPost}`)
strArray[i]=s;
}
else {
let middle = strArray.match(regp)
if (middle){
console.log("a match!!")
console.log(middle)
}
// console.log("replacing with:")
// console.log(`${repPre} XXX ${repPost}`)
strArray = strArray.replaceAll(regp, `${repPre}$1${repPost}`)
}
console.log('done')
return strArray;
}