# MathJax bug upon page refresh

When entering this answer, everything looks good. Editing and saving also work fine. However, when I refresh the page I get $\color{red}{\textit{[Math Processing Error]}}$ for the line tagged $(3)$.

The line is copied below.

$$u_n=A_n\left(u_1+\sum_{k=1}^{n-1}\frac{\beta_k}{A_{k+1}}\right)\tag{3}$$

I have gotten verification from several people on chat that they see the same thing, so I don't think it is browser related.

Further information:

I have tried looking in all the source on the page for a \newcommand or the like to see if something like that could be interfering. I found nothing.

-
Same here, Arch Linux/64bit/Firefox 12.x; it works fine here and on the edit page. –  Asaf Karagila May 3 '12 at 21:20
It works fine on the Formatting Sandbox: meta.math.stackexchange.com/a/4119/622 –  Asaf Karagila May 3 '12 at 21:32
@Asaf: yes, it seems to work fine everywhere but on the main site. I even added a comment so that people could see what the line is supposed to say, and it shows up fine in the comment. –  robjohn May 3 '12 at 22:44
OK, I've been able to reproduce the error with Firefox 11 on MacOSX when the web-based fonts are used (but not with local STIX fonts). I'll look into it and see what I can com up with, and get back to you. –  Davide Cervone May 3 '12 at 23:47

OK, I have tracked down the problem. There are two factors involved that combined to make this fail in just that one answer, and not here or in editing. The first issue is that when you use \tag{xyz}, MathJax marks the element that contains the tag with id="mjx-eqn-xyz" so that it can be referenced with a link (this is part of the support for numbered equations, \label{}, and \ref{}). So your \tag{3} introduced an element with id="mjx-eqn-3". The problem is that there is already an equation on the page with that same tag, and so that same id is already in use, and technically that is invalid HTML (id's are supposed to be unique). MathJax uses the id in order to locate the DOM element associated with a given internal MathML element, and when there are duplicate id's that can cause confusion, which is what happened in this case.

The need to do such lookups is rare (mostly having to do with stretchy elements), so the problem doesn't always show up, but in this instance, there was a second factor involved that contributed to the problem. That factor is that this is the first equation on the page that requires large parentheses. These are stored in a separate font from most of the other characters (there are special fonts for the stretchy characters in five different sizes), and the first time one of those characters is needed, MathJax must load the web font for that size. While the font is being loaded, MathJax cancels the math processing and sets things up to restart the current equation once the font is available. But because the equation has been partly processed, it turns out that MathJax thinks that the DOM element for the previous \tag{3} is part of the current equation (rather than creating a new element as it should), since they have the same id, and that caused parent elements to be out of whack, leading to data not being saved where it was expected to be, and eventually a MathJax math processing error.

There is an easy change to prevent MathJax from reusing an element that has the wrong parent, and that will fix this particular problem, but it doesn't resolve the fact that the two equations have the same id's (and I don't see an obvious way to prevent it since the input is processed and the id's attached long before any output is added to the page).

The bug fix will be in the next release of MathJax, but for now you might be able to avoid the issue of restarting in the equation with the duplicate tag by forcing the large-size font to be loaded earlier on. One way might be to add something like

$\smash{\rlap{\phantom{\Bigg(}}}$


at the beginning of your answer to force a large parenthesis to be typeset, but without taking up any space. Ugly, but probably effective.

-
I added the code given and the bug is gone :-) Thanks! –  robjohn May 5 '12 at 16:30
WOW! Talk about a convoluted bug. I'm impressed by the rarity of the conditions needed here (because "just" same \tag labels exists elsewhere on the question page and they work just peachy...) –  Asaf Karagila May 5 '12 at 16:34
robjohn: glad the hack worked to make your answer readable. @Asaf: yes, it is a convoluted bug. It took me several hours to track it down (I was looking in the wrong direction for quite a while, as I didn't notice the duplicate id until the very end, after which the rest was pretty straight-forward to figure out). –  Davide Cervone May 5 '12 at 16:43
I just ran across this $$|x-p|^2-((x-p)\cdot u)^2 = |x-p|^2-(x\cdot u)^2\tag{1}$$ $$\sum_{k=1}^n|x_k-p|^2-(x_k\cdot u)^2\tag{1}$$ which is probably the same thing with $\sum$ as the stretchy element. I assume it renders here because of the $\sum$ in the original question. –  robjohn May 14 '12 at 9:21