When building a blog or website with Hugo, we often need to insert mathematical formulas into articles. However, by default, Hugo cannot directly render these mathematical formulas. Previously, there were many solutions available online, but recently, starting from Hugo v0.122.0, official support for mathematical formulas has been introduced. With some configuration and steps, we can successfully achieve LaTeX mathematical formula rendering in Hugo. For official documentation, you can refer to Hugo Mathematics.
Step 1: Check and Update Hugo Version
First, we need to ensure that we are using Hugo v0.122.0 or higher. You can check your current Hugo version with the following command:
|
|
If your Hugo version is lower than v0.122.0, you need to update Hugo. You can update via Homebrew (for macOS):
|
|
Or download the latest version of Hugo from the Hugo Releases page.
Step 2: Configure Goldmark Passthrough Extension
Enable the Goldmark passthrough extension in your site configuration file config.toml
to preserve raw Markdown with mathematical formulas. You only need to add or modify the following content:
|
|
Step 3: Create math.html
File in the Theme’s layouts/partials/
Directory
Create a math.html
file in your theme directory (e.g., themes/hugo-theme-stack/layouts/partials/
) and add the following content to load MathJax:
|
|
Step 4: Conditionally Call the Partial Template in the Base Template
Edit your base template layouts/_default/baseof.html
(in your theme directory, e.g., themes/hugo-theme-stack/layouts/_default/baseof.html
) to load mathematical formula support when needed. Add the following content:
|
|
Step 5: Include Mathematical Formulas in Markdown Files
Use LaTeX or TeX syntax to add mathematical formulas in your Markdown files. For example:
|
|
By following the above steps, you can successfully render LaTeX mathematical formulas on your Hugo site.