Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Improve Tex string splitting at double braces: only split for double brace groups (#1304)

Benjamin Hackl36e010076c7b
diff --git a/manim/mobject/svg/tex_mobject.py b/manim/mobject/svg/tex_mobject.py
index de65ebed2..7c29d42d9 100644
--- a/manim/mobject/svg/tex_mobject.py
+++ b/manim/mobject/svg/tex_mobject.py
@@ -203,6 +203,7 @@ import itertools as it
import operator as op
import re
from functools import reduce
+from textwrap import dedent
from ... import config, logger
from ...constants import *
@@ -391,6 +392,15 @@ class MathTex(SingleStringMathTex):
>>> MathTex('a^2 + b^2 = c^2')
MathTex('a^2 + b^2 = c^2')
+ Check that double brace group splitting works correctly::
+
+ >>> t1 = MathTex('{{ a }} + {{ b }} = {{ c }}')
+ >>> len(t1.submobjects)
+ 5
+ >>> t2 = MathTex(r"\frac{1}{a+b\sqrt{2}}")
+ >>> len(t2.submobjects)
+ 1
+
"""
def __init__(
@@ -411,27 +421,49 @@ class MathTex(SingleStringMathTex):
if self.tex_to_color_map is None:
self.tex_to_color_map = {}
self.tex_environment = tex_environment
+ self.brace_notation_split_occurred = False
tex_strings = self.break_up_tex_strings(tex_strings)
self.tex_strings = tex_strings
- SingleStringMathTex.__init__(
- self,
- self.arg_separator.join(tex_strings),
- tex_environment=self.tex_environment,
- tex_template=self.tex_template,
- **kwargs,
- )
- self.break_up_by_substrings()
+ try:
+ SingleStringMathTex.__init__(
+ self,
+ self.arg_separator.join(tex_strings),
+ tex_environment=self.tex_environment,
+ tex_template=self.tex_template,
+ **kwargs,
+ )
+ self.break_up_by_substrings()
+ except ValueError as compilation_error:
+ if self.brace_notation_split_occurred:
+ logger.error(
+ dedent(
+ """\
+ A group of double braces, {{ ... }}, was detected in
+ your string. Manim splits TeX strings at the double
+ braces, which might have caused the current
+ compilation error. If you didn't use the double brace
+ split intentionally, add spaces between the braces to
+ avoid the automatic splitting: {{ ... }} --> { { ... } }.
+ """
+ )
+ )
+ raise compilation_error
self.set_color_by_tex_to_color_map(self.tex_to_color_map)
if self.organize_left_to_right:
self.organize_submobjects_left_to_right()
def break_up_tex_strings(self, tex_strings):
- tex_strings = [str(t) for t in tex_strings]
# Separate out anything surrounded in double braces
- patterns = ["{{", "}}"]
+ pre_split_length = len(tex_strings)
+ tex_strings = [re.split("{{(.*?)}}", str(t)) for t in tex_strings]
+ tex_strings = sum(tex_strings, [])
+ if len(tex_strings) > pre_split_length:
+ self.brace_notation_split_occurred = True
+
# Separate out any strings specified in the isolate
# or tex_to_color_map lists.
+ patterns = []
patterns.extend(
[
"({})".format(re.escape(ss))
@@ -441,10 +473,15 @@ class MathTex(SingleStringMathTex):
]
)
pattern = "|".join(patterns)
- pieces = []
- for s in tex_strings:
- pieces.extend(re.split(pattern, s))
- return list(filter(lambda s: s, pieces))
+ if pattern:
+ pieces = []
+ for s in tex_strings:
+ pieces.extend(re.split(pattern, s))
+ else:
+ pieces = tex_strings
+ ret = [p for p in pieces if p]
+ print(ret)
+ return ret
def break_up_by_substrings(self):
"""