User-Guide

LaTeX Math Formulas

Overview

Linen includes built-in server-side KaTeX rendering with no extra Hexo math plugins required:

  • Build-time pre-rendering: Formulas are rendered to HTML during hexo generate, with zero client-side JavaScript math parsing overhead.
  • On-demand assets: Bundled katex.min.css and .woff2 fonts are generated only when at least one post or page uses KaTeX, and the stylesheet is loaded only on pages containing rendered math.

Global Configuration

Configure latex in _config.linen.yml:

  • Quick enable

    _config.linen.yml
    1
    latex: true
  • Full options (custom CSS or KaTeX options)

    _config.linen.yml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    latex:
    enable: true
    css: "" # Optional local path or CDN URL for KaTeX CSS; leave empty to use bundled assets
    options:
    throwOnError: false # Default: false
    strict: ignore # Default: "ignore"
    macros:
    "\\RR": "\\mathbb{R}"
    "\\E": "\\mathbb{E}"

Page-level Configuration

Enable, disable, or customize KaTeX per post in Front Matter using latex (or its alias math):

1
2
3
4
---
title: Post with Math
latex: true # Or math: true; set false to disable on a single post when enabled globally
---

You can also pass page-level KaTeX options and macros (merged with global options):

1
2
3
4
5
6
7
---
title: Custom Macros Example
latex:
enable: true
macros:
"\\vect": "\\mathbf{#1}"
---

Syntax & Live Examples

This page has latex: true enabled. Below are source snippets alongside their live rendered output.

Inline Math

Wrap single-line expressions in single dollar signs $ ... $:

1
Mass-energy equivalence $E = mc^2$, Euler's identity $e^{i\pi} + 1 = 0$, and conditional probability $P(A \mid B) = \frac{P(A \cap B)}{P(B)}$.

Rendered output: Mass-energy equivalence E=mc2E = mc^2, Euler’s identity eiπ+1=0e^{i\pi} + 1 = 0, and conditional probability P(A∣B)=P(A∩B)P(B)P(A \mid B) = \frac{P(A \cap B)}{P(B)}.

To prevent currency values from being misparsed as math, inline math follows strict rules:

  • Cannot span multiple lines;
  • Opening $ cannot be followed by whitespace or preceded by a digit;
  • Closing $ cannot be preceded by whitespace or followed by a digit.

Values like $100 or from $10 to $20 remain untouched. To output a literal $ in edge cases, escape it with \$.

Display Math

Use double dollar signs $$ ... $$ for standalone single-line or multi-line equations:

1
2
3
$$
\int_{-\infty}^{+\infty} e^{-x^2}\,dx = \sqrt{\pi}
$$

Rendered output:

∫−∞+∞e−x2 dx=π\int_{-\infty}^{+\infty} e^{-x^2}\,dx = \sqrt{\pi}

Multi-line aligned environments and matrices are supported:

1
2
3
4
5
6
7
8
$$
\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 \mathbf{J} + \mu_0 \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t}
\end{aligned}
$$

Rendered output:

∇⋅E=ρε0∇⋅B=0∇×E=−∂B∂t∇×B=μ0J+μ0ε0∂E∂t\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 \mathbf{J} + \mu_0 \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t} \end{aligned}

Display math inside Markdown blockquotes (>) is also supported:

The probability density function of the normal distribution is:

f(x)=1σ2πexp⁡(−(x−μ)22σ2)f(x) = \frac{1}{\sigma \sqrt{2\pi}} \exp\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)

Automatically Skipped Regions

