# serialize

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

https://texform.dev/api/python/serialize

## `serialize`

Kind: function

```python
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.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `node` | SyntaxNode | — | A ``SyntaxNode`` dict, typically from ``Document.to_syntax()`` or a stored snapshot. |
| `**options` | Unpack[SerializeOptions] | — | Serializer style overlays. Omitted keys and explicit ``None`` keep the default (spaced) style. |

**Returns** `str` The serialized LaTeX string.

- `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'
```

- [SerializeOptions](https://texform.dev/api/python/serialize.md#SerializeOptions)
- [Document.to_latex](https://texform.dev/api/python/document.md#Document.to_latex)
- [Serialization](https://texform.dev/docs/serialization.md)

## `SerializeOptions`

Kind: typedDict

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.

| Name | Type | Description |
| --- | --- | --- |
| `style` | Style | 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'
```

- [Document.to_latex](https://texform.dev/api/python/document.md#Document.to_latex)
- [serialize](https://texform.dev/api/python/serialize.md#serialize)

## `Style`

Kind: typeAlias

```python
Literal['spaced', 'compact']
```

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

