Cool stuff: Lab 00

cool-stuff
quarto
themes
keyboard-shortcuts

I want to start a series of posts that I will call “Cool stuff” that highlights some of the cool things you all are doing with Quarto, and some of the cool things you can do with Quarto.

Published

January 23, 2024

Modified

January 24, 2024

I’ll highlight a couple of things here, but I’d love to hear from you all about what you are doing with Quarto. If you have something you’d like to share, please let me know and I’ll highlight it here.

Changing Quarto document themes

Quarto documents are styled using themes. Without specifiying, a default theme is used. But if you want to experiment with different themes, you can do so by specifying a theme in front matter.

---
title: "My Document"
format:
  html:
    theme: cerulean
---

Themes are available for both HTML and PDF output, but they are not the same. Here you can find a list of HTML themes.

For PDF output, you need to specify the documentclass: in the front matter.

---
title: "My Document"
format:
  pdf:
    documentclass: report
---

The options are more limited for PDF output given the nature of the output but you can read more here.

PDF output gotcha

When Quarto documents are rendered to PDF, the output is generated using LaTeX. LaTeX is a typesetting language that is very powerful, but it can be a bit finicky and can produce some unexpected results.

One of the things that can be a bit confusing is how text that is wrapped in backticks is rendered. In LaTeX, text wrapped in backticks like `verbatim text` is rendered as verbatim text. This means that the text is rendered exactly as it is typed, including spaces and line breaks. This is not a problem until the text becomes too long to fit on a single line. When this happens, LaTeX will not wrap the text to the next line, but will instead render the text as a single line that extends beyond the page margins. Not what you expect.

The first piece of advice is to avoid wrapping long text in backticks that is part of a paragraph. Instead, separate the text into its own code formatted markdown block.

Say we have a long piece of text that we want to render as a verbatim string, like a the markdown code to display a link like this one: ![alt text](https://www.wfu.edu/this-is-a-really-long-url-i-guess-it-is-not-long-enough/index.html).

In a paragraph, it will be too long and LaTeX will not wrap it to the next line. Instead, we can separate it into its own code block1.

```markdown
![alt text](https://www.wfu.edu/this-is-a-really-long-url-i-guess-it-is-not-long-enough/index.html)
```
![alt text](https://www.wfu.edu/this-is-a-really-long-url-i-guess-it-is-not-long-enough/index.html)

In many cases this will be enough to solve side-step the problem. But if your verbatim text is still too long, then we can add some LaTeX code to the front matter to tell LaTeX to wrap the in code blocks. This is done by adding the following to the front matter:

---
title: "My Document"
format:
  pdf:
    include-in-header:
      - text: |
          \usepackage{fvextra}
          \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},breaklines,breaknonspaceingroup,breakanywhere}
---

This is definitely a bit more advanced, but it is a good example of how you can customize the output of your Quarto documents.

If more LaTeX customization is needed, you are best to create a new LaTeX document, say header.tex, and add the LaTeX code to that file. Then you can add the following to the front matter:

---
title: "My Document"
format:
  pdf:
    include-in-header: header.tex
---

Keyboard shortcuts in RStudio

RStudio has a number of keyboard shortcuts that can be used to speed up your workflow. You can find a list of them here.

For starters, here are the ones I use the most to work with Quarto and R:

Symbols used in keyboard shortcuts
Symbol Key

Shift

Shift key

Ctrl

Control key

Command

Command key
(Mac only)

Alt

Alt key

Option

Option key
(Mac only)

Esc

Escape key

Tab

Tab key

Enter

Enter key
For Quarto document elements
Description Shortcut
Render Quarto documents

Add a code block to a Quarto document

For R code
Description Shortcut
To invoke code completion when typing R code

Run current line or selection in the Console

To comment or uncomment a line or selection so that it is or is not run as R code

To insert the <- operator to assign code output to a variable

To insert a |> operator to pipe the output of one operation to the input of the next

To reformat R code so that indentation is more legible

Footnotes

  1. Note that a code block that has the language, markdown in this case, without curly braces {} means to show the code highlighting but not to execute the code. In some ways it is equivalent to #| eval: false.↩︎