$ and $$ inside the following regions are ignored automatically:

  • Markdown fenced code blocks (``` / ~~~) and inline code spans (`$x$`)
  • Raw HTML <pre>, <code>, <script>, and <style> blocks
  • Hexo {% codeblock %} and tags
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---
title: LaTeX Math Formulas
excerpt: Write crisp math formulas with Linen's built-in server-side KaTeX rendering.
date: 2026-03-28 23:51
cover: /img/klara-kulikova-PgrSvkU1_4Y-unsplash.jpg
coverInfo:
author: Klara Kulikova
url: https://unsplash.com/photos/PgrSvkU1_4Y
series: User-Guide
tags: [Quick-Start, Configuration]
appendRawMarkdown: true
tocType: flat
latex: true
translations: ['zh-CN']
---

## Overview

Linen includes built-in server-side [KaTeX](https://katex.org/) rendering with no extra Hexo math plugins required:

- **Build-time pre-rendering**: Formulas are rendered to HTML during `hexo generate`, with zero client-side JavaScript math parsing overhead.
- **On-demand assets**: Bundled `katex.min.css` and `.woff2` fonts are generated only when at least one post or page uses KaTeX, and the stylesheet is loaded only on pages containing rendered math.

## Global Configuration

Configure `latex` in `_config.linen.yml`:

- **Quick enable**

```yaml _config.linen.yml
latex: true
```

- **Full options (custom CSS or KaTeX options)**

```yaml _config.linen.yml
latex:
enable: true
css: "" # Optional local path or CDN URL for KaTeX CSS; leave empty to use bundled assets
options:
throwOnError: false # Default: false
strict: ignore # Default: "ignore"
macros:
"\\RR": "\\mathbb{R}"
"\\E": "\\mathbb{E}"
```

## Page-level Configuration

Enable, disable, or customize KaTeX per post in Front Matter using `latex` (or its alias `math`):

```yaml
---
title: Post with Math
latex: true # Or math: true; set false to disable on a single post when enabled globally
---
```

You can also pass page-level KaTeX options and macros (merged with global `options`):

```yaml
---
title: Custom Macros Example
latex:
enable: true
macros:
"\\vect": "\\mathbf{#1}"
---
```

## Syntax & Live Examples

This page has `latex: true` enabled. Below are source snippets alongside their live rendered output.

### Inline Math

Wrap single-line expressions in single dollar signs `$ ... $`:

```markdown
Mass-energy equivalence $E = mc^2$, Euler's identity $e^{i\pi} + 1 = 0$, and conditional probability $P(A \mid B) = \frac{P(A \cap B)}{P(B)}$.
```

Rendered output: Mass-energy equivalence $E = mc^2$, Euler's identity $e^{i\pi} + 1 = 0$, and conditional probability $P(A \mid B) = \frac{P(A \cap B)}{P(B)}$.

To prevent currency values from being misparsed as math, inline math follows strict rules:

- Cannot span multiple lines;
- Opening `$` cannot be followed by whitespace or preceded by a digit;
- Closing `$` cannot be preceded by whitespace or followed by a digit.

Values like `$100` or `from $10 to $20` remain untouched. To output a literal `$` in edge cases, escape it with `\$`.

### Display Math

Use double dollar signs `$$ ... $$` for standalone single-line or multi-line equations:

```markdown
$$
\int_{-\infty}^{+\infty} e^{-x^2}\,dx = \sqrt{\pi}
$$
```

Rendered output:

$$
\int_{-\infty}^{+\infty} e^{-x^2}\,dx = \sqrt{\pi}
$$

Multi-line `aligned` environments and matrices are supported:

```markdown
$$
\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 \mathbf{J} + \mu_0 \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t}
\end{aligned}
$$
```

Rendered output:

$$
\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 \mathbf{J} + \mu_0 \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t}
\end{aligned}
$$

Display math inside Markdown blockquotes (`>`) is also supported:

> The probability density function of the normal distribution is:
>
> $$
> f(x) = \frac{1}{\sigma \sqrt{2\pi}} \exp\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)
> $$

### Automatically Skipped Regions

`$` and `$$` inside the following regions are ignored automatically:

- Markdown fenced code blocks (```` ``` ```` / `~~~`) and inline code spans (`` `$x$` ``)
- Raw HTML `<pre>`, `<code>`, `<script>`, and `<style>` blocks
- Hexo `{% codeblock %}` and `{% raw %}` tags