6.10 Use a custom Pandoc LaTeX template (*)

Pandoc converts Markdown to LaTeX through a template. The template is a LaTeX file containing Pandoc variables, and Pandoc will replace these variables with their values. Below is a simple template that only contains a single variable $body$:


The value of $body$ is the LaTeX code generated from the body of the Markdown document. For example, if the body text is Hello **world**! in Markdown, the value of $body$ will be Hello \textbf{world}!.

If the LaTeX customization methods in Sections 6.1, 6.2, and 6.4 are not enough for you, you may try to use a custom template instead. A template allows you to use arbitrary LaTeX code in it, and hence is much more flexible. To use a template, include the path of the template in the template option of pdf_document, e.g.,

    template: my-template.tex

The default LaTeX template of Pandoc can be found at https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex). If you want to create your own template, you may want to start with this template.

For the full list of Pandoc variables and their meanings (such as $body$ and $title$), see Pandoc’s manual at https://pandoc.org/MANUAL.html#templates. You can also use arbitrary custom variables, which are typically passed to the template from the YAML metadata. If you want to learn by examples, you may take a look at the MonashEBSTemplates package (https://github.com/robjhyndman/MonashEBSTemplates), which has provided several custom LaTeX templates. These templates are under the inst/rmarkdown/templates/*/resources/ directories (here * denotes the template names). For example, the template for the output format MonashEBSTemplates::memo allows you to use a variable branding in the YAML metadata to control whether to include the brand logo of Monash University. This is achieved by an if statement in the template that looks like this: