2023-08-17 23:09:49 +01:00
---
title: "LaTeX"
weight: 4
---
2025-06-14 14:36:10 +01:00
2025-08-13 15:14:46 +08:00
LaTeX math expressions are rendered using \(\KaTeX\) by default. Simply start including them in your Markdown content without any manual configurations.
2023-08-17 23:09:49 +01:00
2025-08-13 15:14:46 +08:00
## Usage
2023-08-17 23:09:49 +01:00
2025-08-13 15:14:46 +08:00
You can use LaTeX for both inline expressions and for larger blocks of text.
2023-08-17 23:09:49 +01:00
2025-08-13 15:14:46 +08:00
### Inline Math
To include an expression within a line of text, wrap it in `\(` and `\)` delimiters.
2023-08-17 23:09:49 +01:00
```markdown {filename="page.md"}
2025-08-13 15:14:46 +08:00
This \(\sigma(z) = \frac{1}{1 + e^{-z}}\) is an inline expression.
2023-08-17 23:09:49 +01:00
```
2025-08-13 15:14:46 +08:00
This \( \sigma(z) = \frac{1}{1 + e^{-z}} \) is an inline expression.
### Display Math
2023-08-17 23:09:49 +01:00
2025-08-13 15:14:46 +08:00
For expressions that you want to stand on their own in a separate paragraph, use `$$` delimiters.
2023-08-17 23:09:49 +01:00
```markdown {filename="page.md"}
2025-08-13 15:14:46 +08:00
$$F(\omega) = \int_{-\infty}^{\infty} f(t)\, e^{-j \omega t} \, dt$$
2023-08-17 23:09:49 +01:00
```
will be rendered as:
2025-08-13 15:14:46 +08:00
$$F(\omega) = \int_{-\infty}^{\infty} f(t)\, e^{-j \omega t} \, dt$$
2024-12-30 23:13:23 +00:00
2025-08-13 15:14:46 +08:00
You can also use LaTeX environments like `aligned` for multi-line expressions.
2024-12-30 23:13:23 +00:00
```latex {filename="page.md"}
$$
\begin{aligned}
\nabla \cdot \mathbf{E} & = \frac{\rho}{\varepsilon_0} \\
\nabla \cdot \mathbf{B} & = 0 \\
\nabla \times \mathbf{E} & = -\frac{\partial \mathbf{B}}{\partial t} \\
\nabla \times \mathbf{B} & = \mu_0 \left( \mathbf{J} + \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t} \right)
\end{aligned}
$$
```
will be rendered as:
$$
\begin{aligned}
\nabla \cdot \mathbf{E} & = \frac{\rho}{\varepsilon_0} \\
\nabla \cdot \mathbf{B} & = 0 \\
\nabla \times \mathbf{E} & = -\frac{\partial \mathbf{B}}{\partial t} \\
\nabla \times \mathbf{B} & = \mu_0 \left( \mathbf{J} + \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t} \right)
\end{aligned}
$$
2023-08-17 23:09:49 +01:00
2025-08-13 15:14:46 +08:00
For a list of supported functions, see [KaTeX supported functions ](https://katex.org/docs/supported.html ).
### Chemistry Expressions
The [mhchem][mhchem] extension is enabled by default, allowing you to easily render chemistry equations and formulas.
Inline: \(\ce{H2O}\) is water.
Separate paragraph:
```markdown {filename="page.md"}
$$\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}$$
```
will be rendered as:
$$\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}$$
2025-06-14 14:36:10 +01:00
## Configuration
> [!IMPORTANT]
> Please enable and configure the [passthrough extension](https://gohugo.io/content-management/mathematics/) in the Hugo configuration file, so that Hugo can detect LaTeX math expressions in your Markdown content.
```yaml {filename="hugo.yaml"}
markup:
goldmark:
extensions:
passthrough:
delimiters:
block: [['\[', '\]'], ["$$", "$$"]]
inline: [['\(', '\)']]
enable: true
```
2025-08-13 15:14:46 +08:00
### Math Engine
2023-08-17 23:09:49 +01:00
2025-08-13 15:14:46 +08:00
[KaTeX][katex] is the default engine used to render LaTeX math expressions during the build process supported by [Hugo][hugo-transform-tomath].
2023-08-19 00:32:10 +01:00
2025-08-13 15:14:46 +08:00
The default is KaTeX, but you can also switch to [MathJax][mathjax] if you need features only available in MathJax.
2023-08-19 00:32:10 +01:00
2025-08-13 15:14:46 +08:00
#### KaTeX
2023-08-19 00:32:10 +01:00
2025-08-13 15:14:46 +08:00
The default setup requires no configuration. Hugo fetches the KaTeX CSS from the CDN.
If you need to pin a specific version of KaTeX or use local assets, you can do so in your `hugo.yaml` file.
2023-08-19 00:32:10 +01:00
2025-08-13 15:14:46 +08:00
##### Override CDN base URL
2023-08-19 00:32:10 +01:00
2025-08-13 15:14:46 +08:00
```yaml {filename="hugo.yaml"}
params:
math:
engine: katex
katex:
base: "https://cdn.jsdelivr.net/npm/katex@0 .16.22/dist"
2023-08-19 00:32:10 +01:00
```
2025-08-13 15:14:46 +08:00
##### Use local assets
2025-06-14 14:36:10 +01:00
2025-08-13 15:14:46 +08:00
You can also place the css file under `assets` and publish additional font files required by KaTeX.
2025-06-14 14:36:10 +01:00
2025-08-13 15:14:46 +08:00
```yaml {filename="hugo.yaml"}
params:
math:
engine: katex
katex:
css: "css/katex.min.css"
assets:
- "fonts/KaTeX_Main-Regular.woff2"
# Add other font files here
```
2025-06-14 14:36:10 +01:00
2025-08-13 15:14:46 +08:00
It will load the KaTeX CSS file from `assets/css/katex.min.css` instead of downloading from CDN.
2025-06-14 14:36:10 +01:00
2025-08-13 15:14:46 +08:00
#### MathJax
2025-06-14 14:36:10 +01:00
2025-08-13 15:14:46 +08:00
Alternatively, you can use [MathJax][mathjax] to render math expressions:
2025-06-14 14:36:10 +01:00
```yaml {filename="hugo.yaml"}
params:
math:
engine: mathjax
```
2025-08-13 15:14:46 +08:00
> [!NOTE]
> You can further customize MathJax (for example, adjust loader options, or change the CDN/source) by overriding the template at `layouts/_partials/scripts/mathjax.html` in your project. Hugo will use your version instead of the theme's default.
2025-06-14 14:36:10 +01:00
[katex]: https://katex.org/
[mathjax]: https://www.mathjax.org/
2025-08-13 15:14:46 +08:00
[mhchem]: https://mhchem.github.io/MathJax-mhchem/
[hugo-transform-tomath]: https://gohugo.io/functions/transform/tomath/