user697473
user697473

Reputation: 2293

inconsistent processing of YAML lists in R Markdown documents (related to loading of LaTeX packages)

I would like to use R Markdown to produce a document that takes advantage of certain LaTeX packages. Sometimes, I want to render the Rmd document to PDF. At other times, I want to render it to HTML.

I would like to load packages via the extra_dependencies option, rather than through the includes or header-includes options. Some of the LaTeX packages should be loaded with options. Others shouldn't be.

When I render the Rmd document to PDF, there is no problem. But when I try to render the same document to HTML, rmarkdown::render chokes on the processing of the extra_dependencies argument. (I am using rmarkdown 2.1.) Here is a minimal example, following the style of the R Markdown Cookbook:

---
title: "Test Processing of YAML Header in R Markdown Document"
output: 
  html_document:
    extra_dependencies:
      array: null
      numprint: ["autolanguage"]
---

Hello.

Rendering that document with rmarkdown::render generates a dependency_resolver -> <Anonymous> -> sapply -> lapply error. If I add dashes before array and numprint, the error is instead Error: invalid version specification 'NULL'. But if I just change html_document to pdf_document, there is no problem.

How may I generate an HTML document while loading packages through the extra_dependencies option? And why does this example work when I generate PDF documents, but not when I generate HTML documents?

Upvotes: 5

Views: 959

Answers (2)

Mikey Harper
Mikey Harper

Reputation: 15409

Note, the extra_dependencies argument is available for multiple different output formats (PDF, HTML), but these settings are output format specific.

If you want to assign LaTeX packages, these will will only work as an extra_dependency for PDF outputs. This is causing the error as it doesn't recognise the syntax of the extra dependency. To get your code to work you need to provide separate options for HTML and PDF:

---
title: "Test Processing of YAML Header in R Markdown Document"
output: 
  html_document: default
  pdf_document:
    extra_dependencies:
      array: null
      numprint: ["autolanguage"]
---

Hello.

You could specify extra dependencies for an HTML document, but these will have to be HTML dependencies, not LaTeX packages. There don't seem to be many great examples showcasing this, as this functionality is mostly used by templates and rarely exposed to end users, but it would allow you to load additional JavaScript dependencies. This example is given here: https://github.com/rstudio/rmarkdown/issues/1654

Upvotes: 3

J_F
J_F

Reputation: 10372

I don´t knwo why this error occurs, but I have a workaround. Instead of using extra_dependencies you can use header_includes.

---
title: "Test Processing of YAML Header in R Markdown Document"
output: 
  html_document
header-includes:
  - \usepackage{array}
  - \usepackage{numprint}["autolanguage"]
---

Upvotes: 2

Related Questions