This page illustrates how mathematical formulas written in Python-Markdown can be rendered using KaTeX with the help of the md_katex extension.
Demo
md_katex is a KaTeX plugin initially developed for use in this blog with Pelican. It focuses on rendering mathematical formulas directly in the browser using the KaTeX JavaScript library, rather than performing offline conversions.
Inline Formula
The md_katex extension supports two inline formula styles:
- GitLab Style: Delimited by
$`
and`$
- Brackets Style: Delimited by
\(
and\)
Markdown Example:
1 2 3 |
|
Rendered Output:
GitLab-style inline formula $`E=mc^2`$
: E=mc^2
Brackets-style inline formula \(E=mc^2\)
: E=mc^2
Generated HTML Source:
1 2 |
|
Block Formula
The md_katex extension also supports rendering block-level formulas in three styles:
- GitLab Style: Delimited by
~~~math
and~~~
- Brackets Style: Delimited by
\[
and\]
- GitHub Style: Delimited by
$$
and$$
GitLab-style Block Formula
Markdown Example:
1 2 3 |
|
Rendered Output:
Generated HTML Source:
1 2 3 |
|
Brackets-style Block Formula
Markdown Example:
1 2 3 |
|
Rendered Output:
GitHub-style Block Formula
Markdown Example:
1 2 3 |
|
Rendered Output:
Installation
First, ensure that you have the python-markdown
library installed. Then, you can install this plugin using the following command:
1 |
|
The generated HTML will include KaTeX formulas, and you will need to load KaTeX JavaScript on the frontend to complete the rendering.
To ensure proper rendering, include the KaTeX script via a <script>
tag in your HTML page. For example, using a CDN:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
References
- https://github.com/dbzhang800/md_katex
- https://katex.org/docs
- https://python-markdown.github.io/