# MathJax preview bug

Since Feb, 3rd when I type an answer, things in MathJax don't get rendered in the preview. What's going on?

The issue was particularly annoying when editing this answer.

I have seen at least one other report here also without a solution or fix so far.

Assuming it is not me making the mistakes, can this bug be looked at?

• @rene: I just managed to reproduce this once (Chrome 48.0.2564.116 (64-bit) on Ubuntu Linux, using Common HTML math renderer with Fast Preview and Assistive MML enabled, FWIW)... but it disappeared when I reloaded the page, and I haven't managed to trigger it again. So, yeah, intermittent issue with an unknown set of trigger conditions. For some reason, MathJax on Chrome seems to suffer more than its fair share of these. :( – Ilmari Karonen Feb 23 '16 at 13:10
• Ping @DavideCervone: I just posted some instructions to reproduce this bug below. – Ilmari Karonen Feb 24 '16 at 20:44
• I am able to paste a screen shot in an email, but not in a comment on this page. – Michael Hardy Feb 25 '16 at 0:23
• I found a temporary fix that's worked twice: when this happens, right-click on the subject line and choose "open link in new window". When I did this, the problem didn't happen in the new window. – Michael Hardy Mar 6 '16 at 16:38
• This problem is still here. And the "temporary fix" I mentioned above doesn't always work. – Michael Hardy Mar 18 '16 at 13:13

Based on some investigation, I've managed to come up with a somewhat reliable way to reproduce this bug:

1. Find a MathJax-heavy post and edit it. Here's one of mine for your convenience.

2. Make sure the the MathJax "Fast Preview" feature is enabled. (Right-click on any MathJax formula, and make sure that Math SettingsMath RendererFast Preview has a check mark next to it in the context menu.)

3. Open the developer console (F12 in Chrome / Firefox).

4. Click on the edit box to focus it. Copy the post source with Ctrl+A, Ctrl+C.

5. Press Ctrl+V to repeatedly paste the post source into the edit box until you see the following error message in the developer console:

Uncaught TypeError: Cannot read property 'insertBefore' of null

Notes:

• The bug seems to be somewhat browser-dependent. I've managed to reliably reproduce it, using the instructions above, on Chrome 48 (on Windows 7 and Ubuntu Linux) and also on IE 11 on Windows 7. It does not seem to occur on Firefox, however.

• IIRC, on Chrome / Win7, I also managed to trigger the bug with Fast Preview disabled, but only when using the Common HTML, Preview HTML or Plain Source math renderers. On Chrome / Ubuntu, however, the bug seems to be only reproducible when using Fast Preview. (Of course, this does not necessarily mean that it's the OS that makes the difference; it could just be some other coincidental difference between the two browser installations.) Alas, I did not do much testing on IE 11, and I don't currently have access to the Win7 box I tested it on.

• Just to confuse things, there's also another, fairly benign and presumably unrelated bug that occasionally causes MathJax not to be rendered in the edit preview until the preview is refreshed. This one seems to be a simple race between MathJax processing and Markdown editor initialization. Simply typing anything into the edit box should be enough to trigger a new math rendering pass.

Also, it seems that the error message logged to the dev console is actually a secondary error, triggered while MathJax is trying to emit an earlier error message. However, both the primary and the secondary errors seem to have the same underlying cause: MathJax is trying to insert something before a script element that has been removed from the DOM, and therefore has no parent node.

Alas, I haven't really been able to diagnose this much further. I can pinpoint the locations where the errors occur: the secondary one happens in here in MathJax.Hub.formatError(), while the primary error seem to occur here in MathJax.Extension["fast-preview"].Preview(). In both cases, the statement that throws the error looks something like:

script.parentNode.insertBefore(..., script);


where script.parentNode is null, presumably because the script element has been removed from the DOM. What I haven't managed to figure out, however, is why MathJax is trying to insert something before a removed DOM element.

My guess is that, somehow, repeatedly updating the edit preview with a lot of complex MathJax formulas in it causes a new math processing pass to start before the previous one has properly finished. AFAIK, MathJax should be preventing this from happening, as long as the Typeset calls are properly queued, but for some reason this mechanism doesn't seem to be working. :(

• That post of yours to which you link certainly was in need of some editing. I've fixed the typesetting problems. I would take issue with some of the notational conventions used in it, but that's another matter. – Michael Hardy Feb 24 '16 at 23:24
• @MichaelHardy: Thanks! :) Come to think of it, there's also a spacing issue with the aligned environment there that I really should fix, but I don't really have much time to fiddle with it just now. – Ilmari Karonen Feb 25 '16 at 7:00
• Ps. Also reproduced on my laptop running Chromium 48.0 on Xubuntu Linux. – Ilmari Karonen Feb 25 '16 at 10:38
• Thanks for the very detailed notes. I will see if I can reproduce and make any headway with it. My suspicions coincide with yours: it looks like the DOM is being replaced before MathJax had finished typesetting it, and the script nodes have been removed from the DOM so have no parents. I thought the MathJax code used by the editor did handle the queuing properly for that, but perhaps I misremember, or perhaps something in 2.6 broke the synchronization. I'll see what I can do to figure it out. – Davide Cervone Feb 25 '16 at 14:53
• I can reproduce in Chrome on OS X, and it looks like you are correct about the reason. I'll have to look into why the DOM is being removed before MathJax completes. I will also look into making MathJax handle that situation better. – Davide Cervone Feb 25 '16 at 17:02
• this appears to be a bug in MathJax, not Stack Exchange? – Jeff Atwood Mar 18 '16 at 8:31
• @Jeff: Seems to be so, although SE might be partly to blame too. Davide might know better, but I suspect the root cause is that the SE Markdown editor preview is "pulling the rug" from under MathJax by updating the preview before MathJax has finished typesetting it. The fact that this crashes MathJax is IMO a bug in MathJax, since it should tolerate such "abuse", but the editor could certainly also be smarter about how it handles preview updates in the presence of MathJax. Still, as long as the crashes are fixed, the rest could be a separate feature-req. – Ilmari Karonen Mar 18 '16 at 11:31
• I have finally been able to look into this more closely, and have proposed changes to the editing script that are under review at the moment. In the meantime, I have provided a user-script that implements the changes so you can test it out before hand yourself. – Davide Cervone Apr 8 '16 at 23:31

