A highlighted source code listing. Examples -------- Normal usage:: listing = Code( "helloworldcpp.cpp", tab_width=4, formatter_style="emacs", background="window", language="cpp", background_config={"strok
| 26 | |
| 27 | |
| 28 | class Code(VMobject, metaclass=ConvertToOpenGL): |
| 29 | """A highlighted source code listing. |
| 30 | |
| 31 | Examples |
| 32 | -------- |
| 33 | |
| 34 | Normal usage:: |
| 35 | |
| 36 | listing = Code( |
| 37 | "helloworldcpp.cpp", |
| 38 | tab_width=4, |
| 39 | formatter_style="emacs", |
| 40 | background="window", |
| 41 | language="cpp", |
| 42 | background_config={"stroke_color": WHITE}, |
| 43 | paragraph_config={"font": "Noto Sans Mono"}, |
| 44 | ) |
| 45 | |
| 46 | We can also render code passed as a string. As the automatic language |
| 47 | detection can be a bit flaky, it is recommended to specify the language |
| 48 | explicitly: |
| 49 | |
| 50 | .. manim:: CodeFromString |
| 51 | :save_last_frame: |
| 52 | |
| 53 | class CodeFromString(Scene): |
| 54 | def construct(self): |
| 55 | code = '''from manim import Scene, Square |
| 56 | |
| 57 | class FadeInSquare(Scene): |
| 58 | def construct(self): |
| 59 | s = Square() |
| 60 | self.play(FadeIn(s)) |
| 61 | self.play(s.animate.scale(2)) |
| 62 | self.wait()''' |
| 63 | |
| 64 | rendered_code = Code( |
| 65 | code_string=code, |
| 66 | language="python", |
| 67 | background="window", |
| 68 | background_config={"stroke_color": "maroon"}, |
| 69 | ) |
| 70 | self.add(rendered_code) |
| 71 | |
| 72 | Parameters |
| 73 | ---------- |
| 74 | code_file |
| 75 | The path to the code file to display. |
| 76 | code_string |
| 77 | Alternatively, the code string to display. |
| 78 | language |
| 79 | The programming language of the code. If not specified, it will be |
| 80 | guessed from the file extension or the code itself. |
| 81 | formatter_style |
| 82 | The style to use for the code highlighting. Defaults to ``"vim"``. |
| 83 | A list of all available styles can be obtained by calling |
| 84 | :meth:`.Code.get_styles_list`. |
| 85 | tab_width |