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.
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
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:
# 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.
Source Code
---title: "Introduction to Code Blocks"description: "This is my reading note about features of code blocks in Quarto."---## Basics of Code BlocksIn 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 SupportQuarto, 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"](#executable-code-blocks) for more information.### Code Execution OptionsCode 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](https://quarto.org/docs/computations/execution-options.html) for more detail.## Non-executable Code BlocksFor non-executable code blocks (e.g. output of a code, text from the man page, etc.), use the `default` indicator. ```defaultThis is a plain text.```Learn more from [the documentation](https://quarto.org/docs/authoring/markdown-basics.html#raw-content).## Executable Code Blocks### Set Up Your Runtime EnvironmentIntuitively 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 BlockThe [fenced echo](https://quarto.org/docs/computations/execution-options.html#fenced-echo) is what you are looking for.### Reuse content from previous code blockOften 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](https://bookdown.org/yihui/rmarkdown-cookbook/reuse-chunks.html) covers the information about how to reference to the content in other code blocks.## Interactive Code AnnotationsInteractive 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* noneBelow is an example of using code annotation from [the official guide about code annotation](https://quarto.org/docs/authoring/code-annotation.html).```{r load packages}#| echo: false#| output: false# (install &) load packagespacman::p_load( conflicted, tidyverse)# handle function conflictsconflicts_prefer(dplyr::filter)conflicts_prefer(dplyr::lag)``````{r load data with annotation}pacman::p_load(palmerpenguins)penguins |> # <1> mutate( # <2> bill_ratio = bill_depth_mm / bill_length_mm, # <2> bill_area = bill_depth_mm * bill_length_mm # <2> ) # <2>```1. Take `penguins`, and then,2. add new columns for the bill ratio and bill area.## Customize the Style of Code BlocksCode 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](https://quarto.org/docs/authoring/markdown-basics.html#source-code).You can also add customized class to a code block and adjusted its style individually, which is discussed in [this Stack Overflow discussion](https://stackoverflow.com/a/74026889).