用的是 Ataias Pereira Reis 给上游提交的 PR adityatelange/hugo-PaperMod#140 的代码,稍微改了一下开关选项。

layouts/partials/ 目录下创建文件

先创建 math.html

 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
<script>
    MathJax = {
        tex: {
            inlineMath: [["$", "$"], ["\\(", "\\)"]],
            displayMath: [["$$", "$$"]],
            processEscapes: true,
            processEnvironments: true,
            tags: "ams",
        },
        options: {
            skipHtmlTags: [
                "script",
                "noscript",
                "style",
                "textarea",
                "pre",
            ],
        },
        startup: {
            ready: () => {
                MathJax.startup.defaultReady();
                // Fix <code> tags after MathJax finishes running
                const all = MathJax.typesetPromise();
                all.then(() => {
                    document.querySelectorAll(".MathJax").forEach(
                        (el) => {
                            el.parentNode.className += " has-jax";
                        },
                    );
                });
            },
        },
    };
</script>
<script
    id="MathJax-script"
    async
    src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js"
></script>

然后创建 head.html 中增加如下的配置:

1
2
3
{{ if or .Params.math .Site.Params.math }}
{{ partial "math.html" . }}
{{ end }}

在 config 或者 page 的 front matter 中配置 math: true,就可以了。

1
$$y =x$$

$$y =x$$

1
测试 $y=x$ inline

测试 $y=x$ inline