Skip to main content

A Reflex custom component library for rendering beautiful mathematical expressions using KaTeX

Project description

reflex-katex

A Reflex custom component library for rendering beautiful mathematical expressions using KaTeX.

Installation

pip install reflex-katex

Quick Start

import reflex as rx
from reflex_katex import inline_math, block_math

def index():
    return rx.vstack(
        rx.text(
            "Here's an inline equation: ",
            inline_math("E = mc^2"),
            " in the middle of text."
        ),
        rx.text("And here's a display equation:"),
        block_math("\\int_0^\\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}"),
        spacing="4"
    )

app = rx.App()
app.add_page(index)

Features

  • 🔢 Inline Math: Render LaTeX expressions within text flow
  • 📐 Block Math: Display centered mathematical equations
  • 🎨 Beautiful Rendering: Powered by KaTeX for fast, high-quality output
  • 🔧 Simple API: Easy-to-use functions that integrate seamlessly with Reflex
  • Accessible: Proper MathML output for screen readers

Usage

Inline Math

Use inline_math() to render mathematical expressions within text:

rx.text(
    "The quadratic formula is ",
    inline_math("x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}"),
    " and it's very useful."
)

Block Math

Use block_math() for display equations that should be centered and prominent:

block_math("\\sum_{n=1}^{\\infty} \\frac{1}{n^2} = \\frac{\\pi^2}{6}")

LaTeX Syntax

reflex-katex supports standard LaTeX mathematical notation:

# Fractions
inline_math("\\frac{1}{2}")

# Superscripts and subscripts
inline_math("x^2 + y_1")

# Greek letters
inline_math("\\alpha + \\beta = \\gamma")

# Integrals
block_math("\\int_a^b f(x) dx")

# Matrices
block_math("\\begin{pmatrix} a & b \\\\ c & d \\end{pmatrix}")

# Complex expressions
block_math("f(x) = \\begin{cases} x^2 & \\text{if } x \\geq 0 \\\\ -x^2 & \\text{if } x < 0 \\end{cases}")

Passing Additional Props

Both functions accept additional component properties:

inline_math("x^2", style={"color": "blue"})
block_math("\\sum x_i", class_name="my-equation")

Examples

Scientific Notation

rx.vstack(
    rx.heading("Scientific Formulas"),
    rx.text("Einstein's mass-energy equivalence: ", inline_math("E = mc^2")),
    rx.text("Schrödinger equation:"),
    block_math("i\\hbar\\frac{\\partial}{\\partial t}\\Psi = \\hat{H}\\Psi"),
    rx.text("Newton's second law: ", inline_math("F = ma")),
)

Mathematical Concepts

rx.vstack(
    rx.heading("Calculus"),
    rx.text("Derivative definition:"),
    block_math("f'(x) = \\lim_{h \\to 0} \\frac{f(x+h) - f(x)}{h}"),
    rx.text("Fundamental theorem of calculus:"),
    block_math("\\int_a^b f'(x) dx = f(b) - f(a)"),
)

Statistics

rx.vstack(
    rx.heading("Statistics"),
    rx.text("Normal distribution probability density:"),
    block_math("f(x) = \\frac{1}{\\sigma\\sqrt{2\\pi}} e^{-\\frac{1}{2}\\left(\\frac{x-\\mu}{\\sigma}\\right)^2}"),
    rx.text("Sample variance: ", inline_math("s^2 = \\frac{1}{n-1}\\sum_{i=1}^n (x_i - \\bar{x})^2")),
)

API Reference

inline_math(expression: str, **props) -> InlineMath

Renders a LaTeX expression inline within text.

Parameters:

  • expression (str): LaTeX mathematical expression
  • **props: Additional component properties

Returns: InlineMath component

block_math(expression: str, **props) -> BlockMath

Renders a LaTeX expression as a centered display equation.

Parameters:

  • expression (str): LaTeX mathematical expression
  • **props: Additional component properties

Returns: BlockMath component

Supported LaTeX

reflex-katex supports a wide range of LaTeX mathematical notation through KaTeX. For a complete list of supported functions and symbols, see the KaTeX documentation.

Common categories include:

  • Basic math: +, -, *, /, =, <, >
  • Fractions: \frac{a}{b}
  • Exponents/subscripts: x^2, x_1
  • Roots: \sqrt{x}, \sqrt[n]{x}
  • Greek letters: \alpha, \beta, \gamma, etc.
  • Calculus: \int, \sum, \prod, \lim
  • Logic: \land, \lor, \neg, \forall, \exists
  • Sets: \in, \subset, \cup, \cap
  • And much more!

Requirements

  • Python 3.8+
  • Reflex >= 0.8.0
  • A modern web browser with JavaScript enabled

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • KaTeX - Fast math typesetting for the web
  • react-katex - React components for KaTeX
  • Reflex - The web framework for Python

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

reflex_katex-0.0.4.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

reflex_katex-0.0.4-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file reflex_katex-0.0.4.tar.gz.

File metadata

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

File hashes

Hashes for reflex_katex-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8efcf250442d3fbf0250641f45abf0f0670b0452802043990f02652d2f70f1a0
MD5 1fddd05c31562d856b39c0344179e22e
BLAKE2b-256 bd395f8c758be34ea705e8deb78d9852d7f54d97f04b2ad4be0ea6dda75de34f

See more details on using hashes here.

Provenance

The following attestation bundles were made for reflex_katex-0.0.4.tar.gz:

Publisher: release.yaml on tn3wman/reflex-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 reflex_katex-0.0.4-py3-none-any.whl.

File metadata

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

File hashes

Hashes for reflex_katex-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 39ddabd9b23a4c3f5ece94f1573721c357d31dfcb860302a37c01bdbd9384075
MD5 1fb2760ff0594cb5de9d9d5506400669
BLAKE2b-256 59c8ae3757b44b7c109a0cba2c11b1d652ee509927231c6b354acfb839454c59

See more details on using hashes here.

Provenance

The following attestation bundles were made for reflex_katex-0.0.4-py3-none-any.whl:

Publisher: release.yaml on tn3wman/reflex-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