Repository: ManimCommunity/manim

Issue:
Specification of `tex_template` via CLI and config broken
Starting in 0.16, it seems that most ways of using tex templates has stopped working.  To reproduce, use this "scene":
```python
class Test(Scene):
    def construct(self):
        t = MathTex("\\bm t")
```
with this template:
```latex
\documentclass[preview]{standalone}

\usepackage{amsmath}
\usepackage{bm}

\begin{document}

YourTextHere

\end{document}
```

When using a config file or the command line argument `--tex_template`, this works on 0.15 but not on 0.16.  It still works correctly if you manually specify the tex template to the constructor of `MathTex`.


Discussion:
Thanks for the report! Just to clarify: neither setting it in a local config file, nor passing the `--tex_template` flag via the command line interface works correctly?

As a workaround you could run `MathTex.set_default(tex_template=your_template)`. I'll try to investigate this, sorry for the inconvenience.
> Thanks for the report! Just to clarify: neither setting it in a local config file, nor passing the `--tex_template` flag via the command line interface works correctly?

Yes, that's correct.

Resolving patch:
diff --git a/manim/_config/utils.py b/manim/_config/utils.py
--- a/manim/_config/utils.py
+++ b/manim/_config/utils.py
@@ -286,6 +286,7 @@ class MyScene(Scene):
         "scene_names",
         "show_in_file_browser",
         "tex_dir",
+        "tex_template",
         "tex_template_file",
         "text_dir",
         "upto_animation_number",
@@ -361,6 +362,8 @@ def update(self, obj: ManimConfig | dict) -> None:
 
         if isinstance(obj, ManimConfig):
             self._d.update(obj._d)
+            if obj.tex_template:
+                self.tex_template = obj.tex_template
 
         elif isinstance(obj, dict):
             # First update the underlying _d, then update other properties
@@ -1540,7 +1543,7 @@ def tex_template(self):
             if fn:
                 self._tex_template = TexTemplateFromFile(filename=fn)
             else:
-                self._tex_template = TexTemplateLibrary.default.copy()
+                self._tex_template = TexTemplate()
         return self._tex_template
 
     @tex_template.setter
@@ -1562,10 +1565,8 @@ def tex_template_file(self, val: str) -> None:
                 )
             else:
                 self._d["tex_template_file"] = Path(val)
-                self._tex_template = TexTemplateFromFile(filename=val)
         else:
             self._d["tex_template_file"] = val  # actually set the falsy value
-            self._tex_template = TexTemplate()  # but don't use it
 
     @property
     def plugins(self):
diff --git a/manim/cli/render/commands.py b/manim/cli/render/commands.py
--- a/manim/cli/render/commands.py
+++ b/manim/cli/render/commands.py
@@ -98,7 +98,7 @@ def __repr__(self):
             keep_running = True
             while keep_running:
                 for SceneClass in scene_classes_from_file(file):
-                    with tempconfig(config):
+                    with tempconfig({}):
                         scene = SceneClass(renderer)
                         rerun = scene.render()
                     if rerun or config["write_all"]:
@@ -116,7 +116,7 @@ def __repr__(self):
     else:
         for SceneClass in scene_classes_from_file(file):
             try:
-                with tempconfig(config):
+                with tempconfig({}):
                     scene = SceneClass()
                     scene.render()
             except Exception:
diff --git a/manim/utils/tex.py b/manim/utils/tex.py
--- a/manim/utils/tex.py
+++ b/manim/utils/tex.py
@@ -96,6 +96,14 @@ def __init__(
         )
         self._rebuild()
 
