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.1

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.1
File Size Uploaded Provenance Actions
texform-0.9.1.tar.gz 543.2 kB With provenance Details for texform-0.9.1.tar.gz

Built distributions (wheels)

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

Total release size: 10.3 MB

Release files / texform-0.9.1.tar.gz

Download URL texform-0.9.1.tar.gz
Size 543.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9971187adbf2ba501478df9f2c6a09966889d2f7851dce9ec6eb4d8b8e7ab233
BLAKE2b-256 checksum
How to use checksums
f51649728a1690368ca43a704441268c424d20e7ee35c46e0617961350e5d134
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 9, 2026.

Transparency log

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

Download URL texform-0.9.1-cp310-abi3-win_amd64.whl
Size 1.4 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
20c46baa0623f1cd00e44c3d169213a4866d7020c0e38a2b3046c4fac479ce48
BLAKE2b-256 checksum
How to use checksums
c259b10e6042bfedb1e2d267c0d1279b3bc3c3cd520f57c09d67212b24c7ac62
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 9, 2026.

Transparency log

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

Download URL texform-0.9.1-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
4d8dd16a67fbcf05bb102d3397aee064b0f5082519c77aebeeff8b093d307ad5
BLAKE2b-256 checksum
How to use checksums
e3d1f3abad7b195737845fcd3122eda43867ee76b3d4bd8b2886ca71036d42ee
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 9, 2026.

Transparency log

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

Download URL texform-0.9.1-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
03cb2654cbed55876dcda550ffae4fa7123e0a99f0b3633c6e205ca74b611fb5
BLAKE2b-256 checksum
How to use checksums
cf9a8dd1c25e4271ea050d16ce2e27aeded486e5254968b62250b72a0dfaff40
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 9, 2026.

Transparency log

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

Download URL texform-0.9.1-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
ef65c148232c92ee9e2fdf7fa20886be6164392eb38622759354a10d4dc9382c
BLAKE2b-256 checksum
How to use checksums
7d9f8556bd380246587c71f24af5b502f182a95ad10535545ddbbf0f7cb84912
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 9, 2026.

Transparency log

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

Download URL texform-0.9.1-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
6d7d1f4e8f72cb8a54ecf3d571c32dbf1bee9814e4751f5de7d1a055a8ca6fce
BLAKE2b-256 checksum
How to use checksums
07dc59364b0843b76bb9936b803251efa8d3974e910cf7e61dd0b26ca06bfbf7
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 9, 2026.

Transparency log

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

Download URL texform-0.9.1-cp310-abi3-macosx_10_12_x86_64.whl
Size 1.6 MB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
ea5b5f18caf95029d918cfaa073ae2844a8413d646e10d6eb49785e626ab3c10
BLAKE2b-256 checksum
How to use checksums
9d89788d0f3cacc817d00c972b651dbc675e74f896f9ab83bc7e2497910730f4
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 9, 2026.

Transparency log

Security for texform 0.9.1

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.1.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 9, 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