Render LaTeX in React
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.
Common errors
-
LaTeX expressions are not rendering; only showing raw text or unstyled symbols.
cause The KaTeX CSS file (`katex/dist/katex.min.css`) has not been imported or included in the project's build.fixAdd `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. -
Error: KaTeX parse error: LaTeX parse error: Undefined control sequence: \xxx
cause This error typically occurs when the `strict` prop is enabled on the `Latex` component, and the LaTeX string contains a parsing error (e.g., an undefined command, mismatched delimiters, or invalid syntax).fixEither 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). -
Error: Cannot read properties of undefined (reading 'replace') at createHtml
cause This error can occur if the `Latex` component receives an unexpected child type, such as an array of strings instead of a single string, or `null`/`undefined` children.fixEnsure that the `children` prop passed to the `Latex` component is always a single string containing the LaTeX content. Concatenate multiple strings if necessary.
Warnings
- gotcha The KaTeX CSS file (`katex/dist/katex.min.css`) must be explicitly imported or included in your project's bundle for LaTeX expressions to render correctly. Without it, equations may appear unstyled, misaligned, or as raw text.
- gotcha The `macros` prop allows defining custom KaTeX macros. Be aware that defining macros via `\gdef` can have security implications, as mentioned in the KaTeX documentation. Arbitrary code execution can occur if untrusted input is used in macro definitions.
- gotcha By default, `react-latex-next` renders in 'non-strict' mode, which means it will fall back to displaying the raw text (without delimiters) if a LaTeX parsing error occurs. If you prefer to throw a JavaScript error instead, you must enable `strict` mode.
- breaking Major versions of `react-latex-next` may update their peer dependencies on `react` and `react-dom`. Ensure your project's React versions are compatible with the peer dependency ranges specified in `react-latex-next`'s `package.json` to avoid installation warnings or runtime issues.
- gotcha Backslashes in LaTeX strings passed as children to the `Latex` component need to be escaped in JavaScript/JSX template literals. For example, `\alpha` should be written as `\\alpha`.
Install
-
npm install react-latex-next -
yarn add react-latex-next -
pnpm add react-latex-next
Imports
- Latex
const Latex = require('react-latex-next');import Latex from 'react-latex-next';
- KaTeX CSS
import 'katex/dist/katex.min.css';
- delimiters (prop)
<Latex>{`$$...$$`}</Latex> // If you want custom delimiters without prop<Latex delimiters={[{ left: '$$', right: '$$', display: true }]}>...</Latex>
Quickstart
import React from 'react';
import 'katex/dist/katex.min.css';
import Latex from 'react-latex-next';
function App() {
const latexContent = `We give illustrations for the {1 + 2} processes $e^+e^-$, gluon-gluon and $\gamma\gamma \to W t\bar b$.
This is a block equation:
$$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$$
Here is an inline equation: $(ax^2 + bx + c = 0)$.
And a custom macro example: $\\f\\relax{x} = x$ is rendered using macros.`;
return (
<div style={{ padding: '20px', fontFamily: 'sans-serif' }}>
<h1>React LaTeX Example</h1>
<Latex
macros={{
"\\f": "#1f(#2)"
}}
strict={false}
>
{latexContent}
</Latex>
<p>This example demonstrates rendering inline and block LaTeX, and using custom macros. The KaTeX CSS is imported directly.</p>
</div>
);
}
export default App;