# @unified-latex/unified-latex-to-hast

> Convert a unified-latex AST to a HAST AST (for HTML conversion)

Latest version **1.8.4** (published 2026-04-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @unified-latex/unified-latex-to-hast
pnpm add @unified-latex/unified-latex-to-hast
yarn add @unified-latex/unified-latex-to-hast
bun add @unified-latex/unified-latex-to-hast
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.8.4 |
| Published | 2026-04-03 |
| First published | 2022-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 21 |
| Unpacked size | 373.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 127 |
| Author | Jason Siefken |
| Maintainers | siefkenj |
| Keywords | pegjs, latex, parser, prettier, unified-latex, unified |

## Links

- npm: https://www.npmjs.com/package/@unified-latex/unified-latex-to-hast
- Repository: https://github.com/siefkenj/unified-latex
- Homepage: https://github.com/siefkenj/unified-latex#readme
- Issues: https://github.com/siefkenj/unified-latex/issues
- npm.io page: https://npm.io/package/@unified-latex/unified-latex-to-hast

## Dependencies (21)

- [hast](https://npm.io/package/hast.md) ^1.0.0
- [cssesc](https://npm.io/package/cssesc.md) ^3.0.0
- [unified](https://npm.io/package/unified.md) ^10.1.2
- [hastscript](https://npm.io/package/hastscript.md) ^7.2.0
- [rehype-raw](https://npm.io/package/rehype-raw.md) ^6.1.1
- [rehype-stringify](https://npm.io/package/rehype-stringify.md) ^9.0.4
- [@unified-latex/unified-latex](https://npm.io/package/@unified-latex/unified-latex.md) ^1.8.4
- [@unified-latex/unified-latex-ctan](https://npm.io/package/@unified-latex/unified-latex-ctan.md) ^1.8.4
- [@unified-latex/unified-latex-lint](https://npm.io/package/@unified-latex/unified-latex-lint.md) ^1.8.4
- [@unified-latex/unified-latex-types](https://npm.io/package/@unified-latex/unified-latex-types.md) ^1.8.4
- [@unified-latex/unified-latex-builder](https://npm.io/package/@unified-latex/unified-latex-builder.md) ^1.8.4
- [@unified-latex/unified-latex-util-trim](https://npm.io/package/@unified-latex/unified-latex-util-trim.md) ^1.8.4
- [@unified-latex/unified-latex-util-align](https://npm.io/package/@unified-latex/unified-latex-util-align.md) ^1.8.4
- [@unified-latex/unified-latex-util-match](https://npm.io/package/@unified-latex/unified-latex-util-match.md) ^1.8.4
- [@unified-latex/unified-latex-util-visit](https://npm.io/package/@unified-latex/unified-latex-util-visit.md) ^1.8.4
- [@unified-latex/unified-latex-util-replace](https://npm.io/package/@unified-latex/unified-latex-util-replace.md) ^1.8.4
- [@unified-latex/unified-latex-util-comments](https://npm.io/package/@unified-latex/unified-latex-util-comments.md) ^1.8.4
- [@unified-latex/unified-latex-util-arguments](https://npm.io/package/@unified-latex/unified-latex-util-arguments.md) ^1.8.4
- [@unified-latex/unified-latex-util-html-like](https://npm.io/package/@unified-latex/unified-latex-util-html-like.md) ^1.8.4
- [@unified-latex/unified-latex-util-ligatures](https://npm.io/package/@unified-latex/unified-latex-util-ligatures.md) ^1.8.4
- [@unified-latex/unified-latex-util-print-raw](https://npm.io/package/@unified-latex/unified-latex-util-print-raw.md) ^1.8.4

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.8.4 (latest) — 2026-04-03
- 1.8.3 — 2025-06-16
- 1.8.2 — 2025-02-09
- 1.8.1 — 2024-10-21
- 1.8.0 — 2024-08-22
- 1.7.1 — 2024-03-19
- 1.7.0 — 2024-02-25
- 1.6.1 — 2024-02-18
- 1.6.0 — 2024-01-17
- 1.5.0 — 2023-10-02
- 1.4.2 — 2023-09-30
- 1.4.1 — 2023-09-02
- 1.4.0 — 2023-06-20
- 1.3.2 — 2023-04-26
- 1.3.1 — 2023-03-05
- … 13 more at https://npm.io/package/@unified-latex/unified-latex-to-hast/versions

## README

<!-- DO NOT MODIFY -->
<!-- This file was autogenerated by build-docs.ts -->
<!-- Edit the docstring in index.ts and regenerate -->
<!-- rather than editing this file directly. -->
# unified-latex-to-hast

## What is this?

Functions to convert `unified-latex` Abstract Syntax Tree (AST) to a HAST (html-like)
tree.

## When should I use this?

If you want to convert LaTeX to HTML.

## Controlling the HTML output

This plugin comes with presets for several common LaTeX macros/environments, but you probably want to
control how various macros evaluate yourself. For example, you may have used `\includegraphics` with `pdf`s
in your LaTeX source by want to output HTML that manipulates the path and includes `png`s instead.
You can accomplish this by passing `macroReplacements` (for environments, there is the similarly-named
`environmentReplacements`) to the plugin.

For example,

```typescript
import { unified } from "unified";
import rehypeStringify from "rehype-stringify";
import { htmlLike } from "@unified-latex/unified-latex-util-html-like";
import { printRaw } from "@unified-latex/unified-latex-util-print-raw";
import { unifiedLatexToHast } from "@unified-latex/unified-latex-to-hast";
import { unifiedLatexFromString } from "@unified-latex/unified-latex-util-parse";
import { getArgsContent } from "@unified-latex/unified-latex-util-arguments";

const convert = (value) =>
    unified()
        .use(unifiedLatexFromString)
        .use(unifiedLatexToHast, {
            macroReplacements: {
                includegraphics: (node) => {
                    const args = getArgsContent(node);
                    const path = printRaw(
                        args[args.length - 1] || []
                    ).replace(/\.pdf$/, ".png");
                    return htmlLike({
                        tag: "img",
                        attributes: { src: path },
                    });
                },
            },
        })
        .use(rehypeStringify)
        .processSync(value).value;

console.log(convert(`\\includegraphics{foo.pdf}`));
```

`macroReplacements` and `environmentReplacements` functions can return any unified-latex `Node`, but
using the `htmlLike` utility function will return nodes that get converted to specific HTML. See `htmlLike`'s
documentation for more details.

## Install

```bash
npm install @unified-latex/unified-latex-to-hast
```

This package contains both esm and commonjs exports. To explicitly access the esm export,
import the `.js` file. To explicitly access the commonjs export, import the `.cjs` file.

# Plugins

## `unifiedLatexToHast`

Unified plugin to convert a `unified-latex` AST into a `hast` AST.

### Usage

`unified().use(unifiedLatexToHast[, options])`

#### options

```typescript
PluginOptions
```

### Type

`Plugin<PluginOptions[], Ast.Root, Hast.Root>`

```typescript
function unifiedLatexToHast(
  options: PluginOptions
): (tree: Ast.Root, file: VFile) => Hast.Root;
```

## `unifiedLatexWrapPars`

Unified plugin to wrap paragraphs in `\html-tag:p{...}` macros.
Because `-` and `:` cannot occur in regular macros, there is no risk of
a conflict.

### Usage

`unified().use(unifiedLatexWrapPars[, options])`

#### options

```typescript
PluginOptions
```

### Type

`Plugin<PluginOptions[], Ast.Root, Ast.Root>`

```typescript
function unifiedLatexWrapPars(options: PluginOptions): (tree: Ast.Root) => void;
```

# Functions

## `attachNeededRenderInfo(ast)`

Attach `renderInfo` needed for converting some macros into their
katex equivalents.

```typescript
function attachNeededRenderInfo(ast: Ast.Ast): void;
```

**Parameters**

| Param | Type      |
| :---- | :-------- |
| ast   | `Ast.Ast` |

## `convertToHtml(tree, options)`

Convert the `unified-latex` AST `tree` into an HTML string. If you need
more precise control or further processing, consider using `unified`
directly with the `unifiedLatexToHast` plugin.

For example,

    unified()
         .use(unifiedLatexFromString)
         .use(unifiedLatexToHast)
         .use(rehypeStringify)
         .processSync("\\LaTeX to convert")

```typescript
function convertToHtml(
  tree: Ast.Node | Ast.Node[],
  options: PluginOptions
): string;
```

**Parameters**

| Param   | Type                     |
| :------ | :----------------------- |
| tree    | `Ast.Node \| Ast.Node[]` |
| options | `PluginOptions`          |

## `wrapPars(nodes, options)`

Wrap paragraphs in `<p>...</p>` tags.

Paragraphs are inserted at

*   parbreak tokens
*   macros listed in `macrosThatBreakPars`
*   environments not listed in `environmentsThatDontBreakPars`

```typescript
function wrapPars(
  nodes: Ast.Node[],
  options: {
    macrosThatBreakPars?: string[];
    environmentsThatDontBreakPars?: string[];
  }
): Ast.Node[];
```

**Parameters**

| Param   | Type                              |
| :------ | :-------------------------------- |
| nodes   | `Ast.Node[]`                      |
| options | <span color='gray'>Omitted</span> |

# Constants

| Name                                   | Type                                                                |
| :------------------------------------- | :------------------------------------------------------------------ |
| `KATEX_SUPPORT`                        | `{ macros: any; environments: any; }`                               |
| `katexSpecificEnvironmentReplacements` | `Record<string, (node: Ast.Environment) => Ast.Node \| Ast.Node[]>` |
| `katexSpecificMacroReplacements`       | `Record<string, (node: Ast.Macro) => Ast.Node \| Ast.Node[]>`       |

# Types

## `PluginOptions`

```typescript
export type PluginOptions = HtmlLikePluginOptions & {
    /**
     * By default, `unifiedLatexToHast` will force the output to be valid HTML.
     * This is accomplished by running `rehypeRaw` on the output which will ensure
     * there are no nested `<p>` tags, and that block elements don't end up as children of `<span>`s,
     * etc. Set to `true` to skip this check.
     */
    skipHtmlValidation?: boolean;
};
```

---
_Source: https://npm.io/package/@unified-latex/unified-latex-to-hast · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
