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

extract-math

v1.2.3

Published

Extract TeX math environments

Downloads

16,805

Readme

extract-math

Build Status npm npm bundle size

Extract TeX math environments.

This package parses TeX shorthands for mathematics environments and extracts inline formulas (e.g.: $x + 1$) and displayed equations (e.g.: $$\sum_{i=1}^n 2^i$$).

import { extractMath } from 'extract-math'

const segments = extractMath('hello $e^{i\\pi}$')

console.log(segments)
// Output: [
//   { type: 'text', math: false, value: 'hello ', raw: 'hello ' },
//   { type: 'inline', math: true, value: 'e^{i\\pi}', raw: 'e^{i\\pi}' }
// ]

The primary use-case is to process the math segments with a math typesetting library like KaTeX.

Installation

You can install extract-math with your npm client of choice.

$ npm install extract-math

Usage

extractMath(input, options?)

Parse the input string and return an array of Segment objects. Segment objects are defined by the following typescript interface:

interface Segment {
  type: 'text' | 'display' | 'inline'
  math: boolean
  value: string
  raw: string
}

The Segment interface can be imported with import { Segment } from 'extract-math'

The function splits the input string into 3 different types of segments:

  • Plain text segments have a text type and the math property set to false
  • Displayed equations have a display type and the math property set to true
  • Inline formulas have an inline type and the math property set to true

The function will check that the closing delimiter isn't immediately followed by a digit before extracting a math segment. This prevents input like $20,000 and $30,000 from being interpreted as inline math.

The second parameter is optional and lets you specify custom options:

interface ExtractMathOptions {
  escape?: string
  delimiters?: {
    inline?: [string, string]
    display?: [string, string]
  }
  allowSurroundingSpace?: Array<'display' | 'inline'>
}

The ExtractMathOptions interface can be imported with import { ExtractMathOptions } from 'extract-math'

Here are the default values:

{
  escape: '\\',
  delimiters: {
    inline: ['$', '$'],
    display: ['$$', '$$']
  },
  allowSurroundingSpace: ['display']
}

You can extract formulas that use LaTeX math delimiters with the following options:

const segments = extractMath('hello \\(e^{i\\pi}\\)', {
  delimiters: {
    inline: ['\\(', '\\)'],
    display: ['\\[', '\\]']
  }
})

console.log(segments)
// Output: [
//   { type: 'text', math: false, value: 'hello ', raw: 'hello ' },
//   { type: 'inline', math: true, value: 'e^{i\\pi}', raw: 'e^{i\\pi}' }
// ]

By default, only the display mode allows the formula to be surrounded by space. You can change this with the allowSurroundingSpace option:

segments = extractMath('$ 42 $$$ 42 $$', {
  allowSurroundingSpace: ['inline', 'display']
})

console.log(segments)
// Output: [
//   { type: 'inline', math: true, value: ' 42 ', raw: ' 42 ' },
//   { type: 'display', math: true, value: ' 42 ', raw: ' 42 ' }
// ]

Escaping

Any delimiter immediately preceded by a backslash \ will be automatically escaped.

const segments = extractMath('in plain \\$ text $$in \\$ equation$$')

console.log(segments)
// Output: [
//   { type: 'text', math: false, value: 'in plain $ text ', raw: 'in plain $ text ' },
//   { type: 'display', math: true, value: 'in $ equation', raw: 'in \\$ equation' }
// ]

The raw property is set to the original string without interpreting the escape sequences. For plain text segments, the property is equal to the value property.

This comes in handy if you're feeding the math expressions to a math typesetting library like KaTeX that expects dollar signs to be escaped.

katex.render(segments[1].raw, ...)

Contributing

Contributions are welcome. This project uses jest for testing.

$ npm test

The code follows the javascript standard style guide adapted for typescript.

$ npm run lint

License - MIT