Repository: ManimCommunity/manim

Issue:
Can't compile LaTeX code on Windows
## Description of bug / unexpected behavior
First, seen in https://github.com/ManimCommunity/manim-website/pull/85#issuecomment-1338896601 (https://github.com/ManimCommunity/manim-website/actions/runs/3627713672/jobs/6117890709) on the `manim-website` repo, it looks like between release `v0.16.0` and `v0.17.0` manim has a bug in rendering LaTeX. 

I could reproduce this locally, the same code works with `v0.16.0` but not with `v0.17.0` (nothing changed in LaTeX installation).


## Expected behavior
<!-- Add a clear and concise description of what you expected to happen. -->
The code should be able to render as it did in previous versions.

## How to reproduce the issue
<!-- Provide a piece of code illustrating the undesired behavior. -->

<details><summary>Code for reproducing the problem</summary>

```py
from manim import *

class Test(Scene):
    def construct(self):
        self.add(Tex(r"\LaTeX"))

```

</details>


## Additional media files
<!-- Paste in the files manim produced on rendering the code above. -->

<details><summary>Images/GIFs</summary>

N/A

</details>


## Logs
<details><summary>Terminal output</summary>
<!-- Add "-v DEBUG" when calling manim to generate more detailed logs -->

```
Manim Community v0.17.0

[12/06/22 08:20:57] INFO     Writing This is some \LaTeX tex_file_writing.py:97
                             to                                                
                             media\Tex\c9129f64b4d15f4b.                       
                             tex                                               
+--------------------- Traceback (most recent call last) ---------------------+
| C:\hostedtoolcache\windows\Python\3.10.8\x64\lib\site-packages\manim\cli\re |
| nder\commands.py:115 in render                                              |
|                                                                             |
|   112             try:                                                      |
|   113                 with tempconfig({}):                                  |
|   114                     scene = SceneClass()                              |
| > 115                     scene.render()                                    |
|   116             except Exception:                                         |
|   117                 error_console.print_exception()                       |
|   118                 sys.exit(1)                                           |
|                                                                             |
| C:\hostedtoolcache\windows\Python\3.10.8\x64\lib\site-packages\manim\scene\ |
| scene.py:223 in render                                                      |
|                                                                             |
|    220         """                                                          |
|    221         self.setup()                                                 |
|    222         try:                                                         |
| >  223             self.construct()                                         |
|    224         except EndSceneEarlyException:                               |
|    225             pass                                                     |
|    226         except RerunSceneException as e:                             |
|                                                                             |
| C:\Users\RUNNER~1\AppData\Local\Temp\tmp9jnfrbmj\test.py:4 in construct     |
|                                                                             |
|    1 from manim import *                                                    |
|    2 class OpeningManim(Scene):                                             |
|    3     def construct(self):                                               |
| >  4         title = Tex(r"This is some \LaTeX")                            |
|    5         basel = MathTex(r"\sum_{n=1}^\infty \frac{1}{n^2} = \frac{\pi^ |
|    6         VGroup(title, basel).arrange(DOWN)                             |
|    7         self.play(                                                     |
|                                                                             |
| C:\hostedtoolcache\windows\Python\3.10.8\x64\lib\site-packages\manim\mobjec |
| t\text\tex_mobject.py:427 in __init__                                       |
|                                                                             |
|   424     def __init__(                                                     |
|   425         self, *tex_strings, arg_separator="", tex_environment="center |
|   426     ):                                                                |
| > 427         super().__init__(                                             |
|   428             *tex_strings,                                             |
|   429             arg_separator=arg_separator,                              |
|   430             tex_environment=tex_environment,                          |
|                                                                             |
| C:\hostedtoolcache\windows\Python\3.10.8\x64\lib\site-packages\manim\mobjec |
| t\text\tex_mobject.py:275 in __init__                                       |
|                                                                             |
|   272         self.brace_notation_split_occurred = False                    |
|   273         self.tex_strings = self._break_up_tex_strings(tex_strings)    |
|   274         try:                                                          |
| > 275             super().__init__(                                         |
|   276                 self.arg_separator.join(self.tex_strings),            |
|   277                 tex_environment=self.tex_environment,                 |
|   278                 tex_template=self.tex_template,                       |
|                                                                             |
| C:\hostedtoolcache\windows\Python\3.10.8\x64\lib\site-packages\manim\mobjec |
| t\text\tex_mobject.py:84 in __init__                                        |
|                                                                             |
|    81                                                                       |
|    82         assert isinstance(tex_string, str)                            |
|    83         self.tex_string = tex_string                                  |
| >  84         file_name = tex_to_svg_file(                                  |
|    85             self._get_modified_expression(tex_string),                |
|    86             environment=self.tex_environment,                         |
|    87             tex_template=self.tex_template,                           |
|                                                                             |
| C:\hostedtoolcache\windows\Python\3.10.8\x64\lib\site-packages\manim\utils\ |
| tex_file_writing.py:54 in tex_to_svg_file                                   |
|                                                                             |
|    51     if tex_template is None:                                          |
|    52         tex_template = config["tex_template"]                         |
|    53     tex_file = generate_tex_file(expression, environment, tex_templat |
| >  54     dvi_file = compile_tex(                                           |
|    55         tex_file,                                                     |
|    56         tex_template.tex_compiler,                                    |
|    57         tex_template.output_format,                                   |
|                                                                             |
| C:\hostedtoolcache\windows\Python\3.10.8\x64\lib\site-packages\manim\utils\ |
| tex_file_writing.py:200 in compile_tex                                      |
|                                                                             |
|   197         exit_code = os.system(command)                                |
|   198         if exit_code != 0:                                            |
|   199             log_file = tex_file.with_suffix(".log")                   |
| > 200             print_all_tex_errors(log_file, tex_compiler, tex_file)    |
|   201             raise ValueError(                                         |
|   202                 f"{tex_compiler} error converting to"                 |
|   203                 f" {output_format[1:]}. See log output above or"      |
|                                                                             |
| C:\hostedtoolcache\windows\Python\3.10.8\x64\lib\site-packages\manim\utils\ |
| tex_file_writing.py:255 in print_all_tex_errors                             |
|                                                                             |
|   252                                                                       |
|   253 def print_all_tex_errors(log_file: Path, tex_compiler: str, tex_file: |
|   254     if not log_file.exists():                                         |
| > 255         raise RuntimeError(                                           |
|   256             f"{tex_compiler} failed but did not produce a log file. " |
|   257             "Check your LaTeX installation.",                         |
|   258         )                                                             |
+-----------------------------------------------------------------------------+
RuntimeError: latex failed but did not produce a log file. Check your LaTeX 
installation.
```

