# asciimath-parser-nearley

> Transform asciimath to LaTeX

Latest version **0.6.11** (published 2026-04-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install asciimath-parser-nearley
pnpm add asciimath-parser-nearley
yarn add asciimath-parser-nearley
bun add asciimath-parser-nearley
```

## Health

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

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

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.6.11 |
| Published | 2026-04-02 |
| First published | 2023-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 114.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 21 |
| Author | widcardw |
| Maintainers | icouldfran, widcardw |
| Keywords | asciimath, LaTeX |

## Links

- npm: https://www.npmjs.com/package/asciimath-parser-nearley
- Repository: https://github.com/widcardw/asciimath-parser
- Homepage: https://github.com/widcardw/asciimath-parser#readme
- Issues: https://github.com/widcardw/asciimath-parser/issues
- npm.io page: https://npm.io/package/asciimath-parser-nearley

## Dependencies (1)

- [moo](https://npm.io/package/moo.md) ^0.5.2

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 0.6.11 (latest) — 2026-04-02
- 0.6.10 — 2025-11-03
- 0.6.9 — 2025-08-12
- 0.6.8 — 2025-08-11
- 0.6.6 — 2025-08-11
- 0.6.5 — 2024-06-14
- 0.6.4 — 2024-06-05
- 0.6.3 — 2023-10-05
- 0.6.2 — 2023-07-26
- 0.6.1 — 2023-07-21
- 0.6.0 — 2023-04-14
- 0.5.9 — 2023-04-12
- 0.5.8 — 2023-03-19
- 0.5.7 — 2023-03-17

## README

# Asciimath Parser Nearley

Another asciimath parser based on [nearley](https://nearley.js.org).

## Usage

Install with a package manager.

```sh
pnpm install asciimath-parser-nearley
```

Import and create an instance of AsciiMath.

```js
import { AsciiMath } from 'asciimath-parser-nearley'
const am = new AsciiMath({
  display: false,
  throws: false,
  symbols: {
    keywords: {/** ... */},
    /** ... */
  },
  replaceBeforeTokenizing: [],
})
console.log(am.toTex('sum_(n=1)^(+oo)1/n^2=pi^2/6'))
// \sum_{ n = 1 }^{ + \infty } \frac{ 1 }{ n^2 } = \frac{ \pi^2 }{ 6 }
```

> **Warning**
> Some rules are not consistent with `asciimath-parser`. Please view the documentation below.

## Differences

Asciimath Parser **Core** version is written in pure TypeScript, and we may
fail to consider some edge cases, so please report any issue if you run into
any error.

Asciimath Parser **Nearley** version is parsed with nearley grammar, so it is
**strict** and you **have to write correct formulas**.

| output | AM core | AM nearley |
| ------ | ---------------- | ------------------ |
| ${\color{red} a}$ | `color(red)(a)` | `color"red" a` |
| $\text{text}$ | `text(text)` | `text"text"` |
| $a\hspace{12pt}b$ | `a hspace(12pt) b` | `a hspace"12pt" b` |
| $\frac{\partial L}{\partial \sigma^2}$ | `pp L (sigma^2)` | `pp L sigma^2` or `pp L (sigma^2)` |

### Performance

Nearley version is about 40% slower than core version; the tokenization step take about half time.

### String Literal

In Nearley version, text have to be quoted with `"` to skip tokenize, for example the argument of `color`, `tex`, and `hspace` command must be quoted. You can use escape sequences `\"` and `\\` in a string literal.

### Different params

- **symbols: Symbols**

  In Nearley version, We removed the `extConst` param, and added `symbols` param:
  ```ts
  const am = new AsciiMath({
    symbols: {
      keyword: {
        dx: { tex: '{\\text{d}x}' },
        dy: { tex: '{\\text{d}y}' },
        dz: { tex: '{\\text{d}z}' },
        dt: { tex: '{\\text{d}t}' },
        ee: { tex: '\\text{e}' },
        ii: { tex: '\\text{i}' },
      },
      opOAB: {
        fbox: { tex: '\\fbox[ $2 ]{ $1 }' },
      },
    },
  })
  ```
- **throws: boolean**

  If true, throws error when parser encounters any syntax error.
  If false, just output error message as a text string.
  Default is false.

### New Grammar

Added new infix symbol `over`, `atop` and `choose`:

```text
a over b,
a atop b,
a choose b
```

Added new symbol `--` which will be transformed into `\hline`. You can draw a table like this

```text
{:
--
|a|b|;
--
c, d;
--
:}
```

Verbatim environment (experimental)

```text
verb"#include<stdio.h>
int main() {
  printf(\"hello, world!\n\");
  return 0;
}"
```

## How to dev

We use [`moo`](https://www.npmjs.com/package/moo) as the tokenizer, but it does not seem to support ESM, so we downloaded the repo and built it locally.

The parser core is _`parser.ne`_. After changing the file, you should run the following code to compile it to _`grammar.js`_.

```sh
pnpm run nearley
```

Then the _`index.ts`_ will export `AsciiMath` properly. You can use it like below, and the params are  *almost* the same as `asciimath-parser`.

```ts
const am = new AsciiMath()
const tex = am.toTex('sum_(n=1)^(+oo)1/n^2=pi^2/6')
console.log(tex)
// \displaystyle{ \sum_{ n = 1 }^{ + \infty } \frac{ 1 }{ n^2 } = \frac{ \pi^2 }{ 6 } }
```

## TODO

- [x] `==_a^b`
- [x] multiline formulas
- [x] unicode support
- [x] escape backslashes in text like `"\\"`
- [x] vabatim environment
  ```text
  verb "aaa
  bbb"
  ```
  translates to
  ```tex
  \begin{aligned}
  & \verb|aaa| \\
  & \verb|bbb|
  \end{aligned}
  ```
- [ ] single paren case like `(` and `)`
- [ ] table syntax sugar
  ```text
  table[
  a, b, c;
  d, e, f;
  ]
  ```
  is equal to
  ```text
  {:
  --
  |a|b|c|
  --
  d, e, f;
  --
  :}
  ```

---
_Source: https://npm.io/package/asciimath-parser-nearley · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
