Skip to contents

Displays source code with syntax highlighting. Each line is a Text submobject. Tokens are colored according to a theme (default: monokai). Includes an optional background rectangle and line numbers.

Details

Syntax highlighting uses simple regex-based token classification. For R, Python, and JavaScript, common patterns (keywords, strings, comments, numbers) are recognized and colored.

Super classes

manimR::MObject -> manimR::VMobject -> Code

Public fields

code_string

The source code string

language

Programming language ("r", "python", "javascript", "plain")

theme

Color theme name

tab_width

Number of spaces per tab

font

Monospace font family

font_size

Font size

line_numbers

Whether to show line numbers

background_color

Background panel color

background_opacity

Background panel opacity

corner_radius

Background corner radius

line_objects

List of Text objects, one per line

background

Background rectangle VMobject

Methods

Inherited methods


Method new()

Create a Code block

Usage

Code$new(
  code_string,
  language = "r",
  theme = "monokai",
  font = "mono",
  font_size = 24,
  line_numbers = TRUE,
  tab_width = 4,
  position = ORIGIN,
  ...
)

Arguments

code_string

Source code string

language

Language for highlighting

theme

Color theme

font

Monospace font

font_size

Font size

line_numbers

Show line numbers

tab_width

Tab width in spaces

position

Position

...

Additional args


Method highlight_lines()

Highlight specific lines (set their opacity to 1, dim others)

Usage

Code$highlight_lines(line_indices, dim_opacity = 0.3)

Arguments

line_indices

Integer vector of line numbers to highlight (1-indexed)

dim_opacity

Opacity for non-highlighted lines


Method get_line()

Get a specific line object

Usage

Code$get_line(n)

Arguments

n

Line number (1-indexed)


Method num_lines()

Get the number of lines

Usage

Code$num_lines()


Method clone()

The objects of this class are cloneable with this method.

Usage

Code$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.