# Configure output style

Choose spaced or compact LaTeX output in the libraries, after normalization, and on the command line.

https://texform.dev/docs/configure-output-style

The serializer writes one of two output styles. `spaced`, the default, puts a space at every math token boundary and is byte-stable across releases, which suits datasets, caches, and tokenizers. `compact` writes LaTeX the way people usually type it, which suits display, review, and LLM prompts. The `style` option selects one for `to_latex()`, `to_tokenized_latex()`, and the module-level `serialize` function; it changes only the text layout, never the tree or the transform profile.

## Choose a style [#choose-a-style]

**Python**

```python
import texform

doc = texform.Parser().parse(r"\begin{pmatrix}x_a^b&y\end{pmatrix}")["document"]
print(doc.to_latex())
print(doc.to_latex(style="compact"))
```

**JavaScript**

```js
import { Parser } from 'texform';

const doc = new Parser().parse(String.raw`\begin{pmatrix}x_a^b&y\end{pmatrix}`).document;
console.log(doc.toLatex());
console.log(doc.toLatex({ style: 'compact' }));
```

```text
\begin {pmatrix} x _ { a } ^ { b } & y \end {pmatrix}
\begin{pmatrix}x_a^b&y\end{pmatrix}
```

Compact drops every optional space, and the braces around a script that is a single TeX token, such as `x_a` or `x_\alpha`. It keeps the braces the syntax needs, as in `x^{10}`, and the spaces that separate tokens, as in `\alpha x`. After an unbraced script it adds one space before a letter or digit, so `x_i y` does not read as a subscript `iy`.

Compact output is idempotent and parses back to the same tree, but its exact layout may still improve across releases. Store spaced output when you compare results across versions.

Python takes `style` as a keyword argument; JavaScript takes it in an options object. Omitting it, or passing `None` or `null`, selects `spaced`, and an unknown value raises a configuration error instead of falling back to the default.

## Normalize to a style [#normalize-to-a-style]

`normalize()` always returns spaced output: its configuration covers parsing and transformation, not serialization. To normalize into compact LaTeX, run the stages yourself and serialize the transformed document:

**Python**

```python
import texform

engine = texform.TransformEngine(profile="corpus")
doc = engine.parse(r"a \over b")["document"]
engine.transform(doc)
print(doc.to_latex(style="compact"))  # \frac{a}{b}
```

**JavaScript**

```js
import { TransformEngine } from 'texform';

const engine = new TransformEngine({ profile: 'corpus' });
const doc = engine.parse(String.raw`a \over b`).document;
engine.transform(doc);
console.log(doc.toLatex({ style: 'compact' })); // \frac{a}{b}
```

## On the command line [#on-the-command-line]

`texform normalize` takes the style as its own flag:

```console
$ texform normalize --profile corpus --style compact 'a \over b'
\frac{a}{b}
```

`--style` accepts `spaced` (the default) or `compact` and applies to plain and `--json` output alike. It is not a `--config` key, because `--config` holds the same normalize configuration as the libraries. `texform tokenize` always tokenizes the spaced form.

A `texform serve` client sets `style` in the `config` of a [`configure`](https://texform.dev/docs/command-line#configure) request, beside `profile`, and the response's `resolved` reports the style in effect.

## Next steps [#next-steps]

- [Serialization](https://texform.dev/docs/serialization) — Output channels, the spaced default, and the text-idempotency guarantee.

- [Serialize API](https://texform.dev/api/python/serialize) — The serialize function and its style option.

- [Edit a document](https://texform.dev/docs/edit-a-document) — Change the tree itself before you choose how to write it back out.

- [Use with a renderer](https://texform.dev/docs/use-with-a-renderer) — Normalize formulas so MathJax or KaTeX can render them.

