01. Academic writing with Quarto
Key Quarto features for academic writing
The implementation of literate programming we are using in this course is Quarto with R. As we have seen in previously, Quarto provides the ability to combine prose and code in a single document. This is a powerful strategy for creating reproducible documents that can be easily updated and shared.
Concepts and strategies
For many of the style components that we use in Quarto, there is a part that is addressed in the front-matter section and a part that is addressed in the prose section and/ or code block sections.
To refresh our memory, the front-matter is fenced by three dashes (---
) and is where we set the document attributes. The prose section is where we write the text of the document. The code block section is where we write the code that will be executed and is fenced by three backticks (```
) and the name of the code interpreter {r}
(R for us).
With the basic structure in Snippet 1 in mind let’s look at each of these elements in turn.
Numbered sections
To number sections in Quarto, we use the number_sections
key with the value yes
. This is set in the front-matter section, nested under the value for the document type to be rendered. For example, to number sections in a PDF document, we would set the number-sections
key to true
in the front-matter section as in Snippet 2.
Headers in the prose section are then numbered automatically. For example, the markdown in Snippet 3.
would render as:
We can also control the depth of the numbering by setting the number-depth
key in the front-matter section. For example, to number sections and subsections, but not subsubsections, we would set the number-depth
key to 2
as in Snippet 4.
Now the first and second headers are numbered and formated but third and subsequent headers are only formatted.
If for some reason you want to turn off numbering for a specific header, you can add {.unnumbered}
to the end of the header. For example, the markdown in Snippet 5.
This is particularly useful in academic writing when we want to add a reference, materials, or other section that is not numbered at the end of the document.
Table of contents
For longer documents including a table of contents can be a useful way to help readers navigate the document. To include a table of contents in Quarto, we use the toc
key with the value true
. Again, in the front-matter section, nested under the format value, as seen in Snippet 6.
If if our headers are numbered, they will appeared numbered in the table of contents. If we unnnumbered a header, it will not appear with a section number. As with section numbering, we can also control the depth of the table of contents by setting the toc-depth
key in the front-matter section. For example, to include sections and subsections, but not subsubsections, we would set the toc-depth
key to 2
as in Snippet 7.
And as with section numbering we can avoid listing a header in the table of contents by adding {.unlisted}
to the end of the header.
Cross-referencing tables and figures
Another key element in academic writing are using cross-references to tables and figures. This allows us to refer to a table or figure by number without having to manually update the number if we add or remove a table or figure.
In this case, we will not need to add anything to the front-matter section. Instead, we will modify keys in the code block section of a code-generated table or figure.
To cross-reference a table or figure, we need to add a prefix to the label
key’s value. The prefix, either tbl-
or fig-
, indicates whether the label is for a table or figure. Additionally, table or figure captions can be added with the tbl-cap
or fig-cap
keys, respectively.
Let’s look at a basic figure that we can cross-reference. The following code block will generate a very simple scatterplot.
For tables generated by R, the process is very similar to that of figures. The only difference is that we use the tbl-
prefix on the label value and the tbl-cap
key instead of the fig-cap
key for the caption.
We can also create tables using markdown syntax. In this case, the format is a little different. Consider Table 1, for example.
| Column 1 | Column 2 | Column 3 |
|:---------|:---------|:---------|
| A | B | C |
| D | E | F |
: A simple table {#tbl-table-1}
In-line citations and references list
The last element we will cover here is adding citations and a references list to a Quarto document. To add citations we need three things:
- A bibliography file
- A reference to the bibliography file in the front-matter section
- A citation in the prose section which is contained in the bibliography file
The bibliography file is a plain text file that contains the citations that we want to use in our document. The file requires the extension .bib
and is formatted using the BibTeX format. BibTeX is a reference syntax that is commonly used in academia.
Let’s take a look at a snippet of a sample file, bibliography.bib
, that contains a single reference in Snippet 9.
In this file, we can see that the reference is for an entry with the type @Manual
. The type of entry will change what fields are relevant and/ or required. In this entry, we have the cite key R-dplyr
, the title, the authors, the year, a note, and a URL. Other entries, and entry types will have different fields.
You can find BibTeX formatted references almost everywhere you can find scholarly work. For example, Google Scholar, Web of Science, and Scopus all provide BibTeX formatted references. Additionally, many journals provide BibTeX formatted references for the articles they publish.
In the front-matter of our Quarto document, we need to add a reference to the bibliography file. This is done using the bibliography
key. For example, if our bibliography file is called bibliography.bib
and is located in the same directory as our Quarto document, we would add the front-matter section in Snippet 10.
With the bibliography file and the reference to the bibliography file in the front-matter section, we can now add citations to our document. To do this, we use the @
symbol followed by the citation key in the prose section. For example, to cite the R-dplyr
reference from the bibliography.bib
file, we would add @R-dplyr
to the prose section as in Snippet 11.
The citation will appear as below in the rendered document.
And automatically, on rendering the document, a references list will be added to the end of the document. For this reason if you have citations in your document, it is a good idea to include a header section # References
at the end of your document.
Check your understanding
Consider the following front-matter sections, A and B.
Choose whether the following statements are true or false.
- Section numbering will be included in the PDF output for both A and B.
- Section numbering will be applied to the first three levels of headers in the PDF output for both A and B.
- A table of contents will be included in the PDF output for both A and B.
- A table of contents will be included in the PDF output for B, but will only include the first two levels of headers.
Now respond to the following questions.
- will cross-reference a figure with the label
fig-scatterplot
. - is the front-matter key to include a path to the file which contains the BibTeX formatted references.
Lab preparation
This rounds out our introduction to academic writing in Quarto. In Lab 01 you will have an opportunity to practice these concepts by doing an article summary which includes some of these features using Quarto.
In preparation for Lab 01, ensure that you are prepared to do the following:
- Edit the front-matter section of a Quarto document to render:
- a PDF document or a Word document
- a document with numbered sections
- a document with a table of contents
- a document with a path to a bibliography file
- Add an inline citation to the prose section of a Quarto document
Also, since you will do an article summary, you should be prepared with:
- an article of interest1 related to text analysis that you have read or at least skimmed for the following:
- the research question
- the data used
- the methods used
- the results/ findings of the study
- a BibTeX formatted reference for the article