CharlieMcVicker / mathjax-react

React Component Library for MathJax
33 stars 14 forks source link

mathjax-react

This project is no longer actively maintained

React Component Library for MathJax

NPM

For a lightweight, build-your-own alternative, check out this gist.

Install

Install the mathjax-react package:

npm install --save mathjax-react

Install MathJax-full as a sibling dependency. This lets your bundler shake the mathjax-full tree and reduce bundle size.

npm install --save mathjax-full

Usage

Take a look at the typescript-examples on our GitHub Pages

import * as React from "react";

import { MathComponent } from "mathjax-react";

class Example extends React.Component {
  render() {
    return <MathComponent tex={String.raw`\int_0^1 x^2\ dx`} />;
  }
}

Reference

Currently, this library contains one component, called the MathComponent. This is how you can interact with MathJax.

MathComponent

Props

useMathJax Hook

This hook provides lower-level access to MathJax for more complicated typesetting needs. For an example, see the live typesetting example.

Props

Returns

Migrating from 1.0.6 to 2.0.0 (React 18 support)

To support React 18, we did a full rewrite. Now, instead of providing onError and onSuccess callbacks on MathComponent we provide a useMathJax hook, which returns three things:

  1. Any errors from MathJax
  2. The most recently rendered HTML from MathJax
  3. A getProps function that should be attached to a node somewhere in the tree. This node will be filled (not replaced) with the HTML from MathJax.

Below is a simple component that replicates the old onError and onSuccess behavior.

function MyComponent({onError, onSuccess, ...props}) {
  { error, renderedHTML, getProps } = useMathJax(props);

  useMemo(() => {
    if(error) onError(error)
  }, [error]);

  useMemo(() => {
    if(renderedHTML) onSuccess();
  }, [renderedHTML]);

  return <div {...getProps()} />
}

Developer Setup

Install

Make sure everything is installed in the main directory:

npm i -D

Build the main directory so that we can link:

npm run build

Run npm link in the main directory to create a global symlink:

npm link

Run install and npm link in the typescript-examples directory to connect the library to the examples.

cd typescript-examples/
npm i -D
npm link mathjax-react

Usage

When working on examples, it is only required to run the following (in typescript-examples/):

npm start

When also working on the library itself, one must also run (in the main directory):

npm start

If rollup is not catching updates to files, the following may work:

npx rollup -w -c

Manifest

src/

Library Source

typescript-examples

Examples using mathjax-react and TypeScript.

License

MIT © charliemcvicker