shime / livedown

Live Markdown previews for your favorite editor.
MIT License
765 stars 51 forks source link

Add KaTeX rendering (KaTeX 0.10.0) #50

Closed hcgatewood closed 3 years ago

hcgatewood commented 5 years ago

Add support for KaTeX rendering.

KaTeX

KaTeX is a JavaScript library for LaTeX-like equation rendering that trades the full-featured functionality of MathJax for much faster render speed. Try it out on their home page.

Updates to Livedown

To get KaTeX rendering working locally, we need to include three resources in the public/vendor directory, referenced from index.html:

Additionally, for full functionality, we want markdown-it to perform the server-side conversion to HTML for us (which the above resources then render properly in the browser). We would use markdown-it-katex, but per this issue and my own debugging, this "official" package does not support current KaTeX versions. Thus, we instead can use an up-to-date fork (thanks to @iktakahiro) which supports KaTeX 0.10, a recent-enough version of KaTeX.

Using KaTeX

Note that there are some differences between KaTeX and LaTeX. For example, the line break (\\) is not supported in $$ environments in KaTeX v0.10.0. Instead, use \begin{array} or \begin{aligned} for multi-line equations.

If you're not sure if/how some equation should render, check it out in the playground on KaTeX's landing page. See below for an example.

$$
\begin{aligned}
a_{n+1} &= \frac{x_{n+1} + nc_n}{n+1} \\
\iff a_{n+1} &= a_n + \frac{x_{n+1} - a_n}{n+1}
\end{aligned}
$$

Screen Shot 2019-03-11 at 7 18 58 PM