Registry / serialization / markdown-katex

markdown-katex

JSON →
library202406.1035pypypi✓ verified 23d ago

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.

pip install markdown-katex
INSTALL
IMPORT
SIG · MARKDOWN-KATEX
M
markdown-katex
serializationpythonv202406.1035
Install
2.8s avg
Import
192ms
Disk
122MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v202406.1035 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.196s · 120.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.8s · import 0.188s · 121MB
122MB installed
● package 122MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

KatexExtension
from markdown_katex import KatexExtension

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">
Debug
Known issues
gotchaThe `markdown-katex` extension specifically supports GitLab-style math delimiters: $`...`$ for inline math and ````math...```` for display math. Other common LaTeX delimiters like `$...$` or `$$...$$` will *not* be processed as math by this extension and will often be rendered as plain text or Markdown emphasis.
fix
Ensure your Markdown content uses the GitLab-style delimiters. If you require other delimiters, you might need a different Python Markdown extension or a custom preprocessor.
affects: All versions
gotchaMarkdown processors can interfere with LaTeX syntax. Characters like underscores (`_`), asterisks (`*`), or backslashes (`\`) within LaTeX math formulas might be interpreted by Python Markdown (e.g., as emphasis) before `markdown-katex` can process them, leading to incorrect rendering.
fix
Where possible, escape problematic Markdown characters within your LaTeX (e.g., `\_` instead of `_`). Consider adding additional Markdown extensions that handle math syntax more robustly (e.g., `pymdownx.arithmatex` in 'generic' mode, though `markdown-katex` provides its own parsing). For Python strings, use raw strings (e.g., `r'\frac{1}{2}'`) to avoid Python's backslash escaping interfering with LaTeX.
affects: All versions
gotchaFor KaTeX to render correctly in the browser, the generated HTML must include the KaTeX CSS stylesheet, typically from a CDN or a local copy. Additionally, the HTML document *must* start with `<!DOCTYPE html>` to prevent browsers from rendering in 'quirks mode,' which can cause display issues.
fix
Always include the KaTeX CSS in your HTML `<head>` section. Ensure your HTML files have the `<!DOCTYPE html>` declaration at the very top.
affects: All versions
gotchaThe `markdown-katex` extension bundles KaTeX binaries for server-side rendering, but it will prioritize using a globally installed `katex-cli` if found on the system. This can lead to unexpected behavior or version mismatches if your system's `katex-cli` is older or configured differently than the version bundled with the Python package.
fix
To ensure consistent rendering, manage your `katex-cli` installation (e.g., via `npm`) carefully or ensure it matches the bundled version. If specific behavior is required, you may need to control your `PATH` environment variable or explicitly manage the `katex-cli` executable used.
affects: All versions
Upgrade
Version history
202406.1035latest on PyPI · released Jun 24, 2024
Audit
Dependencies
markdownrequiredThis is a Python-Markdown extension, requiring the base `markdown` library to function.
Agent activity
11 hits · last 30 days
node
8
Resources