katex extension for Python Markdown
Project description
markdown-katex
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 (mbarkhau@gmail.com) | author/maintainer | 2019-05 | - |
Install
$ pip install markdown-katex
...
$ python -m markdown_katex --version
markdown-katex version: v202008.1024 (using binary: /usr/local/bin/npx --no-install katex)
0.12.0
This package includes the following binaries:
katex-v0.12.0-x86_64-Linux
katex-v0.12.0-x86_64-Macos
katex-v0.12.0-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.12.0
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 .
Usage
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:
- For inline mode formulas: $`...`$
- For display mode formulas: ```math
Here is an example that uses this syntax.
There are two main advantages of this syntax:
- Gitlab has an existing Markdown renderer that can be used without the need to download any software. This implementation also uses KaTeX, so the output should be exactly the same as this extension.
- The fallback behaviour of other Markdown renderers is to render the raw LaTeX as inline code or a code block. This means that they won't inadvertently parse a LaTeX formula as Markdown syntax.
Hopefully other renderers will also adopt support for this syntax as:
- Rendering is done in the browser using KaTeX so implementation effort and should be minimal.
- The false positive rate for existing Markdown documents is negligible (ie. existing alternate use of $` syntax is minimal to non-existent).
Options
no_inline_svg
: Replace inline<svg>
with<img data:image/svg+xml;base64..">
tags.insert_fonts_css
: Insert font loading stylesheet (default: True).
Development/Testing
$ git clone https://gitlab.com/mbarkhau/markdown-katex
$ cd markdown-katex
$ make conda
$ make lint mypy test
MkDocs Integration
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}
WeasyPrint Integration
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)
Changelog for https://gitlab.com/mbarkhau/markdown-katex
Contributors
Thank you to for testing, reporting issues and contributing patches:
- @summersz - Richard Summers
- @bluhme3 - Evan Bluhm
- @pawamoy - Timothée Mazzucotelli
- @briankchan
- @spoorendonk
- @lisongmin
- @alexatadi
- @sacredfox - Akihiro Nomura
v202008.1026
- Fix #12: Bug in handling of paths with whitespace. (Thanks @summersz !)
v202008.1025
- Fix #9: Update
katex.css
- Fix #8: Lookup of binaries on windows
- Update documentation wrt. use with WeasyPrint
v202008.1024
v202006.1021
- Fix #7: File encoding issue on Windows.
v202006.1020
- Fix #7 katex-cli on Windows (now uses pkg --target node12...)
- Fix search for local
katex.ps1
,katex.cmd
,katex.exe
on Windows.
v202005.0017
- Allow use of
macro-file
option. - Update katex-cli to version v0.11.1
v202005.0016-beta
- Fix #6: Regression in code block parsing, introduced in
v202004.0015-beta
v202004.0015-beta
- Fix #3: Inline math inside block
v202004.0014-beta
- Fix #4: Link tag not properly closed
v202001.0013-beta
- Fix: Ignore trailing whitespace after closing fence.
v202001.0012-beta
- Fix: Remove extraneous whitespace to work better with whitespace: pre.
v201912.0011-beta
- Add option
insert_fonts_css
- Document options
v201910.0010-beta
- Add more prominent example to README.md
- Fix #2: Fix spurious log message when using MkDocs
v201908.0009-beta
- Fix #1: Wrong formulas are rendered when multiple formulas are in one doc.
v201907.0008-beta
- Fix: don't require typing package for py<35
v201905.0007-beta
- Fix: Parsing of inline code when using multiple backticks
v201905.0004-beta
- Fix: better error reporting
- Fix: cleanup temp dir
v201905.0002-alpha
- Initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file markdown-katex-202009.1026.tar.gz
.
File metadata
- Download URL: markdown-katex-202009.1026.tar.gz
- Upload date:
- Size: 47.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 956cd1a3aee2302839fe56b9914c493950cd359db233a4e68897d57bf3f35361 |
|
MD5 | befc42274253a9a3eb0c8c42cfe04016 |
|
BLAKE2b-256 | 2badab455d6734c8b2ea0a09101c5ba5e0f9e436c55c782bba9cbec00c0bac3c |
File details
Details for the file markdown_katex-202009.1026-py2.py3-none-any.whl
.
File metadata
- Download URL: markdown_katex-202009.1026-py2.py3-none-any.whl
- Upload date:
- Size: 47.8 MB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67fb883a823bc94ee7aac73ac2aff1324cef975abb43b3170e57d71b96b3fe4f |
|
MD5 | b27b625a21c28c96448cd68f91288798 |
|
BLAKE2b-256 | 4929352f42e446b51288a812491f641c25388a85c2049b945b82437010fefd1c |