# MathJax is really eager to be applied to posts

When writing a MathJax heavy post I noticed a couple of bugs. For reference, here is an example of how the MathJax should look:

$$a_{i+1} = \begin{cases} \frac{a_i}{2} & \text{if a_i \% 2 = 0 (even)} \\ 3a_i + 1 & \text{if a_i \% 2 = 1 (odd)} \end{cases}$$

# Begin and end work outside of math mode

Code

a_{i+1} = \begin{cases}
\frac{a_i}{2}  & \text{if $a_i \% 2 = 0$ (even)} \\
3a_i + 1 & \text{if $a_i \% 2 = 1$ (odd)}
\end{cases}


Live

a_{i+1} = $$\begin{cases} \frac{a_i}{2} & \text{if a_i \% 2 = 0 (even)} \\ 3a_i + 1 & \text{if a_i \% 2 = 1 (odd)} \end{cases}$$

Preview

# An unclosed math-mode delimiter swallows some backslashes

Code

$$a_{i+1} = \begin{cases} \frac{a_i}{2} & \text{if a_i \% 2 = 0 (even)} \\ 3a_i + 1 & \text{if a_i \% 2 = 1 (odd)} \end{cases}  Live$$ a_{i+1} = \begin{cases} \frac{a_i}{2} & \text{if $a_i % 2 = 0$ (even)} \ 3a_i + 1 & \text{if $a_i % 2 = 1$ (odd)} \end{cases}

Preview

• It might be useful to indicate what you think should happen in these cases. It's not clear to me. Mar 22 at 21:13
• @Edward Personally I don't think MathJax should be eagerly evaluating. We've had posts break because of MathJax before, adding a single $$ to your post shouldn't mangle the backslashes in your question. If SE disagrees, I'd want the output to at least be correct (first example) and not be striping backslashes. Since the related bug has regressed to the point where MathJax seems to not work at all in the tag wiki. I really only care the bugs are fixed, not how. – Peilonrayz Mod Mar 22 at 21:29 • I'm not disagreeing, just not understanding. Typical bug report forms ask both "what did happen" (which you have documented) and "what should happen" which I'm not yet able to infer. Mar 22 at 21:33 • Why are your previews in dark mode? Are you using a script to change this? I do get the same results without the $$ when I paste this code but, with them, it works as expected - is it the fact that the MJ is rendering at all without the  that is part of the issue? I think @Edward makes some good points - Having a "what you think should happen instead" might make this easier to understand for me - This also occurs on some other MJ-enabled sites, such as Mathematics, so It'd be good to know what the common concern is and how it should be addressed. :) – Catija StaffMod Mar 26 at 18:35 • @Catija "Why are your previews in dark mode?" Because my preferred browser has a dark mode addon and white previews hurt my eyes when writing up. I do check on a browser with no scripts but keep activity in such an environment to the minimum. "what you think should happen instead" I don't know how better to explain myself than in my first comment. The two solutions I talked about are; ① don't eagerly evaluate the MathJax outside of where I properly tell the software where MathJax is ② render things correctly (see the blockquote example at the top of the question). – Peilonrayz Mod Mar 26 at 19:20 ## 2 Answers # Begin and end work outside of math mode The dollar signs are not the only delimiters that MathJax looks for. Because environments like \begin{align}...\end{align} also introduce math mode (they don't require other delimiters in LaTeX), MathJax also processes any \begin{xyz}...\end{xyz} pairs as math delimiters. So even though you haven't closed your dollar signs, MathJax picks up on the \begin{cases}...\end{cases} and thinks they are math delimiters, so processes the contents. That is why you are seeing part of your expression processed by MathJax. # An unclosed math-mode delimiter swallows some backslashes It's not MathJax that's doing it, it is the Markdown processing. The \% has been turned into % in the page source, and so the rest of that line (2 = 0 (even)}) is considered by MathJax to be a comment. Note also that the final \\  has been turned into a single slash. It used to be that the preview code was set up to work the same as MathJax in terms of determining what is math and what isn't, but that seems to have diverged over time. So the preview doesn't realize this is math, and so is performing Markdown substitutions. I suspect the unclosed  is confusing the preview.

Although we don't have the new "Live/Preview" editor on math.SE, I have seen the the Live part of the first bug many times when entering a post. I have believed this to be a problem with the browser. Usually, if I wait for a few seconds, the MathJax renders fine in the Live window. If I copy the post (so that I keep all my changes after the last draft save) and refresh my page until the Live window renders immediately, I can paste the copied version of my post back into the Code window and all continues as it should.

I've not noticed the second bug, but when my MathJax is not rendering properly, I look for where the problem is in my code and correct it. Perhaps this bug exists for me, but I've corrected my code too quickly to notice that it is this bug.

• My main concern is eagerly evaluating MathJax breaking users code without MathJax being intentionally used - related meta. I have in the past seen high rep users just straight up blame the bug on the low rep user. Who posted code and didn't intentionally use MathJax. We don't need even more ways for such unpleasantries to occur. And as I think you've described; if you're intentionally using MathJax you can probably fix the issues. Making the bug mostly a trivial issue for most other MathJax enabled sites.
– Peilonrayz Mod
Mar 26 at 20:13
• Okay. Since "a MathJax heavy post " was mentioned, I did not realize that the concern was related to unintentional MathJax use. I haven't looked, but I wonder if an option to turn MathJax off/on (per post) for sites that do/don't generally use MathJax has been suggested or investigated. Mar 26 at 20:42