fix long text and distorted Text in manim

264 Views Asked by At

I have a long text in manim and it renderes very distorted. in fact, it not readable when it renders.

can you fix it please


# fast test for manim
from manim import *
from manim_ml.neural_network.layers import FeedForwardLayer
from manim_ml.neural_network.neural_network import NeuralNetwork

config.pixel_height = 900
config.pixel_width = 1400
config.frame_height = 7.0
config.frame_width = 7.0



class test(Scene):
    def construct(self):


        # Make the text
        NN_text ="""
            import keras
            from keras.models import Sequential
            from keras.layers import Dense
            model = Sequential ()
            n_cols = concrete_data. shape [1]
            model.add (Dense (5, activation=\'relu\',
            model.add (Dense(5, activations'relu\', input_shape=(n_ (cols, )))
            model.add (Dense (1))
            model.compile (optimizer=\'adam\', loss=\'mean_squared_error\')
            model.fit (predictors, target)
            predictions = model.predict(test_data)}
        """
        desc = Text(NN_text,font_size=7,
            t2c={"import": YELLOW, "from":RED,"add":GREEN,"model":BLUE,"compile":PURPLE,"fit":YELLOW,"predict":RED},
            disable_ligatures=True,)
        desc=desc.next_to(ORIGIN)
        self.add(desc)   
        self.play(Write(desc))

        

The screenshot of rendered file

proper text with nice alignment.

1

There are 1 best solutions below

1
On

There is a function code in manim for handling the text for codes.

NN_text ="""
import keras
from keras.models import Sequential
from keras. layers import Dense
model = Sequential ()
n_cols = concrete_data. shape [1]
model. add (Dense (5, activation='relu',
model. add (Dense(5, activations' reluj, input_shape=(n_ (cols, )))
model.add(Dense (1))
model. compile (optimizer='adam', loss='mean_squared_error')
model.fit (predictors, target)
predictions = model.predict(test_data)
"""
class code(Scene):
    def construct(self):
        codeText = Code(
            code = NN_text,
            tab_width=4,
            background_stroke_width=1,
            background_stroke_color=WHITE,
            insert_line_no=False,
            style=Code.styles_list[15],
            background="window",
            language="python",  
            font="consolas",
            font_size=18,          
        )    
        self.play(Write(codeText), run_time=5)  
        self.wait()  
        for obj in codeText[2]:
            self.play(Wiggle(obj))