Skip to main content

LaTeX pre-rendering for pelican

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.
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
# increasing it but if startup takes longer than one second, something is
# probably seriously broken.
# KATEX_STARTUP_TIMEOUT = 1.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"""
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
"""

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()

Having a per-file preamble is sadly not possible as of February 23rd, 2020. Pelican parses the content and metadata of files at the same time before notifying any plugins. So by the time any per-file preamble could be applied, the contents of the file have already been parsed.

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

Uploaded Source

Built Distribution

pelican_katex-1.5.0-py3-none-any.whl (165.3 kB view details)

Uploaded Python 3

File details

Details for the file pelican-katex-1.5.0.tar.gz.

File metadata

  • Download URL: pelican-katex-1.5.0.tar.gz
  • Upload date:
  • Size: 164.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

File hashes

Hashes for pelican-katex-1.5.0.tar.gz
Algorithm Hash digest
SHA256 172d234ebb0651bbad65f96932eac9997370257ef89fdb6c1eadb06d64d1d906
MD5 9c92a62e2240b7c8615c40413f14cd7c
BLAKE2b-256 b32197e6ee05fcc9aa78034001f94b837d865206f7cacf2bee97ea3a4a4c8fc8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pelican_katex-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 165.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

File hashes

Hashes for pelican_katex-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f14296db64fed5db989e83996899a4cd9396c93741e96588e60ee2206b7af27
MD5 cf57418ff74198006c65a4101c6cef9a
BLAKE2b-256 ea1a725cc924e4903285bf71424adc8290c6bd8a2b052708816dd2462c8e2d89

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