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

@cutting/use-mathjax

v4.50.17

Published

easy mathjax in react.

Downloads

125

Readme

@cutting/use-mathjax

React components <MathJax />, <SVGMathJax> and a useMathJax hook to easily load MathJax version 3 Tex content.

A real working demo is here which is why I wrote this component.

Here is a working codesandbox.

install

pnpm add @cutting/use-mathjax

MathJax components

Probably the easiest way to use use-mathjax is to use either the <MathJax/> component or the <SVGMathJax> component.

MathJax

<MathJax/> is for rendering mathjax expressions into plain old HTML.

import { MathJax, MathJaxProvider } from '@cutting/use-mathjax';

const Maths = () => {
  return (
    <>
      <MathJax> {\`$$\\\\int x^2dx$$\`}</MathJax>
      <MathJax> {\`$$\\\\frac{5\\\\pi}4$$\`}</MathJax>
      <MathJax> {\`$$\\\\frac{3\\\\pi}2$$\`}</MathJax>
    </>
  )
}

export const App(): JSX.Element {
  return (
    <StrictMode>
      <MathJaxProvider>
        <Maths />
      </MathJaxProvider>
    </StrictMode>
  );
};

SVGMathJax

<SVGMathJax> is for rendering mathjax into an existing svg component.

import { MathJax, MathJaxProvider } from '@cutting/use-mathjax';

const Maths = () => {
  return (
   <svg preserveAspectRatio="xMaxYMid meet" viewBox="0 0 960 654" style={{ overflow: 'visible' }}>
     {[
       { value: Math.PI / 4, label: '$\\frac{\\pi}4$' },
       { value: Math.PI / 2, label: '$\\frac{\\pi}2$' },
      ].map(({ cosX, sinY, label, offsetX, offsetY }) => (
     <g key={label}>
        <SVGMathJax>{label}</SVGMathJax>
     </g>
   </svg>
  )
}

export const App(): JSX.Element {
  return (
    <StrictMode>
      <MathJaxProvider>
        <Maths />
      </MathJaxProvider>
    </StrictMode>
  );
};

The SVGMathJax component takes an expr string prop of MathJax markup.

MathJaxProvider

Both the <MathJax/> and the <SVGMathJax/> components and the useMathJax hook will fail if the current component is not a descendant of the MathJaxProvider:

export { MathJaxProvider } from '@cutting/use-mathjax';

export const App(): JSX.Element {
  return (
    <StrictMode>
      <MathJaxProvider>
        <Maths />
      </MathJaxProvider>
    </StrictMode>
  );
};

useMathJax hook

This is a low level hook used by both the <MathJax/> and the <SVGMathJax/> components

import { useMathJax, MathJaxProvider } from '@cutting/use-mathjax';

const Maths = () => {
  const ref = useRef<HTMLParagraphElement>();
  useMathJax({ elements: ref });

  return (
      <div ref={ref}>
        <p className="math">{`$$x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}$$`}</p>
      </div>
  )
}

export const App(): JSX.Element {
  return (
    <StrictMode>
      <MathJaxProvider>
        <Maths />
      </MathJaxProvider>
    </StrictMode>
  );
};

math notation rendered with useMathJax hook

useMathJax takes a configuration object with a fields property that points to an element of an array of elements with MathJax markup.