Introduction to Code Blocks

This is my reading note about features of code blocks in Quarto.

Basics of Code Blocks

In Quarto you can simply highlight code context using a code block. It is also possible if you would like to run the code in a code block and render the output in your Quarto notebook.

Language Support

Quarto, which is based on Pandoc, supports syntax highlighting for over 140 different languages. By specifying the {=language_name} syntax you can specify the language name. This also applies to inline code. Setting the language name is crucial if you would like to run the content in a code block in the Quarto runtime environment. See “Executable Code Blocks” for more information.

Code Execution Options

Code execution options can be used when you want to, e.g., regulate the code output, hide the code piece, and so on. Read the official guide about execution options for more detail.

Non-executable Code Blocks

For non-executable code blocks (e.g. output of a code, text from the man page, etc.), use the default indicator.

This is a plain text.

Learn more from the documentation.

Executable Code Blocks

Set Up Your Runtime Environment

Intuitively you need to install R language backend on your machine to render code written in R. Note that sometimes you need to install jupyter in order to render code that is written in other languages, such as Python and Bash.

Setting the {=language_name} indicator is also needed if you want to run the code in the Quarto runtime environment.

Displaying the Language Name for an Executable Code Block

The fenced echo is what you are looking for.

Reuse content from previous code block

Often you would like to split a code snippet in different code blocks, or you define a function that is used by different code blocks. This article covers the information about how to reference to the content in other code blocks.

Interactive Code Annotations

Interactive code annotation is a combination of inline comment and bullet points. The display of code annotation can be controlled by the variable code-annotations, which contains the following options:

  • below (default)
  • hover
  • select
  • none

Below is an example of using code annotation from the official guide about code annotation.

pacman::p_load(palmerpenguins)
1penguins |>
2    mutate(
        bill_ratio = bill_depth_mm / bill_length_mm,
        bill_area  = bill_depth_mm * bill_length_mm
    )
1
Take penguins, and then,
2
add new columns for the bill ratio and bill area.
# A tibble: 344 × 10
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
 1 Adelie  Torgersen           39.1          18.7               181        3750
 2 Adelie  Torgersen           39.5          17.4               186        3800
 3 Adelie  Torgersen           40.3          18                 195        3250
 4 Adelie  Torgersen           NA            NA                  NA          NA
 5 Adelie  Torgersen           36.7          19.3               193        3450
 6 Adelie  Torgersen           39.3          20.6               190        3650
 7 Adelie  Torgersen           38.9          17.8               181        3625
 8 Adelie  Torgersen           39.2          19.6               195        4675
 9 Adelie  Torgersen           34.1          18.1               193        3475
10 Adelie  Torgersen           42            20.2               190        4250
# ℹ 334 more rows
# ℹ 4 more variables: sex <fct>, year <int>, bill_ratio <dbl>, bill_area <dbl>

Customize the Style of Code Blocks

Code blocks of the same language highlight share the same CSS class, thus you can apply your own style format for code of different language. Learn more from the documentation.

You can also add customized class to a code block and adjusted its style individually, which is discussed in this Stack Overflow discussion.