Text
Text, LaTeX math and highlighted code, with addressable parts.
Contents:
k.Text: Text with minimal inline markup (**bold**,*italic*,`code`).k.Math: Formula in LaTeX syntax, typeset by the built-in engine (no TeX installation needed).k.Code: Code with syntax highlighting (tree-sitter) and stable tokens:lang=,line_numbers=True,size=,theme="auto"(follows the scene background).
Methods in this area:
code.highlight: Named transition: dims every line exceptlines(numbered from 1);code.highlight(None)removes the highlight.
Back to the reference index.
k.Text (class)#
k.Text(
text: StrVal = "",
*,
size: FloatVal | None = None,
width: float | None = None,
align: Align = "left",
**props: Unpack[TextKeywords],
)
Text with minimal inline markup (**bold**, *italic*, `code`). size= sets the size, width= wraps lines, align= aligns. With a lambda the text is reactive: k.Text(lambda: f"{x():.1f} kWh").
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
text |
StrVal |
"" |
|
size |
FloatVal | None |
None |
size= sets the size, width= wraps lines, align= aligns. |
width |
float | None |
None |
size= sets the size, width= wraps lines, align= aligns. |
align |
Align |
"left" |
size= sets the size, width= wraps lines, align= aligns. |
**props |
Unpack[TextKeywords] |
variadic | Keyword arguments (TextKeywords): name: str | None, key: str | None, x: FloatVal, y: FloatVal, position: VecVal, rotate: FloatVal, anchor: VecVal, z: FloatVal, scale: FloatVal, scale_x: FloatVal, scale_y: FloatVal, opacity: FloatVal, visible: BoolVal, fill: ColorVal, fill_opacity: FloatVal, stroke: ColorVal, stroke_width: FloatVal, dash: FloatsVal, color: ColorVal, mono: BoolVal. |
Props (animatable with .to(), settable with .set() or in the constructor):
| Prop | Kind | Default | Interpolation |
|---|---|---|---|
fill |
color | theme.fg |
linear |
fill_opacity |
float | 1.0 |
linear |
stroke |
color | theme.fg |
linear |
stroke_width |
float | 0.0 |
linear |
dash |
floats | () |
step_end |
text |
str | "" |
step_end |
size |
float | from the theme |
linear |
wrap |
float | 0.0 |
linear |
align |
str | "left" |
step_end |
mono |
bool | False |
step_end |
Props inherited from k.Node: x, y, rotate, scale, scale_x, scale_y, anchor, opacity, z, visible.
Example:
@k.scene
def text(s: k.Scene):
x = k.signal(0.0)
title = k.Text("**Solar** energy", size=0.7).place(at="top", margin=0.8)
value = k.Text(lambda: f"{x():.1f} kWh").place(at="center")
s.play(k.write(title), k.fade_in(value))
s.play(x.to(12), duration=2)
Members:
to: LikeNode.to; a newtext=string morphs the glyphs (equal characters travel).find_all: Every occurrence ofneedle(in the text without markup), as parts.chars: Characters as parts:txt.chars[3:7](markup removed).words: Whitespace-separated words as parts:txt.words[1].lines: Laid-out lines as parts:txt.lines[0].
Inherited from k.Group: children, swap, insert, pop, fit.
Inherited from k.Node: set, unbind, edge, age, entered, exited, copy, place, to_place, unpin.
k.Text.to (method)
text.to(
*,
text: str | None = None,
duration: float | None = None,
ease: EaseLike | None = None,
delay: float = 0.0,
blend: Blend = "replace",
place: PlaceKeywords | Mapping[str, object] | None = None,
**props: Unpack[PropChanges],
) -> Animation
Like Node.to; a new text= string morphs the glyphs (equal characters travel).
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
text |
str | None |
None |
Like Node.to; a new text= string morphs the glyphs (equal characters travel). |
duration |
float | None |
None |
|
ease |
EaseLike | None |
None |
|
delay |
float |
0.0 |
|
blend |
Blend |
"replace" |
|
place |
PlaceKeywords | Mapping[str, object] | None |
None |
|
**props |
Unpack[PropChanges] |
variadic |
k.Text.find_all (method)
text.find_all(needle: str) -> list[GlyphRun]
Every occurrence of needle (in the text without markup), as parts.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
needle |
str |
required | Every occurrence of needle (in the text without markup), as parts. |
k.Text.chars (property)
text.chars: PartView # read-only
Characters as parts: txt.chars[3:7] (markup removed).
k.Text.words (property)
text.words: PartView # read-only
Whitespace-separated words as parts: txt.words[1].
k.Text.lines (property)
text.lines: PartView # read-only
Laid-out lines as parts: txt.lines[0].
k.Math (class)#
k.Math(
tex: str,
*,
size: float = 0.6,
display: bool = True,
engine: str = "builtin",
**props: Unpack[StyleKeywords],
)
Formula in LaTeX syntax, typeset by the built-in engine (no TeX installation needed). \id{name}{...} names a subexpression (eq["name"]); any subexpression is found through the syntax tree (eq["c^2"] ≡ eq["c^{2}"]). k.morph between equations matches names first, then identical TeX subtrees. Unsupported command: K0801.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
tex |
str |
required | |
size |
float |
0.6 |
|
display |
bool |
True |
|
engine |
str |
"builtin" |
|
**props |
Unpack[StyleKeywords] |
variadic | Keyword arguments (StyleKeywords): name: str | None, key: str | None, x: FloatVal, y: FloatVal, position: VecVal, rotate: FloatVal, anchor: VecVal, z: FloatVal, scale: FloatVal, scale_x: FloatVal, scale_y: FloatVal, opacity: FloatVal, visible: BoolVal, fill: ColorVal, fill_opacity: FloatVal, stroke: ColorVal, stroke_width: FloatVal, dash: FloatsVal, color: ColorVal. |
Props (animatable with .to(), settable with .set() or in the constructor):
| Prop | Kind | Default | Interpolation |
|---|---|---|---|
fill |
color | theme.fg |
linear |
fill_opacity |
float | 1.0 |
linear |
stroke |
color | theme.fg |
linear |
stroke_width |
float | 0.0 |
linear |
dash |
floats | () |
step_end |
tex |
str | "" |
step_end |
size |
float | 0.6 |
linear |
display |
bool | True |
step_end |
Props inherited from k.Node: x, y, rotate, scale, scale_x, scale_y, anchor, opacity, z, visible.
Example:
@k.scene
def formula(s: k.Scene):
eq = k.Math(r"\id{lhs}{a^2 + b^2} = c^2").place(at="center")
s.play(k.write(eq))
s.play(eq["lhs"].to(color=k.YELLOW))
s.play(eq["c^2"].to(color=k.GREEN))
See also: k.morph, k.Text, k.write.
Members:
find_all: Every occurrence ofneedle(in the text without markup), as parts.chars: Characters as parts:txt.chars[3:7](markup removed).words: Whitespace-separated words as parts:txt.words[1].lines: Laid-out lines as parts:txt.lines[0].
Inherited from k.Group: children, to, swap, insert, pop, fit.
Inherited from k.Node: set, unbind, edge, age, entered, exited, copy, place, to_place, unpin.
k.Math.find_all (method)
math.find_all(needle: str) -> list[GlyphRun]
Every occurrence of needle (in the text without markup), as parts.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
needle |
str |
required | Every occurrence of needle (in the text without markup), as parts. |
k.Math.chars (property)
math.chars: PartView # read-only
Characters as parts: txt.chars[3:7] (markup removed).
k.Math.words (property)
math.words: PartView # read-only
Whitespace-separated words as parts: txt.words[1].
k.Math.lines (property)
math.lines: PartView # read-only
Laid-out lines as parts: txt.lines[0].
k.Code (class)#
k.Code(
src: str,
lang: str = "python",
*,
theme: str = "auto",
line_numbers: bool = False,
size: float = 0.32,
**props: Unpack[StyleKeywords],
)
Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background). code.highlight highlights lines and k.morph between two versions animates the diff: unchanged lines slide, inserted lines enter, removed lines leave.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
src |
str |
required | |
lang |
str |
"python" |
Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background). |
theme |
str |
"auto" |
Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background). |
line_numbers |
bool |
False |
Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background). |
size |
float |
0.32 |
Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background). |
**props |
Unpack[StyleKeywords] |
variadic | Keyword arguments (StyleKeywords): name: str | None, key: str | None, x: FloatVal, y: FloatVal, position: VecVal, rotate: FloatVal, anchor: VecVal, z: FloatVal, scale: FloatVal, scale_x: FloatVal, scale_y: FloatVal, opacity: FloatVal, visible: BoolVal, fill: ColorVal, fill_opacity: FloatVal, stroke: ColorVal, stroke_width: FloatVal, dash: FloatsVal, color: ColorVal. |
Props (animatable with .to(), settable with .set() or in the constructor):
| Prop | Kind | Default | Interpolation |
|---|---|---|---|
fill |
color | theme.fg |
linear |
fill_opacity |
float | 1.0 |
linear |
stroke |
color | theme.fg |
linear |
stroke_width |
float | 0.0 |
linear |
dash |
floats | () |
step_end |
code |
str | "" |
step_end |
lang |
str | "text" |
step_end |
size |
float | 0.32 |
linear |
line_numbers |
bool | False |
step_end |
palette |
str | "dark" |
step_end |
highlight |
floats | () |
step_start |
highlight_amount |
float | 0.0 |
linear |
Props inherited from k.Node: x, y, rotate, scale, scale_x, scale_y, anchor, opacity, z, visible.
Example:
SRC = """
def add(a, b):
return a + b
"""
@k.scene
def code_block(s: k.Scene):
code = k.Code(SRC, lang="python", line_numbers=True).place(at="center")
s.play(k.write(code))
s.wait(0.5)
See also: code.highlight, k.morph, k.Text.
Members:
highlight: Named transition: dims every line exceptlines(numbered from 1);code.highlight(None)removes the highlight.find_all: Every occurrence ofneedle(in the text without markup), as parts.chars: Characters as parts:txt.chars[3:7](markup removed).words: Whitespace-separated words as parts:txt.words[1].lines: Laid-out lines as parts:txt.lines[0].
Inherited from k.Group: children, to, swap, insert, pop, fit.
Inherited from k.Node: set, unbind, edge, age, entered, exited, copy, place, to_place, unpin.
k.Code.highlight (method)
code.highlight(
lines: Sequence[int] | None = None,
*,
duration: float | None = None,
ease: EaseLike | None = None,
) -> Animation
Named transition: dims every line except lines (numbered from 1); code.highlight(None) removes the highlight. Equivalent to code.to(highlight=lines, highlight_amount=1).
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
lines |
Sequence[int] | None |
None |
Named transition: dims every line except lines (numbered from 1); code.highlight(None) removes the highlight. |
duration |
float | None |
None |
|
ease |
EaseLike | None |
None |
Example:
SRC = """
x = 1
y = 2
print(x + y)
"""
@k.scene
def code_highlight(s: k.Scene):
code = k.Code(SRC, lang="python").place(at="center")
s.add(code)
s.play(code.highlight(lines=[3]))
s.play(code.highlight(None))
See also: k.Code.
k.Code.find_all (method)
code.find_all(needle: str) -> list[GlyphRun]
Every occurrence of needle (in the text without markup), as parts.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
needle |
str |
required | Every occurrence of needle (in the text without markup), as parts. |
k.Code.chars (property)
code.chars: PartView # read-only
Characters as parts: txt.chars[3:7] (markup removed).
k.Code.words (property)
code.words: PartView # read-only
Whitespace-separated words as parts: txt.words[1].
k.Code.lines (property)
code.lines: PartView # read-only
Laid-out lines as parts: txt.lines[0].