Chapter 5 Figures

5.1 Adding figures

Figures and captions can be included, but the syntax is quite different to LaTeX.

Assuming that you would add a figure to a LaTeX document with the following code:

\begin{figure}
\begin{centering}
\includegraphics[width=0.7\textwidth]{Images/ho-tension.png}
\caption{This is a figure caption.}
\label{fig:ho-plot}
\end{centering}
\end{figure}

To include the figure in markdown the syntax is as follows:

```
{r echo=FALSE, ho-plot, out.width='70%', fig.show='hold', fig.cap="This is a figure caption."}
knitr::include_graphics("Images/ho-tension.png")
```
This is a figure caption.

Figure 5.1: This is a figure caption.

Important parts of this command:

  • echo=FALSE prevents the code being used to display the figure being shown.
  • ho-plot is your figure label.
  • out-width is the output width of the figure.
  • fig.cap is the figure caption.
  • knitr::include_graphics gives the location of the image file.

5.2 Figure captions

Figure captions are controlled by the fig.cap setting. Your caption should be inside the double quotes. Remember to use \” to escape any ” characters you may use inside your caption.

You can include cross-references and citations inside figure captions. See Sections 5.3, 6.3, and 7.2 for how to label and cross-reference figures, equations, and tables. Citations are covered in Section 8.1.

5.3 Figure numbering and referencing

Figures with labels and captions are numbered according to the section (e.g. Fig 5.1 in this case).

To reference a figure use the syntax

\@ref(fig:label)

where fig:label would be fig:ho-plot in this case.

You can cross reference between sections/chapters, so make sure to use unique figure labels.