-
Notifications
You must be signed in to change notification settings - Fork 97
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
base: develop
Are you sure you want to change the base?
Conversation
Sorry - looks like I should have forked off develop, not master. Fixing now. |
lib/main.js
Outdated
@@ -741,6 +741,19 @@ function GetState(state) { | |||
MML.SUPER.ID = ID = 0; | |||
MathJax.OutputJax.CommonHTML.ID = 0; | |||
} | |||
|
|||
// Clear any existing user defined macros, then load macros from state | |||
Object.keys(TEX.Definitions.macros).forEach(function(macroName){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you use TeX.Definitions.macros
a number of times, here, perhaps
var MACROS = TeX.Definitions.macros;
would be in order.
(Also, there should be a space between the )
and the following {
.)
lib/main.js
Outdated
} | ||
}); | ||
if (state && state.macros) { | ||
for (var macroName in state.macros) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use if (state.macros.hasOwnProperty(macroName) {...}
inside this loop, so that you will be sure to only use the names that are actually in the object. For example, if someone augments the Object.prototype
object (and we have encountered that before), then you will loop through this additions as well.
lib/main.js
Outdated
if (state && state.macros) { | ||
for (var macroName in state.macros) { | ||
TEX.Definitions.macros[macroName] = state.macros[macroName]; | ||
TEX.Definitions.macros[macroName].isUser = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not need to set this, as it is already set in the objects that are pointed to in state.macros
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured if someone were to serialize the state to JSON, the isUser
property would be lost because the macro is an array.
If that's not something you want to support, I'm happy to remove it. It's not important for our use case at the moment.
lib/main.js
Outdated
@@ -773,6 +787,13 @@ function ReturnResult(result) { | |||
state.defs = GLYPH.defs; | |||
state.n = GLYPH.n; | |||
state.ID = ID; | |||
state.macros = {}; | |||
for (var macroName in TEX.Definitions.macros) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, if (TeX.Definitions.macros.hasOwnProperty(maroName)) {...}
should be used inside this loop.
I like the idea of this, but I've made some review comments above. A couple of additional thoughts:
|
In terms of my last bullet point above, there are methods |
Thanks for the feedback. I liked your |
I appreciate your continued work on this, and like the direction this is going. I do have two issues that still concern me a bit. First, if the Second, if someone redefines an existing macro (e.g., There is still one hitch even with that: extensions that define macros will not override user-defined macros (i.e., they act as though the extension had been loaded initially, even if they are auto-loaded later). That means that if typesetting causes macro to be defined with the same name as one in an extension and then loads the extension, you will remove the user-defined one, but not restore the extension-loaded version that should be there in the absence of user-defined macros. This behavior is controlled by the Of course, this is a pretty low-likelihood situation, but it is one of those places where it could cause confusion about macros not being defined that should be. I suppose an alternative would be to just end up using the |
@rianmcguire any chance you'll get back to this some time soon? It would make a nice addition to 1.2.0 but it can easily wait for a later release. |
@pkra I'm not going to have time to get back to it for at least 2 weeks. |
@rianmcguire thanks for the update! |
@rianmcguire ping. |
So, any progress here? |
I've been thinking about a different approach that uses the Here's how to do it right now. Make a directory named
Then in your application for MathJax, configure the path to the extensions directory, load For example, this driver file (
takes two arguments (TeX expressions), and processes them with a
The output of the first should be a cosine, and the second a sine. You can also check that
This is based on an idea of Imari Karonen on the Math StackExchange site. This does not make the definitions available in the To avoid this, you could disable the |
Mathjax just updated to Version 3. This totally broke the encoding service mathjax conversion from math to text.
The last commit should not be included, here. As mentioned in #451, the change is not needed. Also, it is unrelated to the changes for TeX macros that are the subject of this PR, so should not be part of this PR. Had #451 been merged, there would be no need for this commit anyway. One way to roll it back would be:
If you are using this branch for your production work, and have included it for that, then you should not be using that branch for a pull request (it is best to make a separate branch for the PR, rather than using your own master branch). Because there is already a lot of important comments on this PR with this branch, it would be hard to change it now. But you could branch a new "production" branch from your master branch and use that for your additional commits like this one. |
MathJax supports TeX macros, and once defined, they persist forever in the global state of MathJax-node.
This PR changes the behaviour so any definitions are cleared before typeset, but still allows them to be persisted across multiple typesets using the
state
option.