katex

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package katex renders TeX math to HTML and MathML with KaTeX, with no browser and no Node.js.

The package embeds the minified script of one KaTeX release and runs it in goja, a JavaScript engine in pure Go. Render returns the same markup that katex.renderToString returns in a browser. The markup has two parts: HTML that looks the same in each browser, and MathML for screen readers. Thus a page shows its math with JavaScript off.

The HTML part needs the style sheet and the fonts of the same release. Assets returns them. Serve them next to the pages, and link the style sheet from each page.

Run go generate to change the release. The generator checks the tarball from npm against its published integrity.

Index

Constants

View Source
const Timeout = 5 * time.Second

Timeout is the longest time that one call of Render can take.

View Source
const Version = "0.19.1"

Version is the KaTeX release that the package embeds.

Variables

This section is empty.

Functions

func Assets

func Assets() fs.FS

Assets returns the files that the HTML of Render needs: katex.min.css, the fonts in fonts/ and the licence of KaTeX.

func MathML

func MathML(out string) string

MathML returns the MathML element of a rendering of Render, for a browser that implements MathML Core. MathML Core draws mathvariant only as normal, so each letter and digit of a math alphabet, for example the R of \mathbb{R}, becomes its character in the Mathematical Alphanumeric Symbols block. A prime in a superscript, as in f', follows its base, because the prime glyph of a math font is already raised. Text in a math alphabet keeps its letters and gets the weight and the slant in a style. The result is "" when out has no MathML element.

Types

type Error

type Error struct {
	TeX     string // the formula
	Message string // the message of KaTeX
}

Error is a formula that KaTeX cannot parse.

func (*Error) Error

func (e *Error) Error() string

type Renderer

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

Renderer renders formulas with one set of macros. It keeps the result of each formula, so a formula that a site uses many times costs one render. Its methods are safe for concurrent use, but one Renderer renders one formula at a time.

func New

func New(macros map[string]string) (*Renderer, error)

New returns a Renderer with the macros. A key of macros is a control sequence such as \R, and its value is the expansion, with #1 for the first argument.

func (*Renderer) Render

func (r *Renderer) Render(tex string, display bool) (string, error)

Render returns the HTML and MathML of tex. display selects display math, else the formula is inline. The error is an *Error when KaTeX cannot parse tex.

Jump to

Keyboard shortcuts

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