Skip to main content

A Sphinx extension for rendering math in HTML pages

Project description

sphinxcontrib-katex

sphinxcontrib.katex's documentation on Read the Docs sphinxcontrib-katex's supported Python versions sphinxcontrib.katex's MIT license

A Sphinx extension for rendering math in HTML pages.

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.

Installation

To install sphinxcontrib.katex into your Python virtual environment run:

$ pip install sphinxcontrib-katex

If you want to pre-render the math by running Javascript on your server instead of running it in the browsers of the users, you have to install nodejs.

Usage

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

extensions = ['sphinxcontrib.katex']

To enable server side pre-rendering add in addition (nodejs installation needed):

katex_prerender = True

See the Configuration section for all available settings.

Configuration

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

katex_css_path = \
    'https://cdn.jsdelivr.net/npm/katex@0.16.8/dist/katex.min.css'
katex_js_path = 'katex.min.js'
katex_autorender_path = 'auto-render.min.js'
katex_inline = [r'\(', r'\)']
katex_display = [r'\[', r'\]']
katex_prerender = False
katex_options = ''

The specific delimiters written to HTML when math mode is encountered are controlled by the two lists katex_inline and katex_display.

If katex_prerender is set to True the equations will be pre-rendered on the server and loading of the page in the browser will be faster. On your server you must have a katex executable installed and in your PATH as described in the Installation section.

The string variable katex_options allows you to change all available official KaTeX rendering options, e.g.

katex_options = r'''{
    displayMode: true,
    macros: {
        "\\RR": "\\mathbb{R}"
    }
}'''

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

LaTeX Macros

Most probably you want to add some of 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 have to add them as the latex_macros setting in your conf.py and specify them using proper LaTeX syntax. Afterwards you can include them via the sphinxcontrib.katex.latex_defs_to_katex_macros function into katex_options and add them to the LaTeX preamble:

