@vectojs/tex
v0.1.5
Published
Zero-DOM TeX math typesetting for VectoJS: a vendored KaTeX parse/layout kernel plus a self-contained SVG emit layer.
Maintainers
Readme
@vectojs/tex
@vectojs/tex is the Zero-DOM TeX math typesetting engine of the VectoJS graph: a vendored KaTeX
parse/layout kernel, mechanically stripped of MathML and DOM emission, plus a self-contained SVG
emit layer. It is a leaf package with no dependencies and exists for one constraint: canvas
rasterization goes data URI -> Image -> createImageBitmap -> drawImage, so the output must carry
its own glyph outlines and reference no external font or CSS — neither KaTeX's HTML/CSS output nor
any webfont approach can survive that pipeline. @vectojs/markdown loads it on demand to typeset
$...$, $$...$$, and ```math formulas.
Install
bun add @vectojs/texUsage
import { emitSVG, layout } from '@vectojs/tex';
// Parse + lay out TeX into a box tree (strictness violations never throw).
const tree = layout('x^2 + y^2 = z^2', { displayMode: true });
// Emit a self-contained SVG string plus its em-unit metrics.
const { svg, width, height, depth } = emitSVG(tree, { emPx: 16, color: '#e2e8f0' });
console.log(svg.slice(0, 80), width, height, depth);
// svg is a complete <svg> document with glyph paths inline — rasterizable
// through a data URI, safe to hand to an offscreen drawImage.Highlights
- Vendored KaTeX kernel (
src/kernel/, MIT): copied from a pinned commit byscripts/vendor-katex.tsand token-level stripped of MathML/DOM emission so it stays diffable against upstream; only the registry files are hand-written. - Self-contained SVG emit (
emitSVG): returns{ svg, width, height, depth }in em units with every glyph outline inlined — no webfonts, no external references, inherits nothing from page CSS. - Tolerant layout:
layout(tex, { displayMode?, maxSize?, minRuleThickness?, maxExpand? })renders questionable input instead of throwing for strictness violations, matching how a live editor needs typesetting to behave. - Bounded glyph corpus: fonts resolve through
resolveFont/shippedFonts, coverage is queryable viashippedGlyphCount()andgetGlyph, and sizing scales throughKATEX_FONT_SCALEandsizingRatio. - Consumer-facing lazy load lives in
@vectojs/markdown: the engine imports dynamically on the first formula (withpreloadMathJax()/isMathJaxReady()controls), keeping prose-only bundles at their no-math floor.
Documents @vectojs/[email protected].
