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

mathjax3-react

v1.2.0

Published

React component to easily load MathJax **v3** and process **dynamically** raw ASCIIMath, TeX or MathML content.

Downloads

466

Readme

MathJax3 - React

React component to easily load MathJax v3 and process dynamically raw ASCIIMath, TeX or MathML content.

Installation

NPM:

npm install mathjax3-react --save

YARN:

yarn add mathjax3-react

Basic usage

From HTML String

import React from 'react';
import { MathJaxProvider, MathJaxHtml } from 'mathjax3-react';

function App() {
  return (
    <div>
      <MathJaxProvider>
        <MathJaxHtml html={html} />
      </MathJaxProvider>
    </div>
  );
}

HTML string:

const html = `
<p>Let's analise this equation:</p>
<p style="text-align:center;">
  <math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
    <msup>
      <mrow>
        <mi>r</mi>
      </mrow>
      <mrow>
        <mn>2</mn>
      </mrow>
    </msup>
    <mo>+</mo>
    <msup>
      <mrow>
        <mi>z</mi>
      </mrow>
      <mrow>
        <mn>2</mn>
      </mrow>
    </msup>
    <mo>=</mo>
    <mn>4</mn>
  </math>
</p>
`;

Result:

basic html example

TeX or AsciiMath formula

import React from 'react';
import { MathJaxProvider, MathJaxFormula } from 'mathjax3-react';

function basicUsage() {
  return (
    <div>
      <MathJaxProvider>
        <MathJaxFormula formula="$$\int x^2dx$$" />
      </MathJaxProvider>
    </div>
  );
}

export default basicUsage;

Result:

basic formula example

MathJaxProvider Component

The MathJaxProvider component must be used as a parent for the MathJaxHtml and MathJaxFormula components. This is essential because MathJaxProvider is responsible for loading the MathJax script, which the MathJaxHtml or MathJaxFormula components will consume and utilize.

Rationale Behind This Design

Loading MathJax is a resource-intensive process. To optimize performance, MathJaxProvider should be placed high in your component hierarchy to load the MathJax script only once. This approach prevents the script from being reloaded unnecessarily and allows MathJaxHtml and MathJaxFormula components to operate within contexts that update more frequently, thereby leveraging the pre-loaded MathJax script efficiently.

import React from 'react';
import { MathJaxProvider, MathJaxFormula } from 'mathjax3-react';

function MathInterleavedWithText() {
  return (
    <div>
      <p>Consider the following integral as an example:</p>
      <MathJaxFormula formula="\\int x^2dx" />
      <p>Euler's identity is an astonishing formula in the field of mathematics:</p>
      <MathJaxFormula formula="e^{i\\pi} + 1 = 0" />
      {/* More content and formulas can be added here */}
    </div>
  );
}

function App() {
  return (
    <MathJaxProvider>
      <h1>Mathematical Concepts</h1>
      <MathInterleavedWithText />
    </MathJaxProvider>
  );
}

export default App;

Custom options

You can set custom script url or MathJax by sending them as props to MathJax.Provider component

import React from 'react';
import { MathJaxProvider, MathJaxFormula } from 'mathjax3-react';

function customOptions() {
  return (
    <div>
      <MathJaxProvider
        url="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"
        options={{
          tex: {
            inlineMath: [
              ['$', '$'],
              ['\\(', '\\)'],
            ],
          },
        }}
      >
        <MathJaxFormula formula="Euler's identity: $e^{i\pi} = -1$" />
      </MathJaxProvider>
    </div>
  );
}

export default customOptions;

Result:

custom formula example

Which options are available?

Options props are exactly the same options used in MathJax lib. So you can use official MathJax documentation to set custom options.

Custom Input

import React, { useState } from 'react';
import { MathJaxProvider, MathJaxFormula } from 'mathjax3-react';

function CustomInput() {
  const [value, setValue] = useState('\\int_{-\\infty}^{+\\infty} e^{-x^2} dx = \\sqrt{\\pi}');

  return (
    <div className="App">
      <h1>Custom Math Input</h1>
      <input type="text" value={value} onChange={(e) => setValue(e.target.value)} style={{ width: '100%' }} />
      <MathJaxProvider>
        <MathJaxFormula formula={'$$' + value + '$$'} />
      </MathJaxProvider>
    </div>
  );
}

export default CustomInput;