Configure MathJax via the config object
masterconfig object passed to the context. This allows you to access the full power of the underlying MathJax library.repository·master·Indexed 41 days ago
https://github.com/fast-reflexes/better-react-mathjaxA React component library for integrating MathJax into applications, supporting LaTeX, AsciiMath, and MathML. It utilizes functional components and the hooks API to render math while minimizing flashes of non-typeset content. Version 3.0.3-beta provides support for MathJax versions 2, 3, and 4, offering a provider-consumer pattern via MathJaxContext and MathJax components.
config object passed to the context. This allows you to access the full power of the underlying MathJax library.The renderMode property determines how MathJax handles typesetting:
post (Recommended): Use this in most scenarios.pre: Use this when using dynamic updates and you need to prevent MathJax from typesetting all content after every render for performance reasons, or for other specific use cases where post is problematic.To use MathJax in your React application, you must follow a two-step pattern:
MathJaxContext: This component is responsible for downloading MathJax and providing it to all child components. You should only use one MathJaxContext in your entire application. By default, it imports MathJax from a CDN.MathJax components for math content: Place MathJax components at various levels of your component tree to render actual math. The content inside a MathJax component can range from a single string of math to a large subtree of the DOM.Note: Both components include the "use client" directive, making them client components in frameworks like Next.js.
const App = () => {
return (
<MathJaxContext>
{/* APP CONTENT */}
</MathJaxContext>
)
}
const Component = () => {
return (
<div>
<MathJax>{ /* math content */ }</MathJax>
<p>Text with <MathJax>{ /* math content */ }</MathJax></p>
</div>
)
}The library uses a provider-consumer pattern to manage MathJax:
MathJaxContext: Acts as the provider. It downloads the MathJax library and provides the context to all descendant components.MathJax (and MathJaxBaseContext): These are the consumer components. They use the context provided by MathJaxContext to typeset their content.Typesetting Lifecycle:
MathJax component typesets its content once initially.dynamic flag is set, it typesets every time a change is detected.# Concept: Provider-Consumer Pattern
<MathJaxContext>
<MathJax> ... </MathJax>
</MathJaxContext>MathJax supports display math (takes up more space/different font) and inline math (integrated into text).
asciimath: { displaystyle: false } to your input configuration.inline prop: The MathJax component has an inline prop. This controls whether the wrapper element added by the component is inline or block. It is not synonymous with the math typesetting mode itself.Exception: If the MathJax component's renderMode prop is set to "pre", the component becomes a single piece of math (provided via the text prop), and the inline property directly controls the math mode of the output.
MathJaxContext is a stateful component that downloads MathJax. While you can use multiple instances, it is highly recommended to wrap your entire application in a single MathJaxContext and avoid using it anywhere else. This ensures MathJax is only downloaded once and provides a stable environment for the lifetime of your app.version prop to 3 or update your configuration settings. Refer to the official MathJax migration guide for details on configuration changes.A common challenge is preventing users from seeing raw math strings before MathJax has finished typesetting. Depending on your content type, use these strategies:
hideUntilTypeset: Set this prop to 'first' on individual MathJax components or on the MathJaxContext to hide content until the initial typesetting is complete.onInitTypeset on components or onStartUp/onLoad on the MathJaxContext to show entire page sections only after typesetting is finished.dynamic={true}: Ensure this is set so that updates are re-typeset.hideUntilTypeset='every': Set this to 'every' to hide content during every update. This may cause a short blink but prevents showing unformatted math.renderMode='pre': This mode typesets the math before inserting it into the DOM. This is often the most effective way to eliminate flashes, though it may limit MathJax's ability to adapt to the surrounding context.To ensure correct typesetting and performance, follow these guidelines:
MathJax components. This causes the same math content to be typeset multiple times unnecessarily.dynamic property is set to true, avoid mixing math expressions with literal text within the same DOM element due to React/DOM manipulation conflicts.<p>{ "An example is the equation $${num}x^4 = 100$" }</p><p>An example is the equation <span>{ "$${num}x^4 = 100$" }</p><p>An example is the equation ${num}x^4 = 100$</p><p>An example is the equation { "$${num}x^4 = 100$" }</p>MathJax component also re-rendering. If a child re-renders but the parent MathJax component does not, the math will not be typeset anew. If you encounter this, wrap smaller portions of the math in their own MathJax components so that the state change triggers a re-render of the component containing the math.MathJax components with a single MathJax component wrapping a larger portion of the DOM sub-tree to reduce typesetting overhead.MathJaxContext, the version prop must remain consistent. If you are loading a completely new page context (not just a route change in a Single Page Application), the version can be determined anew.You can install better-react-mathjax using npm or yarn. Add it to your package.json dependencies or run the following commands:
# npm
npm install better-react-mathjax
# yarn
yarn add better-react-mathjaxMathJax 2 uses a Callback and Queue system to handle asynchronous operations and ensure that typesetting tasks do not conflict.
Callback: Used to schedule tasks. You can use Delay(time, callback) to postpone execution or Queue(...args) to add tasks to the internal processing queue.Queue: Manables the execution of tasks. It tracks pending and running tasks and provides methods like Push(specs), Suspend(), and Resume() to control the flow of processing.CallbackObject: Represents a scheduled task, allowing you to call .reset() to cancel it.