katex

package module
v0.0.0-...-d0f6370 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 13 Imported by: 15 modules, 5 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.

README

Goldmark Katex

Go Reference

Goldmark Katex is a Goldmark extension providing math and equation support through KaTeX.

Usage

// Default, errors are highlighted in red in the output
goldmark.New(goldmark.WithExtensions(&katex.Extender{})).Convert(src, dst)

// With ThrowOnError set to true
goldmark.New(goldmark.WithExtensions(&katex.Extender{
    ThrowOnError: true,
})).Convert(src, dst)

Wrap inline math with a pair of single $:

$A$

Wrap block math with a pair of double $:

$$
A = \pi \times r^2
$$

A $$ that begins a line opens a block, which runs to the next $$ or to the end of the document, much as a fenced code block does. The equation is parsed before Markdown, so it is kept verbatim however long it is, and delimiters may hug it:

$$\begin{aligned}
a &= b \\
c &= d
\end{aligned}$$

Double $ in the middle of a line is display math too, rendered in place within the surrounding paragraph:

Einstein wrote $$E = mc^2$$ in 1905.

Example

Given the radius $r$ of a circle, the area $A$ is:

$$
A = \pi \times r^2
$$

And the circumference $C$ is:

$$
C = 2 \pi r
$$

More Goldmark Extensions

  • D2: diagram support through D2

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var KindBlock = ast.NewNodeKind("Block")
View Source
var KindInline = ast.NewNodeKind("Inline")

Functions

func Render

func Render(w io.Writer, src []byte, display bool, throwOnError bool) error
Example
b := bytes.Buffer{}
Render(&b, []byte(`Y = A \dot X^2 + B \dot X + C`), false, false)
fmt.Println(b.String())
Output:
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Y</mi><mo>=</mo><mi>A</mi><msup><mover accent="true"><mi>X</mi><mo>˙</mo></mover><mn>2</mn></msup><mo>+</mo><mi>B</mi><mover accent="true"><mi>X</mi><mo>˙</mo></mover><mo>+</mo><mi>C</mi></mrow><annotation encoding="application/x-tex">Y = A \dot X^2 + B \dot X + C</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="katex-base"><span class="katex-strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.2222em;">Y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="katex-base"><span class="katex-strut" style="height:1.0035em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">A</span><span class="mord"><span class="mord katex-accent"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.9202em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord mathnormal" style="margin-right:0.0785em;">X</span></span><span style="top:-3.2523em;"><span class="pstrut" style="height:3em;"></span><span class="accent-body" style="left:-0.0556em;"><span class="mord">˙</span></span></span></span></span></span></span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="katex-sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="katex-base"><span class="katex-strut" style="height:1.0035em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.0502em;">B</span><span class="mord katex-accent"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.9202em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord mathnormal" style="margin-right:0.0785em;">X</span></span><span style="top:-3.2523em;"><span class="pstrut" style="height:3em;"></span><span class="accent-body" style="left:-0.0556em;"><span class="mord">˙</span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="katex-base"><span class="katex-strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.0715em;">C</span></span></span></span>

Types

type Block

type Block struct {
	ast.BaseBlock
	// contains filtered or unexported fields
}

Block is display math occupying whole lines of its own ("$$...$$").

BlockParser builds it during block parsing, before Markdown ever looks at the equation. Parsing it inline instead let block constructs mangle multi-line equations first: a line followed by "=" became a Setext heading, and the equation was silently truncated at the heading. The equation body is held in Lines(), and IsRaw keeps Markdown away from it.

Display math written in the middle of a line stays inline math; see Parser.

func (*Block) Dump

func (n *Block) Dump(source []byte, level int)

func (*Block) Equation

func (n *Block) Equation(source []byte) []byte

Equation returns the raw LaTeX between the delimiters.

func (*Block) IsRaw

func (n *Block) IsRaw() bool

IsRaw keeps goldmark from running inline Markdown parsing over the equation, which would otherwise read "_" as emphasis, "*" as a list bullet, and so on.

func (*Block) Kind

func (n *Block) Kind() ast.NodeKind

type BlockParser

type BlockParser struct {
}

BlockParser parses display math ("$$...$$") that begins a line.

Running at the block level keeps the equation opaque to Markdown, so an equation of any length survives intact. Parsed inline it would first be cut up by block parsing, most visibly by a line followed by "=" becoming a Setext heading, which truncated the equation at that point.

A "$$" that begins a line opens a block, which runs to the next "$$" or to end of input, as a fenced code block does. Text before the opening delimiter or after the closing one is never consumed: display math written mid-line is left to the inline Parser, which renders it in place.

func (*BlockParser) CanAcceptIndentedLine

func (b *BlockParser) CanAcceptIndentedLine() bool

func (*BlockParser) CanInterruptParagraph

func (b *BlockParser) CanInterruptParagraph() bool

func (*BlockParser) Close

func (b *BlockParser) Close(node ast.Node, reader text.Reader, pc parser.Context)

func (*BlockParser) Continue

func (b *BlockParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State

func (*BlockParser) Open

func (b *BlockParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State)

func (*BlockParser) Trigger

func (b *BlockParser) Trigger() []byte

type Extender

type Extender struct {
	ThrowOnError bool
}

func (*Extender) Extend

func (e *Extender) Extend(m goldmark.Markdown)

type HTMLRenderer

type HTMLRenderer struct {
	html.Config
	// contains filtered or unexported fields
}

func (*HTMLRenderer) RegisterFuncs

func (r *HTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer)

type Inline

type Inline struct {
	ast.BaseInline

	Equation []byte

	// Display selects KaTeX display mode, set for "$$...$$".
	Display bool
}

Inline is math that sits within a line of text.

It covers inline math ("$...$") and display math written mid-line ("text $$x$$ more"), which stays an inline node so it nests validly inside the surrounding paragraph. Display math on a line of its own is a Block.

func (*Inline) Dump

func (n *Inline) Dump(source []byte, level int)

func (*Inline) Inline

func (n *Inline) Inline()

func (*Inline) IsBlank

func (n *Inline) IsBlank(source []byte) bool

func (*Inline) Kind

func (n *Inline) Kind() ast.NodeKind

type Parser

type Parser struct {
}

Parser is the inline parser for math delimited by dollar signs.

It handles inline math ("$...$") and display math written in the middle of a line ("text $$x$$ more"). Display math that begins a line is claimed earlier by BlockParser, so it never reaches this parser; what is left here is always confined to a single line.

func (*Parser) Parse

func (s *Parser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node

func (*Parser) Trigger

func (s *Parser) Trigger() []byte

Jump to

Keyboard shortcuts

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