<!-- Insert screenshots here (only when absolutely necessary, we prefer copy/pasted output!) -->

</details>


## System specifications

<details><summary>System Details</summary>

- OS (with version, e.g., Windows 10 v2004 or macOS 10.15 (Catalina)): Windows 11
- RAM: 32Gb
- Python version (`python/py/python3 --version`): Happens across 3.8 - 3.11
- Installed modules (provide output from `pip list`): Dev environment, see `poetry.lock`

</details>

<details><summary>LaTeX details</summary>

+ LaTeX distribution (e.g. TeX Live 2020): TinyTex (TexLive 2022)
+ Installed LaTeX packages: Default installed on CI
<!-- output of `tlmgr list --only-installed` for TeX Live or a screenshot of the Packages page for MikTeX -->
</details>

<details><summary>FFMPEG</summary>

Output of `ffmpeg -version`: N/A

</details>

## Additional comments
<!-- Add further context that you think might be relevant for this issue here. -->



Discussion:
After some investigation 🔍, found out that https://github.com/ManimCommunity/manim/commit/9d1f066d637cb15baea10e6907ab85efff8fb36f#diff-134f4c9e60c81a2bf9a836661dc3e85b3dce6ce04be578911c6516583c39c205 is the cause of this issue. Printing out the command where latex was executed I could find that, `\` was used instead of `/`. I think `latex` can't understand `\` so it fails to find the input file (and directory to write output) and fails (also it can't figure out where to write the log file and fails there too). I'll make a PR to fix this. 

Command:
```sh
latex -interaction=batchmode -output-format="dvi" -halt-on-error -output-directory="media\Tex" "media\Tex\1367895737f9a7d3.tex" > nul
```
See, `-output-directory` parameter as well the last positional argument.

Resolving patch:
diff --git a/manim/utils/tex_file_writing.py b/manim/utils/tex_file_writing.py
--- a/manim/utils/tex_file_writing.py
+++ b/manim/utils/tex_file_writing.py
@@ -129,8 +129,8 @@ def tex_compilation_command(
             "-interaction=batchmode",
             f'-output-format="{output_format[1:]}"',
             "-halt-on-error",
-            f'-output-directory="{tex_dir}"',
-            f'"{tex_file}"',
+            f'-output-directory="{tex_dir.as_posix()}"',
+            f'"{tex_file.as_posix()}"',
             ">",
             os.devnull,
         ]
@@ -146,8 +146,8 @@ def tex_compilation_command(
             outflag,
             "-interaction=batchmode",
             "-halt-on-error",
-            f'-output-directory="{tex_dir}"',
-            f'"{tex_file}"',
+            f'-output-directory="{tex_dir.as_posix()}"',
+            f'"{tex_file.as_posix()}"',
             ">",
             os.devnull,
         ]
@@ -229,10 +229,10 @@ def convert_to_svg(dvi_file: Path, extension: str, page: int = 1):
             "dvisvgm",
             "--pdf" if extension == ".pdf" else "",
             "-p " + str(page),
-            f'"{dvi_file}"',
+            f'"{dvi_file.as_posix()}"',
             "-n",
             "-v 0",
-            "-o " + f'"{result}"',
+            "-o " + f'"{result.as_posix()}"',
             ">",
             os.devnull,
         ]


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
@@ -33,6 +33,19 @@ def test_tex():
     assert Path(config.media_dir, "Tex", "f2e45e6e82d750e6.svg").exists()
 
 
+def test_tex_temp_directory(tmpdir, monkeypatch):
+    # Adds a test for #3060
+    # It's not possible to reproduce the issue normally, because we use
+    # tempconfig to change media directory to temporary directory by default
+    # we partially, revert that change here.
+    monkeypatch.chdir(tmpdir)
+    Path(tmpdir, "media").mkdir()
+    with tempconfig({"media_dir": "media"}):
+        Tex("The horse does not eat cucumber salad.")
+        assert Path("media", "Tex").exists()
+        assert Path("media", "Tex", "f2e45e6e82d750e6.svg").exists()
+
+
 def test_percent_char_rendering():
     Tex(r"\%")
     assert Path(config.media_dir, "Tex", "3f48edf8ebaf82c8.tex").exists()