数学¶
¥Math¶
MathJax和KaTeX是两个在浏览器中显示数学内容的流行库。虽然这两个库提供类似的功能,但它们使用不同的语法并具有不同的配置选项。本文档网站提供了有关如何轻松地将它们与 Material for MkDocs 集成的信息。
¥MathJax and KaTeX are two popular libraries for displaying mathematical content in browsers. Although both libraries offer similar functionality, they use different syntaxes and have different configuration options. This documentation site provides information on how to integrate them with Material for MkDocs easily.
配置¶
¥Configuration¶
以下配置支持使用MathJax和KaTeX渲染块和内联块方程。
¥The following configuration enables support for rendering block and inline block equations using MathJax and KaTeX.
MathJax¶
¥MathJax¶
MathJax是一个功能强大且灵活的库,支持多种输入格式,例如LaTeX 、 MathML 、 AsciiMath ,以及各种输出格式,例如 HTML、SVG 和 MathML。要在项目中使用 MathJax,请将以下几行添加到mkdocs.yml中。
¥MathJax is a powerful and flexible library that supports multiple input formats, such as LaTeX, MathML, AsciiMath, as well as various output formats like HTML, SVG, MathML. To use MathJax within your project, add the following lines to your mkdocs.yml.
window.MathJax = {
tex: {
inlineMath: [["\\(", "\\)"]],
displayMath: [["\\[", "\\]"]],
processEscapes: true,
processEnvironments: true
},
options: {
ignoreHtmlClass: ".*|",
processHtmlClass: "arithmatex"
}
};
document$.subscribe(() => { // (1)!
MathJax.startup.output.clearCache()
MathJax.typesetClear()
MathJax.texReset()
MathJax.typesetPromise()
})
这将 MathJax 与即时加载集成在一起。
¥This integrates MathJax with instant loading.
查看其他配置选项:
¥See additional configuration options:
卡特克斯¶
¥KaTeX¶
KaTeX是一个轻量级库,专注于速度和简洁性。它支持部分 LaTeX 语法,并可以将数学表达式渲染为 HTML 和 SVG。要在项目中使用KaTeX ,请将以下几行添加到mkdocs.yml文件中。
¥KaTeX is a lightweight library that focuses on speed and simplicity. It supports a subset of LaTeX syntax and can render math to HTML and SVG. To use KaTeX within your project, add the following lines to your mkdocs.yml.
document$.subscribe(({ body }) => { // (1)!
renderMathInElement(body, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false },
{ left: "\\(", right: "\\)", display: false },
{ left: "\\[", right: "\\]", display: true }
],
})
})
这将 KaTeX 与即时加载集成在一起。
¥This integrates KaTeX with instant loading.
用法¶
¥Usage¶
使用块语法¶
¥Using block syntax¶
块必须用$$...$$$$ ... $$或\[...\]\[ ... \]括起来,放在单独的行中:
¥Blocks must be enclosed in $$...$$ or \[...\] on separate lines:
使用内联块语法¶
¥Using inline block syntax¶
内联块必须括在$...$$ ... $或\(...\)\( ... \)中:
¥Inline blocks must be enclosed in $...$ or \(...\):
The homomorphism $f$ is injective if and only if its kernel is only the
singleton set $e_G$, because otherwise $\exists a,b\in G$ with $a\neq b$ such
that $f(a)=f(b)$.
同态\(f\)是单射当且仅当它的核仅为单例集\(e_G\) ,因为否则\(\exists a,b\in G\)且\(a\neq b\)使得\(f(a)=f(b)\) 。
¥The homomorphism \(f\) is injective if and only if its kernel is only the singleton set \(e_G\), because otherwise \(\exists a,b\in G\) with \(a\neq b\) such that \(f(a)=f(b)\).
MathJax 与 KaTeX 的比较¶
¥Comparing MathJax and KaTeX¶
在 MathJax 和 KaTeX 之间做出选择时,需要考虑几个关键因素:
¥When deciding between MathJax and KaTeX, there are several key factors to consider:
速度:KaTeX 通常比 MathJax 更快。如果您的网站需要快速渲染大量复杂的公式,KaTeX 可能是更好的选择。
语法支持:MathJax 支持更广泛的 LaTeX 命令,并能处理多种数学标记语言(例如 AsciiMath 和 MathML)。如果您需要高级 LaTeX 功能,MathJax 可能更适合您。
输出格式:这两个库都支持 HTML 和 SVG 输出。不过,MathJax 还提供 MathML 输出,这对于可访问性至关重要,因为它可以被屏幕阅读器读取。
可配置性:MathJax 提供了一系列配置选项,允许更精确地控制其行为。如果您有特定的渲染需求,MathJax 或许是一个更灵活的选择。
浏览器支持:虽然这两个库在现代浏览器中都能很好地运行,但 MathJax 与旧版浏览器的兼容性更佳。如果您的用户使用多种浏览器(包括旧版浏览器),那么 MathJax 可能是更安全的选择。
总而言之,KaTeX 以其速度和简洁性而出众,而 MathJax 则以牺牲速度为代价,提供了更多功能和更好的兼容性。两者之间的选择很大程度上取决于您的具体需求和限制。
¥Speed: KaTeX is generally faster than MathJax. If your site requires rendering large quantities of complex equations quickly, KaTeX may be the better choice.