Equation
Use Equation for a math formula written in TeX: a variable or short expression inside a sentence, or a formula on its own line in an explanation, a report or an answer from a model. To render a whole Markdown message whose math is written as $…$ and $$…$$, turn on mathExtension in Markdown and it uses Equation for you. For code, use Code.
- Rendering needs KaTeX, which Pika neither bundles nor depends on: install
katex, pass it toConfigProvider(katex={katex}) or to thekatexprop, and import its stylesheet once (import "katex/dist/katex.min.css"), which also loads its fonts. Without KaTeX,Equationshows the TeX source as code. - In Markdown, math is off until you add
mathExtensionfrom@pika-lab/react/markdowntoextensions.
Equation renders with KaTeX’s renderToString, so it renders on the server and in the first paint, and it outputs MathML beside the visual HTML for screen readers. Commands that would need KaTeX’s trust (links, classes, images) stay off.
#Setting up KaTeX
import "katex/dist/katex.min.css";
import katex from "katex";
import { ConfigProvider } from "@pika-lab/react";
export function App() {
return <ConfigProvider katex={katex}>{/* … */}</ConfigProvider>;
}#Demos
#Basic
Inline in a line of text, at the text’s size and color.
Mass and energy are related by E=mc2, and the roots of ax2+bx+c=0 are x=2a−b±b2−4ac.
#Display
display puts the formula on its own centered line. A formula wider than its container scrolls sideways.
#Errors and no KaTeX
A formula KaTeX cannot parse shows its source with an error border instead of breaking the page; its title is KaTeX’s message. Without KaTeX the source shows as code.
A formula that does not parse keeps its source: $\frac{1}{$. Hover it for KaTeX's message.
Without KaTeX the source shows as code: $a^2 + b^2$.
#In Markdown
With mathExtension, $…$ is an inline equation and a $$ block a display one. \$ writes a dollar sign. Without the extension a $ is plain text, so prices and shell variables are never read as math.
The softmax of a vector z is
and $5 stays a price.
#Props
| Name | Type | Default | Description | Version |
|---|---|---|---|---|
value | string | required | The formula in TeX, without $ delimiters. | Unreleased |
display | boolean | false | On its own centered line instead of inline. | Unreleased |
katex | EquationRenderer | undefined | KaTeX for this equation (import katex from "katex"). Defaults to ConfigProvider’s. | Unreleased |
Other props go to the root <span>. EquationRenderer is the part of KaTeX that Equation calls (renderToString); Pika only exports its type and never imports KaTeX at runtime.