ASCIIMathML.js 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513
  1. /*
  2. ASCIIMathML.js
  3. ==============
  4. This file contains JavaScript functions to convert ASCII math notation
  5. and LaTeX to Presentation MathML. Simple graphics commands are also
  6. translated to SVG images. The conversion is done while the (X)HTML
  7. page loads, and should work with Firefox/Mozilla/Netscape 7+ and Internet
  8. Explorer 6/7 + MathPlayer (http://www.dessci.com/en/products/mathplayer/) +
  9. Adobe SVGview 3.03 (http://www.adobe.com/svg/viewer/install/).
  10. Just add the next line to your (X)HTML page with this file in the same folder:
  11. <script type="text/javascript" src="ASCIIMathML.js"></script>
  12. (using the graphics in IE also requires the file "d.svg" in the same folder).
  13. This is a convenient and inexpensive solution for authoring MathML and SVG.
  14. Version 2.0.1 Sept 27, 2007, (c) Peter Jipsen http://www.chapman.edu/~jipsen
  15. This version extends ASCIIMathML.js with LaTeXMathML.js and ASCIIsvg.js.
  16. Latest version at http://www.chapman.edu/~jipsen/mathml/ASCIIMathML.js
  17. If you use it on a webpage, please send the URL to jipsen@chapman.edu
  18. The LaTeXMathML modifications were made by Douglas Woodall, June 2006.
  19. (for details see header on the LaTeXMathML part in middle of file)
  20. This program is free software; you can redistribute it and/or modify
  21. it under the terms of the GNU Lesser General Public License as published by
  22. the Free Software Foundation; either version 2.1 of the License, or (at
  23. your option) any later version.
  24. This program is distributed in the hope that it will be useful, but WITHOUT
  25. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  26. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  27. (at http://www.gnu.org/licences/lgpl.html) for more details.
  28. */
  29. var mathcolor = "blue"; // change it to "" (to inherit) or another color
  30. //var mathfontsize = "1em"; // change to e.g. 1.2em for larger math
  31. var mathfontsize = "1.2em"; // Modified by Ivan Tcholakov, 10-AUG-2009.
  32. var mathfontfamily = "serif"; // change to "" to inherit (works in IE)
  33. // or another family (e.g. "arial")
  34. var automathrecognize = false; // writing "amath" on page makes this true
  35. var checkForMathML = true; // check if browser can display MathML
  36. var notifyIfNoMathML = true; // display note at top if no MathML capability
  37. var alertIfNoMathML = false; // show alert box if no MathML capability
  38. var translateOnLoad = true; // set to false to do call translators from js
  39. var translateLaTeX = true; // false to preserve $..$, $$..$$
  40. var translateLaTeXformatting = true; // false to preserve \emph,\begin{},\end{}
  41. var translateASCIIMath = true; // false to preserve `..`
  42. var translateASCIIsvg = true; // false to preserve agraph.., \begin{graph}..
  43. var avoidinnerHTML = false; // set true if assigning to innerHTML gives error
  44. var displaystyle = true; // puts limits above and below large operators
  45. var showasciiformulaonhover = true; // helps students learn ASCIIMath
  46. var decimalsign = "."; // change to "," if you like, beware of `(1,2)`!
  47. var AMdelimiter1 = "`", AMescape1 = "\\\\`"; // can use other characters
  48. var AMdocumentId = "wikitext" // PmWiki element containing math (default=body)
  49. var checkforprocessasciimathinmoodle = false; // true for systems like Moodle
  50. var dsvglocation = ""; // path to d.svg (blank if same as ASCIIMathML.js loc)
  51. var isIE = document.createElementNS==null;
  52. if (document.getElementById==null)
  53. alert("This webpage requires a recent browser such as\
  54. \nMozilla/Netscape 7+ or Internet Explorer 6+MathPlayer")
  55. // all further ASCIIMathML global variables start with "AM"
  56. function AMcreateElementXHTML(t) {
  57. if (isIE) return document.createElement(t);
  58. else return document.createElementNS("http://www.w3.org/1999/xhtml",t);
  59. }
  60. function AMnoMathMLNote() {
  61. var nd = AMcreateElementXHTML("h3");
  62. nd.setAttribute("align","center")
  63. nd.appendChild(AMcreateElementXHTML("p"));
  64. nd.appendChild(document.createTextNode("To view the "));
  65. var an = AMcreateElementXHTML("a");
  66. an.appendChild(document.createTextNode("ASCIIMathML"));
  67. an.setAttribute("href","http://www.chapman.edu/~jipsen/asciimath.html");
  68. nd.appendChild(an);
  69. nd.appendChild(document.createTextNode(" notation use Internet Explorer 6+"));
  70. an = AMcreateElementXHTML("a");
  71. an.appendChild(document.createTextNode("MathPlayer"));
  72. an.setAttribute("href","http://www.dessci.com/en/products/mathplayer/download.htm");
  73. nd.appendChild(an);
  74. nd.appendChild(document.createTextNode(" or Netscape/Mozilla/Firefox"));
  75. nd.appendChild(AMcreateElementXHTML("p"));
  76. return nd;
  77. }
  78. function AMisMathMLavailable() {
  79. if (navigator.appName.slice(0,8)=="Netscape")
  80. if (navigator.appVersion.slice(0,1)>="5") return null;
  81. else return AMnoMathMLNote();
  82. else if (navigator.appName.slice(0,9)=="Microsoft")
  83. try {
  84. var ActiveX = new ActiveXObject("MathPlayer.Factory.1");
  85. return null;
  86. } catch (e) {
  87. return AMnoMathMLNote();
  88. }
  89. // Added by Ivan Tcholakov, 04-AUG-2009.
  90. else if (navigator.appName=="Opera")
  91. {
  92. if ( parseFloat(navigator.appVersion, 10) >= 9.5 )
  93. {
  94. return null ;
  95. }
  96. else
  97. {
  98. return AMnoMathMLNote();
  99. }
  100. }
  101. //
  102. else return AMnoMathMLNote();
  103. }
  104. // character lists for Mozilla/Netscape fonts
  105. 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];
  106. 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];
  107. 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];
  108. var CONST = 0, UNARY = 1, BINARY = 2, INFIX = 3, LEFTBRACKET = 4,
  109. RIGHTBRACKET = 5, SPACE = 6, UNDEROVER = 7, DEFINITION = 8,
  110. LEFTRIGHT = 9, TEXT = 10; // token types
  111. var AMsqrt = {input:"sqrt", tag:"msqrt", output:"sqrt", tex:null, ttype:UNARY},
  112. AMroot = {input:"root", tag:"mroot", output:"root", tex:null, ttype:BINARY},
  113. AMfrac = {input:"frac", tag:"mfrac", output:"/", tex:null, ttype:BINARY},
  114. AMdiv = {input:"/", tag:"mfrac", output:"/", tex:null, ttype:INFIX},
  115. AMover = {input:"stackrel", tag:"mover", output:"stackrel", tex:null, ttype:BINARY},
  116. AMsub = {input:"_", tag:"msub", output:"_", tex:null, ttype:INFIX},
  117. AMsup = {input:"^", tag:"msup", output:"^", tex:null, ttype:INFIX},
  118. AMtext = {input:"text", tag:"mtext", output:"text", tex:null, ttype:TEXT},
  119. AMmbox = {input:"mbox", tag:"mtext", output:"mbox", tex:null, ttype:TEXT},
  120. AMquote = {input:"\"", tag:"mtext", output:"mbox", tex:null, ttype:TEXT};
  121. var AMsymbols = [
  122. //some greek symbols
  123. {input:"alpha", tag:"mi", output:"\u03B1", tex:null, ttype:CONST},
  124. {input:"beta", tag:"mi", output:"\u03B2", tex:null, ttype:CONST},
  125. {input:"chi", tag:"mi", output:"\u03C7", tex:null, ttype:CONST},
  126. {input:"delta", tag:"mi", output:"\u03B4", tex:null, ttype:CONST},
  127. {input:"Delta", tag:"mo", output:"\u0394", tex:null, ttype:CONST},
  128. {input:"epsi", tag:"mi", output:"\u03B5", tex:"epsilon", ttype:CONST},
  129. {input:"varepsilon", tag:"mi", output:"\u025B", tex:null, ttype:CONST},
  130. {input:"eta", tag:"mi", output:"\u03B7", tex:null, ttype:CONST},
  131. {input:"gamma", tag:"mi", output:"\u03B3", tex:null, ttype:CONST},
  132. {input:"Gamma", tag:"mo", output:"\u0393", tex:null, ttype:CONST},
  133. {input:"iota", tag:"mi", output:"\u03B9", tex:null, ttype:CONST},
  134. {input:"kappa", tag:"mi", output:"\u03BA", tex:null, ttype:CONST},
  135. {input:"lambda", tag:"mi", output:"\u03BB", tex:null, ttype:CONST},
  136. {input:"Lambda", tag:"mo", output:"\u039B", tex:null, ttype:CONST},
  137. {input:"mu", tag:"mi", output:"\u03BC", tex:null, ttype:CONST},
  138. {input:"nu", tag:"mi", output:"\u03BD", tex:null, ttype:CONST},
  139. {input:"omega", tag:"mi", output:"\u03C9", tex:null, ttype:CONST},
  140. {input:"Omega", tag:"mo", output:"\u03A9", tex:null, ttype:CONST},
  141. {input:"phi", tag:"mi", output:"\u03C6", tex:null, ttype:CONST},
  142. {input:"varphi", tag:"mi", output:"\u03D5", tex:null, ttype:CONST},
  143. {input:"Phi", tag:"mo", output:"\u03A6", tex:null, ttype:CONST},
  144. {input:"pi", tag:"mi", output:"\u03C0", tex:null, ttype:CONST},
  145. {input:"Pi", tag:"mo", output:"\u03A0", tex:null, ttype:CONST},
  146. {input:"psi", tag:"mi", output:"\u03C8", tex:null, ttype:CONST},
  147. {input:"Psi", tag:"mi", output:"\u03A8", tex:null, ttype:CONST},
  148. {input:"rho", tag:"mi", output:"\u03C1", tex:null, ttype:CONST},
  149. {input:"sigma", tag:"mi", output:"\u03C3", tex:null, ttype:CONST},
  150. {input:"Sigma", tag:"mo", output:"\u03A3", tex:null, ttype:CONST},
  151. {input:"tau", tag:"mi", output:"\u03C4", tex:null, ttype:CONST},
  152. {input:"theta", tag:"mi", output:"\u03B8", tex:null, ttype:CONST},
  153. {input:"vartheta", tag:"mi", output:"\u03D1", tex:null, ttype:CONST},
  154. {input:"Theta", tag:"mo", output:"\u0398", tex:null, ttype:CONST},
  155. {input:"upsilon", tag:"mi", output:"\u03C5", tex:null, ttype:CONST},
  156. {input:"xi", tag:"mi", output:"\u03BE", tex:null, ttype:CONST},
  157. {input:"Xi", tag:"mo", output:"\u039E", tex:null, ttype:CONST},
  158. {input:"zeta", tag:"mi", output:"\u03B6", tex:null, ttype:CONST},
  159. //binary operation symbols
  160. //{input:"-", tag:"mo", output:"\u0096", tex:null, ttype:CONST},
  161. {input:"*", tag:"mo", output:"\u22C5", tex:"cdot", ttype:CONST},
  162. {input:"**", tag:"mo", output:"\u22C6", tex:"star", ttype:CONST},
  163. {input:"//", tag:"mo", output:"/", tex:null, ttype:CONST},
  164. {input:"\\\\", tag:"mo", output:"\\", tex:"backslash", ttype:CONST},
  165. {input:"setminus", tag:"mo", output:"\\", tex:null, ttype:CONST},
  166. {input:"xx", tag:"mo", output:"\u00D7", tex:"times", ttype:CONST},
  167. {input:"-:", tag:"mo", output:"\u00F7", tex:"divide", ttype:CONST},
  168. {input:"@", tag:"mo", output:"\u2218", tex:"circ", ttype:CONST},
  169. {input:"o+", tag:"mo", output:"\u2295", tex:"oplus", ttype:CONST},
  170. {input:"ox", tag:"mo", output:"\u2297", tex:"otimes", ttype:CONST},
  171. {input:"o.", tag:"mo", output:"\u2299", tex:"odot", ttype:CONST},
  172. {input:"sum", tag:"mo", output:"\u2211", tex:null, ttype:UNDEROVER},
  173. {input:"prod", tag:"mo", output:"\u220F", tex:null, ttype:UNDEROVER},
  174. {input:"^^", tag:"mo", output:"\u2227", tex:"wedge", ttype:CONST},
  175. {input:"^^^", tag:"mo", output:"\u22C0", tex:"bigwedge", ttype:UNDEROVER},
  176. {input:"vv", tag:"mo", output:"\u2228", tex:"vee", ttype:CONST},
  177. {input:"vvv", tag:"mo", output:"\u22C1", tex:"bigvee", ttype:UNDEROVER},
  178. {input:"nn", tag:"mo", output:"\u2229", tex:"cap", ttype:CONST},
  179. {input:"nnn", tag:"mo", output:"\u22C2", tex:"bigcap", ttype:UNDEROVER},
  180. {input:"uu", tag:"mo", output:"\u222A", tex:"cup", ttype:CONST},
  181. {input:"uuu", tag:"mo", output:"\u22C3", tex:"bigcup", ttype:UNDEROVER},
  182. //binary relation symbols
  183. {input:"!=", tag:"mo", output:"\u2260", tex:"ne", ttype:CONST},
  184. {input:":=", tag:"mo", output:":=", tex:null, ttype:CONST},
  185. {input:"lt", tag:"mo", output:"<", tex:null, ttype:CONST},
  186. {input:"<=", tag:"mo", output:"\u2264", tex:"le", ttype:CONST},
  187. {input:"lt=", tag:"mo", output:"\u2264", tex:"leq", ttype:CONST},
  188. {input:">=", tag:"mo", output:"\u2265", tex:"ge", ttype:CONST},
  189. {input:"geq", tag:"mo", output:"\u2265", tex:null, ttype:CONST},
  190. {input:"-<", tag:"mo", output:"\u227A", tex:"prec", ttype:CONST},
  191. {input:"-lt", tag:"mo", output:"\u227A", tex:null, ttype:CONST},
  192. {input:">-", tag:"mo", output:"\u227B", tex:"succ", ttype:CONST},
  193. {input:"-<=", tag:"mo", output:"\u2AAF", tex:"preceq", ttype:CONST},
  194. {input:">-=", tag:"mo", output:"\u2AB0", tex:"succeq", ttype:CONST},
  195. {input:"in", tag:"mo", output:"\u2208", tex:null, ttype:CONST},
  196. {input:"!in", tag:"mo", output:"\u2209", tex:"notin", ttype:CONST},
  197. {input:"sub", tag:"mo", output:"\u2282", tex:"subset", ttype:CONST},
  198. {input:"sup", tag:"mo", output:"\u2283", tex:"supset", ttype:CONST},
  199. {input:"sube", tag:"mo", output:"\u2286", tex:"subseteq", ttype:CONST},
  200. {input:"supe", tag:"mo", output:"\u2287", tex:"supseteq", ttype:CONST},
  201. {input:"-=", tag:"mo", output:"\u2261", tex:"equiv", ttype:CONST},
  202. {input:"~=", tag:"mo", output:"\u2245", tex:"cong", ttype:CONST},
  203. {input:"~~", tag:"mo", output:"\u2248", tex:"approx", ttype:CONST},
  204. {input:"prop", tag:"mo", output:"\u221D", tex:"propto", ttype:CONST},
  205. //logical symbols
  206. {input:"and", tag:"mtext", output:"and", tex:null, ttype:SPACE},
  207. {input:"or", tag:"mtext", output:"or", tex:null, ttype:SPACE},
  208. {input:"not", tag:"mo", output:"\u00AC", tex:"neg", ttype:CONST},
  209. {input:"=>", tag:"mo", output:"\u21D2", tex:"implies", ttype:CONST},
  210. {input:"if", tag:"mo", output:"if", tex:null, ttype:SPACE},
  211. {input:"<=>", tag:"mo", output:"\u21D4", tex:"iff", ttype:CONST},
  212. {input:"AA", tag:"mo", output:"\u2200", tex:"forall", ttype:CONST},
  213. {input:"EE", tag:"mo", output:"\u2203", tex:"exists", ttype:CONST},
  214. {input:"_|_", tag:"mo", output:"\u22A5", tex:"bot", ttype:CONST},
  215. {input:"TT", tag:"mo", output:"\u22A4", tex:"top", ttype:CONST},
  216. {input:"|--", tag:"mo", output:"\u22A2", tex:"vdash", ttype:CONST},
  217. {input:"|==", tag:"mo", output:"\u22A8", tex:"models", ttype:CONST},
  218. //grouping brackets
  219. {input:"(", tag:"mo", output:"(", tex:null, ttype:LEFTBRACKET},
  220. {input:")", tag:"mo", output:")", tex:null, ttype:RIGHTBRACKET},
  221. {input:"[", tag:"mo", output:"[", tex:null, ttype:LEFTBRACKET},
  222. {input:"]", tag:"mo", output:"]", tex:null, ttype:RIGHTBRACKET},
  223. {input:"{", tag:"mo", output:"{", tex:null, ttype:LEFTBRACKET},
  224. {input:"}", tag:"mo", output:"}", tex:null, ttype:RIGHTBRACKET},
  225. {input:"|", tag:"mo", output:"|", tex:null, ttype:LEFTRIGHT},
  226. //{input:"||", tag:"mo", output:"||", tex:null, ttype:LEFTRIGHT},
  227. {input:"(:", tag:"mo", output:"\u2329", tex:"langle", ttype:LEFTBRACKET},
  228. {input:":)", tag:"mo", output:"\u232A", tex:"rangle", ttype:RIGHTBRACKET},
  229. {input:"<<", tag:"mo", output:"\u2329", tex:null, ttype:LEFTBRACKET},
  230. {input:">>", tag:"mo", output:"\u232A", tex:null, ttype:RIGHTBRACKET},
  231. {input:"{:", tag:"mo", output:"{:", tex:null, ttype:LEFTBRACKET, invisible:true},
  232. {input:":}", tag:"mo", output:":}", tex:null, ttype:RIGHTBRACKET, invisible:true},
  233. //miscellaneous symbols
  234. {input:"int", tag:"mo", output:"\u222B", tex:null, ttype:CONST},
  235. {input:"dx", tag:"mi", output:"{:d x:}", tex:null, ttype:DEFINITION},
  236. {input:"dy", tag:"mi", output:"{:d y:}", tex:null, ttype:DEFINITION},
  237. {input:"dz", tag:"mi", output:"{:d z:}", tex:null, ttype:DEFINITION},
  238. {input:"dt", tag:"mi", output:"{:d t:}", tex:null, ttype:DEFINITION},
  239. {input:"oint", tag:"mo", output:"\u222E", tex:null, ttype:CONST},
  240. {input:"del", tag:"mo", output:"\u2202", tex:"partial", ttype:CONST},
  241. {input:"grad", tag:"mo", output:"\u2207", tex:"nabla", ttype:CONST},
  242. {input:"+-", tag:"mo", output:"\u00B1", tex:"pm", ttype:CONST},
  243. {input:"O/", tag:"mo", output:"\u2205", tex:"emptyset", ttype:CONST},
  244. {input:"oo", tag:"mo", output:"\u221E", tex:"infty", ttype:CONST},
  245. {input:"aleph", tag:"mo", output:"\u2135", tex:null, ttype:CONST},
  246. {input:"...", tag:"mo", output:"...", tex:"ldots", ttype:CONST},
  247. {input:":.", tag:"mo", output:"\u2234", tex:"therefore", ttype:CONST},
  248. {input:"/_", tag:"mo", output:"\u2220", tex:"angle", ttype:CONST},
  249. {input:"\\ ", tag:"mo", output:"\u00A0", tex:null, ttype:CONST},
  250. {input:"quad", tag:"mo", output:"\u00A0\u00A0", tex:null, ttype:CONST},
  251. {input:"qquad", tag:"mo", output:"\u00A0\u00A0\u00A0\u00A0", tex:null, ttype:CONST},
  252. {input:"cdots", tag:"mo", output:"\u22EF", tex:null, ttype:CONST},
  253. {input:"vdots", tag:"mo", output:"\u22EE", tex:null, ttype:CONST},
  254. {input:"ddots", tag:"mo", output:"\u22F1", tex:null, ttype:CONST},
  255. {input:"diamond", tag:"mo", output:"\u22C4", tex:null, ttype:CONST},
  256. {input:"square", tag:"mo", output:"\u25A1", tex:null, ttype:CONST},
  257. {input:"|__", tag:"mo", output:"\u230A", tex:"lfloor", ttype:CONST},
  258. {input:"__|", tag:"mo", output:"\u230B", tex:"rfloor", ttype:CONST},
  259. {input:"|~", tag:"mo", output:"\u2308", tex:"lceiling", ttype:CONST},
  260. {input:"~|", tag:"mo", output:"\u2309", tex:"rceiling", ttype:CONST},
  261. {input:"CC", tag:"mo", output:"\u2102", tex:null, ttype:CONST},
  262. {input:"NN", tag:"mo", output:"\u2115", tex:null, ttype:CONST},
  263. {input:"QQ", tag:"mo", output:"\u211A", tex:null, ttype:CONST},
  264. {input:"RR", tag:"mo", output:"\u211D", tex:null, ttype:CONST},
  265. {input:"ZZ", tag:"mo", output:"\u2124", tex:null, ttype:CONST},
  266. {input:"f", tag:"mi", output:"f", tex:null, ttype:UNARY, func:true},
  267. {input:"g", tag:"mi", output:"g", tex:null, ttype:UNARY, func:true},
  268. //standard functions
  269. {input:"lim", tag:"mo", output:"lim", tex:null, ttype:UNDEROVER},
  270. {input:"Lim", tag:"mo", output:"Lim", tex:null, ttype:UNDEROVER},
  271. {input:"sin", tag:"mo", output:"sin", tex:null, ttype:UNARY, func:true},
  272. {input:"cos", tag:"mo", output:"cos", tex:null, ttype:UNARY, func:true},
  273. {input:"tan", tag:"mo", output:"tan", tex:null, ttype:UNARY, func:true},
  274. {input:"sinh", tag:"mo", output:"sinh", tex:null, ttype:UNARY, func:true},
  275. {input:"cosh", tag:"mo", output:"cosh", tex:null, ttype:UNARY, func:true},
  276. {input:"tanh", tag:"mo", output:"tanh", tex:null, ttype:UNARY, func:true},
  277. {input:"cot", tag:"mo", output:"cot", tex:null, ttype:UNARY, func:true},
  278. {input:"sec", tag:"mo", output:"sec", tex:null, ttype:UNARY, func:true},
  279. {input:"csc", tag:"mo", output:"csc", tex:null, ttype:UNARY, func:true},
  280. {input:"log", tag:"mo", output:"log", tex:null, ttype:UNARY, func:true},
  281. {input:"ln", tag:"mo", output:"ln", tex:null, ttype:UNARY, func:true},
  282. {input:"det", tag:"mo", output:"det", tex:null, ttype:UNARY, func:true},
  283. {input:"dim", tag:"mo", output:"dim", tex:null, ttype:CONST},
  284. {input:"mod", tag:"mo", output:"mod", tex:null, ttype:CONST},
  285. {input:"gcd", tag:"mo", output:"gcd", tex:null, ttype:UNARY, func:true},
  286. {input:"lcm", tag:"mo", output:"lcm", tex:null, ttype:UNARY, func:true},
  287. {input:"lub", tag:"mo", output:"lub", tex:null, ttype:CONST},
  288. {input:"glb", tag:"mo", output:"glb", tex:null, ttype:CONST},
  289. {input:"min", tag:"mo", output:"min", tex:null, ttype:UNDEROVER},
  290. {input:"max", tag:"mo", output:"max", tex:null, ttype:UNDEROVER},
  291. //arrows
  292. {input:"uarr", tag:"mo", output:"\u2191", tex:"uparrow", ttype:CONST},
  293. {input:"darr", tag:"mo", output:"\u2193", tex:"downarrow", ttype:CONST},
  294. {input:"rarr", tag:"mo", output:"\u2192", tex:"rightarrow", ttype:CONST},
  295. {input:"->", tag:"mo", output:"\u2192", tex:"to", ttype:CONST},
  296. {input:"|->", tag:"mo", output:"\u21A6", tex:"mapsto", ttype:CONST},
  297. {input:"larr", tag:"mo", output:"\u2190", tex:"leftarrow", ttype:CONST},
  298. {input:"harr", tag:"mo", output:"\u2194", tex:"leftrightarrow", ttype:CONST},
  299. {input:"rArr", tag:"mo", output:"\u21D2", tex:"Rightarrow", ttype:CONST},
  300. {input:"lArr", tag:"mo", output:"\u21D0", tex:"Leftarrow", ttype:CONST},
  301. {input:"hArr", tag:"mo", output:"\u21D4", tex:"Leftrightarrow", ttype:CONST},
  302. //commands with argument
  303. AMsqrt, AMroot, AMfrac, AMdiv, AMover, AMsub, AMsup,
  304. {input:"hat", tag:"mover", output:"\u005E", tex:null, ttype:UNARY, acc:true},
  305. {input:"bar", tag:"mover", output:"\u00AF", tex:"overline", ttype:UNARY, acc:true},
  306. {input:"vec", tag:"mover", output:"\u2192", tex:null, ttype:UNARY, acc:true},
  307. {input:"dot", tag:"mover", output:".", tex:null, ttype:UNARY, acc:true},
  308. {input:"ddot", tag:"mover", output:"..", tex:null, ttype:UNARY, acc:true},
  309. {input:"ul", tag:"munder", output:"\u0332", tex:"underline", ttype:UNARY, acc:true},
  310. AMtext, AMmbox, AMquote,
  311. {input:"bb", tag:"mstyle", atname:"fontweight", atval:"bold", output:"bb", tex:null, ttype:UNARY},
  312. {input:"mathbf", tag:"mstyle", atname:"fontweight", atval:"bold", output:"mathbf", tex:null, ttype:UNARY},
  313. {input:"sf", tag:"mstyle", atname:"fontfamily", atval:"sans-serif", output:"sf", tex:null, ttype:UNARY},
  314. {input:"mathsf", tag:"mstyle", atname:"fontfamily", atval:"sans-serif", output:"mathsf", tex:null, ttype:UNARY},
  315. {input:"bbb", tag:"mstyle", atname:"mathvariant", atval:"double-struck", output:"bbb", tex:null, ttype:UNARY, codes:AMbbb},
  316. {input:"mathbb", tag:"mstyle", atname:"mathvariant", atval:"double-struck", output:"mathbb", tex:null, ttype:UNARY, codes:AMbbb},
  317. {input:"cc", tag:"mstyle", atname:"mathvariant", atval:"script", output:"cc", tex:null, ttype:UNARY, codes:AMcal},
  318. {input:"mathcal", tag:"mstyle", atname:"mathvariant", atval:"script", output:"mathcal", tex:null, ttype:UNARY, codes:AMcal},
  319. {input:"tt", tag:"mstyle", atname:"fontfamily", atval:"monospace", output:"tt", tex:null, ttype:UNARY},
  320. {input:"mathtt", tag:"mstyle", atname:"fontfamily", atval:"monospace", output:"mathtt", tex:null, ttype:UNARY},
  321. {input:"fr", tag:"mstyle", atname:"mathvariant", atval:"fraktur", output:"fr", tex:null, ttype:UNARY, codes:AMfrk},
  322. {input:"mathfrak", tag:"mstyle", atname:"mathvariant", atval:"fraktur", output:"mathfrak", tex:null, ttype:UNARY, codes:AMfrk}
  323. ];
  324. function compareNames(s1,s2) {
  325. if (s1.input > s2.input) return 1
  326. else return -1;
  327. }
  328. var AMnames = []; //list of input symbols
  329. function AMinitSymbols() {
  330. var texsymbols = [], i;
  331. for (i=0; i<AMsymbols.length; i++)
  332. if (AMsymbols[i].tex)
  333. texsymbols[texsymbols.length] = {input:AMsymbols[i].tex,
  334. tag:AMsymbols[i].tag, output:AMsymbols[i].output, ttype:AMsymbols[i].ttype};
  335. AMsymbols = AMsymbols.concat(texsymbols);
  336. AMsymbols.sort(compareNames);
  337. for (i=0; i<AMsymbols.length; i++) AMnames[i] = AMsymbols[i].input;
  338. }
  339. var AMmathml = "http://www.w3.org/1998/Math/MathML";
  340. function AMcreateElementMathML(t) {
  341. if (isIE) return document.createElement("m:"+t);
  342. else return document.createElementNS(AMmathml,t);
  343. }
  344. function AMcreateMmlNode(t,frag) {
  345. // var node = AMcreateElementMathML(name);
  346. if (isIE) var node = document.createElement("m:"+t);
  347. else var node = document.createElementNS(AMmathml,t);
  348. node.appendChild(frag);
  349. return node;
  350. }
  351. function define(oldstr,newstr) {
  352. AMsymbols = AMsymbols.concat([{input:oldstr, tag:"mo", output:newstr,
  353. tex:null, ttype:DEFINITION}]);
  354. AMsymbols.sort(compareNames);
  355. for (i=0; i<AMsymbols.length; i++) AMnames[i] = AMsymbols[i].input;
  356. }
  357. function AMremoveCharsAndBlanks(str,n) {
  358. //remove n characters and any following blanks
  359. var st;
  360. if (str.charAt(n)=="\\" && str.charAt(n+1)!="\\" && str.charAt(n+1)!=" ")
  361. st = str.slice(n+1);
  362. else st = str.slice(n);
  363. for (var i=0; i<st.length && st.charCodeAt(i)<=32; i=i+1);
  364. return st.slice(i);
  365. }
  366. function AMposition(arr, str, n) {
  367. // return position >=n where str appears or would be inserted
  368. // assumes arr is sorted
  369. if (n==0) {
  370. var h,m;
  371. n = -1;
  372. h = arr.length;
  373. while (n+1<h) {
  374. m = (n+h) >> 1;
  375. if (arr[m]<str) n = m; else h = m;
  376. }
  377. return h;
  378. } else
  379. for (var i=n; i<arr.length && arr[i]<str; i++);
  380. return i; // i=arr.length || arr[i]>=str
  381. }
  382. function AMgetSymbol(str) {
  383. //return maximal initial substring of str that appears in names
  384. //return null if there is none
  385. var k = 0; //new pos
  386. var j = 0; //old pos
  387. var mk; //match pos
  388. var st;
  389. var tagst;
  390. var match = "";
  391. var more = true;
  392. for (var i=1; i<=str.length && more; i++) {
  393. st = str.slice(0,i); //initial substring of length i
  394. j = k;
  395. k = AMposition(AMnames, st, j);
  396. if (k<AMnames.length && str.slice(0,AMnames[k].length)==AMnames[k]){
  397. match = AMnames[k];
  398. mk = k;
  399. i = match.length;
  400. }
  401. more = k<AMnames.length && str.slice(0,AMnames[k].length)>=AMnames[k];
  402. }
  403. AMpreviousSymbol=AMcurrentSymbol;
  404. if (match!=""){
  405. AMcurrentSymbol=AMsymbols[mk].ttype;
  406. return AMsymbols[mk];
  407. }
  408. // if str[0] is a digit or - return maxsubstring of digits.digits
  409. AMcurrentSymbol=CONST;
  410. k = 1;
  411. st = str.slice(0,1);
  412. var integ = true;
  413. while ("0"<=st && st<="9" && k<=str.length) {
  414. st = str.slice(k,k+1);
  415. k++;
  416. }
  417. if (st == decimalsign) {
  418. st = str.slice(k,k+1);
  419. if ("0"<=st && st<="9") {
  420. integ = false;
  421. k++;
  422. while ("0"<=st && st<="9" && k<=str.length) {
  423. st = str.slice(k,k+1);
  424. k++;
  425. }
  426. }
  427. }
  428. if ((integ && k>1) || k>2) {
  429. st = str.slice(0,k-1);
  430. tagst = "mn";
  431. } else {
  432. k = 2;
  433. st = str.slice(0,1); //take 1 character
  434. tagst = (("A">st || st>"Z") && ("a">st || st>"z")?"mo":"mi");
  435. }
  436. if (st=="-" && AMpreviousSymbol==INFIX) {
  437. AMcurrentSymbol = INFIX; //trick "/" into recognizing "-" on second parse
  438. return {input:st, tag:tagst, output:st, ttype:UNARY, func:true};
  439. }
  440. return {input:st, tag:tagst, output:st, ttype:CONST};
  441. }
  442. function AMremoveBrackets(node) {
  443. var st;
  444. if (node.nodeName=="mrow") {
  445. st = node.firstChild.firstChild.nodeValue;
  446. if (st=="(" || st=="[" || st=="{") node.removeChild(node.firstChild);
  447. }
  448. if (node.nodeName=="mrow") {
  449. st = node.lastChild.firstChild.nodeValue;
  450. if (st==")" || st=="]" || st=="}") node.removeChild(node.lastChild);
  451. }
  452. }
  453. /*Parsing ASCII math expressions with the following grammar
  454. v ::= [A-Za-z] | greek letters | numbers | other constant symbols
  455. u ::= sqrt | text | bb | other unary symbols for font commands
  456. b ::= frac | root | stackrel binary symbols
  457. l ::= ( | [ | { | (: | {: left brackets
  458. r ::= ) | ] | } | :) | :} right brackets
  459. S ::= v | lEr | uS | bSS Simple expression
  460. I ::= S_S | S^S | S_S^S | S Intermediate expression
  461. E ::= IE | I/I Expression
  462. Each terminal symbol is translated into a corresponding mathml node.*/
  463. var AMnestingDepth,AMpreviousSymbol,AMcurrentSymbol;
  464. function AMparseSexpr(str) { //parses str and returns [node,tailstr]
  465. var symbol, node, result, i, st,// rightvert = false,
  466. newFrag = document.createDocumentFragment();
  467. str = AMremoveCharsAndBlanks(str,0);
  468. symbol = AMgetSymbol(str); //either a token or a bracket or empty
  469. if (symbol == null || symbol.ttype == RIGHTBRACKET && AMnestingDepth > 0) {
  470. return [null,str];
  471. }
  472. if (symbol.ttype == DEFINITION) {
  473. str = symbol.output+AMremoveCharsAndBlanks(str,symbol.input.length);
  474. symbol = AMgetSymbol(str);
  475. }
  476. switch (symbol.ttype) {
  477. case UNDEROVER:
  478. case CONST:
  479. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  480. return [AMcreateMmlNode(symbol.tag, //its a constant
  481. document.createTextNode(symbol.output)),str];
  482. case LEFTBRACKET: //read (expr+)
  483. AMnestingDepth++;
  484. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  485. result = AMparseExpr(str,true);
  486. AMnestingDepth--;
  487. if (typeof symbol.invisible == "boolean" && symbol.invisible)
  488. node = AMcreateMmlNode("mrow",result[0]);
  489. else {
  490. node = AMcreateMmlNode("mo",document.createTextNode(symbol.output));
  491. node = AMcreateMmlNode("mrow",node);
  492. node.appendChild(result[0]);
  493. }
  494. return [node,result[1]];
  495. case TEXT:
  496. if (symbol!=AMquote) str = AMremoveCharsAndBlanks(str,symbol.input.length);
  497. if (str.charAt(0)=="{") i=str.indexOf("}");
  498. else if (str.charAt(0)=="(") i=str.indexOf(")");
  499. else if (str.charAt(0)=="[") i=str.indexOf("]");
  500. else if (symbol==AMquote) i=str.slice(1).indexOf("\"")+1;
  501. else i = 0;
  502. if (i==-1) i = str.length;
  503. st = str.slice(1,i);
  504. if (st.charAt(0) == " ") {
  505. node = AMcreateElementMathML("mspace");
  506. node.setAttribute("width","1ex");
  507. newFrag.appendChild(node);
  508. }
  509. newFrag.appendChild(
  510. AMcreateMmlNode(symbol.tag,document.createTextNode(st)));
  511. if (st.charAt(st.length-1) == " ") {
  512. node = AMcreateElementMathML("mspace");
  513. node.setAttribute("width","1ex");
  514. newFrag.appendChild(node);
  515. }
  516. str = AMremoveCharsAndBlanks(str,i+1);
  517. return [AMcreateMmlNode("mrow",newFrag),str];
  518. case UNARY:
  519. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  520. result = AMparseSexpr(str);
  521. if (result[0]==null) return [AMcreateMmlNode(symbol.tag,
  522. document.createTextNode(symbol.output)),str];
  523. if (typeof symbol.func == "boolean" && symbol.func) { // functions hack
  524. st = str.charAt(0);
  525. if (st=="^" || st=="_" || st=="/" || st=="|" || st==",") {
  526. return [AMcreateMmlNode(symbol.tag,
  527. document.createTextNode(symbol.output)),str];
  528. } else {
  529. node = AMcreateMmlNode("mrow",
  530. AMcreateMmlNode(symbol.tag,document.createTextNode(symbol.output)));
  531. node.appendChild(result[0]);
  532. return [node,result[1]];
  533. }
  534. }
  535. AMremoveBrackets(result[0]);
  536. if (symbol.input == "sqrt") { // sqrt
  537. return [AMcreateMmlNode(symbol.tag,result[0]),result[1]];
  538. } else if (typeof symbol.acc == "boolean" && symbol.acc) { // accent
  539. node = AMcreateMmlNode(symbol.tag,result[0]);
  540. node.appendChild(AMcreateMmlNode("mo",document.createTextNode(symbol.output)));
  541. return [node,result[1]];
  542. } else { // font change command
  543. if (!isIE && typeof symbol.codes != "undefined") {
  544. for (i=0; i<result[0].childNodes.length; i++)
  545. if (result[0].childNodes[i].nodeName=="mi" || result[0].nodeName=="mi") {
  546. st = (result[0].nodeName=="mi"?result[0].firstChild.nodeValue:
  547. result[0].childNodes[i].firstChild.nodeValue);
  548. var newst = [];
  549. for (var j=0; j<st.length; j++)
  550. if (st.charCodeAt(j)>64 && st.charCodeAt(j)<91) newst = newst +
  551. String.fromCharCode(symbol.codes[st.charCodeAt(j)-65]);
  552. else newst = newst + st.charAt(j);
  553. if (result[0].nodeName=="mi")
  554. result[0]=AMcreateElementMathML("mo").
  555. appendChild(document.createTextNode(newst));
  556. else result[0].replaceChild(AMcreateElementMathML("mo").
  557. appendChild(document.createTextNode(newst)),result[0].childNodes[i]);
  558. }
  559. }
  560. node = AMcreateMmlNode(symbol.tag,result[0]);
  561. node.setAttribute(symbol.atname,symbol.atval);
  562. return [node,result[1]];
  563. }
  564. case BINARY:
  565. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  566. result = AMparseSexpr(str);
  567. if (result[0]==null) return [AMcreateMmlNode("mo",
  568. document.createTextNode(symbol.input)),str];
  569. AMremoveBrackets(result[0]);
  570. var result2 = AMparseSexpr(result[1]);
  571. if (result2[0]==null) return [AMcreateMmlNode("mo",
  572. document.createTextNode(symbol.input)),str];
  573. AMremoveBrackets(result2[0]);
  574. if (symbol.input=="root" || symbol.input=="stackrel")
  575. newFrag.appendChild(result2[0]);
  576. newFrag.appendChild(result[0]);
  577. if (symbol.input=="frac") newFrag.appendChild(result2[0]);
  578. return [AMcreateMmlNode(symbol.tag,newFrag),result2[1]];
  579. case INFIX:
  580. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  581. return [AMcreateMmlNode("mo",document.createTextNode(symbol.output)),str];
  582. case SPACE:
  583. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  584. node = AMcreateElementMathML("mspace");
  585. node.setAttribute("width","1ex");
  586. newFrag.appendChild(node);
  587. newFrag.appendChild(
  588. AMcreateMmlNode(symbol.tag,document.createTextNode(symbol.output)));
  589. node = AMcreateElementMathML("mspace");
  590. node.setAttribute("width","1ex");
  591. newFrag.appendChild(node);
  592. return [AMcreateMmlNode("mrow",newFrag),str];
  593. case LEFTRIGHT:
  594. // if (rightvert) return [null,str]; else rightvert = true;
  595. AMnestingDepth++;
  596. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  597. result = AMparseExpr(str,false);
  598. AMnestingDepth--;
  599. var st = "";
  600. if (result[0].lastChild!=null)
  601. st = result[0].lastChild.firstChild.nodeValue;
  602. if (st == "|") { // its an absolute value subterm
  603. node = AMcreateMmlNode("mo",document.createTextNode(symbol.output));
  604. node = AMcreateMmlNode("mrow",node);
  605. node.appendChild(result[0]);
  606. return [node,result[1]];
  607. } else { // the "|" is a \mid
  608. node = AMcreateMmlNode("mo",document.createTextNode(symbol.output));
  609. node = AMcreateMmlNode("mrow",node);
  610. return [node,str];
  611. }
  612. default:
  613. //alert("default");
  614. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  615. return [AMcreateMmlNode(symbol.tag, //its a constant
  616. document.createTextNode(symbol.output)),str];
  617. }
  618. }
  619. function AMparseIexpr(str) {
  620. var symbol, sym1, sym2, node, result, underover;
  621. str = AMremoveCharsAndBlanks(str,0);
  622. sym1 = AMgetSymbol(str);
  623. result = AMparseSexpr(str);
  624. node = result[0];
  625. str = result[1];
  626. symbol = AMgetSymbol(str);
  627. if (symbol.ttype == INFIX && symbol.input != "/") {
  628. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  629. // if (symbol.input == "/") result = AMparseIexpr(str); else ...
  630. result = AMparseSexpr(str);
  631. if (result[0] == null) // show box in place of missing argument
  632. result[0] = AMcreateMmlNode("mo",document.createTextNode("\u25A1"));
  633. else AMremoveBrackets(result[0]);
  634. str = result[1];
  635. // if (symbol.input == "/") AMremoveBrackets(node);
  636. if (symbol.input == "_") {
  637. sym2 = AMgetSymbol(str);
  638. underover = (sym1.ttype == UNDEROVER);
  639. if (sym2.input == "^") {
  640. str = AMremoveCharsAndBlanks(str,sym2.input.length);
  641. var res2 = AMparseSexpr(str);
  642. AMremoveBrackets(res2[0]);
  643. str = res2[1];
  644. node = AMcreateMmlNode((underover?"munderover":"msubsup"),node);
  645. node.appendChild(result[0]);
  646. node.appendChild(res2[0]);
  647. node = AMcreateMmlNode("mrow",node); // so sum does not stretch
  648. } else {
  649. node = AMcreateMmlNode((underover?"munder":"msub"),node);
  650. node.appendChild(result[0]);
  651. }
  652. } else {
  653. node = AMcreateMmlNode(symbol.tag,node);
  654. node.appendChild(result[0]);
  655. }
  656. }
  657. return [node,str];
  658. }
  659. function AMparseExpr(str,rightbracket) {
  660. var symbol, node, result, i, nodeList = [],
  661. newFrag = document.createDocumentFragment();
  662. do {
  663. str = AMremoveCharsAndBlanks(str,0);
  664. result = AMparseIexpr(str);
  665. node = result[0];
  666. str = result[1];
  667. symbol = AMgetSymbol(str);
  668. if (symbol.ttype == INFIX && symbol.input == "/") {
  669. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  670. result = AMparseIexpr(str);
  671. if (result[0] == null) // show box in place of missing argument
  672. result[0] = AMcreateMmlNode("mo",document.createTextNode("\u25A1"));
  673. else AMremoveBrackets(result[0]);
  674. str = result[1];
  675. AMremoveBrackets(node);
  676. node = AMcreateMmlNode(symbol.tag,node);
  677. node.appendChild(result[0]);
  678. newFrag.appendChild(node);
  679. symbol = AMgetSymbol(str);
  680. }
  681. else if (node!=undefined) newFrag.appendChild(node);
  682. } while ((symbol.ttype != RIGHTBRACKET &&
  683. (symbol.ttype != LEFTRIGHT || rightbracket)
  684. || AMnestingDepth == 0) && symbol!=null && symbol.output!="");
  685. if (symbol.ttype == RIGHTBRACKET || symbol.ttype == LEFTRIGHT) {
  686. // if (AMnestingDepth > 0) AMnestingDepth--;
  687. var len = newFrag.childNodes.length;
  688. if (len>0 && newFrag.childNodes[len-1].nodeName == "mrow" && len>1 &&
  689. newFrag.childNodes[len-2].nodeName == "mo" &&
  690. newFrag.childNodes[len-2].firstChild.nodeValue == ",") { //matrix
  691. var right = newFrag.childNodes[len-1].lastChild.firstChild.nodeValue;
  692. if (right==")" || right=="]") {
  693. var left = newFrag.childNodes[len-1].firstChild.firstChild.nodeValue;
  694. if (left=="(" && right==")" && symbol.output != "}" ||
  695. left=="[" && right=="]") {
  696. var pos = []; // positions of commas
  697. var matrix = true;
  698. var m = newFrag.childNodes.length;
  699. for (i=0; matrix && i<m; i=i+2) {
  700. pos[i] = [];
  701. node = newFrag.childNodes[i];
  702. if (matrix) matrix = node.nodeName=="mrow" &&
  703. (i==m-1 || node.nextSibling.nodeName=="mo" &&
  704. node.nextSibling.firstChild.nodeValue==",")&&
  705. node.firstChild.firstChild.nodeValue==left &&
  706. node.lastChild.firstChild.nodeValue==right;
  707. if (matrix)
  708. for (var j=0; j<node.childNodes.length; j++)
  709. if (node.childNodes[j].firstChild.nodeValue==",")
  710. pos[i][pos[i].length]=j;
  711. if (matrix && i>1) matrix = pos[i].length == pos[i-2].length;
  712. }
  713. if (matrix) {
  714. var row, frag, n, k, table = document.createDocumentFragment();
  715. for (i=0; i<m; i=i+2) {
  716. row = document.createDocumentFragment();
  717. frag = document.createDocumentFragment();
  718. node = newFrag.firstChild; // <mrow>(-,-,...,-,-)</mrow>
  719. n = node.childNodes.length;
  720. k = 0;
  721. node.removeChild(node.firstChild); //remove (
  722. for (j=1; j<n-1; j++) {
  723. if (typeof pos[i][k] != "undefined" && j==pos[i][k]){
  724. node.removeChild(node.firstChild); //remove ,
  725. row.appendChild(AMcreateMmlNode("mtd",frag));
  726. k++;
  727. } else frag.appendChild(node.firstChild);
  728. }
  729. row.appendChild(AMcreateMmlNode("mtd",frag));
  730. if (newFrag.childNodes.length>2) {
  731. newFrag.removeChild(newFrag.firstChild); //remove <mrow>)</mrow>
  732. newFrag.removeChild(newFrag.firstChild); //remove <mo>,</mo>
  733. }
  734. table.appendChild(AMcreateMmlNode("mtr",row));
  735. }
  736. node = AMcreateMmlNode("mtable",table);
  737. if (typeof symbol.invisible == "boolean" && symbol.invisible) node.setAttribute("columnalign","left");
  738. newFrag.replaceChild(node,newFrag.firstChild);
  739. }
  740. }
  741. }
  742. }
  743. str = AMremoveCharsAndBlanks(str,symbol.input.length);
  744. if (typeof symbol.invisible != "boolean" || !symbol.invisible) {
  745. node = AMcreateMmlNode("mo",document.createTextNode(symbol.output));
  746. newFrag.appendChild(node);
  747. }
  748. }
  749. return [newFrag,str];
  750. }
  751. function AMparseMath(str) {
  752. var result, node = AMcreateElementMathML("mstyle");
  753. if (mathcolor != "") node.setAttribute("mathcolor",mathcolor);
  754. if (displaystyle) node.setAttribute("displaystyle","true");
  755. if (mathfontfamily != "") node.setAttribute("fontfamily",mathfontfamily);
  756. AMnestingDepth = 0;
  757. node.appendChild(AMparseExpr(str.replace(/^\s+/g,""),false)[0]);
  758. node = AMcreateMmlNode("math",node);
  759. if (showasciiformulaonhover) //fixed by djhsu so newline
  760. node.setAttribute("title",str.replace(/\s+/g," "));//does not show in Gecko
  761. var fnode = AMcreateElementXHTML("span");
  762. fnode.style.fontSize = mathfontsize;
  763. if (mathfontfamily != "") fnode.style.fontFamily = mathfontfamily;
  764. fnode.appendChild(node);
  765. return fnode;
  766. }
  767. function AMstrarr2docFrag(arr, linebreaks) {
  768. var newFrag=document.createDocumentFragment();
  769. var expr = false;
  770. for (var i=0; i<arr.length; i++) {
  771. if (expr) newFrag.appendChild(AMparseMath(arr[i]));
  772. else {
  773. var arri = (linebreaks ? arr[i].split("\n\n") : [arr[i]]);
  774. newFrag.appendChild(AMcreateElementXHTML("span").
  775. appendChild(document.createTextNode(arri[0])));
  776. for (var j=1; j<arri.length; j++) {
  777. newFrag.appendChild(AMcreateElementXHTML("p"));
  778. newFrag.appendChild(AMcreateElementXHTML("span").
  779. appendChild(document.createTextNode(arri[j])));
  780. }
  781. }
  782. expr = !expr;
  783. }
  784. return newFrag;
  785. }
  786. function AMautomathrec(str) {
  787. //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.
  788. //tokens are single letters (except a, A, I) and ASCIIMathML tokens
  789. var texcommand = "\\\\[a-zA-Z]+|\\\\\\s|";
  790. 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])|";
  791. var englishAMtoken = "\\b(?:sum|ox|log|sin|tan|dim|hat|bar|dot)(?![a-z])|";
  792. var secondenglishAMtoken = "|\\bI\\b|\\bin\\b|\\btext\\b"; // took if and or not out
  793. 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?
  794. var letter = "[a-zA-HJ-Z](?=(?:[^a-zA-Z]|$|"+ambigAMtoken+englishAMtoken+simpleAMtoken+"))|";
  795. var token = letter+texcommand+"\\d+|[-()[\\]{}+=*&^_%@/<>,\\|!:;'~]|\\.(?!(?:\x20|$))|"+ambigAMtoken+englishAMtoken+simpleAMtoken;
  796. var re = new RegExp("(^|\\s)((("+token+")\\s?)(("+token+secondenglishAMtoken+")\\s?)+)([,.?]?(?=\\s|$))","g");
  797. str = str.replace(re," `$2`$7");
  798. var arr = str.split(AMdelimiter1);
  799. var re1 = new RegExp("(^|\\s)([b-zB-HJ-Z+*<>]|"+texcommand+ambigAMtoken+simpleAMtoken+")(\\s|\\n|$)","g");
  800. var re2 = new RegExp("(^|\\s)([a-z]|"+texcommand+ambigAMtoken+simpleAMtoken+")([,.])","g"); // removed |\d+ for now
  801. for (i=0; i<arr.length; i++) //single nonenglish tokens
  802. if (i%2==0) {
  803. arr[i] = arr[i].replace(re1," `$2`$3");
  804. arr[i] = arr[i].replace(re2," `$2`$3");
  805. arr[i] = arr[i].replace(/([{}[\]])/,"`$1`");
  806. }
  807. str = arr.join(AMdelimiter1);
  808. str = str.replace(/(\([a-zA-Z]{2,}.*?)\)`/g,"$1`)"); //fix parentheses
  809. str = str.replace(/`(\((a\s|in\s))(.*?[a-zA-Z]{2,}\))/g,"$1`$3"); //fix parentheses
  810. str = str.replace(/\sin`/g,"` in");
  811. str = str.replace(/`(\(\w\)[,.]?(\s|\n|$))/g,"$1`");
  812. str = str.replace(/`([0-9.]+|e.g)`(\\.)/gi,"$1$2");
  813. str = str.replace(/`([0-9.]:)`/g,"$1");
  814. return str;
  815. }
  816. function AMprocessNodeR(n, linebreaks) {
  817. var mtch, str, arr, frg, i;
  818. if (n.childNodes.length == 0) {
  819. if ((n.nodeType!=8 || linebreaks) &&
  820. n.parentNode.nodeName!="form" && n.parentNode.nodeName!="FORM" &&
  821. n.parentNode.nodeName!="textarea" && n.parentNode.nodeName!="TEXTAREA" &&
  822. n.parentNode.nodeName!="pre" && n.parentNode.nodeName!="PRE") {
  823. str = n.nodeValue;
  824. if (!(str == null)) {
  825. str = str.replace(/\r\n\r\n/g,"\n\n");
  826. str = str.replace(/\x20+/g," ");
  827. str = str.replace(/\s*\r\n/g," ");
  828. mtch = false;
  829. str = str.replace(new RegExp(AMescape1, "g"),
  830. function(){mtch = true; return "AMescape1"});
  831. str = str.replace(/\\?end{?a?math}?/i,
  832. function(){automathrecognize = false; mtch = true; return ""});
  833. str = str.replace(/amath|\\begin{a?math}/i,
  834. function(){automathrecognize = true; mtch = true; return ""});
  835. arr = str.split(AMdelimiter1);
  836. if (automathrecognize)
  837. for (i=0; i<arr.length; i++)
  838. if (i%2==0) arr[i] = AMautomathrec(arr[i]);
  839. str = arr.join(AMdelimiter1);
  840. arr = str.split(AMdelimiter1);
  841. for (i=0; i<arr.length; i++) // this is a problem ************
  842. arr[i]=arr[i].replace(/AMescape1/g,AMdelimiter1);
  843. if (arr.length>1 || mtch) {
  844. if (checkForMathML) {
  845. checkForMathML = false;
  846. var nd = AMisMathMLavailable();
  847. AMnoMathML = nd != null;
  848. if (AMnoMathML && notifyIfNoMathML)
  849. if (alertIfNoMathML)
  850. alert("To view the ASCIIMathML notation use Internet Explorer 6 +\nMathPlayer (free from www.dessci.com)\n\
  851. or Firefox/Mozilla/Netscape");
  852. else AMbody.insertBefore(nd,AMbody.childNodes[0]);
  853. }
  854. if (!AMnoMathML) {
  855. frg = AMstrarr2docFrag(arr,n.nodeType==8);
  856. var len = frg.childNodes.length;
  857. n.parentNode.replaceChild(frg,n);
  858. return len-1;
  859. } else return 0;
  860. }
  861. }
  862. } else return 0;
  863. } else if (n.nodeName!="math") {
  864. for (i=0; i<n.childNodes.length; i++)
  865. i += AMprocessNodeR(n.childNodes[i], linebreaks);
  866. }
  867. return 0;
  868. }
  869. function AMprocessNode(n, linebreaks, spanclassAM) {
  870. var frag,st;
  871. if (spanclassAM!=null) {
  872. frag = document.getElementsByTagName("span")
  873. for (var i=0;i<frag.length;i++)
  874. if (frag[i].className == "AM")
  875. AMprocessNodeR(frag[i],linebreaks);
  876. } else {
  877. try {
  878. st = n.innerHTML; // look for AMdelimiter on page
  879. } catch(err) {}
  880. // Corrected by Ivan Tcholakov, 04-AUG-2009.
  881. //if (st==null || /amath|\\begin{a?math}/i.test(st) ||
  882. // st.indexOf(AMdelimiter1+" ")!=-1 || st.slice(-1)==AMdelimiter1 ||
  883. // st.indexOf(AMdelimiter1+"<")!=-1 || st.indexOf(AMdelimiter1+"\n")!=-1) {
  884. // AMprocessNodeR(n,linebreaks);
  885. //}
  886. if (st!=null || /amath|\\begin{a?math}/i.test(st) ||
  887. st.indexOf(AMdelimiter1+" ")!=-1 || st.slice(-1)==AMdelimiter1 ||
  888. st.indexOf(AMdelimiter1+"<")!=-1 || st.indexOf(AMdelimiter1+"\n")!=-1) {
  889. AMprocessNodeR(n,linebreaks);
  890. }
  891. //
  892. }
  893. if (isIE) { //needed to match size and font of formula to surrounding text
  894. frag = document.getElementsByTagName('math');
  895. for (var i=0;i<frag.length;i++) frag[i].update()
  896. }
  897. }
  898. var AMbody;
  899. var AMnoMathML = false, AMtranslated = false;
  900. function translate(spanclassAM) {
  901. if (!AMtranslated) { // run this only once
  902. AMtranslated = true;
  903. AMbody = document.getElementsByTagName("body")[0];
  904. var processN = document.getElementById(AMdocumentId);
  905. // var processN = getElementsByClass(AMbody,"div",AMdocumentClass);
  906. AMprocessNode((processN!=null?processN:AMbody), false, spanclassAM);
  907. }
  908. }
  909. AMinitSymbols();
  910. /*
  911. LaTeXMathML.js
  912. ==============
  913. Version 1.1, July 20, 2007 (c) modifications by Peter Jipsen
  914. (changes: renamed global variables from AM... to LM... so that
  915. LaTeXMathML and ASCIIMathML can be used simultaneously)
  916. Previous header notice:
  917. This file (Version 1.0), is due to Douglas Woodall, June 2006.
  918. It contains JavaScript functions to convert (most simple) LaTeX
  919. math notation to Presentation MathML. It was obtained by
  920. downloading the file ASCIIMathML.js from
  921. http://www1.chapman.edu/~jipsen/mathml/asciimathdownload/
  922. and modifying it so that it carries out ONLY those conversions
  923. that would be carried out in LaTeX. A description of the original
  924. file, with examples, can be found at
  925. www1.chapman.edu/~jipsen/mathml/asciimath.html
  926. ASCIIMathML: Math on the web for everyone
  927. Here is the header notice from the original file:
  928. ASCIIMathML.js
  929. ==============
  930. This file contains JavaScript functions to convert ASCII math notation
  931. to Presentation MathML. The conversion is done while the (X)HTML page
  932. loads, and should work with Firefox/Mozilla/Netscape 7+ and Internet
  933. Explorer 6+MathPlayer (http://www.dessci.com/en/products/mathplayer/).
  934. Just add the next line to your (X)HTML page with this file in the same folder:
  935. <script type="text/javascript" src="ASCIIMathML.js"></script>
  936. This is a convenient and inexpensive solution for authoring MathML.
  937. Version 1.4.7 Dec 15, 2005, (c) Peter Jipsen http://www.chapman.edu/~jipsen
  938. Latest version at http://www.chapman.edu/~jipsen/mathml/ASCIIMathML.js
  939. For changes see http://www.chapman.edu/~jipsen/mathml/asciimathchanges.txt
  940. If you use it on a webpage, please send the URL to jipsen@chapman.edu
  941. This program is free software; you can redistribute it and/or modify
  942. it under the terms of the GNU Lesser General Public License as published by
  943. the Free Software Foundation; either version 2.1 of the License, or (at
  944. your option) any later version.
  945. This program is distributed in the hope that it will be useful,
  946. but WITHOUT ANY WARRANTY; without even the implied warranty of
  947. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  948. General Public License (at http://www.gnu.org/license/lgpl.html)
  949. for more details.
  950. LaTeXMathML.js (ctd)
  951. ==============
  952. Content between $...$ and $$...$$ is converted by this part of the file
  953. */
  954. var LMcheckForMathML = true; // check if browser can display MathML
  955. var LMnotifyIfNoMathML = true; // display note if no MathML capability
  956. var LMalertIfNoMathML = false; // show alert box if no MathML capability
  957. var LMmathcolor = ""; // "" (to inherit) or change to another color
  958. var LMmathfontfamily = "serif"; // change to "" to inherit (works in IE)
  959. // or another family (e.g. "arial")
  960. var LMshowasciiformulaonhover = true; // helps students learn LaTeX
  961. // all further global variables start with "LM"
  962. function LMcreateElementXHTML(t) {
  963. if (isIE) return document.createElement(t);
  964. else return document.createElementNS("http://www.w3.org/1999/xhtml",t);
  965. }
  966. function LMnoMathMLNote() {
  967. var nd = LMcreateElementXHTML("h3");
  968. nd.setAttribute("align","center")
  969. nd.appendChild(LMcreateElementXHTML("p"));
  970. nd.appendChild(document.createTextNode("To view the "));
  971. var an = LMcreateElementXHTML("a");
  972. an.appendChild(document.createTextNode("LaTeXMathML"));
  973. an.setAttribute("href","http://www.maths.nott.ac.uk/personal/drw/lm.html");
  974. nd.appendChild(an);
  975. nd.appendChild(document.createTextNode(" notation use Internet Explorer 6+"));
  976. an = LMcreateElementXHTML("a");
  977. an.appendChild(document.createTextNode("MathPlayer"));
  978. an.setAttribute("href","http://www.dessci.com/en/products/mathplayer/download.htm");
  979. nd.appendChild(an);
  980. nd.appendChild(document.createTextNode(" or Netscape/Mozilla/Firefox"));
  981. nd.appendChild(LMcreateElementXHTML("p"));
  982. return nd;
  983. }
  984. function LMisMathMLavailable() {
  985. if (navigator.appName.slice(0,8)=="Netscape")
  986. if (navigator.appVersion.slice(0,1)>="5") return null;
  987. else return LMnoMathMLNote();
  988. else if (navigator.appName.slice(0,9)=="Microsoft")
  989. try {
  990. var ActiveX = new ActiveXObject("MathPlayer.Factory.1");
  991. return null;
  992. } catch (e) {
  993. return LMnoMathMLNote();
  994. }
  995. else return LMnoMathMLNote();
  996. }
  997. // character lists for Mozilla/Netscape fonts
  998. var LMcal = [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];
  999. var LMfrk = [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];
  1000. var LMbbb = [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];
  1001. //var CONST = 0, UNARY = 1, BINARY = 2, INFIX = 3, LEFTBRACKET = 4,
  1002. // RIGHTBRACKET = 5, SPACE = 6, UNDEROVER = 7, DEFINITION = 8, TEXT = 10,
  1003. var BIG = 11, LONG = 12, STRETCHY = 13, MATRIX = 14; // token types
  1004. var LMsqrt = {input:"\\sqrt", tag:"msqrt", output:"sqrt", ttype:UNARY},
  1005. LMroot = {input:"\\root", tag:"mroot", output:"root", ttype:BINARY},
  1006. LMfrac = {input:"\\frac", tag:"mfrac", output:"/", ttype:BINARY},
  1007. LMover = {input:"\\stackrel", tag:"mover", output:"stackrel", ttype:BINARY},
  1008. LMatop = {input:"\\atop", tag:"mfrac", output:"", ttype:INFIX},
  1009. LMchoose = {input:"\\choose", tag:"mfrac", output:"", ttype:INFIX},
  1010. LMsub = {input:"_", tag:"msub", output:"_", ttype:INFIX},
  1011. LMsup = {input:"^", tag:"msup", output:"^", ttype:INFIX},
  1012. LMtext = {input:"\\mathrm", tag:"mtext", output:"text", ttype:TEXT},
  1013. LMmbox = {input:"\\mbox", tag:"mtext", output:"mbox", ttype:TEXT};
  1014. // Commented out by DRW to prevent 1/2 turning into a 2-line fraction
  1015. // LMdiv = {input:"/", tag:"mfrac", output:"/", ttype:INFIX},
  1016. // Commented out by DRW so that " prints literally in equations
  1017. // LMquote = {input:"\"", tag:"mtext", output:"mbox", ttype:TEXT};
  1018. var LMsymbols = [
  1019. //Greek letters
  1020. {input:"\\alpha", tag:"mi", output:"\u03B1", ttype:CONST},
  1021. {input:"\\beta", tag:"mi", output:"\u03B2", ttype:CONST},
  1022. {input:"\\gamma", tag:"mi", output:"\u03B3", ttype:CONST},
  1023. {input:"\\delta", tag:"mi", output:"\u03B4", ttype:CONST},
  1024. {input:"\\epsilon", tag:"mi", output:"\u03B5", ttype:CONST},
  1025. {input:"\\varepsilon", tag:"mi", output:"\u025B", ttype:CONST},
  1026. {input:"\\zeta", tag:"mi", output:"\u03B6", ttype:CONST},
  1027. {input:"\\eta", tag:"mi", output:"\u03B7", ttype:CONST},
  1028. {input:"\\theta", tag:"mi", output:"\u03B8", ttype:CONST},
  1029. {input:"\\vartheta", tag:"mi", output:"\u03D1", ttype:CONST},
  1030. {input:"\\iota", tag:"mi", output:"\u03B9", ttype:CONST},
  1031. {input:"\\kappa", tag:"mi", output:"\u03BA", ttype:CONST},
  1032. {input:"\\lambda", tag:"mi", output:"\u03BB", ttype:CONST},
  1033. {input:"\\mu", tag:"mi", output:"\u03BC", ttype:CONST},
  1034. {input:"\\nu", tag:"mi", output:"\u03BD", ttype:CONST},
  1035. {input:"\\xi", tag:"mi", output:"\u03BE", ttype:CONST},
  1036. {input:"\\pi", tag:"mi", output:"\u03C0", ttype:CONST},
  1037. {input:"\\varpi", tag:"mi", output:"\u03D6", ttype:CONST},
  1038. {input:"\\rho", tag:"mi", output:"\u03C1", ttype:CONST},
  1039. {input:"\\varrho", tag:"mi", output:"\u03F1", ttype:CONST},
  1040. {input:"\\varsigma", tag:"mi", output:"\u03C2", ttype:CONST},
  1041. {input:"\\sigma", tag:"mi", output:"\u03C3", ttype:CONST},
  1042. {input:"\\tau", tag:"mi", output:"\u03C4", ttype:CONST},
  1043. {input:"\\upsilon", tag:"mi", output:"\u03C5", ttype:CONST},
  1044. {input:"\\phi", tag:"mi", output:"\u03C6", ttype:CONST},
  1045. {input:"\\varphi", tag:"mi", output:"\u03D5", ttype:CONST},
  1046. {input:"\\chi", tag:"mi", output:"\u03C7", ttype:CONST},
  1047. {input:"\\psi", tag:"mi", output:"\u03C8", ttype:CONST},
  1048. {input:"\\omega", tag:"mi", output:"\u03C9", ttype:CONST},
  1049. {input:"\\Gamma", tag:"mo", output:"\u0393", ttype:CONST},
  1050. {input:"\\Delta", tag:"mo", output:"\u0394", ttype:CONST},
  1051. {input:"\\Theta", tag:"mo", output:"\u0398", ttype:CONST},
  1052. {input:"\\Lambda", tag:"mo", output:"\u039B", ttype:CONST},
  1053. {input:"\\Xi", tag:"mo", output:"\u039E", ttype:CONST},
  1054. {input:"\\Pi", tag:"mo", output:"\u03A0", ttype:CONST},
  1055. {input:"\\Sigma", tag:"mo", output:"\u03A3", ttype:CONST},
  1056. {input:"\\Upsilon", tag:"mo", output:"\u03A5", ttype:CONST},
  1057. {input:"\\Phi", tag:"mo", output:"\u03A6", ttype:CONST},
  1058. {input:"\\Psi", tag:"mo", output:"\u03A8", ttype:CONST},
  1059. {input:"\\Omega", tag:"mo", output:"\u03A9", ttype:CONST},
  1060. //fractions
  1061. {input:"\\frac12", tag:"mo", output:"\u00BD", ttype:CONST},
  1062. {input:"\\frac14", tag:"mo", output:"\u00BC", ttype:CONST},
  1063. {input:"\\frac34", tag:"mo", output:"\u00BE", ttype:CONST},
  1064. {input:"\\frac13", tag:"mo", output:"\u2153", ttype:CONST},
  1065. {input:"\\frac23", tag:"mo", output:"\u2154", ttype:CONST},
  1066. {input:"\\frac15", tag:"mo", output:"\u2155", ttype:CONST},
  1067. {input:"\\frac25", tag:"mo", output:"\u2156", ttype:CONST},
  1068. {input:"\\frac35", tag:"mo", output:"\u2157", ttype:CONST},
  1069. {input:"\\frac45", tag:"mo", output:"\u2158", ttype:CONST},
  1070. {input:"\\frac16", tag:"mo", output:"\u2159", ttype:CONST},
  1071. {input:"\\frac56", tag:"mo", output:"\u215A", ttype:CONST},
  1072. {input:"\\frac18", tag:"mo", output:"\u215B", ttype:CONST},
  1073. {input:"\\frac38", tag:"mo", output:"\u215C", ttype:CONST},
  1074. {input:"\\frac58", tag:"mo", output:"\u215D", ttype:CONST},
  1075. {input:"\\frac78", tag:"mo", output:"\u215E", ttype:CONST},
  1076. //binary operation symbols
  1077. {input:"\\pm", tag:"mo", output:"\u00B1", ttype:CONST},
  1078. {input:"\\mp", tag:"mo", output:"\u2213", ttype:CONST},
  1079. {input:"\\triangleleft",tag:"mo", output:"\u22B2", ttype:CONST},
  1080. {input:"\\triangleright",tag:"mo",output:"\u22B3", ttype:CONST},
  1081. {input:"\\cdot", tag:"mo", output:"\u22C5", ttype:CONST},
  1082. {input:"\\star", tag:"mo", output:"\u22C6", ttype:CONST},
  1083. {input:"\\ast", tag:"mo", output:"\u002A", ttype:CONST},
  1084. {input:"\\times", tag:"mo", output:"\u00D7", ttype:CONST},
  1085. {input:"\\div", tag:"mo", output:"\u00F7", ttype:CONST},
  1086. {input:"\\circ", tag:"mo", output:"\u2218", ttype:CONST},
  1087. //{input:"\\bullet", tag:"mo", output:"\u2219", ttype:CONST},
  1088. {input:"\\bullet", tag:"mo", output:"\u2022", ttype:CONST},
  1089. {input:"\\oplus", tag:"mo", output:"\u2295", ttype:CONST},
  1090. {input:"\\ominus", tag:"mo", output:"\u2296", ttype:CONST},
  1091. {input:"\\otimes", tag:"mo", output:"\u2297", ttype:CONST},
  1092. {input:"\\bigcirc", tag:"mo", output:"\u25CB", ttype:CONST},
  1093. {input:"\\oslash", tag:"mo", output:"\u2298", ttype:CONST},
  1094. {input:"\\odot", tag:"mo", output:"\u2299", ttype:CONST},
  1095. {input:"\\land", tag:"mo", output:"\u2227", ttype:CONST},
  1096. {input:"\\wedge", tag:"mo", output:"\u2227", ttype:CONST},
  1097. {input:"\\lor", tag:"mo", output:"\u2228", ttype:CONST},
  1098. {input:"\\vee", tag:"mo", output:"\u2228", ttype:CONST},
  1099. {input:"\\cap", tag:"mo", output:"\u2229", ttype:CONST},
  1100. {input:"\\cup", tag:"mo", output:"\u222A", ttype:CONST},
  1101. {input:"\\sqcap", tag:"mo", output:"\u2293", ttype:CONST},
  1102. {input:"\\sqcup", tag:"mo", output:"\u2294", ttype:CONST},
  1103. {input:"\\uplus", tag:"mo", output:"\u228E", ttype:CONST},
  1104. {input:"\\amalg", tag:"mo", output:"\u2210", ttype:CONST},
  1105. {input:"\\bigtriangleup",tag:"mo",output:"\u25B3", ttype:CONST},
  1106. {input:"\\bigtriangledown",tag:"mo",output:"\u25BD", ttype:CONST},
  1107. {input:"\\dag", tag:"mo", output:"\u2020", ttype:CONST},
  1108. {input:"\\dagger", tag:"mo", output:"\u2020", ttype:CONST},
  1109. {input:"\\ddag", tag:"mo", output:"\u2021", ttype:CONST},
  1110. {input:"\\ddagger", tag:"mo", output:"\u2021", ttype:CONST},
  1111. {input:"\\lhd", tag:"mo", output:"\u22B2", ttype:CONST},
  1112. {input:"\\rhd", tag:"mo", output:"\u22B3", ttype:CONST},
  1113. {input:"\\unlhd", tag:"mo", output:"\u22B4", ttype:CONST},
  1114. {input:"\\unrhd", tag:"mo", output:"\u22B5", ttype:CONST},
  1115. //BIG Operators
  1116. {input:"\\sum", tag:"mo", output:"\u2211", ttype:UNDEROVER},
  1117. {input:"\\prod", tag:"mo", output:"\u220F", ttype:UNDEROVER},
  1118. {input:"\\bigcap", tag:"mo", output:"\u22C2", ttype:UNDEROVER},
  1119. {input:"\\bigcup", tag:"mo", output:"\u22C3", ttype:UNDEROVER},
  1120. {input:"\\bigwedge", tag:"mo", output:"\u22C0", ttype:UNDEROVER},
  1121. {input:"\\bigvee", tag:"mo", output:"\u22C1", ttype:UNDEROVER},
  1122. {input:"\\bigsqcap", tag:"mo", output:"\u2A05", ttype:UNDEROVER},
  1123. {input:"\\bigsqcup", tag:"mo", output:"\u2A06", ttype:UNDEROVER},
  1124. {input:"\\coprod", tag:"mo", output:"\u2210", ttype:UNDEROVER},
  1125. {input:"\\bigoplus", tag:"mo", output:"\u2A01", ttype:UNDEROVER},
  1126. {input:"\\bigotimes", tag:"mo", output:"\u2A02", ttype:UNDEROVER},
  1127. {input:"\\bigodot", tag:"mo", output:"\u2A00", ttype:UNDEROVER},
  1128. {input:"\\biguplus", tag:"mo", output:"\u2A04", ttype:UNDEROVER},
  1129. {input:"\\int", tag:"mo", output:"\u222B", ttype:CONST},
  1130. {input:"\\oint", tag:"mo", output:"\u222E", ttype:CONST},
  1131. //binary relation symbols
  1132. {input:":=", tag:"mo", output:":=", ttype:CONST},
  1133. {input:"\\lt", tag:"mo", output:"<", ttype:CONST},
  1134. {input:"\\gt", tag:"mo", output:">", ttype:CONST},
  1135. {input:"\\ne", tag:"mo", output:"\u2260", ttype:CONST},
  1136. {input:"\\neq", tag:"mo", output:"\u2260", ttype:CONST},
  1137. {input:"\\le", tag:"mo", output:"\u2264", ttype:CONST},
  1138. {input:"\\leq", tag:"mo", output:"\u2264", ttype:CONST},
  1139. {input:"\\leqslant", tag:"mo", output:"\u2264", ttype:CONST},
  1140. {input:"\\ge", tag:"mo", output:"\u2265", ttype:CONST},
  1141. {input:"\\geq", tag:"mo", output:"\u2265", ttype:CONST},
  1142. {input:"\\geqslant", tag:"mo", output:"\u2265", ttype:CONST},
  1143. {input:"\\equiv", tag:"mo", output:"\u2261", ttype:CONST},
  1144. {input:"\\ll", tag:"mo", output:"\u226A", ttype:CONST},
  1145. {input:"\\gg", tag:"mo", output:"\u226B", ttype:CONST},
  1146. {input:"\\doteq", tag:"mo", output:"\u2250", ttype:CONST},
  1147. {input:"\\prec", tag:"mo", output:"\u227A", ttype:CONST},
  1148. {input:"\\succ", tag:"mo", output:"\u227B", ttype:CONST},
  1149. {input:"\\preceq", tag:"mo", output:"\u227C", ttype:CONST},
  1150. {input:"\\succeq", tag:"mo", output:"\u227D", ttype:CONST},
  1151. {input:"\\subset", tag:"mo", output:"\u2282", ttype:CONST},
  1152. {input:"\\supset", tag:"mo", output:"\u2283", ttype:CONST},
  1153. {input:"\\subseteq", tag:"mo", output:"\u2286", ttype:CONST},
  1154. {input:"\\supseteq", tag:"mo", output:"\u2287", ttype:CONST},
  1155. {input:"\\sqsubset", tag:"mo", output:"\u228F", ttype:CONST},
  1156. {input:"\\sqsupset", tag:"mo", output:"\u2290", ttype:CONST},
  1157. {input:"\\sqsubseteq", tag:"mo", output:"\u2291", ttype:CONST},
  1158. {input:"\\sqsupseteq", tag:"mo", output:"\u2292", ttype:CONST},
  1159. {input:"\\sim", tag:"mo", output:"\u223C", ttype:CONST},
  1160. {input:"\\simeq", tag:"mo", output:"\u2243", ttype:CONST},
  1161. {input:"\\approx", tag:"mo", output:"\u2248", ttype:CONST},
  1162. {input:"\\cong", tag:"mo", output:"\u2245", ttype:CONST},
  1163. {input:"\\Join", tag:"mo", output:"\u22C8", ttype:CONST},
  1164. {input:"\\bowtie", tag:"mo", output:"\u22C8", ttype:CONST},
  1165. {input:"\\in", tag:"mo", output:"\u2208", ttype:CONST},
  1166. {input:"\\ni", tag:"mo", output:"\u220B", ttype:CONST},
  1167. {input:"\\owns", tag:"mo", output:"\u220B", ttype:CONST},
  1168. {input:"\\propto", tag:"mo", output:"\u221D", ttype:CONST},
  1169. {input:"\\vdash", tag:"mo", output:"\u22A2", ttype:CONST},
  1170. {input:"\\dashv", tag:"mo", output:"\u22A3", ttype:CONST},
  1171. {input:"\\models", tag:"mo", output:"\u22A8", ttype:CONST},
  1172. {input:"\\perp", tag:"mo", output:"\u22A5", ttype:CONST},
  1173. {input:"\\smile", tag:"mo", output:"\u2323", ttype:CONST},
  1174. {input:"\\frown", tag:"mo", output:"\u2322", ttype:CONST},
  1175. {input:"\\asymp", tag:"mo", output:"\u224D", ttype:CONST},
  1176. {input:"\\notin", tag:"mo", output:"\u2209", ttype:CONST},
  1177. //matrices
  1178. {input:"\\begin{eqnarray}", output:"X", ttype:MATRIX, invisible:true},
  1179. {input:"\\begin{array}", output:"X", ttype:MATRIX, invisible:true},
  1180. {input:"\\\\", output:"}&{", ttype:DEFINITION},
  1181. {input:"\\end{eqnarray}", output:"}}", ttype:DEFINITION},
  1182. {input:"\\end{array}", output:"}}", ttype:DEFINITION},
  1183. //grouping and literal brackets -- ieval is for IE
  1184. {input:"\\big", tag:"mo", output:"X", atval:"1.2", ieval:"2.2", ttype:BIG},
  1185. {input:"\\Big", tag:"mo", output:"X", atval:"1.6", ieval:"2.6", ttype:BIG},
  1186. {input:"\\bigg", tag:"mo", output:"X", atval:"2.2", ieval:"3.2", ttype:BIG},
  1187. {input:"\\Bigg", tag:"mo", output:"X", atval:"2.9", ieval:"3.9", ttype:BIG},
  1188. {input:"\\left", tag:"mo", output:"X", ttype:LEFTBRACKET},
  1189. {input:"\\right", tag:"mo", output:"X", ttype:RIGHTBRACKET},
  1190. {input:"{", output:"{", ttype:LEFTBRACKET, invisible:true},
  1191. {input:"}", output:"}", ttype:RIGHTBRACKET, invisible:true},
  1192. {input:"(", tag:"mo", output:"(", atval:"1", ttype:STRETCHY},
  1193. {input:"[", tag:"mo", output:"[", atval:"1", ttype:STRETCHY},
  1194. {input:"\\lbrack", tag:"mo", output:"[", atval:"1", ttype:STRETCHY},
  1195. {input:"\\{", tag:"mo", output:"{", atval:"1", ttype:STRETCHY},
  1196. {input:"\\lbrace", tag:"mo", output:"{", atval:"1", ttype:STRETCHY},
  1197. {input:"\\langle", tag:"mo", output:"\u2329", atval:"1", ttype:STRETCHY},
  1198. {input:"\\lfloor", tag:"mo", output:"\u230A", atval:"1", ttype:STRETCHY},
  1199. {input:"\\lceil", tag:"mo", output:"\u2308", atval:"1", ttype:STRETCHY},
  1200. // rtag:"mi" causes space to be inserted before a following sin, cos, etc.
  1201. // (see function LMparseExpr() )
  1202. {input:")", tag:"mo",output:")", rtag:"mi",atval:"1",ttype:STRETCHY},
  1203. {input:"]", tag:"mo",output:"]", rtag:"mi",atval:"1",ttype:STRETCHY},
  1204. {input:"\\rbrack",tag:"mo",output:"]", rtag:"mi",atval:"1",ttype:STRETCHY},
  1205. {input:"\\}", tag:"mo",output:"}", rtag:"mi",atval:"1",ttype:STRETCHY},
  1206. {input:"\\rbrace",tag:"mo",output:"}", rtag:"mi",atval:"1",ttype:STRETCHY},
  1207. {input:"\\rangle",tag:"mo",output:"\u232A", rtag:"mi",atval:"1",ttype:STRETCHY},
  1208. {input:"\\rfloor",tag:"mo",output:"\u230B", rtag:"mi",atval:"1",ttype:STRETCHY},
  1209. {input:"\\rceil", tag:"mo",output:"\u2309", rtag:"mi",atval:"1",ttype:STRETCHY},
  1210. // "|", "\\|", "\\vert" and "\\Vert" modified later: lspace = rspace = 0em
  1211. {input:"|", tag:"mo", output:"\u2223", atval:"1", ttype:STRETCHY},
  1212. {input:"\\|", tag:"mo", output:"\u2225", atval:"1", ttype:STRETCHY},
  1213. {input:"\\vert", tag:"mo", output:"\u2223", atval:"1", ttype:STRETCHY},
  1214. {input:"\\Vert", tag:"mo", output:"\u2225", atval:"1", ttype:STRETCHY},
  1215. {input:"\\mid", tag:"mo", output:"\u2223", atval:"1", ttype:STRETCHY},
  1216. {input:"\\parallel", tag:"mo", output:"\u2225", atval:"1", ttype:STRETCHY},
  1217. {input:"/", tag:"mo", output:"/", atval:"1.01", ttype:STRETCHY},
  1218. {input:"\\backslash", tag:"mo", output:"\u2216", atval:"1", ttype:STRETCHY},
  1219. {input:"\\setminus", tag:"mo", output:"\\", ttype:CONST},
  1220. //miscellaneous symbols
  1221. {input:"\\!", tag:"mspace", atname:"width", atval:"-0.167em", ttype:SPACE},
  1222. {input:"\\,", tag:"mspace", atname:"width", atval:"0.167em", ttype:SPACE},
  1223. {input:"\\>", tag:"mspace", atname:"width", atval:"0.222em", ttype:SPACE},
  1224. {input:"\\:", tag:"mspace", atname:"width", atval:"0.222em", ttype:SPACE},
  1225. {input:"\\;", tag:"mspace", atname:"width", atval:"0.278em", ttype:SPACE},
  1226. {input:"~", tag:"mspace", atname:"width", atval:"0.333em", ttype:SPACE},
  1227. {input:"\\quad", tag:"mspace", atname:"width", atval:"1em", ttype:SPACE},
  1228. {input:"\\qquad", tag:"mspace", atname:"width", atval:"2em", ttype:SPACE},
  1229. //{input:"{}", tag:"mo", output:"\u200B", ttype:CONST}, // zero-width
  1230. {input:"\\prime", tag:"mo", output:"\u2032", ttype:CONST},
  1231. {input:"'", tag:"mo", output:"\u02B9", ttype:CONST},
  1232. {input:"''", tag:"mo", output:"\u02BA", ttype:CONST},
  1233. {input:"'''", tag:"mo", output:"\u2034", ttype:CONST},
  1234. {input:"''''", tag:"mo", output:"\u2057", ttype:CONST},
  1235. {input:"\\ldots", tag:"mo", output:"\u2026", ttype:CONST},
  1236. {input:"\\cdots", tag:"mo", output:"\u22EF", ttype:CONST},
  1237. {input:"\\vdots", tag:"mo", output:"\u22EE", ttype:CONST},
  1238. {input:"\\ddots", tag:"mo", output:"\u22F1", ttype:CONST},
  1239. {input:"\\forall", tag:"mo", output:"\u2200", ttype:CONST},
  1240. {input:"\\exists", tag:"mo", output:"\u2203", ttype:CONST},
  1241. {input:"\\Re", tag:"mo", output:"\u211C", ttype:CONST},
  1242. {input:"\\Im", tag:"mo", output:"\u2111", ttype:CONST},
  1243. {input:"\\aleph", tag:"mo", output:"\u2135", ttype:CONST},
  1244. {input:"\\hbar", tag:"mo", output:"\u210F", ttype:CONST},
  1245. {input:"\\ell", tag:"mo", output:"\u2113", ttype:CONST},
  1246. {input:"\\wp", tag:"mo", output:"\u2118", ttype:CONST},
  1247. {input:"\\emptyset", tag:"mo", output:"\u2205", ttype:CONST},
  1248. {input:"\\infty", tag:"mo", output:"\u221E", ttype:CONST},
  1249. {input:"\\surd", tag:"mo", output:"\\sqrt{}", ttype:DEFINITION},
  1250. {input:"\\partial", tag:"mo", output:"\u2202", ttype:CONST},
  1251. {input:"\\nabla", tag:"mo", output:"\u2207", ttype:CONST},
  1252. {input:"\\triangle", tag:"mo", output:"\u25B3", ttype:CONST},
  1253. {input:"\\therefore", tag:"mo", output:"\u2234", ttype:CONST},
  1254. {input:"\\angle", tag:"mo", output:"\u2220", ttype:CONST},
  1255. //{input:"\\\\ ", tag:"mo", output:"\u00A0", ttype:CONST},
  1256. {input:"\\diamond", tag:"mo", output:"\u22C4", ttype:CONST},
  1257. //{input:"\\Diamond", tag:"mo", output:"\u25CA", ttype:CONST},
  1258. {input:"\\Diamond", tag:"mo", output:"\u25C7", ttype:CONST},
  1259. {input:"\\neg", tag:"mo", output:"\u00AC", ttype:CONST},
  1260. {input:"\\lnot", tag:"mo", output:"\u00AC", ttype:CONST},
  1261. {input:"\\bot", tag:"mo", output:"\u22A5", ttype:CONST},
  1262. {input:"\\top", tag:"mo", output:"\u22A4", ttype:CONST},
  1263. {input:"\\square", tag:"mo", output:"\u25AB", ttype:CONST},
  1264. {input:"\\Box", tag:"mo", output:"\u25A1", ttype:CONST},
  1265. {input:"\\wr", tag:"mo", output:"\u2240", ttype:CONST},
  1266. //standard functions
  1267. //Note UNDEROVER *must* have tag:"mo" to work properly
  1268. {input:"\\arccos", tag:"mi", output:"arccos", ttype:UNARY, func:true},
  1269. {input:"\\arcsin", tag:"mi", output:"arcsin", ttype:UNARY, func:true},
  1270. {input:"\\arctan", tag:"mi", output:"arctan", ttype:UNARY, func:true},
  1271. {input:"\\arg", tag:"mi", output:"arg", ttype:UNARY, func:true},
  1272. {input:"\\cos", tag:"mi", output:"cos", ttype:UNARY, func:true},
  1273. {input:"\\cosh", tag:"mi", output:"cosh", ttype:UNARY, func:true},
  1274. {input:"\\cot", tag:"mi", output:"cot", ttype:UNARY, func:true},
  1275. {input:"\\coth", tag:"mi", output:"coth", ttype:UNARY, func:true},
  1276. {input:"\\csc", tag:"mi", output:"csc", ttype:UNARY, func:true},
  1277. {input:"\\deg", tag:"mi", output:"deg", ttype:UNARY, func:true},
  1278. {input:"\\det", tag:"mi", output:"det", ttype:UNARY, func:true},
  1279. {input:"\\dim", tag:"mi", output:"dim", ttype:UNARY, func:true}, //CONST?
  1280. {input:"\\exp", tag:"mi", output:"exp", ttype:UNARY, func:true},
  1281. {input:"\\gcd", tag:"mi", output:"gcd", ttype:UNARY, func:true}, //CONST?
  1282. {input:"\\hom", tag:"mi", output:"hom", ttype:UNARY, func:true},
  1283. {input:"\\inf", tag:"mo", output:"inf", ttype:UNDEROVER},
  1284. {input:"\\ker", tag:"mi", output:"ker", ttype:UNARY, func:true},
  1285. {input:"\\lg", tag:"mi", output:"lg", ttype:UNARY, func:true},
  1286. {input:"\\lim", tag:"mo", output:"lim", ttype:UNDEROVER},
  1287. {input:"\\liminf", tag:"mo", output:"liminf", ttype:UNDEROVER},
  1288. {input:"\\limsup", tag:"mo", output:"limsup", ttype:UNDEROVER},
  1289. {input:"\\ln", tag:"mi", output:"ln", ttype:UNARY, func:true},
  1290. {input:"\\log", tag:"mi", output:"log", ttype:UNARY, func:true},
  1291. {input:"\\max", tag:"mo", output:"max", ttype:UNDEROVER},
  1292. {input:"\\min", tag:"mo", output:"min", ttype:UNDEROVER},
  1293. {input:"\\Pr", tag:"mi", output:"Pr", ttype:UNARY, func:true},
  1294. {input:"\\sec", tag:"mi", output:"sec", ttype:UNARY, func:true},
  1295. {input:"\\sin", tag:"mi", output:"sin", ttype:UNARY, func:true},
  1296. {input:"\\sinh", tag:"mi", output:"sinh", ttype:UNARY, func:true},
  1297. {input:"\\sup", tag:"mo", output:"sup", ttype:UNDEROVER},
  1298. {input:"\\tan", tag:"mi", output:"tan", ttype:UNARY, func:true},
  1299. {input:"\\tanh", tag:"mi", output:"tanh", ttype:UNARY, func:true},
  1300. //arrows
  1301. {input:"\\gets", tag:"mo", output:"\u2190", ttype:CONST},
  1302. {input:"\\leftarrow", tag:"mo", output:"\u2190", ttype:CONST},
  1303. {input:"\\to", tag:"mo", output:"\u2192", ttype:CONST},
  1304. {input:"\\rightarrow", tag:"mo", output:"\u2192", ttype:CONST},
  1305. {input:"\\leftrightarrow", tag:"mo", output:"\u2194", ttype:CONST},
  1306. {input:"\\uparrow", tag:"mo", output:"\u2191", ttype:CONST},
  1307. {input:"\\downarrow", tag:"mo", output:"\u2193", ttype:CONST},
  1308. {input:"\\updownarrow", tag:"mo", output:"\u2195", ttype:CONST},
  1309. {input:"\\Leftarrow", tag:"mo", output:"\u21D0", ttype:CONST},
  1310. {input:"\\Rightarrow", tag:"mo", output:"\u21D2", ttype:CONST},
  1311. {input:"\\Leftrightarrow", tag:"mo", output:"\u21D4", ttype:CONST},
  1312. {input:"\\iff", tag:"mo", output:"~\\Longleftrightarrow~", ttype:DEFINITION},
  1313. {input:"\\Uparrow", tag:"mo", output:"\u21D1", ttype:CONST},
  1314. {input:"\\Downarrow", tag:"mo", output:"\u21D3", ttype:CONST},
  1315. {input:"\\Updownarrow", tag:"mo", output:"\u21D5", ttype:CONST},
  1316. {input:"\\mapsto", tag:"mo", output:"\u21A6", ttype:CONST},
  1317. {input:"\\longleftarrow", tag:"mo", output:"\u2190", ttype:LONG},
  1318. {input:"\\longrightarrow", tag:"mo", output:"\u2192", ttype:LONG},
  1319. {input:"\\longleftrightarrow", tag:"mo", output:"\u2194", ttype:LONG},
  1320. {input:"\\Longleftarrow", tag:"mo", output:"\u21D0", ttype:LONG},
  1321. {input:"\\Longrightarrow", tag:"mo", output:"\u21D2", ttype:LONG},
  1322. {input:"\\Longleftrightarrow", tag:"mo", output:"\u21D4", ttype:LONG},
  1323. {input:"\\longmapsto", tag:"mo", output:"\u21A6", ttype:CONST},
  1324. // disaster if LONG
  1325. //commands with argument
  1326. LMsqrt, LMroot, LMfrac, LMover, LMsub, LMsup, LMtext, LMmbox, LMatop, LMchoose,
  1327. //LMdiv, LMquote,
  1328. //diacritical marks
  1329. {input:"\\acute", tag:"mover", output:"\u00B4", ttype:UNARY, acc:true},
  1330. //{input:"\\acute", tag:"mover", output:"\u0317", ttype:UNARY, acc:true},
  1331. //{input:"\\acute", tag:"mover", output:"\u0301", ttype:UNARY, acc:true},
  1332. //{input:"\\grave", tag:"mover", output:"\u0300", ttype:UNARY, acc:true},
  1333. //{input:"\\grave", tag:"mover", output:"\u0316", ttype:UNARY, acc:true},
  1334. {input:"\\grave", tag:"mover", output:"\u0060", ttype:UNARY, acc:true},
  1335. {input:"\\breve", tag:"mover", output:"\u02D8", ttype:UNARY, acc:true},
  1336. {input:"\\check", tag:"mover", output:"\u02C7", ttype:UNARY, acc:true},
  1337. {input:"\\dot", tag:"mover", output:".", ttype:UNARY, acc:true},
  1338. {input:"\\ddot", tag:"mover", output:"..", ttype:UNARY, acc:true},
  1339. //{input:"\\ddot", tag:"mover", output:"\u00A8", ttype:UNARY, acc:true},
  1340. {input:"\\mathring", tag:"mover", output:"\u00B0", ttype:UNARY, acc:true},
  1341. {input:"\\vec", tag:"mover", output:"\u20D7", ttype:UNARY, acc:true},
  1342. {input:"\\overrightarrow",tag:"mover",output:"\u20D7", ttype:UNARY, acc:true},
  1343. {input:"\\overleftarrow",tag:"mover", output:"\u20D6", ttype:UNARY, acc:true},
  1344. {input:"\\hat", tag:"mover", output:"\u005E", ttype:UNARY, acc:true},
  1345. {input:"\\widehat", tag:"mover", output:"\u0302", ttype:UNARY, acc:true},
  1346. {input:"\\tilde", tag:"mover", output:"~", ttype:UNARY, acc:true},
  1347. //{input:"\\tilde", tag:"mover", output:"\u0303", ttype:UNARY, acc:true},
  1348. {input:"\\widetilde", tag:"mover", output:"\u02DC", ttype:UNARY, acc:true},
  1349. {input:"\\bar", tag:"mover", output:"\u203E", ttype:UNARY, acc:true},
  1350. {input:"\\overbrace", tag:"mover", output:"\u23B4", ttype:UNARY, acc:true},
  1351. {input:"\\overline", tag:"mover", output:"\u00AF", ttype:UNARY, acc:true},
  1352. {input:"\\underbrace", tag:"munder", output:"\u23B5", ttype:UNARY, acc:true},
  1353. {input:"\\underline", tag:"munder", output:"\u00AF", ttype:UNARY, acc:true},
  1354. //{input:"underline", tag:"munder", output:"\u0332", ttype:UNARY, acc:true},
  1355. //typestyles and fonts
  1356. {input:"\\displaystyle",tag:"mstyle",atname:"displaystyle",atval:"true", ttype:UNARY},
  1357. {input:"\\textstyle",tag:"mstyle",atname:"displaystyle",atval:"false", ttype:UNARY},
  1358. {input:"\\scriptstyle",tag:"mstyle",atname:"scriptlevel",atval:"1", ttype:UNARY},
  1359. {input:"\\scriptscriptstyle",tag:"mstyle",atname:"scriptlevel",atval:"2", ttype:UNARY},
  1360. {input:"\\textrm", tag:"mstyle", output:"\\mathrm", ttype: DEFINITION},
  1361. {input:"\\mathbf", tag:"mstyle", atname:"mathvariant", atval:"bold", ttype:UNARY},
  1362. {input:"\\textbf", tag:"mstyle", atname:"mathvariant", atval:"bold", ttype:UNARY},
  1363. {input:"\\mathit", tag:"mstyle", atname:"mathvariant", atval:"italic", ttype:UNARY},
  1364. {input:"\\textit", tag:"mstyle", atname:"mathvariant", atval:"italic", ttype:UNARY},
  1365. {input:"\\mathtt", tag:"mstyle", atname:"mathvariant", atval:"monospace", ttype:UNARY},
  1366. {input:"\\texttt", tag:"mstyle", atname:"mathvariant", atval:"monospace", ttype:UNARY},
  1367. {input:"\\mathsf", tag:"mstyle", atname:"mathvariant", atval:"sans-serif", ttype:UNARY},
  1368. {input:"\\mathbb", tag:"mstyle", atname:"mathvariant", atval:"double-struck", ttype:UNARY, codes:LMbbb},
  1369. {input:"\\mathcal",tag:"mstyle", atname:"mathvariant", atval:"script", ttype:UNARY, codes:LMcal},
  1370. {input:"\\mathfrak",tag:"mstyle",atname:"mathvariant", atval:"fraktur",ttype:UNARY, codes:LMfrk}
  1371. ];
  1372. function compareNames(s1,s2) {
  1373. if (s1.input > s2.input) return 1
  1374. else return -1;
  1375. }
  1376. var LMnames = []; //list of input symbols
  1377. function LMinitSymbols() {
  1378. LMsymbols.sort(compareNames);
  1379. for (i=0; i<LMsymbols.length; i++) LMnames[i] = LMsymbols[i].input;
  1380. }
  1381. var LMmathml = "http://www.w3.org/1998/Math/MathML";
  1382. function LMcreateElementMathML(t) {
  1383. if (isIE) return document.createElement("m:"+t);
  1384. else return document.createElementNS(LMmathml,t);
  1385. }
  1386. function LMcreateMmlNode(t,frag) {
  1387. // var node = LMcreateElementMathML(name);
  1388. if (isIE) var node = document.createElement("m:"+t);
  1389. else var node = document.createElementNS(LMmathml,t);
  1390. node.appendChild(frag);
  1391. return node;
  1392. }
  1393. function newcommand(oldstr,newstr) {
  1394. LMsymbols = LMsymbols.concat([{input:oldstr, tag:"mo", output:newstr,
  1395. ttype:DEFINITION}]);
  1396. }
  1397. function LMremoveCharsAndBlanks(str,n) {
  1398. //remove n characters and any following blanks
  1399. var st;
  1400. st = str.slice(n);
  1401. for (var i=0; i<st.length && st.charCodeAt(i)<=32; i=i+1);
  1402. return st.slice(i);
  1403. }
  1404. function LMposition(arr, str, n) {
  1405. // return position >=n where str appears or would be inserted
  1406. // assumes arr is sorted
  1407. if (n==0) {
  1408. var h,m;
  1409. n = -1;
  1410. h = arr.length;
  1411. while (n+1<h) {
  1412. m = (n+h) >> 1;
  1413. if (arr[m]<str) n = m; else h = m;
  1414. }
  1415. return h;
  1416. } else
  1417. for (var i=n; i<arr.length && arr[i]<str; i++);
  1418. return i; // i=arr.length || arr[i]>=str
  1419. }
  1420. function LMgetSymbol(str) {
  1421. //return maximal initial substring of str that appears in names
  1422. //return null if there is none
  1423. var k = 0; //new pos
  1424. var j = 0; //old pos
  1425. var mk; //match pos
  1426. var st;
  1427. var tagst;
  1428. var match = "";
  1429. var more = true;
  1430. for (var i=1; i<=str.length && more; i++) {
  1431. st = str.slice(0,i); //initial substring of length i
  1432. j = k;
  1433. k = LMposition(LMnames, st, j);
  1434. if (k<LMnames.length && str.slice(0,LMnames[k].length)==LMnames[k]){
  1435. match = LMnames[k];
  1436. mk = k;
  1437. i = match.length;
  1438. }
  1439. more = k<LMnames.length && str.slice(0,LMnames[k].length)>=LMnames[k];
  1440. }
  1441. LMpreviousSymbol=LMcurrentSymbol;
  1442. if (match!=""){
  1443. LMcurrentSymbol=LMsymbols[mk].ttype;
  1444. return LMsymbols[mk];
  1445. }
  1446. LMcurrentSymbol=CONST;
  1447. k = 1;
  1448. st = str.slice(0,1); //take 1 character
  1449. if ("0"<=st && st<="9") tagst = "mn";
  1450. else tagst = (("A">st || st>"Z") && ("a">st || st>"z")?"mo":"mi");
  1451. /*
  1452. // Commented out by DRW (not fully understood, but probably to do with
  1453. // use of "/" as an INFIX version of "\\frac", which we don't want):
  1454. //}
  1455. //if (st=="-" && LMpreviousSymbol==INFIX) {
  1456. // LMcurrentSymbol = INFIX; //trick "/" into recognizing "-" on second parse
  1457. // return {input:st, tag:tagst, output:st, ttype:UNARY, func:true};
  1458. //}
  1459. */
  1460. return {input:st, tag:tagst, output:st, ttype:CONST};
  1461. }
  1462. /*Parsing ASCII math expressions with the following grammar
  1463. v ::= [A-Za-z] | greek letters | numbers | other constant symbols
  1464. u ::= sqrt | text | bb | other unary symbols for font commands
  1465. b ::= frac | root | stackrel binary symbols
  1466. l ::= { | \left left brackets
  1467. r ::= } | \right right brackets
  1468. S ::= v | lEr | uS | bSS Simple expression
  1469. I ::= S_S | S^S | S_S^S | S Intermediate expression
  1470. E ::= IE | I/I Expression
  1471. Each terminal symbol is translated into a corresponding mathml node.*/
  1472. var LMpreviousSymbol,LMcurrentSymbol;
  1473. function LMparseSexpr(str) { //parses str and returns [node,tailstr,(node)tag]
  1474. var symbol, node, result, result2, i, st,// rightvert = false,
  1475. newFrag = document.createDocumentFragment();
  1476. str = LMremoveCharsAndBlanks(str,0);
  1477. symbol = LMgetSymbol(str); //either a token or a bracket or empty
  1478. if (symbol == null || symbol.ttype == RIGHTBRACKET)
  1479. return [null,str,null];
  1480. if (symbol.ttype == DEFINITION) {
  1481. str = symbol.output+LMremoveCharsAndBlanks(str,symbol.input.length);
  1482. symbol = LMgetSymbol(str);
  1483. if (symbol == null || symbol.ttype == RIGHTBRACKET)
  1484. return [null,str,null];
  1485. }
  1486. str = LMremoveCharsAndBlanks(str,symbol.input.length);
  1487. switch (symbol.ttype) {
  1488. case SPACE:
  1489. node = LMcreateElementMathML(symbol.tag);
  1490. node.setAttribute(symbol.atname,symbol.atval);
  1491. return [node,str,symbol.tag];
  1492. case UNDEROVER:
  1493. if (isIE) {
  1494. if (symbol.input.substr(0,4) == "\\big") { // botch for missing symbols
  1495. str = "\\"+symbol.input.substr(4)+str; // make \bigcup = \cup etc.
  1496. symbol = LMgetSymbol(str);
  1497. symbol.ttype = UNDEROVER;
  1498. str = LMremoveCharsAndBlanks(str,symbol.input.length);
  1499. }
  1500. }
  1501. return [LMcreateMmlNode(symbol.tag,
  1502. document.createTextNode(symbol.output)),str,symbol.tag];
  1503. case CONST:
  1504. var output = symbol.output;
  1505. if (isIE) {
  1506. if (symbol.input == "'")
  1507. output = "\u2032";
  1508. else if (symbol.input == "''")
  1509. output = "\u2033";
  1510. else if (symbol.input == "'''")
  1511. output = "\u2033\u2032";
  1512. else if (symbol.input == "''''")
  1513. output = "\u2033\u2033";
  1514. else if (symbol.input == "\\square")
  1515. output = "\u25A1"; // same as \Box
  1516. else if (symbol.input.substr(0,5) == "\\frac") {
  1517. // botch for missing fractions
  1518. var denom = symbol.input.substr(6,1);
  1519. if (denom == "5" || denom == "6") {
  1520. str = symbol.input.replace(/\\frac/,"\\frac ")+str;
  1521. return [node,str,symbol.tag];
  1522. }
  1523. }
  1524. }
  1525. node = LMcreateMmlNode(symbol.tag,document.createTextNode(output));
  1526. return [node,str,symbol.tag];
  1527. case LONG: // added by DRW
  1528. node = LMcreateMmlNode(symbol.tag,document.createTextNode(symbol.output));
  1529. node.setAttribute("minsize","1.5");
  1530. node.setAttribute("maxsize","1.5");
  1531. node = LMcreateMmlNode("mover",node);
  1532. node.appendChild(LMcreateElementMathML("mspace"));
  1533. return [node,str,symbol.tag];
  1534. case STRETCHY: // added by DRW
  1535. if (isIE && symbol.input == "\\backslash")
  1536. symbol.output = "\\"; // doesn't expand, but then nor does "\u2216"
  1537. node = LMcreateMmlNode(symbol.tag,document.createTextNode(symbol.output));
  1538. if (symbol.input == "|" || symbol.input == "\\vert" ||
  1539. symbol.input == "\\|" || symbol.input == "\\Vert") {
  1540. node.setAttribute("lspace","0em");
  1541. node.setAttribute("rspace","0em");
  1542. }
  1543. node.setAttribute("maxsize",symbol.atval); // don't allow to stretch here
  1544. if (symbol.rtag != null)
  1545. return [node,str,symbol.rtag];
  1546. else
  1547. return [node,str,symbol.tag];
  1548. case BIG: // added by DRW
  1549. var atval = symbol.atval;
  1550. if (isIE)
  1551. atval = symbol.ieval;
  1552. symbol = LMgetSymbol(str);
  1553. if (symbol == null)
  1554. return [null,str,null];
  1555. str = LMremoveCharsAndBlanks(str,symbol.input.length);
  1556. node = LMcreateMmlNode(symbol.tag,document.createTextNode(symbol.output));
  1557. if (isIE) { // to get brackets to expand
  1558. var space = LMcreateElementMathML("mspace");
  1559. space.setAttribute("height",atval+"ex");
  1560. node = LMcreateMmlNode("mrow",node);
  1561. node.appendChild(space);
  1562. } else { // ignored in IE
  1563. node.setAttribute("minsize",atval);
  1564. node.setAttribute("maxsize",atval);
  1565. }
  1566. return [node,str,symbol.tag];
  1567. case LEFTBRACKET: //read (expr+)
  1568. if (symbol.input == "\\left") { // left what?
  1569. symbol = LMgetSymbol(str);
  1570. if (symbol != null) {
  1571. if (symbol.input == ".")
  1572. symbol.invisible = true;
  1573. str = LMremoveCharsAndBlanks(str,symbol.input.length);
  1574. }
  1575. }
  1576. result = LMparseExpr(str,true,false);
  1577. if (symbol==null ||
  1578. (typeof symbol.invisible == "boolean" && symbol.invisible))
  1579. node = LMcreateMmlNode("mrow",result[0]);
  1580. else {
  1581. node = LMcreateMmlNode("mo",document.createTextNode(symbol.output));
  1582. node = LMcreateMmlNode("mrow",node);
  1583. node.appendChild(result[0]);
  1584. }
  1585. return [node,result[1],result[2]];
  1586. case MATRIX: //read (expr+)
  1587. if (symbol.input == "\\begin{array}") {
  1588. var mask = "";
  1589. symbol = LMgetSymbol(str);
  1590. str = LMremoveCharsAndBlanks(str,0);
  1591. if (symbol == null)
  1592. mask = "l";
  1593. else {
  1594. str = LMremoveCharsAndBlanks(str,symbol.input.length);
  1595. if (symbol.input != "{")
  1596. mask = "l";
  1597. else do {
  1598. symbol = LMgetSymbol(str);
  1599. if (symbol != null) {
  1600. str = LMremoveCharsAndBlanks(str,symbol.input.length);
  1601. if (symbol.input != "}")
  1602. mask = mask+symbol.input;
  1603. }
  1604. } while (symbol != null && symbol.input != "" && symbol.input != "}");
  1605. }
  1606. result = LMparseExpr("{"+str,true,true);
  1607. // if (result[0]==null) return [LMcreateMmlNode("mo",
  1608. // document.createTextNode(symbol.input)),str];
  1609. node = LMcreateMmlNode("mtable",result[0]);
  1610. mask = mask.replace(/l/g,"left ");
  1611. mask = mask.replace(/r/g,"right ");
  1612. mask = mask.replace(/c/g,"center ");
  1613. node.setAttribute("columnalign",mask);
  1614. node.setAttribute("displaystyle","false");
  1615. if (isIE)
  1616. return [node,result[1],null];
  1617. // trying to get a *little* bit of space around the array
  1618. // (IE already includes it)
  1619. var lspace = LMcreateElementMathML("mspace");
  1620. lspace.setAttribute("width","0.167em");
  1621. var rspace = LMcreateElementMathML("mspace");
  1622. rspace.setAttribute("width","0.167em");
  1623. var node1 = LMcreateMmlNode("mrow",lspace);
  1624. node1.appendChild(node);
  1625. node1.appendChild(rspace);
  1626. return [node1,result[1],null];
  1627. } else { // eqnarray
  1628. result = LMparseExpr("{"+str,true,true);
  1629. node = LMcreateMmlNode("mtable",result[0]);
  1630. if (isIE)
  1631. node.setAttribute("columnspacing","0.25em"); // best in practice?
  1632. else
  1633. node.setAttribute("columnspacing","0.167em"); // correct (but ignored?)
  1634. node.setAttribute("columnalign","right center left");
  1635. node.setAttribute("displaystyle","true");
  1636. node = LMcreateMmlNode("mrow",node);
  1637. return [node,result[1],null];
  1638. }
  1639. case TEXT:
  1640. if (str.charAt(0)=="{") i=str.indexOf("}");
  1641. else i = 0;
  1642. if (i==-1)
  1643. i = str.length;
  1644. st = str.slice(1,i);
  1645. if (st.charAt(0) == " ") {
  1646. node = LMcreateElementMathML("mspace");
  1647. node.setAttribute("width","0.33em"); // was 1ex
  1648. newFrag.appendChild(node);
  1649. }
  1650. newFrag.appendChild(
  1651. LMcreateMmlNode(symbol.tag,document.createTextNode(st)));
  1652. if (st.charAt(st.length-1) == " ") {
  1653. node = LMcreateElementMathML("mspace");
  1654. node.setAttribute("width","0.33em"); // was 1ex
  1655. newFrag.appendChild(node);
  1656. }
  1657. str = LMremoveCharsAndBlanks(str,i+1);
  1658. return [LMcreateMmlNode("mrow",newFrag),str,null];
  1659. case UNARY:
  1660. result = LMparseSexpr(str);
  1661. if (result[0]==null) return [LMcreateMmlNode(symbol.tag,
  1662. document.createTextNode(symbol.output)),str];
  1663. if (typeof symbol.func == "boolean" && symbol.func) { // functions hack
  1664. st = str.charAt(0);
  1665. // if (st=="^" || st=="_" || st=="/" || st=="|" || st==",") {
  1666. if (st=="^" || st=="_" || st==",") {
  1667. return [LMcreateMmlNode(symbol.tag,
  1668. document.createTextNode(symbol.output)),str,symbol.tag];
  1669. } else {
  1670. node = LMcreateMmlNode("mrow",
  1671. LMcreateMmlNode(symbol.tag,document.createTextNode(symbol.output)));
  1672. if (isIE) {
  1673. var space = LMcreateElementMathML("mspace");
  1674. space.setAttribute("width","0.167em");
  1675. node.appendChild(space);
  1676. }
  1677. node.appendChild(result[0]);
  1678. return [node,result[1],symbol.tag];
  1679. }
  1680. }
  1681. if (symbol.input == "\\sqrt") { // sqrt
  1682. if (isIE) { // set minsize, for \surd
  1683. var space = LMcreateElementMathML("mspace");
  1684. space.setAttribute("height","1.2ex");
  1685. space.setAttribute("width","0em"); // probably no effect
  1686. node = LMcreateMmlNode(symbol.tag,result[0])
  1687. // node.setAttribute("minsize","1"); // ignored
  1688. // node = LMcreateMmlNode("mrow",node); // hopefully unnecessary
  1689. node.appendChild(space);
  1690. return [node,result[1],symbol.tag];
  1691. } else
  1692. return [LMcreateMmlNode(symbol.tag,result[0]),result[1],symbol.tag];
  1693. } else if (typeof symbol.acc == "boolean" && symbol.acc) { // accent
  1694. node = LMcreateMmlNode(symbol.tag,result[0]);
  1695. var output = symbol.output;
  1696. if (isIE) {
  1697. if (symbol.input == "\\hat")
  1698. output = "\u0302";
  1699. else if (symbol.input == "\\widehat")
  1700. output = "\u005E";
  1701. else if (symbol.input == "\\bar")
  1702. output = "\u00AF";
  1703. else if (symbol.input == "\\grave")
  1704. output = "\u0300";
  1705. else if (symbol.input == "\\tilde")
  1706. output = "\u0303";
  1707. }
  1708. var node1 = LMcreateMmlNode("mo",document.createTextNode(output));
  1709. if (symbol.input == "\\vec" || symbol.input == "\\check")
  1710. // don't allow to stretch
  1711. node1.setAttribute("maxsize","1.2");
  1712. // why doesn't "1" work? \vec nearly disappears in firefox
  1713. if (isIE && symbol.input == "\\bar")
  1714. node1.setAttribute("maxsize","0.5");
  1715. if (symbol.input == "\\underbrace" || symbol.input == "\\underline")
  1716. node1.setAttribute("accentunder","true");
  1717. else
  1718. node1.setAttribute("accent","true");
  1719. node.appendChild(node1);
  1720. if (symbol.input == "\\overbrace" || symbol.input == "\\underbrace")
  1721. node.ttype = UNDEROVER;
  1722. return [node,result[1],symbol.tag];
  1723. } else { // font change or displaystyle command
  1724. if (!isIE && typeof symbol.codes != "undefined") {
  1725. for (i=0; i<result[0].childNodes.length; i++)
  1726. if (result[0].childNodes[i].nodeName=="mi" || result[0].nodeName=="mi") {
  1727. st = (result[0].nodeName=="mi"?result[0].firstChild.nodeValue:
  1728. result[0].childNodes[i].firstChild.nodeValue);
  1729. var newst = [];
  1730. for (var j=0; j<st.length; j++)
  1731. if (st.charCodeAt(j)>64 && st.charCodeAt(j)<91) newst = newst +
  1732. String.fromCharCode(symbol.codes[st.charCodeAt(j)-65]);
  1733. else newst = newst + st.charAt(j);
  1734. if (result[0].nodeName=="mi")
  1735. result[0]=LMcreateElementMathML("mo").
  1736. appendChild(document.createTextNode(newst));
  1737. else result[0].replaceChild(LMcreateElementMathML("mo").
  1738. appendChild(document.createTextNode(newst)),result[0].childNodes[i]);
  1739. }
  1740. }
  1741. node = LMcreateMmlNode(symbol.tag,result[0]);
  1742. node.setAttribute(symbol.atname,symbol.atval);
  1743. if (symbol.input == "\\scriptstyle" ||
  1744. symbol.input == "\\scriptscriptstyle")
  1745. node.setAttribute("displaystyle","false");
  1746. return [node,result[1],symbol.tag];
  1747. }
  1748. case BINARY:
  1749. result = LMparseSexpr(str);
  1750. if (result[0]==null) return [LMcreateMmlNode("mo",
  1751. document.createTextNode(symbol.input)),str,null];
  1752. result2 = LMparseSexpr(result[1]);
  1753. if (result2[0]==null) return [LMcreateMmlNode("mo",
  1754. document.createTextNode(symbol.input)),str,null];
  1755. if (symbol.input=="\\root" || symbol.input=="\\stackrel")
  1756. newFrag.appendChild(result2[0]);
  1757. newFrag.appendChild(result[0]);
  1758. if (symbol.input=="\\frac") newFrag.appendChild(result2[0]);
  1759. return [LMcreateMmlNode(symbol.tag,newFrag),result2[1],symbol.tag];
  1760. case INFIX:
  1761. str = LMremoveCharsAndBlanks(str,symbol.input.length);
  1762. return [LMcreateMmlNode("mo",document.createTextNode(symbol.output)),
  1763. str,symbol.tag];
  1764. default:
  1765. return [LMcreateMmlNode(symbol.tag, //its a constant
  1766. document.createTextNode(symbol.output)),str,symbol.tag];
  1767. }
  1768. }
  1769. function LMparseIexpr(str) {
  1770. var symbol, sym1, sym2, node, result, tag, underover;
  1771. str = LMremoveCharsAndBlanks(str,0);
  1772. sym1 = LMgetSymbol(str);
  1773. result = LMparseSexpr(str);
  1774. node = result[0];
  1775. str = result[1];
  1776. tag = result[2];
  1777. symbol = LMgetSymbol(str);
  1778. if (symbol.ttype == INFIX) {
  1779. str = LMremoveCharsAndBlanks(str,symbol.input.length);
  1780. result = LMparseSexpr(str);
  1781. if (result[0] == null) // show box in place of missing argument
  1782. result[0] = LMcreateMmlNode("mo",document.createTextNode("\u25A1"));
  1783. str = result[1];
  1784. tag = result[2];
  1785. if (symbol.input == "_" || symbol.input == "^") {
  1786. sym2 = LMgetSymbol(str);
  1787. tag = null; // no space between x^2 and a following sin, cos, etc.
  1788. // This is for \underbrace and \overbrace
  1789. underover = ((sym1.ttype == UNDEROVER) || (node.ttype == UNDEROVER));
  1790. // underover = (sym1.ttype == UNDEROVER);
  1791. if (symbol.input == "_" && sym2.input == "^") {
  1792. str = LMremoveCharsAndBlanks(str,sym2.input.length);
  1793. var res2 = LMparseSexpr(str);
  1794. str = res2[1];
  1795. tag = res2[2]; // leave space between x_1^2 and a following sin etc.
  1796. node = LMcreateMmlNode((underover?"munderover":"msubsup"),node);
  1797. node.appendChild(result[0]);
  1798. node.appendChild(res2[0]);
  1799. } else if (symbol.input == "_") {
  1800. node = LMcreateMmlNode((underover?"munder":"msub"),node);
  1801. node.appendChild(result[0]);
  1802. } else {
  1803. node = LMcreateMmlNode((underover?"mover":"msup"),node);
  1804. node.appendChild(result[0]);
  1805. }
  1806. node = LMcreateMmlNode("mrow",node); // so sum does not stretch
  1807. } else {
  1808. node = LMcreateMmlNode(symbol.tag,node);
  1809. if (symbol.input == "\\atop" || symbol.input == "\\choose")
  1810. node.setAttribute("linethickness","0ex");
  1811. node.appendChild(result[0]);
  1812. if (symbol.input == "\\choose")
  1813. node = LMcreateMmlNode("mfenced",node);
  1814. }
  1815. }
  1816. return [node,str,tag];
  1817. }
  1818. function LMparseExpr(str,rightbracket,matrix) {
  1819. var symbol, node, result, i, tag,
  1820. newFrag = document.createDocumentFragment();
  1821. do {
  1822. str = LMremoveCharsAndBlanks(str,0);
  1823. result = LMparseIexpr(str);
  1824. node = result[0];
  1825. str = result[1];
  1826. tag = result[2];
  1827. symbol = LMgetSymbol(str);
  1828. if (node!=undefined) {
  1829. if ((tag == "mn" || tag == "mi") && symbol!=null &&
  1830. typeof symbol.func == "boolean" && symbol.func) {
  1831. // Add space before \sin in 2\sin x or x\sin x
  1832. var space = LMcreateElementMathML("mspace");
  1833. space.setAttribute("width","0.167em");
  1834. node = LMcreateMmlNode("mrow",node);
  1835. node.appendChild(space);
  1836. }
  1837. newFrag.appendChild(node);
  1838. }
  1839. } while ((symbol.ttype != RIGHTBRACKET)
  1840. && symbol!=null && symbol.output!="");
  1841. tag = null;
  1842. if (symbol.ttype == RIGHTBRACKET) {
  1843. if (symbol.input == "\\right") { // right what?
  1844. str = LMremoveCharsAndBlanks(str,symbol.input.length);
  1845. symbol = LMgetSymbol(str);
  1846. if (symbol != null && symbol.input == ".")
  1847. symbol.invisible = true;
  1848. if (symbol != null)
  1849. tag = symbol.rtag;
  1850. }
  1851. if (symbol!=null)
  1852. str = LMremoveCharsAndBlanks(str,symbol.input.length); // ready to return
  1853. var len = newFrag.childNodes.length;
  1854. if (matrix &&
  1855. len>0 && newFrag.childNodes[len-1].nodeName == "mrow" && len>1 &&
  1856. newFrag.childNodes[len-2].nodeName == "mo" &&
  1857. newFrag.childNodes[len-2].firstChild.nodeValue == "&") { //matrix
  1858. var pos = []; // positions of ampersands
  1859. var m = newFrag.childNodes.length;
  1860. for (i=0; matrix && i<m; i=i+2) {
  1861. pos[i] = [];
  1862. node = newFrag.childNodes[i];
  1863. for (var j=0; j<node.childNodes.length; j++)
  1864. if (node.childNodes[j].firstChild.nodeValue=="&")
  1865. pos[i][pos[i].length]=j;
  1866. }
  1867. var row, frag, n, k, table = document.createDocumentFragment();
  1868. for (i=0; i<m; i=i+2) {
  1869. row = document.createDocumentFragment();
  1870. frag = document.createDocumentFragment();
  1871. node = newFrag.firstChild; // <mrow> -&-&...&-&- </mrow>
  1872. n = node.childNodes.length;
  1873. k = 0;
  1874. for (j=0; j<n; j++) {
  1875. if (typeof pos[i][k] != "undefined" && j==pos[i][k]){
  1876. node.removeChild(node.firstChild); //remove &
  1877. row.appendChild(LMcreateMmlNode("mtd",frag));
  1878. k++;
  1879. } else frag.appendChild(node.firstChild);
  1880. }
  1881. row.appendChild(LMcreateMmlNode("mtd",frag));
  1882. if (newFrag.childNodes.length>2) {
  1883. newFrag.removeChild(newFrag.firstChild); //remove <mrow> </mrow>
  1884. newFrag.removeChild(newFrag.firstChild); //remove <mo>&</mo>
  1885. }
  1886. table.appendChild(LMcreateMmlNode("mtr",row));
  1887. }
  1888. return [table,str];
  1889. }
  1890. if (typeof symbol.invisible != "boolean" || !symbol.invisible) {
  1891. node = LMcreateMmlNode("mo",document.createTextNode(symbol.output));
  1892. newFrag.appendChild(node);
  1893. }
  1894. }
  1895. return [newFrag,str,tag];
  1896. }
  1897. function LMparseMath(str) {
  1898. var result, node = LMcreateElementMathML("mstyle");
  1899. if (LMmathcolor != "") node.setAttribute("mathcolor",LMmathcolor);
  1900. if (LMmathfontfamily != "") node.setAttribute("fontfamily",LMmathfontfamily);
  1901. node.appendChild(LMparseExpr(str.replace(/^\s+/g,""),false,false)[0]);
  1902. node = LMcreateMmlNode("math",node);
  1903. if (LMshowasciiformulaonhover) //fixed by djhsu so newline
  1904. node.setAttribute("title",str.replace(/\s+/g," "));//does not show in Gecko
  1905. var fnode = LMcreateElementXHTML("span");
  1906. fnode.style.fontSize = mathfontsize;
  1907. if (LMmathfontfamily != "") fnode.style.fontFamily = LMmathfontfamily;
  1908. fnode.appendChild(node);
  1909. return fnode;
  1910. }
  1911. function LMstrarr2docFrag(arr, linebreaks) {
  1912. var newFrag=document.createDocumentFragment();
  1913. var expr = false;
  1914. for (var i=0; i<arr.length; i++) {
  1915. if (expr) newFrag.appendChild(LMparseMath(arr[i]));
  1916. else {
  1917. var arri = (linebreaks ? arr[i].split("\n\n") : [arr[i]]);
  1918. newFrag.appendChild(LMcreateElementXHTML("span").
  1919. appendChild(document.createTextNode(arri[0])));
  1920. for (var j=1; j<arri.length; j++) {
  1921. newFrag.appendChild(LMcreateElementXHTML("p"));
  1922. newFrag.appendChild(LMcreateElementXHTML("span").
  1923. appendChild(document.createTextNode(arri[j])));
  1924. }
  1925. }
  1926. expr = !expr;
  1927. }
  1928. return newFrag;
  1929. }
  1930. function LMprocessNodeR(n, linebreaks) {
  1931. var mtch, str, arr, frg, i;
  1932. if (n.childNodes.length == 0) {
  1933. if ((n.nodeType!=8 || linebreaks) &&
  1934. n.parentNode.nodeName!="form" && n.parentNode.nodeName!="FORM" &&
  1935. n.parentNode.nodeName!="textarea" && n.parentNode.nodeName!="TEXTAREA" &&
  1936. n.parentNode.nodeName!="pre" && n.parentNode.nodeName!="PRE") {
  1937. str = n.nodeValue;
  1938. if (!(str == null)) {
  1939. str = str.replace(/\r\n\r\n/g,"\n\n");
  1940. str = str.replace(/\x20+/g," ");
  1941. str = str.replace(/\s*\r\n/g," ");
  1942. // DELIMITERS:
  1943. mtch = (str.indexOf("\$")==-1 ? false : true);
  1944. str = str.replace(/([^\\])\$/g,"$1 \$");
  1945. str = str.replace(/^\$/," \$"); // in case \$ at start of string
  1946. arr = str.split(" \$");
  1947. for (i=0; i<arr.length; i++)
  1948. arr[i]=arr[i].replace(/\\\$/g,"\$");
  1949. if (arr.length>1 || mtch) {
  1950. if (LMcheckForMathML) {
  1951. LMcheckForMathML = false;
  1952. var nd = LMisMathMLavailable();
  1953. LMnoMathML = nd != null;
  1954. if (LMnoMathML && LMnotifyIfNoMathML)
  1955. if (LMalertIfNoMathML)
  1956. alert("To view the ASCIIMathML notation use Internet Explorer 6 +\nMathPlayer (free from www.dessci.com)\n\
  1957. or Firefox/Mozilla/Netscape");
  1958. else LMbody.insertBefore(nd,LMbody.childNodes[0]);
  1959. }
  1960. if (!LMnoMathML) {
  1961. frg = LMstrarr2docFrag(arr,n.nodeType==8);
  1962. var len = frg.childNodes.length;
  1963. n.parentNode.replaceChild(frg,n);
  1964. return len-1;
  1965. } else return 0;
  1966. }
  1967. }
  1968. } else return 0;
  1969. } else if (n.nodeName!="math") {
  1970. for (i=0; i<n.childNodes.length; i++)
  1971. i += LMprocessNodeR(n.childNodes[i], linebreaks);
  1972. }
  1973. return 0;
  1974. }
  1975. var tcnt = 0, dcnt = 0; //theorem and definition counters
  1976. function simpleLaTeXformatting(st) {
  1977. st = st.replace(/\$\$(.*?)\$\$/g,"<p align=\"center\">$\\displaystyle{$1}$</p>");
  1978. st = st.replace(/\\begin{(theorem|lemma|proposition|corollary)}((.|\n)*?)\\end{\1}/g,function(r,s,t){tcnt++; return "<b>"+s.charAt(0).toUpperCase()+s.slice(1)+" "+tcnt+".</b> <i>"+t.replace(/^\s*<\/?\w+\/?>|\s*<\/?\w+\/?>$/g,"")+"</i>"});
  1979. st = st.replace(/\\begin{(definition|example|remark|problem|exercise|conjecture|solution)}((.|\n)*?)\\end{\1}/g,function(r,s,t){dcnt++; return "<b>"+s.charAt(0).toUpperCase()+s.slice(1)+" "+dcnt+".</b> "+t.replace(/^\s*<\/?\w+\/?>|\s*<\/?\w+\/?>$/g,"")});
  1980. st = st.replace(/\\begin{proof}((.|\n)*?)\\end{proof}/g,function(s,t){return "<i>Proof:</i> "+t.replace(/^\s*<\/?\w+\/?>|\s*<\/?\w+\/?>$/g,"")+" &#x25A1;"});
  1981. st = st.replace(/\\emph{(.*?)}/g,"<em>$1</em>");
  1982. st = st.replace(/\\textbf{(.*?)}/g,"<b>$1</b>");
  1983. st = st.replace(/\\cite{(.*?)}/g,"[$1]");
  1984. st = st.replace(/\\chapter{(.*?)}/g,"<h2>$1</h2>");
  1985. st = st.replace(/\\section{(.*?)}(\s*<\/?(br|p)\s?\/?>)?/g,"<h3>$1</h3>");
  1986. st = st.replace(/\\subsection{(.*?)}/g,"<h4>$1</h4>");
  1987. st = st.replace(/\\begin{itemize}(\s*<\/?(br|p)\s?\/?>)?/g,"<ul>");
  1988. st = st.replace(/\\item\s((.|\n)*?)(?=(\\item|\\end))/g,"<li>$1</li>");
  1989. st = st.replace(/\\end{itemize}(\s*<\/?(br|p)\s?\/?>)?/g,"</ul>");
  1990. st = st.replace(/\\begin{enumerate}(\s*<\/?(br|p)\s?\/?>)?/g,"<ol>");
  1991. st = st.replace(/\\end{enumerate}(\s*<\/?(br|p)\s?\/?>)?/g,"</ol>");
  1992. st = st.replace(/\\item\[(.*?)]{(.*?)}/g,"<dt>$1</dt><dd>$2</dd>");
  1993. st = st.replace(/\\begin{description}/g,"<dl>");
  1994. st = st.replace(/\\end{description}/g,"</dl>");
  1995. st = st.replace(/\\newline\b/g,"<br/>");
  1996. st = st.replace(/\\newpage\b/g,"<br style=\"page-break-after:always;\">");
  1997. st = st.replace(/\\par\b/g,"<p>&nbsp;</p>");
  1998. st = st.replace(/\\bigskip/g,"<p style=\"margin-bottom:0.5in\">&nbsp;</p>");
  1999. st = st.replace(/\\medskip/g,"<p style=\"margin-bottom:0.3in\">&nbsp;</p>");
  2000. st = st.replace(/\\smallskip/g,"<p style=\"margin-bottom:0.15in\">&nbsp;</p>");
  2001. st = st.replace(/\\begin{center}(.*?)\\end{center}/g,"<p align=\"center\">$1</p>");
  2002. st = st.replace(/<embed\s+class\s?=\s?"ASCIIsvg"/g,"<embed class=\"ASCIIsvg\" src=\""+dsvglocation+"d.svg\" wmode=\"transparent\"");
  2003. st = st.replace(/(?:\\begin{a?graph}|agraph|\(:graph\s)((.|\n)*?)(?:\\end{a?graph}|enda?graph|:\))/g,function(s,t){return "<div><embed class=\"ASCIIsvg\" src=\""+dsvglocation+"d.svg\" wmode=\"transparent\" script=\'"+t.replace(/<\/?(br|p|pre)\s?\/?>/gi,"\n")+"\'/></div>"});
  2004. // st = st.replace(/\(:graph((.|\n)*?):\)/g,function(s,t){return "<div><embed class=\"ASCIIsvg\" src=\""+dsvglocation+"d.svg\" wmode=\"transparent\" script=\'"+t.replace(/<\/?(br|p|pre)\s?\/?>/gi,"\n")+"\'/></div>"});
  2005. st = st.replace(/insertASCIIMathCalculator/g,"<div class=\"ASCIIMathCalculator\"></div>");
  2006. return st
  2007. }
  2008. function LMprocessNode(n, linebreaks, spanclassLM) {
  2009. var frag,st;
  2010. if (spanclassLM!=null) {
  2011. frag = document.getElementsByTagName("span")
  2012. for (var i=0;i<frag.length;i++)
  2013. if (frag[i].className == "LM")
  2014. LMprocessNodeR(frag[i],linebreaks);
  2015. } else {
  2016. try {
  2017. st = n.innerHTML;
  2018. } catch(err) {}
  2019. var am = /amath|agraph/i.test(st);
  2020. if ((st==null || st.indexOf("\$ ")!=-1 || st.indexOf("\$<")!=-1 ||
  2021. st.indexOf("\\begin")!=-1 || am || st.slice(-1)=="$" ||
  2022. st.indexOf("\$\n")!=-1)&& !/edit-content|HTMLArea|wikiedit/.test(st)){
  2023. if (!avoidinnerHTML && translateLaTeXformatting)
  2024. st = simpleLaTeXformatting(st);
  2025. if (st!=null && am && !avoidinnerHTML) {
  2026. //alert(st)
  2027. st = st.replace(/<sup>(.*?)<\/sup>(\s|(\S))/gi,"^{$1} $3");
  2028. // st = st.replace(/<\/?font.*?>/gi,""); // do this only in amath...end
  2029. st = st.replace(/(Proof:)/g,"<i>$1</i>");
  2030. st = st.replace(/QED/g,"&#x25A1;");
  2031. st = st.replace(/(\\?end{?a?math}?)/ig,"<span></span>$1");
  2032. st = st.replace(/(\bamath|\\begin{a?math})/ig,"<span></span>$1");
  2033. st = st.replace(/([>\n])(Theorem|Lemma|Proposition|Corollary|Definition|Example|Remark|Problem|Exercise|Conjecture|Solution)(:|\W\W?(\w|\.)*?\W?:)/g,"$1<b>$2$3</b>");
  2034. }
  2035. st = st.replace(/%7E/g,"~");
  2036. if (!avoidinnerHTML) n.innerHTML = st;
  2037. LMprocessNodeR(n,linebreaks);
  2038. }
  2039. }
  2040. if (isIE) { //needed to match size and font of formula to surrounding text
  2041. frag = document.getElementsByTagName('math');
  2042. for (var i=0;i<frag.length;i++) frag[i].update()
  2043. }
  2044. }
  2045. var LMbody;
  2046. var LMnoMathML = false, LMtranslated = false;
  2047. function LMtranslate(spanclassLM) {
  2048. if (!LMtranslated) { // run this only once
  2049. LMtranslated = true;
  2050. LMinitSymbols();
  2051. LMbody = document.getElementsByTagName("body")[0];
  2052. var processN = document.getElementById(AMdocumentId);
  2053. //alert(processN)
  2054. LMprocessNode((processN!=null?processN:LMbody), false, spanclassLM);
  2055. }
  2056. }
  2057. if (isIE) { // avoid adding MathPlayer info explicitly to each webpage
  2058. document.write("<object id=\"mathplayer\"\
  2059. classid=\"clsid:32F66A20-7614-11D4-BD11-00104BD3F987\"></object>");
  2060. document.write("<?import namespace=\"m\" implementation=\"#mathplayer\"?>");
  2061. }
  2062. /* ASCIIsvg.js
  2063. ==============
  2064. JavaScript routines to dynamically generate Scalable Vector Graphics
  2065. using a mathematical xy-coordinate system (y increases upwards) and
  2066. very intuitive JavaScript commands (no programming experience required).
  2067. ASCIIsvg.js is good for learning math and illustrating online math texts.
  2068. Works with Internet Explorer+Adobe SVGviewer and SVG enabled Mozilla/Firefox.
  2069. Ver 1.2.9 July 31, 2007 (c) Peter Jipsen http://www.chapman.edu/~jipsen
  2070. Latest version at http://math.chapman.edu/~jipsen/math/pub/ASCIIsvg.js
  2071. If you use it on a webpage, please send the URL to jipsen@chapman.edu
  2072. This program is free software; you can redistribute it and/or modify
  2073. it under the terms of the GNU Lesser General Public License as published by
  2074. the Free Software Foundation; either version 2.1 of the License, or (at
  2075. your option) any later version.
  2076. This program is distributed in the hope that it will be useful,
  2077. but WITHOUT ANY WARRANTY; without even the implied warranty of
  2078. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  2079. General Public License (at http://www.gnu.org/license/lgpl.html)
  2080. for more details.*/
  2081. // you can change these
  2082. var checkIfSVGavailable = true;
  2083. var notifyIfNoSVG = true;
  2084. var alertIfNoSVG = false;
  2085. // global defaults used if not specified by graph (you can change these)
  2086. var defaultwidth = 300; defaultheight = 200; // in pixels
  2087. var defaultxmin = -5.5; defaultxmax = 5.5; // in usercoords
  2088. var defaultborder = 0; border = defaultborder; // in pixel
  2089. var defaultstrokewidth = "1"; // default line width in pixel
  2090. var defaultstroke = "blue"; // default line color
  2091. var defaultstrokeopacity = 1; // transparent = 0, solid =1
  2092. var defaultstrokedasharray = null; // "10,10" gives 10px long dashes
  2093. var defaultfill = "none"; // default fill color
  2094. var defaultfillopacity = 1; // transparent = 0, solid =1
  2095. var defaultfontstyle = "normal"; // default text shape normal|italic|inherit
  2096. var defaultfontfamily = "times"; // default font times|ariel|helvetica|...
  2097. var defaultfontsize = "16"; // default size (scaled automatically)
  2098. var defaultfontweight = "normal";// normal|bold|bolder|lighter|100|...|900
  2099. var defaultfontstroke = "none"; // default font outline color
  2100. var defaultfontfill = "none"; // default font color
  2101. var defaultmarker = "none"; // "dot" | "arrow" | "+" | "-" | "|"
  2102. var defaultendpoints = ""; // "c-d" where c is <|o|* and d is >|o|*
  2103. // global values used for all pictures (you can change these)
  2104. var showcoordinates = true;
  2105. var markerstrokewidth = "1";
  2106. var markerstroke = "black";
  2107. var markerfill = "yellow";
  2108. var markersize = 4;
  2109. var arrowfill = stroke;
  2110. var dotradius = 4;
  2111. var ticklength = 4;
  2112. var axesstroke = "black";
  2113. var gridstroke = "grey";
  2114. var backgroundstyle = "fill-opacity:0; fill:white";
  2115. var singlelettersitalic = true;
  2116. // internal variables (probably no need to change these)
  2117. var picturepos = null; // position of picture relative to top of HTML page
  2118. var xunitlength; // in pixels, used to convert to user coordinates
  2119. var yunitlength; // in pixels
  2120. var origin = [0,0]; // in pixels (default is bottom left corner)
  2121. var above = "above"; // shorthands (to avoid typing quotes)
  2122. var below = "below";
  2123. var left = "left";
  2124. var right = "right";
  2125. var aboveleft = "aboveleft";
  2126. var aboveright = "aboveright";
  2127. var belowleft = "belowleft";
  2128. var belowright = "belowright";
  2129. var xmin, xmax, ymin, ymax, xscl, yscl,
  2130. xgrid, ygrid, xtick, ytick, initialized;
  2131. var strokewidth, strokedasharray, stroke, fill, strokeopacity, fillopacity;
  2132. var fontstyle, fontfamily, fontsize, fontweight, fontstroke, fontfill;
  2133. var marker, endpoints, dynamic = {};
  2134. var picture, svgpicture, doc, width, height, a, b, c, d, i, n, p, t, x, y;
  2135. var isIE = document.createElementNS==null;
  2136. var cpi = "\u03C0", ctheta = "\u03B8"; // character for pi, theta
  2137. var log = function(x) { return ln(x)/ln(10) };
  2138. var pi = Math.PI, e = Math.E, ln = Math.log, sqrt = Math.sqrt;
  2139. var floor = Math.floor, ceil = Math.ceil, abs = Math.abs;
  2140. var sin = Math.sin, cos = Math.cos, tan = Math.tan;
  2141. var arcsin = Math.asin, arccos = Math.acos, arctan = Math.atan;
  2142. var sec = function(x) { return 1/Math.cos(x) };
  2143. var csc = function(x) { return 1/Math.sin(x) };
  2144. var cot = function(x) { return 1/Math.tan(x) };
  2145. var arcsec = function(x) { return arccos(1/x) };
  2146. var arccsc = function(x) { return arcsin(1/x) };
  2147. var arccot = function(x) { return arctan(1/x) };
  2148. var sinh = function(x) { return (Math.exp(x)-Math.exp(-x))/2 };
  2149. var cosh = function(x) { return (Math.exp(x)+Math.exp(-x))/2 };
  2150. var tanh =
  2151. function(x) { return (Math.exp(x)-Math.exp(-x))/(Math.exp(x)+Math.exp(-x)) };
  2152. var sech = function(x) { return 1/cosh(x) };
  2153. var csch = function(x) { return 1/sinh(x) };
  2154. var coth = function(x) { return 1/tanh(x) };
  2155. var arcsinh = function(x) { return ln(x+Math.sqrt(x*x+1)) };
  2156. var arccosh = function(x) { return ln(x+Math.sqrt(x*x-1)) };
  2157. var arctanh = function(x) { return ln((1+x)/(1-x))/2 };
  2158. var sech = function(x) { return 1/cosh(x) };
  2159. var csch = function(x) { return 1/sinh(x) };
  2160. var coth = function(x) { return 1/tanh(x) };
  2161. var arcsech = function(x) { return arccosh(1/x) };
  2162. var arccsch = function(x) { return arcsinh(1/x) };
  2163. var arccoth = function(x) { return arctanh(1/x) };
  2164. var sign = function(x) { return (x==0?0:(x<0?-1:1)) };
  2165. function factorial(x,n) { // Factorial function
  2166. if (n==null) n=1;
  2167. if (Math.abs(x-Math.round(x*1000000)/1000000)<1e-15)
  2168. x = Math.round(x*1000000)/1000000;
  2169. if (x-Math.floor(x)!=0) return NaN;
  2170. for (var i=x-n; i>0; i-=n) x*=i;
  2171. return (x<0?NaN:(x==0?1:x));
  2172. }
  2173. function C(x,k) { // Binomial coefficient function
  2174. var res=1;
  2175. for (var i=0; i<k; i++) res*=(x-i)/(k-i);
  2176. return res;
  2177. }
  2178. function chop(x,n) { // Truncate decimal number to n places after decimal point
  2179. if (n==null) n=0;
  2180. return Math.floor(x*Math.pow(10,n))/Math.pow(10,n);
  2181. }
  2182. function ran(a,b,n) { // Generate random number in [a,b] with n digits after .
  2183. if (n==null) n=0;
  2184. return chop((b+Math.pow(10,-n)-a)*Math.random()+a,n);
  2185. }
  2186. function myCreateElementXHTML(t) {
  2187. if (isIE) return document.createElement(t);
  2188. else return document.createElementNS("http://www.w3.org/1999/xhtml",t);
  2189. }
  2190. function myCreateElementSVG(t) {
  2191. if (isIE) return doc.createElement(t);
  2192. else return doc.createElementNS("http://www.w3.org/2000/svg",t);
  2193. }
  2194. function getElementsByClass(container, tagName, clsName){
  2195. var list = new Array(0);
  2196. var collection = container.getElementsByTagName(tagName);
  2197. for(var i = 0; i < collection.length; i++)
  2198. if(collection[i].className.slice(0,clsName.length)==clsName)
  2199. list[list.length] = collection[i];
  2200. return list;
  2201. }
  2202. function findPos(obj) { // top-left corner of obj on HTML page in pixel
  2203. var curleft = curtop = 0;
  2204. if (obj.offsetParent) {
  2205. curleft = obj.offsetLeft
  2206. curtop = obj.offsetTop
  2207. while (obj = obj.offsetParent) {
  2208. curleft += obj.offsetLeft
  2209. curtop += obj.offsetTop
  2210. }
  2211. }
  2212. return [curleft,curtop];
  2213. }
  2214. function isSVGavailable() {
  2215. var nd = myCreateElementXHTML("center");
  2216. nd.appendChild(document.createTextNode("To view the "));
  2217. var an = myCreateElementXHTML("a");
  2218. an.appendChild(document.createTextNode("ASCIIsvg"));
  2219. an.setAttribute("href","http://www.chapman.edu/~jipsen/asciisvg.html");
  2220. nd.appendChild(an);
  2221. nd.appendChild(document.createTextNode(" images use Internet Explorer 6+"));
  2222. an = myCreateElementXHTML("a");
  2223. an.appendChild(document.createTextNode("Adobe SVGviewer 3.02"));
  2224. an.setAttribute("href","http://www.adobe.com/svg");
  2225. nd.appendChild(an);
  2226. nd.appendChild(document.createTextNode(" or "));
  2227. an = myCreateElementXHTML("a");
  2228. an.appendChild(document.createTextNode("SVG enabled Mozilla/Firefox"));
  2229. an.setAttribute("href",
  2230. "http://www.chapman.edu/~jipsen/svg/svgenabledmozillafirefox.html");
  2231. nd.appendChild(an);
  2232. if (navigator.appName.slice(0,8)=="Netscape")
  2233. if (window['SVGElement']) return null;
  2234. else return nd;
  2235. else if (navigator.appName.slice(0,9)=="Microsoft")
  2236. try {
  2237. var oSVG=eval("new ActiveXObject('Adobe.SVGCtl.3');");
  2238. return null;
  2239. } catch (e) {
  2240. return nd;
  2241. }
  2242. else return nd;
  2243. }
  2244. function setText(st,id) { // add text to an existing node with given id
  2245. var node = document.getElementById(id);
  2246. if (node!=null)
  2247. if (node.childNodes.length!=0) node.childNodes[0].nodeValue = st;
  2248. else node.appendChild(document.createTextNode(st));
  2249. }
  2250. function getX(evt) { // return mouse x-coord in user coordinate system
  2251. var svgroot = evt.target.parentNode;
  2252. return (evt.clientX+(isIE?0:window.pageXOffset)-svgroot.getAttribute("left")-svgroot.getAttribute("ox"))/(svgroot.getAttribute("xunitlength")-0);
  2253. }
  2254. function getY(evt) { // return mouse y-coord in user coordinate system
  2255. var svgroot = evt.target.parentNode;
  2256. return (svgroot.getAttribute("height")-svgroot.getAttribute("oy")-(evt.clientY+(isIE?0:window.pageYOffset)-svgroot.getAttribute("top")))/(svgroot.getAttribute("yunitlength")-0);
  2257. }
  2258. function translateandeval(src) { //modify user input to JavaScript syntax
  2259. var errstr;
  2260. // replace plot(f(x),...) with plot("f(x)",...)
  2261. src = src.replace(/plot\(\x20*([^\"f\[][^\n\r;]+?)\,/g,"plot\(\"$1\",");
  2262. src = src.replace(/plot\(\x20*([^\"f\[][^\n\r;]+)\)/g,"plot(\"$1\")");
  2263. // replace (expr,expr) by [expr,expr] where expr has no (,) in it
  2264. src = src.replace(/([=(,]\x20*)\(([-a-z0-9./+*]+?),([-a-z0-9./+*]+?)\)/g,"$1[$2,$3]");
  2265. // insert * between digit and letter e.g. 2x --> 2*x
  2266. src = src.replace(/([0-9])([a-zA-Z])/g,"$1*$2");
  2267. src = src.replace(/\)([\(0-9a-zA-Z])/g,"\)*$1");
  2268. try {
  2269. with (Math) eval(src); // here the svgpicture object is created
  2270. } catch(err) {
  2271. if (err!="wait") {
  2272. if (typeof err=="object")
  2273. errstr = err.name+" "+err.message+" "+err.number+" "+err.description;
  2274. else errstr = err;
  2275. alert(errstr+"\n"+src)
  2276. }
  2277. }
  2278. }
  2279. function drawPictures() { // main routine; called after webpage has loaded
  2280. var src, id, dsvg, nd, node, ht, index, cols, arr, i, node2;
  2281. var pictures = document.getElementsByTagName("textarea");
  2282. for (index = 0; index<pictures.length; index++)
  2283. if (pictures[index].className=="ASCIIsvg"){
  2284. pictures[index].style.display="none"; // hide the textarea
  2285. }
  2286. var ASbody = document.getElementsByTagName("body")[0];
  2287. pictures = getElementsByClass(ASbody,"embed","ASCIIsvg");
  2288. var len = pictures.length;
  2289. if (checkIfSVGavailable) {
  2290. nd = isSVGavailable();
  2291. if (nd != null && notifyIfNoSVG && len>0)
  2292. if (alertIfNoSVG)
  2293. alert("To view the SVG pictures in Internet Explorer\n\
  2294. download the free Adobe SVGviewer from www.adobe.com/svg or\n\
  2295. use Firefox 2.0 or later");
  2296. else {
  2297. ASbody.insertBefore(nd,ASbody.childNodes[0]);
  2298. }
  2299. }
  2300. if (nd == null) {
  2301. for (index = 0; index < len; index++) {
  2302. width = null; height = null;
  2303. xmin = null; xmax = null; ymin = null; ymax = null;
  2304. xscl = null; xgrid = null; yscl = null; ygrid = null;
  2305. initialized = false;
  2306. picture = pictures[index]; // current picture object
  2307. src = picture.getAttribute("script"); // get the ASCIIsvg code
  2308. if (src==null) src = "";
  2309. // insert "axes()" if not present ******** experimental
  2310. if (!/axes\b|initPicture/.test(src)) {
  2311. var i = 0
  2312. while (/((yscl|ymax|ymin|xscl|xmax|xmin|\bwidth|\bheight)\s*=\s*-?\d*(\d\.|\.\d|\d)\d*\s*;?)/.test(src.slice(i))) i++;
  2313. src = (i==0?"axes(); "+src: src.slice(0,i)+src.slice(i).replace(/((scl|max|min|idth|eight)\s*=\s*-?\d*(\d\.|\.\d|\d)\d*\s*;?)/,"$1\naxes();"));
  2314. }
  2315. ht = picture.getAttribute("height");
  2316. if (isIE) {
  2317. picture.setAttribute("wmode","transparent");
  2318. //alert("*"+picture.getAttribute("src")+dsvglocation);
  2319. //adding d.svg dynamically greates problems in IE...
  2320. if (picture.getAttribute("src")=="") picture.setAttribute("src",dsvglocation+"d.svg");
  2321. }
  2322. if (document.getElementById("picture"+(index+1)+"mml")==null) {
  2323. picture.parentNode.style.position = "relative";
  2324. node = myCreateElementXHTML("div");
  2325. node.style.position = "absolute";
  2326. node.style.top = "0px";
  2327. node.style.left = "0px";
  2328. node.setAttribute("id","picture"+(index+1)+"mml");
  2329. picture.parentNode.insertBefore(node,picture.nextSibling);
  2330. }
  2331. if (ht==null) ht ="";
  2332. // if (ht!="") defaultborder = 25;
  2333. if (ht=="" || src=="")
  2334. if (document.getElementById("picture"+(index+1)+"input")==null) {
  2335. node = myCreateElementXHTML("textarea");
  2336. arr = src.split("\n");
  2337. cols = 0;
  2338. for (i=0;i<arr.length;i++) cols = Math.max(cols,arr[i].length);
  2339. node.setAttribute("rows",Math.min(10,arr.length)+1);
  2340. node.setAttribute("cols",Math.max(Math.min(60,cols),20)+5);
  2341. // node.setAttribute("style","display:block");
  2342. if (isIE) src = src.replace(/([^\r])\n/g,"$1\r");
  2343. node.appendChild(document.createTextNode(src));
  2344. if (src.indexOf("showcode()")==-1) node.style.display = "none";
  2345. node.setAttribute("id","picture"+(index+1)+"input");
  2346. picture.parentNode.insertBefore(node,picture.nextSibling);
  2347. picture.parentNode.insertBefore(myCreateElementXHTML("br"),node);
  2348. node2 = myCreateElementXHTML("button");
  2349. node2.setAttribute("id","picture"+(index+1)+"button");
  2350. if (isIE) node2.onclick = function() {updatePicture(this)};
  2351. else node2.setAttribute("onclick","updatePicture(this)");
  2352. node2.appendChild(document.createTextNode("Update"));
  2353. if (src.indexOf("showcode()")==-1) node2.style.display = "none";
  2354. picture.parentNode.insertBefore(node2,node);
  2355. picture.parentNode.insertBefore(myCreateElementXHTML("br"),node);
  2356. } else src = document.getElementById("picture"+(index+1)+"input").value;
  2357. id = picture.getAttribute("id");
  2358. dsvg = picture.getAttribute("src");
  2359. if (id == null || id == "") {
  2360. id = "picture"+(index+1);
  2361. picture.setAttribute("id",id);
  2362. }
  2363. translateandeval(src)
  2364. }
  2365. }
  2366. }
  2367. function setdefaults() { //called before each graph is evaluated
  2368. strokewidth = defaultstrokewidth;
  2369. stroke = defaultstroke;
  2370. strokeopacity = defaultstrokeopacity;
  2371. strokedasharray = defaultstrokedasharray;
  2372. fill = defaultfill;
  2373. fillopacity = defaultfillopacity;
  2374. fontstyle = defaultfontstyle;
  2375. fontfamily = defaultfontfamily;
  2376. fontsize = defaultfontsize;
  2377. fontweight = defaultfontweight;
  2378. fontstroke = defaultfontstroke;
  2379. fontfill = defaultfontfill;
  2380. marker = defaultmarker;
  2381. endpoints = defaultendpoints;
  2382. }
  2383. function switchTo(id) { // used by dynamic code to select appropriate graph
  2384. picture = document.getElementById(id);
  2385. width = picture.getAttribute("width")-0;
  2386. height = picture.getAttribute("height")-0;
  2387. setdefaults();
  2388. if ((picture.nodeName == "EMBED" || picture.nodeName == "embed") && isIE) {
  2389. svgpicture = picture.getSVGDocument().getElementById("root");
  2390. doc = picture.getSVGDocument();
  2391. } else {
  2392. svgpicture = picture;
  2393. doc = document;
  2394. }
  2395. xunitlength = svgpicture.getAttribute("xunitlength")-0;
  2396. yunitlength = svgpicture.getAttribute("yunitlength")-0;
  2397. xmin = svgpicture.getAttribute("xmin")-0;
  2398. xmax = svgpicture.getAttribute("xmax")-0;
  2399. ymin = svgpicture.getAttribute("ymin")-0;
  2400. ymax = svgpicture.getAttribute("ymax")-0;
  2401. origin = [svgpicture.getAttribute("ox")-0,svgpicture.getAttribute("oy")-0];
  2402. }
  2403. function updatePicture(obj) {
  2404. var node, src, id;
  2405. if (typeof obj=="object") id = obj.id.slice(0,-6);
  2406. else id = (typeof obj=="string"?obj:"picture"+(obj+1));
  2407. src = document.getElementById(id+"input").value;
  2408. xmin = null; xmax = null; ymin = null; ymax = null;
  2409. xscl = null; xgrid = null; yscl = null; ygrid = null;
  2410. initialized = false;
  2411. picture = document.getElementById(id);
  2412. // switchTo(id);
  2413. translateandeval(src)
  2414. }
  2415. function changepicturesize(evt,factor) {
  2416. var obj = evt.target;
  2417. var name = obj.parentNode.getAttribute("name");
  2418. var pic = document.getElementById(name);
  2419. var src = document.getElementById(name+"input").value;
  2420. src = src.replace(/width\s*=\s*\d+/,"width="+(factor*(pic.getAttribute("width")-0)));
  2421. src = src.replace(/height\s*=\s*\d+/,"height="+(factor*(pic.getAttribute("height")-0)));
  2422. document.getElementById(name+"input").value = src;
  2423. //alert(getKey(evt.keycode))
  2424. updatePicture(name);
  2425. }
  2426. var sinceFirstClick = 0; // ondblclick simulation from
  2427. var dblClkTimer; // http://www.enja.org/david/?cat=13 Thanks!
  2428. function timer() {
  2429. if(sinceFirstClick<60) {
  2430. sinceFirstClick++;
  2431. setTimeout("timer()",10);
  2432. } else {
  2433. clearTimeout(dblClkTimer);
  2434. dblClkTimer = "";
  2435. }
  2436. }
  2437. function mClick(evt) {
  2438. if(sinceFirstClick!=0) {
  2439. if(sinceFirstClick <= 40) {
  2440. if (evt.shiftKey) changepicturesize(evt,2);
  2441. else if (evt.altKey) changepicturesize(evt,.5);
  2442. else showHideCode(evt); // do this on dblclick
  2443. clearTimeout(dblClkTimer);
  2444. dblClkTimer = "";
  2445. } else {
  2446. clearTimeout(dblClkTimer);
  2447. sinceFirstClick = 0;
  2448. dblClkTimer = setTimeout("timer()",10);
  2449. }
  2450. } else {
  2451. sinceFirstClick = 0;
  2452. dblClkTimer = setTimeout("timer()",10);
  2453. }
  2454. }
  2455. function showHideCode(evt) { // called by onclick event
  2456. // if (evt.getDetail()==2) {//getDetail unfortunately not in Firefox
  2457. var obj=evt.target;
  2458. var name = obj.parentNode.getAttribute("name");
  2459. var node = document.getElementById(name+"input");
  2460. node.style.display = (node.style.display == "none"?"":"none");
  2461. var node = document.getElementById(name+"button");
  2462. node.style.display = (node.style.display == "none"?"":"none");
  2463. // }
  2464. }
  2465. function showcode() {} // do nothing
  2466. function setBorder(x) { border = x } //deprecate
  2467. function initPicture(x_min,x_max,y_min,y_max) { // set up the graph
  2468. // usually called by axes() or noaxes(), but can be used directly
  2469. if (!initialized) {
  2470. setdefaults();
  2471. initialized = true;
  2472. if (x_min!=null) xmin = x_min;
  2473. if (x_max!=null) xmax = x_max;
  2474. if (y_min!=null) ymin = y_min;
  2475. if (y_max!=null) ymax = y_max;
  2476. if (xmin==null) xmin = defaultxmin;
  2477. if (xmax==null) xmax = defaultxmax;
  2478. if (typeof xmin != "number" || typeof xmax != "number" || xmin >= xmax)
  2479. alert("Picture requires at least two numbers: xmin < xmax");
  2480. else if (y_max != null && (typeof y_min != "number" ||
  2481. typeof y_max != "number" || y_min >= y_max))
  2482. alert("initPicture(xmin,xmax,ymin,ymax) requires numbers ymin < ymax");
  2483. else {
  2484. if (width==null) {
  2485. width = picture.getAttribute("width");
  2486. if (width==null || width=="") width=defaultwidth;
  2487. }
  2488. picture.setAttribute("width",width);
  2489. if (height==null) {
  2490. height = picture.getAttribute("height");
  2491. if (height==null || height=="") height=defaultheight;
  2492. }
  2493. picture.setAttribute("height",height);
  2494. xunitlength = (width-2*border)/(xmax-xmin);
  2495. yunitlength = xunitlength;
  2496. //alert(xmin+" "+xmax+" "+ymin+" "+ymax)
  2497. if (ymin==null) {
  2498. origin = [-xmin*xunitlength+border,height/2];
  2499. ymin = -(height-2*border)/(2*yunitlength);
  2500. ymax = -ymin;
  2501. } else {
  2502. if (ymax!=null) yunitlength = (height-2*border)/(ymax-ymin);
  2503. else ymax = (height-2*border)/yunitlength + ymin;
  2504. origin = [-xmin*xunitlength+border,-ymin*yunitlength+border];
  2505. }
  2506. if (isIE) {
  2507. if (picture.FULLSCREEN==undefined) {
  2508. setTimeout('drawPictures()',50);
  2509. throw "wait";
  2510. }
  2511. svgpicture = picture.getSVGDocument().getElementById("root");
  2512. if (svgpicture==null) {
  2513. setTimeout('drawPictures()',50);
  2514. throw "wait";
  2515. }
  2516. svgpicture = picture.getSVGDocument().getElementById("root");
  2517. while (svgpicture.childNodes.length>0)
  2518. svgpicture.removeChild(svgpicture.lastChild);
  2519. svgpicture.setAttribute("width",width);
  2520. svgpicture.setAttribute("height",height);
  2521. svgpicture.setAttribute("name",picture.getAttribute("id"));
  2522. doc = picture.getSVGDocument();
  2523. var nd = document.getElementById(picture.getAttribute("id")+"mml");
  2524. if (nd!=null) // clear out MathML layer
  2525. while (nd.childNodes.length>0) nd.removeChild(nd.lastChild);
  2526. } else {
  2527. var qnode = document.createElementNS("http://www.w3.org/2000/svg","svg");
  2528. qnode.setAttribute("id",picture.getAttribute("id"));
  2529. qnode.setAttribute("name",picture.getAttribute("id"));
  2530. qnode.setAttribute("style","display:inline");
  2531. qnode.setAttribute("width",picture.getAttribute("width"));
  2532. qnode.setAttribute("height",picture.getAttribute("height"));
  2533. picturepos = findPos(picture);
  2534. qnode.setAttribute("left",picturepos[0]);
  2535. qnode.setAttribute("top",picturepos[1]);
  2536. // qnode.setAttribute("xmlns:xlink","http://www.w3.org/1999/xlink");
  2537. if (picture.parentNode!=null) {
  2538. picture.parentNode.replaceChild(qnode,picture);
  2539. } else {
  2540. svgpicture.parentNode.replaceChild(qnode,svgpicture);
  2541. }
  2542. svgpicture = qnode;
  2543. doc = document;
  2544. }
  2545. svgpicture.setAttribute("xunitlength",xunitlength);
  2546. svgpicture.setAttribute("yunitlength",yunitlength);
  2547. svgpicture.setAttribute("xmin",xmin);
  2548. svgpicture.setAttribute("xmax",xmax);
  2549. svgpicture.setAttribute("ymin",ymin);
  2550. svgpicture.setAttribute("ymax",ymax);
  2551. svgpicture.setAttribute("ox",origin[0]);
  2552. svgpicture.setAttribute("oy",origin[1]);
  2553. var node = myCreateElementSVG("rect");
  2554. node.setAttribute("x","0");
  2555. node.setAttribute("y","0");
  2556. node.setAttribute("width",width);
  2557. node.setAttribute("height",height);
  2558. node.setAttribute("style",backgroundstyle);
  2559. svgpicture.appendChild(node);
  2560. svgpicture.setAttribute("onmousemove","displayCoord(evt)");
  2561. svgpicture.setAttribute("onmouseout","removeCoord(evt)");
  2562. svgpicture.setAttribute("onclick","mClick(evt)");
  2563. node = myCreateElementSVG("text");
  2564. node.appendChild(doc.createTextNode(" "));
  2565. svgpicture.appendChild(node);
  2566. border = defaultborder;
  2567. }
  2568. }
  2569. }
  2570. //////////////////////////user graphics commands start/////////////////////////
  2571. function line(p,q,id,endpts) { // segment connecting points p,q (coordinates in units)
  2572. var node;
  2573. if (id!=null) node = doc.getElementById(id);
  2574. if (node==null) {
  2575. node = myCreateElementSVG("path");
  2576. node.setAttribute("id", id);
  2577. svgpicture.appendChild(node);
  2578. }
  2579. node.setAttribute("d","M"+(p[0]*xunitlength+origin[0])+","+
  2580. (height-p[1]*yunitlength-origin[1])+" "+
  2581. (q[0]*xunitlength+origin[0])+","+(height-q[1]*yunitlength-origin[1]));
  2582. node.setAttribute("stroke-width", strokewidth);
  2583. if (strokedasharray!=null)
  2584. node.setAttribute("stroke-dasharray", strokedasharray);
  2585. node.setAttribute("stroke", stroke);
  2586. node.setAttribute("fill", fill);
  2587. node.setAttribute("stroke-opacity", strokeopacity);
  2588. node.setAttribute("fill-opacity", fillopacity);
  2589. if (marker=="dot" || marker=="arrowdot") {
  2590. ASdot(p,markersize,markerstroke,markerfill);
  2591. if (marker=="arrowdot") arrowhead(p,q);
  2592. ASdot(q,markersize,markerstroke,markerfill);
  2593. } else if (marker=="arrow") arrowhead(p,q);
  2594. if (endpts==null && endpoints!="") endpts = endpoints;
  2595. if (endpts!=null) {
  2596. if (endpts.indexOf("<-") != -1) arrowhead(q,p);
  2597. if (endpts.indexOf("o-") != -1) dot(p, "open");
  2598. if (endpts.indexOf("*-") != -1) dot(p, "closed");
  2599. if (endpts.indexOf("->") != -1) arrowhead(p,q);
  2600. if (endpts.indexOf("-o") != -1) dot(q, "open");
  2601. if (endpts.indexOf("-*") != -1) dot(q, "closed");
  2602. }
  2603. }
  2604. function path(plist,id,c,endpts) {
  2605. if (c==null) c="";
  2606. var node, st, i;
  2607. if (id!=null) node = doc.getElementById(id);
  2608. if (node==null) {
  2609. node = myCreateElementSVG("path");
  2610. node.setAttribute("id", id);
  2611. svgpicture.appendChild(node);
  2612. }
  2613. if (typeof plist == "string") st = plist;
  2614. else {
  2615. st = "M";
  2616. st += (plist[0][0]*xunitlength+origin[0])+","+
  2617. (height-plist[0][1]*yunitlength-origin[1])+" "+c;
  2618. for (i=1; i<plist.length; i++)
  2619. st += (plist[i][0]*xunitlength+origin[0])+","+
  2620. (height-plist[i][1]*yunitlength-origin[1])+" ";
  2621. }
  2622. node.setAttribute("d", st);
  2623. node.setAttribute("stroke-width", strokewidth);
  2624. if (strokedasharray!=null)
  2625. node.setAttribute("stroke-dasharray", strokedasharray);
  2626. node.setAttribute("stroke", stroke);
  2627. node.setAttribute("fill", fill);
  2628. node.setAttribute("stroke-opacity", strokeopacity);
  2629. node.setAttribute("fill-opacity", fillopacity);
  2630. if (marker=="dot" || marker=="arrowdot")
  2631. for (i=0; i<plist.length; i++)
  2632. if (c!="C" && c!="T" || i!=1 && i!=2)
  2633. ASdot(plist[i],markersize,markerstroke,markerfill);
  2634. if (endpts==null && endpoints!="") endpts = endpoints;
  2635. if (endpts!=null) {
  2636. if (endpts.indexOf("<-") != -1) arrowhead(plist[1],plist[0]);
  2637. if (endpts.indexOf("o-") != -1) dot(plist[0], "open");
  2638. if (endpts.indexOf("*-") != -1) dot(plist[0], "closed");
  2639. if (endpts.indexOf("->") != -1) arrowhead(plist[plist.length-2],plist[plist.length-1]);
  2640. if (endpts.indexOf("-o") != -1) dot(plist[plist.length-1], "open");
  2641. if (endpts.indexOf("-*") != -1) dot(plist[plist.length-1], "closed");
  2642. }
  2643. }
  2644. function curve(plist,id,endpts) {
  2645. path(plist,id,"T",endpts);
  2646. }
  2647. function vector(p,q,id) {
  2648. line(p,q,id,"","->");
  2649. }
  2650. function circle(center,radius,id) { // coordinates in units
  2651. var node;
  2652. if (id!=null) node = doc.getElementById(id);
  2653. if (node==null) {
  2654. node = myCreateElementSVG("circle");
  2655. node.setAttribute("id", id);
  2656. svgpicture.appendChild(node);
  2657. }
  2658. node.setAttribute("cx",center[0]*xunitlength+origin[0]);
  2659. node.setAttribute("cy",height-center[1]*yunitlength-origin[1]);
  2660. node.setAttribute("r",radius*xunitlength);
  2661. node.setAttribute("stroke-width", strokewidth);
  2662. node.setAttribute("stroke", stroke);
  2663. node.setAttribute("fill", fill);
  2664. node.setAttribute("stroke-opacity", strokeopacity);
  2665. node.setAttribute("fill-opacity", fillopacity);
  2666. }
  2667. function loop(p,d,id) {
  2668. // d is a direction vector e.g. [1,0] means loop starts in that direction
  2669. if (d==null) d=[1,0];
  2670. path([p,[p[0]+d[0],p[1]+d[1]],[p[0]-d[1],p[1]+d[0]],p],id,"C");
  2671. if (marker=="arrow" || marker=="arrowdot")
  2672. arrowhead([p[0]+Math.cos(1.4)*d[0]-Math.sin(1.4)*d[1],
  2673. p[1]+Math.sin(1.4)*d[0]+Math.cos(1.4)*d[1]],p);
  2674. }
  2675. function arc(start,end,radius,id) { // coordinates in units
  2676. var node, v;
  2677. //alert([fill, stroke, origin, xunitlength, yunitlength, height])
  2678. if (id!=null) node = doc.getElementById(id);
  2679. if (radius==null) {
  2680. v=[end[0]-start[0],end[1]-start[1]];
  2681. radius = Math.sqrt(v[0]*v[0]+v[1]*v[1]);
  2682. }
  2683. if (node==null) {
  2684. node = myCreateElementSVG("path");
  2685. node.setAttribute("id", id);
  2686. svgpicture.appendChild(node);
  2687. }
  2688. node.setAttribute("d","M"+(start[0]*xunitlength+origin[0])+","+
  2689. (height-start[1]*yunitlength-origin[1])+" A"+radius*xunitlength+","+
  2690. radius*yunitlength+" 0 0,0 "+(end[0]*xunitlength+origin[0])+","+
  2691. (height-end[1]*yunitlength-origin[1]));
  2692. node.setAttribute("stroke-width", strokewidth);
  2693. node.setAttribute("stroke", stroke);
  2694. node.setAttribute("fill", fill);
  2695. node.setAttribute("stroke-opacity", strokeopacity);
  2696. node.setAttribute("fill-opacity", fillopacity);
  2697. if (marker=="arrow" || marker=="arrowdot") {
  2698. u = [(end[1]-start[1])/4,(start[0]-end[0])/4];
  2699. v = [(end[0]-start[0])/2,(end[1]-start[1])/2];
  2700. //alert([u,v])
  2701. v = [start[0]+v[0]+u[0],start[1]+v[1]+u[1]];
  2702. } else v=[start[0],start[1]];
  2703. if (marker=="dot" || marker=="arrowdot") {
  2704. ASdot(start,markersize,markerstroke,markerfill);
  2705. if (marker=="arrowdot") arrowhead(v,end);
  2706. ASdot(end,markersize,markerstroke,markerfill);
  2707. } else if (marker=="arrow") arrowhead(v,end);
  2708. }
  2709. function sector(center,start,end,id) { // center,start,end should be isoceles
  2710. var rx = start[0]-center[0], ry = start[1]-center[1];
  2711. arc(start,end,Math.sqrt(rx*rx+ry*ry),id+"arc");
  2712. path([end,center,start],id+"path");
  2713. }
  2714. function ellipse(center,rx,ry,id) { // coordinates in units
  2715. var node;
  2716. if (id!=null) node = doc.getElementById(id);
  2717. if (node==null) {
  2718. node = myCreateElementSVG("ellipse");
  2719. node.setAttribute("id", id);
  2720. svgpicture.appendChild(node);
  2721. }
  2722. node.setAttribute("cx",center[0]*xunitlength+origin[0]);
  2723. node.setAttribute("cy",height-center[1]*yunitlength-origin[1]);
  2724. node.setAttribute("rx",rx*xunitlength);
  2725. node.setAttribute("ry",ry*yunitlength);
  2726. node.setAttribute("stroke-width", strokewidth);
  2727. node.setAttribute("stroke", stroke);
  2728. node.setAttribute("fill", fill);
  2729. node.setAttribute("stroke-opacity", strokeopacity);
  2730. node.setAttribute("fill-opacity", fillopacity);
  2731. }
  2732. function triangle(p,q,r,id) {
  2733. path([p,q,r,p],id)
  2734. }
  2735. function rect(p,q,id,rx,ry) { // opposite corners in units, rounded by radii
  2736. var node;
  2737. if (id!=null) node = doc.getElementById(id);
  2738. if (node==null) {
  2739. node = myCreateElementSVG("rect");
  2740. node.setAttribute("id", id);
  2741. svgpicture.appendChild(node);
  2742. }
  2743. node.setAttribute("x",p[0]*xunitlength+origin[0]);
  2744. node.setAttribute("y",height-q[1]*yunitlength-origin[1]);
  2745. node.setAttribute("width",(q[0]-p[0])*xunitlength);
  2746. node.setAttribute("height",(q[1]-p[1])*yunitlength);
  2747. if (rx!=null) node.setAttribute("rx",rx*xunitlength);
  2748. if (ry!=null) node.setAttribute("ry",ry*yunitlength);
  2749. node.setAttribute("stroke-width", strokewidth);
  2750. node.setAttribute("stroke", stroke);
  2751. node.setAttribute("fill", fill);
  2752. node.setAttribute("stroke-opacity", strokeopacity);
  2753. node.setAttribute("fill-opacity", fillopacity);
  2754. }
  2755. function text(p,st,pos,id,fontsty) {
  2756. var dnode, node, dx = 0, dy = fontsize/3;
  2757. if (/(`|\$)/.test(st)) { // layer for ASCIIMathML and LaTeXMathML
  2758. dnode = document.getElementById(svgpicture.getAttribute("name")+"mml");
  2759. if (dnode!=null) {
  2760. if (id!=null) node = document.getElementById(id);
  2761. if (node==null) {
  2762. //alert(dnode.childNodes.length)
  2763. node = myCreateElementXHTML("div");
  2764. node.setAttribute("id", id);
  2765. node.style.position = "absolute";
  2766. dnode.appendChild(node);
  2767. }
  2768. while (node.childNodes.length>0) node.removeChild(node.lastChild);
  2769. node.appendChild(document.createTextNode(st));
  2770. // node.lastChild.nodeValue = st;
  2771. node.style.left = ""+(p[0]*xunitlength+origin[0])+"px";
  2772. node.style.top = ""+(height-p[1]*yunitlength-origin[1])+"px";
  2773. if (/`/.test(st)) AMprocessNode(node); else LMprocessNode(node);
  2774. dx = -node.offsetWidth/2;
  2775. dy = -node.offsetHeight/2;
  2776. if (pos!=null) {
  2777. if (/above/.test(pos)) dy = -node.offsetHeight;
  2778. if (/below/.test(pos)) dy = 0;
  2779. if (/right/.test(pos)) dx = 0;
  2780. if ( /left/.test(pos)) dx = -node.offsetWidth;
  2781. }
  2782. node.style.left = ""+(p[0]*xunitlength+origin[0]+dx)+"px";
  2783. node.style.top = ""+(height-p[1]*yunitlength-origin[1]+dy)+"px";
  2784. }
  2785. return p;
  2786. }
  2787. var textanchor = "middle"; // regular text goes into SVG
  2788. if (pos!=null) {
  2789. if (/above/.test(pos)) dy = -fontsize/2;
  2790. if (/below/.test(pos)) dy = fontsize-0;
  2791. if (/right/.test(pos)) {textanchor = "start"; dx = fontsize/4;}
  2792. if ( /left/.test(pos)) {textanchor = "end"; dx = -fontsize/4;}
  2793. }
  2794. if (id!=null) node = doc.getElementById(id);
  2795. if (node==null) {
  2796. node = myCreateElementSVG("text");
  2797. node.setAttribute("id", id);
  2798. svgpicture.appendChild(node);
  2799. node.appendChild(doc.createTextNode(st));
  2800. }
  2801. while (node.childNodes.length>1) node.removeChild(node.lastChild);
  2802. // node.appendChild(document.createTextNode("\xA0"+st+"\xA0"));
  2803. //alert("here");
  2804. node.lastChild.nodeValue = "\xA0"+st+"\xA0";
  2805. node.setAttribute("x",p[0]*xunitlength+origin[0]+dx);
  2806. node.setAttribute("y",height-p[1]*yunitlength-origin[1]+dy);
  2807. node.setAttribute("font-style",(fontsty!=null?fontsty:
  2808. (st.search(/^[a-zA-Z]$/)!=-1?"italic":fontstyle)));
  2809. node.setAttribute("font-family",fontfamily);
  2810. node.setAttribute("font-size",fontsize);
  2811. node.setAttribute("font-weight",fontweight);
  2812. node.setAttribute("text-anchor",textanchor);
  2813. if (fontstroke!="none") node.setAttribute("stroke",fontstroke);
  2814. if (fontfill!="none") node.setAttribute("fill",fontfill);
  2815. return p;
  2816. }
  2817. function mtext(p,st,pos,fontsty) { // method for updating text on an svg
  2818. // "this" is the text object or the svgpicture object
  2819. var textanchor = "middle";
  2820. var dx = 0; var dy = fontsize/3;
  2821. if (pos!=null) {
  2822. if (pos.slice(0,5)=="above") dy = -fontsize/2;
  2823. if (pos.slice(0,5)=="below") dy = fontsize-0;
  2824. if (pos.slice(0,5)=="right" || pos.slice(5,10)=="right") {
  2825. textanchor = "start";
  2826. dx = fontsize/2;
  2827. }
  2828. if (pos.slice(0,4)=="left" || pos.slice(5,9)=="left") {
  2829. textanchor = "end";
  2830. dx = -fontsize/2;
  2831. }
  2832. }
  2833. var node = this;
  2834. if (this.nodeName=="svg") {
  2835. node = myCreateElementSVG("text");
  2836. this.appendChild(node);
  2837. node.appendChild(doc.createTextNode(st));
  2838. }
  2839. node.lastChild.nodeValue = st;
  2840. node.setAttribute("x",p[0]+dx);
  2841. node.setAttribute("y",p[1]+dy);
  2842. node.setAttribute("font-style",(fontsty!=null?fontsty:fontstyle));
  2843. node.setAttribute("font-family",fontfamily);
  2844. node.setAttribute("font-size",fontsize);
  2845. node.setAttribute("font-weight",fontweight);
  2846. node.setAttribute("text-anchor",textanchor);
  2847. if (fontstroke!="none") node.setAttribute("stroke",fontstroke);
  2848. if (fontfill!="none") node.setAttribute("fill",fontfill);
  2849. }
  2850. function image(imgurl,p,w,h,id) { // not working yet
  2851. var node;
  2852. if (id!=null) node = doc.getElementById(id);
  2853. if (node==null) {
  2854. node = myCreateElementSVG("image");
  2855. node.setAttribute("id", id);
  2856. svgpicture.appendChild(node);
  2857. }
  2858. node.setAttribute("x",p[0]*xunitlength+origin[0]);
  2859. node.setAttribute("y",height-p[1]*yunitlength-origin[1]);
  2860. node.setAttribute("width",w);
  2861. node.setAttribute("height",h);
  2862. node.setAttribute("xlink:href", imgurl);
  2863. }
  2864. function ASdot(center,radius,s,f) { // coordinates in units, radius in pixel
  2865. if (s==null) s = stroke; if (f==null) f = fill;
  2866. var node = myCreateElementSVG("circle");
  2867. node.setAttribute("cx",center[0]*xunitlength+origin[0]);
  2868. node.setAttribute("cy",height-center[1]*yunitlength-origin[1]);
  2869. node.setAttribute("r",radius);
  2870. node.setAttribute("stroke-width", strokewidth);
  2871. node.setAttribute("stroke", s);
  2872. node.setAttribute("fill", f);
  2873. svgpicture.appendChild(node);
  2874. }
  2875. function dot(center, typ, label, pos, id) {
  2876. var node;
  2877. var cx = center[0]*xunitlength+origin[0];
  2878. var cy = height-center[1]*yunitlength-origin[1];
  2879. if (id!=null) node = doc.getElementById(id);
  2880. if (typ=="+" || typ=="-" || typ=="|") {
  2881. if (node==null) {
  2882. node = myCreateElementSVG("path");
  2883. node.setAttribute("id", id);
  2884. svgpicture.appendChild(node);
  2885. }
  2886. if (typ=="+") {
  2887. node.setAttribute("d",
  2888. " M "+(cx-ticklength)+" "+cy+" L "+(cx+ticklength)+" "+cy+
  2889. " M "+cx+" "+(cy-ticklength)+" L "+cx+" "+(cy+ticklength));
  2890. node.setAttribute("stroke-width", .5);
  2891. node.setAttribute("stroke", axesstroke);
  2892. } else {
  2893. if (typ=="-") node.setAttribute("d",
  2894. " M "+(cx-ticklength)+" "+cy+" L "+(cx+ticklength)+" "+cy);
  2895. else node.setAttribute("d",
  2896. " M "+cx+" "+(cy-ticklength)+" L "+cx+" "+(cy+ticklength));
  2897. node.setAttribute("stroke-width", strokewidth);
  2898. node.setAttribute("stroke", stroke);
  2899. }
  2900. } else {
  2901. if (node==null) {
  2902. node = myCreateElementSVG("circle");
  2903. node.setAttribute("id", id);
  2904. svgpicture.appendChild(node);
  2905. }
  2906. node.setAttribute("cx",cx);
  2907. node.setAttribute("cy",cy);
  2908. node.setAttribute("r",dotradius);
  2909. node.setAttribute("stroke-width", strokewidth);
  2910. node.setAttribute("stroke", stroke);
  2911. node.setAttribute("fill", (typ=="open"?"white":
  2912. (typ=="closed"?stroke:markerfill)));
  2913. }
  2914. if (label!=null)
  2915. text(center,label,(pos==null?"below":pos),(id==null?id:id+"label"))
  2916. }
  2917. point = dot; //alternative name
  2918. function arrowhead(p,q) { // draw arrowhead at q (in units) add size param
  2919. var up;
  2920. var v = [p[0]*xunitlength+origin[0],height-p[1]*yunitlength-origin[1]];
  2921. var w = [q[0]*xunitlength+origin[0],height-q[1]*yunitlength-origin[1]];
  2922. var u = [w[0]-v[0],w[1]-v[1]];
  2923. var d = Math.sqrt(u[0]*u[0]+u[1]*u[1]);
  2924. if (d > 0.00000001) {
  2925. u = [u[0]/d, u[1]/d];
  2926. up = [-u[1],u[0]];
  2927. var node = myCreateElementSVG("path");
  2928. node.setAttribute("d","M "+(w[0]-15*u[0]-4*up[0])+" "+
  2929. (w[1]-15*u[1]-4*up[1])+" L "+(w[0]-3*u[0])+" "+(w[1]-3*u[1])+" L "+
  2930. (w[0]-15*u[0]+4*up[0])+" "+(w[1]-15*u[1]+4*up[1])+" z");
  2931. node.setAttribute("stroke-width", markerstrokewidth);
  2932. node.setAttribute("stroke", stroke); /*was markerstroke*/
  2933. node.setAttribute("fill", stroke); /*was arrowfill*/
  2934. node.setAttribute("stroke-opacity", strokeopacity);
  2935. node.setAttribute("fill-opacity", fillopacity);
  2936. svgpicture.appendChild(node);
  2937. }
  2938. }
  2939. function chopZ(st) {
  2940. var k = st.indexOf(".");
  2941. if (k==-1) return st;
  2942. for (var i=st.length-1; i>k && st.charAt(i)=="0"; i--);
  2943. if (i==k) i--;
  2944. return st.slice(0,i+1);
  2945. }
  2946. function grid(dx,dy) { // for backward compatibility
  2947. axes(dx,dy,null,dx,dy)
  2948. }
  2949. function noaxes() {
  2950. if (!initialized) initPicture();
  2951. }
  2952. function axes(dx,dy,labels,gdx,gdy) {
  2953. //xscl=x is equivalent to xtick=x; xgrid=x; labels=true;
  2954. var x, y, ldx, ldy, lx, ly, lxp, lyp, pnode, st;
  2955. if (!initialized) initPicture();
  2956. if (typeof dx=="string") { labels = dx; dx = null; }
  2957. if (typeof dy=="string") { gdx = dy; dy = null; }
  2958. if (xscl!=null) {dx = xscl; gdx = xscl; labels = dx}
  2959. if (yscl!=null) {dy = yscl; gdy = yscl}
  2960. if (xtick!=null) {dx = xtick}
  2961. if (ytick!=null) {dy = ytick}
  2962. dx = (dx==null?xunitlength:dx*xunitlength);
  2963. dy = (dy==null?dx:dy*yunitlength);
  2964. fontsize = Math.min(dx/2,dy/2,16); //alert(fontsize)
  2965. ticklength = fontsize/4;
  2966. if (xgrid!=null) gdx = xgrid;
  2967. if (ygrid!=null) gdy = ygrid;
  2968. if (gdx!=null) {
  2969. gdx = (typeof gdx=="string"?dx:gdx*xunitlength);
  2970. gdy = (gdy==null?dy:gdy*yunitlength);
  2971. pnode = myCreateElementSVG("path");
  2972. st="";
  2973. for (x = origin[0]; x<width; x = x+gdx)
  2974. st += " M"+x+",0"+" "+x+","+height;
  2975. for (x = origin[0]-gdx; x>0; x = x-gdx)
  2976. st += " M"+x+",0"+" "+x+","+height;
  2977. for (y = height-origin[1]; y<height; y = y+gdy)
  2978. st += " M0,"+y+" "+width+","+y;
  2979. for (y = height-origin[1]-gdy; y>0; y = y-gdy)
  2980. st += " M0,"+y+" "+width+","+y;
  2981. pnode.setAttribute("d",st);
  2982. pnode.setAttribute("stroke-width", .5);
  2983. pnode.setAttribute("stroke", gridstroke);
  2984. pnode.setAttribute("fill", fill);
  2985. svgpicture.appendChild(pnode);
  2986. }
  2987. pnode = myCreateElementSVG("path");
  2988. st="M0,"+(height-origin[1])+" "+width+","+
  2989. (height-origin[1])+" M"+origin[0]+",0 "+origin[0]+","+height;
  2990. for (x = origin[0]+dx; x<width; x = x+dx)
  2991. st += " M"+x+","+(height-origin[1]+ticklength)+" "+x+","+
  2992. (height-origin[1]-ticklength);
  2993. for (x = origin[0]-dx; x>0; x = x-dx)
  2994. st += " M"+x+","+(height-origin[1]+ticklength)+" "+x+","+
  2995. (height-origin[1]-ticklength);
  2996. for (y = height-origin[1]+dy; y<height; y = y+dy)
  2997. st += " M"+(origin[0]+ticklength)+","+y+" "+(origin[0]-ticklength)+","+y;
  2998. for (y = height-origin[1]-dy; y>0; y = y-dy)
  2999. st += " M"+(origin[0]+ticklength)+","+y+" "+(origin[0]-ticklength)+","+y;
  3000. if (labels!=null) with (Math) {
  3001. ldx = dx/xunitlength;
  3002. ldy = dy/yunitlength;
  3003. lx = (xmin>0 || xmax<0?xmin:0);
  3004. ly = (ymin>0 || ymax<0?ymin:0);
  3005. lxp = (ly==0?"below":"above");
  3006. lyp = (lx==0?"left":"right");
  3007. var ddx = floor(1.1-log(ldx)/log(10))+1;
  3008. var ddy = floor(1.1-log(ldy)/log(10))+1;
  3009. for (x = ldx; x<=xmax; x = x+ldx)
  3010. text([x,ly],chopZ(x.toFixed(ddx)),lxp);
  3011. for (x = -ldx; xmin<=x; x = x-ldx)
  3012. text([x,ly],chopZ(x.toFixed(ddx)),lxp);
  3013. for (y = ldy; y<=ymax; y = y+ldy)
  3014. text([lx,y],chopZ(y.toFixed(ddy)),lyp);
  3015. for (y = -ldy; ymin<=y; y = y-ldy)
  3016. text([lx,y],chopZ(y.toFixed(ddy)),lyp);
  3017. }
  3018. fontsize = defaultfontsize;
  3019. pnode.setAttribute("d",st);
  3020. pnode.setAttribute("stroke-width", .5);
  3021. pnode.setAttribute("stroke", axesstroke);
  3022. pnode.setAttribute("fill", fill);
  3023. pnode.setAttribute("stroke-opacity", strokeopacity);
  3024. pnode.setAttribute("fill-opacity", fillopacity);
  3025. svgpicture.appendChild(pnode);
  3026. }
  3027. function mathjs(st) {
  3028. //translate a math formula to js function notation
  3029. // a^b --> pow(a,b)
  3030. // na --> n*a
  3031. // (...)d --> (...)*d
  3032. // n! --> factorial(n)
  3033. // sin^-1 --> arcsin etc.
  3034. //while ^ in string, find term on left and right
  3035. //slice and concat new formula string
  3036. st = st.replace(/\s/g,"");
  3037. if (st.indexOf("^-1")!=-1) {
  3038. st = st.replace(/sin\^-1/g,"arcsin");
  3039. st = st.replace(/cos\^-1/g,"arccos");
  3040. st = st.replace(/tan\^-1/g,"arctan");
  3041. st = st.replace(/sec\^-1/g,"arcsec");
  3042. st = st.replace(/csc\^-1/g,"arccsc");
  3043. st = st.replace(/cot\^-1/g,"arccot");
  3044. st = st.replace(/sinh\^-1/g,"arcsinh");
  3045. st = st.replace(/cosh\^-1/g,"arccosh");
  3046. st = st.replace(/tanh\^-1/g,"arctanh");
  3047. st = st.replace(/sech\^-1/g,"arcsech");
  3048. st = st.replace(/csch\^-1/g,"arccsch");
  3049. st = st.replace(/coth\^-1/g,"arccoth");
  3050. }
  3051. st = st.replace(/^e$/g,"(Math.E)");
  3052. st = st.replace(/^e([^a-zA-Z])/g,"(Math.E)$1");
  3053. st = st.replace(/([^a-zA-Z])e/g,"$1(Math.E)");
  3054. // st = st.replace(/([^a-zA-Z])e([^a-zA-Z])/g,"$1(Math.E)$2");
  3055. st = st.replace(/([0-9])([\(a-zA-Z])/g,"$1*$2");
  3056. st = st.replace(/\)([\(0-9a-zA-Z])/g,"\)*$1");
  3057. var i,j,k, ch, nested;
  3058. while ((i=st.indexOf("^"))!=-1) {
  3059. //find left argument
  3060. if (i==0) return "Error: missing argument";
  3061. j = i-1;
  3062. ch = st.charAt(j);
  3063. if (ch>="0" && ch<="9") {// look for (decimal) number
  3064. j--;
  3065. while (j>=0 && (ch=st.charAt(j))>="0" && ch<="9") j--;
  3066. if (ch==".") {
  3067. j--;
  3068. while (j>=0 && (ch=st.charAt(j))>="0" && ch<="9") j--;
  3069. }
  3070. } else if (ch==")") {// look for matching opening bracket and function name
  3071. nested = 1;
  3072. j--;
  3073. while (j>=0 && nested>0) {
  3074. ch = st.charAt(j);
  3075. if (ch=="(") nested--;
  3076. else if (ch==")") nested++;
  3077. j--;
  3078. }
  3079. while (j>=0 && (ch=st.charAt(j))>="a" && ch<="z" || ch>="A" && ch<="Z")
  3080. j--;
  3081. } else if (ch>="a" && ch<="z" || ch>="A" && ch<="Z") {// look for variable
  3082. j--;
  3083. while (j>=0 && (ch=st.charAt(j))>="a" && ch<="z" || ch>="A" && ch<="Z")
  3084. j--;
  3085. } else {
  3086. return "Error: incorrect syntax in "+st+" at position "+j;
  3087. }
  3088. //find right argument
  3089. if (i==st.length-1) return "Error: missing argument";
  3090. k = i+1;
  3091. ch = st.charAt(k);
  3092. if (ch>="0" && ch<="9" || ch=="-") {// look for signed (decimal) number
  3093. k++;
  3094. while (k<st.length && (ch=st.charAt(k))>="0" && ch<="9") k++;
  3095. if (ch==".") {
  3096. k++;
  3097. while (k<st.length && (ch=st.charAt(k))>="0" && ch<="9") k++;
  3098. }
  3099. } else if (ch=="(") {// look for matching closing bracket and function name
  3100. nested = 1;
  3101. k++;
  3102. while (k<st.length && nested>0) {
  3103. ch = st.charAt(k);
  3104. if (ch=="(") nested++;
  3105. else if (ch==")") nested--;
  3106. k++;
  3107. }
  3108. } else if (ch>="a" && ch<="z" || ch>="A" && ch<="Z") {// look for variable
  3109. k++;
  3110. while (k<st.length && (ch=st.charAt(k))>="a" && ch<="z" ||
  3111. ch>="A" && ch<="Z") k++;
  3112. } else {
  3113. return "Error: incorrect syntax in "+st+" at position "+k;
  3114. }
  3115. st = st.slice(0,j+1)+"Math.pow("+st.slice(j+1,i)+","+st.slice(i+1,k)+")"+
  3116. st.slice(k);
  3117. }
  3118. while ((i=st.indexOf("!"))!=-1) {
  3119. //find left argument
  3120. if (i==0) return "Error: missing argument";
  3121. j = i-1;
  3122. ch = st.charAt(j);
  3123. if (ch>="0" && ch<="9") {// look for (decimal) number
  3124. j--;
  3125. while (j>=0 && (ch=st.charAt(j))>="0" && ch<="9") j--;
  3126. if (ch==".") {
  3127. j--;
  3128. while (j>=0 && (ch=st.charAt(j))>="0" && ch<="9") j--;
  3129. }
  3130. } else if (ch==")") {// look for matching opening bracket and function name
  3131. nested = 1;
  3132. j--;
  3133. while (j>=0 && nested>0) {
  3134. ch = st.charAt(j);
  3135. if (ch=="(") nested--;
  3136. else if (ch==")") nested++;
  3137. j--;
  3138. }
  3139. while (j>=0 && (ch=st.charAt(j))>="a" && ch<="z" || ch>="A" && ch<="Z")
  3140. j--;
  3141. } else if (ch>="a" && ch<="z" || ch>="A" && ch<="Z") {// look for variable
  3142. j--;
  3143. while (j>=0 && (ch=st.charAt(j))>="a" && ch<="z" || ch>="A" && ch<="Z")
  3144. j--;
  3145. } else {
  3146. return "Error: incorrect syntax in "+st+" at position "+j;
  3147. }
  3148. st = st.slice(0,j+1)+"factorial("+st.slice(j+1,i)+")"+st.slice(i+1);
  3149. }
  3150. return st;
  3151. }
  3152. function plot(fun,x_min,x_max,points,id,endpts) {
  3153. var pth = [];
  3154. var f = function(x) { return x }, g = fun;
  3155. var name = null;
  3156. if (typeof fun=="string")
  3157. eval("g = function(x){ with(Math) return "+mathjs(fun)+" }");
  3158. else if (typeof fun=="object") {
  3159. eval("f = function(t){ with(Math) return "+mathjs(fun[0])+" }");
  3160. eval("g = function(t){ with(Math) return "+mathjs(fun[1])+" }");
  3161. }
  3162. if (typeof x_min=="string") { name = x_min; x_min = xmin }
  3163. else name = id;
  3164. var min = (x_min==null?xmin:x_min);
  3165. var max = (x_max==null?xmax:x_max);
  3166. var inc = max-min-0.000001*(max-min);
  3167. inc = (points==null?inc/200:inc/points);
  3168. var gt;
  3169. //alert(typeof g(min))
  3170. for (var t = min; t <= max; t += inc) {
  3171. gt = g(t);
  3172. if (!(isNaN(gt)||Math.abs(gt)=="Infinity")) pth[pth.length] = [f(t), gt];
  3173. }
  3174. path(pth,name,null,endpts);
  3175. return p;
  3176. }
  3177. // make polar plot
  3178. // make Riemann sums
  3179. function slopefield(fun,dx,dy) {
  3180. var g = fun;
  3181. if (typeof fun=="string")
  3182. eval("g = function(x,y){ with(Math) return "+mathjs(fun)+" }");
  3183. var gxy,x,y,u,v,dz;
  3184. if (dx==null) dx=1;
  3185. if (dy==null) dy=1;
  3186. dz = Math.sqrt(dx*dx+dy*dy)/6;
  3187. var x_min = Math.ceil(xmin/dx);
  3188. var y_min = Math.ceil(ymin/dy);
  3189. for (x = x_min; x <= xmax; x += dx)
  3190. for (y = y_min; y <= ymax; y += dy) {
  3191. gxy = g(x,y);
  3192. if (!isNaN(gxy)) {
  3193. if (Math.abs(gxy)=="Infinity") {u = 0; v = dz;}
  3194. else {u = dz/Math.sqrt(1+gxy*gxy); v = gxy*u;}
  3195. line([x-u,y-v],[x+u,y+v]);
  3196. }
  3197. }
  3198. }
  3199. ///////////////////////user graphics commands end here/////////////////////////
  3200. function show_props(obj) {
  3201. var result = "";
  3202. for (var i=0; i< obj.childNodes.length; i++)
  3203. result += obj.childNodes.item(i) + "\n";
  3204. return result;
  3205. }
  3206. function displayCoord(evt) {
  3207. if (showcoordinates) {
  3208. //alert(show_props(evt.target.parentNode))
  3209. var svgroot = evt.target.parentNode;
  3210. var nl = svgroot.childNodes;
  3211. for (var i=0; i<nl.length && nl.item(i).nodeName!="text"; i++);
  3212. var cnode = nl.item(i);
  3213. cnode.mtext = mtext;
  3214. cnode.mtext([svgroot.getAttribute("width")-0,svgroot.getAttribute("height")-0],"("+getX(evt).toFixed(2)+", "+getY(evt).toFixed(2)+")", "aboveleft", "");
  3215. }
  3216. }
  3217. function removeCoord(evt) {
  3218. var svgroot = evt.target.parentNode;
  3219. var nl = svgroot.childNodes;
  3220. for (var i=0; i<nl.length && nl.item(i).nodeName!="text"; i++);
  3221. var cnode = nl.item(i);
  3222. cnode.mtext = mtext;
  3223. cnode.mtext([svgroot.getAttribute("width")-0,svgroot.getAttribute("height")-0],"", "aboveleft", "");
  3224. }
  3225. function initASCIIMathCalculators(li) {
  3226. var i;
  3227. for (i=0; i<li.length; i++) {
  3228. li[i].innerHTML = calcstr;
  3229. AMprocessNode(li[i]);
  3230. }
  3231. li = document.getElementsByTagName("textarea");
  3232. var st;
  3233. for (i=0; i<li.length; i++) {
  3234. st = li[i].getAttribute("onkeyup");
  3235. if (st!=null) eval(String(st).replace(/function anonymous\(\)/,""));
  3236. }
  3237. }
  3238. function calculate(inputId,outputId) {
  3239. var str = document.getElementById(inputId).value;
  3240. var err = "";
  3241. var ind = str.lastIndexOf("\n");
  3242. if (ind==str.length-1) str = str.slice(0,ind);
  3243. str = str.slice(str.lastIndexOf("\n")+1);
  3244. try {
  3245. var res = eval(mathjs(str));
  3246. } catch(e) {
  3247. err = "syntax incomplete";
  3248. }
  3249. if (!isNaN(res) && res!="Infinity")
  3250. str = "`"+str+" =` "+(Math.abs(res-Math.round(res*1000000)/1000000)<1e-15?Math.round(res*1000000)/1000000:res)+err;
  3251. else if (str!="") str = "`"+str+"` = undefined"; //debug:+mathjs(str);
  3252. var outnode = document.getElementById(outputId);
  3253. var n = outnode.childNodes.length;
  3254. for (var i=0; i<n; i++)
  3255. outnode.removeChild(outnode.firstChild);
  3256. outnode.appendChild(document.createTextNode(str));
  3257. AMprocessNode(outnode);
  3258. }
  3259. function append(st){
  3260. document.getElementById('in').value+=st;
  3261. calculate('in','out');
  3262. document.getElementById('in').scrollTop = 1000;
  3263. document.getElementById('in').focus();
  3264. }
  3265. function clearTextArea(){
  3266. document.getElementById('in').value="";
  3267. calculate('in','out');
  3268. document.getElementById('in').focus();
  3269. }
  3270. var calcstr = "<table align=\"center\">\n<tr><th>\nASCIIMath Scientific Calculator\n</th></tr>\n<tr><td>\nClick in the box to use your keyboard or use the buttons\n</td></tr>\n<tr><td>\n<textarea id=\"in\" rows=\"3\" cols=\"40\" onkeyup=\"calculate('in','out')\"></textarea></td></tr>\n<tr><td height=\"50\">Result: &nbsp; &nbsp; <span id=\"out\"></span></td></tr>\n</table>\n<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody align=\"center\">\n<tr>\n<td colspan=\"4\">\n<button onclick=\"append('sin^-1(')\"><font size=2>`sin^-1`</font></button><button onclick=\"append('cos^-1(')\"><font size=2>`cos^-1`</font></button><button onclick=\"append('tan^-1(')\"><font size=2>`tan^-1`</font></button></td>\n<td><button onclick=\"clearTextArea()\">&nbsp;`C`&nbsp;</button></td>\n\n</tr>\n<tr>\n<td><button onclick=\"append('pi')\">&nbsp;`pi` &nbsp;</button></td>\n<td><button onclick=\"append('sin(')\">&nbsp;`sin`</button></td>\n<td><button onclick=\"append('cos(')\">&nbsp;`cos`</button></td>\n<td><button onclick=\"append('tan(')\">&nbsp;`tan`</button></td>\n<td><button onclick=\"append('^')\">`x^y`</button></td>\n</tr>\n<tr>\n<td><button onclick=\"append('!')\">&nbsp; `!` &nbsp;</button></td>\n\n<td><button onclick=\"append('(')\"><font size=2>&nbsp;&nbsp;`(`&nbsp;&nbsp;</font></button></td>\n<td><button onclick=\"append(')')\"><font size=2>&nbsp;&nbsp;`)`&nbsp;&nbsp;</font></button></td>\n<td><button onclick=\"append('sqrt(')\"><font size=2>`sqrt({::}^\ )`</font></button></td>\n<td><button onclick=\"append('/')\">&nbsp;`-:\ `</button></td>\n</tr>\n<tr>\n<td><button onclick=\"append('log(')\">`log`</button></td>\n<td><button onclick=\"append('7')\">&nbsp; `7` &nbsp;</button></td>\n<td><button onclick=\"append('8')\">&nbsp; `8` &nbsp;</button></td>\n\n<td><button onclick=\"append('9')\">&nbsp; `9` &nbsp;</button></td>\n<td><button onclick=\"append('*')\">&nbsp;`times`&nbsp;</button></td>\n</tr>\n<tr>\n<td><button onclick=\"append('ln(')\">&nbsp;`ln`&nbsp;</button></td>\n<td><button onclick=\"append('4')\">&nbsp; `4` &nbsp;</button></td>\n<td><button onclick=\"append('5')\">&nbsp; `5` &nbsp;</button></td>\n<td><button onclick=\"append('6')\">&nbsp; `6` &nbsp;</button></td>\n\n<td><button onclick=\"append('-')\">&nbsp;`-{::}`&nbsp;</button></td>\n</tr>\n<tr>\n<td><button onclick=\"append('e')\">&nbsp; `e` &nbsp;</button></td>\n<td><button onclick=\"append('1')\">&nbsp;&nbsp;`1` &nbsp;</button></td>\n<td><button onclick=\"append('2')\">&nbsp; `2` &nbsp;</button></td>\n<td><button onclick=\"append('3')\">&nbsp; `3` &nbsp;</button></td>\n<td><button onclick=\"append('+')\">&nbsp;`+{::}`&nbsp;</button></td>\n\n</tr>\n<tr>\n<td> <!--button onclick=\"append('pi')\">&nbsp;`pi` &nbsp;</button--></td>\n<td><button onclick=\"append('0')\">&nbsp; `0` &nbsp;</button></td>\n<td><button onclick=\"append('.')\">&nbsp; `.` &nbsp;</button></td>\n<td><button onclick=\"append('\\n')\">&nbsp;`\"ent\"`</button></td>\n</tr>\n</tbody>\n</table>";
  3271. // GO1.1 Generic onload by Brothercake
  3272. // http://www.brothercake.com/
  3273. //onload function (replaces the onload="translate()" in the <body> tag)
  3274. function generic()
  3275. {
  3276. if (translateOnLoad) {
  3277. var nd = document.getElementById("processasciimathinmoodle");
  3278. if (nd!=null) dsvglocation = nd.className;
  3279. if (nd!=null || !checkforprocessasciimathinmoodle) {
  3280. if (translateLaTeX) LMtranslate();
  3281. if (translateASCIIMath) translate();
  3282. if (translateASCIIsvg) drawPictures();
  3283. }
  3284. var li = getElementsByClass(document,"div","ASCIIMathCalculator");
  3285. if (li.length>0) initASCIIMathCalculators(li);
  3286. }
  3287. };
  3288. //setup onload function
  3289. if(typeof window.addEventListener != 'undefined')
  3290. {
  3291. //.. gecko, safari, konqueror and standard
  3292. window.addEventListener('load', generic, false);
  3293. }
  3294. else if(typeof document.addEventListener != 'undefined')
  3295. {
  3296. //.. opera 7
  3297. document.addEventListener('load', generic, false);
  3298. }
  3299. else if(typeof window.attachEvent != 'undefined')
  3300. {
  3301. //.. win/ie
  3302. window.attachEvent('onload', generic);
  3303. }
  3304. //** remove this condition to degrade older browsers
  3305. else
  3306. {
  3307. //.. mac/ie5 and anything else that gets this far
  3308. //if there's an existing onload function
  3309. if(typeof window.onload == 'function')
  3310. {
  3311. //store it
  3312. var existing = onload;
  3313. //add new onload handler
  3314. window.onload = function()
  3315. {
  3316. //call existing onload function
  3317. existing();
  3318. //call generic onload function
  3319. generic();
  3320. };
  3321. }
  3322. else
  3323. {
  3324. //setup onload function
  3325. window.onload = generic;
  3326. }
  3327. }