Skip to main content

texform

Python bindings for TeXForm, a LaTeX formula parser, editor, and normalizer built on a structured command knowledge base.

pip install texform

Quick start

import texform

# Normalize a formula into a canonical form chosen by profile.
engine = texform.TransformEngine(profile="corpus")
normalized = engine.normalize(r"a \over b")
assert normalized == r"\frac { a } { b }"

# Parse through the engine, transform the live document in place, then serialize.
parsed = engine.parse(r"a \over b")
if parsed["document"] is not None:
    document = parsed["document"]
    engine.transform(document)
    assert document.to_latex() == r"\frac { a } { b }"

Profiles select the normalization target: "authoring", "faithful", "corpus", and "equiv".

Shared knowledge and document copies

Knowledge configuration belongs to KnowledgeBase; pass the same instance to each parser, engine, or document that should interoperate. Omitting it uses a process-wide default. Separately constructed knowledge bases have different identities even with identical package lists.

kb = texform.KnowledgeBase(["base", "ams"])
parser = texform.Parser(kb)
engine = texform.TransformEngine("equiv", kb)
document = parser.parse("x")["document"]
variant = document.copy()  # copy.copy and copy.deepcopy also copy the tree.
assert variant.knowledge_base() == kb
assert variant.root() != document.root()
assert document.root() == document.root()
assert document.root().document() is document
engine.transform(variant)
records = kb.commands("math")  # Sorted copied records; knowledge is immutable.

items, remove_commands, remove_environments, and remove_delimiter_controls are keyword options on KnowledgeBase. Knowledge queries have moved from Parser and TransformEngine to KnowledgeBase. commands(mode), environments(mode), characters(mode), delimiters(), and packages() enumerate its contents. KnowledgeBase([]) constructs empty knowledge. Document(kb, mode="text") creates a text root, and Document.from_syntax(snapshot, kb) rebuilds a snapshot with shared knowledge. count_targets(source, knowledge_base=kb) uses the same knowledge for analysis.

Constructing formulas

Constructors return detached nodes. Attach the finished subtree with append_child. Arguments accept Node, source str, star bool, None, or immutable Paired(value, open, close). Strings contain source inside the argument boundaries. Supply either every slot or only required slots; None omits optional slots and disables a star.

doc = texform.Document()
frac = doc.create_command("frac", ["a+b", "c"])
scripted = doc.create_scripted(frac, sub="i", sup="2")
paren = doc.create_delimited_group("(", ")", [scripted])
doc.append_child(doc.root(), paren)
print(doc.to_latex())

Groups accept children as nodes or source strings; a string is parsed and all its nodes are spliced into the list, so one string may yield several children. Environment bodies accept a group, a list of nodes or source strings, source, or None for an empty body. parse_fragment(source, mode="math") produces a detached group. Constructors accept mode= to specify the detached subtree's context; create_group(mode, children) uses that mode for both group content and context. Delimiters use strings: "(", "\\langle", or "." for no delimiter.

Invalid construction, edits, and strict Document.from_syntax imports raise ConformanceError, an EditError subclass with path and stable rule attributes. Invalid source arguments raise ParseError with diagnostics. A rejected operation leaves the document unchanged. to_latex and serialize check only structure, not conformance to a knowledge base. Rename environments with set_env_name; set_command_name applies to command nodes.

Editing and locating nodes

node.path() describes its current location, such as root.child.0.base; detached nodes return None. Save a path before copying a document, then use copy.node_at(path) to locate the corresponding node. Missing paths raise EditError. node.slot() returns a dictionary with a snake_case kind and optional index, while node.is_known() reports knowledge status for command-like nodes and environments.

doc = texform.Parser().parse("x_i")["document"]
base = doc.node_at("root.child.0.base")
scripted = doc.set_superscript(base, "2")
assert scripted == doc.node_at("root.child.0")
doc.set_subscript(base, None)
doc.set_superscript(base, None)  # Both scripts absent: collapse to the base.

set_subscript and set_superscript accept the same argument inputs as constructors and return the resulting wrapper or base. set_arg fills or clears optional slots, toggles stars, and preserves existing paired boundaries when given an ordinary value. set_arg_delimiters changes paired boundaries; set_delimiters changes a delimited group; set_prime_count requires a positive count.

clone_node(node) makes a detached deep copy within the document. import_node(node) takes a handle from any document, copying it and checking the subtree against the destination knowledge base. Error subtrees cannot be imported from another document. The source remains unchanged, and a same-document import behaves as a clone.

Python-specific notes

  • normalize returns str. transform updates the document and returns None.
  • normalize_with_report returns {"normalized", "report"}. transform_with_report returns the report dict. Both use the same config object and keyword overlays as the plain methods. Output text and errors follow the ordinary transform contract. Report fields, phase divisions, and counters are diagnostic and are not a stable compatibility promise.
  • Parser.parse returns a dict with a document value (or None) plus a diagnostics list — the same three-state contract as the Rust API.
  • All names follow Python conventions: methods and dict keys are snake_case (to_latex, validate_argspec returns arg_count).
  • config is a complete configuration class (ParseConfig / TransformConfig). Other keyword arguments are overlays (normalize(src, rewrite={"enabled": False})). Saved dicts expand with **saved_overrides. Nested override values must be dicts, not config class instances.
  • parser.default_parse_config(), engine.default_parse_config(), and engine.default_transform_config() return the complete defaults actually in force. Nested assignment sticks: cfg = engine.default_transform_config(); cfg.rewrite.enabled = False.
  • Serialize options are keywords too: document.to_latex(script_order="sup_first").
  • Unknown keys, wrong types, and arrays where an object is expected raise ConfigError with a field path. None / omitted means not set.
  • Parse and edit errors raise structured exceptions (texform.ParseError and friends); no Rust panic ever crosses the boundary.
  • The package ships py.typed and .pyi stubs, so type checkers and IDE completion work out of the box.
  • Wheels are abi3 and require Python 3.10 or newer.

