ManimCommunity / manim

A community-maintained Python framework for creating mathematical animations.
https://www.manim.community
MIT License
19.93k stars 1.48k forks source link

LaTeX rendering incorrectly #2912

Open arredondos opened 1 year ago

arredondos commented 1 year ago

Description of bug / unexpected behavior

When using a custom LaTeX template, fractions are rendered incorrectly (no fraction line or only fraction line). The generated .tex, .svg and .pdf files are correct. No LaTeX warnings or errors are emitted.

How to reproduce the issue

Here is a minimal working example that reproduces the problem:

Code for reproducing the problem ```py from manim import * preamble = r""" \usepackage[mathrm=sym]{unicode-math} \setmathfont{Fira Math} """ template = TexTemplate( preamble = preamble, tex_compiler = 'lualatex', output_format='.pdf' ) class MyScene(Scene): def construct(self): self.add(Tex(r"$\frac{a}{b}$", tex_template = template)) ```

Additional media files

Images/GIFs Here's the output, where the fraction bar is missing: ![MyScene_ManimCE_v0 16 0 post0](https://user-images.githubusercontent.com/32519217/182978499-79435102-3905-4b3d-b035-c2641cfec805.png) If you remove the option output_format='.pdf' then only the fraction bar is rendered.

Logs

Terminal output Nothing noteworthy or out of the ordinary in the terminal output.

System specifications

