react-latex-next is a React component library designed for embedding and rendering LaTeX mathematical expressions within React applications. As of version 3.0.0, it leverages the high-performance KaTeX library to parse and display mathematical notation. The component automatically identifies LaTeX fragments within text using configurable delimiters, similar to KaTeX's auto-render extension, and renders them. While its release cadence is not fixed, it typically aligns with React ecosystem updates and bug fixes, maintaining compatibility with recent React versions. Key differentiators include its focus on straightforward React integration, support for custom KaTeX macros (with security considerations), and a default non-strict rendering mode that gracefully falls back to raw text on error, though a strict mode is available to throw errors. It requires explicit inclusion of KaTeX's CSS for correct styling.
npm install react-latex-nextVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import the `Latex` component, include the necessary KaTeX CSS, and render both inline and display-mode LaTeX equations within a React functional component. It also shows how to define and use custom KaTeX macros via the `macros` prop and configures `strict` mode.
Ensure `import 'katex/dist/katex.min.css';` is present in your entry file or a relevant component. For Next.js, this typically goes into `_app.js` or directly into the component.
Sanitize any user-provided input before using it to define KaTeX macros via the `macros` prop. Refer to the KaTeX API documentation for detailed security guidance on persistent macros.
To enable strict error handling, pass the `strict` prop to the `Latex` component: `<Latex strict>{textWithPotentiallyBrokenLatex}</Latex>`. This will cause rendering to fail and throw an error for invalid LaTeX.Check the `peerDependencies` in `react-latex-next`'s `package.json` (e.g., `"react": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0"`) and upgrade or downgrade your `react` and `react-dom` packages accordingly.
When writing LaTeX inside JavaScript strings, especially with template literals, always double-escape backslashes: `$`\\alpha`$`.
Add `import 'katex/dist/katex.min.css';` to your application's entry point (e.g., `index.js`, `_app.js` in Next.js) or to the component where `Latex` is used.
Either correct the LaTeX syntax within the string, or remove the `strict` prop (it defaults to `false`, which displays the raw text instead of throwing an error).
Ensure that the `children` prop passed to the `Latex` component is always a single string containing the LaTeX content. Concatenate multiple strings if necessary.