use csl-file for pdf-output in bookdown - r

I would like to use a .csl-file for formatting references with bookdown. Adding csl: some-style.csl to index.Rmd affects the output to gitbook, but not to pdf_book. I know that I can specify biblio-style, but this only accepts some standard styles and not csl-files. Is there a proper workaround?
Steps to reproduce:
Create new project with RStudio and choose "Book Project using bookdown" as option.
Download some .csl file from https://www.zotero.org/styles and copy to root of project.
Add csl: my_csl_file.csl to the header in index.Rmd.
Build the book to pdf and html, and observe the differences in the references (either in the references section, or in the introduction)
Header in index.Rmd:
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [book.bib, packages.bib]
csl: american-sociological-review.csl
link-citations: yes
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
---
HTML output (correct):
PDF output (incorrect):

I had the same problem. The following procedure worked for me:
Create new project with RStudio and choose "Book Project using
bookdown" as option.
Download some .csl file from https://www.zotero.org/styles and copy to root of project. In my case: chicago-author-date-de.csl
Set in _output.yml citation_package: none
Add in all formats (gitbook, pdf_book, epub_book) in _output.yml the line pandoc_args: [ "--csl", "chicago-author-date-de.csl" ]
Delete or comment out in index.Rmd the line biblio-style: apalike
Replace the content of 06-references.Rmd with # References {-}
Here is my _output.yml file:
bookdown::gitbook:
css: style.css
pandoc_args: [ "--csl", "chicago-author-date-de.csl" ]
config:
toc:
before: |
<li>A Minimal Book Example</li>
after: |
<li>Published with bookdown</li>
download: ["pdf", "epub"]
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: none
pandoc_args: [ "--csl", "chicago-author-date-de.csl" ]
keep_tex: yes
bookdown::epub_book:
pandoc_args: [ "--csl", "chicago-author-date-de.csl" ]

Related

Bookdown: Produce only PDF, no HTML

I have a large bookdown project. My YAML header below.
My only desired output is a PDF. However, bookdown by default produces a HTML as well. This makes compilation take more time, and also uses more caching space on my disk. Any chance I can disable producing HTML output? I have tried to play around with the site parameter in the YAML heading, but no luck.
title: "Title"
subtitle: "Subtitle"
author:
- "Name"
- "Institution"
date: "September 2020"
output:
bookdown::pdf_book:
toc: false
includes:
in_header: preamble.tex
keep_tex: yes
latex_engine: xelatex
citation_package: natbib
fontsize: 12pt
linestretch: 1.5
site: bookdown::bookdown_site
documentclass: book
bibliography: [references.bib]
biblio-style: "apalike"
geometry: "left=2.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm"
bookdown doesn't really have a "default" output format. If it produces HTML, the output format must have been provided somewhere. Check if you have a _output.yml under the root directory of your book project. If you do, you may delete it. Then bookdown will use the output field that you specified in the YAML frontmatter of your Rmd document.

R bookdown: How to change the title of the table of content for rendering a PDF book

I am using bookdown to render a PDF book using the documentclass "scrreprt" from koma script. This works so far, however the book is in German
and the table of contents has a title "Contents" instead of a german "Inhaltsverzeichnis".
The "header" of my index.rmd:
---
title: "Mein Buchtitel"
author: "Jens Laufer"
date: "`r Sys.Date()`"
documentclass: scrreprt
---
The output of table of contents looks like this:
I tried also to add a preamble.tex in index.Rmd:
---
title: "Mein Buchtitel"
author: "Jens Laufer"
date: "`r Sys.Date()`"
documentclass: scrreprt
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
---
preamble.tex
\renewcommand{\contentsname}{Inhaltsverzeichnis}
However this doesn't help.
Any ideas? Thanks!
You can set the variable toc-title in the YAML metadata, e.g.,
toc-title: "Table of Contents"
See the Pandoc Manual for more info: https://pandoc.org/MANUAL.html
I could fix the problem by changing the preamble.tex to this:
\usepackage[ngerman]{babel}
\selectlanguage{ngerman}

How to output a pdf book from Rnotebook?

I'm not getting a pdf output from my rnotebook.
My header in my "index.Rmd" is:
---
title: "some title"
author: "me"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
description: "Notes on a Leasing Company busines risk and opportunities."
---
I'm rendering the book in the console with the following code:
bookdown::render_book("index.rmd", output_format = "bookdown::pdf_book")
Output is:
Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
always_allow_html: yes
Note however that the HTML output will not be visible in non-HTML formats.
How do I go about changing the output type?
My file "_output.yml" file is:
toc:
before: |
<li>A Minimal Book Example</li>
after: |
<li>Published with bookdown</li>
download: ["pdf", "epub"]
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
bookdown::epub_book: default

how to specify a csl bibliography style on bookdown

how to specify a csl bibliography style on bookdown Chinese Bookdown Chinese example, for example a zotero one CSL.
I downloaded the csl file to the working directory, and specify it in index.Rmd YAML header as
bibliography: book.bib
biblio-style: chinese-gb7714-2005-numeric
or
bibliography: book.bib
biblio-style: chinese-gb7714-2005-numeric.csl
but Rstudio promted me:
Error: Failed to build the bibliography via bibtex
Please delete bookdown.Rmd after you finish debugging the error.
Execution halted
Exited with status 1.
when I changed it to
bibliography: book.bib
csl: chinese-gb7714-2005-numeric
or
bibliography: book.bib
csl: chinese-gb7714-2005-numeric.csl
I can build the book, however the reference list style was not the one the CSL expected.
All the YAML header
---
title: "题名"
author: "姓名"
date: "`r Sys.Date()`"
bibliography: book.bib
description: "矮脚黄"
documentclass: ctexbook
geometry:
- b5paper
- tmargin=2.5cm
- bmargin=2.5cm
- lmargin=3.5cm
- rmargin=2.5cm
github-repo: yihui/bookdown-chinese
indent: 2m
link-citations: yes
lof: no
lot: no
colorlinks: yes
site: bookdown::bookdown_site
biblio-style: unsrt
whitespace: none
---
updated:
today I found the header of Chapter reference was not correct. It should be "Reference" instead of the previous chapter's.
You probably missed two lines:
Set in _output.yml citation_package: none
Add in all formats (gitbook, pdf_book, epub_book) in _output.yml the line pandoc_args: [ "--csl", "chinese-gb7714-2005-numeric.csl" ]
See the full procedure at use csl-file for pdf-output in bookdown

R bookdown - custom title page

How to customize title page using bookdown?
I tried using the following code in the YAML header.
includes:
in_header: preamble.tex
before_body: body.tex
The body.tex file was something pretty simple, just for test:
\begin{titlepage}
Hello world
\end{titlepage}
In the LaTeX template <R-Library>/rmarkdown/rmd/latex/default-1.17.0.2.tex we see
\begin{document}
$if(title)$
\maketitle
$endif$
$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$
$for(include-before)$
$include-before$
This means that a titlepage is created using \maketitle if a title is defined in the YAML headers. Similar for the abstract. If you remove both these tags from your YAML headers, then the content from the file body.tex will be the first to be processed and you are free to customize your titlepage there.
See the answers to this question for an alternative approach.
I ended up editing the _output.yml file to reference a copy of default-1.17.0.2.tex template in my R project directory using yaml template tag.
bookdown::gitbook:
css: style.css
config:
toc:
before: |
<li>A Minimal Book Example</li>
after: |
<li>Published with bookdown</li>
edit: https://github.com/rstudio/bookdown-demo/edit/master/%s
download: ["pdf", "epub"]
bookdown::pdf_book:
fig_caption: true
number_sections: yes
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
template: template.tex
bookdown::epub_book: default
For some reason i had an error compiling the pdf (! Undefined control sequence...) so I included a latex command \usepackage{graphicx} in template.tex to fix it. Now it is supposed that I am free to customize title page and whatsoever.

Resources