Learn more

The Python API mirrors the Rust facade one-to-one. For the full picture — the editable document tree, transform profiles, and the architecture — see the repository README.

Columnar export

document.to_columnar() flattens the tree into columns for bulk structural analysis with Arrow or DataFrame tools. It carries information comparable to document.to_syntax(): to_syntax() returns a nested snapshot, while to_columnar() returns columnar data.

document.to_columnar() exports the attached tree once as {"nodes": ..., "args": ...}, with one Python list per column. Nodes use preorder; arguments include every slot, including absent optional slots and scalar values. Each table can be passed directly to pyarrow.table() without a TeXForm dependency on PyArrow:

import pyarrow as pa

tables = texform.Parser().parse(r"\sqrt{x}")["document"].to_columnar()
nodes = pa.table(tables["nodes"])
arguments = pa.table(tables["args"])

parent, slot_index, and content use -1 when no index applies; other missing fields are None. Node mode describes its context, while value_kind distinguishes math, text, and operator-name arguments. open and close retain actual paired-argument boundaries. Row indices belong to the snapshot, detached subtrees are excluded, and documents containing errors remain exportable.

License

Apache-2.0.

Metadata

Release files for texform 0.8.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 texform 0.8.0
File Size Uploaded Provenance Actions
texform-0.8.0.tar.gz 522.8 kB With provenance Details for texform-0.8.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for texform 0.8.0
File Actions
texform-0.8.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for texform-0.8.0-cp310-abi3-win_amd64.whl
texform-0.8.0-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details for texform-0.8.0-cp310-abi3-musllinux_1_2_x86_64.whl
texform-0.8.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details for texform-0.8.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
texform-0.8.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details for texform-0.8.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
texform-0.8.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for texform-0.8.0-cp310-abi3-macosx_11_0_arm64.whl
texform-0.8.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details for texform-0.8.0-cp310-abi3-macosx_10_12_x86_64.whl

Total release size: 10.2 MB

Release files / texform-0.8.0.tar.gz

Download URL texform-0.8.0.tar.gz
Size 522.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b1a8084f85e707acc35676ff37fc1ba9d41fa8f2be009ecbe13dba40270722ab
BLAKE2b-256 checksum
How to use checksums
4fa881d3512749f4c4b3ef028e6d45ab13ad552eb36d2d194e833b0f6f31deea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release files / texform-0.8.0-cp310-abi3-win_amd64.whl

Download URL texform-0.8.0-cp310-abi3-win_amd64.whl
Size 1.4 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
56051837e6f9ef8f0519409792b9b0361855ff733e2f02e57b7559595c31f119
BLAKE2b-256 checksum
How to use checksums
bcb9ebfaae12fadc180664154702c8be3732b2b07b379dbfae98d8ee24557d2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release files / texform-0.8.0-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL texform-0.8.0-cp310-abi3-musllinux_1_2_x86_64.whl
Size 1.9 MB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
de985a561ca013ff591665bdc32132bcdf864d58b84dabb92e6d3bbba648d481
BLAKE2b-256 checksum
How to use checksums
0e2487f80b6ddb40913553a424e7aa3ccf39ef201b0194f4b9029295b117267e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release files / texform-0.8.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL texform-0.8.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.7 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
bcd4c91d01791b247cf2c2b93fb0c39b8934dabbf79b7afb45a138d1e61129c8
BLAKE2b-256 checksum
How to use checksums
40f6bd0195bce8962734741b1713fd9471caad9ef468b09d704e0f85ba2549d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release files / texform-0.8.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL texform-0.8.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 1.7 MB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
b0370da1524bcd152029640c4e045edd297b3a99fe82789f53ffd04249bb6a6d
BLAKE2b-256 checksum
How to use checksums
52ed637e361c2bd92f185ce1b2e051c3692f8e0c4f14e055bd66526599ccc794
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release files / texform-0.8.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL texform-0.8.0-cp310-abi3-macosx_11_0_arm64.whl
Size 1.5 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9f56c664672a5cdf9bb63147caac6d1040d6a5826ea79088fa7a0c2eb0e6731c
BLAKE2b-256 checksum
How to use checksums
23d512e5846e6a49acaae1955513b2f37e6a5d84814860a03dad0cafe81c65a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release files / texform-0.8.0-cp310-abi3-macosx_10_12_x86_64.whl

Download URL texform-0.8.0-cp310-abi3-macosx_10_12_x86_64.whl
Size 1.5 MB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
201499291ada55e99a344a440b99bd2cb946bd53625d9708fd34602019ae3c2c
BLAKE2b-256 checksum
How to use checksums
369e51b5074622d13ecb53e1ec3654ca11de0d093ad8e4081eabdc4a515cd315
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Security for texform 0.8.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.

Full provenance

All 7 files in this release have been uploaded with provenance metadata.

Select from 7 files

Attestation for texform-0.8.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

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