import sphinxcontrib.katex as katex

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}
"""

# Translate LaTeX macros to KaTeX and add to options for HTML builder
katex_macros = katex.latex_defs_to_katex_macros(latex_macros)
katex_options = 'macros: {' + katex_macros + '}'

# Add LaTeX macros for LATEX builder
latex_elements = {'preamble': latex_macros}

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Version 0.9.7 (2023-07-03)

  • Changed: use KaTeX 0.16.8

  • Removed: support for Python 3.7

Version 0.9.6 (2023-06-13)

  • Changed: use custom sphinx theme based on insipid for HTML documentation pages

Version 0.9.5 (2023-06-07)

  • Changed: use KaTeX 0.16.7

  • Fixed: convert KaTeXServer.timeout_error() to class method

Version 0.9.4 (2023-01-04)

  • Changed: use KaTeX 0.16.4

Version 0.9.3 (2022-11-25)

  • Changed: reverted enforcement of 100% font scale as a larger scaling of 1.21em is the expected KaTeX default behavior

Version 0.9.2 (2022-11-25)

  • Fixed: update Python package version number

Version 0.9.1 (2022-11-25)

  • Added: support for Python 3.11

  • Changed: use KaTeX 0.16.3

  • Changed: enforce 100% of document font-size for HTML

Version 0.9.0 (2022-08-19)

  • Added: local KaTeX server to dramatically speed up pre-rendering

  • Added: katex.min.js and auto-render.min.js are now included in the Python package

  • Added: support for Python 3.10

  • Changed: use KaTeX 0.16.0

  • Removed: support for Python 3.6

Version 0.8.6 (2021-05-27)

  • Fixed: allow to work with Sphinx>=4.0.0

Version 0.8.5 (2021-05-26)

  • Fixed: remove extra space after inline math when using pre-rendering

Version 0.8.4 (2021-05-18)

  • Changed: increase top padding of equations by 2px

Version 0.8.3 (2021-05-18)

  • Fixed: building of documentation on RTD

Version 0.8.2 (2021-05-18)

  • Fixed: PyPI package version number

Version 0.8.1 (2021-05-18)

  • Fixed: PyPI package had wrong version number

Version 0.8.0 (2021-05-18)

  • Added: support for Python 3.9

  • Added: support for Sphinx>=4.0.0

  • Added: tests for Windows and macOS

  • Changed: switch to KaTeX 0.13.11

  • Changed: switched CI tests from Travis to Github Actions

  • Changed: running sphinx will now fail in pre-render mode if KaTeX fails

  • Removed: support for Python 2.7, 3.4, 3.5

Version 0.7.2 (2021-04-28)

  • Fixed: Sphinx>=4.0.0 is not supported at the moment

Version 0.7.1 (2020-10-29)

  • Fixed: label of fraction example in docs

Version 0.7.0 (2020-10-29)

  • Added: fraction example to docs

  • Changed: switch to KaTeX 0.12.0

  • Changed: add small top and bottom padding to equations

Version 0.6.1 (2020-05-25)

  • Fixed: run katex under Windows

Version 0.6.0 (2020-02-13)

  • Changed: switch to Katex 0.11.1

  • Changed: add tests for Python 3.7 and 3.8

Version 0.5.1 (2019-08-13)

  • Added: equation numbers in documentation (#16)

  • Changed: subset of tests for sphinx<=1.6 (#23)

  • Changed: several improvements to documentation

Version 0.5.0 (2019-07-25)

  • Added: katex server side pre-rendering (#15)

  • Changed: switch to Katex 0.10.2 (#17)

  • Removed: deprecated Sphinx setup_math (#10)

Version 0.4.1 (2019-01-08)

  • Fixed: macros example in documentation

Version 0.4.0 (2018-12-14)

  • Added: Sphinx documentation and setup RTD page

  • Added: Travis-CI tests

  • Changed: KaTeX version 0.10.0

  • Changed: make compatible with sphinx>=1.6

  • Removed: configuration option katex_version

Version 0.3.1 (2018-10-08)

  • Fixed: incompatibility with sphinx>=1.8 (#8)

Version 0.3.0 (2018-09-06)

  • Added: allow for user defined autorendering delimiters (#7)

  • Fixed: bug if katex_options was blank (#5)

Version 0.2.0 (2018-06-22)

  • Added: document all configuration settings

  • Added: automatic setting of delimiters for KaTeX auto-renderer

  • Removed: katex_macros option

Version 0.1.6 (2018-04-12)

  • Added: equation numbering across pages with sphinx>=1.7

  • Changed: KaTeX version 0.9.0

Version 0.1.5 (2017-12-19)

  • Added: helper function to convert LaTeX defs to KaTeX macros

  • Changed: improvement of code readability

  • Fixed: mouse over for equation numbers in Firefox

Version 0.1.4 (2017-11-27)

  • Changed: move equation numbers to the right and center vertically

Version 0.1 (2017-11-24)

  • Added: initial release

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.9.7.tar.gz (99.1 kB view details)

Uploaded Source

Built Distribution

sphinxcontrib_katex-0.9.7-py3-none-any.whl (97.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sphinxcontrib-katex-0.9.7.tar.gz
  • Upload date:
  • Size: 99.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for sphinxcontrib-katex-0.9.7.tar.gz
Algorithm Hash digest
SHA256 3a6754b1cfc90e141e3f73e0835eafc8262da5fafe7a4c674fa708fab79cebd7
MD5 ffa6f9490d9c44f5eb8e971ac51ba450
BLAKE2b-256 04612b61f5cdcb1a63f071f3b56bf5d56dfa05d35cabd49a9c1c0a1b2efa55dc

See more details on using hashes here.

File details

Details for the file sphinxcontrib_katex-0.9.7-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinxcontrib_katex-0.9.7-py3-none-any.whl
Algorithm Hash digest
SHA256 30a5d772ebdc120431b41f36ed6a989b04329efc499b4ec9301f469e7cc69e9a
MD5 fd93b6f234afb4bad18e3592768490b9
BLAKE2b-256 8816e6ff9d3be5f3f99e33a8bf2afbe9cf5e64880789c5715c71a200f14b687f

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