Skip to main content

A Python wrapper for the [KaTeX](https://katex.org/) library, providing fast server-side rendering of mathematical expressions.

Project description

PyKaTeX

A Python wrapper for the KaTeX library, providing fast server-side rendering of mathematical expressions.

PyKaTeX uses a C extension module with QuickJS-NG to enable Python to call into KaTeX, which is written in Javascript. The current version of PyKaTeX uses KaTeX v0.16.22 and QuickJS-NG v0.10.1.

⚠️ PyKaTex is currently in alpha. Some options are only partially supported (see the "usage" section for more information) and KaTeX exceptions are not handled gracefully yet.

Installation

pip install pykatex

Or, to install from source, clone the repository and run

pip install .

in the root of this repository.

Usage

PyKaTeX provides a single function renderToString that takes a LaTeX string and optional keyword arguments:

pykatex.renderToString(input, **options)

It can be used as follows.

import pykatex as katex

# Basic usage
html = katex.renderToString("E = mc^2", output="html")
print(html)

In PyKaTex, the options are passed to renderToString as optional keyword arguments that are mostly compatible with the KaTeX options, with the following exceptions:

  • The macros option is not yet implemented
  • Currently only string and boolean values are supported for the strict option (functions are not supported)

Note that you'll need to include KaTeX CSS in your HTML for proper rendering when using HTML output.

Options

Output Options

  • output (str): Output format. Options:

    • "html" (default): HTML with CSS classes
    • "mathml": MathML markup
    • "htmlAndMathml": Both HTML and MathML

    You can also use the provided constants:

    import pykatex
    html = pykatex.renderToString("x^2", output=pykatex.OUTPUT_HTML)
    mathml = pykatex.renderToString("x^2", output=pykatex.OUTPUT_MATHML)
    both = pykatex.renderToString("x^2", output=pykatex.OUTPUT_HTMLANDMATHML)
    

Display Options

  • displayMode (bool): Render in display mode (centered, larger). Default: False
  • leqno (bool): Place equation numbers on the left. Default: False
  • fleqn (bool): Left-align equations in display mode. Default: False

Error Handling

  • throwOnError (bool): Throw an exception on parsing errors. Default: True
  • errorColor (str): Color for error messages. Default: "#cc0000"

Styling

  • minRuleThickness (float): Minimum thickness of rules/lines
  • colorIsTextColor (bool): Use text color for styling. Default: False
  • maxSize (float): Maximum allowed size multiplier
  • maxExpand (float): Maximum number of macro expansions

Security & Behavior

  • strict (str or bool): Error reporting level:
    • "warn": Log warnings for unrecognized commands
    • "error": Throw errors for unrecognized commands
    • "ignore": Silently ignore unrecognized commands
    • True: Same as "error"
    • False: Same as "ignore"
  • trust (bool): Allow potentially unsafe commands. Default: False
  • globalGroup (bool): Place definitions in global scope. Default: False

Architecture

PyKaTeX consists of:

  • A Python C extension module (pykatex.c) that provides the Python interface
  • QuickJS JavaScript engine (src/quickjs/) for executing KaTeX
  • A wrapper script (katex.js) that calls into the KaTeX library (src/katex/)
  • QuickJS bytecode (katex.bytecode.h) for the wrapper script, generated by the QuickJS-NG compiler qjs
  • C wrapper code (katex.c) that bridges Python and JavaScript

The katex.bytecode.h file is generated by running

quickjs/build/qjsc -e -N katex -o katex.bytecode.h katex.js

in the src/ folder, and manually remove the JS_NewCustomContext and main functions. This needs to be done again when the wrapper script or KaTeX library is changed (for example, when KaTeX is updated to a new version).

License

This project bundles KaTeX and QuickJS-NG. Please refer to their respective licenses for terms and conditions.

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

pykatex-0.0.2.tar.gz (404.8 kB view details)

Uploaded Source

Built Distributions

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

pykatex-0.0.2-cp314-cp314t-musllinux_1_2_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

pykatex-0.0.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pykatex-0.0.2-cp314-cp314-musllinux_1_2_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

pykatex-0.0.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pykatex-0.0.2-cp313-cp313-musllinux_1_2_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

pykatex-0.0.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pykatex-0.0.2-cp312-cp312-musllinux_1_2_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

pykatex-0.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pykatex-0.0.2-cp311-cp311-musllinux_1_2_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

pykatex-0.0.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pykatex-0.0.2-cp310-cp310-musllinux_1_2_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

pykatex-0.0.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pykatex-0.0.2-cp39-cp39-musllinux_1_2_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

pykatex-0.0.2-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pykatex-0.0.2-cp38-cp38-musllinux_1_2_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ x86-64

pykatex-0.0.2-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file pykatex-0.0.2.tar.gz.

File metadata

  • Download URL: pykatex-0.0.2.tar.gz
  • Upload date:
  • Size: 404.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pykatex-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5d5102a01e5ba8ea393ff200865d6c3fca8fe91635f60e56701e53bffb137005
MD5 5456e1c12bbc8e416f73a1d46f5b7d08
BLAKE2b-256 89e784c7026fd53adc1aa1af414a7820c168e190edb4ea899a1767b86b2990a8

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8ff9b32ce9990116b408c0d2371d669bb4c5ce80edf0a576e28f94395d031ca9
MD5 5e59cefd28d6bd8caf815d05ac495b29
BLAKE2b-256 b293a5efa771615011346f58dd547ca6f15bf9d1df3d3226126134602aff07df

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9cb6da89dfa13c2bb1ca1a3ea2639bc3f507af6079009960d7f6bd9cbcdb6291
MD5 49ca87fac900ac9706563adca6cf4110
BLAKE2b-256 d976e913f2ac3555e9821273329f00704bbfb2f6f749b09eab4088cef110040e

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 16d14d7fef33c0c3156a39b7d1942cdde780016964faf951b2120b1de5265dd8
MD5 29565f1c0f6491361c6f899ae1c392b3
BLAKE2b-256 4077cbe118120585e255d511ef7ae1c1bc7742fd65b896a369b8dcf72e2f2d77

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bd9f558759d6218e49d30ac39de0df85b7f80d3212adde5d495ab24c1ebcee83
MD5 4e2a86324fb3739be6d64c6ab7543578
BLAKE2b-256 a7453eab7adbbcd7e609ef89d8357fb3fbd55dd626e04c9b2907dafd5565d944

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7a33f864d7c84a28f5d2d293e6c47ec9be419bab2a6af70fb3373c20d14ebafb
MD5 ea81da1bf1389daca3a4fb4346157b82
BLAKE2b-256 11936fb24a540725fee5c169651000dc663a0264cd29e5628edbe3a7461c637d

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 351f9f27ff5c8638e81653756a672f39d21353f86e45e0a55d136dff03f9617e
MD5 74f96d9bb5e68d326850fb6e0d4840aa
BLAKE2b-256 291495c0b1c13527e5d61c53cd7c31d9ef04bf2c3a7e44f49c5d6be64fee83a0

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5b7ac9653db356d327638dcc7efcdd8fb318f2bc1d54ffc9dda8eb3e7291ac41
MD5 b497939d062803d261aed911cb2e5c39
BLAKE2b-256 9a63b6c293d31e513b3c09fe0ec11787886cc690ce19bb120cd753564d8c1158

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e2370e5a8bdfba818e37a791dc46718a0b723c83f34c2572c472e971ec582d5e
MD5 79e603f1ff8e81702f972a11898ec48f
BLAKE2b-256 585ae1ef64a5730e05a7c18f827441cae9983fb635fca437693516e8ba64c503

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7c058cc5cb03f8c0a71ebbeaf2bc0f0ff35e419a5257e1e0958f83f7173ea663
MD5 4f0e1301d898c655672e46b06d62ba60
BLAKE2b-256 4bcb382a8d2b125b57d667213ec7b559aa9c404077b16c52cf8351b5cc072aeb

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6b243d10c84e23b330acb8f6ef193f8f4a3a1d957dd3a21060f1a6e8de85a6d5
MD5 540cf0f0bfbf7e8c7cd58d8101f5e3cd
BLAKE2b-256 b96724193160001e3602ab8b7245206bc1c3383f8020239e28ce1e4dfa6a7244

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 029e47dfcc76491ab2825671cdd28531e19ff0577d234fcc806d594df3c87cac
MD5 d9d2043b3bee180b91df4a8e7eeda1ad
BLAKE2b-256 398d82ac86d346a8515754eadebd8a599aa25bba80cd357bffc7afa3b500f631

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 695ad4fb5ef4572dcae119658be1ce8edee995454384b3ee2461adfdfa3a6f87
MD5 d51cfb7aa46f1f5bd82ef9aa8a0e78e0
BLAKE2b-256 f3b0c547a9ab303528768e3ed2a315750417665cc543aea1ab6a1e0db5e21b24

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e2a3ce272b1e4f41e60d6361fa5dce594a09590caad661161e3a6a12b7100dc3
MD5 6c4ed7900e1e9de3a1e3e557af3cd8d4
BLAKE2b-256 fac874e16621225183bd7304860d902801b1f29aee0485c6b2acdb0f122ae65b

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e953ab230ee8a5e30ef44de636b3af5b326a3ce6eadfb5142198f605fac5fdbe
MD5 b2bbea8b85e8f136f13740f9250c4a2a
BLAKE2b-256 ad70270d571be226dd694ce29154ff7f91ab093bfc4ad3732d523a1eac36d0c4

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp38-cp38-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3ebe5ee985f30c2f38bfc93f990b56e7124aba46e61611add53e141ab68d837c
MD5 42637d529f131f2d3e049aabb1c26eb6
BLAKE2b-256 be5aa112d2f7e12b4086a996bda8530e551b54ecd1e30c4a3e7f8b279da5dfb2

See more details on using hashes here.

File details

Details for the file pykatex-0.0.2-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pykatex-0.0.2-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 78c3e281982f593a15e011b93fc8f01b3c0ad34339cdc5bb2f1874314a6dda98
MD5 ccea612c79f120e638429070f17cdc9c
BLAKE2b-256 b142bb48cbc19133f567b5a0b99053a4387cb1faf8e97162878bf5b179200f48

See more details on using hashes here.

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