6  Cross-referencing

In this chapter, you will
  • learn how to make cross-references to figure, tables, document sections etc.

Cross-referencing lets you refer to figures and tables in your document. Once set up, this happens automatically, so you don’t need to re-number all the figures when you add or delete one.

6.1 Cross-referencing figures and tables

The block that made the figure or table needs to have a label that starts with fig- or tbl-, respectively. The figure or table also need a caption.

#| label: fig-height-violin
#| fig-cap: Violin plot of plant heights
#| fig-alt: Violin plot of plant heights made with ggplot2
#| warning: false

trait_height |>
  ggplot(aes(x = Treatment, y = Value)) +
  geom_violin(fill = "grey") +
  labs(y = "Plant Height cm")

Violin plot of plant heights made with ggplot2

Figure 6.1: Violin plot of plant heights

To cross-reference the figure made by the above chunk, we need to use this notation.

Plant traits are cool (@fig-height-violin)

Plant traits are cool (Figure 6.1)

If you type an @ in the visual editor it will make suggestion. If, in the visual editor, it does not show up in a smaller blue font, something has gone wrong. Delete it and make sure there is a space before the @.

To refer to a table made by a block labelled tbl-traits-results, you would use

Plant traits are cool (@tbl-traits-results))

If you get the chunk name wrong, there will be a warning when the document in knitted, and question marks shown where the reference should be.

Non-existing (@fig-no-exist)

Non-existing (?fig-no-exist)

6.2 Cross-referencing equations

To cross-reference an equation, we need to give it a label.


Now the label can be used.

The mean (@eq-mean).

\[ \frac{\sum_{i=1}^{n}{x_i}}{n} \tag{6.1}\]

The mean (Equation 6.1).

6.3 Cross-referencing document sections

You can also make cross-references to other sections of the document. First, you need to set up a identifier for the section you want to link to. In the visual editor, this can be done by clicking on the three dots to the right of the header (Figure 6.2) . The ID should start with #sec- and be in lower case (Figure 6.3). Now you can use this ID to make a link by typing @.

Screenshot of the three dots that get the Edit Attributes menu

Figure 6.2: Click on the three dots to get the Edit Attributes menu

Screenshot of the Edit Attributes menu

Figure 6.3: The Edit Attributes menu

In the source editor, it will look like this.

## Cross-referencing document sections {#sec-cross-referencing-document-sections}

Cross-referencing (@sec-cross-referencing-document-sections)

Cross-referencing (Section 6.3)

Note that the reference must be written in lower case and spaces must be replaced by hyphens. Punctuation is deleted.