Skip to main content

pyqt5-math-widget

A PyQt5 library for rendering and interactively editing mathematical formulas. No LaTeX engine required — everything is rendered natively via QPainter.


Installation

pip install pyqt5-math-widget

Widgets

Class Purpose
MathView Read-only formula renderer
MathEditor Interactive formula editor with toolbar
MathDialog Modal dialog wrapping MathEditor

MathView — read-only renderer

Basic usage

from pyqt5_math_widget import MathView, FracNode, TextNode, SqrtNode

view = MathView()
view.set_node(FracNode(TextNode("1"), SqrtNode(TextNode("x"))))
layout.addWidget(view)

From a FormulaModel

from pyqt5_math_widget import MathView, FormulaModel

model = FormulaModel()
model.insert_frac()
model.insert_greek("pi")

view = MathView.from_model(model)

API

view.set_node(node)           # Set formula from a FormulaNode
view.set_model(model)         # Attach a FormulaModel
view.set_text("sin(x)")       # Set from plain text
view.set_zoom(1.5)            # Scale factor (default 1.0)
view.zoom()                   # -> float
view.set_background(color)    # QColor fill
view.set_transparent(True)    # Transparent background
view.set_padding(16, 12)      # Horizontal, vertical padding in px
view.set_foreground(color)    # Global text colour
view.to_pixmap(scale=2.0)     # -> QPixmap (for export/saving)
view.to_latex()               # -> str
view.to_sympy()               # -> str
view.to_text()                # -> str
view.model()                  # -> FormulaModel

MathEditor — interactive editor

Basic usage

from pyqt5_math_widget import MathEditor

editor = MathEditor()
editor.formula_changed.connect(lambda s: print("sympy:", s))
editor.latex_changed.connect(lambda s: print("latex:", s))
layout.addWidget(editor)

Constructor parameters

MathEditor(
    parent=None,
    show_toolbar=True,          # Show button toolbar below canvas
    toolbar_groups=None,        # List of group names to show (see below)
    zoom=1.0,                   # Initial zoom level
    readonly=False,             # Disable editing
)

Toolbar groups

Pass a subset of these strings to toolbar_groups:

Group Contents
"struct" fraction, sqrt, power, subscript, integral, sum, product, limit, matrix, ...
"operators" +, -, ·, ×, =, ≠, ≤, ≥, ±, ≈, ∂, ∇, ∈, ∀, →, ...
"greek" α β γ δ ε θ λ μ ν π ρ σ τ φ ψ ω Σ Π Γ Δ Θ Ω ∞ ...
"functions" sin cos tan arcsin log ln exp max min det tr ...
"edit" Delete, Clear, ← →, Undo, Redo
editor.set_toolbar_groups(["struct", "greek", "edit"])

Programmatic insertion API

editor.insert_frac()                   # Fraction a/b
editor.insert_sqrt(nth=False)          # Square root
editor.insert_sqrt(nth=True)           # Nth root
editor.insert_power(wrap_current=True) # Superscript
editor.insert_subscript()              # Subscript
editor.insert_power_sub()              # Combined x^n_m
editor.insert_parens("(", ")")         # Parentheses (any bracket pair)
editor.insert_parens("[", "]")         # Square brackets
editor.insert_abs()                    # |x|
editor.insert_integral(definite=True)  # Definite integral
editor.insert_integral(definite=False) # Indefinite integral
editor.insert_sum(with_limits=True)    # Sum Σ
editor.insert_product(with_limits=True)# Product Π
editor.insert_limit()                  # lim
editor.insert_matrix(2, 2, "(")        # 2x2 matrix with round brackets
editor.insert_matrix(3, 3, "[")        # 3x3 matrix with square brackets
editor.insert_overline()               # Overline
editor.insert_underline()              # Underline
editor.insert_func("sin")             # sin(...) function
editor.insert_greek("alpha")          # α
editor.insert_operator("->")          # →
editor.insert_text("x", italic=True)  # Plain character
editor.insert_node(my_node)           # Raw FormulaNode

Editing control

editor.delete_at_cursor()    # Delete at cursor (Backspace)
editor.clear()               # Clear entire formula
editor.undo()                # Ctrl+Z
editor.redo()                # Ctrl+Y
editor.can_undo()            # -> bool
editor.can_redo()            # -> bool
editor.cursor_left()         # Move cursor left
editor.cursor_right()        # Move cursor right

Output

editor.to_sympy()    # "integrate(sin(x), x)"
editor.to_latex()    # r"\int \sin(x)\,dx"
editor.to_text()     # "integrate(sin(x), x)"
editor.to_pixmap(scale=2.0)   # -> QPixmap

Display control

editor.set_zoom(1.5)
editor.set_readonly(True)
editor.set_toolbar_visible(False)
editor.model()                # -> FormulaModel for direct access
editor.set_model(model)       # Replace model

