You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复Manim中长文本渲染扭曲、无法读取的问题

问题

使用Manim渲染长代码文本时出现严重扭曲,文本完全无法正常读取。测试代码如下:

# 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))
解决方案

核心问题分析

原代码使用Text组件渲染代码文本,Text更适配普通排版文本,对代码的等宽字体、换行对齐支持不足;加上代码文本本身存在缩进冗余和语法错误,最终导致渲染扭曲。

修复步骤

  • 替换为Code组件:Manim的Code组件专为代码展示设计,自带等宽字体、自动对齐和语法高亮支持,可直接解决文本扭曲问题。
  • 修正代码文本的语法与缩进:清理原代码文本中的语法错误(如缺失符号、错误变量名),移除冗余缩进避免渲染偏移。
  • 调整显示参数:根据帧尺寸合理设置字体大小,保证文本清晰可读。

修改后的完整代码

# 修复后的测试代码
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):
        # 修正后的代码文本,修复语法错误并调整缩进
        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, activation='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)
"""
        # 使用Code组件渲染代码,设置合适的字体大小与高亮
        desc = Code(
            code=NN_text,
            language="python",
            font_size=14,
            line_numbers=False,
            # 自定义语法高亮颜色,匹配原需求的颜色设置
            style={
                "keyword": YELLOW,  # 对应import、from
                "name.class": BLUE,  # 对应model相关
                "name.function": GREEN,  # 对应add
                "name.builtin": PURPLE,  # 对应compile
                "name.decorator": YELLOW,  # 对应fit
                "name.variable": RED  # 对应predict
            }
        )
        desc.move_to(ORIGIN)
        self.add(desc)
        self.play(Write(desc))

内容的提问来源于stack exchange,提问作者CSe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:20:47