3
txt11 = TexMobject(r"-7", color=BLACK)
txt12 = TexMobject(r"\frac{1}{7}", color=BLACK).next_to(txt11, RIGHT)

I wanna color the denominator of txt12, tried splitting txt12 to:

txt12 = TexMobject(r"\frac{1}", r"{7}", color=BLACK).next_to(txt11, RIGHT)
txt12.set_color_by_tex("{7}", BLUE)

but not work, then I create a new txt13 overlapped with txt12 but a null molecular:

txt13 = TexMobject(r"\frac{}{7}", color=BLUE).move_to(txt12.get_center())

also not work.

Is there feasible way to color part of equation like fraction? thanks!

James Hao
  • 765
  • 2
  • 11
  • 40

1 Answers1

6

Specifically for a fraction, you can use

TexMobject(r"1", r"\over", r"7").set_color_by_tex("7", BLUE)

set_color_by_tex("7", BLUE) sets the submobject with text 7 to BLUE.

The LaTeX {a \over b} command is a shorthand for \frac{a}{b}.

This is how Grant Sanderson sometimes do it, for example, in this recent video.

hzahnuwen
  • 136
  • 7