Meta

Mathjax does not render new lines

+7
−0

I can't make a new-line in MathJax.

Markup:

$$1 + 2 \\ 3$$


Result:

$$1 + 2 \ 3$$

It's supposed to look something like this (but centred):

$1 + 2$
$3$

Why does this post require moderator attention?
Why should this post be closed?

I don't know mathjax myself, but I just checked a post with newlines and saw that it used \\\\ (four instead of two). Does that make a difference for you? Monica Cellio‭ 8 months ago

Treating this as a doc issue at least for now. Monica Cellio‭ 8 months ago

Monica Cellio‭ 8 months ago

+5
−0

The problem lies in a conflict between the Markdown and MathJax parsers. The Markdown parser runs first[1] and uses \ as an escape character, and so \\ becomes a single backslash. The MathJax parser then only sees that single backslash, which means it won't create the newline.

This also breaks some other formulations, for example, sets:

$\{1,2,3\}$


Expected result: $\{1,2,3\}$
Actual result: ${1,2,3}$

The Markdown parser turns \{ into a literal {.

A list of backslash-escaped characters can be found here.

There are two ways to get around this problem.

Firstly, you can wrap the code in HTML tags. This will prevent the Markdown renderer from parsing the inside of it. Taking the earlier example, we would type

<p>$\{1,2,3\}$</p>


which turns into

$\{1,2,3\}$

You could also escape your backslashes if it comes before a markdown escape character. This method is more annoying, however, as it will break compatibility with pure LaTeX.

The previous example would be written as either \\{1,2,3\\} (escaping the backslash) or \\\{1,2,3\\\} (escaping both the backslash and braces), and a newline would be written as \\\\.

(You can use \\\ if the character following it isn't a Markdown escape character)

1. Technically, the Markdown parser runs server side and gives the parsed document to the client, which runs MathJax on it, but all that matters here is the order. ↩︎

Why does this post require moderator attention?

Shouldn't the Markdown parser treat $ and $\$ specially, then? wizzwizz4‭ 8 months ago

@wizzwizz4 Maybe? The parser Codidact uses wasn't meant for latex and so doesn't have that sort of ability. Moshi ‭ 8 months ago

If (if) we're modifying the Markdown parser, I wonder whether replacing MathJax with KaTeX would be a good idea, too. wizzwizz4‭ 8 months ago

@wizzwizz4 KaTeX is great, but it sadly still isn't as good as MathJax in terms of features. If you want though, you could make a new meta post to discuss that change Moshi ‭ 8 months ago

One possibility to fix this is before passing it to MarkDown to pass it through another code which escapes all backslashes occurring inside MathJax expressions. That code would only have to detect the configured MathJax triggers (i.e. the dollar signs, and whatever else MathJax is configured to interpret as formula delimiters), but otherwise would be agnostic towards MathJax syntax (its only job would be to escape things that should not be interpreted by MarkDown so that MathJax can see them). celtschk‭ 8 months ago

This community is part of the Codidact network. We have other communities too — take a look!