mhchem.js 6.8 KB

12345678910111213
  1. /*
  2. * /MathJax/extensions/TeX/mhchem.js
  3. *
  4. * Copyright (c) 2009-2013 The MathJax Consortium
  5. *
  6. * Part of the MathJax library.
  7. * See http://www.mathjax.org for details.
  8. *
  9. * Licensed under the Apache License, Version 2.0;
  10. * you may not use this file except in compliance with the License.
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. */MathJax.Extension["TeX/mhchem"]={version:"2.2"};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var c=MathJax.InputJax.TeX,d=MathJax.Object.Subclass({string:"",i:0,tex:"",atom:!1,sup:"",sub:"",Init:function(a){this.string=a},ParseTable:{"-":"Minus","+":"Plus","(":"Open",")":"Close","[":"Open","]":"Close","\x3c":"Less","^":"Superscript",_:"Subscript","*":"Dot",".":"Dot","\x3d":"Equal","#":"Pound",$:"Math","\\":"Macro"," ":"Space"},Arrows:{"-\x3e":"rightarrow","\x3c-":"leftarrow","\x3c-\x3e":"leftrightarrow","\x3c\x3d\x3e":"rightleftharpoons","\x3c\x3d\x3e\x3e":"Rightleftharpoons","\x3c\x3c\x3d\x3e":"Leftrightharpoons","^":"uparrow",v:"downarrow"},Bonds:{"-":"-","\x3d":"\x3d","#":"\\equiv","~":"\\tripledash","~-":"\\begin{CEstack}{}\\tripledash\\\\-\\end{CEstack}","~\x3d":"\\raise2mu{\\begin{CEstack}{}\\tripledash\\\\-\\\\-\\end{CEstack}}","~--":"\\raise2mu{\\begin{CEstack}{}\\tripledash\\\\-\\\\-\\end{CEstack}}","-~-":"\\raise2mu{\\begin{CEstack}{}-\\\\\\tripledash\\\\-\\end{CEstack}}","...":"{\\cdot}{\\cdot}{\\cdot}","....":"{\\cdot}{\\cdot}{\\cdot}{\\cdot}","-\x3e":"\\rightarrow","\x3c-":"\\leftarrow","??":"\\text{??}"},Parse:function(){this.tex="";for(this.atom=!1;this.i<this.string.length;){var a=this.string.charAt(this.i);if(a.match(/[a-z]/i))this.ParseLetter();else if(a.match(/[0-9]/))this.ParseNumber();else this["Parse"+(this.ParseTable[a]||"Other")](a)}this.FinishAtom();return this.tex},ParseLetter:function(){this.FinishAtom();this.Match(/^v( |$)/)?this.tex+="{\\"+this.Arrows.v+"}":(this.tex+="\\text{"+this.Match(/^[a-z]+/i)+"}",this.atom=!0)},ParseNumber:function(){var a=this.Match(/^\d+/);if(this.atom&&!this.sub)this.sub=a;else{this.FinishAtom();var b=this.Match(/^\/\d+/);b?(a="\\frac{"+a+"}{"+b.substr(1)+"}",this.tex+="\\mathchoice{\\textstyle"+a+"}{"+a+"}{"+a+"}{"+a+"}"):(this.tex+=a,this.i<this.string.length&&(this.tex+="\\,"))}},ParseMinus:function(a){if(!this.atom||this.i!==this.string.length-1&&" "!==this.string.charAt(this.i+1)){this.FinishAtom();if("-\x3e"===this.string.substr(this.i,2)){this.i+=2;this.AddArrow("-\x3e");return}this.tex+="{-}"}else this.sup+=a;this.i++},ParsePlus:function(a){this.atom?this.sup+=a:(this.FinishAtom(),this.tex+=a);this.i++},ParseDot:function(a){this.FinishAtom();this.tex+="\\cdot ";this.i++},ParseEqual:function(a){this.FinishAtom();this.tex+="{\x3d}";this.i++},ParsePound:function(a){this.FinishAtom();this.tex+="{\\equiv}";this.i++},ParseOpen:function(a){this.FinishAtom();var b=this.Match(/^\([v^]\)/);b?this.tex+="{\\"+this.Arrows[b.charAt(1)]+"}":(this.tex+="{"+a,this.i++)},ParseClose:function(a){this.FinishAtom();this.atom=!0;this.tex+=a+"}";this.i++},ParseLess:function(a){this.FinishAtom();var b=this.Match(/^(<->?|<=>>?|<<=>)/);b?this.AddArrow(b):(this.tex+=a,this.i++)},ParseSuperscript:function(a){a=this.string.charAt(++this.i);if("{"===a)this.i++,a=this.Find("}"),"-."===a?this.sup+="{-}{\\cdot}":a&&(this.sup+=d(a).Parse().replace(/^\{-\}/,"-"));else if(" "===a||""===a)this.tex+="{\\"+this.Arrows["^"]+"}",this.i++;else if(a=this.Match(/^(\d+|-\.)/))this.sup+=a},ParseSubscript:function(a){if("{"==this.string.charAt(++this.i))this.i++,this.sub+=d(this.Find("}")).Parse().replace(/^\{-\}/,"-");else if(a=this.Match(/^\d+/))this.sub+=a},ParseMath:function(a){this.FinishAtom();this.i++;this.tex+=this.Find(a)},ParseMacro:function(a){this.FinishAtom();this.i++;var b=this.Match(/^([a-z]+|.)/i)||" ";"sbond"===b?this.tex+="{-}":"dbond"===b?this.tex+="{\x3d}":"tbond"===b?this.tex+="{\\equiv}":"bond"===b?(a=this.Match(/^\{.*?\}/)||"",a=a.substr(1,a.length-2),this.tex+="{"+(this.Bonds[a]||"\\text{??}")+"}"):"{"===b?this.tex+="{\\{":"}"===b?(this.tex+="\\}}",this.atom=!0):this.tex+=a+b},ParseSpace:function(a){this.FinishAtom();this.i++},ParseOther:function(a){this.FinishAtom();this.tex+=a;this.i++},AddArrow:function(a){var b=this.Match(/^[CT]\[/);b&&(this.i--,b=b.charAt(0));var c=this.GetBracket(b),b=this.GetBracket(b);a=this.Arrows[a];c||b?(b&&(a+="["+b+"]"),a="\\mathrel{\\x"+(a+("{"+c+"}"))+"}"):a="\\long"+a+" ";this.tex+=a},FinishAtom:function(){if(this.sup||this.sub){if(this.sup&&this.sub&&!this.atom){var a=this.sup,b=this.sub;a.match(/\d/)||(a+="\\vphantom{0}");b.match(/\d/)||(b+="\\vphantom{0}");this.tex+="\\raise 1pt{\\scriptstyle\\begin{CEscriptstack}"+a+"\\\\"+b+"\\end{CEscriptstack}}\\kern-.125em "}else this.sup||(this.sup="\\Space{0pt}{0pt}{.2em}"),this.tex+="^{"+this.sup+"}_{"+this.sub+"}";this.sup=this.sub=""}this.atom=!1},GetBracket:function(a){if("["!==this.string.charAt(this.i))return"";this.i++;var b=this.Find("]");"C"===a?b="\\ce{"+b+"}":"T"===a&&(b.match(/^\{.*\}$/)||(b="{"+b+"}"),b="\\text"+b);return b},Match:function(a){if(a=a.exec(this.string.substr(this.i)))a=a[0],this.i+=a.length;return a},Find:function(a){for(var b=this.string.length,d=this.i,e=0;this.i<b;){var f=this.string.charAt(this.i++);if(f===a&&0===e)return this.string.substr(d,this.i-d-1);"{"===f?e++:"}"===f&&(e?e--:c.Error(["ExtraCloseMissingOpen","Extra close brace or missing open brace"]))}e&&c.Error(["MissingCloseBrace","Missing close brace"]);c.Error(["NoClosingChar","Can't find closing %1",a])}});MathJax.Extension["TeX/mhchem"].CE=d;c.Definitions.Add({macros:{ce:"CE",cf:"CE",cee:"CE",xleftrightarrow:["Extension","AMSmath"],xrightleftharpoons:["Extension","AMSmath"],xRightleftharpoons:["Extension","AMSmath"],xLeftrightharpoons:["Extension","AMSmath"],longrightleftharpoons:["Macro","\\stackrel{\\textstyle{{-}\\!\\!{\\rightharpoonup}}}{\\smash{{\\leftharpoondown}\\!\\!{-}}}"],longRightleftharpoons:["Macro","\\stackrel{\\textstyle{-}\\!\\!{\\rightharpoonup}}{\\small\\smash\\leftharpoondown}"],longLeftrightharpoons:["Macro","\\stackrel{\\rightharpoonup}{{{\\leftharpoondown}\\!\\!\\textstyle{-}}}"],hyphen:["Macro","\\text{-}"],tripledash:["Macro","\\raise3mu{\\tiny\\text{-}\\kern2mu\\text{-}\\kern2mu\\text{-}}"]},environment:{CEstack:["Array",null,null,null,"r",null,"0.001em","T",1],CEscriptstack:["Array",null,null,null,"r",null,"0.2em","S",1]}},null,!0);MathJax.Extension["TeX/AMSmath"]||c.Definitions.Add({macros:{xrightarrow:["Extension","AMSmath"],xleftarrow:["Extension","AMSmath"]}},null,!0);MathJax.Hub.Register.StartupHook("TeX AMSmath Ready",function(){c.Definitions.Add({macros:{xleftrightarrow:["xArrow",8596,6,6],xrightleftharpoons:["xArrow",8652,5,7],xRightleftharpoons:["xArrow",8652,5,7],xLeftrightharpoons:["xArrow",8652,5,7]}},null,!0)});c.Parse.Augment({CE:function(a){a=this.GetArgument(a);this.string=d(a).Parse()+this.string.substr(this.i);this.i=0}});MathJax.Hub.Startup.signal.Post("TeX mhchem Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/mhchem.js");