bookdown error ' Could not parse YAML header: mapping values are not allowed in this context "source" '

While attempting to knit together an RMarkdown - Bookdown Project - into PDF, I encounter this error:

[pandoc warning] Could not parse YAML header: did not find expected key "source" (line 74, column 2)

The individual chapters knit together into HTML. But I can not successfully Knit to PDF

The headers for the .Rmd files look like this:

---
output:
  pdf_document: default
 latex_engine: xelatex
---

I can't find any `source' files (the .Rmd files) that show any unusual characters on line 74 column 2.

Are the 'source' files here the individual .Rmd files? Why is this error occurring?

Thank you for help!

Your formatting in the YAML looks a little odd - is there are space just before latex?

Regardless, I couldn't reproduce your error. Can you simplify your Rmd down to the fewest possible lines and post that?

For example,

  • Delete lines 75 onwards - do you still get the error
  • Delete lines 50 onwards - do you still get the error

Thank you. I fixed the spacing before latex, and then tried knit to PDF again. But received this error:

Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 3, column 17
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Parser error: while parsing a block mapping at line 1, column 1 did not find expected key at line 3, column 2
Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Parser error: while parsing a block mapping at line 1, column 1 did not find expected key at line 3, column 2

I'm still getting this error when I try to knit together an Rmd to PDF that consists of just

---
output:
  pdf_document: default
  latex_engine: xelatex
---
# Fundamentals of Data Visualization

Given that this is a bookdown project, is it reading individual YAML headers off of each Rmd?

In YAML indentation matters, try with this

---
output:
  pdf_document: default
    latex_engine: xelatex
---
1 Like

Do you need YAML headers in bookdown? I've not used bookdown for a while, but when I did I just had

---
knit: "bookdown::preview_chapter"
---

At the top of the chapters.

Thanks for the suggestion I fixed the indentation but still getting the "could not parse..." error.

Yes, I deleted them, and then bookdown created the headers again automatically. Using the default headers still didn't fix it. Thanks for the ideas. I'll keep working on it.

I don't know where you're at with yaml header but they are really specific on indentation and typology.

latex_engine is an argument of pdf_document, you can't use pdf_document: default if you specify a modification of one of the arguments. Here, I would write

---
output:
  pdf_document: 
    latex_engine: xelatex
---

So remove the default if you want to precise a a argument of pdf_document, and indent it correctly : an argument of pdf_document should be indented under.

Hope it helps.

1 Like