How to Pass Pandoc_Args to Yaml Header in Rmarkdown

How can we pass pandoc_args to yaml header in rmarkdown?

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"
]
---

What can I control with YAML header options in pandoc?

Almost everything set in the YAML metadata has only an effect through the pandoc template in use.

Pandoc templates may contain variables. For example in your HTML template, you could write:

<title>$title$</title>

These template variables can be set with the --variable KEY[=VAL] option.

However, they are also set from the document metadata, which in turn can be set either by using:

  • the --metadata KEY[=VAL] option,
  • a YAML metadata block, or
  • the --metadata-file option.

The --variable options inserts strings verbatim into the template, while --metadata escapes strings. Strings in YAML metadata (also when using --metadata-file) are interpreted as markdown, which you can circumvent by using pandoc markdown's generic raw attributes. For example for HTML output:

`<script>alert()</script>`{=html}

See this table for a schematic:

|                        | --variable        | --metadata        | YAML metadata and --metadata-file |
|------------------------|-------------------|-------------------|-----------------------------------|
| values can be… | strings and bools | strings and bools | also YAML objects and lists |
| strings are… | inserted verbatim | escaped | interpreted as markdown |
| accessible by filters: | no | yes | yes |

To answer your question: the template determines what fields in the YAML metadata block have an effect. To view, for example, the default latex template, use:

$ pandoc -D latex

To see some variables that are set automatically by pandoc, see the Manual. Finally, other behaviours of pandoc (such as markdown extensions, etc) can only be set as command-line options (except when using a wrapper script).

Setting document title in Rmarkdown from parameters

Try to use a second YAML metadata block, and put the parameterized metadata in there.

I got the following code to work as expected (i.e., producing a document title from the list of params):

---
output: html_document
params:
set_title: "My Title!"
---

---
title: `r params$set_title`
---

The RMarkdown documentation notes that YAML metadata blocks are combined by Pandoc. Use the first block to define the parameter set, and the second one to use the parameters as metadata. Knitr will execute the R code to interpret the parameters in the second block.Then Pandoc will merge the metadata blocks together.

Update (2017):

This can be accomplished in a single block, like so:

---
output: html_document
params:
set_title: "My Title!"
title: "`r params$set_title`"
---

This works because the title comes after the params definition. I put quotes around the in-line R code to prevent "Scanner errors".

How to include R code within Latex in YAML header for custom-formatted RMarkdown Template

You are using an inline R chunk inside a tex document. That cannot work.

Instead, use the argument pandoc_args of pdf_document() to pass variables to pandoc. Inside the header.tex you can then use a pandoc variable:

args <- pandoc_variable_arg("cover", system.file("resources/cover.png", package = "mytemplate"))

report <- function() {

## location of resource files in the package
header <- system.file("resources/header.tex", package = "mytemplate")

## derives the style from default PDF template
rmarkdown::pdf_document(
latex_engine = "xelatex", fig_caption = FALSE,
includes = rmarkdown::includes(in_header = header),
pandoc_args = args # pass to pandoc
)
}

And header.tex:

\usepackage{fancyhdr}
\thispagestyle{fancy}
\fancyhead[LC]{
\includegraphics{$cover$}
}

Adapting Latex class and template file to an Rmarkdown template

This can be accomplished with "-r", "markdown-auto_identifiers"

It can be specified in the YAML with

output: 
bulldown::thesis_pdf:
pandoc_args: [
"-r", "markdown-auto_identifiers"
]

or in a function like bookdown::pdf_book using the pandoc_args argument

pandoc_args = c(pandoc_args, "--top-level-division=chapter", "-r","markdown-auto_identifiers")

Header and footer in YAML metablock for rmarkdown and pandoc

There is no native pandoc-markdown support of headers and footers. However, since pandoc generates pdf documents through latex, you can tweak your latex template to accommodate those.

Create a new template from the default one :

pandoc -D latex > footer_template.latex

Add the following lines in the latex preamble (this is a very basic example that should produce a centered footer, see the fancyhdr user manual if you need something more advanced)

$if(footer)$
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyfoot[C]{$footer$}
$endif$

Finally, add this to your document yaml header :

output: 
pdf_document:
template: test.latex

Your template should either be in ~/.pandoc/templates or in the same directory as test.rmd for this to work.



Related Topics



Leave a reply



Submit