# spurious slash causes \, to be misinterpreted

Note: In the submitted version of the question, the comma is no longer being displayed – but when I edit the question, it’s displayed again in the preview.

I encountered a strange bug (at least it seems like one; perhaps I just don’t see the logic behind the behaviour):

$$\int\frac1x\,\mathrm dx$$

$$\begin{eqnarray} \int\frac1x\,\mathrm dx \end{eqnarray}$$

\begin{eqnarray} \int\frac1x\,\mathrm dx \end{eqnarray}

The code for all three is the same, with \, used to put some space between the quotient and the differential. The only difference is that the first one is written as $$...$$, the second one as \begin{eqnarray}...\end{eqnarray} and the third one as \begin\{eqnarray}...\end{eqnarray}, with a spurious slash in front of the opening brace. I would have thought that this should lead to a syntax error warning, not to \, being interpreted as a comma in the environment.

• On my PC (Fedora Linux running Chromium browser, HTML-CSS as MathJax renderer) I am not seeing any comma or other perceptible difference between the three expressions. – hardmath Mar 26 at 13:47
• @hardmath: Did you see the note at the top? Does it not show up when you try to edit? It's the same for me in Firefox, Safari and Chrome (all on Mac OS $10.14.6$), so it doesn't seem to be browser-dependent. – joriki Mar 26 at 13:51
• Yes, in the Edit preview the third expression suddenly acquires a comma ! – hardmath Mar 26 at 13:54
• @hardmath: OK, I've made the note more prominent :-) – joriki Mar 26 at 13:57
• The same happens if one writes "\end\{eqnarray}" or puts any other valid thing instead of "eqnarray". But why would you use a "\" after the environment name? It's the first time I see it written like that. – Zacky Mar 26 at 15:27
• @Zacky: It was a typo, I don't usually write it like that :-) Still, it seems like a bug when you have a typo in the environment header and instead of a warning it causes escape sequences to malfunction. It took me a bit to figure out why \, wasn't working... – joriki Mar 26 at 21:27
• Yes, when you write code with syntax errors, unexpected things will happen. I don't see how it's a bug. – Najib Idrissi Mar 27 at 7:47
• @NajibIdrissi: The important question isn't whether we should call it a bug but whether it should be changed. I think it should be changed. Error handling is an important part of functionality. Ideal error handling is outputting a helpful error message. Adequate error handling is failing at the point of the error. Error handling that causes spurious errors elsewhere that are hard for the user to connect to the actual error is bad error handling and should be fixed. – joriki Mar 27 at 10:02

This turns out to be an interaction between StackExchange's Markdown processor and MathJax. Here's what's going on: in the editor, the preview code has to remove the mathematics before it is processed by Markdown (so that the mathematics isn't changed by Markdown) and then puts it back in the text of the post. It does that by looking for the math delimiters, which includes $...$, $$...$$, and \begin{...}...\end{...}. Since \begin\{eqnarray}...\end{eqnarray} does not match any of these (because of the \{ rather than {), it is not removed from the document as math, and instead is processed directly by Markdown. Markdown uses the backslash as an escape character that prevents the usual Markdown action of the following character. In doing so, the backslash is removed (but apparently only when it actually prevents a Markdown action), so \{ becomes { and \, becomes , (but \b stays \b and \e stays \e since these are not preventing any Markdown action).

That means that

\begin\{eqnarray}
\int\frac1x\,\mathrm dx
\end{eqnarray}


becomes

\begin{eqnarray}
\int\frac1x,\mathrm dx
\end{eqnarray}


in the actual HTML for the preview. When MathJax runs on the result, it now does match as a math delimiter (since the \{ has been turned into { by markdown), and the math is processed. Because \, has been turned into , by Markdown, you see the comma rather than the space that \, would have produced.

When the final post is created, it is processed on the server to do the Markdown formatting. Originally, the server-side code and the preview code were the same, but over time, the server-side code has been updated, and it appears that it no longer does exactly what the preview code does. I suspect that the Markdown processing is slightly different, and that while \{ is being converted to {, the \, is not being changed. The , is not really a Markdown command, so \, its not escaping a markdown function, and so it shouldn't be altered, just as \b and \e aren't. I suspect that the in-browser Markdown processor is just treating \ followed by any non-letter as an escape, whereas the server-side one is more discerning. I don't know that for sure, but that is what it looks like.

As for error messages, MathJax only reports errors on things that it recognizes as math, and since \begin\{ doesn't look like \begin{...}, MathJax doesn't recognize it as a math delimiter, and doesn't process it (including not reporting any errors). If you used \begin\{ inside other math delimiters (e.g., $$\begin\{array}{c} a \end{array}$$, MathJax would report an error.

• Thanks for the detailed analysis! – joriki Apr 3 at 16:29