mathjax/MathJax — Issue #186: Feature request: limit scope of TeX macro definitions
Author: vyznev
Created: 2011-12-28 10:31:24+00:00
State: open

We're seeing some problems with MathJax's handling of TeX macros (`\def`, `\newcommand`, etc.) on math.stackexchange.com. Basically, there are two issues:

* Typical StackExchange pages include many posts and comments written by several users. Since macro definitions are global to the entire page, macros defined in one post leak out to all subsequent posts on the page.

* The StackExchange software also features a live preview of posts. This works reasonably well with MathJax in general (although it does have occasional performance issues with long posts), but a problem is that, if one accidentally redefines a standard LaTeX command while typing a post, the definition lingers and there's no easy way to reset it. See [this meta thread](http://meta.math.stackexchange.com/questions/3394/old-def-definitions-linger) for an example.

I realize that these issues basically arise from the way we're using MathJax on math.SE, and that an obvious solution would be to "stop doing that, then". However, this is not as practical as it sounds: StackExchange websites are fundamentally collaborative, so having a mixture of content from multiple authors on a page is unavoidable. The live preview is also a standard StackExchange feature, and an extremely convenient one too. I doubt most users would be willing to give it up. I suppose we could disable MathJax parsing in the live preview, but the lack of feedback would make writing LaTeX formulas much more difficult.

I suppose one solution would be to just disable macro definitions (e.g. by redefining `\def` _et al._ themselves) on math.SE, but I'd hesitate to do that, since clearly some of our users do find this feature useful (even if most probably aren't even aware of it). What I'd prefer to see would be some features in MathJax to limit the scope of the definitions. As I suggested in the meta.math.SE discussion, that would mean either:

* some way to save the macro definitions before a `Typeset` call and to roll them back afterwards, or
* some way (e.g. a specific class name) to mark an HTML element so that TeX macros defined inside it don't leak outside.

I suppose either of these features would be good enough for us: if we had the latter, we could wrap our posts and preview panes in such HTML elements, while if we had the former, we could implement the latter manually by finding all such elements on the page and typesetting them one by one. (We don't really need nested scopes, although it's an obvious generalization of the second feature.)

Ps. If MathJax already has some way to achieve what I've described above, please let us know (and sorry for bothering you, if that's the case).