latex

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 24 Imported by: 1 module, 1 package

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 latex opens a LaTeX or a plain TeX document and puts it on pages (D62). It runs a copy of the TeX engine of github.com/go-tex/engine, in latex/internal/engine, and draws its pages onto the display list of ivy (D10).

A document reads other files by their names: its class, its packages, the files of \input and its figures. The package reads them from two places. WithDir gives the folder of the document, and the field TeX of ivy.Config gives a tree of TeX files, such as the classes and the packages of a TeX distribution. The module of ivy holds no file of LaTeX, so a document whose class neither place holds is set without it. The package reads no file of the disk and no variable of the environment, because a document is input from a stranger (D5).

The engine sets the text in Latin Modern and the math in Latin Modern Math, as TeX does, when the field Fonts or the field TeX of ivy.Config holds them, such as fonts.TeX of the module github.com/xo/ivy/fonts or the tree of a TeX distribution. Without them, it sets the text in the Go fonts and the math in STIX Two Math.

The engine runs the whole document when a program first asks for its pages, because TeX numbers the pages and resolves the references of the whole document. The context stops the run. A command that the engine does not know is skipped, and the page is drawn without it.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Format = ivy.Format{
	Name:  "latex",
	Mimes: []string{"text/x-tex", "application/x-tex", "text/x-latex", "application/x-latex"},
	Exts:  []string{"tex", "latex", "ltx"},
	Open: func(ctx context.Context, src ivy.Source, cfg ivy.Config) (ivy.Document, error) {
		return Open(ctx, src, WithConfig(cfg))
	},
}

Format describes the package for the registry of a program.

Functions

This section is empty.

Types

type Document

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

Document is an open LaTeX document.

func Open

func Open(ctx context.Context, src ivy.Source, opts ...Option) (*Document, error)

Open opens a TeX document. It reads the whole source into memory. It returns an error that wraps ivy.ErrFormat if src is not text, and one that wraps ivy.ErrLimit if src is larger than the memory of the limits. The engine runs later, when a program asks for the pages.

func (*Document) Close

func (d *Document) Close() error

Close satisfies the ivy.Document interface.

func (*Document) Describe

func (d *Document) Describe(ctx context.Context) (ivy.Info, error)

Describe satisfies the ivy.Describer interface. It gives the title and the authors of \hypersetup, or of \title and \author. It runs the engine.

Example

A program reads the title and the authors that \title and \author give, without drawing the pages.

package main

import (
	"bytes"
	"context"
	"fmt"
	"os"
	"path/filepath"

	"github.com/xo/ivy"
	"github.com/xo/ivy/latex"
)

func main() {
	ctx := context.Background()
	dir := filepath.Join("testdata", "report")
	buf, err := os.ReadFile(filepath.Join(dir, "report.tex"))
	if err != nil {
		fmt.Println(err)
		return
	}
	cfg := ivy.Config{TeX: os.DirFS(filepath.Join("..", "fonts"))}
	d, err := latex.Open(ctx, bytes.NewReader(buf), latex.WithConfig(cfg), latex.WithDir(os.DirFS(dir)), latex.WithName("report"))
	if err != nil {
		fmt.Println(err)
		return
	}
	defer d.Close()
	info, err := d.Describe(ctx)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%q %q\n", info.Title, info.Authors)
}
Output:
"A Report on the Rendering of Documents" ["The Test Team"]

func (*Document) Page

func (d *Document) Page(ctx context.Context, n int) (ivy.Page, error)

Page satisfies the ivy.Pager interface. It runs the engine.

func (*Document) PageCount

func (d *Document) PageCount(ctx context.Context) (int, error)

PageCount satisfies the ivy.Pager interface. It runs the engine.

type Option

type Option func(*options)

Option is an option of Open.

func WithConfig

func WithConfig(cfg ivy.Config) Option

WithConfig sets the configuration that every format shares.

func WithDir

func WithDir(dir fs.FS) Option

WithDir gives the folder of the document. The document reads the files that it names, such as \input{chapter1} and \includegraphics{fig}, from dir, by their paths relative to the folder. dir must be safe for concurrent use. Without it, the document reads only the files of the field TeX of ivy.Config.

func WithName

func WithName(name string) Option

WithName gives the job name of the document: the name of its input file without the extension, such as "report" for report.tex. The engine reads the files of the run that carry the job name, such as report.bbl, from the folder of WithDir. Without it, the job name is "texput", as in TeX.

Example

A program sets a LaTeX document that cites a bibliography. WithDir gives the folder of the document, where the engine finds its figures and its report.bbl, and WithName gives the job name, so that the engine reads report.bbl and not texput.bbl. The field TeX of ivy.Config gives the classes, the packages and the fonts of TeX, such as fonts.TeX of the module github.com/xo/ivy/fonts.

package main

import (
	"bytes"
	"context"
	"fmt"
	"os"
	"path/filepath"

	"github.com/xo/ivy"
	"github.com/xo/ivy/latex"
)

func main() {
	ctx := context.Background()
	dir := filepath.Join("testdata", "report")
	buf, err := os.ReadFile(filepath.Join(dir, "report.tex"))
	if err != nil {
		fmt.Println(err)
		return
	}
	cfg := ivy.Config{TeX: os.DirFS(filepath.Join("..", "fonts"))}
	d, err := latex.Open(ctx, bytes.NewReader(buf),
		latex.WithConfig(cfg),
		latex.WithDir(os.DirFS(dir)),
		latex.WithName("report"),
	)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer d.Close()
	n, err := d.PageCount(ctx)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(n > 0)
}
Output:
true

type Page

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

Page is a page of a LaTeX document.

func (*Page) Draw

func (p *Page) Draw(ctx context.Context, d display.Device, res float32) error

Draw satisfies the display.Drawer interface (D58). A figure that fails to draw is left out, and Draw returns an *ivy.PartialError.

func (*Page) Render

func (p *Page) Render(ctx context.Context, dst *okage.Pixmap, t okage.Transform) error

Render satisfies the ivy.Page interface.

func (*Page) Size

func (p *Page) Size() (width, height float32)

Size satisfies the ivy.Page interface. It is the paper of the class, or US Letter for a document of plain TeX, as in pdfTeX. A LaTeX document that names no paper and has no class takes the text of the page with a margin of one inch.

Directories

Path Synopsis
internal
engine
Package engine is the core of a pure-Go (CGO=0) TeX engine: a faithful re-implementation of TeX's mouth and gullet — category-code tokenization, the equivalents table (eqtb) with grouping/scoping, macro definition with delimited parameters, and the expansion machinery (\def, \edef, \let, \expandafter, \csname, \noexpand, \string, \the, \number, conditionals, integer registers).
Package engine is the core of a pure-Go (CGO=0) TeX engine: a faithful re-implementation of TeX's mouth and gullet — category-code tokenization, the equivalents table (eqtb) with grouping/scoping, macro definition with delimited parameters, and the expansion machinery (\def, \edef, \let, \expandafter, \csname, \noexpand, \string, \the, \number, conditionals, integer registers).
mathsvg
Package mathsvg gives the engine of LaTeX the part of the API of github.com/go-tex/math that it calls, on the layout of math of okage, texmath (okage D78).
Package mathsvg gives the engine of LaTeX the part of the API of github.com/go-tex/math that it calls, on the layout of math of okage, texmath (okage D78).
nofs
Package nofs gives the engine of LaTeX the part of the API of the package os that it calls, without the file system and without the environment (D62).
Package nofs gives the engine of LaTeX the part of the API of the package os that it calls, without the file system and without the environment (D62).
opentype
Package opentype gives the engine of LaTeX the part of the API of github.com/go-opentype/opentype that it calls, on the fonts of okage (D62).
Package opentype gives the engine of LaTeX the part of the API of github.com/go-opentype/opentype that it calls, on the fonts of okage (D62).

Jump to

Keyboard shortcuts

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