Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Meta

Comments on Mathjax does not render new lines

Parent

Mathjax does not render new lines

+9
−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$

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (3 comments)
Post
+7
−0

Tl;dr, Markdown and LaTeX don't like each other

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.

Workarounds

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)

Note: MathJax 3 has issues with \\

If the above doesn't work, then it might be a problem with MathJax. Try using the \displaylines environment (Note: it uses three backslashes instead of two, or you can use \cr)

<p>$$\displaylines{123 \\\ 321}$$</p>

or

<p>$$\displaylines{123 \cr 321}$$</p>

will give you

$$\displaylines{123 \cr 321}$$


  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. ↩︎

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

`array` and `align` environment (9 comments)
General comments (8 comments)
`array` and `align` environment
Wolgwang‭ wrote over 3 years ago

\begin{array}{l} P \text { (both girls, at least one winter girl })&=P \text { (both girls, at least one winter child) }\\ &=(1 / 4) P(\text { at least one winter child }) \\ &=(1 / 4)(1-P(\text { both are non-winter })) \\ &=(1 / 4)\left(1-(3 / 4)^{2}\right) \end{array} This code is giving me an unexpected result.

Wolgwang‭ wrote over 3 years ago

Instead of giving this it is giving this

Moshi‭ wrote over 3 years ago

$$\begin{array}{l} P \text { (both girls, at least one winter girl })&=P \text { (both girls, at least one winter child) }\\ &=(1 / 4) P(\text { at least one winter child }) \\ &=(1 / 4)(1-P(\text { both are non-winter })) \\ &=(1 / 4)\left(1-(3 / 4)^{2}\right) \end{array}$$

Moshi‭ wrote over 3 years ago

Wolgwang‭ Try wrapping it in a <p> tag

<p>$$\begin{array}{l} P \text { (both girls, at least one winter girl })&=P \text { (both girls, at least one winter child) }\\ &=(1 / 4) P(\text { at least one winter child }) \\ &=(1 / 4)(1-P(\text { both are non-winter })) \\ &=(1 / 4)\left(1-(3 / 4)^{2}\right) \end{array}$$</p>
Wolgwang‭ wrote over 3 years ago

Moshi‭ Thanks :-) That works. Can you please include this in your answer also?

Moshi‭ wrote over 3 years ago

Wolgwang‭ It's already there :)

Wolgwang‭ wrote over 3 years ago

Moshi‭ How to type <p>$$123 \\ 321$$</p> ? It gives this instead of this.

Moshi‭ wrote over 3 years ago

Wolgwang‭ https://github.com/mathjax/MathJax/issues/2312

MathJax 3 (what Codidact is using) has issues with \\.

For now, you can wrap them in an environment such as \displaylines, e.g.

<p>$$\displaylines{123 \\\ 321}$$</p>

Don't ask me why \displaylines uses three backslashes, I don't know. \cr works as well if you want to use that.

$$\displaylines{123 \\\ 321}$$

Wolgwang‭ wrote about 3 years ago

For future readers: If you want to quote Mathjax input that requires <p>, you can use <blockquote> to quote it.