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.9.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.9.0
File Size Uploaded Provenance Actions
texform-0.9.0.tar.gz 538.1 kB With provenance Details for texform-0.9.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for texform 0.9.0
File Actions
texform-0.9.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for texform-0.9.0-cp310-abi3-win_amd64.whl
texform-0.9.0-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details for texform-0.9.0-cp310-abi3-musllinux_1_2_x86_64.whl
texform-0.9.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.9.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
texform-0.9.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details for texform-0.9.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
texform-0.9.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for texform-0.9.0-cp310-abi3-macosx_11_0_arm64.whl
texform-0.9.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details for texform-0.9.0-cp310-abi3-macosx_10_12_x86_64.whl

Total release size: 10.1 MB

Release files / texform-0.9.0.tar.gz

Download URL texform-0.9.0.tar.gz
Size 538.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a90f49d59a4aff50caf987499a1b66950b2960c0f86fdd8c9349cbce5c34a92b
BLAKE2b-256 checksum
How to use checksums
3c0bc99a8f105b0efc7c7ab8065f81364e8504eddec48d882c28f7f46d52e36e
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 Oct 4, 2026.

Transparency log

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

Download URL texform-0.9.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
6262b48ebc7acc4f1a83e27aee6ba734e39161a1bdd58b35959d291d556b4c80
BLAKE2b-256 checksum
How to use checksums
69d30a7640049c198c32f419154a9df23e8a224344b9f1f7c3f59e8c33987db7
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 Oct 4, 2026.

Transparency log

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

Download URL texform-0.9.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
63c30fc583613b9553f8a21d5be1911189f948ffedadeb94f87085345a0bcae2
BLAKE2b-256 checksum
How to use checksums
39f9e64564c8e9b2ef769e8072722f56ca79cb6cde404c02a840a37f5940e05b
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 Oct 4, 2026.

Transparency log

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

Download URL texform-0.9.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.6 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
c9c61316d6ec5e4f9a3ae548ac351bc49b8cf523ed15d0db2eafa7f5598024d2
BLAKE2b-256 checksum
How to use checksums
3bae6eb471442e01401a6a99eb8df33857b36b46c6065488e8a6efaa0a4b01ca
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 Oct 4, 2026.

Transparency log

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

Download URL texform-0.9.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
ad56ba1c853b7b59210857b677a32671f0270ed140dc47c0cf853c595e8ba313
BLAKE2b-256 checksum
How to use checksums
5efe29c8f8ab63ff21029a213cef636a7d5578748bbba0b2d31293e1af9b1226
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 Oct 4, 2026.

Transparency log

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

Download URL texform-0.9.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
659eb06d092fed9a502b1957752c892c4c9d79a612cfdb64a1bf4a6dbc7bc0af
BLAKE2b-256 checksum
How to use checksums
7cf94b9968ecdcc5c95398bf2a79c5e033f9c528b3562980e8e69bc841b07a05
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 Oct 4, 2026.

Transparency log

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

Download URL texform-0.9.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
b0eaa495fd93544df9c7f441eed05af38880e9a3bf4d5b21401be446e3192c77
BLAKE2b-256 checksum
How to use checksums
a7fe36c6fa286801c4267a370622f7e53da7d2fcd7f636fdee274cccbeea8157
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 Oct 4, 2026.

Transparency log

Security for texform 0.9.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.9.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 Oct 4, 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