Adds KaTeX support for Python Markdown
MIT License
This is an extension for Python Markdown which adds KaTeX support.
```math
f(x) = \int_{-\infty}^\infty
\hat f(\xi)\,e^{2 \pi i \xi x}
\,d\xi
```
Project/Repo:
Code Quality/CI:
Name | role | since | until |
---|---|---|---|
Manuel Barkhau ([email protected]) | author/maintainer | 2019-05 | - |
$ pip install markdown-katex
...
$ python -m markdown_katex --version
markdown-katex version: v202406.1035 (using binary: /usr/local/bin/npx --no-install katex)
0.15.1
This package includes the following binaries:
katex_v0.15.1_node10_x86_64_Linux
katex_v0.15.1_node10_x86_64_Darwin
katex_v0.15.1_node12_x86_64_Windows
If you are on a different platform, or want to use a more recent version of katex-cli
, you will need to install it via npm.
$ npx katex
$ npx katex --version
0.15.1
This extension will always use the locally installed version of KaTeX if it is available, instead of using the implementation bundled with this package.
No JavaScript is required to render the resulting HTML, so it can be used with more limited renderers (which don't support JavaScript) such as WeasyPrint .
Formulas can be created and edited interactively using the editor on katex.org. They also have some good documentation for the subset of LaTeX that is supported. When embedding these in your Markdown files, they must be marked with a special syntax in order to be rendered using KaTeX. There are many syntax extensions for Markdown that allow LaTeX formulas to be embedded, however this package only supports the syntax introduced by Gitlab:
Here is an example that uses this syntax.
There are two main advantages of this syntax:
Hopefully other renderers will also adopt support for this syntax as:
no_inline_svg
: Replace inline <svg>
with <img data:image/svg+xml;base64..">
tags.insert_fonts_css
: Insert font loading stylesheet (default: True).$ git clone https://gitlab.com/mbarkhau/markdown-katex
$ cd markdown-katex
$ make conda
$ make lint mypy test
In your mkdocs.yml
add this to markdown_extensions.
# mkdocs.yml
markdown_extensions:
- markdown_katex:
no_inline_svg: True
insert_fonts_css: True
macro-file: macros.tex
The macro-file
might looks something like this:
% macros.tex
\mymacro:\text{prefix #1 suffix}
When you generate html that is to be consumed by WeasyPrint, you need to use the no_inline_svg=True
option. This is due to a long standing limitation of WeasyPrint. Without this option, some KaTeX formulas will not render properly, e.g. \sqrt
md_ctx = markdown.Markdown(
extensions=[
'markdown.extensions.toc',
'markdown.extensions.extra',
'markdown.extensions.abbr',
...
'markdown_katex',
],
extension_configs={
'markdown_katex': {
'no_inline_svg': True, # fix for WeasyPrint
'insert_fonts_css': True,
},
}
)
raw_html_text = md_ctx.convert(md_text)
You can also use markdown-katex for the conversion of individual formulas from tex to html:
from markdown_katex.extension import tex2html
tex_text = r"""
\frac{1}{\left(\sqrt{\phi\sqrt{5}}-\phi\right)e^{\frac{2}{5}\pi}}=
1+\frac{e^{-2\pi}} {
1+\frac{e^{-4\pi}} {
1+\frac{e^{-6\pi}} {
1+\frac{e^{-8\pi}} {
1+\cdots
}
}
}
}
"""
options = {'no_inline_svg': True, 'insert_fonts_css': False}
html = tex2html(tex_text, options)