typst_mobject

Mobjects representing text rendered using Typst.

Important

The typst Python package must be installed to use these classes. Install it via pip install typst>=0.14 or add the typst optional dependency group (pip install manim[typst]).

Typst mobjects compile Typst markup directly to SVG using the typst Python package and then import the result through SVGMobject. Use Typst for general Typst markup and MathTypst for display-style math.

Examples

Basic text and math

Example: TypstTextReferenceExample

../_images/TypstTextReferenceExample-1.png
from manim import *

class TypstTextReferenceExample(Scene):
    def construct(self):
        text = Typst(
            r"*Hello* from _Typst!_",
            color=YELLOW,
            font_size=72,
        )
        self.add(text)
class TypstTextReferenceExample(Scene):
    def construct(self):
        text = Typst(
            r"*Hello* from _Typst!_",
            color=YELLOW,
            font_size=72,
        )
        self.add(text)

References: Typst

Example: MathTypstReferenceExample

../_images/MathTypstReferenceExample-1.png
from manim import *

class MathTypstReferenceExample(Scene):
    def construct(self):
        equation = MathTypst(
            r"sum_(k=1)^n k = frac(n(n + 1), 2)",
            font_size=72,
        )
        self.add(equation)
class MathTypstReferenceExample(Scene):
    def construct(self):
        equation = MathTypst(
            r"sum_(k=1)^n k = frac(n(n + 1), 2)",
            font_size=72,
        )
        self.add(equation)

References: MathTypst

Selecting subexpressions

Typst mobjects expose label-based selection via select(). There are two common ways to create selectable groups:

  • use ordinary Typst labels in Typst

  • use Manim’s {{ ... }} shorthand in MathTypst

Note

The {{ ... }} shorthand is currently only supported by MathTypst. For Typst, create labels directly in the Typst source, for example with #box[body] <label>.

Example: TypstLabelSelectionExample

../_images/TypstLabelSelectionExample-1.png
from manim import *

class TypstLabelSelectionExample(Scene):
    def construct(self):
        text = Typst(
            r'''
            #box[
                *Typst* labels also work in regular markup.
            ] <headline>

            #let pick(body) = [#box(body) <picked>]
            We can highlight #pick[multiple] #pick[fragments] at once.
            ''',
            font_size=42,
        )
        text.select("headline").set_color(BLUE)
        text.select("picked").set_color(YELLOW)
        self.add(text)
class TypstLabelSelectionExample(Scene):
    def construct(self):
        text = Typst(
            r'''
            #box[
                *Typst* labels also work in regular markup.
            ] 

            #let pick(body) = [#box(body) ]
            We can highlight #pick[multiple] #pick[fragments] at once.
            ''',
            font_size=42,
        )
        text.select("headline").set_color(BLUE)
        text.select("picked").set_color(YELLOW)
        self.add(text)

References: Typst select()

Example: MathTypstSelectionExample

../_images/MathTypstSelectionExample-1.png
from manim import *

class MathTypstSelectionExample(Scene):
    def construct(self):
        equation = MathTypst(
            "{{ a^2 + b^2 : lhs }} = {{ c^2 }}",
            font_size=72,
        )
        equation.select("lhs").set_color(BLUE)
        equation.select(0).set_color(YELLOW)
        self.add(equation)
class MathTypstSelectionExample(Scene):
    def construct(self):
        equation = MathTypst(
            "{{ a^2 + b^2 : lhs }} = {{ c^2 }}",
            font_size=72,
        )
        equation.select("lhs").set_color(BLUE)
        equation.select(0).set_color(YELLOW)
        self.add(equation)

References: MathTypst select()

Inspecting baseline frames

For debugging or alignment tasks, Typst mobjects can optionally track a per-element baseline frame. Enable this with track_baselines=True and query either baseline_frames for all tracked leaf elements or get_baseline_frame() for a specific selected submobject.

text = Typst("Ggf", track_baselines=True)
orig, right, up = text.baseline_frames[0]

eq = MathTypst("{{ a^2 + b^2 : lhs }} = c^2", track_baselines=True)
for part in eq.select("lhs"):
    orig, right, up = eq.get_baseline_frame(part)
    print(orig, right, up)

Classes

MathTypst

Convenience wrapper: wraps the input in Typst math delimiters.

Typst

A mobject rendered from a Typst markup string.