Discourse Math Plugin

official

(Mark McClure) #44

Ahh, the parsing logic is in
assets/javascripts/lib/discourse-markdown/discourse-math.js.es6

That looks quite managable.


(Jason Sachs) #45

Usual MathJax issues: although I haven’t tried the plugin (yet – I need to twist some arms first in our internal Discourse instance), I would strongly argue against heuristics in parsing the $ character, in favor of one of the following:

  • per-post trigger of math (checkbox? but that’s intrusive in the UI… not sure if CommonMark allows directives like !math on or something)
  • configurability of MathJax delimeters – the \( and \) are common (recommended!) alternatives to $

Heuristics can be helpful, but when they are wrong then what do you do?


(William Di Luigi) #46

I can’t find this setting in my admin console. Is this normal?


(Joshua Rosenfeld) #47

Markdown It is no longer experimental, so that setting was removed. You should not need to enable it. I edited that out of the first post.


(William Di Luigi) #48

Thanks. However, even though I enabled discourse-math from the admin panel, I still I can’t seem to make it work on my discourse instance :frowning:

See here: OIS COATS: Problema di comprensione - Aiuto - Forum Allenamenti OII

Do you have an idea about what could be missing?


(William Di Luigi) #49

Solved. I just needed to rebake all posts.


(xiasummer) #50

Could this support syntax like this for commonmark?

  1. I see this in your post on CommonMark. I find it very powerful and supports align and other important latex formulas.

https://talk.commonmark.org/t/mathjax-extension-for-latex-equations/698

It supports commands like

\begin{align} \end{align}

like the examples here

http://kasperpeulen.github.io/stmd/js/

#### TeX Samples

The following equations are represented in the HTML source code as LaTeX expressions.

**The Lorenz Equations**
\begin{aligned}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{aligned}

**The Cauchy-Schwarz Inequality**
\[
\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)
\]

**A Cross Product Formula**
\[ \mathbf{V}_1 \times \mathbf{V}_2 =  \begin{vmatrix}
\mathbf{i} & \mathbf{j} & \mathbf{k} \\
\frac{\partial X}{\partial u} &  \frac{\partial Y}{\partial u} & 0 \\
\frac{\partial X}{\partial v} &  \frac{\partial Y}{\partial v} & 0
\end{vmatrix} \]

**The probability of getting $k$ heads when flipping $n$ coins is**

$$P(E)   = {n \choose k} p^k (1-p)^{ n-k}$$

**A Rogers-Ramanujan Identity**

\[1 +  \frac{q^2}{(1-q)}+\frac{q^6}{(1-q)(1-q^2)}+\cdots =
\prod_{j=0}^{\infty}\frac{1}{(1-q^{5j+2})(1-q^{5j+3})},
\quad\quad \text{for $|q|<1$}.\]

**Maxwell’s Equations**

\begin{aligned}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\   \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0 \end{aligned}

Finally, while display equations look good for a page of samples, the ability to mix math and text in a paragraph is also important. This expression $\sqrt{3×-1}+(1+x)^2$ is an example of an inline equation. As you see, MathJax equations can be used this way as well, without unduly disturbing the spacing between lines. 

(Sam Saffron) #51

Why don’t you try it out on try.discourse.org, discourse math is enabled there.


(xiasummer) #52

https://try.discourse.org/t/discourse-math-plugin/907/1

Yes, I tried

Seems, only $ can be transformed other latex commands seems to be not working.


(Sam Saffron) #53

$$ is block level, so you need

$$
Text here
$$

(Agustín Santiago Gutiérrez) #54

I had created an Issue on github for this, but the Issue Tracker seems to have disappeared.

“Same line math”, that is, things like $a+b$ using single $, do not display at all when inside a “spoiler” tag (The “hide details” thing).

For example

The following formula is shown as plain ascii $a+b+c$

However, the next one is perfectly rendered:
$$
a+b+c
$$

I have just updated to the most recent version of both discourse and discourse-math (1.9.0.beta15 and 0.9 , respectively) and this bug seems to always occur, no special configuration to reproduce, just put some latex within a “spoiler tag”.


(Agustín Santiago Gutiérrez) #55

I emphasize, such a thing only happens inside these “hide details” elements. Outside of them, within a typical reply, latex shows perfectly both with $ and with $$.


(Michael Brown) #56

Problem with the version on try:

If there’s a block at the top of the post and you add text above it, you get a JS error and the preview stops rendering:

e.g. Try Discourse Math - Demo


#57

I searched for posts that discusses the Asciimath syntax and didn’t see any.

From the MathJax Asciimath page:

By default, you mark mathematical expressions written in this form by surrounding them in “back-ticks”, i.e., ....

Suggest a combination of what is available today and a backtick to maintain some consistency with MathJax.

For example:

$` E=MC^2 `$

And

$$` 
E=MC^2
`$$

(xiasummer) #58

Can you add a switch to let people choose whether allowing real time previewing?

I usually write a lot of formulas in discourse. But I find it very slow when I have added too many formulas.

If I can choose, I’d like to stop the real time previewing of the formulas when editing, just let the formulas there, and when we finish editing and post the whole reply, then the mathjax take effects and make the $xxx$ turn into formulas. That will save a lot of calculation I think.

Yours.


(Sam Saffron) #59

I think the correct fix here is to limit the pain you experience cause there is a ton of caching we are not doing that we should do to avoid this being such a painful experience.

Can you post an example problem post here with tons of formulas so we can use it for testing?


(xiasummer) #60

Should this do it? The BSM PDE formula

$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $
$\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2} \frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
	+ \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
	\mathrm r \mathrm C $

(Stephen Chung) #61

Can’t you just… Er… Turn off preview?


(Sam Saffron) #62

Sure, but preview should not be “recalculating” every single block of math it should have a local cache of sorts so it only calculates math for changed blocks.


(xiasummer) #63

Good idea. I tried “Hilde the preview”, then editing will be much faster if there are already many equations.