3b1b-manim/manimlib/utils/tex.py

39 lines
1.2 KiB
Python
Raw Normal View History

from __future__ import annotations
2022-12-20 22:35:41 -08:00
import re
from functools import lru_cache
2022-12-29 10:37:46 -08:00
from manimlib.utils.tex_to_symbol_count import TEX_TO_SYMBOL_COUNT
2022-12-20 22:35:41 -08:00
@lru_cache
2022-12-20 22:35:41 -08:00
def num_tex_symbols(tex: str) -> int:
tex = remove_tex_environments(tex)
commands_pattern = r"""
(?P<sqrt>\\sqrt\[[0-9]+\])| # Special sqrt with number
(?P<cmd>\\[a-zA-Z!,-/:;<>]+) # Regular commands
2022-12-20 22:35:41 -08:00
"""
total = 0
pos = 0
for match in re.finditer(commands_pattern, tex, re.VERBOSE):
# Count normal characters up to this command
total += sum(1 for c in tex[pos:match.start()] if c not in "^{} \n\t_$\\&")
2022-12-29 10:37:46 -08:00
if match.group("sqrt"):
total += len(match.group()) - 5
else:
total += TEX_TO_SYMBOL_COUNT.get(match.group(), 1)
pos = match.end()
# Count remaining characters
total += sum(1 for c in tex[pos:] if c not in "^{} \n\t_$\\&")
return total
def remove_tex_environments(tex: str) -> str:
# Handle \phantom{...} with any content
tex = re.sub(r"\\phantom\{[^}]*\}", "", tex)
# Handle other environment commands
tex = re.sub(r"\\(begin|end)(\{\w+\})?(\{\w+\})?(\[\w+\])?", "", tex)
return tex