laozhu / hugo-nuo

🎨 A responsive light & clean hugo theme for blogger.
https://laozhu.me
MIT License
427 stars 150 forks source link

Use KaTeX instead of MathJax #97

Closed VincentTam closed 5 years ago

VincentTam commented 5 years ago

KaTeX renders math much quicker than MathJax. To get an idea of how slow the later can be, you may try Math Meta.SE's MathJax tutorial.

You may consult, for example, davidar/TeX.js#9 for more info. Despite the closing of this linked issue, KaTeX has eventually been implemented in that project.

Therefore, many Hugo themes ship with KaTeX, notably Beautiful Hugo.

Hsins commented 5 years ago

I think that this issue is not an issue because you can switch the math equation render between KaTeX and MathJax by modifying the import script resource.

Besides, there are some equations not parse in KaTeX but MathJax.

VincentTam commented 5 years ago

A quick search for "mathjax" in this repo shows that MathJax is embedded in this theme's Go-HTML and SCSS template. I would like to know how you can "switch the math equation renderer between KaTeX and MathJax" without getting deep into the code.

Besides, there are some equations not parse in KaTeX but MathJax.

Mind giving an example? I only know that AMScd is not (and won't be) supported, and that it lacks support for equation auto-referencing. However, MathJax's support for AMScd is so primitive that professional mathematicians would hardly use them.

Hsins commented 5 years ago

Take a look in this file:

<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

It include a js file to render with MathJax. If you want to render with KaTeX, we can get the code from its offical site:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/katex.css" integrity="sha384-xNwWFq3SIvM4dq/1RUyWumk8nj/0KFg4TOnNcfzUU4X2gNn3WoRML69gO7waf3xh" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/katex.js" integrity="sha384-UP7zD+aGyuDvxWQEDSRYcvoTxJSD82C6VvuEBktJZGo25CVhDstY9sCDHvyceo9L" crossorigin="anonymous"></script>

Well, it seems that many rendering issues are done. But can we use something below in KaTeX?

% 定義指令
\newcommand{\water}{H_{2}O}
% 使用
\water

image

VincentTam commented 5 years ago

Thanks for your suggestion, but why don't we consider incorporating that into the project through a PR/a commit? In fact, many Hugo themes have chosen KaTeX instead of MathJax, such as BH, Finite, Minos, etc.

This has been documented in Macros section of supported functions.

$$\def\water{\rm H_2O} \rm 2\, H_2 + O_2 \to 2\, \water$$
laozhu commented 5 years ago

@VincentTam @Hsins Thanks for all of your suggestions, I haven't heard KaTeX before, but I will consider supporting KaTeX after an investigation.

Maybe KaTeX and MathJax can be both supported through a switcher in config.toml, the default is MathJax for compatibility, you can switch to KaTex if you like it.