Skip to content

math_spec.typesetting.typst

Typst. Multi-letter identifiers in math are upright by default, which is why names go through italic("…").

TypstFormat #

See :class:math_spec.typesetting.format.Format.

The cyclic operators spell with .o, Typst's circled modifier; minus.circle does not compile.

cases_row = ', ' class-attribute #

dash = '---' class-attribute #

notation = 'typst' class-attribute #

operators = {name: typst for name, (_, typst) in OPERATOR_SPELLINGS.items()} class-attribute #

apply(function, argument) #

Source code in src/math_spec/typesetting/typst.py
def apply(self, function: str, argument: str) -> str:
    return f'{function}({argument})'

cardinality(inner) #

Source code in src/math_spec/typesetting/typst.py
def cardinality(self, inner: str) -> str:
    return f'abs({inner})'

cases(arms) #

Source code in src/math_spec/typesetting/typst.py
def cases(self, arms: list[tuple[str, str]]) -> str:
    return 'cases({})'.format(self.cases_row.join(f'{value} & {condition}' for value, condition in arms))

document(blocks, *, standalone) #

No preamble/body split: standalone only adds the page setup.

Source code in src/math_spec/typesetting/typst.py
def document(self, blocks: list[str], *, standalone: bool) -> str:
    """No preamble/body split: ``standalone`` only adds the page setup."""
    body = paragraphs(blocks)
    return f'{_PREAMBLE}\n{body}' if standalone else body

equations(lines, *, numbered) #

A block equation, aligned on & as amsmath does.

Source code in src/math_spec/typesetting/typst.py
def equations(self, lines: list[Line], *, numbered: bool) -> str:
    """A block equation, aligned on ``&`` as amsmath does."""
    body = ' \\\n  '.join(aligned_rows(lines, self, gap=' & '))
    numbering = '#set math.equation(numbering: "(1)")\n' if numbered else ''
    return f'{numbering}$ {body} $'

escape(prose) #

Source code in src/math_spec/typesetting/typst.py
def escape(self, prose: str) -> str:
    return _escape(prose)

fraction(numerator, denominator) #

Source code in src/math_spec/typesetting/typst.py
def fraction(self, numerator: str, denominator: str) -> str:
    return f'frac({numerator}, {denominator})'

glossary(title, entries) #

Source code in src/math_spec/typesetting/typst.py
def glossary(self, title: str, entries: list[Entry]) -> str:
    rows = '\n'.join(f'/ {self.math(e.symbol)}: {e.meaning}' for e in entries)
    return f'== {title}\n{rows}'

greek(name) #

Source code in src/math_spec/typesetting/typst.py
def greek(self, name: str) -> str:
    return name

italic(name) #

Source code in src/math_spec/typesetting/typst.py
def italic(self, name: str) -> str:
    return f'italic({_quote(name)})'

joined(parts, operator) #

Source code in src/math_spec/typesetting/typst.py
def joined(self, parts: list[str], operator: str) -> str:
    return f' {operator} '.join(parts) if operator else ', '.join(parts)

math(expression) #

Source code in src/math_spec/typesetting/typst.py
def math(self, expression: str) -> str:
    return f'${expression}$'

mono(text) #

Source code in src/math_spec/typesetting/typst.py
def mono(self, text: str) -> str:
    return _raw(text)

note(text) #

Source code in src/math_spec/typesetting/typst.py
def note(self, text: str) -> str:
    return text

parenthesise(inner) #

Source code in src/math_spec/typesetting/typst.py
def parenthesise(self, inner: str) -> str:
    return f'({inner})'

prose(text) #

Source code in src/math_spec/typesetting/typst.py
def prose(self, text: str) -> str:
    return f'upright({_quote(text)})'

quoted(label) #

Source code in src/math_spec/typesetting/typst.py
def quoted(self, label: str) -> str:
    in_quotes = f"'{label}'"
    return f'upright({_quote(in_quotes)})'

script(letter) #

Source code in src/math_spec/typesetting/typst.py
def script(self, letter: str) -> str:
    return f'cal({letter})'

section(title, body) #

Source code in src/math_spec/typesetting/typst.py
def section(self, title: str, body: str) -> str:
    return f'== {title}\n{body}'

subscript(base, indices) #

Source code in src/math_spec/typesetting/typst.py
def subscript(self, base: str, indices: list[str]) -> str:
    return f'{base}_({",".join(indices)})' if indices else base

summation(domain, body) #

Source code in src/math_spec/typesetting/typst.py
def summation(self, domain: str, body: str) -> str:
    return f'sum_({domain}) {body}'

superscript(base, tail) #

Source code in src/math_spec/typesetting/typst.py
def superscript(self, base: str, tail: str) -> str:
    return f'{base}^({tail})'

upright(name) #

Source code in src/math_spec/typesetting/typst.py
def upright(self, name: str) -> str:
    return f'upright({_quote(name)})'