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
- func DefaultFont() []byte
- type Metrics
- type Renderer
- func (r *Renderer) RenderDisplaySVG(tex string, sizePx int) (string, error)
- func (r *Renderer) RenderDisplaySVGMetrics(tex string, sizePx int) (string, Metrics, error)
- func (r *Renderer) RenderDisplaySVGMetricsSizes(tex string, s Sizes) (string, Metrics, error)
- func (r *Renderer) RenderSVG(tex string, sizePx int) (string, error)
- func (r *Renderer) RenderSVGMetrics(tex string, sizePx int) (string, Metrics, error)
- func (r *Renderer) RenderSVGMetricsSizes(tex string, s Sizes) (string, Metrics, error)
- func (r *Renderer) WithScriptSpace(ss float64) *Renderer
- type Sizes
Constants ¶
const DefaultScriptSpace = 0.5
DefaultScriptSpace is \scriptspace in plain TeX and LaTeX: 0.5pt.
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 (*Renderer) RenderDisplaySVG ¶ added in v0.2.0
RenderDisplaySVG is like RenderSVG but in display style (larger operators, limits set above/below).
func (*Renderer) RenderDisplaySVGMetrics ¶ added in v0.5.0
RenderDisplaySVGMetrics is RenderSVGMetrics in display style.
func (*Renderer) RenderDisplaySVGMetricsSizes ¶ added in v0.67.0
RenderDisplaySVGMetricsSizes is RenderDisplaySVGMetrics with an explicit size triple.
func (*Renderer) RenderSVG ¶
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
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.
func (*Renderer) RenderSVGMetricsSizes ¶ added in v0.67.0
RenderSVGMetricsSizes is RenderSVGMetrics with an explicit size triple.
func (*Renderer) WithScriptSpace ¶ added in v0.61.0
WithScriptSpace returns a Renderer that adds ss after every subscript and superscript box (tex.web §757-§759, make_scripts), in the unit of sizePx — points when sizePx is the point size, as the engine passes it. \scriptspace=0pt is 0. The copy shares the receiver's glyph cache, so it costs nothing to make per document; the receiver is unchanged.
type Sizes ¶ added in v0.67.0
type Sizes struct {
Text, Script, ScriptScript float64
}
Sizes is the explicit set of math sizes of one formula, in the unit RenderSVG's sizePx has (points when sizePx is the point size). In TeX these are the sizes of \textfont, \scriptfont and \scriptscriptfont (tex.web §699-§703), which LaTeX sets per class size with \DeclareMathSizes (fontmath.ltx): 10/7/5 at 10pt, 10.95/8/6 at 11pt, 12/8/6 at 12pt.
Text may be fractional. A zero Script or ScriptScript is derived from Text with the font's MATH constants ScriptPercentScaleDown and ScriptScriptPercentScaleDown, which is what RenderSVG has always done, so Sizes{Text: n} sets what RenderSVG(n) sets.