Version: dev

to_latex

Stable function · Display

Tier: stable · Kind: function · Topic: Display

JuliaTypeScriptPythonRustGo
to_latextoLatexto_latexto_latexToLatex

Julia

to_latex(target)
to_latex(::Nothing)
to_latex(target::Real)
to_latex(target::String)
to_latex(target::ASTExpr)
to_latex(target::Equation)
to_latex(target::Model)
to_latex(target::ReactionSystem)
to_latex(target::EsmFile)
to_latex(target) -> String

Format target as LaTeX mathematical notation (\frac{\partial N}{\partial t} = -\lambda \cdot N). Accepts the same domain as to_unicode and to_ascii , with the same handling of bare names, non-finite numbers and containers (a container renders as its plain-text summary). Any other type throws an ArgumentError.

Examples

to_latex(parse_equation("D(N) = -lambda * N"))
# "\frac{\partial N}{\partial t} = -\lambda \cdot N"

Source

Example (display)

decay_eq = model.equations[1]
println(to_unicode(decay_eq))
@assert to_unicode(decay_eq) == "∂N/∂t = (−λ)·N"
@assert to_latex(decay_eq) == raw"\frac{\partial N}{\partial t} = (-\lambda) \cdot N"
@assert to_ascii(decay_eq) == "D(N)/Dt = (-lambda) * N"

# Expressions display the same way.
@assert to_unicode(model.equations[2].rhs) == "ln(2)/λ"

From docs/api/examples/julia/documents.jl

TypeScript

function toLatex(expr: Expr | Equation | Model | ReactionSystem | Reaction | EsmFile): string

Format an expression as LaTeX mathematical notation

Source

Example (display)

const model = loadPath(fixture('decay.esm')).models!.Decay
const [decay, halfLife] = model.equations

assert.equal(toUnicode(decay), '∂N/∂t = (−λ)·N')
assert.equal(toLatex(decay), '\\frac{\\partial N}{\\partial t} = (-\\lambda) \\cdot N')
assert.equal(toAscii(decay), 'D(N)/Dt = (-lambda) * N')

// A single expression renders the same way.
assert.equal(toUnicode(halfLife.rhs), 'ln(2)/λ')

// So does a whole model: a summary line followed by its equations.
assert.equal(toUnicode(model).split('\n').length, 3)

From docs/api/examples/typescript/display.ts

Python

to_latex(target: Expr | Equation | Model | ReactionSystem | EsmFile) -> str

Format target as LaTeX mathematical notation.

Parameters

  • target (Expr | Equation | Model | ReactionSystem | EsmFile) — Expression, equation, model, reaction system, or ESM file to format

Returns

  • str — LaTeX string representation

Source

Example (display)

decay_eq, half_life_eq = model.equations
print(earthsci_ast.to_unicode(decay_eq))
print(earthsci_ast.to_latex(decay_eq))
print(earthsci_ast.to_ascii(half_life_eq))
assert earthsci_ast.to_unicode(decay_eq) == "∂N/∂t = (−λ)·N"
assert earthsci_ast.to_latex(decay_eq) == r"\frac{\partial N}{\partial t} = (-\lambda) \cdot N"
assert earthsci_ast.to_ascii(half_life_eq) == "half_life = log(2) / lambda"

# A whole model renders as a one-line summary.
assert earthsci_ast.to_unicode(model) == "Model: Decay (1 state, 1 parameter, 1 observed, 2 equations)"

From docs/api/examples/python/documents.py

Rust

pub fn to_latex<T: TextRenderable + ?Sized>(target: &T) -> String

Render target as LaTeX. See to_unicode for the accepted domain.

Source · Rust docs

Example (display)

let doc = load_path(fixture("decay.esm"))?;
let model = &doc.models.as_ref().unwrap()["Decay"];
let decay = &model.equations[0];

let unicode = to_unicode(decay);
let latex = to_latex(decay);
let ascii = to_ascii(decay);
println!("{unicode}\n{latex}\n{ascii}");
assert_eq!(ascii, "D(N)/Dt = (-lambda) * N");
assert!(unicode.contains('λ'));
assert!(latex.contains("\\lambda"));

// The same functions take a single expression, or a whole model (which
// renders a summary).
assert_eq!(to_ascii(&model.equations[1].rhs), "log(2) / lambda");
assert!(to_unicode(model).contains("half_life"));

From docs/api/examples/rust/display.rs

Go

func ToLatex(target Expression) string

ToLatex converts an expression, equation or container to a LaTeX string with chemical subscripts and operator precedence.

Source · Go docs

Example (display)

doc, err := esm.LoadPath(fixture("decay.esm"))
if err != nil {
	log.Fatal(err)
}
model := doc.Models["Decay"]
decay := model.Equations[0]
fmt.Println(esm.ToUnicode(decay))
fmt.Println(esm.ToLatex(decay))
fmt.Println(esm.ToASCII(decay))

// A single expression renders the same way.
fmt.Println(esm.ToUnicode(model.Equations[1].RHS))

From docs/api/examples/go/display_test.go