npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@latex-math/react

v1.2.2

Published

Interactive React components and hooks for math input, live rendering, and real-time LaTeX evaluation. Powered by `@latex-math/core` and MathLive.

Readme

@latex-math/react

Interactive React components and hooks for math input, live rendering, and real-time LaTeX evaluation. Powered by @latex-math/core and MathLive.

npm version License: MIT


Features

  • 🧮 Interactive LatexInput: Visual, WYSIWYG math editing powered by MathLive. Supports keyboard shortcuts (/ for fractions, ^ for powers), symbol toolbars, and instant copy buttons.
  • 🔄 TeX Auto-Normalization: Visual indicators and automatic correction for unbraced syntax (e.g. typing \sqrt 1+2 prompts or normalizes to \sqrt{1} + 2).
  • ✍️ Fast Rendering (LatexExpression): Built-in KaTeX equation renderer for display and inline mathematical formulas.
  • 🎣 Evaluation Hook (useLatexEvaluation): Reactive hook that parses and evaluates mathematical LaTeX on the fly with custom variable scopes and error boundaries.

Installation

Install both @latex-math/react and @latex-math/core:

npm install @latex-math/react @latex-math/core
yarn add @latex-math/react @latex-math/core
# or
pnpm add @latex-math/react @latex-math/core

CSS Import (Required for Math Formulas)

Import KaTeX styles in your root layout or application entry (layout.tsx, _app.tsx, or main.tsx):

import "@latex-math/react/dist/index.css";
// or
import "katex/dist/katex.min.css";

Using with Next.js & Tailwind CSS

  1. Client Component: Because <LatexInput /> interacts with the browser's custom element DOM, ensure your component has the "use client" directive at the top:
"use client";
import { LatexInput } from '@latex-math/react';
  1. Tailwind CSS Configuration: To ensure Tailwind compiles the utility classes used inside @latex-math/react, add the package to your tailwind.config.js:
// tailwind.config.js
module.exports = {
  content: [
    "./app/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
    "./node_modules/@latex-math/react/**/*.{js,mjs,cjs}", // <-- Add this line
  ],
  // ...
};

(Or in Tailwind v4 globals.css: @source "../node_modules/@latex-math/react";)


Quick Start

1. LatexInput (WYSIWYG Math Editor)

import React, { useState } from 'react';
import { LatexInput } from '@latex-math/react';

export function MathEditor() {
  const [latex, setLatex] = useState('\\frac{a}{b} + \\sqrt{x}');

  return (
    <div className="max-w-xl mx-auto p-4">
      <LatexInput
        value={latex}
        onChange={setLatex}
        placeholder="Type math..."
        showToolbar={true}
        showLatexBadge={true}
      />
    </div>
  );
}

2. LatexExpression (Math Display)

Render static or dynamic mathematical expressions with KaTeX:

import React from 'react';
import { LatexExpression } from '@latex-math/react';

export function EquationDisplay() {
  return (
    <div>
      {/* Display block */}
      <LatexExpression expression="\int_{0}^{\infty} e^{-x^2} \, dx = \frac{\sqrt{\pi}}{2}" displayMode={true} />

      {/* Inline math */}
      <p>
        The quadratic formula is <LatexExpression expression="x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}" displayMode={false} />.
      </p>
    </div>
  );
}

3. useLatexEvaluation (Real-Time Evaluation Hook)

import React, { useState } from 'react';
import { LatexInput, useLatexEvaluation } from '@latex-math/react';

export function Calculator() {
  const [latex, setLatex] = useState('2x^2 + 3x - 5');

  const { result, error, isEvaluating } = useLatexEvaluation(latex, {
    variables: { x: 3 }
  });

  return (
    <div className="space-y-4">
      <LatexInput value={latex} onChange={setLatex} />

      {isEvaluating && <p>Evaluating...</p>}
      {error && <p className="text-red-500">Error: {error.message}</p>}
      {result !== null && (
        <div className="font-semibold text-lg">
          Result for x = 3: {result.toString()}
        </div>
      )}
    </div>
  );
}

Component API

<LatexInput />

| Prop | Type | Default | Description | |---|---|---|---| | value | string | (required) | Current LaTeX string value | | onChange | (value: string) => void | (required) | Callback fired when the expression changes | | variables | Record<string, number> | {} | Variable scope for live evaluation | | placeholder | string | "\text{Type math... }" | Placeholder displayed inside empty math field | | showToolbar | boolean | true | Displays quick math buttons (fractions, powers, roots, calculus, trig) | | showLatexBadge | boolean | true | Displays bottom bar showing the underlying LaTeX string and copy action | | showEvaluatedResult | boolean | true | Shows evaluated answer in the bottom-right corner of the input box | | showKeyboard | boolean | true | Controls whether the virtual keyboard toggle button is visible/active | | showMenu | boolean | true | Controls whether the MathLive contextual menu toggle button is visible | | fontsDirectory | string | CDN | Directory URL for MathLive fonts (defaults to official unpkg CDN) | | className | string | "" | Root container styling class | | inputWrapperClassName | string | "" | Math input box styling class (supports custom bg-*, border-*, text-*, rounded-*) | | mathFieldClassName | string | "" | Alias for inputWrapperClassName (combined onto the same math input box) | | mathFieldContainerClassName | string | "" | Backwards-compatible alias for inputWrapperClassName | | resultClassName | string | "" | Evaluated result badge styling (supports custom bg-*, text-*, border-*, and corner positioning) |

<LatexExpression />

| Prop | Type | Default | Description | |---|---|---|---| | expression | string | (required) | The LaTeX string to render | | displayMode | boolean | true | Whether to render as centered display mode (true) or inline (false) | | output | "html" | "htmlAndMathml" | "mathml" | "html" | Markup output format (defaults to "html" to avoid duplicate MathML rendering) | | className | string | "" | Additional CSS class names |

useLatexEvaluation(expression, options)

const { result, ast, error, isEvaluating } = useLatexEvaluation(expression, {
  variables: { x: 4, y: 10 }
});
  • expression: string - The LaTeX formula to evaluate.
  • options.variables: Record<string, number> - Optional variable values.
  • result: Computed number, matrix, or null.
  • ast: The parsed @latex-math/core AST node.
  • error: Evaluation or parsing error if invalid.

License

MIT © Sora1123