npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

asciimath-parser-nearley

v0.6.5

Published

Transform asciimath to LaTeX

Downloads

7

Readme

Asciimath Parser Nearley

Another asciimath parser based on nearley.

Usage

Install with a package manager.

pnpm install asciimath-parser-nearley

Import and create an instance of AsciiMath.

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:

    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:

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

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

Verbatim environment (experimental)

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

How to dev

We use 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.

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.

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
    verb "aaa
    bbb"
    translates to
    \begin{aligned}
    & \verb|aaa| \\
    & \verb|bbb|
    \end{aligned}
  • [ ] single paren case like ( and )
  • [ ] table syntax sugar
    table[
    a, b, c;
    d, e, f;
    ]
    is equal to
    {:
    --
    |a|b|c|
    --
    d, e, f;
    --
    :}