print("Hello, world!")Hello, world!
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.
You can include inline code snippets in between single backticks, e.g. `print("Hello, world!")` (this renders as print("Hello, world!")).
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!")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.
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!
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.
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.
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:
print("This code is initially hidden.")This code is initially hidden.
The folding code blocks are expanded in the static PDF output.
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.
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()
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()
```
Chunk options can be applied to every code block using the document YAML.
---
execute:
echo: false
warning: false
message: false
---