katex renderToString math: working versions (tested 2026-10-16)
Render inline and display math to HTML and MathML strings with katex, control invalid input via throwOnError and define reusable macros offline.
passed5 passed · 0 failed2026-10-16 20:33 UTC- Status
- passed
- Last passed
- 2026-10-16 20:33 UTC
- Tests
- 5 / 5
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
katex@0.19.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38962988948
The code
Exactly the files that passed. Your agent can pull them with get_recipe("katex-render-to-string") (install), or see them on GitHub.
package.json
{
"name": "katex-render-to-string",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"katex": "0.19.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/render.ts
import katex from "katex";
export type RenderOptions = {
displayMode?: boolean;
throwOnError?: boolean;
macros?: Record<string, string>;
};
export function renderMath(tex: string, options: RenderOptions = {}): string {
return katex.renderToString(tex, {
displayMode: options.displayMode ?? false,
throwOnError: options.throwOnError ?? true,
macros: options.macros ?? {},
});
}
export function isParseError(error: unknown): boolean {
return error instanceof katex.ParseError;
}
test/render.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import katex from "katex";
import { renderMath, isParseError } from "../src/render.ts";
test("renders inline math with katex classes and MathML", () => {
const html = renderMath("x^2");
assert.match(html, /class="katex"/);
assert.match(html, /<math[^>]*xmlns="http:\/\/www\.w3\.org\/1998\/Math\/MathML"/);
assert.match(html, /<annotation encoding="application\/x-tex">/);
assert.doesNotMatch(html, /katex-display/);
});
test("renders display math with katex-display wrapper", () => {
const html = renderMath("\\sum_{i=0}^{n} i", { displayMode: true });
assert.match(html, /class="katex-display"/);
assert.match(html, /class="katex"/);
assert.match(html, /<math[^>]*display="block"/);
});
test("throws a ParseError for invalid TeX when throwOnError is true", () => {
assert.throws(
() => renderMath("\\frac{"),
(error: unknown) => isParseError(error),
);
assert.throws(() => renderMath("\\undefinedmacro"), katex.ParseError);
});
test("renders an error span for invalid TeX when throwOnError is false", () => {
let html = "";
assert.doesNotThrow(() => {
html = renderMath("\\frac{", { throwOnError: false });
});
assert.match(html, /katex-error/);
assert.match(html, /#cc0000/);
});
test("applies custom macros across a render call", () => {
const html = renderMath("\\RR", { macros: { "\\RR": "\\mathbb{R}" } });
assert.match(html, /mathbb/);
assert.match(html, /<annotation encoding="application\/x-tex">\\RR<\/annotation>/);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.