newcommand.js 4.3 KB

12345678910111213
  1. /*
  2. * /MathJax/extensions/TeX/newcommand.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/newcommand"]={version:"2.2"};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var f=MathJax.InputJax.TeX,g=f.Definitions;g.Add({macros:{newcommand:"NewCommand",renewcommand:"NewCommand",newenvironment:"NewEnvironment",renewenvironment:"NewEnvironment",def:"MacroDef",let:"Let"}},null,!0);f.Parse.Augment({NewCommand:function(a){var c=this.trimSpaces(this.GetArgument(a)),b=this.GetBrackets(a),d=this.GetBrackets(a),e=this.GetArgument(a);"\\"===c.charAt(0)&&(c=c.substr(1));c.match(/^(.|[a-z]+)$/i)||f.Error(["IllegalControlSequenceName","Illegal control sequence name for %1",a]);b&&(b=this.trimSpaces(b),b.match(/^[0-9]+$/)||f.Error(["IllegalParamNumber","Illegal number of parameters specified in %1",a]));this.setDef(c,["Macro",e,b,d])},NewEnvironment:function(a){var c=this.trimSpaces(this.GetArgument(a)),b=this.GetBrackets(a),d=this.GetArgument(a),e=this.GetArgument(a);b&&(b=this.trimSpaces(b),b.match(/^[0-9]+$/)||f.Error(["IllegalParamNumber","Illegal number of parameters specified in %1",a]));this.setEnv(c,["BeginEnv","EndEnv",d,e,b])},MacroDef:function(a){var c=this.GetCSname(a),b=this.GetTemplate(a,"\\"+c);a=this.GetArgument(a);b instanceof Array?this.setDef(c,["MacroWithTemplate",a].concat(b)):this.setDef(c,["Macro",a,b])},Let:function(a){var c=this.GetCSname(a),b;b=this.GetNext();"\x3d"===b&&(this.i++,b=this.GetNext());"\\"===b?(a=this.GetCSname(a),(b=this.csFindMacro(a))||(g.mathchar0mi[a]?b=["csMathchar0mi",g.mathchar0mi[a]]:g.mathchar0mo[a]?b=["csMathchar0mo",g.mathchar0mo[a]]:g.mathchar7[a]?b=["csMathchar7",g.mathchar7[a]]:null!=g.delimiter["\\"+a]&&(b=["csDelimiter",g.delimiter["\\"+a]]))):(b=["Macro",b],this.i++);this.setDef(c,b)},setDef:function(a,c){c.isUser=!0;g.macros[a]=c},setEnv:function(a,c){c.isUser=!0;g.environment[a]=c},GetCSname:function(a){"\\"!==this.GetNext()&&f.Error(["DoubleBackSlash","\\ must be followed by a control sequence"]);return this.trimSpaces(this.GetArgument(a)).substr(1)},GetTemplate:function(a,c){var b,d=[],e=0;this.GetNext();for(var h=this.i;this.i<this.string.length;){b=this.GetNext();if("#"===b)h!==this.i&&(d[e]=this.string.substr(h,this.i-h)),b=this.string.charAt(++this.i),b.match(/^[1-9]$/)||f.Error(["CantUseHash2","Illegal use of # in template for %1",c]),parseInt(b)!=++e&&f.Error(["SequentialParam","Parameters for %1 must be numbered sequentially",c]),h=this.i+1;else if("{"===b)return h!==this.i&&(d[e]=this.string.substr(h,this.i-h)),0<d.length?[e,d]:e;this.i++}f.Error(["MissingReplacementString","Missing replacement string for definition of %1",a])},MacroWithTemplate:function(a,c,b,d){if(b){var e=[];this.GetNext();d[0]&&!this.MatchParam(d[0])&&f.Error(["MismatchUseDef","Use of %1 doesn't match its definition",a]);for(var h=0;h<b;h++)e.push(this.GetParameter(a,d[h+1]));c=this.SubstituteArgs(e,c)}this.string=this.AddArgs(c,this.string.slice(this.i));this.i=0;++this.macroCount>f.config.MAXMACROS&&f.Error(["MaxMacroSub1","MathJax maximum macro substitution count exceeded; is there a recursive macro call?"])},BeginEnv:function(a,c,b,d){if(d){for(var e=[],f=0;f<d;f++)e.push(this.GetArgument("\\begin{"+name+"}"));c=this.SubstituteArgs(e,c);b=this.SubstituteArgs(e,b)}a.edef=b;this.string=this.AddArgs(c,this.string.slice(this.i));this.i=0;return a},EndEnv:function(a,c){this.string=this.AddArgs(a.edef,this.string.slice(this.i));this.i=0;return c},GetParameter:function(a,c){if(null==c)return this.GetArgument(a);for(var b=this.i,d=0,e=0;this.i<this.string.length;)if("{"===this.string.charAt(this.i))this.i===b&&(e=1),this.GetArgument(a),d=this.i-b;else{if(this.MatchParam(c))return e&&(b++,d-=2),this.string.substr(b,d);this.i++;d++;e=0}f.Error(["RunawayArgument","Runaway argument for %1?",a])},MatchParam:function(a){if(this.string.substr(this.i,a.length)!==a)return 0;this.i+=a.length;return 1}});f.Environment=function(a){g.environment[a]=["BeginEnv","EndEnv"].concat([].slice.call(arguments,1));g.environment[a].isUser=!0};MathJax.Hub.Startup.signal.Post("TeX newcommand Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/newcommand.js");