jax.js 64 KB

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