ManimCommunity / ManimCommunity/manim

Manim's unexpected colouring behaviour under the radical sign

Open
#1,996 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

issue:bug
Dominant language
Python
Stars
40.9k
Forks
3.1k
Avg merge
3d 12h
Merged PRs (30d)
25

Description

## Description of bug / unexpected behavior
Hi,

I want to color the two's in purple and the three's in yellow by running these code on manim:

**first code**
```py
import numpy as np

class vectorTry(Scene):
def construct(self):
val_a = 3
val_b = 2
color_a = "#f1f514"
color_b = "#cf0492"
tex_a = Tex(str(val_a).format(int), color=color_a).move_to([-1,2,0]).scale(2)
tex_b = Tex(str(val_b).format(int), color=color_b).move_to([1,2,0]).scale(2)

self.play(FadeIn(tex_a, tex_b))
self.wait()

#using \over because \frac fails here
form = MathTex(r'\hat{u}= { 3 \hat{i} + 2\hat{j} \over{ \sqrt{ 2 ^{2} + 3 ^{2} } } }',
substrings_to_isolate=["2", "3"]).scale(1.25)

idx_a = [int(i) for i, character in enumerate(form) if character.get_tex_string() == tex_a[0].get_tex_string()]
idx_b = [int(i) for i, character in enumerate(form) if character.get_tex_string() == tex_b[0].get_tex_string()]

for i in range(len(form)):
print(form[i])

print(idx_a)
print(idx_b)

get_pos_a = form[idx_a[0]].get_center()
get_pos_b = form[idx_b[0]].get_center()
a1_copy = []
b1_copy = []

#here I force colouring of two's and three's on the MathTex object
for i in range(len(idx_a)):
a1_copy += form[idx_a[i]].set_color(color_a)
for i in range(len(idx_b)):
b1_copy += form[idx_b[i]].set_color(color_b)

self.play(FadeIn(form))
self.play(tex_a.animate.move_to(get_pos_a).match_height(form[idx_a[0]]))
self.play(tex_b.animate.move_to(get_pos_b).match_height(form[idx_b[0]]))
self.wait(1)
```

or

**second code**
```py
import numpy as np

class vectorTry(Scene):
def construct(self):
val_a = 3
val_b = 2
color_a = "#0470cf"
color_b = "#cf0492"
tex_a = Tex(str(val_a).format(int), color=color_a).move_to([-1,2,0]).scale(2)
tex_b = Tex(str(val_b).format(int), color=color_b).move_to([1,2,0]).scale(2)

self.play(FadeIn(tex_a, tex_b))
self.wait()

#using \over because \frac fails here
form = MathTex(r'\hat{u}= { 3 \hat{i}+ 2\hat{j} \over{ \sqrt{ {2^{2}+3^{2} } } } }',
substrings_to_isolate=["2", "3"]).scale(1.25)

idx_a = np.array([i for i, character in enumerate(form) if character.get_tex_string() == tex_a[0].get_tex_string()],
dtype='int')
idx_b = np.array([i for i, character in enumerate(form) if character.get_tex_string() == tex_b[0].get_tex_string()],
dtype='int')

get_pos_a = form[idx_a[0]].get_center()
get_pos_b = form[idx_b[0]].get_center()

#using the set_color methods of MathTex object
form.set_color_by_tex(str(val_a).format(int), color_a)
b1_copy = form[idx_b[0]].set_color(color_b)

self.play(FadeIn(form))
self.play(tex_a.animate.move_to(get_pos_a).match_height(form[idx_a[0]]))
self.play(tex_b.animate.move_to(get_pos_b).match_height(form[idx_b[0]]))
self.wait(1)
```

Fails to color the two's and three's below the radical correctly.

## Expected behavior
The two's below the radical should have coloured in purple, the three's in yellow.

## Additional Info

Printing the indices of two's and three's seems that manim is aware of the indices of the numbers.

```py
for i in range(len(form)):
print(form[i])

>>> SingleStringMathTex('\\hat{u}= { ')
SingleStringMathTex('3')
SingleStringMathTex(' \\hat{i} + ')
SingleStringMathTex('2')
SingleStringMathTex('\\hat{j} \\over{ \\sqrt{ ')
SingleStringMathTex('2')
SingleStringMathTex(' ^{')
SingleStringMathTex('2')
SingleStringMathTex('} + ')
SingleStringMathTex('3')
SingleStringMathTex(' ^{')
SingleStringMathTex('2')
SingleStringMathTex('} } } }')

print(idx_a)
print(idx_b)

>>> [1, 9]
[3, 5, 7, 11]
```

## Additional media files
Whether I run them in the discord page of manim or their jupyter binder, the result is the same.

**first code output**
https://user-images.githubusercontent.com/47322232/131674737-1ccf5afa-a4f8-49cc-b986-47f1f19ffcc5.mp4

**second code output**
https://user-images.githubusercontent.com/47322232/131674520-c001f054-c73b-404d-b7a4-b77a84382de7.mp4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the two provided MathTex examples, then start at the MathTex entry point and the set_color_by_tex and set_color calls used in the report. Done means the occurrences of 2 and 3 below the radical receive the requested colors consistently; the payload names no source files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.