mathtex

package
v1.0.1-0...-517973f Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MPL-2.0 Imports: 24 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mathtex typesets LaTeX math for Kvit Notes: the formulas of display math blocks ($$…$$) and of inline spans ($…$).

The typesetting is done by MicroTeX, a C++ engine, built as a shared library (kvitmath.dll, libkvitmath.dylib, libkvitmath.so, from third_party/microtex and mathtex/native by tools/build-mathlib.sh) that this package loads at run time and calls without cgo, through purego on Linux and macOS and through the system's DLL loader on Windows. The library lays a formula out and records what drawing it would do: which character of which font file goes where, and the lines and rectangles. This package draws those on a unison canvas with the same font files. When the library or its resources are not found, math is off: Available is false, LoadError says why, and the editor shows the TeX source.

MicroTeX is not thread-safe; every call into it holds one mutex. Rendered formulas are cached by TeX, size and style.

Index

Constants

View Source
const (
	// MaxTeXChars is the longest TeX source laid out, in UTF-16 code units.
	// Real formulas are a line or two.
	MaxTeXChars = 8192
	// MaxTextSize is the largest size a formula is set at, in pixels per em.
	MaxTextSize = 512
	// MaxDevicePixelRatio, MaxRasterPixels and MaxRasterEdge bound the
	// images Image makes.
	MaxDevicePixelRatio = 8.0
	MaxRasterPixels     = 64 * 1024 * 1024
	MaxRasterEdge       = 32768
)

Ceilings on what a formula can ask for. A note is untrusted input, and its TeX feeds sizes straight into layout and into rasters.

Variables

This section is empty.

Functions

func Available

func Available() bool

Available reports whether formulas can be rendered: the library and its resources were found and the engine initialised. When they were not, math is off, the editor shows the TeX source, and LoadError says why.

func Commands

func Commands() []string

Commands is every command name a user can type, from MicroTeX's tables of symbols and macros (the \newcommand definitions and the NewTX additions included), without the backslash, sorted: what the math-command menu completes from. Internal names, those holding '@', are left out. It is nil while math is off.

func ErrorFor

func ErrorFor(tex string) string

ErrorFor is "" when tex renders, else why not: what the block shows beside the source instead of the formula. Blank TeX is not an error; it renders nothing.

func Image

func Image(tex string, sizePx int, fg unison.Color, dpr float64, vpad, hpad int, display bool) (*image.NRGBA, error)

Image draws tex into a new image: sizePx pixels per em, in fg, dpr device pixels per logical pixel, with vpad transparent logical pixels above and below and hpad left and right (SideBearingPadding says how much keeps overhanging glyphs inside). The formula is placed hpad from the left edge, so a caller that lines it up with a box shifts it left by hpad.

Blank TeX gives no image and no error. The ratio is capped at MaxDevicePixelRatio and lowered further until the image fits in MaxRasterEdge pixels a side and MaxRasterPixels in all, because the size, the ratio and the formula's length all multiply into the image and a note sets them.

func LibraryPath

func LibraryPath() string

LibraryPath is the library file in use, or "" when math is off.

func LoadError

func LoadError() error

LoadError is why math is off, or nil when it is available.

func OpticalMathSize

func OpticalMathSize(textSizePx int, textXHeightPx float64) int

OpticalMathSize is the size, in pixels, to set math at beside text of textSizePx whose font has an x-height of textXHeightPx, so both look the same size: the math font's x-height (Charter math 0.442 em, Computer Modern 0.4305 em, well below an interface sans-serif's 0.5 em) is matched to the text's, as LaTeX's mathscale does when math is paired with an unrelated text face. It never shrinks the math below the text size and enlarges it at most 1.5 times. TextXHeight measures textXHeightPx.

func ResourceRoot

func ResourceRoot() string

ResourceRoot is the resource directory the engine was initialised with: the fonts and the XML files MicroTeX reads. It is "" when none was found.

func SelfTest

func SelfTest(w io.Writer) int

SelfTest is the check a package's build runs on the layout it made, with `kvit-notes --math-selftest`: it finds the library and the resources, draws one formula into an image, and writes where they were found and the outcome to w:

