facelessuser / pymdown-extensions

Extensions for Python Markdown
https://facelessuser.github.io/pymdown-extensions/
Other
953 stars 255 forks source link

Katex support for Arithmatex extension #185

Closed nmanumr closed 6 years ago

nmanumr commented 6 years ago

I recently updated to the latest version of pymdown-extensions Here I am facing an issue rendering equations using katex.

An algebraic expression consisting of two terms such as $a+x$, $ax+b$ etc is called binomial or a binomial expression.

## Binomial Formula
The following expressions are valid for any $n\in Z^n$. (n is called index)

$$ (a+b)^n = a^n +\frac{n}{1!} a^{n-1} b^1+ \frac{n(n-1)}{2!} a^{n-2} b^2 + \frac{n(n-1)(n-2)}{3!} a^{n-3} b^3 +...+ b^n $$

$$ (a+b)^n = ^nC_0 a^n + ^nC_1 a^{n-1} b^1 + ^nC_2 a^{n-2} b^2 +...+ ^nC_r a^{n-r} b^r +...+^nC_n a^{n-n} b^n $$

$$ (a+b)^n = \displaystyle\sum_{r=0}^n \binom{n}{r} a^{n-r} b^r $$

Here is the output of above markdown before update image

And here is the output of above markdown after update image

Further, I noticed that the output of each math equation become something like this: image But in previous version its output is something like this: image

facelessuser commented 6 years ago

This was mentioned over at the MkDocs material repo, but I want to post this here for others who come along so they understand what happened.

Version 4.6.0 recently simplified arithmatex logic to output in MathJax script format (wrapped in a script). The reason why this wasn't done before is that the script format won't show a fallback preview while loading. But I recently discovered how to do previews with the script format, so I migrated to that to have one output format.

In short, Arithmatex was originally written for MathJax, so that was the only library I checked against. I was either unaware or have since forgotten that libraries like KaTeX are available to do this as well. So this change was backwards compatible for MathJax, which I test for, but not for anything else.

With that said, I see no reason why I can't add an option to bring back the plain format (non-script).

tueda commented 6 years ago

For now, the following seems to be a workaround, instead of the normal auto-render of KaTeX (though I'm not a JavaScript expert and it's not thoroughly tested)

renderMathInElement(document.body);
var items = document.getElementsByClassName("MathJax_Preview");
for (var i = 0; i < items.length; i++) {
  katex.render(items[i].innerText, items[i]);
}
facelessuser commented 6 years ago

@tueda Thanks for the workaround! That is a good temporary workaround.

If adding additional JavaScript is not possible, or unwanted, you can also just lock skip the the current 4.6.0 version in your dependencies. 4.7.0 is planned to bring back old functionality, most likely under a new boolean options. Though I will bring back the options to configure how the content is wrapped. I am hoping over the weekend to get the old functionality back.

facelessuser commented 6 years ago

4.7.0 will bring back the generic output via the generic option.