Help understanding bookdown YAML (a new bookdown user)

Hi!

I'm trying to use bookdown. I keep getting various errors that I don't quite understand, and I think it stems from the fact that I don't understand the YAML in my index. Its an amalgamation of various great tutorials I found online. Would someone be able to help me understand the YAML? I put comments of what I think things do, but most of it I don't understand, especially the output!

---
#####################
## metadata ##
#####################
title: "project" #names the project
date: "`r Sys.Date()`" #dates the project

#####################
## Output ## 
#####################
#I don't understand the output part, is this saying to output to html, pdf and word? I think I have too much going on here...

site: bookdown::bookdown_site
documentclass: book
output: 
 bookdown::gitbook: default
 html_document:
  toc: true
  toc_float: true
 bookdown::pdf_book:
  toc: no
  template: null
  includes:
   in_header: _tex/preamble.tex
   before_body: _tex/doc_preface.tex
 bookdown::word_document2:
   fig_caption: yes
   md_extensions: +footnotes
   toc: yes

#####################
## Make it pretty ## 
#####################
#Again, is this overkill? Are some lines saying the same as others? 
theme: "cerulean"
highlight: tango
fig_width: 7    
fig_height: 6
fig_caption: true
fontsize: 12pt #What font is this setting for? 
subparagraph: yes

#####################
## Referencing## 
#####################

link-citations: yes
biblio-style: apalike
bibliography: GK_references.bib
csl: african-online-scientific-information-systems-harvard.csl

#####################
## misc## 
#####################
always_allow_html: yes
classoption: oneside

---

This issue isn't really the warnings/errors as it still seems to knit, its that I don't understand what I've typed properly.

Thank you in advance!

Hi @GabriellaS-K,

My overall advice would be to start simple and build your way up. As of right now, you are rendering your book to 4 different formats, gitbook, HTML, PDF, and Word. It will be really challenging to make all four outputs look and feel the same, so unless it is critical to have every format, I would start with one format and add more as required.

To take a step back, YAML is its own "language" and its core principle is that white-space/indentation matters. So any indented text is a child of the non-indented text above it. Think of these as arguments to functions.

There is a lot to unpack here, so I will try do it in steps:

The text below uses the default gitbook settings. Nothing special here.

output: 
 bookdown::gitbook: default

These are the details for rendering to an HTML document (one single document). The above gitbook is actually HTML also, but with a different look (document split into chapters). Rendering to both is probably redundant and one could be removed. If you prefer this type of format, you may consider bookdown::html_book() instead of html_document() (or bookdown::html_document2()). toc and toc_float are just enabling the table of contents and allowing the TOC to float alongside the document as your scroll.

output: 
 html_document:
  toc: true
  toc_float: true

Everything below corresponds to the PDF version and its settings. The documentclass of book is a specific LaTeX format, like the article class commonly used. Most of these settings are straightforward (table of contents, no template), but the includes argument passes in two LaTeX files which probably contain some custom formatting code, some of which goes in the LaTeX preamble (preamble.tex) and some which goes at the beginning of the book (before the body). These files are custom and presumably supplied by you.

documentclass: book
output: 
 bookdown::pdf_book:
  toc: no
  template: null
  includes:
   in_header: _tex/preamble.tex
   before_body: _tex/doc_preface.tex

This is your Word document specification, not much special going on here, just adding footnote support and to include figure captions and table of contents.

output: 
 bookdown::word_document2:
   fig_caption: yes
   md_extensions: +footnotes
   toc: yes

Pretty sure most of these settings are being ignored currently, as they are mostly arguments to a specific format but they aren't being passed to the format properly since they need to be indented below the the relevant output format in your output section. I could be wrong though. Theme is an HTML-specific configuration setting. fontsize only applies to PDF format (maybe Word too), I believe. Highlight defines a syntax/code highlighter, the figure options specify default sizes and whether captions are included.

theme: "cerulean"
highlight: tango
fig_width: 7    
fig_height: 6
fig_caption: true
fontsize: 12pt #What font is this setting for? 
subparagraph: yes

