-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprettyMath.html
More file actions
165 lines (145 loc) · 5.14 KB
/
Copy pathprettyMath.html
File metadata and controls
165 lines (145 loc) · 5.14 KB
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Pretty Math Demo</title>
<link rel="stylesheet" type="text/css" href="mathquill.css" />
</head>
<body>
<span class="mathquill-editable" id="mqContent" >f(x)=?</span><br />
Mathquill: <input id="mqInput" type="text" style="width:200px" /><br />
Symja: <input id="sjaInput" type="text" style="width:200px" />
<script src="jquery.min.js" type="text/javascript" ></script>
<script src="mathquill.js" type="text/javascript" ></script>
<script type="text/javascript" >
$(document).ready(function(){
var mq = $("#mqContent");
var mqC = $("#mqClone");
var mqI = $("#mqInput");
var sjaI = $("#sjaInput");
mq.mathquill('revert');
mq.mathquill('editable');
mq.bind('keyup', function(e){
//alert(e.keyCode);
var latex;
var LOG = 1;
var CHAR = 0;
var TRIG = 1;
var SQRT = 1;
var oldVal = mqI.val();
var edVal = mq.mathquill('latex');
var tempStr = edVal;
// sqrt rule
// change sqrt\: to \sqrt{}
// does not handle cases such as space is already there prior to entering sqrt
var newStr = tempStr.replace(/sqrt\\:/gi,"\\sqrt{}");
if (tempStr != newStr) {
tempStr = newStr;
latex = SQRT;
} else {
newStr = tempStr.replace(/sin\\:/gi,"\\sin\\left(\\right)");
newStr = newStr.replace(/([^\\])sin\\left\(\\right\)|^sin\\left\(\\right\)/gi,"$1\\sin\\left(\\right)");
newStr = newStr.replace(/cos\\:/gi,"\\cos\\left(\\right)");
newStr = newStr.replace(/([^\\])cos\\left\(\\right\)|^cos\\left\(\\right\)/gi,"$1\\cos\\left(\\right)");
newStr = newStr.replace(/tan\\:/gi,"\\tan\\left(\\right)");
newStr = newStr.replace(/([^\\])tan\\left\(\\right\)|^tan\\left\(\\right\)/gi,"$1\\tan\\left(\\right)");
if (tempStr!= newStr) {
tempStr = newStr;
latex = TRIG;
} else {
newStr = tempStr.replace(/pi\\:/gi,"\\pi");
newStr = newStr.replace(/E\\:/g,"\\text{E}");
newStr = newStr.replace(/e\\:/g,"\\text{e}");
if (tempStr != newStr) {
tempStr = newStr;
latex = CHAR;
} else {
newStr = tempStr.replace(/log\\:/gi,"\\log\\left(\\right)");
newStr = newStr.replace(/([^\\])log\\left\(\\right\)|^log\\left\(\\right\)/gi,"$1\\log\\left(\\right)");
newStr = newStr.replace(/ln\\:/gi,"\\ln\\left(\\right)");
newStr = newStr.replace(/([^\\])ln\\left\(\\right\)|^ln\\left\(\\right\)/gi,"$1\\ln\\left(\\right)");
if (tempStr != newStr) {
tempStr = newStr;
latex = LOG;
} else {
}
}
}
}
if (edVal != tempStr) {
edVal = tempStr;
// this causes to lose cursor
var root = mq.data('[[mathquill internal data]]')['block'];
//root.init('\\');
// count cursor steps until end
var next = root.cursor.next;
var steps = latex;
root.cursor.moveRight();
var parent = root.cursor.parent;
while (next != root.cursor.next || !mq.hasClass("hasCursor")) {
steps++;
next = root.cursor.next;
root.cursor.moveRight();
}
var prev = root.cursor.prev;
$(prev).data("cursorPrev", true);
mq.mathquill('latex',edVal).focus();
var root = mq.data('[[mathquill internal data]]')['block'];
// move cursor back to right location
while(steps > 0) {
root.cursor.moveLeft();
steps--;
}
$(prev).data("cursorPrev", false);
}
var newVal = edVal;
mqI.val(newVal);
if (oldVal != newVal) {
mqI.change();
}
});
mqI.change(function(){
var mqVal = mqI.val();
var edVal = mqVal;
var tempStr = edVal;
// change \: to spaces
var newStr = tempStr.replace(/\\:/g," ");
tempStr = newStr;
// change \cdot to *
newStr = tempStr.replace(/\\cdot/g,"*");
tempStr = newStr;
// change \pi to Pi
newStr = tempStr.replace(/\\pi/g,"Pi");
tempStr = newStr;
// text
newStr = tempStr.replace(/\\text{([a-zA-Z0-9]*)}/g,"$1");
tempStr = newStr;
// TODO: NO RECURSION DONE!!! NEED TO CORRECT ASAP
// change frac to division
newStr = tempStr.replace(/\\frac{(.*)}{(.*)}/g,"$1/$2");
tempStr = newStr;
// change {} to () for exponents
newStr = tempStr.replace(/\^{([^}]+)}/g,"^($1)");
tempStr = newStr;
// change \left(\right) to ()
newStr = tempStr.replace(/\\left\(([^\\right.]*)\\right\)/g,"($1)");
tempStr = newStr;
// change trig and log functions
// log and ln are assumed symja syntax
newStr = tempStr.replace(/\\([a-z]*)\(([^\\.]*)\)/gi, function(m,p1, p2) {
if (p1.length == 1) {
return p1[0].toUpperCase() +"["+p2+"]";
} else if (p1.length >1 ) {
return p1[0].toUpperCase() + p1.substr(1) +"["+p2+"]";
}
});
tempStr = newStr;
//sqrt
newStr = tempStr.replace(/\\sqrt{([^}]+)}|\\sqrt{}/gi,"Sqrt[$1]");
tempStr = newStr;
var sjaVal = tempStr;
sjaI.val(sjaVal);
});
});
</script>
</body>
</html>