jax.js 65 KB

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