Skip to content

Rendered submobjects of MathTex may not correspond to their tex strings #3548

@0x13b

Description

@0x13b

Description of bug / unexpected behavior

Tex string of SingleStringMathTex submobject of MathTex object does not match its rendered symbol.

The problem appears when using subscripts and superscripts in latex formula.

For example, the two objects eq1 = MathTex('A', '^n', '_1') and eq2 = MathTex('A', '_1', '^n') produce identical symbols n when rendering eq1[1], eq2[1], though the corresponding tex strings are different ('^n' and '_1').

UPD: Dealing with MathTex('\sum', '^n', '_1', 'x') does not produce correct order of terms and their svg submobjects neither for '^' going first, nor for '_' going first. See MinimalWithSum Scene example.

Expected behavior

Tex strings of every SingleStringMathTex submobject of MathTex should correspond to their rendered symbol.

How to reproduce the issue

Code for reproducing the problem
from manim import *

class Minimal(Scene):
    def construct(self):
        """ This shows that substring may not correspond to tex shape """
        eq1 = MathTex('A', '^n', '_1')  # expected behavior
        eq2 = MathTex('A', '_1', '^n')  # unexpected behavior
        eq1[1].set_color(RED)
        eq2[1].set_color(RED)

        font = {'font_size': 24}
        txt1 = Text(eq1[1].get_tex_string(), color=RED, **font) # '^n'
        txt2 = Text(eq2[1].get_tex_string(), color=RED, **font) # '_1'

        cap1 = Text('tex rendered', **font)
        cap2 = Text('tex substring', **font)
        
        grp = VGroup(cap1, cap2, eq1, txt1, eq2, txt2).arrange_in_grid(3,2)
        grp.scale(2).move_to(ORIGIN)
        self.add(grp)


class MinimalWithSum(Scene):
    def construct(self):
        """ This shows that substring may not correspond to tex shape """
        t2cm = {'\sum': BLUE, '^n': RED, '_1': GREEN, 'x':YELLOW}
        eq1 = MathTex('\sum', '^n', '_1', 'x', tex_to_color_map=t2cm)  # unexpected behavior, first 2 terms messed up
        eq2 = MathTex('\sum', '_1', '^n', 'x', tex_to_color_map=t2cm)  # unexpected behavior, first 3 terms messed up

        font = {'font_size': 24}
        txts = [Text(sub.get_tex_string(), t2c=t2cm, **font) for sub in (eq1, eq2) for i in range(len(sub))]
        txt1 = VGroup(*txts[:4])
        txt2 = VGroup(*txts[4:])

        cap1 = Text('tex rendered', **font)
        cap2 = Text('tex substrings', **font)
        
        grp = VGroup(cap1, cap2, eq1, txt1, eq2, txt2).arrange_in_grid(3,2)
        grp.scale(2).move_to(ORIGIN)
        self.add(grp)

Additional media files

Images/GIFs

Minimal_ManimCE_v0 18 0

MinimalWithSum_ManimCE_v0 18 0

Logs

Terminal output
Manim Community v0.18.0

[12/26/23 02:29:27] INFO                                                                       scene_file_writer.py:735
                             File ready at
                             'D:\manim\media\images\test_bug\Minimal_ManimCE_v0.18.0.png'

                    INFO     Rendered Minimal                                                              scene.py:241
                             Played 0 animations
                    INFO     Previewed File at:                                                         file_ops.py:227
                             'D:\manim\media\images\test_bug\Minimal_ManimCE_v0.18.0.png'

System specifications

System Details
  • OS (with version, e.g., Windows 10 v2004 or macOS 10.15 (Catalina)): Windows 10 v22H2
  • RAM 32:
  • Python version (python/py/python3 --version): 3.11.3
  • Installed modules (provide output from pip list):
Package                   Version
------------------------- ----------
asttokens                 2.0.5
attrs                     23.1.0
backcall                  0.2.0
backports.cached-property 1.0.2
brotlipy                  0.7.0
build                     0.10.0
CacheControl              0.12.11
certifi                   2023.11.17
cffi                      1.15.1
charset-normalizer        3.1.0
cleo                      2.0.1
click                     8.1.3
click-default-group       1.2.2
cloudpickle               2.2.1
cloup                     0.13.1
colorama                  0.4.6
colour                    0.1.5
comm                      0.1.2
crashtest                 0.4.1
cryptography              38.0.4
dataclasses               0.8
debugpy                   1.5.1
decorator                 5.1.1
distlib                   0.3.6
dulwich                   0.21.5
executing                 0.8.3
filelock                  3.12.0
future                    0.18.3
glcontext                 2.3.7
html5lib                  1.1
idna                      3.4
importlib-metadata        6.6.0
importlib-resources       5.12.0
installer                 0.7.0
ipykernel                 6.19.2
ipython                   8.12.0
isosurfaces               0.1.0
jaraco.classes            3.2.3
jedi                      0.18.1
jsonschema                4.17.3
jupyter_client            8.1.0
jupyter_core              5.3.0
keyring                   23.13.1
lockfile                  0.12.2
manim                     0.18.0
ManimPango                0.5.0
mapbox-earcut             1.0.0
markdown-it-py            2.2.0
matplotlib-inline         0.1.6
mdurl                     0.1.0
moderngl                  5.8.2
moderngl-window           2.4.1
more-itertools            9.1.0
msgpack                   1.0.5
multipledispatch          0.6.0
nest-asyncio              1.5.6
networkx                  2.8.8
numpy                     1.24.3
packaging                 23.1
parso                     0.8.3
pexpect                   4.8.0
pickleshare               0.7.5
Pillow                    9.5.0
pip                       23.0.1
pkginfo                   1.9.6
pkgutil_resolve_name      1.3.10
platformdirs              3.5.1
poetry                    1.5.0
poetry-core               1.6.0
poetry-plugin-export      1.3.1
pooch                     1.7.0
prompt-toolkit            3.0.36
psutil                    5.9.0
ptyprocess                0.7.0
pure-eval                 0.2.2
pycairo                   1.23.0
pycparser                 2.21
pydub                     0.25.1
pyglet                    1.5.27
Pygments                  2.15.1
pyOpenSSL                 23.1.1
pyproject_hooks           1.0.0
pyrr                      0.10.3
pyrsistent                0.19.3
PySocks                   1.7.1
python-dateutil           2.8.2
pywin32                   305.1
pywin32-ctypes            0.2.0
PyYAML                    6.0
pyzmq                     25.0.2
rapidfuzz                 2.15.1
requests                  2.31.0
requests-toolbelt         1.0.0
rich                      13.3.5
scipy                     1.10.1
screeninfo                0.8.1
setuptools                66.0.0
shellingham               1.5.1
six                       1.16.0
skia-pathops              0.7.4
spyder-kernels            2.4.3
srt                       3.5.2
stack-data                0.2.0
svgelements               1.9.5
tomli                     2.0.1
tomlkit                   0.11.8
tornado                   6.2
tqdm                      4.65.0
traitlets                 5.7.1
trove-classifiers         2023.5.22
typing_extensions         4.9.0
urllib3                   1.26.15
virtualenv                20.23.0
watchdog                  2.2.1
wcwidth                   0.2.5
webencodings              0.5.1
wheel                     0.38.4
win-inet-pton             1.1.0
zipp                      3.15.0
LaTeX details
  • LaTeX distribution (e.g. TeX Live 2020): TeX Live 2021
  • Installed LaTeX packages:

latex packages.txt

</details>

## Additional comments
<!-- Add further context that you think might be relevant for this issue here. -->

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    🆕 New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions