Skip to main content

A Python package for composable LaTeX mathematical expressions

Project description

latexsymb

A Python package for composable LaTeX mathematical expressions.

Overview

latexsymb transforms the way mathematical LaTeX expressions are written in Python by providing a programmatic, composable approach. Instead of writing error-prone LaTeX syntax with backslashes and braces, you can build complex mathematical expressions using intuitive Python functions and operators.

Installation

pip install -e .

Quick Start

from latexsymb import lsymb, pths, Sum, sin, cos, alpha, beta

# Create basic symbols
x = lsymb("x")
y = lsymb("y")

# Use operators to build expressions
fraction = x / y  # → \frac{x}{y}
power = x ** "2"  # → x^{2}

# Use mathematical functions
sine_expr = sin(x)  # → \sin\left(x\right)

# Build complex expressions
summation = Sum(sin(alpha * x), "i=1", "n")
# → \sum_{i=1}^{n} \sin\left(\alpha x\right)

print(summation)

Key Features

1. Operator Overloading

Mathematical operators work intuitively:

from latexsymb import lsymb

x = lsymb("x")
y = lsymb("y")

# Basic operations
x + y     # → x+y
x - y     # → x-y  
x * y     # → x y (space-separated)
x / y     # → \frac{x}{y}
x ** "2"  # → x^{2}

2. Delimiter Functions

Auto-sizing delimiters for clean expressions:

from latexsymb import pths, sqbr, br, ang

pths("x + y")     # → \left(x + y\right)
sqbr("x + y")     # → \left[x + y\right]
br("x + y")       # → \left\{x + y\right\}
ang("x, y")       # → \left\langle x, y\right\rangle

3. Mathematical Functions

Standard mathematical notation:

from latexsymb import Sum, Prod, Int, lim, dd, pp

Sum("x_i", "i=1", "n")      # → \sum_{i=1}^{n} x_i
Prod("a_i", "i=1", "n")     # → \prod_{i=1}^{n} a_i
Int("x^2", "dx", "0", "1")  # → \int_{0}^{1} x^2 \, dx
lim("f(x)", "x", "0")       # → \lim_{x\rightarrow0} f(x)
dd("f(x)", "x")             # → \frac{d}{dx} f(x)
pp("f(x,y)", "x")           # → \frac{\partial}{\partial x} f(x,y)

4. Common Mathematical Functions

Callable functions that produce proper LaTeX:

from latexsymb import sin, cos, log, ln, exp, sqrt

sin("x")         # → \sin\left(x\right)
cos("\\theta")   # → \cos\left(\theta\right)
log("x", "2")    # → \log_{2}\left(x\right)
ln("x")          # → \ln\left(x\right)
exp("x")         # → \exp\left(x\right)
sqrt("x")        # → \sqrt{x}

5. Pre-defined Symbols

Common symbols and Greek letters:

from latexsymb import alpha, beta, gamma, pi, infty, R, C

# Greek letters
alpha   # → \alpha
beta    # → \beta
pi      # → \pi

# Mathematical constants
infty   # → \infty

# Number sets  
R       # → \mathbb{R}
C       # → \mathbb{C}

6. LaTeX Environments

Create multi-line equations and inline math:

from latexsymb import lenv, il

# Inline math
il("x + y")  # → $x + y$

# Multi-line environments
equations = ["x &= y", "y &= z"]
lenv("align*", equations)
# → \begin{align*}x &= yy &= z\end{align*}

Complex Examples

Quadratic Formula

from latexsymb import lsymb, sqrt

a, b, c = lsymb("a"), lsymb("b"), lsymb("c")

# x = (-b ± √(b² - 4ac)) / (2a)
discriminant = sqrt(b**"2" - lsymb("4")*a*c)
formula = (-b + lsymb("\\pm") + discriminant) / (lsymb("2")*a)

print(formula)
# → \frac{-b+\pm+\sqrt{b^{2}-4 a c}}{2 a}

Taylor Series

from latexsymb import Sum, under, lsymb, dd

x, a, n = lsymb("x"), lsymb("a"), lsymb("n")
f = lsymb("f")

# f(x) = Σ(n=0 to ∞) [f⁽ⁿ⁾(a)/n!] (x-a)ⁿ
term = (dd(f, under(x, n)) / lsymb("n!")) * (x - a)**n
taylor = Sum(term, "n=0", "\\infty")

print(taylor)

Running Tests

pytest tests/

License

MIT License

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

latexsymb-1.0.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

latexsymb-1.0.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file latexsymb-1.0.0.tar.gz.

File metadata

  • Download URL: latexsymb-1.0.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for latexsymb-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9f0ea412f0fcd6c5b6d029e0749a7f67fa5e7ac728d963fefc02deeda3946db7
MD5 4418b94ad673d538f50cfe2f4263bd9d
BLAKE2b-256 605fab8fa68dac56340c42c809eef736b77609d31826cf258ba215217a40546c

See more details on using hashes here.

File details

Details for the file latexsymb-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: latexsymb-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for latexsymb-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0026c9b161cc345da7ac97643527f3a59fbb012d7162487e77760b9d759ac69c
MD5 aabb48e9f37a1593a4055163b238eefc
BLAKE2b-256 a4503c872cc5366c9e80fee642b94bec913ac406670f9c7604044626782b531e

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