Add \begingroup and \endgroup commands to make it possible to have local macros. Add the \let and \renewenvironment commands. Add \global and \gdef now that there are local namespaces
This commit is contained in:
parent
e23b8b4756
commit
d5416ceb10
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
16
extensions/TeX/begingroup.js
Normal file
16
extensions/TeX/begingroup.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* /MathJax/extensions/TeX/begingroup.js
|
||||
*
|
||||
* Copyright (c) 2010 Design Science, Inc.
|
||||
*
|
||||
* Part of the MathJax library.
|
||||
* See http://www.mathjax.org for details.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var d=MathJax.InputJax.TeX;var b=d.Definitions;var a=MathJax.Object.Subclass({macros:null,environments:null,Init:function(e,f){this.macros=(e||{});this.environments=(f||{})},Find:function(e,f){if(this[f][e]){return this[f][e]}},Def:function(e,g,f){this[f][e]=g},Undef:function(e,f){delete this[f][e]},Merge:function(e){MathJax.Hub.Insert(this.macros,e.macros);MathJax.Hub.Insert(this.environments,e.environments)},MergeGlobals:function(e){var f=this.macros;for(var g in f){if(f.hasOwnProperty(g)&&f[g].global){e.Def(g,f[g],"macros",true);delete f[g].global;delete f[g]}}},Clear:function(g){this.environments={};if(g){this.macros={}}else{var e=this.macros;for(var f in e){if(e.hasOwnProperty(f)&&!e[f].global){delete e[f]}}}return this}});var c=d.nsStack=MathJax.Object.Subclass({stack:null,top:0,isEqn:false,Init:function(e){this.isEqn=e;this.stack=[];if(!e){this.Push(a(b.macros,b.environments))}else{this.Push(a())}},Def:function(e,h,f,g){var i=this.top-1;if(g){while(i>0){this.stack[i].Undef(e,f);i--}if(!(h instanceof Array)){h=[h]}if(this.isEqn){h.global=true}}this.stack[i].Def(e,h,f)},Push:function(e){this.stack.push(e);this.top=this.stack.length},Pop:function(){var e;if(this.top>1){e=this.stack[--this.top];if(this.isEqn){this.stack.pop()}}else{if(this.isEqn){this.Clear()}}return e},Find:function(e,g){for(var f=this.top-1;f>=0;f--){var h=this.stack[f].Find(e,g);if(h){return h}}return null},Merge:function(e){e.stack[0].MergeGlobals(this);this.stack[this.top-1].Merge(e.stack[0]);var f=[this.top,this.stack.length-this.top].concat(e.stack.slice(1));this.stack.splice.apply(this.stack,f);this.top=this.stack.length},Reset:function(){this.top=this.stack.length},Clear:function(e){this.stack=[this.stack[0].Clear()];this.top=this.stack.length}},{nsFrame:a});b.macros.begingroup="BeginGroup";b.macros.endgroup="EndGroup";b.macros.global=["Extension","newcommand"];b.macros.gdef=["Extension","newcommand"];d.Parse.Augment({BeginGroup:function(e){d.eqnStack.Push(a())},EndGroup:function(e){if(d.eqnStack.top>1){d.eqnStack.Pop()}else{if(d.rootStack.top===1){d.Error("Extra "+e+" or missing \\begingroup")}else{d.eqnStack.Clear();d.rootStack.Pop()}}},csFindMacro:function(e){return(d.eqnStack.Find(e,"macros")||d.rootStack.Find(e,"macros"))},envFindName:function(e){return(d.eqnStack.Find(e,"environments")||d.rootStack.Find(e,"environments"))}});d.rootStack=c();d.eqnStack=c(true);d.prefilterHooks.Add(function(){d.rootStack.Reset();d.eqnStack.Clear(true)});d.postfilterHooks.Add(function(){d.rootStack.Merge(d.eqnStack)});MathJax.Hub.Register.StartupHook("TeX newcommand Ready",function(){b.macros.global="Global";b.macros.gdef=["Macro","\\global\\def"];d.Parse.Augment({setDef:function(e,f){d.eqnStack.Def(e,f,"macros",this.stack.isGlobal);delete this.stack.env.isGlobal},setEnv:function(e,f){d.eqnStack.Def(e,f,"environments")},Global:function(e){var f=this.i;var g=this.GetCSname(e);this.i=f;if(g!=="let"&&g!=="def"&&g!=="newcommand"){d.Error(e+" not followed by \\let, \\def, or \\newcommand")}this.stack.env.isGlobal=true}})});MathJax.Hub.Startup.signal.Post("TeX begingroup Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/begingroup.js");
|
||||
|
|
@ -12,5 +12,5 @@
|
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var b="1.1";var c=MathJax.InputJax.TeX;var a=c.Definitions;MathJax.Hub.Insert(a,{macros:{newcommand:"NewCommand",renewcommand:"NewCommand",newenvironment:"NewEnvironment",def:"MacroDef"}});c.Parse.Augment({NewCommand:function(d){var e=this.trimSpaces(this.GetArgument(d)),g=this.trimSpaces(this.GetBrackets(d)),f=this.GetArgument(d);if(g===""){g=null}if(e.charAt(0)==="\\"){e=e.substr(1)}if(!e.match(/^(.|[a-z]+)$/i)){c.Error("Illegal control sequence name for "+d)}if(g!=null&&!g.match(/^[0-9]+$/)){c.Error("Illegal number of parameters specified in "+d)}a.macros[e]=["Macro",f,g]},NewEnvironment:function(e){var f=this.trimSpaces(this.GetArgument(e)),h=this.trimSpaces(this.GetBrackets(e)),g=this.GetArgument(e),d=this.GetArgument(e);if(h===""){h=null}if(h!=null&&!h.match(/^[0-9]+$/)){c.Error("Illegal number of parameters specified in "+e)}a.environment[f]=["BeginEnv","EndEnv",g,d,h]},MacroDef:function(d){var e=this.GetCSname(d),g=this.GetTemplate(d,"\\"+e),f=this.GetArgument(d);if(!(g instanceof Array)){a.macros[e]=["Macro",f,g]}else{a.macros[e]=["MacroWithTemplate",f,g[0],g[1]]}},GetCSname:function(e){var f=this.GetNext();if(f!=="\\"){c.Error("\\ must be followed by a control sequence")}var d=this.trimSpaces(this.GetArgument(e));return d.substr(1)},GetTemplate:function(f,e){var j,g=[],h=0;j=this.GetNext();var d=this.i;while(this.i<this.string.length){j=this.GetNext();if(j==="#"){if(d!==this.i){g[h]=this.string.substr(d,this.i-d)}j=this.string.charAt(++this.i);if(!j.match(/^[1-9]$/)){c.Error("Illegal use of # in template for "+e)}if(parseInt(j)!=++h){c.Error("Parameters for "+e+" must be numbered sequentially")}d=this.i+1}else{if(j==="{"){if(d!==this.i){g[h]=this.string.substr(d,this.i-d)}if(g.length>0){return[h,g]}else{return h}}}this.i++}c.Error("Missing replacement string for definition of "+f)},MacroWithTemplate:function(e,h,j,g){if(j){var d=[];this.GetNext();if(g[0]&&!this.MatchParam(g[0])){c.Error("Use of "+e+" doesn't match its definition")}for(var f=0;f<j;f++){d.push(this.GetParameter(e,g[f+1]))}h=this.SubstituteArgs(d,h)}this.string=this.AddArgs(h,this.string.slice(this.i));this.i=0;if(++this.macroCount>c.config.MAXMACROS){c.Error("MathJax maximum macro substitution count exceeded; is there a recursive macro call?")}},BeginEnv:function(g,j,d,h){if(h){var e=[];for(var f=0;f<h;f++){e.push(this.GetArgument("\\begin{"+name+"}"))}j=this.SubstituteArgs(e,j);d=this.SubstituteArgs(e,d)}g.edef=d;this.string=this.AddArgs(j,this.string.slice(this.i));this.i=0;return g},EndEnv:function(d,e){this.string=this.AddArgs(d.edef,this.string.slice(this.i));this.i=0;return e},GetParameter:function(e,h){if(h==null){return this.GetArgument(e)}var g=this.i,d=0,f=0;while(this.i<this.string.length){if(this.string.charAt(this.i)==="{"){if(this.i===g){f=1}this.GetArgument(e);d=this.i-g}else{if(this.MatchParam(h)){if(f){g++;d-=2}return this.string.substr(g,d)}else{this.i++;d++;f=0}}}c.Error("Runaway argument for "+e+"?")},MatchParam:function(d){if(this.string.substr(this.i,d.length)!==d){return 0}this.i+=d.length;return 1}});c.Environment=function(d){a.environment[d]=["BeginEnv","EndEnv"].concat([].slice.call(arguments,1))};MathJax.Hub.Startup.signal.Post("TeX newcommand Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/newcommand.js");
|
||||
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var b="1.1.1";var c=MathJax.InputJax.TeX;var a=c.Definitions;MathJax.Hub.Insert(a,{macros:{newcommand:"NewCommand",renewcommand:"NewCommand",newenvironment:"NewEnvironment",renewenvironment:"NewEnvironment",def:"MacroDef",let:"Let"}});c.Parse.Augment({NewCommand:function(d){var e=this.trimSpaces(this.GetArgument(d)),g=this.trimSpaces(this.GetBrackets(d)),f=this.GetArgument(d);if(g===""){g=null}if(e.charAt(0)==="\\"){e=e.substr(1)}if(!e.match(/^(.|[a-z]+)$/i)){c.Error("Illegal control sequence name for "+d)}if(g!=null&&!g.match(/^[0-9]+$/)){c.Error("Illegal number of parameters specified in "+d)}this.setDef(e,["Macro",f,g])},NewEnvironment:function(e){var f=this.trimSpaces(this.GetArgument(e)),h=this.trimSpaces(this.GetBrackets(e)),g=this.GetArgument(e),d=this.GetArgument(e);if(h===""){h=null}if(h!=null&&!h.match(/^[0-9]+$/)){c.Error("Illegal number of parameters specified in "+e)}this.setEnv(f,["BeginEnv","EndEnv",g,d,h])},MacroDef:function(d){var e=this.GetCSname(d),g=this.GetTemplate(d,"\\"+e),f=this.GetArgument(d);if(!(g instanceof Array)){this.setDef(e,["Macro",f,g])}else{this.setDef(e,["MacroWithTemplate",f].concat(g))}},Let:function(d){var e=this.GetCSname(d),f;var g=this.GetNext();if(g==="="){this.i++;g=this.GetNext()}if(g==="\\"){d=this.GetCSname(d);f=this.csFindMacro(d);if(!f){if(a.mathchar0mi[d]){f=["csMathchar0mi",a.mathchar0mi[d]]}else{if(a.mathchar0mo[d]){f=["csMathchar0mo",a.mathchar0mo[d]]}else{if(a.mathchar7[d]){f=["csMathchar7",a.mathchar7[d]]}else{if(a.delimiter["\\"+d]!=null){f=["csDelimiter",a.delimiter["\\"+d]]}}}}}}else{f=["Macro",g];this.i++}this.setDef(e,f)},setDef:function(d,e){a.macros[d]=e},setEnv:function(d,e){a.environment[d]=e},GetCSname:function(e){var f=this.GetNext();if(f!=="\\"){c.Error("\\ must be followed by a control sequence")}var d=this.trimSpaces(this.GetArgument(e));return d.substr(1)},GetTemplate:function(f,e){var j,g=[],h=0;j=this.GetNext();var d=this.i;while(this.i<this.string.length){j=this.GetNext();if(j==="#"){if(d!==this.i){g[h]=this.string.substr(d,this.i-d)}j=this.string.charAt(++this.i);if(!j.match(/^[1-9]$/)){c.Error("Illegal use of # in template for "+e)}if(parseInt(j)!=++h){c.Error("Parameters for "+e+" must be numbered sequentially")}d=this.i+1}else{if(j==="{"){if(d!==this.i){g[h]=this.string.substr(d,this.i-d)}if(g.length>0){return[h,g]}else{return h}}}this.i++}c.Error("Missing replacement string for definition of "+f)},MacroWithTemplate:function(e,h,j,g){if(j){var d=[];this.GetNext();if(g[0]&&!this.MatchParam(g[0])){c.Error("Use of "+e+" doesn't match its definition")}for(var f=0;f<j;f++){d.push(this.GetParameter(e,g[f+1]))}h=this.SubstituteArgs(d,h)}this.string=this.AddArgs(h,this.string.slice(this.i));this.i=0;if(++this.macroCount>c.config.MAXMACROS){c.Error("MathJax maximum macro substitution count exceeded; is there a recursive macro call?")}},BeginEnv:function(g,j,d,h){if(h){var e=[];for(var f=0;f<h;f++){e.push(this.GetArgument("\\begin{"+name+"}"))}j=this.SubstituteArgs(e,j);d=this.SubstituteArgs(e,d)}g.edef=d;this.string=this.AddArgs(j,this.string.slice(this.i));this.i=0;return g},EndEnv:function(d,e){this.string=this.AddArgs(d.edef,this.string.slice(this.i));this.i=0;return e},GetParameter:function(e,h){if(h==null){return this.GetArgument(e)}var g=this.i,d=0,f=0;while(this.i<this.string.length){if(this.string.charAt(this.i)==="{"){if(this.i===g){f=1}this.GetArgument(e);d=this.i-g}else{if(this.MatchParam(h)){if(f){g++;d-=2}return this.string.substr(g,d)}else{this.i++;d++;f=0}}}c.Error("Runaway argument for "+e+"?")},MatchParam:function(d){if(this.string.substr(this.i,d.length)!==d){return 0}this.i+=d.length;return 1}});c.Environment=function(d){a.environment[d]=["BeginEnv","EndEnv"].concat([].slice.call(arguments,1))};MathJax.Hub.Startup.signal.Post("TeX newcommand Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/newcommand.js");
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
286
unpacked/extensions/TeX/begingroup.js
Normal file
286
unpacked/extensions/TeX/begingroup.js
Normal file
|
@ -0,0 +1,286 @@
|
|||
/*************************************************************
|
||||
*
|
||||
* MathJax/extensions/TeX/begingroup.js
|
||||
*
|
||||
* Implements \begingroup and \endgroup commands that make local
|
||||
* definitions possible and are removed when the \endgroup occurs.
|
||||
*
|
||||
* ---------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2011 Design Science, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
|
||||
var TEX = MathJax.InputJax.TeX; var TEXDEF = TEX.Definitions;
|
||||
|
||||
/****************************************************/
|
||||
|
||||
//
|
||||
// A namespace for localizing macros and environments
|
||||
// (\begingroup and \endgroup create and destroy these)
|
||||
//
|
||||
var NSFRAME = MathJax.Object.Subclass({
|
||||
macros: null, // the local macro definitions
|
||||
environments: null, // the local environments
|
||||
Init: function (macros,environments) {
|
||||
this.macros = (macros || {});
|
||||
this.environments = (environments || {});
|
||||
},
|
||||
//
|
||||
// Find a macro or environment by name
|
||||
//
|
||||
Find: function (name,type) {if (this[type][name]) {return this[type][name]}},
|
||||
//
|
||||
// Define or remove a macro or environment
|
||||
//
|
||||
Def: function (name,value,type) {this[type][name] = value},
|
||||
Undef: function (name,type) {delete this[type][name]},
|
||||
//
|
||||
// Merge two namespaces (used when the equation namespace is combined with the root one)
|
||||
//
|
||||
Merge: function (frame) {
|
||||
MathJax.Hub.Insert(this.macros,frame.macros);
|
||||
MathJax.Hub.Insert(this.environments,frame.environments);
|
||||
},
|
||||
//
|
||||
// Move global macros to the stack (globally) and remove from the frame
|
||||
//
|
||||
MergeGlobals: function (stack) {
|
||||
var macros = this.macros;
|
||||
for (var cs in macros) {if (macros.hasOwnProperty(cs) && macros[cs].global) {
|
||||
stack.Def(cs,macros[cs],"macros",true);
|
||||
delete macros[cs].global; delete macros[cs];
|
||||
}}
|
||||
},
|
||||
//
|
||||
// Clear the macro and environment lists
|
||||
// (but not global macros unless "all" is true)
|
||||
//
|
||||
Clear: function (all) {
|
||||
this.environments = {};
|
||||
if (all) {this.macros = {}} else {
|
||||
var macros = this.macros;
|
||||
for (var cs in macros) {
|
||||
if (macros.hasOwnProperty(cs) && !macros[cs].global) {delete macros[cs]}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
});
|
||||
|
||||
/****************************************************/
|
||||
|
||||
//
|
||||
// A Stack of namespace frames
|
||||
//
|
||||
var NSSTACK = TEX.nsStack = MathJax.Object.Subclass({
|
||||
stack: null, // the namespace frames
|
||||
top: 0, // the current top one (we don't pop for real until the equation completes)
|
||||
isEqn: false, // true if this is the equation stack (not the global one)
|
||||
//
|
||||
// Set up the initial stack frame
|
||||
//
|
||||
Init: function (eqn) {
|
||||
this.isEqn = eqn; this.stack = [];
|
||||
if (!eqn) {this.Push(NSFRAME(TEXDEF.macros,TEXDEF.environments))}
|
||||
else {this.Push(NSFRAME())}
|
||||
},
|
||||
//
|
||||
// Define a macro or environment in the top frame
|
||||
//
|
||||
Def: function (name,value,type,global) {
|
||||
var n = this.top-1;
|
||||
if (global) {
|
||||
//
|
||||
// Define global macros in the base frame and remove that cs
|
||||
// from all other frames. Mark the global ones in equations
|
||||
// so they can be made global when merged with the root stack.
|
||||
//
|
||||
while (n > 0) {this.stack[n].Undef(name,type); n--}
|
||||
if (!(value instanceof Array)) {value = [value]}
|
||||
if (this.isEqn) {value.global = true}
|
||||
}
|
||||
this.stack[n].Def(name,value,type);
|
||||
},
|
||||
//
|
||||
// Push a new namespace frame on the stack
|
||||
//
|
||||
Push: function (frame) {
|
||||
this.stack.push(frame);
|
||||
this.top = this.stack.length;
|
||||
},
|
||||
//
|
||||
// Pop the top stack frame
|
||||
// (if it is the root, just keep track of the pop so we can
|
||||
// reset it if the equation is reprocessed)
|
||||
//
|
||||
Pop: function () {
|
||||
var top;
|
||||
if (this.top > 1) {
|
||||
top = this.stack[--this.top];
|
||||
if (this.isEqn) {this.stack.pop()}
|
||||
} else if (this.isEqn) {
|
||||
this.Clear();
|
||||
}
|
||||
return top;
|
||||
},
|
||||
//
|
||||
// Search the stack from top to bottom for the first
|
||||
// definition of the given control sequence in the given type
|
||||
//
|
||||
Find: function (name,type) {
|
||||
for (var i = this.top-1; i >= 0; i--) {
|
||||
var def = this.stack[i].Find(name,type);
|
||||
if (def) {return def}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
//
|
||||
// Combine the equation stack with the global one
|
||||
// (The bottom frame of the equation goes with the top frame of the global one,
|
||||
// and the remainder are pushed on the global stack, truncated to the
|
||||
// position where items were poped from it.)
|
||||
//
|
||||
Merge: function (stack) {
|
||||
stack.stack[0].MergeGlobals(this);
|
||||
this.stack[this.top-1].Merge(stack.stack[0]);
|
||||
var data = [this.top,this.stack.length-this.top].concat(stack.stack.slice(1));
|
||||
this.stack.splice.apply(this.stack,data);
|
||||
this.top = this.stack.length;
|
||||
},
|
||||
//
|
||||
// Put back the temporarily poped items
|
||||
//
|
||||
Reset: function () {this.top = this.stack.length},
|
||||
//
|
||||
// Clear the stack and start with a blank frame
|
||||
//
|
||||
Clear: function (all) {
|
||||
this.stack = [this.stack[0].Clear()];
|
||||
this.top = this.stack.length;
|
||||
}
|
||||
},{
|
||||
nsFrame: NSFRAME
|
||||
});
|
||||
|
||||
/****************************************************/
|
||||
|
||||
//
|
||||
// Define the new macros
|
||||
//
|
||||
TEXDEF.macros.begingroup = "BeginGroup";
|
||||
TEXDEF.macros.endgroup = "EndGroup";
|
||||
TEXDEF.macros.global = ["Extension","newcommand"];
|
||||
TEXDEF.macros.gdef = ["Extension","newcommand"];
|
||||
|
||||
TEX.Parse.Augment({
|
||||
//
|
||||
// Implement \begingroup
|
||||
//
|
||||
BeginGroup: function (name) {
|
||||
TEX.eqnStack.Push(NSFRAME());
|
||||
},
|
||||
//
|
||||
// Implements \endgroup
|
||||
//
|
||||
EndGroup: function (name) {
|
||||
//
|
||||
// If the equation has pushed frames, pop one,
|
||||
// Otherwise clear the equation stack and pop the top global one
|
||||
//
|
||||
if (TEX.eqnStack.top > 1) {
|
||||
TEX.eqnStack.Pop();
|
||||
} else if (TEX.rootStack.top === 1) {
|
||||
TEX.Error("Extra "+name+" or missing \\begingroup");
|
||||
} else {
|
||||
TEX.eqnStack.Clear();
|
||||
TEX.rootStack.Pop();
|
||||
}
|
||||
},
|
||||
|
||||
//
|
||||
// Replace the original routines with ones that looks through the
|
||||
// equation and root stacks for the given name
|
||||
//
|
||||
csFindMacro: function (name) {
|
||||
return (TEX.eqnStack.Find(name,"macros") || TEX.rootStack.Find(name,"macros"));
|
||||
},
|
||||
envFindName: function (name) {
|
||||
return (TEX.eqnStack.Find(name,"environments") || TEX.rootStack.Find(name,"environments"));
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/****************************************************/
|
||||
|
||||
TEX.rootStack = NSSTACK(); // the global namespace stack
|
||||
TEX.eqnStack = NSSTACK(true); // the equation stack
|
||||
|
||||
//
|
||||
// Reset the global stack and clear the equation stack
|
||||
// (this gets us back to the initial stack state as it was
|
||||
// before the equation was first processed, in case the equation
|
||||
// get restarted due to an autoloaded file)
|
||||
//
|
||||
TEX.prefilterHooks.Add(function () {TEX.rootStack.Reset(); TEX.eqnStack.Clear(true)});
|
||||
|
||||
//
|
||||
// We only get here if there were no errors and the equation is fully
|
||||
// processed (all restarts are complete). So we merge the equation
|
||||
// stack into the global stack, thus making the changes from this
|
||||
// equation permanent.
|
||||
//
|
||||
TEX.postfilterHooks.Add(function () {TEX.rootStack.Merge(TEX.eqnStack)});
|
||||
|
||||
/*********************************************************/
|
||||
|
||||
MathJax.Hub.Register.StartupHook("TeX newcommand Ready",function () {
|
||||
|
||||
//
|
||||
// Add the commands that depend on the newcommand code
|
||||
//
|
||||
TEXDEF.macros.global = "Global";
|
||||
TEXDEF.macros.gdef = ["Macro","\\global\\def"];
|
||||
|
||||
TEX.Parse.Augment({
|
||||
//
|
||||
// Modify the way macros and environments are defined
|
||||
// to make them go into the equation namespace stack
|
||||
//
|
||||
setDef: function (name,value) {
|
||||
TEX.eqnStack.Def(name,value,"macros",this.stack.isGlobal);
|
||||
delete this.stack.env.isGlobal;
|
||||
},
|
||||
setEnv: function (name,value) {TEX.eqnStack.Def(name,value,"environments")},
|
||||
|
||||
//
|
||||
// Implement \global (for \global\let, \global\def and \global\newcommand)
|
||||
//
|
||||
Global: function (name) {
|
||||
var i = this.i; var cs = this.GetCSname(name); this.i = i;
|
||||
if (cs !== "let" && cs !== "def" && cs !== "newcommand")
|
||||
{TEX.Error(name+" not followed by \\let, \\def, or \\newcommand")}
|
||||
this.stack.env.isGlobal = true;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
MathJax.Hub.Startup.signal.Post("TeX begingroup Ready");
|
||||
|
||||
});
|
||||
|
||||
MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/begingroup.js");
|
|
@ -7,7 +7,7 @@
|
|||
*
|
||||
* ---------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2009 Design Science, Inc.
|
||||
* Copyright (c) 2009-2011 Design Science, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -23,17 +23,19 @@
|
|||
*/
|
||||
|
||||
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
|
||||
var VERSION = "1.1";
|
||||
var VERSION = "1.1.1";
|
||||
|
||||
var TEX = MathJax.InputJax.TeX;
|
||||
var TEXDEF = TEX.Definitions;
|
||||
|
||||
MathJax.Hub.Insert(TEXDEF,{
|
||||
macros: {
|
||||
newcommand: 'NewCommand',
|
||||
renewcommand: 'NewCommand',
|
||||
newenvironment: 'NewEnvironment',
|
||||
def: 'MacroDef'
|
||||
newcommand: 'NewCommand',
|
||||
renewcommand: 'NewCommand',
|
||||
newenvironment: 'NewEnvironment',
|
||||
renewenvironment: 'NewEnvironment',
|
||||
def: 'MacroDef',
|
||||
let: 'Let'
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -50,7 +52,7 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
|
|||
if (cs.charAt(0) === "\\") {cs = cs.substr(1)}
|
||||
if (!cs.match(/^(.|[a-z]+)$/i)) {TEX.Error("Illegal control sequence name for "+name)}
|
||||
if (n != null && !n.match(/^[0-9]+$/)) {TEX.Error("Illegal number of parameters specified in "+name)}
|
||||
TEXDEF.macros[cs] = ['Macro',def,n];
|
||||
this.setDef(cs,['Macro',def,n]);
|
||||
},
|
||||
|
||||
/*
|
||||
|
@ -63,7 +65,7 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
|
|||
edef = this.GetArgument(name);
|
||||
if (n === '') {n = null}
|
||||
if (n != null && !n.match(/^[0-9]+$/)) {TEX.Error("Illegal number of parameters specified in "+name)}
|
||||
TEXDEF.environment[env] = ['BeginEnv','EndEnv',bdef,edef,n];
|
||||
this.setEnv(env,['BeginEnv','EndEnv',bdef,edef,n]);
|
||||
},
|
||||
|
||||
/*
|
||||
|
@ -73,10 +75,46 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
|
|||
var cs = this.GetCSname(name),
|
||||
params = this.GetTemplate(name,"\\"+cs),
|
||||
def = this.GetArgument(name);
|
||||
if (!(params instanceof Array)) {TEXDEF.macros[cs] = ['Macro',def,params]}
|
||||
else {TEXDEF.macros[cs] = ['MacroWithTemplate',def,params[0],params[1]]}
|
||||
if (!(params instanceof Array)) {this.setDef(cs,['Macro',def,params])}
|
||||
else {this.setDef(cs,['MacroWithTemplate',def].concat(params))}
|
||||
},
|
||||
|
||||
/*
|
||||
* Implements the \let command
|
||||
*/
|
||||
Let: function (name) {
|
||||
var cs = this.GetCSname(name), macro;
|
||||
var c = this.GetNext(); if (c === "=") {this.i++; c = this.GetNext()}
|
||||
//
|
||||
// All \let commands create entries in the macros array, but we
|
||||
// have to look in the various mathchar and delimiter arrays if
|
||||
// the source isn't a macro already, and attach the data to a
|
||||
// macro with the proper routine to process it.
|
||||
//
|
||||
// A command of the form \let\cs=char produces a macro equivalent
|
||||
// to \def\cs{char}, which is as close as MathJax can get for this.
|
||||
// So \let\bgroup={ is possible, but doesn't work as it does in TeX.
|
||||
//
|
||||
if (c === "\\") {
|
||||
name = this.GetCSname(name);
|
||||
macro = this.csFindMacro(name);
|
||||
if (!macro) {
|
||||
if (TEXDEF.mathchar0mi[name]) {macro = ["csMathchar0mi",TEXDEF.mathchar0mi[name]]} else
|
||||
if (TEXDEF.mathchar0mo[name]) {macro = ["csMathchar0mo",TEXDEF.mathchar0mo[name]]} else
|
||||
if (TEXDEF.mathchar7[name]) {macro = ["csMathchar7",TEXDEF.mathchar7[name]]} else
|
||||
if (TEXDEF.delimiter["\\"+name] != null) {macro = ["csDelimiter",TEXDEF.delimiter["\\"+name]]}
|
||||
}
|
||||
} else {macro = ["Macro",c]; this.i++}
|
||||
this.setDef(cs,macro);
|
||||
},
|
||||
|
||||
/*
|
||||
* Routines to set the macro and environment definitions
|
||||
* (overridden by begingroup to make localized versions)
|
||||
*/
|
||||
setDef: function (name,value) {TEXDEF.macros[name] = value},
|
||||
setEnv: function (name,value) {TEXDEF.environment[name] = value},
|
||||
|
||||
/*
|
||||
* Get a CS name or give an error
|
||||
*/
|
||||
|
|
|
@ -842,7 +842,9 @@
|
|||
newcommand: ['Extension','newcommand'],
|
||||
renewcommand: ['Extension','newcommand'],
|
||||
newenvironment: ['Extension','newcommand'],
|
||||
renewenvironment: ['Extension','newcommand'],
|
||||
def: ['Extension','newcommand'],
|
||||
let: ['Extension','newcommand'],
|
||||
|
||||
verb: ['Extension','verb'],
|
||||
|
||||
|
@ -950,34 +952,60 @@
|
|||
* Lookup a control-sequence and process it
|
||||
*/
|
||||
ControlSequence: function (c) {
|
||||
var name = this.GetCS(), mchar, def;
|
||||
if (TEXDEF.macros[name]) { // javascript macro
|
||||
var macro = TEXDEF.macros[name];
|
||||
var name = this.GetCS(), macro = this.csFindMacro(name);
|
||||
if (macro) {
|
||||
if (!(macro instanceof Array)) {macro = [macro]}
|
||||
var fn = macro[0]; if (!(fn instanceof Function)) {fn = this[fn]}
|
||||
fn.apply(this,["\\"+name].concat(macro.slice(1)));
|
||||
} else if (TEXDEF.mathchar0mi[name]) { // normal mathchar (mi)
|
||||
mchar = TEXDEF.mathchar0mi[name]; def = {mathvariant: MML.VARIANT.ITALIC};
|
||||
if (mchar instanceof Array) {def = mchar[1]; mchar = mchar[0]}
|
||||
this.Push(this.mmlToken(MML.mi(MML.entity("#x"+mchar)).With(def)));
|
||||
} else if (TEXDEF.mathchar0mo[name]) { // normal mathchar (mo)
|
||||
mchar = TEXDEF.mathchar0mo[name]; def = {stretchy: FALSE};
|
||||
if (mchar instanceof Array) {def = mchar[1]; def.stretchy = FALSE; mchar = mchar[0]}
|
||||
this.Push(this.mmlToken(MML.mo(MML.entity("#x"+mchar)).With(def)));
|
||||
} else if (TEXDEF.mathchar7[name]) { // mathchar in current family
|
||||
mchar = TEXDEF.mathchar7[name]; def = {mathvariant: MML.VARIANT.NORMAL};
|
||||
if (mchar instanceof Array) {def = mchar[1]; mchar = mchar[0]}
|
||||
if (this.stack.env.font) {def.mathvariant = this.stack.env.font}
|
||||
this.Push(this.mmlToken(MML.mi(MML.entity("#x"+mchar)).With(def)));
|
||||
} else if (TEXDEF.delimiter["\\"+name] != null) { // delimiter
|
||||
var delim = TEXDEF.delimiter["\\"+name]; def = {};
|
||||
if (delim instanceof Array) {def = delim[1]; delim = delim[0]}
|
||||
if (delim.length === 4) {delim = MML.entity('#x'+delim)} else {delim = MML.chars(delim)}
|
||||
this.Push(this.mmlToken(MML.mo(delim).With({fence: FALSE, stretchy: FALSE}).With(def)));
|
||||
} else { // error
|
||||
this.csUndefined("\\"+name);
|
||||
}
|
||||
fn.apply(this,[c+name].concat(macro.slice(1)));
|
||||
} else if (TEXDEF.mathchar0mi[name]) {this.csMathchar0mi(name,TEXDEF.mathchar0mi[name])}
|
||||
else if (TEXDEF.mathchar0mo[name]) {this.csMathchar0mo(name,TEXDEF.mathchar0mo[name])}
|
||||
else if (TEXDEF.mathchar7[name]) {this.csMathchar7(name,TEXDEF.mathchar7[name])}
|
||||
else if (TEXDEF.delimiter["\\"+name] != null) {this.csDelimiter(name,TEXDEF.delimiter["\\"+name])}
|
||||
else {this.csUndefined(c+name)}
|
||||
},
|
||||
//
|
||||
// Look up a macro in the macros list
|
||||
// (overridden in begingroup extension)
|
||||
//
|
||||
csFindMacro: function (name) {return TEXDEF.macros[name]},
|
||||
//
|
||||
// Handle normal mathchar (as an mi)
|
||||
//
|
||||
csMathchar0mi: function (name,mchar) {
|
||||
var def = {mathvariant: MML.VARIANT.ITALIC};
|
||||
if (mchar instanceof Array) {def = mchar[1]; mchar = mchar[0]}
|
||||
this.Push(this.mmlToken(MML.mi(MML.entity("#x"+mchar)).With(def)));
|
||||
},
|
||||
//
|
||||
// Handle normal mathchar (as an m0)
|
||||
//
|
||||
csMathchar0mo: function (name,mchar) {
|
||||
var def = {stretchy: false};
|
||||
if (mchar instanceof Array) {def = mchar[1]; def.stretchy = false; mchar = mchar[0]}
|
||||
this.Push(this.mmlToken(MML.mo(MML.entity("#x"+mchar)).With(def)));
|
||||
},
|
||||
//
|
||||
// Handle mathchar in current family
|
||||
//
|
||||
csMathchar7: function (name,mchar) {
|
||||
var def = {mathvariant: MML.VARIANT.NORMAL};
|
||||
if (mchar instanceof Array) {def = mchar[1]; mchar = mchar[0]}
|
||||
if (this.stack.env.font) {def.mathvariant = this.stack.env.font}
|
||||
this.Push(this.mmlToken(MML.mi(MML.entity("#x"+mchar)).With(def)));
|
||||
},
|
||||
//
|
||||
// Handle delimiter
|
||||
//
|
||||
csDelimiter: function (name,delim) {
|
||||
var def = {};
|
||||
if (delim instanceof Array) {def = delim[1]; delim = delim[0]}
|
||||
if (delim.length === 4) {delim = MML.entity('#x'+delim)} else {delim = MML.chars(delim)}
|
||||
this.Push(this.mmlToken(MML.mo(delim).With({fence: false, stretchy: false}).With(def)));
|
||||
},
|
||||
//
|
||||
// Handle undefined control sequence
|
||||
// (overridden in noUndefined extension)
|
||||
//
|
||||
csUndefined: function (name) {
|
||||
TEX.Error("Undefined control sequence "+name);
|
||||
},
|
||||
|
@ -1411,10 +1439,10 @@
|
|||
Begin: function (name) {
|
||||
var env = this.GetArgument(name);
|
||||
if (env.match(/[^a-z*]/i)) {TEX.Error('Invalid environment name "'+env+'"')}
|
||||
if (!TEXDEF.environment[env]) {TEX.Error('Unknown environment "'+env+'"')}
|
||||
var cmd = this.envFindName(env); if (!cmd) {TEX.Error('Unknown environment "'+env+'"')}
|
||||
if (++this.macroCount > TEX.config.MAXMACROS)
|
||||
{TEX.Error("MathJax maximum substitution count exceeded; is there a recursive latex environment?")}
|
||||
var cmd = TEXDEF.environment[env]; if (!(cmd instanceof Array)) {cmd = [cmd]}
|
||||
if (!(cmd instanceof Array)) {cmd = [cmd]}
|
||||
var mml = STACKITEM.begin().With({name: env, end: cmd[1], parse:this});
|
||||
if (cmd[0] && this[cmd[0]]) {mml = this[cmd[0]].apply(this,[mml].concat(cmd.slice(2)))}
|
||||
this.Push(mml);
|
||||
|
@ -1422,6 +1450,7 @@
|
|||
End: function (name) {
|
||||
this.Push(STACKITEM.end().With({name: this.GetArgument(name)}));
|
||||
},
|
||||
envFindName: function (name) {return TEXDEF.environment[name]},
|
||||
|
||||
Equation: function (begin,row) {return row},
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user