MathDialog — modal dialog

from pyqt5_math_widget import MathDialog
from PyQt5.QtWidgets import QDialog

dlg = MathDialog(
    parent=self,
    title="Insert Formula",
    initial_text="sin(x)",
    toolbar_groups=["struct", "greek", "edit"],
    show_output=True,
    zoom=1.1,
)
if dlg.exec_() == QDialog.Accepted:
    print(dlg.result_sympy())
    print(dlg.result_latex())
    print(dlg.result_text())

Connect the signal without blocking:

dlg = MathDialog(parent=self)
dlg.accepted_formula.connect(lambda s: print("inserted:", s))
dlg.show()

FormulaModel — programmatic tree manipulation

from pyqt5_math_widget import FormulaModel

model = FormulaModel()
model.insert_frac()
model.insert_text("x")
model.move_right()
model.insert_text("2")

print(model.to_sympy())  # (x) / (2)
print(model.to_latex())  # \frac{x}{2}

model.undo()
model.redo()
model.clear()

Formula node tree

Build a formula tree and display it statically:

from pyqt5_math_widget import (
    MathView, SeqNode, TextNode, FracNode, SqrtNode,
    PowerNode, IntegralNode, SumProdNode, MatrixNode,
    ParenNode, AbsNode,
)

root = SeqNode([
    TextNode("E"),
    TextNode("="),
    FracNode(
        TextNode("m"),
        SqrtNode(SeqNode([TextNode("1"), TextNode("-"), PowerNode(TextNode("v"), TextNode("2"))])),
    ),
])

view = MathView()
view.set_node(root)

Available node types

Class Description
TextNode(text, size, italic, bold, color) Text / symbol
PlaceholderNode(size) Editable placeholder
SeqNode(children) Horizontal sequence
FracNode(num, den) Fraction
SqrtNode(body, index=None) Square / nth root
PowerNode(base, exp) Superscript
SubNode(base, sub) Subscript
PowerSubNode(base, exp, sub) Combined sup+sub
ParenNode(body, left, right) Scalable brackets
AbsNode(body) Absolute value
IntegralNode(body, var, lower, upper) Integral
SumProdNode(kind, body, var, lower, upper) Sum / Product
LimitNode(body, var, approach, direction) Limit
MatrixNode(rows, left, right) Matrix
OverlineNode(body) Overline
UnderlineNode(body) Underline

Each node implements:

node.to_sympy()   # SymPy expression string
node.to_latex()   # LaTeX string
node.to_text()    # Plain text
node.measure(painter)  # -> RenderMetrics
node.draw(painter, x, y, hits, selected, cursor_id)
node.collect_ids()     # -> List[int]
node.clone()           # deep copy

Constants and maps

from pyqt5_math_widget import GREEK_MAP, OPERATOR_MAP

GREEK_MAP["alpha"]   # -> "α"
OPERATOR_MAP["->"]   # -> "→"

Integration

from pyqt5_math_widget import MathDialog
from PyQt5.QtWidgets import QPushButton

btn = QPushButton("\u03A3")
btn.clicked.connect(lambda: self._open_formula_editor())

def _open_formula_editor(self):
    dlg = MathDialog(initial_text=self._input.text(), parent=self.window())
    if dlg.exec_():
        self._input.setText(dlg.result_sympy())

License

MIT

Metadata

Release files for pyqt5-math-widget 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyqt5-math-widget 1.2.0
File Size Uploaded Provenance Actions
pyqt5_math_widget-1.2.0.tar.gz 38.2 kB Without provenance Details for pyqt5_math_widget-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyqt5-math-widget 1.2.0
File Interpreter ABI Platform Actions
pyqt5_math_widget-1.2.0-py3-none-any.whl Python 3 none any Details for pyqt5_math_widget-1.2.0-py3-none-any.whl

Total release size: 76.7 kB

Release files / pyqt5_math_widget-1.2.0.tar.gz

Download URL pyqt5_math_widget-1.2.0.tar.gz
Size 38.2 kB
Tags Source
SHA-256 checksum
How to use checksums
891f941c868ef66ada6e83d6f8be9cfa80c579d1b29289cdd6fb22fe6f270196
BLAKE2b-256 checksum
How to use checksums
69884127bf3519512c852b18dc8810ead2aa8609fcc63f5371dacd8e277938aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.10

Release files / pyqt5_math_widget-1.2.0-py3-none-any.whl

Download URL pyqt5_math_widget-1.2.0-py3-none-any.whl
Size 38.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8705c101fd548d3db3f1410aa2306db2a2fe216ee65c9c56f9daffc74a06cc45
BLAKE2b-256 checksum
How to use checksums
7382c6be68548cacf6d92cd3ad124415ac4108d12acf60c2c5e9ea7f02b8b63a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.10

Security for pyqt5-math-widget 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page