Adding Math Formula Support to Hugo
I happened to need it, so I just went ahead and solved it, based on KaTeX. It supports math formulas almost perfectly; both inline and display formulas can be rendered directly without modifying or adding any Shortcodes. The only drawback is that symbols with a double meaning in Markdown syntax itself, such as *, need to be escaped as \*.
Steps
1. Modify extend_head.html
All paths below are described with the site root directory as the current directory:
If layouts/partials/extend_head.html exists, simply append the content at the end; if it doesn’t exist, create that path and copy the specified file into the directory for editing. The command is as follows:
| |
Following the KaTeX documentation Auto-render Extension, add the following content:
| |
2. Usage
Add the following field to the front matter of the article in which you want to use math formulas:
| |
You can also add the above setting to archetypes/defaults.md, so that the field is automatically added every time a new article is created. In addition, changing true to false will disable the math formula feature in that article.
The usage format is the same as Markdown math formulas. Just be aware of the characters that need escaping. Here is an example, in which * is escaped as \*.
| |
$$0.414213562373095048\approx6*16^{-1}+a*16^{-2}+0*16^{-3}+\cdots$$