6.4 Include additional LaTeX packages

The use of additional LaTeX packages can allow for extensive customization of document styling. In addition, several packages such as kableExtra (Zhu 2024) may have LaTeX dependencies for the R package to function. Much like R, we need to load packages within the R Markdown document before we are able to use their functions.

6.4.1 Loading LaTeX packages

We can load additional LaTeX packages using the extra_dependencies option within the pdf_document YAML settings. This allows us to provide a list of LaTeX packages to be loaded in the intermediate LaTeX output document, e.g.,

---
title: "Using more LaTeX packages"
output: 
  pdf_document:
    extra_dependencies: ["bbm", "threeparttable"]
---

If you need to specify options when loading the package, you can add a second level to the list and provide the options as a list, e.g.,

output: 
  pdf_document:
    extra_dependencies:
      caption: ["labelfont={bf}"]
      hyperref: ["unicode=true", "breaklinks=true"]
      lmodern: null

For those familiar with LaTeX, this is equivalent to the following LaTeX code:

\usepackage[labelfont={bf}]{caption} 
\usepackage[unicode=true, breaklinks=true]{hyperref}
\usepackage{lmodern}

The advantage of using the extra_dependencies argument over the includes argument introduced in Section 6.1 is that you do not need to include an external file, so your Rmd document can be self-contained.

6.4.2 Example packages

There is an extensive community for LaTeX, and there are over 4,000 packages available through the Comprehensive TeX Archive Network (CTAN). Here are some examples of LaTeX packages you could consider using within your report:

  • pdfpages: Include full PDF pages from an external PDF document within your document.
  • caption: Change the appearance of caption subtitles. For example, you can make the figure title italic or bold.
  • fancyhdr: Change the style of running headers of all pages.

References

Zhu, Hao. 2024. kableExtra: Construct Complex Table with Kable and Pipe Syntax. http://haozhu233.github.io/kableExtra/.