AMSmath.js 13 KB

12345678910111213141516171819202122232425
  1. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /*************************************************************
  4. *
  5. * MathJax/extensions/TeX/AMSmath.js
  6. *
  7. * Implements AMS math environments and macros.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2009-2013 The MathJax Consortium
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */MathJax.Extension["TeX/AMSmath"]={version:"2.2",number:0,startNumber:0,labels:{},eqlabels:{},refs:[]};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var d=MathJax.ElementJax.mml,f=MathJax.InputJax.TeX,e=MathJax.Extension["TeX/AMSmath"],n=f.Definitions,h=f.Stack.Item,g=f.config.equationNumbers,m=function(a){for(var b=[],c=0,d=a.length;c<d;c++)b[c]=f.Parse.prototype.Em(a[c]);return b.join(" ")};n.Add({mathchar0mo:{iiiint:["2A0C",{texClass:d.TEXCLASS.OP}]},macros:{mathring:["Accent","2DA"],nobreakspace:"Tilde",negmedspace:["Spacer",d.LENGTH.NEGATIVEMEDIUMMATHSPACE],negthickspace:["Spacer",d.LENGTH.NEGATIVETHICKMATHSPACE],idotsint:["MultiIntegral","\\int\\cdots\\int"],dddot:["Accent","20DB"],ddddot:["Accent","20DC"],sideset:["Macro","\\mathop{\\mathop{\\rlap{\\phantom{#3}}}\\nolimits#1\\!\\mathop{#3}\\nolimits#2}",3],boxed:["Macro","\\fbox{$\\displaystyle{#1}$}",1],tag:"HandleTag",notag:"HandleNoTag",label:"HandleLabel",ref:"HandleRef",eqref:["HandleRef",!0],substack:["Macro","\\begin{subarray}{c}#1\\end{subarray}",1],injlim:["NamedOp","inj\x26thinsp;lim"],projlim:["NamedOp","proj\x26thinsp;lim"],varliminf:["Macro","\\mathop{\\underline{\\mmlToken{mi}{lim}}}"],varlimsup:["Macro","\\mathop{\\overline{\\mmlToken{mi}{lim}}}"],varinjlim:["Macro","\\mathop{\\underrightarrow{\\mmlToken{mi}{lim}\\Rule{-1pt}{0pt}{1pt}}\\Rule{0pt}{0pt}{.45em}}"],varprojlim:["Macro","\\mathop{\\underleftarrow{\\mmlToken{mi}{lim}\\Rule{-1pt}{0pt}{1pt}}\\Rule{0pt}{0pt}{.45em}}"],DeclareMathOperator:"HandleDeclareOp",operatorname:"HandleOperatorName",genfrac:"Genfrac",frac:["Genfrac","","","",""],tfrac:["Genfrac","","","",1],dfrac:["Genfrac","","","",0],binom:["Genfrac","(",")","0em",""],tbinom:["Genfrac","(",")","0em",1],dbinom:["Genfrac","(",")","0em",0],cfrac:"CFrac",shoveleft:["HandleShove",d.ALIGN.LEFT],shoveright:["HandleShove",d.ALIGN.RIGHT],xrightarrow:["xArrow",8594,5,6],xleftarrow:["xArrow",8592,7,3]},environment:{align:["AMSarray",null,!0,!0,"rlrlrlrlrlrl",m([5/18,2,5/18,2,5/18,2,5/18,2,5/18,2,5/18])],"align*":["AMSarray",null,!1,!0,"rlrlrlrlrlrl",m([5/18,2,5/18,2,5/18,2,5/18,2,5/18,2,5/18])],multline:["Multline",null,!0],"multline*":["Multline",null,!1],split:["AMSarray",null,!1,!1,"rl",m([5/18])],gather:["AMSarray",null,!0,!0,"c"],"gather*":["AMSarray",null,!1,!0,"c"],alignat:["AlignAt",null,!0,!0],"alignat*":["AlignAt",null,!1,!0],alignedat:["AlignAt",null,!1,!1],aligned:["AlignedArray",null,null,null,"rlrlrlrlrlrl",m([5/18,2,5/18,2,5/18,2,5/18,2,5/18,2,5/18]),".5em","D"],gathered:["AlignedArray",null,null,null,"c",null,".5em","D"],subarray:["Array",null,null,null,null,m([0,0,0,0]),"0.1em","S",1],smallmatrix:["Array",null,null,null,"c",m([1/3]),".2em","S",1],equation:["EquationBegin","Equation",!0],"equation*":["EquationBegin","EquationStar",!1],eqnarray:["AMSarray",null,!0,!0,"rcl",d.LENGTH.THICKMATHSPACE,".5em"],"eqnarray*":["AMSarray",null,!1,!0,"rcl",d.LENGTH.THICKMATHSPACE,".5em"]},delimiter:{"\\lvert":["2223",{texClass:d.TEXCLASS.OPEN}],"\\rvert":["2223",{texClass:d.TEXCLASS.CLOSE}],"\\lVert":["2225",{texClass:d.TEXCLASS.OPEN}],"\\rVert":["2225",{texClass:d.TEXCLASS.CLOSE}]}},null,!0);f.Parse.Augment({HandleTag:function(a){var b=this.GetStar(),c=this.trimSpaces(this.GetArgument(a)),k=c;b||(c=g.formatTag(c));b=this.stack.global;b.tagID=k;b.notags&&f.Error(["CommandNotAllowedInEnv","%1 not allowed in %2 environment",a,b.notags]);b.tag&&f.Error(["MultipleCommand","Multiple %1",a]);b.tag=d.mtd.apply(d,this.InternalMath(c)).With({id:g.formatID(k)})},HandleNoTag:function(a){this.stack.global.tag&&delete this.stack.global.tag;this.stack.global.notag=!0},HandleLabel:function(a){var b=this.stack.global,c=this.GetArgument(a);""===c||e.refUpdate||(b.label&&f.Error(["MultipleCommand","Multiple %1",a]),b.label=c,(e.labels[c]||e.eqlabels[c])&&f.Error(["MultipleLabel","Label '%1' multiply defined",c]),e.eqlabels[c]="???")},HandleRef:function(a,b){var c=this.GetArgument(a),k=e.labels[c]||e.eqlabels[c];k||(k="??",e.badref=!e.refUpdate);var f=k;b&&(f=g.formatTag(f));g.useLabelIds&&(k=c);this.Push(d.mrow.apply(d,this.InternalMath(f)).With({href:g.formatURL(g.formatID(k)),"class":"MathJax_ref"}))},HandleDeclareOp:function(a){var b=this.GetStar()?"\\limits":"",c=this.trimSpaces(this.GetArgument(a));"\\"==c.charAt(0)&&(c=c.substr(1));a=this.GetArgument(a);a=a.replace(/\*/g,"\\text{*}").replace(/-/g,"\\text{-}");f.Definitions.macros[c]=["Macro","\\mathop{\\rm "+a+"}"+b]},HandleOperatorName:function(a){var b=this.GetStar()?"\\limits":"\\nolimits";a=this.trimSpaces(this.GetArgument(a));a=a.replace(/\*/g,"\\text{*}").replace(/-/g,"\\text{-}");this.string="\\mathop{\\rm "+a+"}"+b+" "+this.string.slice(this.i);this.i=0},HandleShove:function(a,b){var c=this.stack.Top();("multline"!==c.type||c.data.length)&&f.Error(["CommandAtTheBeginingOfLine","%1 must come at the beginning of the line",a]);c.data.shove=b},CFrac:function(a){var b=this.trimSpaces(this.GetBrackets(a,"")),c=this.GetArgument(a),k=this.GetArgument(a),c=d.mfrac(f.Parse("\\strut\\textstyle{"+c+"}",this.stack.env).mml(),f.Parse("\\strut\\textstyle{"+k+"}",this.stack.env).mml()),b={l:d.ALIGN.LEFT,r:d.ALIGN.RIGHT,"":""}[b];null==b&&f.Error(["IllegalAlign","Illegal alignment specified in %1",a]);b&&(c.numalign=c.denomalign=b);this.Push(c)},Genfrac:function(a,b,c,k,e){b=null==b?this.GetDelimiterArg(a):this.convertDelimiter(b);c=null==c?this.GetDelimiterArg(a):this.convertDelimiter(c);null==k&&(k=this.GetArgument(a));null==e&&(e=this.trimSpaces(this.GetArgument(a)));var l=this.ParseArg(a),g=this.ParseArg(a),l=d.mfrac(l,g);""!==k&&(l.linethickness=k);if(b||c)l=f.fenced(b,l,c);""!==e&&(b=["D","T","S","SS"][e],null==b&&f.Error(["BadMathStyleFor","Bad math style for %1",a]),l=d.mstyle(l),"D"===b?(l.displaystyle=!0,l.scriptlevel=0):(l.displaystyle=!1,l.scriptlevel=e-1));this.Push(l)},Multline:function(a,b){this.Push(a);this.checkEqnEnv();return h.multline(b,this.stack).With({arraydef:{displaystyle:!0,rowspacing:".5em",width:f.config.MultLineWidth,columnwidth:"100%",side:f.config.TagSide,minlabelspacing:f.config.TagIndent}})},AMSarray:function(a,b,c,d,e){this.Push(a);c&&this.checkEqnEnv();d=d.replace(/[^clr]/g,"").split("").join(" ");d=d.replace(/l/g,"left").replace(/r/g,"right").replace(/c/g,"center");return h.AMSarray(a.name,b,c,this.stack).With({arraydef:{displaystyle:!0,rowspacing:".5em",columnalign:d,columnspacing:e||"1em",rowspacing:"3pt",side:f.config.TagSide,minlabelspacing:f.config.TagIndent}})},AlignAt:function(a,b,c){var d,e,g="",h=[];c||(e=this.GetBrackets("\\begin{"+a.name+"}"));d=this.GetArgument("\\begin{"+a.name+"}");for(d.match(/[^0-9]/)&&f.Error(["PositiveIntegerArg","Argument to %1 must me a positive integer","\\begin{"+a.name+"}"]);0<d;)g+="rl",h.push("0em 0em"),d--;h=h.join(" ");if(c)return this.AMSarray(a,b,c,g,h);a=this.Array.call(this,a,null,null,g,h,".5em","D");return this.setArrayAlign(a,e)},EquationBegin:function(a,b){this.checkEqnEnv();this.stack.global.forcetag=b&&"none"!==g.autoNumber;return a},EquationStar:function(a,b){this.stack.global.tagged=!0;return b},checkEqnEnv:function(){this.stack.global.eqnenv&&f.Error(["ErroneousNestingEq","Erroneous nesting of equation structures"]);this.stack.global.eqnenv=!0},MultiIntegral:function(a,b){var c=this.GetNext();if("\\"===c){var d=this.i,c=this.GetArgument(a);this.i=d;"\\limits"===c&&(b="\\idotsint"===a?"\\!\\!\\mathop{\\,\\,"+b+"}":"\\!\\!\\!\\mathop{\\,\\,\\,"+b+"}")}this.string=b+" "+this.string.slice(this.i);this.i=0},xArrow:function(a,b,c,e){c={width:"+"+(c+e)+"mu",lspace:c+"mu"};e=this.GetBrackets(a);a=this.ParseArg(a);b=d.mo(d.chars(String.fromCharCode(b))).With({stretchy:!0,texClass:d.TEXCLASS.REL});b=d.munderover(b);b.SetData(b.over,d.mpadded(a).With(c).With({voffset:".15em"}));e&&(e=f.Parse(e,this.stack.env).mml(),b.SetData(b.under,d.mpadded(e).With(c).With({voffset:"-.24em"})));this.Push(b)},GetDelimiterArg:function(a){var b=this.trimSpaces(this.GetArgument(a));if(""==b)return null;null==n.delimiter[b]&&f.Error(["MissingOrUnrecognizedDelim","Missing or unrecognized delimiter for %1",a]);return this.convertDelimiter(b)},GetStar:function(){var a="*"===this.GetNext();a&&this.i++;return a}});h.Augment({autoTag:function(){var a=this.global;if(!a.notag){e.number++;a.tagID=g.formatNumber(e.number.toString());var b=f.Parse("\\text{"+g.formatTag(a.tagID)+"}",{}).mml();a.tag=d.mtd(b.With({id:g.formatID(a.tagID)}))}},getTag:function(){var a=this.global,b=a.tag;a.tagged=!0;a.label&&(e.eqlabels[a.label]=a.tagID,g.useLabelIds&&(b.id=g.formatID(a.label)));delete a.tag;delete a.tagID;delete a.label;return b}});h.multline=h.array.Subclass({type:"multline",Init:function(a,b){this.SUPER(arguments).Init.apply(this);this.numbered=a&&"none"!==g.autoNumber;this.save={notag:b.global.notag};b.global.tagged=!a&&!b.global.forcetag},EndEntry:function(){var a=d.mtd.apply(d,this.data);this.data.shove&&(a.columnalign=this.data.shove);this.row.push(a);this.data=[]},EndRow:function(){1!=this.row.length&&f.Error(["MultlineRowsOneCol","The rows within the %1 environment must have exactly one column","multline"]);this.table.push(this.row);this.row=[]},EndTable:function(){this.SUPER(arguments).EndTable.call(this);if(this.table.length){var a=this.table.length-1,b,c=-1;this.table[0][0].columnalign||(this.table[0][0].columnalign=d.ALIGN.LEFT);this.table[a][0].columnalign||(this.table[a][0].columnalign=d.ALIGN.RIGHT);!this.global.tag&&this.numbered&&this.autoTag();this.global.tag&&!this.global.notags&&(c="left"===this.arraydef.side?0:this.table.length-1,this.table[c]=[this.getTag()].concat(this.table[c]));b=0;for(a=this.table.length;b<a;b++)this.table[b]=(b===c?d.mlabeledtr:d.mtr).apply(d,this.table[b])}this.global.notag=this.save.notag}});h.AMSarray=h.array.Subclass({type:"AMSarray",Init:function(a,b,c,d){this.SUPER(arguments).Init.apply(this);this.numbered=b&&"none"!==g.autoNumber;this.save={notags:d.global.notags,notag:d.global.notag};d.global.notags=c?null:a;d.global.tagged=!b&&!d.global.forcetag},EndRow:function(){var a=d.mtr;!this.global.tag&&this.numbered&&this.autoTag();this.global.tag&&!this.global.notags&&(this.row=[this.getTag()].concat(this.row),a=d.mlabeledtr);this.numbered&&delete this.global.notag;this.table.push(a.apply(d,this.row));this.row=[]},EndTable:function(){this.SUPER(arguments).EndTable.call(this);this.global.notags=this.save.notags;this.global.notag=this.save.notag}});h.start.Augment({oldCheckItem:h.start.prototype.checkItem,checkItem:function(a){if("stop"===a.type){a=this.mmlData();var b=this.global;!e.display||b.tag||b.tagged||b.isInner||"all"!==g.autoNumber&&!b.forcetag||this.autoTag();if(b.tag){var b=[this.getTag(),d.mtd(a)],c={side:f.config.TagSide,minlabelspacing:f.config.TagIndent,columnalign:a.displayAlign};a.displayAlign===d.INDENTALIGN.LEFT?(c.width="100%",a.displayIndent&&!String(a.displayIndent).match(/^0+(\.0*)?($|[a-z%])/)&&(c.columnwidth=a.displayIndent+" fit",c.columnspacing="0",b=[b[0],d.mtd(),b[1]])):a.displayAlign===d.INDENTALIGN.RIGHT&&(c.width="100%",a.displayIndent&&!String(a.displayIndent).match(/^0+(\.0*)?($|[a-z%])/)&&(c.columnwidth="fit "+a.displayIndent,c.columnspacing="0",b[2]=d.mtd()));a=d.mtable(d.mlabeledtr.apply(d,b)).With(c)}return h.mml(a)}return this.oldCheckItem.call(this,a)}});f.prefilterHooks.Add(function(a){e.display=a.display;e.number=e.startNumber;e.eqlabels={};e.badref=!1;e.refUpdate&&(e.number=a.script.MathJax.startNumber)});f.postfilterHooks.Add(function(a){a.script.MathJax.startNumber=e.startNumber;e.startNumber=e.number;MathJax.Hub.Insert(e.labels,e.eqlabels);e.badref&&!a.math.texError&&e.refs.push(a.script)});MathJax.Hub.Register.MessageHook("Begin Math Input",function(){e.refs=[];e.refUpdate=!1});MathJax.Hub.Register.MessageHook("End Math Input",function(a){if(e.refs.length){e.refUpdate=!0;a=0;for(var b=e.refs.length;a<b;a++)e.refs[a].MathJax.state=MathJax.ElementJax.STATE.UPDATE;return MathJax.Hub.processInput({scripts:e.refs,start:(new Date).getTime(),i:0,j:0,jax:{},jaxIDs:[]})}return null});f.resetEquationNumbers=function(a,b){e.startNumber=a||0;b||(e.labels={})};MathJax.Hub.Startup.signal.Post("TeX AMSmath Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/AMSmath.js");