当更换全局字体时,Mathjax渲染出的公式字体大小会异常。实际原因为Mathjax会自动调整渲染后的字符大小以及位置,与tex-chtml-full.js文件中的matchFontHeight的设置相关。源文件中该属性默认设为了!0,所以需要到源文件中进行修改为0以关闭自动调整,关闭之后上下标等内容存在异常,可以通过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
function func() {
console.log('Butterfly.js loaded!');
const mjxScripts = document.querySelectorAll('mjx-script');
mjxScripts.forEach(script => {
console.log(script.parentNode.tagName);
if (script.parentNode.tagName === 'MJX-MSUB') {
script.style.cssText = 'vertical-align: -0.2em !important';
}
else if (script.parentNode.tagName === 'MJX-MUNDEROVER') {
script.style.cssText = 'vertical-align: -0.3em !important';
}
script.childNodes.forEach(child => {
if (child.tagName === 'MJX-TEXATOM' || child.tagName === 'MJX-MI' || child.tagName === 'MJX-MN') {
child.style.cssText = 'font-size: 70.7% !important';
}
})
});
const mjxUnder = document.querySelectorAll('mjx-under, mjx-over');
mjxUnder.forEach(under => {
const texAtom = under.querySelector('mjx-texatom') || under.querySelector('mjx-mi') || under.querySelector('mjx-mn');
if (texAtom) {
if (texAtom.tagName === 'MJX-TEXATOM') {
texAtom.style.cssText = 'font-size: 70.7% !important; padding-left: 0.4em';
}
else
{
texAtom.style.cssText = 'font-size: 70.7% !important';
}
}
});
const mjxSub = document.querySelectorAll('mjx-msub, mjx-msup');
mjxSub.forEach(sub => {
sub.style.cssText = 'font-size: 100% !important';
});
}

var timesRun = 0;
function clock() {
timesRun += 1;
if (document.querySelector("mjx-script")) {
func();
clearInterval(int);
}
else if (timesRun > 5)
{
console.log('MathJax not found!');
clearInterval(int);
}
else
{
console.log('Waiting for MathJax...');
}
}

var int = self.setInterval("clock()", 1000);