Where's the LaTeX/MathJAX?

Unless I’m missing something, there doesn’t seem to be a way to use LaTeX in posts. Doesn’t Discourse have simple checkbox somewhere that enables MathJAX?

Aand apparently their vision of a simplified plug-in system wasn’t so easy to implement with Ruby on Rails. Here’s topic on the official meta, and a GitHub project too.

Yep! There should be MathJax installed, check out the bottom of this post:

Double dollar signs will do an equation

$$a^2 + b^2 = c^2$$

and single dollar signs with spaces around them will do inline: $a^2 + b^2 = c^2$

I debated whether to have single dollar signs without the space, but it might unintentionally trigger for $13 + $12 = $15.

Hmm… they aren’t showing up in the preview; let’s see if submitting the post renders them…

$$
\left( \begin{array}{ccc}
a_{11} & a_{12} & \ldots \
a_{21} & a_{22} & \ldots \
\vdots & \vdots & \ddots
\end{array} \right) \cdot
\left( \begin{array}{ccc}
b_{11} & b_{12} & \ldots \
b_{21} & b_{22} & \ldots \
\vdots & \vdots & \ddots
\end{array} \right) =
\left( \begin{array}{ccc}
a_{11}\cdot b_{11} & a_{12}\cdot b_{12} & \ldots \
a_{21}\cdot b_{21} & a_{22}\cdot b_{22} & \ldots \
\vdots & \vdots & \ddots
\end{array} \right)
$$

So it does render on submission, but not in the live preview unfortunately. And unlike Markdown, LaTeX wasn’t exactly designed to look comprehensible in plain-text too.

@kalid Could you add live editing support like Math.StackExchange has to your Discourse to-do list?

Ah! Yes, it’s only on submission, but not preview. I’m using a MathJax plugin and it seems it’s not wired up for the previews.

Short term fix is to preview in https://stackedit.io/ but I’d like to get it integrated here. Thanks for the suggestion.

After some digging, it seems this is a bug in the MathJax plugin. I have a workaround currently (it’ll refresh the LaTex 500ms after you stop typing)

$$ a^2 + b^2 = c ^2 $$

(From a tech perspective, it seems the previewRefreshed event is not firing.)

Do you mean that you have the fix pending? Because it is not working for me in Chrome.

Hrm, just trying in chrome now:

$$a^2 + b^2 = c^2$$

Could you try hard-refreshing the page (ctrl + F5 or ctrl + shift + F5). Discourse caches a lot of javascript and probably only loaded the header once (if you have the same browser window open). Thanks for helping debug!

Did not work, even after restarting Chrome and hard-refreshing. Odd as we’re using to same browser (did you try in others?).

Trying in Firefox right now…

$$a^2 + b^2 = c^2$$

Yep, the preview renders the MathJax (after a delay). Could you open Chrome’s Developer Tools (tools menu > developer tools) and see if there are any red errors appearing in the console?

Still not working.

There’s an uncaught TypeError: undefined is not a function. It occurs on line 81 of VM49 11 (so it’s referring to Discourse.ComposerView.prototype.on):

Discourse.ComposerView.prototype.on("keyDown", function(){
    if (mathTimeout) { clearTimeout(mathTimeout); }
    mathTimeout = setTimeout(typesetMath, 500);
});

Manually calling typesetMath() from the console displays the math between the $$ bounded block tags properly, but not the inline $ bounded ones.

Also, the math preview disappears as soon as you edit the input again; does that occur for you too?

Whoops, thank you! I logged in with a non-admin account and the same thing happened.

It should be fixed up now (fingers crossed). This is a workaround until the official plugin gets updated. (I may take a crack at exactly what is going on but need to dive into Discourse more.)

Now I don’t need to call typesetMath() manually anymore, but the issues of inline math and flickering previews still remains.

I suppose this should be left to the plug-in makers; but I am wondering: why hasn’t someone fixed this in a pull request already?