math-lib: <library file>
math-res: <resource directory>
selftest: OK (<width>x<height>)

or "selftest: FAIL (<why>)". KVIT_MATH_SELFTEST_TEX replaces the formula, to check a particular alphabet. It returns the exit code: 0 when the formula was drawn. Run it from outside the checkout, so the repository's own library cannot stand in for a missing one.

func SideBearingPadding

func SideBearingPadding(textSizePx int) int

SideBearingPadding is the transparent margin, in logical pixels, a formula drawn at textSizePx needs left and right so no glyph is cut off at its own edges.

MicroTeX sizes a formula to its typographic advance width, which is not the area its glyphs cover: the italic f carries its tail left of the origin and its hook right of the advance, and other letters overhang by less. Measured across the NewTX/XCharter reference formulas at 15, 17, 20 and 32 px, the ink reaches at most 0.067 of the text size left of the box (x^2) and 0.200 right of it (the script-size f in A_f). The margin is a little above the worst case on both sides and scales with the size.

func TextXHeight

func TextXHeight(fonts *text.Fonts, st text.Style) float64

TextXHeight is the x-height, in pixels, of text in style st with fonts: the height of the ink of a lowercase x in its family, weight and slant at its size, which OpticalMathSize matches the math to, rounded up to a whole pixel. The rounding keeps the math at the sizes the earlier Qt version of Kvit Notes set it at, which read the x-height in whole pixels: beside 15 px DejaVu Sans, whose x is 8.2 px tall, display math is set at 20 px, which is what a 9 px x-height gives and 8.2 px does not.

The height per em is measured once per face by drawing an x large, in the style's colour, and finding its ink; the drawing is never shown, but the colour has to be opaque for the x to leave ink. The result is 0 when the measuring fails.

func UseFonts

func UseFonts(fonts *text.Fonts)

UseFonts gives the fonts text MicroTeX has no math font for is measured and drawn with: the app's own, which fall back through the system's fonts for any script. Until it is called such text is laid out from an estimate and not drawn. Formulas rendered before with other fonts are forgotten, since their layout may change; giving the same fonts again changes nothing, so every editor an app opens can call it.

Types

type Formula

type Formula struct {
	Metrics
	// TeX, Size and Display are what it was rendered from.
	TeX     string
	Size    int
	Display bool
	// contains filtered or unexported fields
}

Formula is a laid-out formula, ready to draw.

func Render

func Render(tex string, sizePx int, display bool) (*Formula, error)

Render lays tex out at sizePx pixels per em, in display style (a $$…$$ block: limits over and under large operators, full-size fractions) or in text style (a $…$ span: limits beside the operator, a formula that fits a line of text). The result is cached. Blank TeX renders as an empty formula with no error; TeX that does not parse, or is longer than MaxTeXChars, returns the error MicroTeX gives, and so does every call while math is off.

func (*Formula) Draw

func (f *Formula) Draw(gc *unison.Canvas, x, y float32, fg unison.Color)

Draw draws the formula with the top-left corner of its box at (x, y); its baseline is Baseline below y. Everything drawn in the default colour takes fg, and colours the TeX sets with \color are kept. Text the math fonts do not cover is drawn with the fonts given to UseFonts, and left out without them.

type Metrics

type Metrics struct {
	// Width and Height are the formula's box, rounded up to whole pixels so
	// an image of that size holds all of it.
	Width, Height float64
	// Baseline is the distance from the top of the box down to the line the
	// formula stands on, unrounded: drawing puts the baseline exactly there.
	Baseline float64
	// Ascent is Baseline, and Descent the rest of Height below it.
	Ascent, Descent float64
	// Depth is how far the formula reaches below its baseline.
	Depth float64
	// Valid is false when the TeX does not parse; Error then says why.
	Valid bool
	Error string
}

Metrics is a formula's layout, in logical pixels.

func Measure

func Measure(tex string, sizePx int, display bool) Metrics

Measure is a formula's layout at sizePx in display or text style, as Render makes it. Blank TeX is valid with zero size; TeX that does not parse is not valid, with the error.

Jump to

Keyboard shortcuts

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