How can we pass pandoc_args to yaml header in rmarkdown? - r

I'm using rmarkdown to produce a word (.docx) report. I would like to change the header of the toc. This seems possible as pandoc_args can be passed as options in the yaml header in the case of a doc file (1). But I'm not doing it right. Could anyone provide a working example ?
(1) pandoc.args is included in the rmarkdown possible options and in the pandoc manual, there is a toc-title option
---
title: "yaml header"
author: "cedric"
output:
word_document:
toc: true
pandoc_args: toc-title="Table des matières"
---
# One section
# Another
This produces :

The title of the table of contents is document metadata, so you can set it with YAML metadata block.
---
title: "yaml header"
author: "cedric"
output:
word_document:
toc: true
toc-title: "Table des matières"
---
Or passed it in with the -M command-line flag.
---
title: "yaml header"
author: "cedric"
output:
word_document:
toc: true
pandoc_args: [
"-M", "toc-title=Table des matières"
]
---

Related

How can I add a table of contents in R Markdown?

I'm having a hard time trying to add a table of contents in R Markdown.
I want the table of contents to be on the left side of the document.
I tried this code, but it didn't work for me (when I knitr the document, it works fine, but the TOC is not available):
---
title: "Relatório VANT - P&D"
author: "Empresa: XXXX"
date: "Data: 24/05/2020"
output:
html_document:
toc: true
toc_float: true
---
How can I solve my problem?
It is important to indent toc, e.g.
---
title: "Relatório VANT - P&D"
author: "Empresa: XXXX"
date: "Data: 24/05/2020"
output:
html_document:
toc: true
toc_float: true
---
# h1
# h2
# h2.2

How can I modify yaml instructions outside of the document I am rendering

