Typst

Qualified name: manim.mobject.text.typst\_mobject.Typst

class Typst(typst_code, *, font_size=48, typst_preamble='', color=None, stroke_width=None, font_paths=None, track_baselines=False, should_center=True, height=None, **kwargs)[source]

Bases: SVGMobject

A mobject rendered from a Typst markup string.

The Typst source is compiled to SVG via the typst Python package (a self-contained Rust binary extension — no system-level install required) and then imported through SVGMobject.

Parameters:
  • typst_code (str) – Raw Typst markup to be compiled. This string is placed verbatim into the body of a minimal Typst document.

  • font_size (float) – Font size in Manim font-size units (default: DEFAULT_FONT_SIZE, i.e. 48). The actual scaling is applied after SVG import, matching the approach used by SingleStringMathTex.

  • typst_preamble (str) – Extra Typst code inserted before the body. Useful for #import, #set, or #show rules. Default: "".

  • color (ManimColor) – The color of the mobject. By default the standard VMobject color (white in dark mode). Overrides the Typst text fill color.

  • stroke_width (float | None) – SVG stroke width override. If None (default), the stroke widths from Typst’s SVG output are preserved.

  • font_paths (list[str | Path] | None) – Optional list of additional font directories passed to the Typst compiler (e.g. for custom fonts not installed system-wide).

  • track_baselines (bool) – Whether to keep enough per-element reference data to recover the current Typst baseline frame for each imported submobject. When enabled, baseline_frames and get_baseline_frame() can be used to retrieve the current (orig, right, up) positions for the imported SVG elements.

  • should_center (bool) – Whether to center the mobject after import (default True).

  • height (float | None) – Target height of the mobject. If None (default), the height is determined by font_size.

  • **kwargs (Any) – Forwarded to SVGMobject.

Examples

Example: TypstExample

from manim import *

class TypstExample(Scene):
    def construct(self):
        formula = Typst(r"$ integral_a^b f(x) dif x $")
        self.play(Write(formula))
class TypstExample(Scene):
    def construct(self):
        formula = Typst(r"$ integral_a^b f(x) dif x $")
        self.play(Write(formula))

References: Typst

Example: TypstTextExample

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

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

References: Typst

Methods

get_baseline_frame

Return the current Typst baseline frame for a tracked submobject.

get_mob_from_shape_element

Attach Typst-specific metadata to imported shape mobjects.

init_colors

Recolor black submobjects to self.color.

modify_xml_tree

Convert data-typst-label attributes to id before parsing.

scale

Scale the size by a factor.

select

Select a labeled sub-expression.

Attributes

always

Call a method on a mobject every frame.

animate

Used to animate the application of any method of self.

animation_overrides

baseline_frames

Current Typst baseline frames for all tracked leaf submobjects.

color

depth

The depth of the mobject.

fill_color

If there are multiple colors (for gradient) this returns the first one

font_size

The font size of the Typst mobject.

hash_seed

Include baseline tracking in the SVG cache key.

height

The height of the mobject.

n_points_per_curve

sheen_factor

stroke_color

width

The width of the mobject.

target

original_id

static _get_reference_baseline_frame(shape)[source]

Return the reference (orig, right, up) frame for a Typst SVG element.

The frame is expressed in the same pre-centering coordinate system as the imported submobject points after the element’s own SVG transform has been applied.

Parameters:

shape (SVGElement)

Return type:

ndarray | None

