Use pandoc template with bookdown - r

To use a pandoc template with a normal Rmd file I add
output:
pdf_document:
template: templateName.latex
to the yaml header. But I can't figure out how to do it with bookdown. Neither
output:
bookdown::pdf_book:
pandoc_args: --template templateName.latex
nor
output:
bookdown::pdf_book:
template: tenplateName.latex
seem to work in either index.Rmd or in _bookdown.yml.

Either
output:
bookdown::pdf_book:
pandoc_args: ["--template", "templateName.latex"]
or
output:
bookdown::pdf_book:
template: templateName.latex
should work. For the first case, make sure to use an array in pandoc_args. For the second case, you had a typo (the filename should be templateName instead of tenplateName). If they don't work, please provide a reproducible example.

Related

is there a way to directly pass latex code to before_body in an rmarkdown YAML header?

For example, Is there a way to pass \newpage to the before_body: YAML tag, without having to use and pass a separate .tex file?
i.e. something like this:
output:
bookdown::pdf_document2:
toc: yes
includes:
before_body: "\newpage"
instead of this:
output:
bookdown::pdf_document2:
toc: yes
includes:
before_body: before_body.tex
where before_body.tex contains \newpage
Thanks !

Bookdown: Exporting to a word document (Error in files2[[format]] : attempt to select less than one element in get1index)

I am writing my thesis using the bookdown package and the memoir latex class. Everything works relatively fine when I am exporting to pdf or to html but I am unable to export the thesis to a word document...
I get the following mysterious error:
Error in files2[[format]] :
attempt to select less than one element in get1index
It is difficult to provide a reproducible example though, as I am working from my messy dissertation repository.
But here is (a part of) my _output.yml file:
bookdown::pdf_book:
includes:
in_header: latex/preamble.tex # defines style and latex options
before_body: latex/before_body.tex # defines cover page
latex_engine: xelatex # lualatex or xelatex
citation_package: none # needs to be "none" in order to use the csl file
keep_tex: true # keeps the .tex file
dev: "cairo_pdf"
toc: false # deactivates default table of contents
highlight: pygments # code highlighting
pandoc_args: [ "--csl", "./csl/apa6.csl" ] # specifies the csl file to be used
bookdown::word_document2:
pandoc_args: [
"--csl", "./csl/apa6.csl",
"--bibliography", "./bib/packages.bib",
"--bibliography", "./bib/thesis.bib",
#"--reference-doc", "./assets/2018-05-17-aim1-draft.docx",
"--filter", "pandoc-citeproc"
#"--filter", "./assets/fix-apa-ampersands.py"
]
Any idea ?
This is a bug of the bookdown package, which I just fixed on Github. Please try the development version there:
remotes::install_github('rstudio/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.

How to insert appendix after references in Rmd using Rstudio?

I am using Rstudio, to create a pdf / html document from an Rmd file. The header looks sth like this:
title: "Title"
author: "Me"
date: "`r format(Sys.time(), '%B %d, %Y')`"
bibliography: bibliography.bib
output:
html_document:
toc: true
number_sections: true
Now, I have some sections, and then include the references. After that, an appendix should follow, but I encounter the exact same problem as described here: Pandoc insert appendix after bibliography
There is a fixed solution in this thread, but I have no idea how I can do that within RStudio directly. To get the document, I just press the "Knit html" button, and do not run any pandoc commands myself. So where should I put the
--include-after-body
part, and how should the appendix rmd file look like?
As noted in the rmarkdown manual, you could use this syntax:
---
output:
html_document:
includes:
after_body: appendix.md
---
This is equivalent to the general way to add arbitrary pandoc arguments to a Rmd file:
---
output:
html_document:
pandoc_args: ["--include-after-body=appendix.md"]
---
The following might be easier; works if you knit to PDF, Word or HTML:
Everything I wanted to say in the main document.
# References
<div id="refs"></div>
\newpage
# Appendix
Some details that will bore the readers of the main document.
In the original post, this was also posted as an answer (few years after current question was asked): see https://stackoverflow.com/a/44294306/8234333 and https://stackoverflow.com/a/16428699/8234333

RMarkdown: How to change headline in table of contents in R Markdown?

When I'm using the toc: yes command, the document contains "Content" as the headline of the table of contents. Due to the fact that I want to write this document in german, I also want to change the headline of the table of contents to the equivalent german word. Any suggestions on how to change this?
This in your YAML should also work:
header-includes:
\renewcommand{\contentsname}{Inhalt}
With this YAML header that includes LaTeX-Package babel, it works for me:
---
title: "TOC in German"
header-includes:
- \usepackage[ngerman]{babel}
output:
pdf_document:
toc: true
number_sections: yes
---
You can also make use of babel simply specifying in the YAML header the "lang" field. For example:
---
lang: de-DE
title: "Ausgefallener Titel"
toc: 1
---
I.e., you can use the "lang" field and specify your desired language-and-country code. For example, de-DE, en-US, etc. (Of course, the corresponding babel package for that language should be installed, or you'll get an error).

Resources