I'd like to rmarkdown::render a R document without indicating the yaml options within the document itself.
Ideally that could be an argument on rmarkdown::render or knitr::spin like what you can do to pass params (see the Rmarkdown reference book). Typically I'd like author, date and the output options too.
I think this is possible because spining the following document without specifying anything I get the following output (so there must be a template of default args that I can hopefully change)
As an example, how could I do to render a document that would give me the same output as say the below (but of course without specifying the yaml in the document ie no yaml whatsoever in the document)
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
pdf_document:
toc: true
highlight: zenburn
---
#' # Title
Hello world
#+ one_plus_one
1 + 1
You can pass yaml options as parameters too. For example:
---
params:
title: "add title"
author: "add author"
output: pdf_document
title: "`r params$title`"
author: "`r params$author`"
---
This is my document text.
Then, in a separate R script:
rmarkdown::render("my_doc.rmd",
params=list(title="My title",
author="eipi10"))
You could cat a sink into a tempfile.
xxx <- "
#' # Title
Hello world
#+ one_plus_one
1 + 1
"
tmp <- tempfile()
sink(tmp)
cat("
---
title: 'Sample Document'
output:
html_document:
toc: true
theme: united
pdf_document:
toc: true
highlight: zenburn
---", xxx)
sink()
w.d <- getwd()
rmarkdown::render(tmp, output_file=paste(w.d, "myfile", sep="/"))

R Markdown keywords don't appear in the document

I want to knit a pdf-document but the keywords don't appear in the final document. Can someone say me what I'm doing wrong?
---
title: "title"
subtitle: "subtitle"
author: "author"
date: "09 04 2019"
output:
pdf_document:
keywords: "word1, word2, word3"
footerdate: yes
abstract: 'Insert abstract here'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Introduction
When knitting to PDF, keywords are sent to the file metadata but are not actually visible in the file. Source
You can customize the template for PDF generation or render Rmd files with base_format: rticles::elsevier_article.
However, there is another way to show keywords on the PDF just by adding few codes to the Rmd file:
Add a LaTeX code to provide the \keyword command (\providecommand{\keywords}[1]{\textbf{\textit{Keywords---}} #1}) into YAML's header-includes key;
Add keywords in the text section using the LaTeX command
---
title: "title"
subtitle: "subtitle"
author: "author"
date: "09 04 2019"
output:
pdf_document:
# keywords: "word1, word2, word3" # <- You don't have to add your keywords here since they only appear as the 'invisible' metadata of PDF
footerdate: yes
abstract: 'Insert abstract here'
header-includes:
- |
```{=latex}
\providecommand{\keywords}[1]{\textbf{\textit{Keywords---}} #1}
```
---
```{=latex}
\keywords{word1, word2, word3}
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Introduction

R markdown YAML dynamic variables

In RMarkdown, I seem to be able to create 'some' dynamic variables in the YAML header, but not for others:
For instance, this works:
---
title:
"Some Title, `r format(Sys.time(), '%d %B, %Y')`"
...
---
But this does NOT.
---
...
pdf_document:
keep_tex: `r 'true'`
---
But this DOES (ie not dynamic).
---
...
pdf_document:
keep_tex: true
---
So how can I 'dynamically' assign the keep_tex to either true or false, what I want to do, is something like this:
---
...
pdf_document:
keep_tex: `r getOption('mypackage.keep_tex')`
---
I don't know if the template options can be set programmatically in the YAML header of the .Rmd file.
As an alternative, if you use rmarkdown::render to render your document, you may specify the output template (pdf_document), and then set template options (e.g. keep_tex) programmatically.
For example, if you have a .Rmd file called "test.Rmd" like this:
---
title:
"Some Title, `r format(Sys.time(), '%d %B, %Y')`"
---
...and some logical object which determines whether to keep the intermediate TeX file or not, e.g.
my_keep <- TRUE
...you may render the input file to PDF format and keep the TeX file like this:
render(input = "test.Rmd",
output_format = pdf_document(keep_tex = my_keep))

Using a YAML header argument in knitr

I am making a set of slides using rmarkdown and the LaTeX option of outputting it to beamer.
I have two templates I use in my slides - one specific for the LaTeX options, and one pandoc template that I have modified to account for some additional feature of my slides.
I have defined an option in the YAML header called to_print which is a boolean TRUE/FALSE that I pass to the pandoc template that tells it to add a package and clean up the slides for printing.
I also want to use this variable to define the name of the files. The basic idea being that I want to have one .rmd file for my slides, and then just changing this one option to signify that it's for printing by students, or for me presenting.
I have figured out that I can pass the render function using the knit parameter in the YAML header, but I have to specify to_print = TRUE and set the condition in the ifelse() statement in the output_file correspondingly.
Currently I have:
---
title: "Introduction to R"
subtitle: "Reading and saving data in R"
date: '`r format(Sys.Date())`'
output:
beamer_presentation:
fonttheme: professionalfonts
highlight: tango
includes:
in_header: "../../templatefiles/beamer_header.tex"
template: "../../templatefiles/beamer_template.tex"
incremental: no
keep_tex: yes
slide_level: 3
theme: Warsaw
toc: yes
fontsize: 10pt
fontenc: T1
to_print: TRUE
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding,
output_file = file.path(ifelse(TRUE,
gsub("\\..*","_handout.pdf", inputFile),
gsub("\\..*", ".pdf", inputFile)))) })
---
I would like to specify just one parameter that is then used to signify if this is a handout to be printed:
Pseudo-code:
to_print: TRUE
knit: (function(inputFile, encoding) {rmarkdown::render(inputFile, encoding = encoding,
output_file = file.path(ifelse(YAML_PARAM$to_print,
gsub("\\..*","_handout.pdf", inputFile),
gsub("\\..*", ".pdf", inputFile)))) })
Is this possible, or something equivalent that would let me have one .rmd file with a boolean to toggle presentation vs. printing?
You can use the rmarkdown::yaml_front_matter() function:
---
title: "Introduction to R"
subtitle: "Reading and saving data in R"
date: '`r format(Sys.Date())`'
output:
beamer_presentation:
fonttheme: professionalfonts
highlight: tango
includes:
in_header: "../../templatefiles/beamer_header.tex"
template: "../../templatefiles/beamer_template.tex"
incremental: no
keep_tex: yes
slide_level: 3
theme: Warsaw
toc: yes
fontsize: 10pt
fontenc: T1
to_print: TRUE
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding,
output_file = file.path(ifelse(rmarkdown::yaml_front_matter(inputFile)$to_print,
gsub("\\..*","_handout.pdf", inputFile),
gsub("\\..*", ".pdf", inputFile)))) })
---

Resources