Skip to main content

LaTeX pre-rendering for pelican with katex.js

Project description

LaTeX Pre-rendering for Pelican

pelican-katex integrates LaTeX rendering directly into the pelican generation process and eliminates the delay in displaying math you usually experience on the web. It does so by hooking itself into docutils' reStructuredText parser as well as the markdown package and processing the formulas with KaTeX. The generated HTML pages only contain the finished HTML/MathML output. Therefore, you do not need to ship the KaTeX javascript implementation with your website anymore and improve the accessibility as well as the load time of your internet presence.

For a demo visit this blog post. Notice how all the formulas are just there. There is no loading and the website does not even serve the javascript part of KaTeX.

Note, that you still need to include the KaTeX stylesheets with your website, for example

<link rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/katex/dist/katex.min.css"
      crossorigin="anonymous">

Installation

First of all, you need to install nodejs so that pelican-katex can run KaTeX. Then run pip install pelican-katex and add "pelican_katex" to the PlUGINS setting in your configuration file. Finally, remove the katex.js <script> tag from your template and enjoy a lighter website and instant formulas.

Syntax

reStructuredText
~~~~~~~~~~~~~~~~

In rst you write inline math with the usual math role (:math:`f(x)`) or
block math with

.. math::

    \int \textrm{math block}.

# markdown

In markdown you get inline math in between $ signs, like $f(x) = \sqrt{x}$.
Note, that $ only creates a math environment if it is preceded by whitespace
or at the beginning of a block and followed by some non-whitespace character.
This is necessary so that you can still write about the 5$ in your pocket. To
write a dollar sign preceded by whitespace, escape it with a backslash as in
\$10. Block math is triggered with

$$\int \textrm{math block}.$$

Math blocks can have linebreaks but no empty lines.

Configuration

The plugin offers several configuration options that you can set in your pelicanconf.py.

# nodejs binary path or command to run KaTeX with.
# KATEX_NODEJS_BINARY = "node"

# Path to the katex file to use. This project comes with version `0.10` of
# katex but if you want to use a different one you can overwrite the path
# here. To use a katex npm installation, set this to `"katex"`.
# KATEX_PATH = "/path/to/katex.js"

# By default, this plugin will redefine reStructuredText's `math` role and
# directive. However, if you prefer to have leave the docutil's defaults
# alone, you can use this to define a `katex` role for example.
# KATEX_DIRECTIVE = "katex"

# How long to wait for the initial startup of the rendering server. You can
# increase it but if startup takes longer than a few seconds, something is
# probably seriously broken.
# KATEX_STARTUP_TIMEOUT = 5.0

# Time budget in seconds per call to the rendering engine. 1 second should
# be plenty since most renderings take less than 50ms.
# KATEX_RENDER_TIMEOUT = 1.0

# Define a preamble of LaTeX commands that will be prepended to any rendered
# LaTeX code.
# KATEX_PREAMBLE = None

# Here you can pass a dictionary of default options that you want to run
# KaTeX with. All possible options are listed on KaTeX's options page,
# https://katex.org/docs/options.html.
# KATEX = {
#     # Abort the build instead of coloring broken math in red
#     "throwOnError": True
# }

Preamble

The KATEX_PREAMBLE option allows you to share definitions between all of your math blocks across all files. It takes a string of any LaTeX commands you would like, for example

KATEX_PREAMBLE = r"""
\def\ceil#1{\lceil #1 \rceil}
\def\floor#1{\lfloor #1 \rfloor}
"""

If you have a large preamble, it might be nice to extract it into a .tex file. Note, that pelican will not be aware of changes made to that file in autoreload mode and you will have to restart pelican manually.

from pathlib import Path
KATEX_PREAMBLE = Path("preamble.tex").read_text()

You can also add more definitions per file to the preamble with preamble-blocks that do not produce any output.

reStructuredText
~~~~~~~~~~~~~~~~

.. math::
   :preamble:

   \def\pelican{\textrm{pelican}^2}

This definition will be available in subsequent blocks

.. math::

   \sqrt{\pelican}

or inline :math:`\pelican = 1`.

# markdown

In markdown it is not as easy to define properties of blocks, so we chose to
start a preamble block with an @ such as

$$@
\def\pelican{\textrm{pelican}^2}
$$

which works just the same in blocks

$$\sqrt{\pelican}$$

or inline $\pelican = 1$.

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

pelican_katex-1.8.4.tar.gz (175.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pelican_katex-1.8.4-py3-none-any.whl (177.6 kB view details)

Uploaded Python 3

File details

Details for the file pelican_katex-1.8.4.tar.gz.

File metadata

  • Download URL: pelican_katex-1.8.4.tar.gz
  • Upload date:
  • Size: 175.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pelican_katex-1.8.4.tar.gz
Algorithm Hash digest
SHA256 a01c0851e0af145b55c2e9c5db822cdeb6616b1adfe55aa1a9ff259106aca317
MD5 d59ba8f1a5e04c7ee9480cbbf57f0de7
BLAKE2b-256 82b68f1a4c4fbbb5e4634febb8b460e38e7cf20e3c8a886b6cfdcf0ee2618503

See more details on using hashes here.

Provenance

The following attestation bundles were made for pelican_katex-1.8.4.tar.gz:

Publisher: auto-publish.yml on martenlienen/pelican-katex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pelican_katex-1.8.4-py3-none-any.whl.

File metadata

  • Download URL: pelican_katex-1.8.4-py3-none-any.whl
  • Upload date:
  • Size: 177.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pelican_katex-1.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c9d81dadf90d5bfcba473d83bd74429e6e182fb2637986ad9fc8f16d88aae1f1
MD5 8497e559d2dd98b581613675b9578c1e
BLAKE2b-256 11f7dce2d778d53016c9cf1b2144b7fcadeb5f9ba21d077c6a6c8cb970f5a903

See more details on using hashes here.

Provenance

The following attestation bundles were made for pelican_katex-1.8.4-py3-none-any.whl:

Publisher: auto-publish.yml on martenlienen/pelican-katex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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