Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

More tex options (#457)

Arone604985f9f8f
diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst
index 80b47905e..940c8efa8 100644
--- a/docs/source/changelog.rst
+++ b/docs/source/changelog.rst
@@ -60,6 +60,7 @@ Mobjects, Scenes, and Animations
#. The :code:`Container` class has been made into an AbstractBaseClass, i.e. in cannot be instantiated. Instead, use one of its children classes
#. The ``TextMobject`` and ``TexMobject`` objects have been deprecated, due to their confusing names, in favour of ``Tex`` and ``MathTex``. You can still, however, continue to use ``TextMobject`` and ``TexMobject``, albeit with Deprecation Warnings constantly reminding you to switch.
#. Add a :code:`Variable` class for displaying text that continuously updates to reflect the value of a python variable.
+#. The ``Tex`` and ``MathTex`` objects allow you to specify a custom TexTemplate using the ``template`` keyword argument.
#. :code:`VGroup` now supports printing the class names of contained mobjects and :code:`VDict` supports printing the internal dict of mobjects
diff --git a/manim/mobject/svg/svg_mobject.py b/manim/mobject/svg/svg_mobject.py
index 247c03215..0d2cbdf54 100644
--- a/manim/mobject/svg/svg_mobject.py
+++ b/manim/mobject/svg/svg_mobject.py
@@ -287,7 +287,7 @@ class SVGMobject(VMobject):
all_childNodes_have_id = []
if not isinstance(element, minidom.Element):
return
- if element.hasAttribute("id"):
+ if element.hasAttribute("id") and element.tagName not in ("g", "defs"):
return [element]
for e in element.childNodes:
all_childNodes_have_id.append(self.get_all_childNodes_have_id(e))
diff --git a/manim/mobject/svg/tex_mobject.py b/manim/mobject/svg/tex_mobject.py
index 9cb359740..3a0efafd4 100644
--- a/manim/mobject/svg/tex_mobject.py
+++ b/manim/mobject/svg/tex_mobject.py
@@ -48,13 +48,20 @@ class SingleStringMathTex(SVGMobject):
"organize_left_to_right": False,
"alignment": "",
"type": "tex",
+ "template": None,
}
def __init__(self, tex_string, **kwargs):
digest_config(self, kwargs)
+ if self.template is None:
+ self.template = kwargs.get("tex_template", config["tex_template"])
assert isinstance(tex_string, str)
self.tex_string = tex_string
- file_name = tex_to_svg_file(self.get_modified_expression(tex_string), self.type)
+ file_name = tex_to_svg_file(
+ self.get_modified_expression(tex_string),
+ self.type,
+ tex_template=self.template,
+ )
SVGMobject.__init__(self, file_name=file_name, **kwargs)
if self.height is None:
self.scale(TEX_MOB_SCALE_FACTOR)
diff --git a/manim/utils/tex.py b/manim/utils/tex.py
index 969481281..14d596b32 100644
--- a/manim/utils/tex.py
+++ b/manim/utils/tex.py
@@ -16,6 +16,10 @@ class TexTemplateFromFile:
"use_ctex": False,
"filename": "tex_template.tex",
"text_to_replace": "YourTextHere",
+ "tex_compiler": {
+ "command": "latex",
+ "output_format": ".dvi",
+ },
}
body = ""
diff --git a/manim/utils/tex_file_writing.py b/manim/utils/tex_file_writing.py
index e1b527eb6..51298b1ac 100644
--- a/manim/utils/tex_file_writing.py
+++ b/manim/utils/tex_file_writing.py
@@ -21,11 +21,14 @@ def tex_hash(expression):
return hasher.hexdigest()[:16]
-def tex_to_svg_file(expression, source_type):
- tex_template = config["tex_template"]
+def tex_to_svg_file(expression, source_type, tex_template=None):
+ if tex_template is None:
+ tex_template = config["tex_template"]
tex_file = generate_tex_file(expression, tex_template, source_type)
- dvi_file = tex_to_dvi(tex_file, tex_template.use_ctex)
- return dvi_to_svg(dvi_file, use_ctex=tex_template.use_ctex)
+ dvi_file = tex_to_dvi(tex_file, tex_template.use_ctex, tex_template.tex_compiler)
+ return dvi_to_svg(
+ dvi_file, use_ctex=tex_template.use_ctex, tex_compiler=tex_template.tex_compiler
+ )
def generate_tex_file(expression, tex_template, source_type):
@@ -42,67 +45,88 @@ def generate_tex_file(expression, tex_template, source_type):
return result
-def tex_to_dvi(tex_file, use_ctex=False):
- result = tex_file.replace(".tex", ".dvi" if not use_ctex else ".xdv")
+def tex_compilation_command(compiler, tex_file, tex_dir):
+ if compiler["command"] in {"latex", "pdflatex", "luatex", "lualatex"}:
+ commands = [
+ compiler["command"],
+ "-interaction=batchmode",
+ f'-output-format="{compiler["output_format"][1:]}"',
+ "-halt-on-error",
+ f'-output-directory="{tex_dir}"',
+ f'"{tex_file}"',
+ ">",
+ os.devnull,
+ ]
+ elif compiler["command"] == "xelatex":
+ if compiler["output_format"] == ".xdv":
+ outflag = "-no-pdf"
+ elif compiler["output_format"] == ".pdf":
+ outflag = ""
+ else:
+ raise ValueError("xelatex output is either pdf or xdv")
+ commands = [
+ "xelatex",
+ outflag,
+ "-interaction=batchmode",
+ "-halt-on-error",
+ '-output-directory="{}"'.format(tex_dir),
+ '"{}"'.format(tex_file),
+ ">",
+ os.devnull,
+ ]
+ else:
+ raise ValueError(f"Tex compiler {compiler['command']} unknown.")
+ return " ".join(commands)
+
+
+def tex_to_dvi(tex_file, use_ctex=False, tex_compiler=None):
+ if tex_compiler is None:
+ tex_compiler = (
+ {"command": "xelatex", "output_format": ".xdv"}
+ if use_ctex
+ else {"command": "latex", "output_format": ".dvi"}
+ )
+ result = tex_file.replace(".tex", tex_compiler["output_format"])
result = Path(result).as_posix()
tex_file = Path(tex_file).as_posix()
tex_dir = Path(file_writer_config["tex_dir"]).as_posix()
if not os.path.exists(result):
- commands = (
- [
- "latex",
- "-interaction=batchmode",
- "-halt-on-error",
- '-output-directory="{}"'.format(tex_dir),
- '"{}"'.format(tex_file),
- ">",
- os.devnull,
- ]
- if not use_ctex
- else [
- "xelatex",
- "-no-pdf",
- "-interaction=batchmode",
- "-halt-on-error",
- '-output-directory="{}"'.format(tex_dir),
- '"{}"'.format(tex_file),
- ">",
- os.devnull,
- ]
- )
- exit_code = os.system(" ".join(commands))
+ command = tex_compilation_command(tex_compiler, tex_file, tex_dir)
+ exit_code = os.system(command)
if exit_code != 0:
log_file = tex_file.replace(".tex", ".log")
- raise Exception(
- (
- "LaTeX error converting to dvi. "
- if not use_ctex
- else "XeLaTeX error converting to xdv. "
- )
- + f"See log output above or the log file: {log_file}"
+ raise ValueError(
+ f"{tex_compiler['command']} error converting to"
+ f" {tex_compiler['output_format'][1:]}. See log output above or"
+ f" the log file: {log_file}"
)
return result
-def dvi_to_svg(dvi_file, use_ctex=False, regen_if_exists=False):
+def dvi_to_svg(
+ dvi_file, use_ctex=False, tex_compiler=None, regen_if_exists=False, page=1
+):
"""
- Converts a dvi, which potentially has multiple slides, into a
- directory full of enumerated pngs corresponding with these slides.
- Returns a list of PIL Image objects for these images sorted as they
- where in the dvi
+ Converts a dvi, xdv, or pdf file into an svg using dvisvgm.
"""
- result = dvi_file.replace(".dvi" if not use_ctex else ".xdv", ".svg")
+ if tex_compiler is None:
+ tex_compiler = (
+ {"command": "xelatex", "output_format": ".xdv"}
+ if use_ctex
+ else {"command": "latex", "output_format": ".dvi"}
+ )
+ result = dvi_file.replace(tex_compiler["output_format"], ".svg")
result = Path(result).as_posix()
dvi_file = Path(dvi_file).as_posix()
if not os.path.exists(result):
commands = [
"dvisvgm",
+ "--pdf" if tex_compiler["output_format"] == ".pdf" else "",
+ "-p " + str(page),
'"{}"'.format(dvi_file),
"-n",
- "-v",
- "0",
- "-o",
- '"{}"'.format(result),
+ "-v 0",
+ "-o " + f'"{result}"',
">",
os.devnull,
]