Render LaTeX in React

3.0.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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.

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;

view raw JSON →