1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-12-18 18:36:07 +01:00
mkdocs-material/docs/reference/math.md

199 lines
5.6 KiB
Markdown
Raw Normal View History

2020-07-24 16:11:42 +02:00
---
2022-06-19 13:01:44 +02:00
icon: material/alphabet-greek
2020-07-24 16:11:42 +02:00
---
2023-05-23 17:30:23 +02:00
# Math
2020-07-24 16:11:42 +02:00
2024-01-20 02:53:16 +01:00
[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
2023-05-23 17:30:23 +02:00
with Material for MkDocs easily.
2020-07-24 16:11:42 +02:00
2021-10-10 12:19:14 +02:00
[MathJax]: https://www.mathjax.org/
[LaTeX]: https://en.wikibooks.org/wiki/LaTeX/Mathematics
[MathML]: https://en.wikipedia.org/wiki/MathML
[AsciiMath]: http://asciimath.org/
2023-05-13 13:34:57 +02:00
[KaTeX]: https://katex.org/
2020-07-24 16:11:42 +02:00
2023-05-23 17:30:23 +02:00
2020-07-24 16:11:42 +02:00
## Configuration
2024-01-20 02:53:16 +01:00
The following configuration enables support for rendering block and
2023-05-23 17:30:23 +02:00
inline block equations using [MathJax] and [KaTeX].
### MathJax
2024-01-20 02:53:16 +01:00
[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
2023-05-23 17:30:23 +02:00
to your `mkdocs.yml`.
2020-07-24 16:11:42 +02:00
2022-09-11 19:25:40 +02:00
=== ":octicons-file-code-16: `docs/javascripts/mathjax.js`"
2020-07-24 16:11:42 +02:00
``` js
window.MathJax = {
tex: {
inlineMath: [["\\(", "\\)"]],
displayMath: [["\\[", "\\]"]],
processEscapes: true,
processEnvironments: true
},
options: {
ignoreHtmlClass: ".*|",
processHtmlClass: "arithmatex"
}
};
2021-12-11 14:30:07 +01:00
document$.subscribe(() => { // (1)!
2024-01-20 02:53:16 +01:00
MathJax.startup.output.clearCache()
MathJax.typesetClear()
MathJax.texReset()
MathJax.typesetPromise()
})
2020-07-24 16:11:42 +02:00
```
2021-10-10 12:19:14 +02:00
1. This integrates MathJax with [instant loading].
2023-05-23 17:30:23 +02:00
=== ":octicons-file-code-16: `mkdocs.yml`"
2020-07-24 16:11:42 +02:00
``` yaml
2021-10-10 12:19:14 +02:00
markdown_extensions:
- pymdownx.arithmatex:
generic: true
2020-07-24 16:11:42 +02:00
extra_javascript:
2023-05-23 17:30:23 +02:00
- javascripts/mathjax.js
- https://unpkg.com/mathjax@3/es5/tex-mml-chtml.js
2020-07-24 16:11:42 +02:00
```
2023-05-23 17:30:23 +02:00
See additional configuration options:
- [Arithmatex]
[Arithmatex]: ../setup/extensions/python-markdown-extensions.md#arithmatex
[instant loading]: ../setup/setting-up-navigation.md#instant-loading
<script id="MathJax-script" async src="https://unpkg.com/mathjax@3/es5/tex-mml-chtml.js"></script>
2024-01-20 02:53:16 +01:00
<script>
window.MathJax = {
tex: {
inlineMath: [["\\(", "\\)"]],
displayMath: [["\\[", "\\]"]],
processEscapes: true,
processEnvironments: true
},
options: {
ignoreHtmlClass: ".*|",
processHtmlClass: "arithmatex"
}
};
</script>
2023-05-23 17:30:23 +02:00
### KaTeX
2024-01-20 02:53:16 +01:00
[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
2023-05-23 17:30:23 +02:00
[KaTeX] within your project, add the following lines to your `mkdocs.yml`.
=== ":octicons-file-code-16: `docs/javascripts/katex.js`"
``` js
document$.subscribe(({ body }) => { // (1)!
renderMathInElement(body, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false },
{ left: "\\(", right: "\\)", display: false },
{ left: "\\[", right: "\\]", display: true }
],
})
})
```
1. This integrates KaTeX with [instant loading].
=== ":octicons-file-code-16: `mkdocs.yml`"
2023-05-13 13:34:57 +02:00
``` yaml
markdown_extensions:
- pymdownx.arithmatex:
generic: true
2024-01-20 02:53:16 +01:00
2023-05-13 13:34:57 +02:00
extra_javascript:
2024-01-20 02:53:16 +01:00
- javascripts/katex.js
- https://unpkg.com/katex@0/dist/katex.min.js
- https://unpkg.com/katex@0/dist/contrib/auto-render.min.js
2024-01-20 02:53:16 +01:00
2023-05-13 13:34:57 +02:00
extra_css:
- https://unpkg.com/katex@0/dist/katex.min.css
2023-05-13 13:34:57 +02:00
```
2020-07-24 16:11:42 +02:00
## Usage
### Using block syntax
Blocks must be enclosed in `#!latex $$...$$` or `#!latex \[...\]` on separate
lines:
2020-07-24 16:11:42 +02:00
2023-05-23 17:30:23 +02:00
``` latex title="block syntax"
2020-07-24 16:11:42 +02:00
$$
2024-10-08 14:35:43 +02:00
\cos x=\sum_{k=0}^{\infty}\frac{(-1)^k}{(2k)!}x^{2k}
2020-07-24 16:11:42 +02:00
$$
```
<div class="result" markdown>
2020-07-24 16:11:42 +02:00
$$
2024-10-08 14:35:43 +02:00
\cos x=\sum_{k=0}^{\infty}\frac{(-1)^k}{(2k)!}x^{2k}
2020-07-24 16:11:42 +02:00
$$
</div>
2020-07-24 18:29:34 +02:00
### Using inline block syntax
2020-07-24 16:11:42 +02:00
2020-07-24 18:29:34 +02:00
Inline blocks must be enclosed in `#!latex $...$` or `#!latex \(...\)`:
2020-07-24 16:11:42 +02:00
2023-05-23 17:30:23 +02:00
``` latex title="inline syntax"
2024-01-20 02:53:16 +01:00
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
2020-07-24 16:11:42 +02:00
that $f(a)=f(b)$.
```
<div class="result" markdown>
2020-07-24 16:11:42 +02:00
2024-01-20 02:53:16 +01:00
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
2020-07-24 16:11:42 +02:00
that $f(a)=f(b)$.
</div>
2023-05-23 17:30:23 +02:00
## Comparing MathJax and KaTeX
2024-01-20 02:53:16 +01:00
When deciding between MathJax and KaTeX, there are several key factors to
2023-05-23 17:30:23 +02:00
consider:
2024-01-20 02:53:16 +01:00
- __Speed__: KaTeX is generally faster than MathJax. If your site requires
rendering large quantities of complex equations quickly, KaTeX may be the
better choice.
2023-05-23 17:30:23 +02:00
2024-01-20 02:53:16 +01:00
- __Syntax Support__: MathJax supports a wider array of LaTeX commands and can
process a variety of mathematical markup languages (like AsciiMath and MathML).
If you need advanced LaTeX features, MathJax may be more suitable.
2023-05-23 17:30:23 +02:00
2024-01-20 02:53:16 +01:00
- __Output Format__: Both libraries support HTML and SVG outputs. However,
MathJax also offers MathML output, which can be essential for accessibility,
as it is readable by screen readers.
2023-05-23 17:30:23 +02:00
2024-01-20 02:53:16 +01:00
- __Configurability__: MathJax provides a range of configuration options,
allowing for more precise control over its behavior. If you have specific
rendering requirements, MathJax might be a more flexible choice.
2023-05-23 17:30:23 +02:00
2024-01-20 02:53:16 +01:00
- __Browser Support__: While both libraries work well in modern browsers,
MathJax has broader compatibility with older browsers. If your audience uses a
variety of browsers, including older ones, MathJax might be a safer option.
2023-05-23 17:30:23 +02:00
2024-01-20 02:53:16 +01:00
In summary, KaTeX shines with its speed and simplicity, whereas MathJax offers
more features and better compatibility at the expense of speed. The choice
2023-05-23 17:30:23 +02:00
between the two will largely depend on your specific needs and constraints.