_original__init__(typst_code, *, font_size=48, typst_preamble='', color=None, stroke_width=None, font_paths=None, track_baselines=False, should_center=True, height=None, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

Parameters:
  • typst_code (str)

  • font_size (float)

  • typst_preamble (str)

  • color (TypeAliasForwardRef('~manim.utils.color.core.ParsableManimColor') | None)

  • stroke_width (float | None)

  • font_paths (list[str | Path] | None)

  • track_baselines (bool)

  • should_center (bool)

  • height (float | None)

  • kwargs (Any)

_rebuild_label_aliases()[source]

Rebuild user-facing label aliases from imported SVG ids.

Return type:

None

_refresh_svg_stroke_widths()[source]

Refresh pixel stroke widths for Typst-authored SVG strokes.

SVG stroke widths are specified in the SVG’s local coordinate system, while Manim stroke widths are pixel-based. For Typst-authored strokes such as fraction bars or underlines, rescale them according to the current geometric scale of the imported element so their visual weight stays proportional to the rest of the expression.

Return type:

None

_user_label_keys()[source]

Return the label keys that were created from data-typst-label attributes (filtering out internal Typst group IDs and auto-numbered groups).

Return type:

list[str]

property baseline_frames: list[tuple[ndarray, ndarray, ndarray]]

Current Typst baseline frames for all tracked leaf submobjects.

property font_size: float

The font size of the Typst mobject.

get_baseline_frame(submobject)[source]

Return the current Typst baseline frame for a tracked submobject.

The returned tuple contains the current positions of (orig, right, up). These are recovered from the stored reference frame and the submobject’s current affine position in the scene.

Parameters:

submobject (VMobject)

Return type:

tuple[ndarray, ndarray, ndarray]

get_mob_from_shape_element(shape)[source]

Attach Typst-specific metadata to imported shape mobjects.

Parameters:

shape (SVGElement)

Return type:

VMobject | None

property hash_seed: tuple

Include baseline tracking in the SVG cache key.

init_colors(propagate_colors=True)[source]

Recolor black submobjects to self.color.

Typst renders text in black (fill="#000000") by default. This mirrors the approach of SingleStringMathTex.init_colors(): any submobject whose color is black is recolored to self.color, while explicitly colored submobjects (non-black) are preserved.

Parameters:

propagate_colors (bool)

Return type:

Self

modify_xml_tree(element_tree)[source]

Convert data-typst-label attributes to id before parsing.

Typst’s SVG renderer emits data-typst-label on <g> elements that carry a label (created via #box(body) <label>). The svgelements library propagates custom data-* attributes from parent groups to all children, making them unusable as unique group keys. id attributes, on the other hand, are not inherited.

This method walks the XML tree and promotes every data-typst-label to id (on <g> elements only), so that get_mobjects_from() can pick them up via its existing id-based grouping logic.

Parameters:

element_tree (ElementTree)

Return type:

ElementTree

scale(scale_factor, scale_stroke=False, *, about_point=None, about_edge=None)[source]

Scale the size by a factor.

Default behavior is to scale about the center of the vmobject.

Parameters:
  • scale_factor (float) – The scaling factor \(\alpha\). If \(0 < |\alpha| < 1\), the mobject will shrink, and for \(|\alpha| > 1\) it will grow. Furthermore, if \(\alpha < 0\), the mobject is also flipped.

  • scale_stroke (bool) – Boolean determining if each submobject’s outline is scaled when the object is scaled. If enabled, each submobject keeps its relative stroke width (for example, a submobject with a 2px outline scaled by a factor of .5 will have a 1px outline, while a submobject with 0px stroke remains at 0px).

  • kwargs – Additional keyword arguments passed to scale().

  • about_point (ndarray | None)

  • about_edge (ndarray | None)

Returns:

self

Return type:

VMobject

Examples

Example: MobjectScaleExample

../_images/MobjectScaleExample-3.png
from manim import *

class MobjectScaleExample(Scene):
    def construct(self):
        c1 = Circle(1, RED).set_x(-1)
        c2 = Circle(1, GREEN).set_x(1)

        vg = VGroup(c1, c2)
        vg.set_stroke(width=50)
        self.add(vg)

        self.play(
            c1.animate.scale(.25),
            c2.animate.scale(.25,
                scale_stroke=True)
        )
class MobjectScaleExample(Scene):
    def construct(self):
        c1 = Circle(1, RED).set_x(-1)
        c2 = Circle(1, GREEN).set_x(1)

        vg = VGroup(c1, c2)
        vg.set_stroke(width=50)
        self.add(vg)

        self.play(
            c1.animate.scale(.25),
            c2.animate.scale(.25,
                scale_stroke=True)
        )

See also

move_to()

select(key)[source]

Select a labeled sub-expression.

Labels are created in the Typst source either manually via the manimgrp helper or automatically through the {{ }} double-brace notation in MathTypst.

Parameters:

key (str | int) – A label name (str) matching a data-typst-label in the SVG, or an integer index into the auto-numbered {{ }} groups (_grp-0, _grp-1, …).

Returns:

The submobjects corresponding to the selected group.

Return type:

VGroup

Raises:
  • KeyError – If no group with the given label exists.

  • IndexError – If an integer index is out of range.

Examples

Example: TypstSelectExample

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

class TypstSelectExample(Scene):
    def construct(self):
        eq = MathTypst(
            "{{ a + b : num }} / {{ c : den }} = {{ lambda }} {{ x }}"
        )
        eq.select("num").set_color(RED)  # "a + b"
        eq.select("den").set_color(BLUE) # "c"
        eq.select(0).set_color(YELLOW)   # "lambda" (auto-numbered: "grp-0")
        eq.select(1).set_color(GREEN)    # "x" (auto-numbered: "grp-1")

        self.add(eq)
class TypstSelectExample(Scene):
    def construct(self):
        eq = MathTypst(
            "{{ a + b : num }} / {{ c : den }} = {{ lambda }} {{ x }}"
        )
        eq.select("num").set_color(RED)  # "a + b"
        eq.select("den").set_color(BLUE) # "c"
        eq.select(0).set_color(YELLOW)   # "lambda" (auto-numbered: "grp-0")
        eq.select(1).set_color(GREEN)    # "x" (auto-numbered: "grp-1")

        self.add(eq)

References: MathTypst select()