math

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

README

math — go-tex

License Go Coverage

A pure-Go (no cgo) TeX math-mode typesetter → SVG. It parses a subset of TeX math syntax and lays it out to a self-contained <svg> using the OpenType MATH table — via go-opentype for MATH metrics and vector glyph outlines — with no TeX engine, no server, and no cgo. It compiles to GOOS=js/GOARCH=wasm, so it renders math client-side and offline in a browser Web Worker.

This is the math-mode component of the go-tex pure-Go TeX effort; the full engine will live in go-tex/tex.

What it does

Typesets, using real OpenType MATH metrics (axis height, script shifts, fraction rule thickness, script scale-down, …):

  • variables as Unicode math-italic; atom-class spacing (TeX Appendix G: thin/medium/thick around operators, relations, punctuation);
  • superscripts (^), subscripts (_), and primes ('), with big-operator limits set above/below in display style;
  • fractions (\frac, \dfrac, \tfrac) with the rule on the math axis;
  • radicals\sqrt{…} and \sqrt[n]{…} with a stretchy radical sign;
  • stretchy delimiters\left( … \right), [ ], \{ \}, \langle, \lfloor, |, and null ., grown to the content via MATH size variants;
  • matricesmatrix, pmatrix, bmatrix, Bmatrix, vmatrix, Vmatrix, and cases;
  • accents\hat \bar \vec \tilde \dot \ddot \check \breve \acute \grave — plus \overline/\underline;
  • math alphabets\mathbb \mathcal \mathfrak \mathbf \mathsf \mathtt \mathit \mathrm (via Unicode math alphanumerics, hole-corrected) and \text;
  • ~200 named symbols — Greek, big operators, binary operators, relations, arrows, set/logic; spacing (\, \: \; \! \quad \qquad); and explicit \displaystyle/\textstyle.
\sum_{i=1}^{n} i^2      \sqrt[3]{\frac{a}{b}}      \left(\frac{x+1}{2}\right)^n
\begin{pmatrix} a & b \\ c & d \end{pmatrix}      \mathbb{R} \subset \mathbb{C}

Each renders to a crisp, resolution-independent SVG of positioned glyph paths (with <rect> rules for fractions, radicals and bars). Use RenderDisplaySVG for display style (larger operators, limits above/below).

Install

go get github.com/go-tex/math

Usage

package main

import (
	"fmt"

	texmath "github.com/go-tex/math"
)

func main() {
	// DefaultFont returns an embedded MATH font (STIX Two Math, OFL).
	r, err := texmath.New(texmath.DefaultFont())
	if err != nil {
		panic(err)
	}
	svg, err := r.RenderSVG(`\frac{x^2+1}{\alpha-\beta}`, 40) // 40px base size
	if err != nil {
		panic(err)
	}
	fmt.Println(svg) // <svg …>…</svg>
}

New accepts any OpenType font that carries a MATH table (STIX Two Math, Latin Modern Math, XITS Math, …); DefaultFont embeds STIX Two Math so the zero-config path — and the wasm worker — is self-contained.

WebAssembly

Being pure Go (CGO=0), it compiles to GOOS=js GOARCH=wasm. cmd/wasm is a worker that exposes globalThis.renderMathSVG(tex):

GOOS=js GOARCH=wasm go build -o texmath.wasm ./cmd/wasm

Measured (STIX Two Math embedded): ~1.6 MB gzip worker (most of it the font, which is subsettable), ~0.1–0.3 ms per formula — fast enough to re-render on every keystroke.

Scope — what this is, and is not

This is a math-mode typesetter for preview, not a TeX engine and not a replacement for a TeX distribution (TeXLive). It renders TeX math to SVG; it does not process LaTeX documents — there is no macro expander, no page/line breaking, no packages, no fonts-beyond-the-one-you-pass, and no PDF/DVI output. Full LaTeX is the separate, staged go-tex/tex engine effort.

Within math mode it is broad (see the feature list above). Known limitations: stretchy glyphs use MATH size variants only (not yet the assembly recipe, so delimiters taller than the largest variant stop growing); accent positioning is approximate; and there is no \substack, alignment (aligned/&-columns beyond matrices), or user-defined macros. The symbol table is easy to extend.

Tests

Statement coverage is held at 100% (parser, layout, and error paths), go vet clean, and green across the six 64-bit Go targets plus js/wasm and wasip1/wasm.

go test ./...

Fonts

DefaultFont embeds STIX Two Math, licensed under the SIL Open Font License 1.1 — see STIXTwoMath-OFL.txt. The test-only testdata/nomath.otf is Source Serif 4 (also OFL).

License

BSD-3-Clause — see LICENSE. Copyright the go-tex/math authors. (The embedded font is under its own OFL license, above.)

Documentation

Overview

Package math is a pure-Go TeX math-mode typesetter: it parses a broad subset of TeX math syntax and lays it out to a self-contained SVG using the OpenType MATH table (via go-opentype) for metrics and vector glyph outlines — no TeX engine, no server, no cgo. It compiles to GOOS=js/wasm for offline math preview.

Supported: math-italic variables and a large named-symbol table with proper atom-class spacing (Appendix G); superscripts/subscripts and big-operator limits; fractions; radicals (\sqrt, \sqrt[n]); stretchy delimiters (\left…\right); accents (\hat, \vec, \bar, …); \overline/\underline; math alphabets (\mathbb, \mathcal, \mathfrak, \mathrm, \mathbf, \mathsf, \mathtt, \mathit); \text; matrices (matrix/pmatrix/bmatrix/vmatrix/Vmatrix/cases), array (with an l/c/r + | column spec), aligned/split, gathered and smallmatrix; primes; spacing commands; \displaystyle/\textstyle; declarative in-math font switches (\rm \bf \it \sf \tt \cal \sl); named operators (\log, \sin, \lim, …) and \operatorname / \operatorname*; and the modular annotations \bmod, \pmod, \mod and \pod.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFont

func DefaultFont() []byte

DefaultFont returns an embedded MATH font (STIX Two Math, OFL).

Types

type Metrics added in v0.5.0

type Metrics struct {
	Width, Height, Depth float64
}

Metrics describes a rendered math box in pixels. Width is the advance width; Height the extent above the baseline; Depth the extent below it. In the SVG returned alongside these metrics the baseline sits at y=Height, and the box spans [0,Width]×[0,Height+Depth] with no padding — so a caller can align the math baseline with its surrounding text baseline exactly (place the SVG's top edge Height above the baseline), which RenderSVG's padded, baseline-agnostic output does not permit.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer typesets TeX math with a single MATH-table font.

func New

func New(fontBytes []byte) (*Renderer, error)

New builds a Renderer from an OpenType font carrying a MATH table.

func (*Renderer) RenderDisplaySVG added in v0.2.0

func (r *Renderer) RenderDisplaySVG(tex string, sizePx int) (string, error)

RenderDisplaySVG is like RenderSVG but in display style (larger operators, limits set above/below).

func (*Renderer) RenderDisplaySVGMetrics added in v0.5.0

func (r *Renderer) RenderDisplaySVGMetrics(tex string, sizePx int) (string, Metrics, error)

RenderDisplaySVGMetrics is RenderSVGMetrics in display style.

func (*Renderer) RenderSVG

func (r *Renderer) RenderSVG(tex string, sizePx int) (string, error)

RenderSVG typesets tex at the given base pixel size (inline/text style) and returns a complete, self-contained <svg> document.

func (*Renderer) RenderSVGMetrics added in v0.5.0

func (r *Renderer) RenderSVGMetrics(tex string, sizePx int) (string, Metrics, error)

RenderSVGMetrics typesets tex at inline/text style like RenderSVG, but returns a tightly-cropped SVG together with its exact box Metrics (advance width, height above the baseline, depth below). This is the baseline-aware form: the caller can place the box so the math baseline meets the text baseline instead of guessing from the overall height.

Directories

Path Synopsis
cmd
wasm command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL