tex

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

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

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 9 Imported by: 0

README

tex-go

A Go library provides Latex utilities like rendering LaTex templates and compiling a Tex file to a PDF

Documentation

Index

Examples

Constants

View Source
const (
	Ext = ".tex"
)

Variables

View Source
var (
	// Show xelatex output or not.
	DebugMode = false

	// Left delimiter for tex template(default: "\{\{").
	LeftDelimter = "\\{\\{"

	// Right delimiter for tex template(default: "\}\}").
	RightDelimter = "\\}\\}"
)

Functions

func LoadTemplates

func LoadTemplates(dir string) (map[string]*template.Template, error)
Example
// Dependency represents the dependency of tex-go.
type Dependency struct {
	Name string
	Desc string
	URL  string
}

// Manual represents the manual of tex-go.
type Manual struct {
	Title        string
	Author       string
	About        string
	Dependencies []Dependency
	ExampleCode  string
}

manual := Manual{
	Title:  "tex-go Manual",
	Author: "Frank Xu",
	About:  "A Go library provides Latex utilities like rendering LaTex templates and compiling a Tex file to a PDF.",
	Dependencies: []Dependency{
		Dependency{
			Name: "TexLive",
			Desc: "tex2pdf calls `xelatex` command which comes with installation of TexLive.",
			URL:  "https://tug.org/texlive/",
		},
		Dependency{
			Name: "minted",
			Desc: "minted is used for code highlighting. TexLive Installation with scheme-full includes minted.",
			URL:  "https://www.ctan.org/pkg/minted",
		},
		Dependency{
			Name: "pygments",
			Desc: "pygments is required by minted.",
			URL:  "https://www.ctan.org/pkg/minted",
		},
	},
	ExampleCode: `
		package main

		import (
                    "github.com/northbright/tex-go"
		)
		`,
}

srcDir := "templates"
dstDir := "out"

// Load templates.
m, err := tex.LoadTemplates(srcDir)
if err != nil {
	log.Printf("LoadTemplates() error: %v", err)
	return
}

// Output
if err = tex.Output(m, dstDir, manual); err != nil {
	log.Printf("Output() error: %v", err)
	return
}
Output:

func Output

func Output(m map[string]*template.Template, outputDir string, data any) error

func OutputTex

func OutputTex(t *template.Template, outputTex string, data any) error

func ToPDF

func ToPDF(texFile string) (string, error)

ToPDF compiles a tex file into the PDF file by running xelatex. It outputs the pdf under the source tex file's dir and returns the compiled PDF path.

Example
// Open DEBUG mode if need.
tex.DebugMode = true

texFile := "templates/manual.tex"

// Compile a tex file to PDF.
pdf, err := tex.ToPDF(texFile)
if err != nil {
	log.Printf("ToPDF() error: %v", err)
	return
}

fmt.Printf("ToPDF OK, output pdf: %v\n", filepath.Base(pdf))
Output:

ToPDF OK, output pdf: my_book.pdf

Types

This section is empty.

Jump to

Keyboard shortcuts

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