LaTeX Math Formulas in React with Comark and KaTeX
A React and Vite app that renders inline and display LaTeX math in Markdown with the Comark math plugin and KaTeX.
This example renders inline and display LaTeX math expressions in React and Vite using @comark/react/plugins/math and KaTeX.
Features
- Parses
$...$inline formulas and$$...$$display blocks with themathplugin. - Renders parsed expressions through the
MathReact component and KaTeX stylesheet. - Supports fractions, integrals, sums, matrices, Greek letters, subscripts, and superscripts.
Usage
Install the renderer and
katexpeer dependency:npm install @comark/react katexImport the
mathplugin,Mathcomponent, and KaTeX stylesheet:import { MarkdownClient } from '@comark/react' import math, { Math } from '@comark/react/plugins/math' import 'katex/dist/katex.min.css'Pass the plugin and component to
MarkdownClient:<MarkdownClient value={content} components={{ Math }} plugins={[math()]} />Write math expressions in Markdown:
Inline: $E = mc^2$ Display: $$ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $$
Syntax examples
Inline math: wrap an expression in single
$delimiters:The formula $x^2 + y^2 = z^2$ is inline.Display math: wrap a block equation in
$$delimiters:$$ \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2} $$Fractions: use
\frac{numerator}{denominator}:$\frac{a}{b}$Greek letters: use commands such as
\alpha,\beta, and\gamma:$\alpha + \beta = \gamma$Subscripts and superscripts: use
_and^:$x_1^2 + x_2^2$