System Details - Windows 10 v21H2 - RAM: 32GB - Python version: 3.10.2 - Installed modules (provide output from `pip list`): ``` WARNING: Ignoring invalid distribution -ip (c:\python310\lib\site-packages) WARNING: Ignoring invalid distribution -anim (c:\python310\lib\site-packages) Package Version --------------------- ------------ argon2-cffi 21.3.0 argon2-cffi-bindings 21.2.0 asttokens 2.0.5 attrs 22.1.0 backcall 0.2.0 beautifulsoup4 4.11.1 bleach 5.0.1 blis 0.7.7 bs4 0.0.1 catalogue 2.0.7 certifi 2021.10.8 cffi 1.15.1 charset-normalizer 2.0.12 click 8.0.4 click-default-group 1.2.2 cloup 0.13.1 colorama 0.4.4 colour 0.1.5 commonmark 0.9.1 cycler 0.11.0 cymem 2.0.6 Cython 0.29.32 debugpy 1.6.0 decorator 5.1.1 defusedxml 0.7.1 en-core-web-sm 3.2.0 en-core-web-trf 3.2.0 entrypoints 0.4 et-xmlfile 1.1.0 executing 0.8.3 fastjsonschema 2.16.1 filelock 3.6.0 fonttools 4.32.0 gensim 4.1.2 glcontext 2.3.6 hdbscan 0.8.28 huggingface-hub 0.5.1 idna 3.3 ipykernel 6.12.1 ipython 8.2.0 ipython-genutils 0.2.0 ipywidgets 7.7.1 isosurfaces 0.1.0 jarowinkler 1.0.2 jedi 0.18.1 Jinja2 3.1.1 joblib 1.1.0 jsonschema 4.9.0 jupyter-client 7.2.2 jupyter-core 4.9.2 jupyterlab-pygments 0.2.2 jupyterlab-widgets 1.1.1 kaleido 0.2.1 kiwisolver 1.4.2 langcodes 3.3.0 Levenshtein 0.18.1 manim 0.16.0.post0 ManimPango 0.4.1 mapbox-earcut 0.12.11 MarkupSafe 2.1.1 matplotlib 3.5.1 matplotlib-inline 0.1.3 mistune 0.8.4 moderngl 5.6.4 moderngl-window 2.4.1 multipledispatch 0.6.0 murmurhash 1.0.6 nbclient 0.6.6 nbconvert 6.5.0 nbformat 5.4.0 nest-asyncio 1.5.5 networkx 2.8.4 nltk 3.7 notebook 6.4.12 numpy 1.22.3 openpyxl 3.0.9 packaging 21.3 pandas 1.4.2 pandocfilters 1.5.0 parso 0.8.3 pathy 0.6.1 pickleshare 0.7.5 Pillow 9.1.0 pip 22.2.1 plotly 5.9.0 preshed 3.0.6 prometheus-client 0.14.1 prompt-toolkit 3.0.29 psutil 5.9.0 pure-eval 0.2.2 pycairo 1.21.0 pycparser 2.21 pydantic 1.8.2 pydub 0.25.1 pyglet 1.5.26 Pygments 2.11.2 pyparsing 3.0.8 pyrr 0.10.3 pyrsistent 0.18.1 PySocks 1.7.1 python-dateutil 2.8.2 pytz 2022.1 pywin32 303 pywinpty 2.0.6 PyYAML 6.0 pyzmq 22.3.0 rapidfuzz 2.0.8 regex 2022.3.15 requests 2.27.1 rich 12.4.4 sacremoses 0.0.49 scikit-learn 1.0.2 scipy 1.8.0 screeninfo 0.8 seaborn 0.11.2 Send2Trash 1.8.0 sentence-transformers 2.2.0 sentencepiece 0.1.96 setuptools 62.1.0 six 1.16.0 skia-pathops 0.7.2 smart-open 5.2.1 soupsieve 2.3.2 spacy 3.2.4 spacy-alignments 0.8.5 spacy-legacy 3.0.9 spacy-loggers 1.0.2 spacy-transformers 1.1.5 spylls 0.1.7 sqlitedict 2.0.0 srsly 2.4.2 srt 3.5.2 stack-data 0.2.0 tabulate 0.8.9 tenacity 8.0.1 terminado 0.15.0 thinc 8.0.15 threadpoolctl 3.1.0 tinycss2 1.1.1 tokenizers 0.11.6 torch 1.11.0 torchvision 0.12.0 tornado 6.1 tqdm 4.64.0 traitlets 5.3.0 transformers 4.17.0 typer 0.4.1 typing_extensions 4.1.1 Unidecode 1.3.4 urllib3 1.26.9 wasabi 0.9.1 watchdog 2.1.9 wcwidth 0.2.5 webencodings 0.5.1 widgetsnbextension 3.6.1 wrapt 1.14.0 xlrd 2.0.1 youtube-dl 2021.12.17 zipp 3.8.0 WARNING: Ignoring invalid distribution -ip (c:\python310\lib\site-packages) WARNING: Ignoring invalid distribution -anim (c:\python310\lib\site-packages) WARNING: Ignoring invalid distribution -ip (c:\python310\lib\site-packages) WARNING: Ignoring invalid distribution -anim (c:\python310\lib\site-packages) ```
LaTeX details + LaTeX distribution: MikTeX + Installed LaTeX packages: ![Screenshot 2022-08-05 025324](https://user-images.githubusercontent.com/32519217/182979095-f238f200-2b11-4930-a4da-09285c49e364.jpg)
FFMPEG Output of `ffmpeg -version`: ``` ffmpeg version 5.0.1-essentials_build-www.gyan.dev Copyright (c) 2000-2022 the FFmpeg developers built with gcc 11.2.0 (Rev7, Built by MSYS2 project) configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband libavutil 57. 17.100 / 57. 17.100 libavcodec 59. 18.100 / 59. 18.100 libavformat 59. 16.100 / 59. 16.100 libavdevice 59. 4.100 / 59. 4.100 libavfilter 8. 24.100 / 8. 24.100 libswscale 6. 4.100 / 6. 4.100 libswresample 4. 3.100 / 4. 3.100 libpostproc 56. 3.100 / 56. 3.100 ```
SolidTux commented 1 year ago

The same thing happens for me, even without unicode-math. Just using lualatex is enough to trigger it.

PhilippImhof commented 1 year ago

FWIW, I get the following error:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ ./manim/manim/mobject/text/tex_mobject.py:434 in __init__                    │
│                                                                                                  │
│   431 │   def __init__(                                                                          │
│   432 │   │   self, *tex_strings, arg_separator="", tex_environment="center", **kwargs           │
│   433 │   ):                                                                                     │
│ ❱ 434 │   │   super().__init__(                                                                  │
│   435 │   │   │   *tex_strings,                                                                  │
│   436 │   │   │   arg_separator=arg_separator,                                                   │
│   437 │   │   │   tex_environment=tex_environment,                                               │
│                                                                                                  │
│ ./manim/manim/mobject/text/tex_mobject.py:288 in __init__                    │
│                                                                                                  │
│   285 │   │   │   │   tex_template=self.tex_template,                                            │
│   286 │   │   │   │   **kwargs,                                                                  │
│   287 │   │   │   )                                                                              │
│ ❱ 288 │   │   │   self._break_up_by_substrings()                                                 │
│   289 │   │   except ValueError as compilation_error:                                            │
│   290 │   │   │   if self.brace_notation_split_occurred:                                         │
│   291 │   │   │   │   logger.error(                                                              │
│                                                                                                  │
│ ./manim/manim/mobject/text/tex_mobject.py:362 in _break_up_by_substrings     │
│                                                                                                  │
│   359 │   │   │   │   # positioned in the right part of the MathTex                              │
│   360 │   │   │   │   sub_tex_mob.submobjects = [VectorizedPoint()]                              │
│   361 │   │   │   │   last_submob_index = min(curr_index, len(self.submobjects) - 1)             │
│ ❱ 362 │   │   │   │   sub_tex_mob.move_to(self.submobjects[last_submob_index], RIGHT)            │
│   363 │   │   │   else:                                                                          │
│   364 │   │   │   │   sub_tex_mob.submobjects = self.submobjects[curr_index:new_index]           │
│   365 │   │   │   new_submobjects.append(sub_tex_mob)                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
IndexError: list index out of range
heafnerj commented 2 weeks ago

I discovered this problem today. The LaTeX bundle I maintain, mandi, requires lualatex so I hope this bug can be fixed.

uwezi commented 2 weeks ago

I believe we solved this already on Discord.... I haven't seen it here on Github before.

The problem is caused because in general all shapes generated by LaTeX and dvisvgm are filled paths with a zero outline, and Manim thus assumes that all stroke_widths should be zero for the resulting svg-import. However, in some rare circumstances lines are just included as lines in the LaTeX output.

Workaround: identify the glyph and set the stroke width accordingly - the following code is untested, because I have neither luatex nor the fira-font currently installed....

from manim import *

preamble = r"""
\usepackage[mathrm=sym]{unicode-math}
\setmathfont{Fira Math}
"""

template = TexTemplate(
    preamble = preamble, 
    tex_compiler = 'lualatex', 
    output_format='.pdf'
)

class MyScene(Scene):
    def construct(self):
        tex = Tex(r"$\frac{a}{b}$", tex_template = template)
        tex[0][1].set_stroke(width=3,color=WHITE)
        self.add(tex)
heafnerj commented 2 weeks ago

Thank you for the rapid reply! I was experimenting before seeing your post and discovered that commenting out the output_format='pdf' and using {a}\over{b} instead of \frac{a}{b} allows the bar to render. I will be away most of the day but will try your fix tonight. UPDATE: It works.