There's nothing to work on; the problem hasn't been consistently reproduced.

I'm sure that doesn't make you feel any better, and I wish I could help... But it's hard to fix a problem if you don't know what causes it or how to make it happen.

There are countless problems like this caused by folks with flaky network connections, old browsers, sketchy add-ons, Safari, or just plain bad luck. It's unfortunate, but such is life.

To improve the chances of a problem being identified and fixed, when reporting a bug please try to describe it in a way that makes it easy for readers reproduce:

If there's a visual anomaly associated with the bug, then post a screenshot, circling the portion that offends you. Applying a tasteful drop-shadow to your screenshot is optional; drawing the circles freehand is mandatory.

If you can't get it to happen consistently, try harder; if you still can't get it to happen consistently, be sure to mention that before we all waste our time trying to reproduce this awesome new feature bug you've found.

If your bug is only when you're using a beta browser, though, be aware they're not supported. Get a production browser and report back only if it's broken there too.

• And also tag it as a bug. :) – Adam Lear Feb 22 '16 at 21:01
• This is not easy to reproduce; it happens erratically. I've used math.stackexchange.com for year without seeing this and then it started happening frequently. – Michael Hardy Feb 22 '16 at 21:06
• Well, keep your bug report updated, @michael. When it happens, take note of exactly what you were doing and add a note to the post. And also what Adam said. – Shog9 Feb 22 '16 at 21:55
• Is there some way to post a screen shot either in my question above or in a comment? I can paste it into an email, but not here. – Michael Hardy Feb 25 '16 at 0:24
• @Shog9 : ^^ See my screen shot comment above. – Michael Hardy Feb 25 '16 at 1:17
• Yes, you can put a screenshot in your question, @MichaelHardy. There's a button on the toolbar, or the shortcut key is Ctrl+G. – Shog9 Feb 25 '16 at 2:30
• @MichaelHardy: Also, if you really want to link a screenshot from a comment, you can use the full post editor to upload it, and then copy the resulting imgur URL into the comment. – Ilmari Karonen Feb 25 '16 at 10:34
• @Shog9 : I tried that and it didn't work. – Michael Hardy Feb 25 '16 at 17:24
• what do you mean it didn't work, @Michael? – Shog9 Feb 25 '16 at 18:27
• @Shog9 : Just that nothing happened. If I understand you correctly, in order to paste a screenshot into a comment, I should depress the "g" key while depressing the Ctrl key. I did that and nothing happened. – Michael Hardy Feb 25 '16 at 19:07
• No, that only works in the post editor, @Michael. I would avoid trying to put screenshots (or any other critical information) in comments. – Shog9 Feb 25 '16 at 19:12
• @Shog9 : It doesn't work in posts either. I just get a box that says "Drag and drop or click here." There's no way to paste anything there. – Michael Hardy Feb 25 '16 at 19:24
• Paste only works in Chrome, @Michael. Ctrl+V with that dialog open. For other browsers, you'll have to save the screenshot locally and then upload the file. – Shog9 Feb 25 '16 at 19:28
• -1, instructions for circles should be explicit – corsiKa Mar 11 '16 at 20:44