Peter Jipsen library for math rendering
// math rendering
/* ASCIIMathML.js ============== This file contains JavaScript functions to convert ASCII math notation to Presentation MathML. The conversion is done while the (X)HTML page loads, and should work with Firefox/Mozilla/Netscape 7+ and Internet Explorer 6+MathPlayer (http://www.dessci.com/en/products/mathplayer/). Just add the next line to your (X)HTML page with this file in the same folder: <script type="text/javascript" src="ASCIIMathML.js"></script> This is a convenient and inexpensive solution for authoring MathML. Version 1.4.7 Aug 7, 2005, (c) Peter Jipsen http://www.chapman.edu/~jipsen Latest version at http://www.chapman.edu/~jipsen/mathml/ASCIIMathML.js For changes see http://www.chapman.edu/~jipsen/mathml/asciimathchanges.txt If you use it on a webpage, please send the URL to jipsen@chapman.edu This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License (at http://www.gnu.org/copyleft/gpl.html) for more details. */ var checkForMathML = true; // check if browser can display MathML var notifyIfNoMathML = true; // put note at top of page if no MathML capability var mathcolor = "blue"; // change it to "" (to inherit) or any other color var mathfontfamily = "serif"; // change to "" to inherit (works in IE) // or another family (e.g. "arial") var displaystyle = true; // puts limits above and below large operators var showasciiformulaonhover = true; // helps students learn ASCIIMath var decimalsign = "."; // change to "," if you like, beware of `(1,2)`! var AMdelimiter1 = "`", AMescape1 = "\\\\`"; // can use other characters var AMdelimiter2 = "$", AMescape2 = "\\\\\\$", AMdelimiter2regexp = "\\$"; var doubleblankmathdelimiter = false; // if true, x+1 is equal to `x+1` // for IE this works only in <!-- --> //var separatetokens;// has been removed (email me if this is a problem) var isIE = document.createElementNS==null; if (document.getElementById==null) alert("This webpage requires a recent browser such as\ \nMozilla/Netscape 7+ or Internet Explorer 6+MathPlayer") // all further global variables start with "AM" function AMcreateElementXHTML(t) { if (isIE) return document.createElement(t); else return document.createElementNS("http://www.w3.org/1999/xhtml",t); } function AMnoMathMLNote() { var nd = AMcreateElementXHTML("h3"); nd.setAttribute("align","center") nd.appendChild(AMcreateElementXHTML("p")); nd.appendChild(document.createTextNode("To view the ")); var an = AMcreateElementXHTML("a"); an.appendChild(document.createTextNode("ASCIIMathML")); an.setAttribute("href","http://www.chapman.edu/~jipsen/asciimath.html"); nd.appendChild(an); nd.appendChild(document.createTextNode(" notation use Internet Explorer 6+")); an = AMcreateElementXHTML("a"); an.appendChild(document.createTextNode("MathPlayer")); an.setAttribute("href","http://www.dessci.com/en/products/mathplayer/download.htm"); nd.appendChild(an); nd.appendChild(document.createTextNode(" or Netscape/Mozilla/Firefox")); nd.appendChild(AMcreateElementXHTML("p")); return nd; } function AMisMathMLavailable() { if (navigator.appName.slice(0,8)=="Netscape") if (navigator.appVersion.slice(0,1)>="5") return null; else return AMnoMathMLNote(); else if (navigator.appName.slice(0,9)=="Microsoft") try { var ActiveX = new ActiveXObject("MathPlayer.Factory.1"); return null; } catch (e) { return AMnoMathMLNote(); } else return AMnoMathMLNote(); } // character lists for Mozilla/Netscape fonts 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]; 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]; 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]; var CONST = 0, UNARY = 1, BINARY = 2, INFIX = 3, LEFTBRACKET = 4, RIGHTBRACKET = 5, SPACE = 6, UNDEROVER = 7, DEFINITION = 8, LEFTRIGHT = 9, TEXT = 10; // token types var AMsqrt = {input:"sqrt", tag:"msqrt", output:"sqrt", tex:null, ttype:UNARY}, AMroot = {input:"root", tag:"mroot", output:"root", tex:null, ttype:BINARY}, AMfrac = {input:"frac", tag:"mfrac", output:"/", tex:null, ttype:BINARY}, AMdiv = {input:"/", tag:"mfrac", output:"/", tex:null, ttype:INFIX}, AMover = {input:"stackrel", tag:"mover", output:"stackrel", tex:null, ttype:BINARY}, AMsub = {input:"_", tag:"msub", output:"_", tex:null, ttype:INFIX}, AMsup = {input:"^", tag:"msup", output:"^", tex:null, ttype:INFIX}, AMtext = {input:"text", tag:"mtext", output:"text", tex:null, ttype:TEXT}, AMmbox = {input:"mbox", tag:"mtext", output:"mbox", tex:null, ttype:TEXT}, AMquote = {input:"\"", tag:"mtext", output:"mbox", tex:null, ttype:TEXT}; var AMsymbols = [ //some greek symbols {input:"alpha", tag:"mi", output:"\u03B1", tex:null, ttype:CONST}, {input:"beta", tag:"mi", output:"\u03B2", tex:null, ttype:CONST}, {input:"chi", tag:"mi", output:"\u03C7", tex:null, ttype:CONST}, {input:"delta", tag:"mi", output:"\u03B4", tex:null, ttype:CONST}, {input:"Delta", tag:"mo", output:"\u0394", tex:null, ttype:CONST}, {input:"epsi", tag:"mi", output:"\u03B5", tex:"epsilon", ttype:CONST}, {input:"varepsilon", tag:"mi", output:"\u025B", tex:null, ttype:CONST}, {input:"eta", tag:"mi", output:"\u03B7", tex:null, ttype:CONST}, {input:"gamma", tag:"mi", output:"\u03B3", tex:null, ttype:CONST}, {input:"Gamma", tag:"mo", output:"\u0393", tex:null, ttype:CONST}, {input:"iota", tag:"mi", output:"\u03B9", tex:null, ttype:CONST}, {input:"kappa", tag:"mi", output:"\u03BA", tex:null, ttype:CONST}, {input:"lambda", tag:"mi", output:"\u03BB", tex:null, ttype:CONST}, {input:"Lambda", tag:"mo", output:"\u039B", tex:null, ttype:CONST}, {input:"mu", tag:"mi", output:"\u03BC", tex:null, ttype:CONST}, {input:"nu", tag:"mi", output:"\u03BD", tex:null, ttype:CONST}, {input:"omega", tag:"mi", output:"\u03C9", tex:null, ttype:CONST}, {input:"Omega", tag:"mo", output:"\u03A9", tex:null, ttype:CONST}, {input:"phi", tag:"mi", output:"\u03C6", tex:null, ttype:CONST}, {input:"varphi", tag:"mi", output:"\u03D5", tex:null, ttype:CONST}, {input:"Phi", tag:"mo", output:"\u03A6", tex:null, ttype:CONST}, {input:"pi", tag:"mi", output:"\u03C0", tex:null, ttype:CONST}, {input:"Pi", tag:"mo", output:"\u03A0", tex:null, ttype:CONST}, {input:"psi", tag:"mi", output:"\u03C8", tex:null, ttype:CONST}, {input:"Psi", tag:"mi", output:"\u03A8", tex:null, ttype:CONST}, {input:"rho", tag:"mi", output:"\u03C1", tex:null, ttype:CONST}, {input:"sigma", tag:"mi", output:"\u03C3", tex:null, ttype:CONST}, {input:"Sigma", tag:"mo", output:"\u03A3", tex:null, ttype:CONST}, {input:"tau", tag:"mi", output:"\u03C4", tex:null, ttype:CONST}, {input:"theta", tag:"mi", output:"\u03B8", tex:null, ttype:CONST}, {input:"vartheta", tag:"mi", output:"\u03D1", tex:null, ttype:CONST}, {input:"Theta", tag:"mo", output:"\u0398", tex:null, ttype:CONST}, {input:"upsilon", tag:"mi", output:"\u03C5", tex:null, ttype:CONST}, {input:"xi", tag:"mi", output:"\u03BE", tex:null, ttype:CONST}, {input:"Xi", tag:"mo", output:"\u039E", tex:null, ttype:CONST}, {input:"zeta", tag:"mi", output:"\u03B6", tex:null, ttype:CONST}, {input:"-", tag:"mi", output:"-", tex:null, ttype:CONST}, //binary operation symbols {input:"*", tag:"mo", output:"\u22C5", tex:"cdot", ttype:CONST}, {input:"**", tag:"mo", output:"\u22C6", tex:"star", ttype:CONST}, {input:"//", tag:"mo", output:"/", tex:null, ttype:CONST}, {input:"\\\\", tag:"mo", output:"\\", tex:"backslash", ttype:CONST}, {input:"setminus", tag:"mo", output:"\\", tex:null, ttype:CONST}, {input:"xx", tag:"mo", output:"\u00D7", tex:"times", ttype:CONST}, {input:"-:", tag:"mo", output:"\u00F7", tex:"divide", ttype:CONST}, {input:"@", tag:"mo", output:"\u2218", tex:"circ", ttype:CONST}, {input:"o+", tag:"mo", output:"\u2295", tex:"oplus", ttype:CONST}, {input:"ox", tag:"mo", output:"\u2297", tex:"otimes", ttype:CONST}, {input:"o.", tag:"mo", output:"\u2299", tex:"odot", ttype:CONST}, {input:"sum", tag:"mo", output:"\u2211", tex:null, ttype:UNDEROVER}, {input:"prod", tag:"mo", output:"\u220F", tex:null, ttype:UNDEROVER}, {input:"^^", tag:"mo", output:"\u2227", tex:"wedge", ttype:CONST}, {input:"^^^", tag:"mo", output:"\u22C0", tex:"bigwedge", ttype:UNDEROVER}, {input:"vv", tag:"mo", output:"\u2228", tex:"vee", ttype:CONST}, {input:"vvv", tag:"mo", output:"\u22C1", tex:"bigvee", ttype:UNDEROVER}, {input:"nn", tag:"mo", output:"\u2229", tex:"cap", ttype:CONST}, {input:"nnn", tag:"mo", output:"\u22C2", tex:"bigcap", ttype:UNDEROVER}, {input:"uu", tag:"mo", output:"\u222A", tex:"cup", ttype:CONST}, {input:"uuu", tag:"mo", output:"\u22C3", tex:"bigcup", ttype:UNDEROVER}, //binary relation symbols {input:"!=", tag:"mo", output:"\u2260", tex:"ne", ttype:CONST}, {input:":=", tag:"mo", output:":=", tex:null, ttype:CONST}, {input:"lt", tag:"mo", output:"<", tex:null, ttype:CONST}, {input:"<=", tag:"mo", output:"\u2264", tex:"le", ttype:CONST}, {input:"lt=", tag:"mo", output:"\u2264", tex:"leq", ttype:CONST}, {input:">=", tag:"mo", output:"\u2265", tex:"ge", ttype:CONST}, {input:"geq", tag:"mo", output:"\u2265", tex:null, ttype:CONST}, {input:"-<", tag:"mo", output:"\u227A", tex:"prec", ttype:CONST}, {input:"-lt", tag:"mo", output:"\u227A", tex:null, ttype:CONST}, {input:">-", tag:"mo", output:"\u227B", tex:"succ", ttype:CONST}, {input:"in", tag:"mo", output:"\u2208", tex:null, ttype:CONST}, {input:"!in", tag:"mo", output:"\u2209", tex:"notin", ttype:CONST}, {input:"sub", tag:"mo", output:"\u2282", tex:"subset", ttype:CONST}, {input:"sup", tag:"mo", output:"\u2283", tex:"supset", ttype:CONST}, {input:"sube", tag:"mo", output:"\u2286", tex:"subseteq", ttype:CONST}, {input:"supe", tag:"mo", output:"\u2287", tex:"supseteq", ttype:CONST}, {input:"-=", tag:"mo", output:"\u2261", tex:"equiv", ttype:CONST}, {input:"~=", tag:"mo", output:"\u2245", tex:"cong", ttype:CONST}, {input:"~~", tag:"mo", output:"\u2248", tex:"approx", ttype:CONST}, {input:"prop", tag:"mo", output:"\u221D", tex:"propto", ttype:CONST}, //logical symbols {input:"and", tag:"mtext", output:"and", tex:null, ttype:SPACE}, {input:"or", tag:"mtext", output:"or", tex:null, ttype:SPACE}, {input:"not", tag:"mo", output:"\u00AC", tex:"neg", ttype:CONST}, {input:"=>", tag:"mo", output:"\u21D2", tex:"implies", ttype:CONST}, {input:"if", tag:"mo", output:"if", tex:null, ttype:SPACE}, {input:"<=>", tag:"mo", output:"\u21D4", tex:"iff", ttype:CONST}, {input:"AA", tag:"mo", output:"\u2200", tex:"forall", ttype:CONST}, {input:"EE", tag:"mo", output:"\u2203", tex:"exists", ttype:CONST}, {input:"_|_", tag:"mo", output:"\u22A5", tex:"bot", ttype:CONST}, {input:"TT", tag:"mo", output:"\u22A4", tex:"top", ttype:CONST}, {input:"|--", tag:"mo", output:"\u22A2", tex:"vdash", ttype:CONST}, {input:"|==", tag:"mo", output:"\u22A8", tex:"models", ttype:CONST}, //grouping brackets {input:"(", tag:"mo", output:"(", tex:null, ttype:LEFTBRACKET}, {input:")", tag:"mo", output:")", tex:null, ttype:RIGHTBRACKET}, {input:"[", tag:"mo", output:"[", tex:null, ttype:LEFTBRACKET}, {input:"]", tag:"mo", output:"]", tex:null, ttype:RIGHTBRACKET}, {input:"{", tag:"mo", output:"{", tex:null, ttype:LEFTBRACKET}, {input:"}", tag:"mo", output:"}", tex:null, ttype:RIGHTBRACKET}, {input:"|", tag:"mo", output:"|", tex:null, ttype:LEFTRIGHT}, //{input:"||", tag:"mo", output:"||", tex:null, ttype:LEFTRIGHT}, {input:"(:", tag:"mo", output:"\u2329", tex:"langle", ttype:LEFTBRACKET}, {input:":)", tag:"mo", output:"\u232A", tex:"rangle", ttype:RIGHTBRACKET}, {input:"<<", tag:"mo", output:"\u2329", tex:null, ttype:LEFTBRACKET}, {input:">>", tag:"mo", output:"\u232A", tex:null, ttype:RIGHTBRACKET}, {input:"{:", tag:"mo", output:"{:", tex:null, ttype:LEFTBRACKET, invisible:true}, {input:":}", tag:"mo", output:":}", tex:null, ttype:RIGHTBRACKET, invisible:true}, //miscellaneous symbols {input:"int", tag:"mo", output:"\u222B", tex:null, ttype:CONST}, {input:"dx", tag:"mi", output:"{:d x:}", tex:null, ttype:DEFINITION}, {input:"dy", tag:"mi", output:"{:d y:}", tex:null, ttype:DEFINITION}, {input:"dz", tag:"mi", output:"{:d z:}", tex:null, ttype:DEFINITION}, {input:"dt", tag:"mi", output:"{:d t:}", tex:null, ttype:DEFINITION}, {input:"oint", tag:"mo", output:"\u222E", tex:null, ttype:CONST}, {input:"del", tag:"mo", output:"\u2202", tex:"partial", ttype:CONST}, {input:"grad", tag:"mo", output:"\u2207", tex:"nabla", ttype:CONST}, {input:"+-", tag:"mo", output:"