ManimCommunity / manim

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

Tex-Mobject width and font_size arguments affect eachother #3875

Closed JonasFovea closed 4 months ago

JonasFovea commented 4 months ago

Description of bug / unexpected behavior

I would like to have a Tex-Mobject with a specific width and font size, so that the lines fill the full width.

So I specified both the width and the font size. But the text is either scaled to fit the width, or the font size is correct, but the text is wider, than specified.

Expected behavior

Text of the specified width with the specified font size and added linebreaks if necessary.

How to reproduce the issue

Code for reproducing the problem ```py Tex(r"The \emph{same} point $\mathbf{p}$ can be expressed in two coordinate systems " r"by a linear combination of the respective base vectors.", width=120, font_size=60, tex_environment='flushleft') ```

Additional media files

Part of what is redered with the width set to 120 and fontsize of 60 (text is too wide and font is too small): image Removing the font size, but keeping the width (text is too small): image

Images/GIFs

Logs

Terminal output ``` PASTE HERE OR PROVIDE LINK TO https://pastebin.com/ OR SIMILAR ```

System specifications

System Details - OS (with version, e.g., Windows 10 v2004 or macOS 10.15 (Catalina)): - RAM: - Python version (`python/py/python3 --version`): - Installed modules (provide output from `pip list`): ``` PASTE HERE ```
LaTeX details + LaTeX distribution (e.g. TeX Live 2020): + Installed LaTeX packages:

Additional comments

uwezi commented 4 months ago

Once LaTeX has rendered your Tex() objects no letters or words will ever be able to move around anymore. If you want to typeset text with a certain running length of the text lines, you will have to do this within your Tex() object, for example by using the minipage environment of LaTeX.

from manim import *

class textex(Scene):
    def construct(self):
        t1 = Tex(r"This is plain text mode\\ in two centered lines.").to_edge(UP, buff=0)
        self.add(t1)
        t2 = Tex(
            r"This is a longer text block in a so-called minipage environment allowing for justified margins. And just because I can I also throw in some math $E=mc^2$.",
            tex_environment="{minipage}{7cm}"
        ).set_color(YELLOW).scale_to_fit_width(8).next_to(t1,DOWN)
        t3 = Tex(
            r"\raggedright This is a longer text block in a so-called minipage environment allowing for justified margins. And just because I can I also throw in some math $E=mc^2$.",
            tex_environment="{minipage}{7cm}"
        ).set_color(YELLOW).scale(0.5).next_to(t2,DOWN)
        t4 = Tex(
            r"\raggedleft This is a longer text block in a so-called minipage environment allowing for justified margins. And just because I can I also throw in some math $E=mc^2$.",
            tex_environment="{minipage}{7cm}"
        ).set_color(YELLOW).scale(0.5).next_to(t3,DOWN)
        t5 = Tex(
            r"\centering This is a longer text block in a so-called minipage environment allowing for justified margins. And just because I can I also throw in some math $E=mc^2$.",
            tex_environment="{minipage}{7cm}"
        ).set_color(YELLOW).scale(0.5).next_to(t4,DOWN)
        self.add(t2,t3,t4,t5)
        t6 = MathTex(
            r"Q = \sum\limits_{i=1}^{n}i = \frac{n\left(n-1\right)}{2} \quad\text{C.\ F.\ Gauss}"
        ).set_color(RED).next_to(t5,DOWN)
        self.add(t6)

bild

uwezi commented 4 months ago
class jonasfovea(Scene):
    def construct(self):
        text = Tex(r"""
{20em}The \emph{same} point $\mathbf{p}$ can be expressed
in two coordinate systems
by a linear combination of the respective base vectors.""",
            font_size=60,
            tex_environment='minipage')
        self.add(text)

bild

uwezi commented 4 months ago

just a note of caution: in Manim 0.18.1 it is not possible to use tex_environment="{minipage}{7cm}" because of a bug which hopefully will be gone in the upcoming 0.19. Instead you need to use the syntax from my second example, which would also work in future versions of Manim...

JonasFovea commented 4 months ago

Ah okay, that makes sense!

Thanks a lot!