Skip to main content

A `Sphinx extension`_ for rendering math in HTML pages.

Project description

The extension uses KaTeX for rendering of math in HTML pages. It is designed as a replacement for the built-in extension sphinx.ext.mathjax, which uses MathJax for rendering.

Usage

Installation:

pip install sphinxcontrib-katex

In conf.py of your sphinx project, add the extension with:

extensions = [
    'sphinxcontrib.katex',
    ]

Configuration

The behavior of the sphinxcontrib.katex can be changed by configuration entries in the conf.py file of your documentation project. In the following all configuration entries are listed and their default values are shown.

katex_version = 0.9
katex_css_path = 'https://cdn.jsdelivr.net/npm/katex@' + \
                 katex_version + \
                 '/dist/katex.min.css'
katex_js_path = 'https://cdn.jsdelivr.net/npm/katex@' + \
                 katex_version + \
                 '/dist/contrib/auto-render.min.js'
katex_inline = [r'\(', r'\)']
katex_display = [r'\[', r'\]']
katex_options = {}

The version of KaTeX used is controlled by the katex_version config setting, which per default is also automatically added to the URL strings for the KaTeX CSS and JS files. The specific delimiters written to HTML when math mode is encountered are controlled by katex_inline and katex_display.

The katex_options setting allows you to change all available official KaTeX rendering options.

You can also add KaTeX auto-rendering options to the katex_options, but be aware that the delimiters entry gets always overwritten by the entries of katex_inline and katex_display.

LaTeX Macros

Most probably you want to add some ogf your LaTeX math commands for the rendering. In KaTeX this is supported by LaTeX macros (\def). You can use the katex_options configuration setting to add those:

katex_options = {
    r'''macros:  {
        "\\i": "\\mathrm{i}",
        "\\e": "\\mathrm{e}^{#1}",
        "\\vec": "\\mathbf{#1}",
        "\\x": "\\vec{x}",
        "\\d": "\\operatorname{d}\\!{}",
        "\\dirac": "\\operatorname{\\delta}\\left(#1\\right)",
        "\\scalarprod": "\\left\\langle#1,#2\\right\\rangle",
    }
    '''
    }

The disadvantage of this option is that those macros will be only available in the HTML based Sphinx builders. If you want to use them in the LaTeX based builders as well you can add them in an extra file in your projet, for example definitions.py:

latex_macros = r"""
    \def \i                {\mathrm{i}}
    \def \e              #1{\mathrm{e}^{#1}}
    \def \vec            #1{\mathbf{#1}}
    \def \x                {\vec{x}}
    \def \d                {\operatorname{d}\!}
    \def \dirac          #1{\operatorname{\delta}\left(#1\right)}
    \def \scalarprod   #1#2{\left\langle#1,#2\right\rangle}
"""

Note, that we used proper LaTeX syntax here and not the special one required for katex_options. This is fine as sphinxcontrib.katex provides a function to translate to the required KaTeX syntax. To use our definitions for HTML and LaTeX Sphinx builders add the following to your conf.py.

import sys

import sphinxcontrib.katex as katex

# Allow import/extensions from current path
sys.path.insert(0, os.path.abspath('.'))
from definitions import latex_macros

# Translate LaTeX macros to the required KaTeX format and add to options
katex_macros = katex.latex_defs_to_katex_macros(latex_macros)
katex_options = 'macros: {' + katex_macros + '}'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinxcontrib-katex-0.2.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

sphinxcontrib_katex-0.2.0-py2.py3-none-any.whl (9.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file sphinxcontrib-katex-0.2.0.tar.gz.

File metadata

File hashes

Hashes for sphinxcontrib-katex-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b0b5c3387b02dd14923e59ead7318bc7e631327b0e432d29e5f40ee68651a3d5
MD5 98a922f08da84b61f8b1c494a9390715
BLAKE2b-256 46a8ee8bd917f6096f616e4b1f26db62416a29e17bbad014442ed6ef8630fdfd

See more details on using hashes here.

File details

Details for the file sphinxcontrib_katex-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sphinxcontrib_katex-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5001d37db8e4eaab36cdbc8b6f7c133ddb98a1d390ab3cf95dbaaaa57cbea0e2
MD5 eedc5c35a76c457f098d2706300587ed
BLAKE2b-256 7af338fc80a568509d3178aba11e6f8f4b744b0f24ba8455dc31e0200f582e04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page