Debian Codegraph @

unpacked/jax/input/AsciiMath/jax.js

raw
1 /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3
4 /*************************************************************
5 *
6 * MathJax/jax/input/AsciiMath/jax.js
7 *
8 * An Input Jax for AsciiMath notation
9 * (see http://www1.chapman.edu/~jipsen/mathml/asciimath.html).
10 *
11 * Originally adapted for MathJax by David Lippman.
12 * Additional work done by Davide P. Cervone.
13 *
14 * The current development repository for AsciiMathML is
15 * https://github.com/mathjax/asciimathml
16 *
17 * A portion of this file is taken from
18 * ASCIIMathML.js Version 2.2 Mar 3, 2014, (c) Peter Jipsen http://www.chapman.edu/~jipsen
19 * and is used by permission of Peter Jipsen, who has agreed to allow us to
20 * release it under the Apache2 license (see below). That portion is indicated
21 * via comments.
22 *
23 * The remainder falls under the copyright that follows.
24 *
25 * ---------------------------------------------------------------------
26 *
27 * Copyright (c) 2012-2020 The MathJax Consortium
28 *
29 * Licensed under the Apache License, Version 2.0 (the "License");
30 * you may not use this file except in compliance with the License.
31 * You may obtain a copy of the License at
32 *
33 * http://www.apache.org/licenses/LICENSE-2.0
34 *
35 * Unless required by applicable law or agreed to in writing, software
36 * distributed under the License is distributed on an "AS IS" BASIS,
37 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 */
41
42 (function (ASCIIMATH) {
43
44 var MML; // Filled in later
45
46 //
47 // Make a documentFragment work-alike that uses MML objects
48 // rather than DOM objects.
49 //
50 var DOCFRAG = MathJax.Object.Subclass({
51 firstChild: null,
52 lastChild: null,
53 Init: function () {
54 this.childNodes = [];
55 },
56 appendChild: function (node) {
57 if (node.parent) {node.parent.removeChild(node)}
58 if (this.lastChild) {this.lastChild.nextSibling = node}
59 if (!this.firstChild) {this.firstChild = node}
60 this.childNodes.push(node); node.parent = this;
61 this.lastChild = node;
62 return node;
63 },
64 removeChild: function (node) {
65 for (var i = 0, m = this.childNodes.length; i < m; i++)
66 {if (this.childNodes[i] === node) break}
67 if (i === m) return;
68 this.childNodes.splice(i,1);
69 if (node === this.firstChild) {this.firstChild = node.nextSibling}
70 if (node === this.lastChild) {
71 if (!this.childNodes.length) {this.lastChild = null}
72 else {this.lastChild = this.childNodes[this.childNodes.length-1]}
73 }
74 if (i) {this.childNodes[i-1].nextSibling = node.nextSibling}
75 node.nextSibling = node.parent = null;
76 return node;
77 },
78 replaceChild: function (node,old) {
79 for (var i = 0, m = this.childNodes.length; i < m; i++)
80 {if (this.childNodes[i] === old) break}
81 if (i) {this.childNodes[i-1].nextSibling = node} else {this.firstChild = node}
82 if (i >= m-1) {this.lastChild = node}
83 this.childNodes[i] = node; node.nextSibling = old.nextSibling;
84 old.nextSibling = old.parent = null;
85 return old;
86 },
87 hasChildNodes: function (node) {
88 return (this.childNodes.length>0);
89 },
90 toString: function () {return "{"+this.childNodes.join("")+"}"}
91 });
92
93 var INITASCIIMATH = function () {
94 MML = MathJax.ElementJax.mml;
95 var MBASEINIT = MML.mbase.prototype.Init;
96
97 //
98 // Make MML elements looks like DOM elements (add the
99 // methods that AsciiMath needs)
100 //
101 MML.mbase.Augment({
102 firstChild: null,
103 lastChild: null,
104 nodeValue: null,
105 nextSibling: null,
106 Init: function () {
107 var obj = MBASEINIT.apply(this,arguments) || this;
108 obj.childNodes = obj.data;
109 obj.nodeName = obj.type;
110 return obj;
111 },
112 appendChild: function (node) {
113 if (node.parent) {node.parent.removeChild(node)}
114 var nodes = arguments;
115 if (node.isa(DOCFRAG)) {
116 nodes = node.childNodes;
117 node.data = node.childNodes = [];
118 node.firstChild = node.lastChild = null;
119 }
120 for (var i = 0, m = nodes.length; i < m; i++) {
121 node = nodes[i];
122 if (this.lastChild) {this.lastChild.nextSibling = node}
123 if (!this.firstChild) {this.firstChild = node}
124 this.Append(node);
125 this.lastChild = node;
126 }
127 return node;
128 },
129 removeChild: function (node) {
130 for (var i = 0, m = this.childNodes.length; i < m; i++)
131 {if (this.childNodes[i] === node) break}
132 if (i === m) return;
133 this.childNodes.splice(i,1);
134 if (node === this.firstChild) {this.firstChild = node.nextSibling}
135 if (node === this.lastChild) {
136 if (!this.childNodes.length) {this.lastChild = null}
137 else {this.lastChild = this.childNodes[this.childNodes.length-1]}
138 }
139 if (i) {this.childNodes[i-1].nextSibling = node.nextSibling}
140 node.nextSibling = node.parent = null;
141 return node;
142 },
143 replaceChild: function (node,old) {
144 for (var i = 0, m = this.childNodes.length; i < m; i++)
145 {if (this.childNodes[i] === old) break}
146 // FIXME: make this work with DOCFRAG's?
147 if (i) {this.childNodes[i-1].nextSibling = node} else {this.firstChild = node}
148 if (i >= m-1) {this.lastChild = node}
149 this.SetData(i,node); node.nextSibling = old.nextSibling;
150 old.nextSibling = old.parent = null;
151 return old;
152 },
153 hasChildNodes: function (node) {
154 return (this.childNodes.length>0);
155 },
156 setAttribute: function (name,value) {this[name] = value}
157 });
158 };
159
160 //
161 // Set up to isolate ASCIIMathML.js
162 //
163
164 var window = {}; // hide the true window
165
166 //
167 // Hide the true document, and add functions that
168 // use and produce MML objects instead of DOM objects
169 //
170 var document = {
171 getElementById: true,
172 createElementNS: function (ns,type) {
173 var node = MML[type]();
174 if (type === "mo" && ASCIIMATH.config.useMathMLspacing) {node.useMMLspacing = 0x80}
175 return node;
176 },
177 createTextNode: function (text) {return MML.chars(text).With({nodeValue:text})},
178 createDocumentFragment: function () {return DOCFRAG()}
179 };
180
181 var navigator = {appName: "MathJax"}; // hide the true navigator object
182
183 /******************************************************************
184 *
185 * The following section is ASCIIMathML.js Version 2.2
186 * (c) Peter Jipsen, used with permission.
187 *
188 * Some sections are commented out to save space in the
189 * minified version (but that is not strictly necessary).
190 *
191 ******************************************************************/
192
193 /*
194 ASCIIMathML.js
195 ==============
196 This file contains JavaScript functions to convert ASCII math notation
197 and (some) LaTeX to Presentation MathML. The conversion is done while the
198 HTML page loads, and should work with Firefox and other browsers that can
199 render MathML.
200
201 Just add the next line to your HTML page with this file in the same folder:
202
203 <script type="text/javascript" src="ASCIIMathML.js"></script>
204
205 Version 2.2 Mar 3, 2014.
206 Latest version at https://github.com/mathjax/asciimathml
207 If you use it on a webpage, please send the URL to [email protected]
208
209 Copyright (c) 2014 Peter Jipsen and other ASCIIMathML.js contributors
210
211 Permission is hereby granted, free of charge, to any person obtaining a copy
212 of this software and associated documentation files (the "Software"), to deal
213 in the Software without restriction, including without limitation the rights
214 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
215 copies of the Software, and to permit persons to whom the Software is
216 furnished to do so, subject to the following conditions:
217
218 The above copyright notice and this permission notice shall be included in
219 all copies or substantial portions of the Software.
220
221 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
222 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
223 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
224 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
225 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
226 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
227 THE SOFTWARE.
228 */
229 //var asciimath = {};
230
231 //(function(){
232 var mathcolor = "blue"; // change it to "" (to inherit) or another color
233 //var mathfontsize = "1em"; // change to e.g. 1.2em for larger math
234 //var mathfontfamily = "serif"; // change to "" to inherit (works in IE)
235 // or another family (e.g. "arial")
236 //var automathrecognize = false; // writing "amath" on page makes this true
237 //var checkForMathML = true; // check if browser can display MathML
238 //var notifyIfNoMathML = true; // display note at top if no MathML capability
239 //var alertIfNoMathML = false; // show alert box if no MathML capability
240 //var translateOnLoad = true; // set to false to do call translators from js
241 //var translateASCIIMath = true; // false to preserve `..`
242 var displaystyle = true; // puts limits above and below large operators
243 var showasciiformulaonhover = true; // helps students learn ASCIIMath
244 var decimalsign = "."; // change to "," if you like, beware of `(1,2)`!
245 //var AMdelimiter1 = "`", AMescape1 = "\\\\`"; // can use other characters
246 //var AMdocumentId = "wikitext" // PmWiki element containing math (default=body)
247 var fixphi = true; //false to return to legacy phi/varphi mapping
248
249 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
250
251 var isIE = (navigator.appName.slice(0,9)=="Microsoft");
252 /*
253 var noMathML = false, translated = false;
254
255 if (isIE) { // add MathPlayer info to IE webpages
256 document.write("<object id=\"mathplayer\"\
257 classid=\"clsid:32F66A20-7614-11D4-BD11-00104BD3F987\"></object>");
258 document.write("<?import namespace=\"m\" implementation=\"#mathplayer\"?>");
259 }
260
261 // Add a stylesheet, replacing any previous custom stylesheet (adapted from TW)
262 function setStylesheet(s) {
263 var id = "AMMLcustomStyleSheet";
264 var n = document.getElementById(id);
265 if(document.createStyleSheet) {
266 // Test for IE's non-standard createStyleSheet method
267 if(n)
268 n.parentNode.removeChild(n);
269 // This failed without the &nbsp;
270 document.getElementsByTagName("head")[0].insertAdjacentHTML("beforeEnd","&nbsp;<style id='" + id + "'>" + s + "</style>");
271 } else {
272 if(n) {
273 n.replaceChild(document.createTextNode(s),n.firstChild);
274 } else {
275 n = document.createElement("style");
276 n.type = "text/css";
277 n.id = id;
278 n.appendChild(document.createTextNode(s));
279 document.getElementsByTagName("head")[0].appendChild(n);
280 }
281 }
282 }
283
284 setStylesheet("#AMMLcloseDiv \{font-size:0.8em; padding-top:1em; color:#014\}\n#AMMLwarningBox \{position:absolute; width:100%; top:0; left:0; z-index:200; text-align:center; font-size:1em; font-weight:bold; padding:0.5em 0 0.5em 0; color:#ffc; background:#c30\}");
285
286 function init(){
287 var msg, warnings = new Array();
288 if (document.getElementById==null){
289 alert("This webpage requires a recent browser such as Mozilla Firefox");
290 return null;
291 }
292 if (checkForMathML && (msg = checkMathML())) warnings.push(msg);
293 if (warnings.length>0) displayWarnings(warnings);
294 if (!noMathML) initSymbols();
295 return true;
296 }
297
298 function checkMathML(){
299 if (navigator.appName.slice(0,8)=="Netscape")
300 if (navigator.appVersion.slice(0,1)>="5") noMathML = null;
301 else noMathML = true;
302 else if (navigator.appName.slice(0,9)=="Microsoft")
303 try {
304 var ActiveX = new ActiveXObject("MathPlayer.Factory.1");
305 noMathML = null;
306 } catch (e) {
307 noMathML = true;
308 }
309 else if (navigator.appName.slice(0,5)=="Opera")
310 if (navigator.appVersion.slice(0,3)>="9.5") noMathML = null;
311 else noMathML = true;
312 //noMathML = true; //uncomment to check
313 if (noMathML && notifyIfNoMathML) {
314 var msg = "To view the ASCIIMathML notation use Internet Explorer + MathPlayer or Mozilla Firefox 2.0 or later.";
315 if (alertIfNoMathML)
316 alert(msg);
317 else return msg;
318 }
319 }
320
321 function hideWarning(){
322 var body = document.getElementsByTagName("body")[0];
323 body.removeChild(document.getElementById('AMMLwarningBox'));
324 body.onclick = null;
325 }
326
327 function displayWarnings(warnings) {
328 var i, frag, nd = createElementXHTML("div");
329 var body = document.getElementsByTagName("body")[0];
330 body.onclick=hideWarning;
331 nd.id = 'AMMLwarningBox';
332 for (i=0; i<warnings.length; i++) {
333 frag = createElementXHTML("div");
334 frag.appendChild(document.createTextNode(warnings[i]));
335 frag.style.paddingBottom = "1.0em";
336 nd.appendChild(frag);
337 }
338 nd.appendChild(createElementXHTML("p"));
339 nd.appendChild(document.createTextNode("For instructions see the "));
340 var an = createElementXHTML("a");
341 an.appendChild(document.createTextNode("ASCIIMathML"));
342 an.setAttribute("href","http://www.chapman.edu/~jipsen/asciimath.html");
343 nd.appendChild(an);
344 nd.appendChild(document.createTextNode(" homepage"));
345 an = createElementXHTML("div");
346 an.id = 'AMMLcloseDiv';
347 an.appendChild(document.createTextNode('(click anywhere to close this warning)'));
348 nd.appendChild(an);
349 var body = document.getElementsByTagName("body")[0];
350 body.insertBefore(nd,body.childNodes[0]);
351 }
352
353 function translate(spanclassAM) {
354 if (!translated) { // run this only once
355 translated = true;
356 var body = document.getElementsByTagName("body")[0];
357 var processN = document.getElementById(AMdocumentId);
358 if (translateASCIIMath) AMprocessNode((processN!=null?processN:body), false, spanclassAM);
359 }
360 }
361 */
362 function createElementXHTML(t) {
363 if (isIE) return document.createElement(t);
364 else return document.createElementNS("http://www.w3.org/1999/xhtml",t);
365 }
366
367 var AMmathml = "http://www.w3.org/1998/Math/MathML";
368
369 function AMcreateElementMathML(t) {
370 if (isIE) return document.createElement("m:"+t);
371 else return document.createElementNS(AMmathml,t);
372 }
373
374 function createMmlNode(t,frag) {
375 var node;
376 if (isIE) node = document.createElement("m:"+t);
377 else node = document.createElementNS(AMmathml,t);
378 if (frag) node.appendChild(frag);
379 return node;
380 }
381
382 function newcommand(oldstr,newstr) {
383 AMsymbols.push({input:oldstr, tag:"mo", output:newstr, tex:null, ttype:DEFINITION});
384 refreshSymbols();
385 }
386
387 function newsymbol(symbolobj) {
388 AMsymbols.push(symbolobj);
389 refreshSymbols();
390 }
391
392 // character lists for Mozilla/Netscape fonts
393 var AMcal = ["\uD835\uDC9C","\u212C","\uD835\uDC9E","\uD835\uDC9F","\u2130","\u2131","\uD835\uDCA2","\u210B","\u2110","\uD835\uDCA5","\uD835\uDCA6","\u2112","\u2133","\uD835\uDCA9","\uD835\uDCAA","\uD835\uDCAB","\uD835\uDCAC","\u211B","\uD835\uDCAE","\uD835\uDCAF","\uD835\uDCB0","\uD835\uDCB1","\uD835\uDCB2","\uD835\uDCB3","\uD835\uDCB4","\uD835\uDCB5","\uD835\uDCB6","\uD835\uDCB7","\uD835\uDCB8","\uD835\uDCB9","\u212F","\uD835\uDCBB","\u210A","\uD835\uDCBD","\uD835\uDCBE","\uD835\uDCBF","\uD835\uDCC0","\uD835\uDCC1","\uD835\uDCC2","\uD835\uDCC3","\u2134","\uD835\uDCC5","\uD835\uDCC6","\uD835\uDCC7","\uD835\uDCC8","\uD835\uDCC9","\uD835\uDCCA","\uD835\uDCCB","\uD835\uDCCC","\uD835\uDCCD","\uD835\uDCCE","\uD835\uDCCF"];
394
395 var AMfrk = ["\uD835\uDD04","\uD835\uDD05","\u212D","\uD835\uDD07","\uD835\uDD08","\uD835\uDD09","\uD835\uDD0A","\u210C","\u2111","\uD835\uDD0D","\uD835\uDD0E","\uD835\uDD0F","\uD835\uDD10","\uD835\uDD11","\uD835\uDD12","\uD835\uDD13","\uD835\uDD14","\u211C","\uD835\uDD16","\uD835\uDD17","\uD835\uDD18","\uD835\uDD19","\uD835\uDD1A","\uD835\uDD1B","\uD835\uDD1C","\u2128","\uD835\uDD1E","\uD835\uDD1F","\uD835\uDD20","\uD835\uDD21","\uD835\uDD22","\uD835\uDD23","\uD835\uDD24","\uD835\uDD25","\uD835\uDD26","\uD835\uDD27","\uD835\uDD28","\uD835\uDD29","\uD835\uDD2A","\uD835\uDD2B","\uD835\uDD2C","\uD835\uDD2D","\uD835\uDD2E","\uD835\uDD2F","\uD835\uDD30","\uD835\uDD31","\uD835\uDD32","\uD835\uDD33","\uD835\uDD34","\uD835\uDD35","\uD835\uDD36","\uD835\uDD37"];
396
397 var AMbbb = ["\uD835\uDD38","\uD835\uDD39","\u2102","\uD835\uDD3B","\uD835\uDD3C","\uD835\uDD3D","\uD835\uDD3E","\u210D","\uD835\uDD40","\uD835\uDD41","\uD835\uDD42","\uD835\uDD43","\uD835\uDD44","\u2115","\uD835\uDD46","\u2119","\u211A","\u211D","\uD835\uDD4A","\uD835\uDD4B","\uD835\uDD4C","\uD835\uDD4D","\uD835\uDD4E","\uD835\uDD4F","\uD835\uDD50","\u2124","\uD835\uDD52","\uD835\uDD53","\uD835\uDD54","\uD835\uDD55","\uD835\uDD56","\uD835\uDD57","\uD835\uDD58","\uD835\uDD59","\uD835\uDD5A","\uD835\uDD5B","\uD835\uDD5C","\uD835\uDD5D","\uD835\uDD5E","\uD835\uDD5F","\uD835\uDD60","\uD835\uDD61","\uD835\uDD62","\uD835\uDD63","\uD835\uDD64","\uD835\uDD65","\uD835\uDD66","\uD835\uDD67","\uD835\uDD68","\uD835\uDD69","\uD835\uDD6A","\uD835\uDD6B"];
398 /*var AMcal = [0xEF35,0x212C,0xEF36,0xEF37,0x2130,0x2131,0xEF38,0x210B,0x2110,0xEF39,0xEF3A,0x2112,0x2133,0xEF3B,0xEF3C,0xEF3D,0xEF3E,0x211B,0xEF3F,0xEF40,0xEF41,0xEF42,0xEF43,0xEF44,0xEF45,0xEF46];
399 var AMfrk = [0xEF5D,0xEF5E,0x212D,0xEF5F,0xEF60,0xEF61,0xEF62,0x210C,0x2111,0xEF63,0xEF64,0xEF65,0xEF66,0xEF67,0xEF68,0xEF69,0xEF6A,0x211C,0xEF6B,0xEF6C,0xEF6D,0xEF6E,0xEF6F,0xEF70,0xEF71,0x2128];
400 var AMbbb = [0xEF8C,0xEF8D,0x2102,0xEF8E,0xEF8F,0xEF90,0xEF91,0x210D,0xEF92,0xEF93,0xEF94,0xEF95,0xEF96,0x2115,0xEF97,0x2119,0x211A,0x211D,0xEF98,0xEF99,0xEF9A,0xEF9B,0xEF9C,0xEF9D,0xEF9E,0x2124];*/
401
402 var CONST = 0, UNARY = 1, BINARY = 2, INFIX = 3, LEFTBRACKET = 4,
403 RIGHTBRACKET = 5, SPACE = 6, UNDEROVER = 7, DEFINITION = 8,
404 LEFTRIGHT = 9, TEXT = 10, /*BIG = 11, LONG = 12, STRETCHY = 13,
405 MATRIX = 14,*/ UNARYUNDEROVER = 15; // token types
406
407 var AMquote = {input:"\"", tag:"mtext", output:"mbox", tex:null, ttype:TEXT};
408
409 var AMsymbols = [
410 //some greek symbols
411 {input:"alpha", tag:"mi", output:"\u03B1", tex:null, ttype:CONST},
412 {input:"beta", tag:"mi", output:"\u03B2", tex:null, ttype:CONST},
413 {input:"chi", tag:"mi", output:"\u03C7", tex:null, ttype:CONST},
414 {input:"delta", tag:"mi", output:"\u03B4", tex:null, ttype:CONST},
415 {input:"Delta", tag:"mo", output:"\u0394", tex:null, ttype:CONST},
416 {input:"epsi", tag:"mi", output:"\u03B5", tex:"epsilon", ttype:CONST},
417 {input:"varepsilon", tag:"mi", output:"\u025B", tex:null, ttype:CONST},
418 {input:"eta", tag:"mi", output:"\u03B7", tex:null, ttype:CONST},
419 {input:"gamma", tag:"mi", output:"\u03B3", tex:null, ttype:CONST},
420 {input:"Gamma", tag:"mo", output:"\u0393", tex:null, ttype:CONST},
421 {input:"iota", tag:"mi", output:"\u03B9", tex:null, ttype:CONST},
422 {input:"kappa", tag:"mi", output:"\u03BA", tex:null, ttype:CONST},
423 {input:"lambda", tag:"mi", output:"\u03BB", tex:null, ttype:CONST},
424 {input:"Lambda", tag:"mo", output:"\u039B", tex:null, ttype:CONST},
425 {input:"lamda", tag:"mi", output:"\u03BB", tex:null, ttype:CONST},
426 {input:"Lamda", tag:"mo", output:"\u039B", tex:null, ttype:CONST},
427 {input:"mu", tag:"mi", output:"\u03BC", tex:null, ttype:CONST},
428 {input:"nu", tag:"mi", output:"\u03BD", tex:null, ttype:CONST},
429 {input:"omega", tag:"mi", output:"\u03C9", tex:null, ttype:CONST},
430 {input:"Omega", tag:"mo", output:"\u03A9", tex:null, ttype:CONST},
431 {input:"phi", tag:"mi", output:fixphi?"\u03D5":"\u03C6", tex:null, ttype:CONST},
432 {input:"varphi", tag:"mi", output:fixphi?"\u03C6":"\u03D5", tex:null, ttype:CONST},
433 {input:"Phi", tag:"mo", output:"\u03A6", tex:null, ttype:CONST},
434 {input:"pi", tag:"mi", output:"\u03C0", tex:null, ttype:CONST},
435 {input:"Pi", tag:"mo", output:"\u03A0", tex:null, ttype:CONST},
436 {input:"psi", tag:"mi", output:"\u03C8", tex:null, ttype:CONST},
437 {input:"Psi", tag:"mi", output:"\u03A8", tex:null, ttype:CONST},
438 {input:"rho", tag:"mi", output:"\u03C1", tex:null, ttype:CONST},
439 {input:"sigma", tag:"mi", output:"\u03C3", tex:null, ttype:CONST},
440 {input:"Sigma", tag:"mo", output:"\u03A3", tex:null, ttype:CONST},
441 {input:"tau", tag:"mi", output:"\u03C4", tex:null, ttype:CONST},
442 {input:"theta", tag:"mi", output:"\u03B8", tex:null, ttype:CONST},
443 {input:"vartheta", tag:"mi", output:"\u03D1", tex:null, ttype:CONST},
444 {input:"Theta", tag:"mo", output:"\u0398", tex:null, ttype:CONST},
445 {input:"upsilon", tag:"mi", output:"\u03C5", tex:null, ttype:CONST},
446 {input:"xi", tag:"mi", output:"\u03BE", tex:null, ttype:CONST},
447 {input:"Xi", tag:"mo", output:"\u039E", tex:null, ttype:CONST},
448 {input:"zeta", tag:"mi", output:"\u03B6", tex:null, ttype:CONST},
449
450 //binary operation symbols
451 //{input:"-", tag:"mo", output:"\u0096", tex:null, ttype:CONST},
452 {input:"*", tag:"mo", output:"\u22C5", tex:"cdot", ttype:CONST},
453 {input:"**", tag:"mo", output:"\u2217", tex:"ast", ttype:CONST},
454 {input:"***", tag:"mo", output:"\u22C6", tex:"star", ttype:CONST},
455 {input:"//", tag:"mo", output:"/", tex:null, ttype:CONST},
456 {input:"\\\\", tag:"mo", output:"\\", tex:"backslash", ttype:CONST},
457 {input:"setminus", tag:"mo", output:"\\", tex:null, ttype:CONST},
458 {input:"xx", tag:"mo", output:"\u00D7", tex:"times", ttype:CONST},
459 {input:"|><", tag:"mo", output:"\u22C9", tex:"ltimes", ttype:CONST},
460 {input:"><|", tag:"mo", output:"\u22CA", tex:"rtimes", ttype:CONST},
461 {input:"|><|", tag:"mo", output:"\u22C8", tex:"bowtie", ttype:CONST},
462 {input:"-:", tag:"mo", output:"\u00F7", tex:"div", ttype:CONST},
463 {input:"divide", tag:"mo", output:"-:", tex:null, ttype:DEFINITION},
464 {input:"@", tag:"mo", output:"\u2218", tex:"circ", ttype:CONST},
465 {input:"o+", tag:"mo", output:"\u2295", tex:"oplus", ttype:CONST},
466 {input:"ox", tag:"mo", output:"\u2297", tex:"otimes", ttype:CONST},
467 {input:"o.", tag:"mo", output:"\u2299", tex:"odot", ttype:CONST},
468 {input:"sum", tag:"mo", output:"\u2211", tex:null, ttype:UNDEROVER},
469 {input:"prod", tag:"mo", output:"\u220F", tex:null, ttype:UNDEROVER},
470 {input:"^^", tag:"mo", output:"\u2227", tex:"wedge", ttype:CONST},
471 {input:"^^^", tag:"mo", output:"\u22C0", tex:"bigwedge", ttype:UNDEROVER},
472 {input:"vv", tag:"mo", output:"\u2228", tex:"vee", ttype:CONST},
473 {input:"vvv", tag:"mo", output:"\u22C1", tex:"bigvee", ttype:UNDEROVER},
474 {input:"nn", tag:"mo", output:"\u2229", tex:"cap", ttype:CONST},
475 {input:"nnn", tag:"mo", output:"\u22C2", tex:"bigcap", ttype:UNDEROVER},
476 {input:"uu", tag:"mo", output:"\u222A", tex:"cup", ttype:CONST},
477 {input:"uuu", tag:"mo", output:"\u22C3", tex:"bigcup", ttype:UNDEROVER},
478
479 //binary relation symbols
480 {input:"!=", tag:"mo", output:"\u2260", tex:"ne", ttype:CONST},
481 {input:":=", tag:"mo", output:":=", tex:null, ttype:CONST},
482 {input:"lt", tag:"mo", output:"<", tex:null, ttype:CONST},
483 {input:"<=", tag:"mo", output:"\u2264", tex:"le", ttype:CONST},
484 {input:"lt=", tag:"mo", output:"\u2264", tex:"leq", ttype:CONST},
485 {input:"gt", tag:"mo", output:">", tex:null, ttype:CONST},
486 {input:">=", tag:"mo", output:"\u2265", tex:"ge", ttype:CONST},
487 {input:"gt=", tag:"mo", output:"\u2265", tex:"geq", ttype:CONST},
488 {input:"-<", tag:"mo", output:"\u227A", tex:"prec", ttype:CONST},
489 {input:"-lt", tag:"mo", output:"\u227A", tex:null, ttype:CONST},
490 {input:">-", tag:"mo", output:"\u227B", tex:"succ", ttype:CONST},
491 {input:"-<=", tag:"mo", output:"\u2AAF", tex:"preceq", ttype:CONST},
492 {input:">-=", tag:"mo", output:"\u2AB0", tex:"succeq", ttype:CONST},
493 {input:"in", tag:"mo", output:"\u2208", tex:null, ttype:CONST},
494 {input:"!in", tag:"mo", output:"\u2209", tex:"notin", ttype:CONST},
495 {input:"sub", tag:"mo", output:"\u2282", tex:"subset", ttype:CONST},
496 {input:"sup", tag:"mo", output:"\u2283", tex:"supset", ttype:CONST},
497 {input:"sube", tag:"mo", output:"\u2286", tex:"subseteq", ttype:CONST},
498 {input:"supe", tag:"mo", output:"\u2287", tex:"supseteq", ttype:CONST},
499 {input:"-=", tag:"mo", output:"\u2261", tex:"equiv", ttype:CONST},
500 {input:"~=", tag:"mo", output:"\u2245", tex:"cong", ttype:CONST},
501 {input:"~~", tag:"mo", output:"\u2248", tex:"approx", ttype:CONST},
502 {input:"prop", tag:"mo", output:"\u221D", tex:"propto", ttype:CONST},
503
504 //logical symbols
505 {input:"and", tag:"mtext", output:"and", tex:null, ttype:SPACE},
506 {input:"or", tag:"mtext", output:"or", tex:null, ttype:SPACE},
507 {input:"not", tag:"mo", output:"\u00AC", tex:"neg", ttype:CONST},
508 {input:"=>", tag:"mo", output:"\u21D2", tex:"implies", ttype:CONST},
509 {input:"if", tag:"mo", output:"if", tex:null, ttype:SPACE},
510 {input:"<=>", tag:"mo", output:"\u21D4", tex:"iff", ttype:CONST},
511 {input:"AA", tag:"mo", output:"\u2200", tex:"forall", ttype:CONST},
512 {input:"EE", tag:"mo", output:"\u2203", tex:"exists", ttype:CONST},
513 {input:"_|_", tag:"mo", output:"\u22A5", tex:"bot", ttype:CONST},
514 {input:"TT", tag:"mo", output:"\u22A4", tex:"top", ttype:CONST},
515 {input:"|--", tag:"mo", output:"\u22A2", tex:"vdash", ttype:CONST},
516 {input:"|==", tag:"mo", output:"\u22A8", tex:"models", ttype:CONST},
517
518 //grouping brackets
519 {input:"(", tag:"mo", output:"(", tex:"left(", ttype:LEFTBRACKET},
520 {input:")", tag:"mo", output:")", tex:"right)", ttype:RIGHTBRACKET},
521 {input:"[", tag:"mo", output:"[", tex:"left[", ttype:LEFTBRACKET},
522 {input:"]", tag:"mo", output:"]", tex:"right]", ttype:RIGHTBRACKET},
523 {input:"{", tag:"mo", output:"{", tex:null, ttype:LEFTBRACKET},
524 {input:"}", tag:"mo", output:"}", tex:null, ttype:RIGHTBRACKET},
525 {input:"|", tag:"mo", output:"|", tex:null, ttype:LEFTRIGHT},
526 {input:":|:", tag:"mo", output:"|", tex:null, ttype:CONST},
527 {input:"|:", tag:"mo", output:"|", tex:null, ttype:LEFTBRACKET},
528 {input:":|", tag:"mo", output:"|", tex:null, ttype:RIGHTBRACKET},
529 //{input:"||", tag:"mo", output:"||", tex:null, ttype:LEFTRIGHT},
530 {input:"(:", tag:"mo", output:"\u2329", tex:"langle", ttype:LEFTBRACKET},
531 {input:":)", tag:"mo", output:"\u232A", tex:"rangle", ttype:RIGHTBRACKET},
532 {input:"<<", tag:"mo", output:"\u2329", tex:null, ttype:LEFTBRACKET},
533 {input:">>", tag:"mo", output:"\u232A", tex:null, ttype:RIGHTBRACKET},
534 {input:"{:", tag:"mo", output:"{:", tex:null, ttype:LEFTBRACKET, invisible:true},
535 {input:":}", tag:"mo", output:":}", tex:null, ttype:RIGHTBRACKET, invisible:true},
536
537 //miscellaneous symbols
538 {input:"int", tag:"mo", output:"\u222B", tex:null, ttype:CONST},
539 {input:"dx", tag:"mi", output:"{:d x:}", tex:null, ttype:DEFINITION},
540 {input:"dy", tag:"mi", output:"{:d y:}", tex:null, ttype:DEFINITION},
541 {input:"dz", tag:"mi", output:"{:d z:}", tex:null, ttype:DEFINITION},
542 {input:"dt", tag:"mi", output:"{:d t:}", tex:null, ttype:DEFINITION},
543 {input:"oint", tag:"mo", output:"\u222E", tex:null, ttype:CONST},
544 {input:"del", tag:"mo", output:"\u2202", tex:"partial", ttype:CONST},
545 {input:"grad", tag:"mo", output:"\u2207", tex:"nabla", ttype:CONST},
546 {input:"+-", tag:"mo", output:"\u00B1", tex:"pm", ttype:CONST},
547 {input:"O/", tag:"mo", output:"\u2205", tex:"emptyset", ttype:CONST},
548 {input:"oo", tag:"mo", output:"\u221E", tex:"infty", ttype:CONST},
549 {input:"aleph", tag:"mo", output:"\u2135", tex:null, ttype:CONST},
550 {input:"...", tag:"mo", output:"...", tex:"ldots", ttype:CONST},
551 {input:":.", tag:"mo", output:"\u2234", tex:"therefore", ttype:CONST},
552 {input:":'", tag:"mo", output:"\u2235", tex:"because", ttype:CONST},
553 {input:"/_", tag:"mo", output:"\u2220", tex:"angle", ttype:CONST},
554 {input:"/_\\", tag:"mo", output:"\u25B3", tex:"triangle", ttype:CONST},
555 {input:"'", tag:"mo", output:"\u2032", tex:"prime", ttype:CONST},
556 {input:"tilde", tag:"mover", output:"~", tex:null, ttype:UNARY, acc:true},
557 {input:"\\ ", tag:"mo", output:"\u00A0", tex:null, ttype:CONST},
558 {input:"frown", tag:"mo", output:"\u2322", tex:null, ttype:CONST},
559 {input:"quad", tag:"mo", output:"\u00A0\u00A0", tex:null, ttype:CONST},
560 {input:"qquad", tag:"mo", output:"\u00A0\u00A0\u00A0\u00A0", tex:null, ttype:CONST},
561 {input:"cdots", tag:"mo", output:"\u22EF", tex:null, ttype:CONST},
562 {input:"vdots", tag:"mo", output:"\u22EE", tex:null, ttype:CONST},
563 {input:"ddots", tag:"mo", output:"\u22F1", tex:null, ttype:CONST},
564 {input:"diamond", tag:"mo", output:"\u22C4", tex:null, ttype:CONST},
565 {input:"square", tag:"mo", output:"\u25A1", tex:null, ttype:CONST},
566 {input:"|__", tag:"mo", output:"\u230A", tex:"lfloor", ttype:CONST},
567 {input:"__|", tag:"mo", output:"\u230B", tex:"rfloor", ttype:CONST},
568 {input:"|~", tag:"mo", output:"\u2308", tex:"lceiling", ttype:CONST},
569 {input:"~|", tag:"mo", output:"\u2309", tex:"rceiling", ttype:CONST},
570 {input:"CC", tag:"mo", output:"\u2102", tex:null, ttype:CONST},
571 {input:"NN", tag:"mo", output:"\u2115", tex:null, ttype:CONST},
572 {input:"QQ", tag:"mo", output:"\u211A", tex:null, ttype:CONST},
573 {input:"RR", tag:"mo", output:"\u211D", tex:null, ttype:CONST},
574 {input:"ZZ", tag:"mo", output:"\u2124", tex:null, ttype:CONST},
575 {input:"f", tag:"mi", output:"f", tex:null, ttype:UNARY, func:true},
576 {input:"g", tag:"mi", output:"g", tex:null, ttype:UNARY, func:true},
577
578 //standard functions
579 {input:"lim", tag:"mo", output:"lim", tex:null, ttype:UNDEROVER},
580 {input:"Lim", tag:"mo", output:"Lim", tex:null, ttype:UNDEROVER},
581 {input:"sin", tag:"mo", output:"sin", tex:null, ttype:UNARY, func:true},
582 {input:"cos", tag:"mo", output:"cos", tex:null, ttype:UNARY, func:true},
583 {input:"tan", tag:"mo", output:"tan", tex:null, ttype:UNARY, func:true},
584 {input:"sinh", tag:"mo", output:"sinh", tex:null, ttype:UNARY, func:true},
585 {input:"cosh", tag:"mo", output:"cosh", tex:null, ttype:UNARY, func:true},
586 {input:"tanh", tag:"mo", output:"tanh", tex:null, ttype:UNARY, func:true},
587 {input:"cot", tag:"mo", output:"cot", tex:null, ttype:UNARY, func:true},
588 {input:"sec", tag:"mo", output:"sec", tex:null, ttype:UNARY, func:true},
589 {input:"csc", tag:"mo", output:"csc", tex:null, ttype:UNARY, func:true},
590 {input:"arcsin", tag:"mo", output:"arcsin", tex:null, ttype:UNARY, func:true},
591 {input:"arccos", tag:"mo", output:"arccos", tex:null, ttype:UNARY, func:true},
592 {input:"arctan", tag:"mo", output:"arctan", tex:null, ttype:UNARY, func:true},
593 {input:"coth", tag:"mo", output:"coth", tex:null, ttype:UNARY, func:true},
594 {input:"sech", tag:"mo", output:"sech", tex:null, ttype:UNARY, func:true},
595 {input:"csch", tag:"mo", output:"csch", tex:null, ttype:UNARY, func:true},
596 {input:"exp", tag:"mo", output:"exp", tex:null, ttype:UNARY, func:true},
597 {input:"abs", tag:"mo", output:"abs", tex:null, ttype:UNARY, rewriteleftright:["|","|"]},
598 {input:"norm", tag:"mo", output:"norm", tex:null, ttype:UNARY, rewriteleftright:["\u2225","\u2225"]},
599 {input:"floor", tag:"mo", output:"floor", tex:null, ttype:UNARY, rewriteleftright:["\u230A","\u230B"]},
600 {input:"ceil", tag:"mo", output:"ceil", tex:null, ttype:UNARY, rewriteleftright:["\u2308","\u2309"]},
601 {input:"log", tag:"mo", output:"log", tex:null, ttype:UNARY, func:true},
602 {input:"ln", tag:"mo", output:"ln", tex:null, ttype:UNARY, func:true},
603 {input:"det", tag:"mo", output:"det", tex:null, ttype:UNARY, func:true},
604 {input:"dim", tag:"mo", output:"dim", tex:null, ttype:CONST},
605 {input:"mod", tag:"mo", output:"mod", tex:null, ttype:CONST},
606 {input:"gcd", tag:"mo", output:"gcd", tex:null, ttype:UNARY, func:true},
607 {input:"lcm", tag:"mo", output:"lcm", tex:null, ttype:UNARY, func:true},
608 {input:"lub", tag:"mo", output:"lub", tex:null, ttype:CONST},
609 {input:"glb", tag:"mo", output:"glb", tex:null, ttype:CONST},
610 {input:"min", tag:"mo", output:"min", tex:null, ttype:UNDEROVER},
611 {input:"max", tag:"mo", output:"max", tex:null, ttype:UNDEROVER},
612 {input:"Sin", tag:"mo", output:"Sin", tex:null, ttype:UNARY, func:true},
613 {input:"Cos", tag:"mo", output:"Cos", tex:null, ttype:UNARY, func:true},
614 {input:"Tan", tag:"mo", output:"Tan", tex:null, ttype:UNARY, func:true},
615 {input:"Arcsin", tag:"mo", output:"Arcsin", tex:null, ttype:UNARY, func:true},
616 {input:"Arccos", tag:"mo", output:"Arccos", tex:null, ttype:UNARY, func:true},
617 {input:"Arctan", tag:"mo", output:"Arctan", tex:null, ttype:UNARY, func:true},
618 {input:"Sinh", tag:"mo", output:"Sinh", tex:null, ttype:UNARY, func:true},
619 {input:"Cosh", tag:"mo", output:"Cosh", tex:null, ttype:UNARY, func:true},
620 {input:"Tanh", tag:"mo", output:"Tanh", tex:null, ttype:UNARY, func:true},
621 {input:"Cot", tag:"mo", output:"Cot", tex:null, ttype:UNARY, func:true},
622 {input:"Sec", tag:"mo", output:"Sec", tex:null, ttype:UNARY, func:true},
623 {input:"Csc", tag:"mo", output:"Csc", tex:null, ttype:UNARY, func:true},
624 {input:"Log", tag:"mo", output:"Log", tex:null, ttype:UNARY, func:true},
625 {input:"Ln", tag:"mo", output:"Ln", tex:null, ttype:UNARY, func:true},
626 {input:"Abs", tag:"mo", output:"abs", tex:null, ttype:UNARY, notexcopy:true, rewriteleftright:["|","|"]},
627
628 //arrows
629 {input:"uarr", tag:"mo", output:"\u2191", tex:"uparrow", ttype:CONST},
630 {input:"darr", tag:"mo", output:"\u2193", tex:"downarrow", ttype:CONST},
631 {input:"rarr", tag:"mo", output:"\u2192", tex:"rightarrow", ttype:CONST},
632 {input:"->", tag:"mo", output:"\u2192", tex:"to", ttype:CONST},
633 {input:">->", tag:"mo", output:"\u21A3", tex:"rightarrowtail", ttype:CONST},
634 {input:"->>", tag:"mo", output:"\u21A0", tex:"twoheadrightarrow", ttype:CONST},
635 {input:">->>", tag:"mo", output:"\u2916", tex:"twoheadrightarrowtail", ttype:CONST},
636 {input:"|->", tag:"mo", output:"\u21A6", tex:"mapsto", ttype:CONST},
637 {input:"larr", tag:"mo", output:"\u2190", tex:"leftarrow", ttype:CONST},
638 {input:"harr", tag:"mo", output:"\u2194", tex:"leftrightarrow", ttype:CONST},
639 {input:"rArr", tag:"mo", output:"\u21D2", tex:"Rightarrow", ttype:CONST},
640 {input:"lArr", tag:"mo", output:"\u21D0", tex:"Leftarrow", ttype:CONST},
641 {input:"hArr", tag:"mo", output:"\u21D4", tex:"Leftrightarrow", ttype:CONST},
642 //commands with argument
643 {input:"sqrt", tag:"msqrt", output:"sqrt", tex:null, ttype:UNARY},
644 {input:"root", tag:"mroot", output:"root", tex:null, ttype:BINARY},
645 {input:"frac", tag:"mfrac", output:"/", tex:null, ttype:BINARY},
646 {input:"/", tag:"mfrac", output:"/", tex:null, ttype:INFIX},
647 {input:"stackrel", tag:"mover", output:"stackrel", tex:null, ttype:BINARY},
648 {input:"overset", tag:"mover", output:"stackrel", tex:null, ttype:BINARY},
649 {input:"underset", tag:"munder", output:"stackrel", tex:null, ttype:BINARY},
650 {input:"_", tag:"msub", output:"_", tex:null, ttype:INFIX},
651 {input:"^", tag:"msup", output:"^", tex:null, ttype:INFIX},
652 {input:"hat", tag:"mover", output:"\u005E", tex:null, ttype:UNARY, acc:true},
653 {input:"bar", tag:"mover", output:"\u00AF", tex:"overline", ttype:UNARY, acc:true},
654 {input:"vec", tag:"mover", output:"\u2192", tex:null, ttype:UNARY, acc:true},
655 {input:"dot", tag:"mover", output:".", tex:null, ttype:UNARY, acc:true},
656 {input:"ddot", tag:"mover", output:"..", tex:null, ttype:UNARY, acc:true},
657 {input:"overarc", tag:"mover", output:"\u23DC", tex:"overparen", ttype:UNARY, acc:true},
658 {input:"ul", tag:"munder", output:"\u0332", tex:"underline", ttype:UNARY, acc:true},
659 {input:"ubrace", tag:"munder", output:"\u23DF", tex:"underbrace", ttype:UNARYUNDEROVER, acc:true},
660 {input:"obrace", tag:"mover", output:"\u23DE", tex:"overbrace", ttype:UNARYUNDEROVER, acc:true},
661 {input:"text", tag:"mtext", output:"text", tex:null, ttype:TEXT},
662 {input:"mbox", tag:"mtext", output:"mbox", tex:null, ttype:TEXT},
663 {input:"color", tag:"mstyle", ttype:BINARY},
664 {input:"id", tag:"mrow", ttype:BINARY},
665 {input:"class", tag:"mrow", ttype:BINARY},
666 {input:"cancel", tag:"menclose", output:"cancel", tex:null, ttype:UNARY},
667 AMquote,
668 {input:"bb", tag:"mstyle", atname:"mathvariant", atval:"bold", output:"bb", tex:null, ttype:UNARY},
669 {input:"mathbf", tag:"mstyle", atname:"mathvariant", atval:"bold", output:"mathbf", tex:null, ttype:UNARY},
670 {input:"sf", tag:"mstyle", atname:"mathvariant", atval:"sans-serif", output:"sf", tex:null, ttype:UNARY},
671 {input:"mathsf", tag:"mstyle", atname:"mathvariant", atval:"sans-serif", output:"mathsf", tex:null, ttype:UNARY},
672 {input:"bbb", tag:"mstyle", atname:"mathvariant", atval:"double-struck", output:"bbb", tex:null, ttype:UNARY, codes:AMbbb},
673 {input:"mathbb", tag:"mstyle", atname:"mathvariant", atval:"double-struck", output:"mathbb", tex:null, ttype:UNARY, codes:AMbbb},
674 {input:"cc", tag:"mstyle", atname:"mathvariant", atval:"script", output:"cc", tex:null, ttype:UNARY, codes:AMcal},
675 {input:"mathcal", tag:"mstyle", atname:"mathvariant", atval:"script", output:"mathcal", tex:null, ttype:UNARY, codes:AMcal},
676 {input:"tt", tag:"mstyle", atname:"mathvariant", atval:"monospace", output:"tt", tex:null, ttype:UNARY},
677 {input:"mathtt", tag:"mstyle", atname:"mathvariant", atval:"monospace", output:"mathtt", tex:null, ttype:UNARY},
678 {input:"fr", tag:"mstyle", atname:"mathvariant", atval:"fraktur", output:"fr", tex:null, ttype:UNARY, codes:AMfrk},
679 {input:"mathfrak", tag:"mstyle", atname:"mathvariant", atval:"fraktur", output:"mathfrak", tex:null, ttype:UNARY, codes:AMfrk}
680 ];
681
682 function compareNames(s1,s2) {
683 if (s1.input > s2.input) return 1
684 else return -1;
685 }
686
687 var AMnames = []; //list of input symbols
688
689 function initSymbols() {
690 var i;
691 var symlen = AMsymbols.length;
692 for (i=0; i<symlen; i++) {
693 if (AMsymbols[i].tex) {
694 AMsymbols.push({input:AMsymbols[i].tex,
695 tag:AMsymbols[i].tag, output:AMsymbols[i].output, ttype:AMsymbols[i].ttype,
696 acc:(AMsymbols[i].acc||false)});
697 }
698 }
699 refreshSymbols();
700 }
701
702 function refreshSymbols(){
703 var i;
704 AMsymbols.sort(compareNames);
705 for (i=0; i<AMsymbols.length; i++) AMnames[i] = AMsymbols[i].input;
706 }
707
708 function define(oldstr,newstr) {
709 AMsymbols.push({input:oldstr, tag:"mo", output:newstr, tex:null, ttype:DEFINITION});
710 refreshSymbols(); // this may be a problem if many symbols are defined!
711 }
712
713 function AMremoveCharsAndBlanks(str,n) {
714 //remove n characters and any following blanks
715 var st;
716 if (str.charAt(n)=="\\" && str.charAt(n+1)!="\\" && str.charAt(n+1)!=" ")
717 st = str.slice(n+1);
718 else st = str.slice(n);
719 for (var i=0; i<st.length && st.charCodeAt(i)<=32; i=i+1);
720 return st.slice(i);
721 }
722
723 function position(arr, str, n) {
724 // return position >=n where str appears or would be inserted
725 // assumes arr is sorted
726 if (n==0) {
727 var h,m;
728 n = -1;
729 h = arr.length;
730 while (n+1<h) {
731 m = (n+h) >> 1;
732 if (arr[m]<str) n = m; else h = m;
733 }
734 return h;
735 } else
736 for (var i=n; i<arr.length && arr[i]<str; i++);
737 return i; // i=arr.length || arr[i]>=str
738 }
739
740 function AMgetSymbol(str) {
741 //return maximal initial substring of str that appears in names
742 //return null if there is none
743 var k = 0; //new pos
744 var j = 0; //old pos
745 var mk; //match pos
746 var st;
747 var tagst;
748 var match = "";
749 var more = true;
750 for (var i=1; i<=str.length && more; i++) {
751 st = str.slice(0,i); //initial substring of length i
752 j = k;
753 k = position(AMnames, st, j);
754 if (k<AMnames.length && str.slice(0,AMnames[k].length)==AMnames[k]){
755 match = AMnames[k];
756 mk = k;
757 i = match.length;
758 }
759 more = k<AMnames.length && str.slice(0,AMnames[k].length)>=AMnames[k];
760 }
761 AMpreviousSymbol=AMcurrentSymbol;
762 if (match!=""){
763 AMcurrentSymbol=AMsymbols[mk].ttype;
764 return AMsymbols[mk];
765 }
766 // if str[0] is a digit or - return maxsubstring of digits.digits
767 AMcurrentSymbol=CONST;
768 k = 1;
769 st = str.slice(0,1);
770 var integ = true;
771 while ("0"<=st && st<="9" && k<=str.length) {
772 st = str.slice(k,k+1);
773 k++;
774 }
775 if (st == decimalsign) {
776 st = str.slice(k,k+1);
777 if ("0"<=st && st<="9") {
778 integ = false;
779 k++;
780 while ("0"<=st && st<="9" && k<=str.length) {
781 st = str.slice(k,k+1);
782 k++;
783 }
784 }
785 }
786 if ((integ && k>1) || k>2) {
787 st = str.slice(0,k-1);
788 tagst = "mn";
789 } else {
790 k = 2;
791 st = str.slice(0,1); //take 1 character
792 tagst = (("A">st || st>"Z") && ("a">st || st>"z")?"mo":"mi");
793 }
794 if (st=="-" && AMpreviousSymbol==INFIX) {
795 AMcurrentSymbol = INFIX; //trick "/" into recognizing "-" on second parse
796 return {input:st, tag:tagst, output:st, ttype:UNARY, func:true};
797 }
798 return {input:st, tag:tagst, output:st, ttype:CONST};
799 }
800
801 function AMremoveBrackets(node) {
802 var st;
803 if (!node.hasChildNodes()) { return; }
804 if (node.firstChild.hasChildNodes() && (node.nodeName=="mrow" || node.nodeName=="M:MROW")) {
805 st = node.firstChild.firstChild.nodeValue;
806 if (st=="(" || st=="[" || st=="{") node.removeChild(node.firstChild);
807 }
808 if (node.lastChild.hasChildNodes() && (node.nodeName=="mrow" || node.nodeName=="M:MROW")) {
809 st = node.lastChild.firstChild.nodeValue;
810 if (st==")" || st=="]" || st=="}") node.removeChild(node.lastChild);
811 }
812 }
813
814 /*Parsing ASCII math expressions with the following grammar
815 v ::= [A-Za-z] | greek letters | numbers | other constant symbols
816 u ::= sqrt | text | bb | other unary symbols for font commands
817 b ::= frac | root | stackrel binary symbols
818 l ::= ( | [ | { | (: | {: left brackets
819 r ::= ) | ] | } | :) | :} right brackets
820 S ::= v | lEr | uS | bSS Simple expression
821 I ::= S_S | S^S | S_S^S | S Intermediate expression
822 E ::= IE | I/I Expression
823 Each terminal symbol is translated into a corresponding mathml node.*/
824
825 var AMnestingDepth,AMpreviousSymbol,AMcurrentSymbol;
826
827 function AMparseSexpr(str) { //parses str and returns [node,tailstr]
828 var symbol, node, result, i, st,// rightvert = false,
829 newFrag = document.createDocumentFragment();
830 str = AMremoveCharsAndBlanks(str,0);
831 symbol = AMgetSymbol(str); //either a token or a bracket or empty
832 if (symbol == null || symbol.ttype == RIGHTBRACKET && AMnestingDepth > 0) {
833 return [null,str];
834 }
835 if (symbol.ttype == DEFINITION) {
836 str = symbol.output+AMremoveCharsAndBlanks(str,symbol.input.length);
837 symbol = AMgetSymbol(str);
838 }
839 switch (symbol.ttype) { case UNDEROVER:
840 case CONST:
841 str = AMremoveCharsAndBlanks(str,symbol.input.length);
842 return [createMmlNode(symbol.tag, //its a constant
843 document.createTextNode(symbol.output)),str];
844 case LEFTBRACKET: //read (expr+)
845 AMnestingDepth++;
846 str = AMremoveCharsAndBlanks(str,symbol.input.length);
847 result = AMparseExpr(str,true);
848 AMnestingDepth--;
849 if (typeof symbol.invisible == "boolean" && symbol.invisible)
850 node = createMmlNode("mrow",result[0]);
851 else {
852 node = createMmlNode("mo",document.createTextNode(symbol.output));
853 node = createMmlNode("mrow",node);
854 node.appendChild(result[0]);
855 }
856 return [node,result[1]];
857 case TEXT:
858 if (symbol!=AMquote) str = AMremoveCharsAndBlanks(str,symbol.input.length);
859 if (str.charAt(0)=="{") i=str.indexOf("}");
860 else if (str.charAt(0)=="(") i=str.indexOf(")");
861 else if (str.charAt(0)=="[") i=str.indexOf("]");
862 else if (symbol==AMquote) i=str.slice(1).indexOf("\"")+1;
863 else i = 0;
864 if (i==-1) i = str.length;
865 st = str.slice(1,i);
866 if (st.charAt(0) == " ") {
867 node = createMmlNode("mspace");
868 node.setAttribute("width","1ex");
869 newFrag.appendChild(node);
870 }
871 newFrag.appendChild(
872 createMmlNode(symbol.tag,document.createTextNode(st)));
873 if (st.charAt(st.length-1) == " ") {
874 node = createMmlNode("mspace");
875 node.setAttribute("width","1ex");
876 newFrag.appendChild(node);
877 }
878 str = AMremoveCharsAndBlanks(str,i+1);
879 return [createMmlNode("mrow",newFrag),str];
880 case UNARYUNDEROVER:
881 case UNARY:
882 str = AMremoveCharsAndBlanks(str,symbol.input.length);
883 result = AMparseSexpr(str);
884 if (result[0]==null) return [createMmlNode(symbol.tag,
885 document.createTextNode(symbol.output)),str];
886 if (typeof symbol.func == "boolean" && symbol.func) { // functions hack
887 st = str.charAt(0);
888 if (st=="^" || st=="_" || st=="/" || st=="|" || st=="," ||
889 (symbol.input.length==1 && symbol.input.match(/\w/) && st!="(")) {
890 return [createMmlNode(symbol.tag,
891 document.createTextNode(symbol.output)),str];
892 } else {
893 node = createMmlNode("mrow",
894 createMmlNode(symbol.tag,document.createTextNode(symbol.output)));
895 node.appendChild(result[0]);
896 return [node,result[1]];
897 }
898 }
899 AMremoveBrackets(result[0]);
900 if (symbol.input == "sqrt") { // sqrt
901 return [createMmlNode(symbol.tag,result[0]),result[1]];
902 } else if (typeof symbol.rewriteleftright != "undefined") { // abs, floor, ceil
903 node = createMmlNode("mrow", createMmlNode("mo",document.createTextNode(symbol.rewriteleftright[0])));
904 node.appendChild(result[0]);
905 node.appendChild(createMmlNode("mo",document.createTextNode(symbol.rewriteleftright[1])));
906 return [node,result[1]];
907 } else if (symbol.input == "cancel") { // cancel
908 node = createMmlNode(symbol.tag,result[0]);
909 node.setAttribute("notation","updiagonalstrike");
910 return [node,result[1]];
911 } else if (typeof symbol.acc == "boolean" && symbol.acc) { // accent
912 node = createMmlNode(symbol.tag,result[0]);
913 var accnode = createMmlNode("mo",document.createTextNode(symbol.output));
914 if (symbol.input=="vec" && (
915 (result[0].nodeName=="mrow" && result[0].childNodes.length==1
916 && result[0].firstChild.firstChild.nodeValue !== null
917 && result[0].firstChild.firstChild.nodeValue.length==1) ||
918 (result[0].firstChild.nodeValue !== null
919 && result[0].firstChild.nodeValue.length==1) )) {
920 accnode.setAttribute("stretchy",false);
921 }
922 node.appendChild(accnode);
923 return [node,result[1]];
924 } else { // font change command
925 if (!isIE && typeof symbol.codes != "undefined") {
926 for (i=0; i<result[0].childNodes.length; i++)
927 if (result[0].childNodes[i].nodeName=="mi" || result[0].nodeName=="mi") {
928 st = (result[0].nodeName=="mi"?result[0].firstChild.nodeValue:
929 result[0].childNodes[i].firstChild.nodeValue);
930 var newst = [];
931 for (var j=0; j<st.length; j++)
932 if (st.charCodeAt(j)>64 && st.charCodeAt(j)<91)
933 newst = newst + symbol.codes[st.charCodeAt(j)-65];
934 else if (st.charCodeAt(j)>96 && st.charCodeAt(j)<123)
935 newst = newst + symbol.codes[st.charCodeAt(j)-71];
936 else newst = newst + st.charAt(j);
937 if (result[0].nodeName=="mi")
938 result[0]=createMmlNode("mo").
939 appendChild(document.createTextNode(newst));
940 else result[0].replaceChild(createMmlNode("mo").
941 appendChild(document.createTextNode(newst)),
942 result[0].childNodes[i]);
943 }
944 }
945 node = createMmlNode(symbol.tag,result[0]);
946 node.setAttribute(symbol.atname,symbol.atval);
947 return [node,result[1]];
948 }
949 case BINARY:
950 str = AMremoveCharsAndBlanks(str,symbol.input.length);
951 result = AMparseSexpr(str);
952 if (result[0]==null) return [createMmlNode("mo",
953 document.createTextNode(symbol.input)),str];
954 AMremoveBrackets(result[0]);
955 var result2 = AMparseSexpr(result[1]);
956 if (result2[0]==null) return [createMmlNode("mo",
957 document.createTextNode(symbol.input)),str];
958 AMremoveBrackets(result2[0]);
959 if (['color', 'class', 'id'].indexOf(symbol.input) >= 0) {
960
961 // Get the second argument
962 if (str.charAt(0)=="{") i=str.indexOf("}");
963 else if (str.charAt(0)=="(") i=str.indexOf(")");
964 else if (str.charAt(0)=="[") i=str.indexOf("]");
965 st = str.slice(1,i);
966
967 // Make a mathml node
968 node = createMmlNode(symbol.tag,result2[0]);
969
970 // Set the correct attribute
971 if (symbol.input === "color") node.setAttribute("mathcolor", st)
972 else if (symbol.input === "class") node.setAttribute("class", st)
973 else if (symbol.input === "id") node.setAttribute("id", st)
974 return [node,result2[1]];
975 }
976 if (symbol.input=="root" || symbol.output=="stackrel")
977 newFrag.appendChild(result2[0]);
978 newFrag.appendChild(result[0]);
979 if (symbol.input=="frac") newFrag.appendChild(result2[0]);
980 return [createMmlNode(symbol.tag,newFrag),result2[1]];
981 case INFIX:
982 str = AMremoveCharsAndBlanks(str,symbol.input.length);
983 return [createMmlNode("mo",document.createTextNode(symbol.output)),str];
984 case SPACE:
985 str = AMremoveCharsAndBlanks(str,symbol.input.length);
986 node = createMmlNode("mspace");
987 node.setAttribute("width","1ex");
988 newFrag.appendChild(node);
989 newFrag.appendChild(
990 createMmlNode(symbol.tag,document.createTextNode(symbol.output)));
991 node = createMmlNode("mspace");
992 node.setAttribute("width","1ex");
993 newFrag.appendChild(node);
994 return [createMmlNode("mrow",newFrag),str];
995 case LEFTRIGHT:
996 // if (rightvert) return [null,str]; else rightvert = true;
997 AMnestingDepth++;
998 str = AMremoveCharsAndBlanks(str,symbol.input.length);
999 result = AMparseExpr(str,false);
1000 AMnestingDepth--;
1001 st = "";
1002 if (result[0].lastChild!=null)
1003 st = result[0].lastChild.firstChild.nodeValue;
1004 if (st == "|" && str.charAt(0)!==",") { // its an absolute value subterm
1005 node = createMmlNode("mo",document.createTextNode(symbol.output));
1006 node = createMmlNode("mrow",node);
1007 node.appendChild(result[0]);
1008 return [node,result[1]];
1009 } else { // the "|" is a \mid so use unicode 2223 (divides) for spacing
1010 node = createMmlNode("mo",document.createTextNode("\u2223"));
1011 node = createMmlNode("mrow",node);
1012 return [node,str];
1013 }
1014 default:
1015 //alert("default");
1016 str = AMremoveCharsAndBlanks(str,symbol.input.length);
1017 return [createMmlNode(symbol.tag, //its a constant
1018 document.createTextNode(symbol.output)),str];
1019 }
1020 }
1021
1022 function AMparseIexpr(str) {
1023 var symbol, sym1, sym2, node, result, underover;
1024 str = AMremoveCharsAndBlanks(str,0);
1025 sym1 = AMgetSymbol(str);
1026 result = AMparseSexpr(str);
1027 node = result[0];
1028 str = result[1];
1029 symbol = AMgetSymbol(str);
1030 if (symbol.ttype == INFIX && symbol.input != "/") {
1031 str = AMremoveCharsAndBlanks(str,symbol.input.length);
1032 // if (symbol.input == "/") result = AMparseIexpr(str); else ...
1033 result = AMparseSexpr(str);
1034 if (result[0] == null) // show box in place of missing argument
1035 result[0] = createMmlNode("mo",document.createTextNode("\u25A1"));
1036 else AMremoveBrackets(result[0]);
1037 str = result[1];
1038 // if (symbol.input == "/") AMremoveBrackets(node);
1039 underover = (sym1.ttype == UNDEROVER || sym1.ttype == UNARYUNDEROVER);
1040 if (symbol.input == "_") {
1041 sym2 = AMgetSymbol(str);
1042 if (sym2.input == "^") {
1043 str = AMremoveCharsAndBlanks(str,sym2.input.length);
1044 var res2 = AMparseSexpr(str);
1045 AMremoveBrackets(res2[0]);
1046 str = res2[1];
1047 node = createMmlNode((underover?"munderover":"msubsup"),node);
1048 node.appendChild(result[0]);
1049 node.appendChild(res2[0]);
1050 node = createMmlNode("mrow",node); // so sum does not stretch
1051 } else {
1052 node = createMmlNode((underover?"munder":"msub"),node);
1053 node.appendChild(result[0]);
1054 }
1055 } else if (symbol.input == "^" && underover) {
1056 node = createMmlNode("mover",node);
1057 node.appendChild(result[0]);
1058 } else {
1059 node = createMmlNode(symbol.tag,node);
1060 node.appendChild(result[0]);
1061 }
1062 if (typeof sym1.func != 'undefined' && sym1.func) {
1063 sym2 = AMgetSymbol(str);
1064 if (sym2.ttype != INFIX && sym2.ttype != RIGHTBRACKET &&
1065 (sym1.input.length>1 || sym2.ttype == LEFTBRACKET)) {
1066 result = AMparseIexpr(str);
1067 node = createMmlNode("mrow",node);
1068 node.appendChild(result[0]);
1069 str = result[1];
1070 }
1071 }
1072 }
1073 return [node,str];
1074 }
1075
1076 function AMparseExpr(str,rightbracket) {
1077 var symbol, node, result, i,
1078 newFrag = document.createDocumentFragment();
1079 do {
1080 str = AMremoveCharsAndBlanks(str,0);
1081 result = AMparseIexpr(str);
1082 node = result[0];
1083 str = result[1];
1084 symbol = AMgetSymbol(str);
1085 if (symbol.ttype == INFIX && symbol.input == "/") {
1086 str = AMremoveCharsAndBlanks(str,symbol.input.length);
1087 result = AMparseIexpr(str);
1088 if (result[0] == null) // show box in place of missing argument
1089 result[0] = createMmlNode("mo",document.createTextNode("\u25A1"));
1090 else AMremoveBrackets(result[0]);
1091 str = result[1];
1092 AMremoveBrackets(node);
1093 node = createMmlNode(symbol.tag,node);
1094 node.appendChild(result[0]);
1095 newFrag.appendChild(node);
1096 symbol = AMgetSymbol(str);
1097 }
1098 else if (node!=undefined) newFrag.appendChild(node);
1099 } while ((symbol.ttype != RIGHTBRACKET &&
1100 (symbol.ttype != LEFTRIGHT || rightbracket)
1101 || AMnestingDepth == 0) && symbol!=null && symbol.output!="");
1102 if (symbol.ttype == RIGHTBRACKET || symbol.ttype == LEFTRIGHT) {
1103 // if (AMnestingDepth > 0) AMnestingDepth--;
1104 var len = newFrag.childNodes.length;
1105 if (len>0 && newFrag.childNodes[len-1].nodeName == "mrow"
1106 && newFrag.childNodes[len-1].lastChild
1107 && newFrag.childNodes[len-1].lastChild.firstChild ) { //matrix
1108 //removed to allow row vectors: //&& len>1 &&
1109 //newFrag.childNodes[len-2].nodeName == "mo" &&
1110 //newFrag.childNodes[len-2].firstChild.nodeValue == ","
1111 var right = newFrag.childNodes[len-1].lastChild.firstChild.nodeValue;
1112 if (right==")" || right=="]") {
1113 var left = newFrag.childNodes[len-1].firstChild.firstChild.nodeValue;
1114 if (left=="(" && right==")" && symbol.output != "}" ||
1115 left=="[" && right=="]") {
1116 var pos = []; // positions of commas
1117 var matrix = true;
1118 var m = newFrag.childNodes.length;
1119 for (i=0; matrix && i<m; i=i+2) {
1120 pos[i] = [];
1121 node = newFrag.childNodes[i];
1122 if (matrix) matrix = node.nodeName=="mrow" &&
1123 (i==m-1 || node.nextSibling.nodeName=="mo" &&
1124 node.nextSibling.firstChild.nodeValue==",")&&
1125 node.firstChild.firstChild.nodeValue==left &&
1126 node.lastChild.firstChild.nodeValue==right;
1127 if (matrix)
1128 for (var j=0; j<node.childNodes.length; j++)
1129 if (node.childNodes[j].firstChild.nodeValue==",")
1130 pos[i][pos[i].length]=j;
1131 if (matrix && i>1) matrix = pos[i].length == pos[i-2].length;
1132 }
1133 matrix = matrix && (pos.length>1 || pos[0].length>0);
1134 var columnlines = [];
1135 if (matrix) {
1136 var row, frag, n, k, table = document.createDocumentFragment();
1137 for (i=0; i<m; i=i+2) {
1138 row = document.createDocumentFragment();
1139 frag = document.createDocumentFragment();
1140 node = newFrag.firstChild; // <mrow>(-,-,...,-,-)</mrow>
1141 n = node.childNodes.length;
1142 k = 0;
1143 node.removeChild(node.firstChild); //remove (
1144 for (j=1; j<n-1; j++) {
1145 if (typeof pos[i][k] != "undefined" && j==pos[i][k]){
1146 node.removeChild(node.firstChild); //remove ,
1147 if (node.firstChild.nodeName=="mrow" && node.firstChild.childNodes.length==1 &&
1148 node.firstChild.firstChild.firstChild.nodeValue=="\u2223") {
1149 //is columnline marker - skip it
1150 if (i==0) { columnlines.push("solid"); }
1151 node.removeChild(node.firstChild); //remove mrow
1152 node.removeChild(node.firstChild); //remove ,
1153 j+=2;
1154 k++;
1155 } else if (i==0) { columnlines.push("none"); }
1156 row.appendChild(createMmlNode("mtd",frag));
1157 k++;
1158 } else frag.appendChild(node.firstChild);
1159 }
1160 row.appendChild(createMmlNode("mtd",frag));
1161 if (i==0) { columnlines.push("none"); }
1162 if (newFrag.childNodes.length>2) {
1163 newFrag.removeChild(newFrag.firstChild); //remove <mrow>)</mrow>
1164 newFrag.removeChild(newFrag.firstChild); //remove <mo>,</mo>
1165 }
1166 table.appendChild(createMmlNode("mtr",row));
1167 }
1168 node = createMmlNode("mtable",table);
1169 node.setAttribute("columnlines", columnlines.join(" "));
1170 if (typeof symbol.invisible == "boolean" && symbol.invisible) node.setAttribute("columnalign","left");
1171 newFrag.replaceChild(node,newFrag.firstChild);
1172 }
1173 }
1174 }
1175 }
1176 str = AMremoveCharsAndBlanks(str,symbol.input.length);
1177 if (typeof symbol.invisible != "boolean" || !symbol.invisible) {
1178 node = createMmlNode("mo",document.createTextNode(symbol.output));
1179 newFrag.appendChild(node);
1180 }
1181 }
1182 return [newFrag,str];
1183 }
1184
1185 function parseMath(str,latex) {
1186 var frag, node;
1187 AMnestingDepth = 0;
1188 //some basic cleanup for dealing with stuff editors like TinyMCE adds
1189 str = str.replace(/&nbsp;/g,"");
1190 str = str.replace(/&gt;/g,">");
1191 str = str.replace(/&lt;/g,"<");
1192 frag = AMparseExpr(str.replace(/^\s+/g,""),false)[0];
1193 node = createMmlNode("mstyle",frag);
1194 if (mathcolor != "") node.setAttribute("mathcolor",mathcolor);
1195 if (mathfontsize != "") {
1196 node.setAttribute("fontsize", mathfontsize);
1197 node.setAttribute("mathsize", mathfontsize);
1198 }
1199 if (mathfontfamily != "") {
1200 node.setAttribute("fontfamily", mathfontfamily);
1201 node.setAttribute("mathvariant", mathfontfamily);
1202 }
1203
1204 if (displaystyle) node.setAttribute("displaystyle","true");
1205 node = createMmlNode("math",node);
1206 if (showasciiformulaonhover) //fixed by djhsu so newline
1207 node.setAttribute("title",str.replace(/\s+/g," "));//does not show in Gecko
1208 return node;
1209 }
1210
1211 /*
1212 function strarr2docFrag(arr, linebreaks, latex) {
1213 var newFrag=document.createDocumentFragment();
1214 var expr = false;
1215 for (var i=0; i<arr.length; i++) {
1216 if (expr) newFrag.appendChild(parseMath(arr[i],latex));
1217 else {
1218 var arri = (linebreaks ? arr[i].split("\n\n") : [arr[i]]);
1219 newFrag.appendChild(createElementXHTML("span").
1220 appendChild(document.createTextNode(arri[0])));
1221 for (var j=1; j<arri.length; j++) {
1222 newFrag.appendChild(createElementXHTML("p"));
1223 newFrag.appendChild(createElementXHTML("span").
1224 appendChild(document.createTextNode(arri[j])));
1225 }
1226 }
1227 expr = !expr;
1228 }
1229 return newFrag;
1230 }
1231
1232 function AMautomathrec(str) {
1233 //formula is a space (or start of str) followed by a maximal sequence of *two* or more tokens, possibly separated by runs of digits and/or space.
1234 //tokens are single letters (except a, A, I) and ASCIIMathML tokens
1235 var texcommand = "\\\\[a-zA-Z]+|\\\\\\s|";
1236 var ambigAMtoken = "\\b(?:oo|lim|ln|int|oint|del|grad|aleph|prod|prop|sinh|cosh|tanh|cos|sec|pi|tt|fr|sf|sube|supe|sub|sup|det|mod|gcd|lcm|min|max|vec|ddot|ul|chi|eta|nu|mu)(?![a-z])|";
1237 var englishAMtoken = "\\b(?:sum|ox|log|sin|tan|dim|hat|bar|dot)(?![a-z])|";
1238 var secondenglishAMtoken = "|\\bI\\b|\\bin\\b|\\btext\\b"; // took if and or not out
1239 var simpleAMtoken = "NN|ZZ|QQ|RR|CC|TT|AA|EE|sqrt|dx|dy|dz|dt|xx|vv|uu|nn|bb|cc|csc|cot|alpha|beta|delta|Delta|epsilon|gamma|Gamma|kappa|lambda|Lambda|omega|phi|Phi|Pi|psi|Psi|rho|sigma|Sigma|tau|theta|Theta|xi|Xi|zeta"; // uuu nnn?
1240 var letter = "[a-zA-HJ-Z](?=(?:[^a-zA-Z]|$|"+ambigAMtoken+englishAMtoken+simpleAMtoken+"))|";
1241 var token = letter+texcommand+"\\d+|[-()[\\]{}+=*&^_%\\\@/<>,\\|!:;'~]|\\.(?!(?:\x20|$))|"+ambigAMtoken+englishAMtoken+simpleAMtoken;
1242 var re = new RegExp("(^|\\s)((("+token+")\\s?)(("+token+secondenglishAMtoken+")\\s?)+)([,.?]?(?=\\s|$))","g");
1243 str = str.replace(re," `$2`$7");
1244 var arr = str.split(AMdelimiter1);
1245 var re1 = new RegExp("(^|\\s)([b-zB-HJ-Z+*<>]|"+texcommand+ambigAMtoken+simpleAMtoken+")(\\s|\\n|$)","g");
1246 var re2 = new RegExp("(^|\\s)([a-z]|"+texcommand+ambigAMtoken+simpleAMtoken+")([,.])","g"); // removed |\d+ for now
1247 for (i=0; i<arr.length; i++) //single nonenglish tokens
1248 if (i%2==0) {
1249 arr[i] = arr[i].replace(re1," `$2`$3");
1250 arr[i] = arr[i].replace(re2," `$2`$3");
1251 arr[i] = arr[i].replace(/([{}[\]])/,"`$1`");
1252 }
1253 str = arr.join(AMdelimiter1);
1254 str = str.replace(/((^|\s)\([a-zA-Z]{2,}.*?)\)`/g,"$1`)"); //fix parentheses
1255 str = str.replace(/`(\((a\s|in\s))(.*?[a-zA-Z]{2,}\))/g,"$1`$3"); //fix parentheses
1256 str = str.replace(/\sin`/g,"` in");
1257 str = str.replace(/`(\(\w\)[,.]?(\s|\n|$))/g,"$1`");
1258 str = str.replace(/`([0-9.]+|e.g|i.e)`(\.?)/gi,"$1$2");
1259 str = str.replace(/`([0-9.]+:)`/g,"$1");
1260 return str;
1261 }
1262
1263 function processNodeR(n, linebreaks,latex) {
1264 var mtch, str, arr, frg, i;
1265 if (n.childNodes.length == 0) {
1266 if ((n.nodeType!=8 || linebreaks) &&
1267 n.parentNode.nodeName!="form" && n.parentNode.nodeName!="FORM" &&
1268 n.parentNode.nodeName!="textarea" && n.parentNode.nodeName!="TEXTAREA"
1269 //&&
1270 //n.parentNode.nodeName!="pre" && n.parentNode.nodeName!="PRE"
1271 ) {
1272 str = n.nodeValue;
1273 if (!(str == null)) {
1274 str = str.replace(/\r\n\r\n/g,"\n\n");
1275 str = str.replace(/\x20+/g," ");
1276 str = str.replace(/\s*\r\n/g," ");
1277 if(latex) {
1278 // DELIMITERS:
1279 mtch = (str.indexOf("\$")==-1 ? false : true);
1280 str = str.replace(/([^\\])\$/g,"$1 \$");
1281 str = str.replace(/^\$/," \$"); // in case \$ at start of string
1282 arr = str.split(" \$");
1283 for (i=0; i<arr.length; i++)
1284 arr[i]=arr[i].replace(/\\\$/g,"\$");
1285 } else {
1286 mtch = false;
1287 str = str.replace(new RegExp(AMescape1, "g"),
1288 function(){mtch = true; return "AMescape1"});
1289 str = str.replace(/\\?end{?a?math}?/i,
1290 function(){automathrecognize = false; mtch = true; return ""});
1291 str = str.replace(/amath\b|\\begin{a?math}/i,
1292 function(){automathrecognize = true; mtch = true; return ""});
1293 arr = str.split(AMdelimiter1);
1294 if (automathrecognize)
1295 for (i=0; i<arr.length; i++)
1296 if (i%2==0) arr[i] = AMautomathrec(arr[i]);
1297 str = arr.join(AMdelimiter1);
1298 arr = str.split(AMdelimiter1);
1299 for (i=0; i<arr.length; i++) // this is a problem ************
1300 arr[i]=arr[i].replace(/AMescape1/g,AMdelimiter1);
1301 }
1302 if (arr.length>1 || mtch) {
1303 if (!noMathML) {
1304 frg = strarr2docFrag(arr,n.nodeType==8,latex);
1305 var len = frg.childNodes.length;
1306 n.parentNode.replaceChild(frg,n);
1307 return len-1;
1308 } else return 0;
1309 }
1310 }
1311 } else return 0;
1312 } else if (n.nodeName!="math") {
1313 for (i=0; i<n.childNodes.length; i++)
1314 i += processNodeR(n.childNodes[i], linebreaks,latex);
1315 }
1316 return 0;
1317 }
1318
1319 function AMprocessNode(n, linebreaks, spanclassAM) {
1320 var frag,st;
1321 if (spanclassAM!=null) {
1322 frag = document.getElementsByTagName("span")
1323 for (var i=0;i<frag.length;i++)
1324 if (frag[i].className == "AM")
1325 processNodeR(frag[i],linebreaks,false);
1326 } else {
1327 try {
1328 st = n.innerHTML; // look for AMdelimiter on page
1329 } catch(err) {}
1330 //alert(st)
1331 if (st==null || /amath\b|\\begin{a?math}/i.test(st) ||
1332 st.indexOf(AMdelimiter1+" ")!=-1 || st.slice(-1)==AMdelimiter1 ||
1333 st.indexOf(AMdelimiter1+"<")!=-1 || st.indexOf(AMdelimiter1+"\n")!=-1) {
1334 processNodeR(n,linebreaks,false);
1335 }
1336 }
1337 }
1338
1339 function generic(){
1340 if(!init()) return;
1341 if (translateOnLoad) {
1342 translate();
1343 }
1344 };
1345 //setup onload function
1346 if(typeof window.addEventListener != 'undefined'){
1347 //.. gecko, safari, konqueror and standard
1348 window.addEventListener('load', generic, false);
1349 }
1350 else if(typeof document.addEventListener != 'undefined'){
1351 //.. opera 7
1352 document.addEventListener('load', generic, false);
1353 }
1354 else if(typeof window.attachEvent != 'undefined'){
1355 //.. win/ie
1356 window.attachEvent('onload', generic);
1357 }else{
1358 //.. mac/ie5 and anything else that gets this far
1359 //if there's an existing onload function
1360 if(typeof window.onload == 'function'){
1361 //store it
1362 var existing = onload;
1363 //add new onload handler
1364 window.onload = function(){
1365 //call existing onload function
1366 existing();
1367 //call generic onload function
1368 generic();
1369 };
1370 }else{
1371 window.onload = generic;
1372 }
1373 }
1374
1375 //expose some functions to outside
1376 asciimath.newcommand = newcommand;
1377 asciimath.newsymbol = newsymbol;
1378 asciimath.AMprocesssNode = AMprocessNode;
1379 asciimath.parseMath = parseMath;
1380 asciimath.translate = translate;
1381 })();
1382
1383 */
1384
1385 /******************************************************************
1386 *
1387 * The previous section is ASCIIMathML.js Version 2.2
1388 * (c) Peter Jipsen, used with permission.
1389 *
1390 ******************************************************************/
1391
1392 showasciiformulaonhover = false;
1393 mathfontfamily = "";
1394 mathcolor = "";
1395 mathfontsize = "";
1396
1397 //
1398 // Remove remapping of mathvariants to plane1 (MathJax handles that)
1399 // Change functions to mi rather than mo (to get spacing right)
1400 //
1401 (function () {
1402 for (var i = 0, m = AMsymbols.length; i < m; i++) {
1403 if (AMsymbols[i].codes) {delete AMsymbols[i].codes}
1404 if (AMsymbols[i].func) {AMsymbols[i].tag = "mi"}
1405 }
1406 })();
1407
1408 //
1409 // Access to AsciiMath functions and values
1410 //
1411 ASCIIMATH.Augment({
1412 AM: {
1413 Init: function () {
1414 displaystyle = ASCIIMATH.config.displaystyle;
1415 // Old versions use the "decimal" option, so take it into account if it
1416 // is defined by the user. See issue 384.
1417 decimalsign = (ASCIIMATH.config.decimal || ASCIIMATH.config.decimalsign);
1418 // unfix phi and varphi, if requested
1419 if (!ASCIIMATH.config.fixphi) {
1420 for (var i = 0, m = AMsymbols.length; i < m; i++) {
1421 if (AMsymbols[i].input === "phi") {AMsymbols[i].output = "\u03C6"}
1422 if (AMsymbols[i].input === "varphi") {AMsymbols[i].output = "\u03D5"; i = m}
1423 }
1424 }
1425
1426 INITASCIIMATH();
1427 initSymbols();
1428 },
1429 Augment: function (def) {
1430 for (var id in def) {if (def.hasOwnProperty(id)) {
1431 switch (id) {
1432 case "displaystyle": displaystyle = def[id]; break;
1433 case "decimal": decimal = def[id]; break;
1434 case "parseMath": parseMath = def[id]; break;
1435 case "parseExpr": AMparseExpr = def[id]; break;
1436 case "parseIexpr": AMparseIexpr = def[id]; break;
1437 case "parseSexpr": AMparseSexpr = def[id]; break;
1438 case "removeBrackets": AMremoveBrackets = def[id]; break;
1439 case "getSymbol": AMgetSymbol = def[id]; break;
1440 case "position": position = def[id]; break;
1441 case "removeCharsAndBlanks": AMremoveCharsAndBlanks = def[id]; break;
1442 case "createMmlNode": createMmlNode = def[id]; break;
1443 case "createElementMathML": AMcreateElementMathML = def[id]; break;
1444 case "createElementXHTML": createElementXHTML = def[id]; break;
1445 case "initSymbols": initSymbols = def[id]; break;
1446 case "refreshSymbols": refreshSymbols = def[id]; break;
1447 case "compareNames": compareNames = def[id]; break;
1448 };
1449 this[id] = def[id];
1450 }};
1451 },
1452 parseMath: parseMath,
1453 parseExpr: AMparseExpr,
1454 parseIexpr: AMparseIexpr,
1455 parseSexr: AMparseSexpr,
1456 removeBrackets: AMremoveBrackets,
1457 getSymbol: AMgetSymbol,
1458 position: position,
1459 removeCharsAndBlanks: AMremoveCharsAndBlanks,
1460 createMmlNode: createMmlNode,
1461 createElementMathML: AMcreateElementMathML,
1462 createElementXHTML: createElementXHTML,
1463 initSymbols: initSymbols,
1464 refreshSymbols: refreshSymbols,
1465 compareNames: compareNames,
1466
1467 createDocumentFragment: DOCFRAG,
1468 document: document,
1469
1470 define: define,
1471 newcommand: newcommand,
1472 newsymbol: newsymbol,
1473 symbols: AMsymbols,
1474 names: AMnames,
1475
1476 TOKEN: {
1477 CONST:CONST, UNARY:UNARY, BINARY:BINARY, INFIX:INFIX,
1478 LEFTBRACKET:LEFTBRACKET, RIGHTBRACKET:RIGHTBRACKET, SPACE:SPACE,
1479 UNDEROVER:UNDEROVER, DEFINITION:DEFINITION, LEFTRIGHT:LEFTRIGHT, TEXT:TEXT,
1480 UNARYUNDEROVER:UNARYUNDEROVER
1481 }
1482 }
1483 });
1484
1485 //
1486 // Make minimizer think these have been used
1487 //
1488 var junk = [window, navigator]; junk = null;
1489
1490 })(MathJax.InputJax.AsciiMath);
1491
1492
1493 /************************************************************************/
1494
1495 (function (ASCIIMATH) {
1496 var MML;
1497
1498 ASCIIMATH.Augment({
1499 sourceMenuTitle: /*_(MathMenu)*/ ["AsciiMathInput","AsciiMath Input"],
1500 annotationEncoding: "text/x-asciimath",
1501
1502 prefilterHooks: MathJax.Callback.Hooks(true), // hooks to run before processing AsciiMath
1503 postfilterHooks: MathJax.Callback.Hooks(true), // hooks to run after processing AsciiMath
1504
1505 Translate: function (script) {
1506 var mml, math = MathJax.HTML.getScript(script);
1507 var data = {math:math, script:script};
1508 var callback = this.prefilterHooks.Execute(data); if (callback) return callback;
1509 math = data.math;
1510 try {
1511 mml = this.AM.parseMath(math);
1512 } catch(err) {
1513 if (!err.asciimathError) {throw err}
1514 mml = this.formatError(err,math);
1515 }
1516 data.math = MML(mml); this.postfilterHooks.Execute(data);
1517 return this.postfilterHooks.Execute(data) || data.math;
1518 },
1519 formatError: function (err,math,script) {
1520 var message = err.message.replace(/\n.*/,"");
1521 MathJax.Hub.signal.Post(["AsciiMath Jax - parse error",message,math,script]);
1522 return MML.Error(message);
1523 },
1524 Error: function (message) {
1525 throw MathJax.Hub.Insert(Error(message),{asciimathError: true});
1526 },
1527 //
1528 // Initialize the MML variable and AsciiMath itself
1529 //
1530 Startup: function () {
1531 MML = MathJax.ElementJax.mml;
1532 this.AM.Init();
1533 }
1534 });
1535
1536 ASCIIMATH.loadComplete("jax.js");
1537
1538 })(MathJax.InputJax.AsciiMath);
1539
1540
1541