4.50.13 • Published 20 days ago

@cutting/use-mathjax v4.50.13

Weekly downloads
24
License
MIT
Repository
github
Last release
20 days ago

@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.

4.50.13

20 days ago

4.50.12

1 month ago

4.50.11

1 month ago

4.50.10

1 month ago

4.50.9

2 months ago

4.50.8

4 months ago

4.50.7

5 months ago

4.50.4

9 months ago

4.50.5

6 months ago

4.50.6

6 months ago

4.50.0

1 year ago

4.50.3

11 months ago

4.50.1

1 year ago

4.50.2

12 months ago

4.49.1

1 year ago

4.49.0

1 year ago

4.47.2

1 year ago

4.48.0

1 year ago

4.47.1

2 years ago

4.46.0

2 years ago

4.47.0

2 years ago

4.44.0

2 years ago

4.45.0

2 years ago

4.43.0

2 years ago

4.42.1

2 years ago

4.37.3

2 years ago

4.39.0

2 years ago

4.37.2

2 years ago

4.35.4

2 years ago

4.37.1

2 years ago

4.35.3

2 years ago

4.37.0

2 years ago

4.35.2

2 years ago

4.42.0

2 years ago

4.40.2

2 years ago

4.40.1

2 years ago

4.40.0

2 years ago

4.31.1

2 years ago

4.31.0

2 years ago

4.35.1

2 years ago

4.35.0

2 years ago

4.33.1

2 years ago

4.33.0

2 years ago

4.31.2

2 years ago

4.38.0

2 years ago

4.41.0

2 years ago

4.29.0

2 years ago

4.28.12

2 years ago

4.32.0

2 years ago

4.30.0

2 years ago

4.36.0

2 years ago

4.34.1

2 years ago

4.34.0

2 years ago

4.28.11

2 years ago

4.28.10

2 years ago

4.28.9

2 years ago

4.28.2

2 years ago

4.28.3

2 years ago

4.28.4

2 years ago

4.28.6

2 years ago

4.28.8

2 years ago

4.28.0

2 years ago

4.27.0

3 years ago

4.27.1

3 years ago

4.27.2

3 years ago

4.26.1

3 years ago

4.26.2

3 years ago

4.26.0

3 years ago

4.25.1

3 years ago

4.25.0

3 years ago

4.24.4

3 years ago

4.24.2

3 years ago

4.24.3

3 years ago

4.22.0

3 years ago

4.21.1

3 years ago

4.23.0

3 years ago

4.24.0

3 years ago

4.24.1

3 years ago

4.21.0

3 years ago

4.20.1

3 years ago

4.20.0

3 years ago

4.19.0

3 years ago

4.18.0

3 years ago

4.17.0

3 years ago

4.16.0

3 years ago

4.15.0

3 years ago

4.14.0

3 years ago

4.13.0

3 years ago

4.11.0

3 years ago

4.10.0

3 years ago

4.9.1

3 years ago

4.9.0

3 years ago