These are fine. The first option means your citations will be hyperlinked to their entry in the bibliography. You have presumably provided a .bib file containing the possible documents to cite, and a CSL file is just an XML way of describing how to format the citations in-text and in the bibliography.

I think your APA biblio-style is probably being ignored since you are using CSL instead. biblio-style only applies to PDF anyway, I think, and only if you are using natbib or biblatex for LaTeX citations. Probably can be removed without issue. Since pandoc handles the citations using pandoc-citeproc, the other citation settings should apply to all output formats

link-citations: yes
biblio-style: apalike
bibliography: GK_references.bib
csl: african-online-scientific-information-systems-harvard.csl

classoption is related to the documentclass specified much higher in your YAML. It specifies the onside option for the book document class for PDF format. The always_allow_html option makes it so when you have HTML content (such as a widget) that it will play nicely with Word and PDF format documents, which cannot handle interactive content.

always_allow_html: yes
classoption: oneside
4 Likes

Goodness, this was so incredibly helpful! Thank you so much for taking the time, this has made things so much clearer.
I have made a few changes, based on what you've explained. I # out the pdf and word knitting sections, I can deal with those later, for now by just running html it knits much faster and produces a nice output. The tex files I got from [here] which was linked in this tutorial, I will use these for now and think about editing further down my coding journey!

Removing the theme and highlight definitely make the document less attractive, so keeping those, but the other settings (fig_width, height, caption, font size and subparagraph) don't seem to make a difference, so commented them out for now. Also successfully removed the biblio-style

However, I have seemed to do something that makes my markdown files store individually. My YAML looks like:

---
#####################
## metadata ##
#####################
title: "Results"
date: "`r Sys.Date()`"

#####################
## Output ##
#####################

site: bookdown::bookdown_site
documentclass: book
output: 
 bookdown::gitbook: default
 bookdown::html_book:
  toc: true
  toc_float: true
# bookdown::pdf_book:
#  toc: no
#  template: null
#  includes:
#   in_header: _tex/preamble.tex
#   before_body: _tex/doc_preface.tex 
   
# bookdown::word_document2:
#   fig_caption: yes
#   md_extensions: +footnotes
#   toc: yes #un hashtag these when want to do word

theme: "cerulean"
highlight: tango

link-citations: yes
bibliography: GK_references.bib
csl: african-online-scientific-information-systems-harvard.csl

#always_allow_html: yes #unhashtag for pdf
#classoption: oneside #unhashtag for pdf
---

I have uploaded a screenshot of what the folder all this is stored in looks like after knitting, you can see the '_book' file has each chapter + subchapter stored separately, and no overall main version?

Thank you again for your help

You still have two separate output formats in your YAML. One as GitBook style, and a second HTML book with cerulean theme. But likely you are only rendering one, unless you opt-in to rendering both. I think if you render both you will actually overwrite one with the other, because they both write HTML files to separate chapters, you will need to send each book to a unique directory to avoid this.

I would start with only GitBook for now, as this is the common format most people expect when using bookdown. It is HTML anyway, so no need to render two HTML books, in my opinion.

However, I have seemed to do something that makes my markdown files store individually.

Both gitbook and html_book save each chapter to a separate HTML file, by default. So what you see is the expected behaviour. This is on purpose so that each chapter can be navigated to and viewed as separate web pages. The "main" file which coordinates the whole book is the index.html file. Open this file in a web browser (e.g. Google Chrome) and you should see your books landing page and you should be able to navigate to the different chapters.

A simplified YAML like this should get you a lot of mileage:

---
title: "Results"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output: 
 bookdown::gitbook: default
link-citations: yes
bibliography: GK_references.bib
csl: african-online-scientific-information-systems-harvard.csl
---
1 Like

Oh! Brilliant! I guess because I have been running it with errors from the start, I always ended up with a 'main' file (which had to be deleted) that this is what I was expecting to see.

Thanks again-a real help!

1 Like

No problem. The bookdown book (https://bookdown.org/yihui/bookdown/) is a tremendous resource for getting familiar with how everything works, I strongly recommend it.

1 Like

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.