markdown-katex

202406.1035 · active · verified Sat Apr 11

markdown-katex is an extension for Python Markdown that adds KaTeX support. It processes LaTeX math formulas within Markdown text and converts them into HTML that can be rendered by KaTeX, often including bundled KaTeX binaries for server-side rendering, thus reducing reliance on client-side JavaScript. It primarily supports GitLab-style math delimiters. The current version is 202406.1035, with a release cadence that often aligns with KaTeX binary updates and bug fixes.

Warnings

Install

Imports

Quickstart

Initialize the `Markdown` parser with `KatexExtension` and convert Markdown text containing GitLab-style math formulas. The output HTML will contain the necessary KaTeX-generated markup. Note that for visual rendering, the KaTeX CSS must be linked in the final HTML document.

import markdown
from markdown_katex import KatexExtension

markdown_text = '''
# My Document with Math

Inline math: $`E=mc^2`$

Display math:
```math
x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}
```
'''

md = markdown.Markdown(extensions=[KatexExtension()])
html = md.convert(markdown_text)

print(html)

# For complete rendering, remember to include KaTeX CSS in your HTML:
# <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/katex.min.css" integrity="sha384-wcNdUgu4o+9NqyZ/lXkP5b/SjP/0XfC/vS+G0Hh0Q0Q=" crossorigin="anonymous">

view raw JSON →