# serialize

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

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

## `serialize`

Kind: function

```ts
serialize(node: SyntaxNode, options: SerializeOptions | null | undefined): string
```

**Deprecated.** Prefer `Document.fromSyntax(node).toLatex(options)` or
`document.toLatex(options)`. This remains exported as a compatibility helper
for older snapshot call sites and takes the same options as `toLatex`.

Render a [SyntaxNode](/api/js/parser#SyntaxNode) object to LaTeX text.

`Error` nodes round-trip their captured snippet. An ordinary `Prime` emits
`\prime` symbols, while a pure prime superscript serializes compactly as
`f'` or `f''`. The serializer guarantees text
idempotency.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `node` | SyntaxNode | — | - A [SyntaxNode](/api/js/parser#SyntaxNode) object, typically from
[Document.toSyntax](/api/js/document#Document.toSyntax) or a stored snapshot. |
| `options` | SerializeOptions | null | undefined | — | - A [SerializeOptions](/api/js/serialize#SerializeOptions) object, or omit/`null` for the
default (spaced) style. |

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

```ts
import { Parser, serialize } from 'texform';

const result = new Parser().parse(String.raw`x^2`);
if (!result.document) throw new Error('parse failed');
const syntax = result.document.toSyntax();
serialize(syntax);                       // 'x ^ { 2 }'
serialize(syntax, { style: 'compact' }); // 'x^2'
```

- [SyntaxNode](https://texform.dev/api/js/parser.md#SyntaxNode)
- [Document.toSyntax](https://texform.dev/api/js/document.md#Document.toSyntax)
- [SerializeOptions](https://texform.dev/api/js/serialize.md#SerializeOptions)

## `SerializeOptions`

Kind: interface

Options for [Document.toLatex](/api/js/document#Document.toLatex), [Document.toTokenizedLatex](/api/js/document#Document.toTokenizedLatex),
and [serialize](/api/js/serialize#serialize). Unknown keys and invalid values throw
`TexformConfigError`. For a task-oriented walkthrough, see the
Serialization guide.

| Name | Type | Description |
| --- | --- | --- |
| `style` | Style | null | undefined | `"spaced"` (default when omitted or `null`) 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. |

```ts
const doc = new Parser().parse(String.raw`\frac{a}{b}+x_i^2`).document;
doc.toLatex(); // '\\frac { a } { b } + x _ { i } ^ { 2 }'
doc.toLatex({ style: 'compact' }); // '\\frac{a}{b}+x_i^2'
```

- [Document.toLatex](https://texform.dev/api/js/document.md#Document.toLatex)
- [Document.toTokenizedLatex](https://texform.dev/api/js/document.md#Document.toTokenizedLatex)
- [serialize](https://texform.dev/api/js/serialize.md#serialize)

## `Style`

Kind: typeAlias

```ts
"spaced" | "compact"
```

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

