Pythonserialize

serialize

Render a SyntaxNode to LaTeX text, and the output style options.

serializefunction#

serialize(node: SyntaxNode, **options) -> str

Render a SyntaxNode dict to LaTeX text.

This is the free-function counterpart to Document.to_latex(); both take the same keyword options. Error nodes round-trip their captured snippet, ordinary Prime nodes emit \prime symbols, pure prime superscripts serialize compactly as f' / f'', and the serializer guarantees text idempotency. For the conceptual model, see the Serialization guide. It checks only the structure, not conformance to any knowledge base.

ParameterTypeDescription
nodeSyntaxNode

A SyntaxNode dict, typically from Document.to_syntax() or a stored snapshot.

**optionsUnpack[SerializeOptions]

Serializer style overlays. Omitted keys and explicit None keep the default (spaced) style.

Returns
str —

The serialized LaTeX string.

Raises
  • ConfigError —

    If an option key is unknown or a value has the wrong type.

Python
result = texform.Parser().parse(r"x^2")
document = result["document"]
assert document is not None
syntax = document.to_syntax()
texform.serialize(syntax)                                            # 'x ^ { 2 }'
texform.serialize(syntax, style="compact")                          # 'x^2'

SerializeOptionsdict type#

Options for Document.to_latex, Document.to_tokenized_latex, and serialize.

Passed as keywords. Omitted or None style selects the default spaced style. Unknown keys and invalid values raise ConfigError. For a task-oriented walkthrough, see the Serialization guide.

FieldTypeDescription
styleStyle | None

"spaced" separates every token and is byte-stable across releases. "compact" keeps only required spaces and drops braces around single-token scripts; it preserves parsed structure, but its layout may improve across releases.

Python
doc = texform.Parser().parse(r"\frac{a}{b}+x_i^2")["document"]
doc.to_latex()                  # '\frac { a } { b } + x _ { i } ^ { 2 }'
doc.to_latex(style="compact")   # '\frac{a}{b}+x_i^2'

Styletype#

Literal['spaced', 'compact']

Spaced output is byte-stable; compact preserves structure and text idempotency.