前言
由于我想要写一篇文章,其中需要写化学方程式,但anzhiyu主题似乎没自带,所以研究了一下,写了篇教程方便其他需要的小伙伴
2025/10/5补充:据评论区大佬 Prorise 提出,anzhiyu 博客主题其实是自带了对 katex 的支持,我再次翻阅文档后发现确实如此,并且大佬还给出了详细教程,在此非常感谢这位大佬
如果还没有使用我的方案,建议大家优先选择 Prorise 大佬的方案
效果演示
此处表达式转载自https://www.bilibili.com/read/cv39488642
$$ \text{Na}^\text{+}[\text{:}\mathop{\text{O}}\limits_{\mathbf{..}}^{\mathbf{..}}\text{:}\text{H}]^\text{-} $$
$$\ce{2Al2O3_{\text{(熔融)}}} \xlongequal[冰晶石]{电解} \ce{4Al + 3O2 ^}$$
$$\ce{N2(g) + 3H2(g) <=> 2NH3(g)} \qquad \Delta H = -92.5kJ/mol$$
教程
文章内
1
| npm install hexo-filter-katex --save
|
1 2 3 4 5 6 7 8
| katex: enable: true copy_tex: false mhchem: true cdn: css: https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css js: https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js mhchem: https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/mhchem.min.js
|
- 在
themes\anzhiyu\layout\includes\head.pug
中最底部添加
1 2 3 4
| if theme.katex.enable link(rel="stylesheet" href=theme.katex.cdn.css) script(defer src=theme.katex.cdn.js) script(defer src=theme.katex.cdn.mhchem)
|
评论区
感谢本文章评论区网友茯芺苒的置顶评论,在 twikoo 评论区中也可以写公式
- 在博客根目录
_config.anzhiyu.yml
中找到# Inject
配置项,引入 css
1 2 3 4
| inject: head: - <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">
|
- 在
source\js\
文件夹下新建名为twikoo.js
的文件并粘贴以下内容,没有 js 这个文件夹的也可自己新建,如果想要其他位置自行更改即可
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
| document.addEventListener('DOMContentLoaded', function() { if (typeof katex !== 'undefined') { try { if (typeof mhchem !== 'undefined') { console.log('mhchem扩展已加载'); } } catch (e) { console.warn('mhchem扩展加载可能有问题:', e); } } function renderAllMath() { if (typeof renderMathInElement !== 'function') { console.log('renderMathInElement函数未加载'); return; } const katexOptions = { delimiters: [ {left: '$$', right: '$$', display: true}, {left: '$', right: '$', display: false}, {left: '\\(', right: '\\)', display: false}, {left: '\\[', right: '\\]', display: true} ], throwOnError: false, strict: false, trust: true, macros: { "\\ce": "\\ce{#1}", "\\pu": "\\pu{#1}" } }; renderMathInElement(document.body, katexOptions); const commentArea = document.getElementById('tcomment'); if (commentArea) { renderMathInElement(commentArea, katexOptions); } } setTimeout(renderAllMath, 100); const observer = new MutationObserver(function(mutations) { let shouldRender = false; mutations.forEach(function(mutation) { if (mutation.addedNodes.length) { mutation.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.classList && ( node.classList.contains('tk-comment') || node.querySelector('.tk-comment') )) { shouldRender = true; } } }); } }); if (shouldRender) { setTimeout(renderAllMath, 200); } }); const commentContainer = document.getElementById('tcomment'); if (commentContainer) { observer.observe(commentContainer, { childList: true, subtree: true }); } window.addEventListener('popstate', function() { setTimeout(renderAllMath, 300); }); });
|
- 在博客根目录
_config.anzhiyu.yml
中找到# Inject
配置项,引入 js
1 2 3 4 5 6
| bottom: - <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"></script> - <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/mhchem.min.js"></script> - <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js"></script> - <script src="/js/twikoo.js"></script>
|
其他方案及补充说明
内容扩展:添加数学公式支持(KaTeX)
Hexo 插件使用——hexo-filter-mathjax
让 Hexo 搭建的博客支持 LaTeX
Twikoo 如何启用 katex 支持
为 hexo 博客更换 pandoc 渲染