8  Code

Quarto supports executable code, code examples, and syntax-highlighted snippets for a wide range of programming languages. This makes it ideal for creating teaching materials, laboratory manuals, lecture notes, and technical documentation.

8.1 Including inline code snippets

You can include inline code snippets in between single backticks, e.g. `print("Hello, world!")` (this renders as print("Hello, world!")).

8.2 Including code blocks

You can include code blocks in triple backticks, followed by the language name (for syntax highlighting). For example:

```python

print("Hello, world!")

```

This results in the following rendered code output with syntax highlighting:

print("Hello, world!")
Tip

Supported languages for syntax highlighting include: Python, R, Bash, C, C++, Java, JavaScript, MATLAB, markdown and many more. See the Quarto documentation for a full list of supported languages.

For completely unformatted text use the ```text option.

8.3 Running Code

Executable code is placed inside a code chunk (notice the curly braces after the opening triple backticks). You can specify the programming language for the code chunk (e.g. python, r, julia, bash, etc.) and Quarto will automatically highlight the syntax.

```{python}

print("Hello, world!")

```

This results in the following rendered output (shows code and output):

print("Hello, world!")
Hello, world!

8.4 Options for running code

You can control how code is executed and displayed using chunk options. For example, you can hide the code but show the output, or hide both the code and the output. You can also control whether warnings and messages are displayed.

You can set chunk options for individual code chunks using the #| syntax directly after the opening triple backticks.

8.4.1 Showing output but hiding code

You can run the code and display the output, but hide the code source from viewers with #| echo: false

```{python}

#| echo: false

print("Only the output will appear.")

```

This results in the following rendered output:

Only the output will appear.

8.4.2 Folding Long Code Blocks

Long code examples can be collapsed to avoid cluttering the document with #| code-fold: true. You can also add a summary to the code block with #| code-summary: "Show Python code".

```{python}

#| code-fold: true

#| code-summary: "Show Python code"

print("This code is initially hidden.")

```

This results in the following rendered output:

Show Python code
print("This code is initially hidden.")
This code is initially hidden.
Note

The folding code blocks are expanded in the static PDF output.

Tip

It’s a good idea to keep code lines short (e.g. < 80 characters) as the LaTeX output does not wrap long lines of code.

8.5 Figures from code

Figures generated from Python, R, or Julia code are automatically treated as figures. To make these accessible, you should include:

  • #| label: fig-line-plot - this gives a reference label to the figure
  • #| fig-cap: "A simple line plot" - this gives a figure caption
  • #| fig-alt: "Descriptive alt text" - this gives alternative text for accessibility (e.g. screen readers)

For example, the following code chunk generates a simple line plot:

```{python}

#| label: fig-line-plot

#| fig-cap: "A simple line plot"

#| fig-alt: "Line graph showing values increasing and decreasing across four observations."

import matplotlib.pyplot as plt

plt.plot([1, 23, 2, 4])

plt.show()

```

This results in the following rendered output:

import matplotlib.pyplot as plt

plt.plot([1, 23, 2, 4])
plt.show()
Line graph showing values increasing and decreasing across four observations.
Figure 8.1: A simple line plot
Tip

Often it’s useful to add #| echo: false (see Section 8.4.1) at the start of the figure. This makes sure to not echo the code and only display the figure:

```{python}

#| echo: false

#| label: fig-line-plot-noecho

#| fig-cap: "A simple line plot"

#| fig-alt: "A simplified plot with the#| echo: falseto not show the code"

import matplotlib.pyplot as plt

plt.plot([1, 23, 2, 4])

plt.show()

```

A simplified plot with the `#| echo: false` to not show the code
Figure 8.2: A simple line plot

8.6 Global Code Options

Chunk options can be applied to every code block using the document YAML.

---
execute:
  echo: false
  warning: false
  message: false
---