mirror of
https://github.com/3b1b/manim.git
synced 2025-08-20 05:14:12 +00:00
Add Tex.make_number_changable
This commit is contained in:
parent
04d3e6a47c
commit
0f9adbf91c
1 changed files with 50 additions and 5 deletions
|
@ -212,16 +212,61 @@ class Tex(StringMobject):
|
||||||
):
|
):
|
||||||
return self.set_parts_color_by_dict(color_map)
|
return self.set_parts_color_by_dict(color_map)
|
||||||
|
|
||||||
|
def get_tex(self) -> str:
|
||||||
|
return self.get_string()
|
||||||
|
|
||||||
|
# Specific to Tex
|
||||||
def substr_to_path_count(self, substr: str) -> int:
|
def substr_to_path_count(self, substr: str) -> int:
|
||||||
tex = self.get_tex()
|
tex = self.get_tex()
|
||||||
if len(self) != num_tex_symbols(tex):
|
if len(self) != num_tex_symbols(tex):
|
||||||
log.warning(
|
log.warning(f"Estimated size of {tex} does not match true size")
|
||||||
f"Estimated size of {tex} does not match true size",
|
|
||||||
)
|
|
||||||
return num_tex_symbols(substr)
|
return num_tex_symbols(substr)
|
||||||
|
|
||||||
def get_tex(self) -> str:
|
def make_number_changable(
|
||||||
return self.get_string()
|
self,
|
||||||
|
value: float | int | str,
|
||||||
|
index: int = 0,
|
||||||
|
replace_all: bool = False,
|
||||||
|
**config,
|
||||||
|
) -> VMobject:
|
||||||
|
substr = str(value)
|
||||||
|
parts = self.select_parts(substr)
|
||||||
|
if len(parts) == 0:
|
||||||
|
log.warning(f"{value} not found in Tex.make_number_changable call")
|
||||||
|
return VMobject()
|
||||||
|
if index > len(parts) - 1:
|
||||||
|
log.warning(f"Requested {index}th occurance of {value}, but only {len(parts)} exist")
|
||||||
|
return VMobject()
|
||||||
|
if not replace_all:
|
||||||
|
parts = [parts[index]]
|
||||||
|
|
||||||
|
from manimlib.mobject.numbers import DecimalNumber
|
||||||
|
|
||||||
|
decimal_mobs = []
|
||||||
|
for part in parts:
|
||||||
|
if "." in substr:
|
||||||
|
num_decimal_places = len(substr.split(".")[1])
|
||||||
|
else:
|
||||||
|
num_decimal_places = 0
|
||||||
|
decimal_mob = DecimalNumber(
|
||||||
|
float(value),
|
||||||
|
num_decimal_places=num_decimal_places,
|
||||||
|
**config,
|
||||||
|
)
|
||||||
|
decimal_mob.replace(part)
|
||||||
|
decimal_mob.match_style(part)
|
||||||
|
if len(part) > 1:
|
||||||
|
self.remove(*part[1:])
|
||||||
|
self.replace_submobject(self.submobjects.index(part[0]), decimal_mob)
|
||||||
|
decimal_mobs.append(decimal_mob)
|
||||||
|
|
||||||
|
# Replace substr with something that looks like a tex command. This
|
||||||
|
# is to ensure Tex.substr_to_path_count counts it correctly.
|
||||||
|
self.string = self.string.replace(substr, R"\decimalmob", 1)
|
||||||
|
|
||||||
|
if replace_all:
|
||||||
|
return VGroup(*decimal_mobs)
|
||||||
|
return decimal_mobs[index]
|
||||||
|
|
||||||
|
|
||||||
class TexText(Tex):
|
class TexText(Tex):
|
||||||
|
|
Loading…
Add table
Reference in a new issue