$\require{begingroup} \begingroup$As discussed in the comments to Davide Cervone's answer, I have added a client-side fix to this bug to version 1.2. of the Stack Overflow Unofficial Patch.
The fix works by injecting a special hidden TeX command (currently named \resetstack
) at the beggining of each post, comment, snippet (as shown e.g. on the front page) or question/answer link (as in the sidebar), which resets the TeX InputJax macro definitions to their default values.
The implementation is currently a bit complicated, but hopefully it can be simplified later, if some of the needed code is incorporated directly into MathJax as Davide suggests in the comments.
Some important notes about the fix:
Since SOUP is a client-side user script, installing it will fix the bug for you, but not for others. In particular, any definitions made in your posts will still leak to other posts when viewed by users who don't have SOUP installed (which, alas, means most of them).
I therefore strongly recommend that, if you use \def
, \(re)newcommand
or other such features in your posts, you should always prefix them with:
$\require{begingroup} \begingroup$
and end them with:
$\endgroup$
to keep your definitions from leaking into other posts. (Note that using SOUP should eliminate any memory leak issues due to unclosed \begingroup
tags while composing a post, since SOUP will reset the definition stack whenever the preview pane is re-typeset.)
You should also avoid redefining symbols in question titles, or anywhere near the top of your posts where the definitions would be likely to show up on the front page or in search results. (Alas, there's currently no 100% reliable way to prevent that from happening, except by not redefining TeX commands at all.)
Because of these issues, \def
and friends are still best used sparingly, and only for things that would be unlikely to conflict with notation in other posts. For example, \newcommand{\foo}{\operatorname{foo}}
is probably fine, but \newcommand{\sin}{\cos}
is not.
Also note that, even with SOUP, definitions imported from extensions via \require
will still leak into other posts. This is pretty much impossible to avoid without breaking some extensions badly, and should basically be considered as being by design.$\endgroup$
Edit: Turns out that my original implementation of this fix suffered from a race condition that made it work on Firefox only occasionally. This should be fixed in v1.2.1 that I just released; anyone using it is strongly advised to upgrade.
(Also, the upcoming v1.4 release of SOUP will have more MathJax scoping fixes, including one for \label
s, again based on a suggestion by Davide Cervone. Those still need some more testing before release, though.)
\renewcommand
? I can understand\DeclareMathOperator
and\newcommand
which seem much less dangerous because clashes are rather unlikely, but I can't think of a really good use of\renewcommand
. $\endgroup$\newcommand
or\renewcommand
was in the comments? $$\renewcommand{\sin}{\left.{{\unicode{x263A}}\atop{\unicode{x263A}}}\right)}$$ $\endgroup$\geqslant
($\geqslant$) overgeq
($\geq$) and thus write\let\geq\geqslant
in the beginning of my posts. (I don't want to write\geqslant
every time.) Same with\phi
($\phi$) and\varphi
($\varphi$). $\endgroup$$\DeclareMathOperator{\ran}{ran}\ran f=\emptyset$
to get $\DeclareMathOperator{\ran}{ran}\ran f=\emptyset$. $\endgroup$$\def\a{a}$ \a
results in a space, but$\def\a{a}$\a
doesn't. $\endgroup$