設定數學式
在 Zensical 中,要做如下的設定才會秀出正確的數學方程式。這會啟用 Arithmatex,並載入 MathJax。
設定
在 docs/js 中加入:
js/mathjax.js
window.MathJax = {
tex: {
inlineMath: [["\\(", "\\)"]],
displayMath: [["\\[", "\\]"]],
processEscapes: true,
processEnvironments: true
},
options: {
ignoreHtmlClass: ".*|",
processHtmlClass: "arithmatex"
}
};
document$.subscribe(() => {
MathJax.startup.output.clearCache()
MathJax.typesetClear()
MathJax.texReset()
MathJax.typesetPromise()
})
component$.subscribe(({ ref }) => {
if (ref.classList.contains("md-annotation"))
MathJax.typesetPromise([ref])
})
並在 zensical.toml 中加入如下設定:
[project.markdown_extensions.pymdownx.arithmatex]
generic = true
[project]
extra_javascript = [
"js/mathjax.js",
"https://unpkg.com/mathjax@3/es5/tex-mml-chtml.js",
]
document$.subscribe 是為了搭配 instant navigation,換頁後數學式仍會重新排版。
測試
如果要獨立顯示數學公式,使用區塊語法 $$...$$ 或 \[...\],方程式通常會另起一行並置中顯示。
這是區塊公式:
\[
\int_0^1 x^2 \,dx = \frac{1}{3}
\]
如果要在文本段落內插入數學公式,使用行內語法 $...$ 或 \(...\),方程式通常不會換行。
這是行內公式 \(E = mc^2\)。