Add String.get_symbol_substrings and Tex.get_symbol_substrings

This commit is contained in:
Grant Sanderson 2022-12-30 15:07:41 -08:00
parent 4335e85659
commit 3aa9eb6372
2 changed files with 14 additions and 2 deletions

View file

@ -561,7 +561,10 @@ class StringMobject(SVGMobject, ABC):
return self.select_parts(selector)[index]
def substr_to_path_count(self, substr: str) -> int:
return len(re.sub(R"\s", "", substr))
return len(re.sub(r"\s", "", substr))
def get_symbol_substrings(self):
return list(re.sub(r"\s", "", self.string))
def select_unisolated_substring(self, pattern: str | re.Pattern) -> VGroup:
if isinstance(pattern, str):

View file

@ -42,7 +42,7 @@ class Tex(StringMobject):
isolate = [isolate]
isolate = [*isolate, *tex_strings]
tex_string = " ".join(tex_strings)
tex_string = (" ".join(tex_strings)).strip()
# Prevent from passing an empty string.
if not tex_string.strip():
@ -222,6 +222,15 @@ class Tex(StringMobject):
log.warning(f"Estimated size of {tex} does not match true size")
return num_tex_symbols(substr)
def get_symbol_substrings(self):
pattern = "|".join((
# Tex commands
r"\\[a-zA-Z]+",
# And most single characters, with these exceptions
r"[^\^\{\}\s\_\$\\\&]",
))
return re.findall(pattern, self.string)
def make_number_changable(
self,
value: float | int | str,