Repository: ManimCommunity/manim

Issue:
Latex and Sphinx are not yet functioning together.
This example is working on sphinx:
```
.. manim:: DotScene1
    :quality: low
    :save_last_frame:

        class DotScene1(Scene):
            def construct(self):
                dot = Dot().set_color(GREEN)
                self.add(dot)
                self.wait(1)
```
However, when I have something tex related, it throws an error:
```
.. manim:: TextExample
    :quality: medium
    :save_last_frame:
    
    class TextExample(Scene):
        def construct(self):
            t = TextMobject("Hello World")
            self.add(t)
```
> Exception occurred:
  File "/home/k/projects/manim-community/manim/utils/tex_file_writing.py", line 32, in generate_tex_file
    with open(result, "w", encoding="utf-8") as outfile:
FileNotFoundError: [Errno 2] No such file or directory: 'media/Tex/7d1ec941f0e30957.tex'
The full traceback has been saved in /tmp/sphinx-err-4zdxhjgt.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!
make: *** [Makefile:26: html] Error 2

A similar error message comes e.g. for this example:
```
.. manim:: Plot1
    :quality: medium
    :save_last_frame:

    class Plot1(GraphScene):
       def construct(self):
           self.setup_axes()
           my_func = lambda x: np.sin(x)
           func_graph=self.get_graph(my_func)
           self.add(func_graph)
```


Discussion:

Resolving patch:
diff --git a/docs/source/manim_directive.py b/docs/source/manim_directive.py
--- a/docs/source/manim_directive.py
+++ b/docs/source/manim_directive.py
@@ -152,8 +152,18 @@ def run(self):
         ]
         source_block = "\n".join(source_block)
 
-        media_dir = os.path.join("source", "media")
+        media_dir = os.path.join(setup.confdir, "media")
+        if not os.path.exists(media_dir):
+            os.mkdir(media_dir)
         images_dir = os.path.join(media_dir, "images")
+        if not os.path.exists(images_dir):
+            os.mkdir(images_dir)
+        tex_dir = os.path.join(media_dir, "tex")
+        if not os.path.exists(tex_dir):
+            os.mkdir(tex_dir)
+        text_dir = os.path.join(media_dir, "text")
+        if not os.path.exists(text_dir):
+            os.mkdir(text_dir)
         video_dir = os.path.join(media_dir, "videos")
         output_file = f"{clsname}-{classnamedict[clsname]}"
 
@@ -163,6 +173,8 @@ def run(self):
             f'config["pixel_width"] = {pixel_width}',
             f'file_writer_config["media_dir"] = "{media_dir}"',
             f'file_writer_config["images_dir"] = "{images_dir}"',
+            f'file_writer_config["tex_dir"] = "{tex_dir}"',
+            f'file_writer_config["text_dir"] = "{text_dir}"',
             f'file_writer_config["video_dir"] = "{video_dir}"',
             f'file_writer_config["save_last_frame"] = {save_last_frame}',
             f'file_writer_config["save_as_gif"] = {save_as_gif}',
@@ -200,7 +212,7 @@ def run(self):
 
         rendered_template = jinja2.Template(TEMPLATE).render(
             display_source=display_source,
-            filesrc=filesrc[6:],
+            filesrc_rel=os.path.relpath(filesrc, setup.confdir),
             output_file=output_file,
             save_last_frame=save_last_frame,
             save_as_gif=save_as_gif,
@@ -239,10 +251,10 @@ def setup(app):
 
     <video class="manim-video" controls loop autoplay src="./{{ output_file }}.mp4"></video>
 {% elif save_as_gif %}
-.. image:: {{ filesrc }}
+.. image:: /{{ filesrc_rel }}
     :align: center
 {% elif save_last_frame %}
-.. image:: {{ filesrc }}
+.. image:: /{{ filesrc_rel }}
     :align: center
 {% endif %}
 


Test patch: