Customizing preamble.tex in bookdown - r

I started using bookdown to generate a longer report, but came into some touble as I wanted to customize the pdf output. \documentclass[]{} does not work in the preamble.tex file. I think that comand is somehow executed by pandoc beforhand, unfortunately I did not find out how to pass options to it so far.
I worked around a few things like setting the fontsize via \usepackage[12pt]{moresize} but I was wondering how to acces the set up options properly. The bookdown documentation does not cover this subject in sufficient detail for me to understand.

Look at this BookDown Documentation,
You can just add following in your header yaml,
---
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
---

I found the answer in the Pandoc documentation

Related

R Bookdown: Append chapter without re-rending whole book

I am rather experienced with R but new to markdown and especially bookdown. I wonder how to render a single chapter and merge it with the existing book without re-building the whole book. The reason why I am asking is that I am creating a book that contains parameterised chapters and each chapter runs very long (1-2 hours). All in all, it will be hundreds of chapters with the same analysis, but different input data and would time-wise add up. I am aware of the knit-and-merge approach, in fact, that's the reason why I chose bookdown, but somehow it is not working as expected.
I am honestly also a little bit confused by the different rendering possibilities. There is the built-book button, the knit button, serve_book(), render_book(), preview_chapter(), ... I figured that one should avoid using the knit button in bookdown and that serve_book() will automatically compile changes. My approach so far is entering serve_book() into console, where it renders the whole book, then I create a new chapter, hit save and it runs the chunks of only that chapter (exactly what I want!) and the single HTML file of the new chapter appears. However, it doesn't show them when I look at the book via the index.html file.
I am only interested in an HTML book and do not need any other output formats.
In order to recreate the problem, I am just using the bookdown example and adding chapters to it.
_bookdown.yaml
book_filename: "book_name"
new_session: yes #true
before_chapter_script: _common.R
delete_merged_file: true
language:
ui:
chapter_name: "Chapter "
_output.html
bookdown::bs4_book:
css: style.css
theme:
primary: "#10395B"
#repo: https://github.com/rstudio/bookdown-demo
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
bookdown::epub_book: default
_index.yaml
---
title: |
![](./Logo_PureGene.png){width=200px}|
|-|
||
author: "Name"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
#output: bookdown::bs4_book
documentclass: book
bibliography: [book.bib, packages.bib]
# url: your book url like https://bookdown.org/yihui/bookdown
# cover-image: path to the social sharing image like images/cover.jpg
description: |
This is a minimal example of using the bookdown package to write a book.
The HTML output format for this example is bookdown::bs4_book,
set in the _output.yml file.
biblio-style: apalike
csl: chicago-fullnote-bibliography.csl
---
# About {-}
I would appreciate any help!
Thanks in advance :)

Bookdown: Cross-reference figure in another file

I have taken the plunge and am preparing a manuscript for publication entirely in RStudio using bookdown. In the main text I would like to cross-reference figures in a separate supporting information .Rmd file.
Let's say this is my main text file called main.Rmd:
---
title: "Main text"
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
Here is the supporting text called supporting.Rmd, and the figure to refer to, saved in the same folder:
---
title: "Supporting info"
output:
bookdown::pdf_book:
toc: no
---
Here is the supporting text.
```{r supporting-figure}
plot(cars)
```
How can I cross-ref supporting-figure in the main text?
I have checked the section on cross-references in Yihui's bookdown manual, but I cannot see how to extend this to cross-references between files.
I also found this question:
Cross-reference figure in a separate Rmarkdown (PDF) file
but the accepted answer does not work for me (perhaps because I am using bookdown rather than base Rmarkdown?)
I am not entirely sure how you are compiling these two files into a single bookdown document, because as it stands they are just two separate R Markdown documents. But there are two issues:
Figures need a caption to be cross-referenced
You can only cross-reference figures which have a caption assigned with fig.cap, as explained here:
If we assign a figure caption to a code chunk via the chunk option
fig.cap, R plots will be put into figure environments, which will be
automatically labeled and numbered, and can also be cross-referenced.
Incorrectly configured bookdown project:
From what I can tell, you have not got the project configured correctly for bookdown:
The main file should be called index.Rmd
Supporting files should not have any YAML
You should include site: bookdown::bookdown_site in the YAML of the main documene
Check out this answer for some tips on a minimal bookdown file.
Solution
index.Rmd
---
title: "Main text"
site: bookdown::bookdown_site
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
supporting.Rmd
Here is the supporting text.
```{r supporting-figure, fig.cap= "Some Figure"}
plot(cars)
```
I ran into the same issue and came up with this solution if you aim at compiling 2 different pdfs. It relies on LaTeX's xr package for cross references: https://stackoverflow.com/a/52532269/576684

citation-package: biblatex not working in R bookdown

EDIT: After some investigation, this question is really about the following option in the output yml:
citation-package: biblatex
Without this option, bookdown is using the default citeproc and it's not clear how to modify the number of authors. However, when this option is used, the referencing is not working anymore and my document contains only the refnames in bold instead of inline citations. So I really need to know why the citation-package: biblatex is not working
===== original question below
I can't get bookdown to honour my maxcitename=2 settings. I have tried using this output yml
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
keep_tex: yes
toc_depth: 3
toc_appendix: yes
with this line in the preamble.tex file:
\usepackage[backend=bibtex, maxcitenames=2, style=authoryear]{biblatex}
I have also tried using this output yml:
bibliography: [likertimputebiblio.bib, packages.bib]
biblatexoptions: [maxcitenames=2]
csl: harvard-university-of-wolverhampton.csl
link-citations: true
nocite: |
#R-bookdown
and I have also tried this output yml:
site: bookdown::bookdown_site
documentclass: book
header-includes:
- \usepackage[backend=bibtex, maxcitenames=2, style=authoryear]{biblatex}
but nothing seems to work.
Please help. Thanks.
The solution to this problem was found after much perseverence!
When setting the output yml, indented under output: etc...
citation_package: biblatex
... the in line references were failing to link to the .bib file and so the refnames were just showing up in bold and failing to make any inline citations.
The expected solution should be to use the additional option:
biblatexoptions: [backend=bibtex, maxcitenames=2]
(maxcitenames=2 is the main reason I want to use biblatex) but this was failing with the error "option backend not recognized."
Finally the solution was to modify the default template in the directory
C:\Program Files\R-3.4.0\library\rmarkdown\rmd\latex
at line 100, from
\usepackage$if(biblio-style)$[style=$biblio-style$]$endif${biblatex}
to
\usepackage$if(biblio-style)$[backend=bibtex, style=$biblio-style$]$endif${biblatex}
I would like to suggest to the package author that this is a bug that needs fixing, because backend=bibtex is a valid option and should have been passed through

rmarkdown not printing LaTeX on github

I pushed the html output of my rmarkdown code to github with
output:
html_document:
keep_md: true
which gave me the output I wanted, however, none of the comments that were typed in LaTeX were displayed in their natural form. I'm not sure if this is possible, but just curious if there is a way around it. Thanks!
GFM does not include LaTeX support.

Header style in R markdown

I am writing a report in R markdown (using pdflatex to convert it into pdf) and I am trying to set the style of the different headers (color, size, interline before and after).
I am using this formalisation :
#H1
##H2
###H3
I have the basics of Markdown and LaTeX but I'm far to be an expert. Is there a way to do it without creating a LaTeX template ? For exemple with some YAML code in the head of the document ?
Mine for now :
---
title: "Guide"
output:
pdf_document:
fig_caption: yes
highlight: tango
toc: yes
toc_depth: 2
documentclass: report
---
Or by changing the R options ?
```{r set-options, echo=FALSE, cache=FALSE}
options(some stuff)
```
I've found some indications while searching that suggest it may be possible, but I could'nt manage to find any understandable exemple ...
Thanks for any feedback.
If you do not want to do any LaTeX, you could add your own CSS (see documentation) and set it that way, as in this answer.
You would then need to do: How to convert Markdown + CSS -> PDF?
However, your results may not be as faithfully rendered as if you create a LaTeX template.

Resources