Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear user defined TeX macros from global state #348

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ var queue = []; // queue of typesetting requests of the form [data,callbac
var data, callback, originalData; // the current queue item
var errors = []; // errors collected durring the typesetting
var ID = 0; // id for this SVG element
var userMacros = {}; // tracks user-defined macros during typesetting
var userEnvironment = {}; // tracks user-defined environments during typesetting

//
// The delimiters used for each of the input formats
Expand Down Expand Up @@ -416,6 +418,27 @@ function ConfigureMathJax() {

});

//
// Track user-defined macros and environment
//
MathJax.Hub.Register.StartupHook("TeX newcommand Ready",function () {
var TEX = MathJax.InputJax.TeX,
TEXDEF = TEX.Definitions;

var originalSetDef = TEX.Parse.prototype.setDef;
var originalSetEnv = TEX.Parse.prototype.setEnv;
TEX.Parse.Augment({
setDef: function (name,value) {
userMacros[name] = value;
return originalSetDef.apply(this, arguments);
},
setEnv: function (name,value) {
userEnvironment[name] = value;
return originalSetEnv.apply(this, arguments);
},
});
});

//
// Reset the color extension after `autoload-all`
//
Expand Down Expand Up @@ -715,6 +738,7 @@ function StartQueue() {
function GetState(state) {
var SVG = MathJax.OutputJax.SVG,
TEX = MathJax.InputJax.TeX,
TEXDEF = TEX.Definitions,
MML = MathJax.ElementJax.mml,
AMS = MathJax.Extension["TeX/AMSmath"],
HUB = MathJax.Hub, HTML = MathJax.HTML,
Expand All @@ -741,6 +765,38 @@ function GetState(state) {
MML.SUPER.ID = ID = 0;
MathJax.OutputJax.CommonHTML.ID = 0;
}

// Clear any existing user-defined macros and environment, then load them from state
for (var name in userMacros) {
if (userMacros.hasOwnProperty(name)) {
delete TEXDEF.macros[name];
}
}
userMacros = {};
if (state && state.macros) {
for (var name in state.macros) {
if (state.macros.hasOwnProperty(name)) {
userMacros[name] = TEXDEF.macros[name] = state.macros[name];
// Set isUser on array, in case the state has been serialized and the property has been lost.
TEXDEF.macros[name].isUser = true;
}
}
}
for (var name in userEnvironment) {
if (userEnvironment.hasOwnProperty(name)) {
delete TEXDEF.environment[name];
}
}
userEnvironment = {};
if (state && state.environment) {
for (var name in state.environment) {
if (state.environment.hasOwnProperty(name)) {
userEnvironment[name] = TEXDEF.environment[name] = state.environment[name];
// Set isUser on array, in case the state has been serialized and the property has been lost.
TEXDEF.environment[name].isUser = true;
}
}
}
}

//
Expand All @@ -765,6 +821,7 @@ function ReturnResult(result) {
if (state) {
var AMS = MathJax.Extension["TeX/AMSmath"];
var GLYPH = MathJax.OutputJax.SVG.BBOX.GLYPH;
var TEX = MathJax.InputJax.TeX;
state.AMS.startNumber = AMS.startNumber;
state.AMS.labels = AMS.labels;
state.AMS.IDs = AMS.IDs;
Expand All @@ -773,6 +830,8 @@ function ReturnResult(result) {
state.defs = GLYPH.defs;
state.n = GLYPH.n;
state.ID = ID;
state.macros = userMacros;
state.environment = userEnvironment;
}
serverState = STATE.READY;
callback(result, originalData);
Expand Down
Loading