Typecho 增加 LaTeX 公式渲染

记录  ·  2026-03-20

使用 KaTeX

KaTeX自动渲染文档地址: https://katex.org/docs/autorender.html
在主题header.php文件内添加:

<script defer type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" />
<script defer type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js"></script>

如使用七牛云CDN加速,则将header.php内容改为:

<script defer type="text/javascript" src="https://cdn.staticfile.net/KaTeX/0.16.9/katex.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.staticfile.net/KaTeX/0.16.9/katex.min.css" />
<script defer type="text/javascript" src="https://cdn.staticfile.net/KaTeX/0.16.9/contrib/auto-render.min.js"></script>

在主题footer.php文件内添加:

<script type = "text/javascript" >
  document.addEventListener("DOMContentLoaded", function() {
    renderMathInElement(document.body, {
      delimiters: [{
          left: "$$",
          right: "$$",
          display: true
      }, {
          left: "$",
          right: "$",
          display: false
      }],
      ignoredTags: ["script", "noscript", "style", "textarea", "pre", "code"],
      ignoredClasses: ["nokatex"]
    });
  });
</script>
评论
亦桉小站. All Rights Reserved.
Your IP: 206.189.94.68

赣ICP备19000739号-3