+    def __eq__(self, other: TexTemplate) -> bool:
+        return (
+            self.body == other.body
+            and self.tex_compiler == other.tex_compiler
+            and self.output_format == other.output_format
+            and self.post_doc_commands == other.post_doc_commands
+        )
+
     def _rebuild(self):
         """Rebuilds the entire TeX template text from ``\\documentclass`` to ``\\end{document}`` according to all settings and choices."""
         self.body = (


Test patch:
diff --git a/tests/module/mobject/text/test_texmobject.py b/tests/module/mobject/text/test_texmobject.py
--- a/tests/module/mobject/text/test_texmobject.py
+++ b/tests/module/mobject/text/test_texmobject.py
@@ -4,19 +4,19 @@
 
 import pytest
 
-from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, config
+from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, config, tempconfig
 from manim.mobject.types.vectorized_mobject import VMobject
 from manim.utils.color import RED
 
 
 def test_MathTex():
     MathTex("a^2 + b^2 = c^2")
-    assert Path(config.media_dir, "Tex", "3879f6b03bc495cd.svg").exists()
+    assert Path(config.media_dir, "Tex", "eb38bdba08f46c80.svg").exists()
 
 
 def test_SingleStringMathTex():
     SingleStringMathTex("test")
-    assert Path(config.media_dir, "Tex", "79822967f1fa1935.svg").exists()
+    assert Path(config.media_dir, "Tex", "5b2faa68ebf42d1e.svg").exists()
 
 
 @pytest.mark.parametrize(  # : PT006
@@ -30,12 +30,12 @@ def test_double_braces_testing(text_input, length_sub):
 
 def test_tex():
     Tex("The horse does not eat cucumber salad.")
-    assert Path(config.media_dir, "Tex", "983949cac5bdd272.svg").exists()
+    assert Path(config.media_dir, "Tex", "f2e45e6e82d750e6.svg").exists()
 
 
 def test_percent_char_rendering():
     Tex(r"\%")
-    assert Path(config.media_dir, "Tex", "ce6e53f2d9c537bb.tex").exists()
+    assert Path(config.media_dir, "Tex", "3f48edf8ebaf82c8.tex").exists()
 
 
 def test_tex_whitespace_arg():
@@ -171,3 +171,13 @@ def test_error_in_nested_context(capsys):
     stdout = str(capsys.readouterr().out)
     # validate useless context is not included
     assert r"\begin{frame}" not in stdout
+
+
+def test_tempconfig_resetting_tex_template():
+    my_template = TexTemplate()
+    my_template.preamble = "Custom preamble!"
+    tex_template_config_value = config.tex_template
+    with tempconfig({"tex_template": my_template}):
+        assert config.tex_template.preamble == "Custom preamble!"
+
+    assert config.tex_template.preamble != "Custom preamble!"
diff --git a/tests/opengl/test_texmobject_opengl.py b/tests/opengl/test_texmobject_opengl.py
--- a/tests/opengl/test_texmobject_opengl.py
+++ b/tests/opengl/test_texmobject_opengl.py
@@ -9,12 +9,12 @@
 
 def test_MathTex(using_opengl_renderer):
     MathTex("a^2 + b^2 = c^2")
-    assert Path(config.media_dir, "Tex", "3879f6b03bc495cd.svg").exists()
+    assert Path(config.media_dir, "Tex", "eb38bdba08f46c80.svg").exists()
 
 
 def test_SingleStringMathTex(using_opengl_renderer):
     SingleStringMathTex("test")
-    assert Path(config.media_dir, "Tex", "79822967f1fa1935.svg").exists()
+    assert Path(config.media_dir, "Tex", "5b2faa68ebf42d1e.svg").exists()
 
 
 @pytest.mark.parametrize(  # : PT006
@@ -28,7 +28,7 @@ def test_double_braces_testing(using_opengl_renderer, text_input, length_sub):
 
 def test_tex(using_opengl_renderer):
     Tex("The horse does not eat cucumber salad.")
-    assert Path(config.media_dir, "Tex", "983949cac5bdd272.svg").exists()
+    assert Path(config.media_dir, "Tex", "f2e45e6e82d750e6.svg").exists()
 
 
 def test_tex_whitespace_arg(using_opengl_renderer):