惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
N
News | PayPal Newsroom
WordPress大学
WordPress大学
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Hacker News: Front Page
有赞技术团队
有赞技术团队
Scott Helme
Scott Helme
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
V
Visual Studio Blog
S
Securelist
Jina AI
Jina AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
P
Palo Alto Networks Blog
K
Kaspersky official blog
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
Webroot Blog
Webroot Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
博客园 - 【当耐特】
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LINUX DO - 最新话题
Latest news
Latest news
T
Tenable Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿

FrWalker Blog

代码开发与环境管理工具汇总 求职-LeetCode热题100刷题记录 新机迁移-hexo\vscode\便携git\miniconda\i卡环境配置 通用印刷电路板全自动布线算法 基于元器件手册的智能建库算法 博客语法记录 柔性电路板FPC自动布线 拓竹P1S初体验 cdn加速hexo博客_2次开发hexo-cdn-jsdelivr 修改博客加密插件hexo-blog-encrypt 3.1.9,适配移动端 强化学习-1-多臂老虎机 强化学习-2-马尔可夫决策过程 强化学习-3-动态规划 强化学习-4-时序差分 强化学习-5-Dyna-Q算法 强化学习-6-DQN算法 2024华为软件精英挑战赛-智慧港口 3.liunx shell及其脚本理解 2.linux文件系统及用户管理
不更换插件解决Hexo博客Latex公式的渲染问题
FrWalker · 2025-01-16 · via FrWalker Blog

发表于更新于

字数总计:568阅读时长:2分钟 新加坡

hexohexolatexmathjaxkatex

不更换插件解决Hexo博客Latex公式的渲染问题

问题描述

在Hexo博客中,使用默认的MathJax或者Katex渲染数学公式时,会出现渲染错误,导致公式无法正常显示。

解决方法

这里不考虑更换hexo渲染器的解决方案,容易导致各种样式问题和兼容性问题,主要采纳一些简单的语法技巧来解决。

1. 必要的情况下使用\进行转义

alt text alt text

上图的公式在hexo无法正常显示,但在_前面加上\后可以正常显示:
alt text
原因不明,来源参考EasyHexo:
alt text

2. 使用<span>包裹公式,注意标签独占一行

很多公式直接写渲染异常,但用或者

标签将LaTeX公式包裹起来,这样公式内容就不会被markdown渲染器识别为转义字符,就会显示正常,典型的比如换行字符\\.
不使用<span>标签的公式渲染效果如下:

1
2
3
4
5
6
7
8
9
$$
\begin{aligned}
V(s)& =\mathbb{E}[G_t|S_t=s] \\
&=\mathbb{E}[R_t+\gamma R_{t+1}+\gamma^2R_{t+2}+\ldots|S_t=s] \\
&=\mathbb{E}[R_t+\gamma(R_{t+1}+\gamma R_{t+2}+\ldots)|S_t=s] \\
&=\mathbb{E}[R_t+\gamma G_{t+1}|S_t=s] \\
&=\mathbb{E}[R_t+\gamma V(S_{t+1})|S_t=s]
\end{aligned}
$$

使用<span>标签的公式渲染效果如下:

1
2
3
4
5
6
7
8
9
10
11
<span>
$$
\begin{aligned}
V(s)& =\mathbb{E}[G_t|S_t=s] \\
&=\mathbb{E}[R_t+\gamma R_{t+1}+\gamma^2R_{t+2}+\ldots|S_t=s] \\
&=\mathbb{E}[R_t+\gamma(R_{t+1}+\gamma R_{t+2}+\ldots)|S_t=s] \\
&=\mathbb{E}[R_t+\gamma G_{t+1}|S_t=s] \\
&=\mathbb{E}[R_t+\gamma V(S_{t+1})|S_t=s]
\end{aligned}
$$
</span>

3. 引入js代码

MathJax:

1
2
3
4
5
6
7
8
9
10
<script>
MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']]
}
};
</script>
<script id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
</script>

Katex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.8/dist/katex.min.css" integrity="sha384-GvrOXuhMATgEsSwCs4smul74iXGOixntILdUW9XmUC6+HX0sLNAK3q71HotJqlAn" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.8/dist/katex.min.js" integrity="sha384-cpW21h6RZv/phavutF+AuVYrr+dA8xD9zs6FwLpaCct6O9ctzYFfFr4dgmgccOTx" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.8/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
renderMathInElement(document.body, {


delimiters: [
{left: '$$', right: '$$', display: true},
{left: '$', right: '$', display: false},
{left: '\\(', right: '\\)', display: false},
{left: '\\[', right: '\\]', display: true}
],

throwOnError : false
});
});
</script>

其中,2和3的方法来源于KEVIN’S BLOG

头像头像

FrWalker

a blog for sharing my thoughts and experiences

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 FrWalker Blog