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 2020) 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.,

  1. ---
  2. title: "Using more LaTeX packages"
  3. output:
  4. pdf_document:
  5. extra_dependencies: ["bbm", "threeparttable"]
  6. ---

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.,

  1. output:
  2. pdf_document:
  3. extra_dependencies:
  4. caption: ["labelfont={bf}"]
  5. hyperref: ["unicode=true", "breaklinks=true"]
  6. lmodern: null

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

  1. \usepackage[labelfont={bf}]{caption}
  2. \usepackage[unicode=true, breaklinks=true]{hyperref}
  3. \userpackage{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. 2020. kableExtra: Construct Complex Table with Kable and Pipe Syntax. https://CRAN.R-project.org/package=kableExtra.