Beautiful Hugo includes built-in support for KaTeX, MathJax, and Mermaid (diagrams and flowcharts). Mermaid needs no configuration. For math, enable the Goldmark passthrough extension (see below).
Math Rendering
Choose the math rendering engine via mathEngine in your config. See Configuration — Math Engine for the full option reference and selfHosted behavior.
Passthrough
Markdown removes backslashes and other characters from LaTeX before the browser sees it. The Goldmark passthrough extension keeps the math unchanged. Hugo does not merge the [markup] section of a theme configuration, so put this in your site configuration:
[markup.goldmark.extensions.passthrough]
enable = true
[markup.goldmark.extensions.passthrough.delimiters]
block = [['\[', '\]'], ['$$', '$$']]
inline = [['\(', '\)']]
Single $ is not an inline delimiter, because prose often contains a stray $. Add ['$', '$'] to inline if you want it.
With passthrough enabled, the theme loads the KaTeX or MathJax files only on the pages that contain math. Set math: true in the front matter of a page to load them anyway, for example if a shortcode or a partial adds the math. Without passthrough, the theme loads them on every page, and you must escape the delimiters (\\( in place of \().
Server-side rendering
Set mathRender = "server" to render KaTeX at build time, as this site does. Pages then load only the KaTeX CSS, and the math is visible before any script runs. See Configuration — Server-side rendering for the limits.
Inline math
The golden ratio is . Euler’s identity states that .
Display math
Long equation test (mobile scroll behavior)
This very long equation tests horizontal scrolling on mobile devices:
Source
The golden ratio is \(\varphi = \frac{1+\sqrt{5}}{2}\).
$$
\varphi = \frac{1+\sqrt{5}}{2}
$$
KaTeX is loaded from CDN, or served from the site when selfHosted = true. MathJax is loaded from CDN even when selfHosted = true.
Mermaid
Mermaid renders diagrams from a simple text syntax. Write the diagram in a fenced code block with the mermaid language. A codeblock render hook turns it into a diagram. This is the recommended form. The mermaid shortcode is still supported and gives the same result.
Flowchart
Sequence Diagram
Gantt Chart
This one uses the shortcode:
Source — Fenced Code Block
```mermaid
graph TD
A[User visits site] --> B{Has session?}
B -->|Yes| C[Load preferences]
B -->|No| D[Show defaults]
C --> E[Render page]
D --> E
```
Source — Shortcode
{{< mermaid >}}
sequenceDiagram
participant U as User
participant B as Browser
U->>B: Navigate to page
B-->>U: Display page
{{< /mermaid >}}
Alignment
Set the horizontal alignment with a block attribute on the fence, or with the align parameter of the shortcode:
```mermaid {align="center"}
graph LR
A --> B
```
center— centered (recommended for most diagrams)left— left-alignedright— right-aligned
Block attributes need markup.goldmark.parser.attribute.block = true in the site configuration.
Dark Mode
Mermaid diagrams are dual-rendered for light and dark themes. The theme uses .theme-light and .theme-dark CSS classes internally, so the correct version displays automatically based on the user’s color scheme preference. No extra configuration is needed.