How do I get rid of scanner errors when loading a CSS file into an RMarkdown document?

Hello,

I writing an RMarkdown document and am trying to style it by adding a CSS path file to the YAML header. However, when I try to knit the document, I get a scanner error:

title: "Title"
output:
html_document:
css: "filepath.css"

# > Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: while parsing a quoted scalar at line 6, column 8 did not find expected hexdecimal number at line 6, column 13
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted

What is going on? How do I get rid of the error?

Whitespace matters in YAML heading. html_document is nested under output: and so it needs to be preceded by a tab. css is nested under html_document and so it needs yet another tab before it.

---
title: "Title"
author: "Me, Myself, and I"
date: "`r Sys.Date()`"
output: 
  html_document:
    css: "filepath.css"
---

Unfortunately, the problem persists even when I apply whitespace like you suggested. It makes me wonder if there is a bug in my CSS stylesheet, but it seems normal to me:

body {
  background-color: #FEFEFE;
  color: #2D1674;
  margin: auto;
  width: 85vw;
  height: 85vh;
  line-height: 2em;
  font-size: 1em;
  font-family: sans-serif;
}

So I consulted StackOverflow and found that the following code works:

title: "title"
output: html_document

# insert HTML block
<link href="filepath.css" rel="stylesheet" />

This solution applies the CSS to the document like I want, though I don't know why.

The error you get if from YAML parsing, so this means that your YAML header is probably not indented correctly to add the CSS. This is as @James_Murray shared.

If you get the exact same issue, it means there is still an issue in the YAML formatting that makes the YAML not correctly parsable. YAML needs to respect a specific format to be read.

There is several way to apply CSS to a HTML output format. This is one of them. You could also use a css chunk

```{css, echo = FALSE}
body {
  background-color: #FEFEFE;
  color: #2D1674;
  margin: auto;
  width: 85vw;
  height: 85vh;
  line-height: 2em;
  font-size: 1em;
  font-family: sans-serif;
}
```

Anyway, it seems this is working for you now, so it is good

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.