Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a custom template for Rmd pdf without changing current setup

I have recently learned that modifying the default (Pandoc?) template for a Rmd pdf report gives access to some neat customisation.

So in order to modify the default template I first move it to my working directory using:

file.copy(system.file("rmd/latex/default-1.17.0.2.tex", package ="rmarkdown"), "template.tex")

Next I type up my Rmd file:

---
output:
  pdf_document:
    template: template.tex
    keep_tex: true
    latex_engine: xelatex
    includes:
      in_header: in_header.tex
    number_sections: true
---

This is a rmd kind of document.

Where in_header.tex includes only one line

\geometry{a4paper, top=38mm, left=45mm, right=45mm}

I first just want to run the Rmd file with the template as is (expecting no change) but the report fails to compile if and only if I include the template - otherwise it runs without an issue.

The error is

! Undefined control sequence.
l.58 \geometry
[...]

Question

How can I use the template argument to build on an editable template in my working folder without causing any other change to my current setup.

Info

> R.Version()[["version.string"]]
[1] "R version 3.5.1 (2018-07-02)"
> packageVersion("rmarkdown")
[1] ‘1.10’
> packageVersion("knitr")
[1] ‘1.20’
like image 838
sindri_baldur Avatar asked Oct 18 '18 14:10

sindri_baldur


People also ask

What is an RMD template?

An .Rmd template is essentially the replacement for a worksheet. It is a partially-filled out R Markdown file that you can create. You can create a template for every homework assignment or exercise that you want your students to work through. And they can access all of these templates easily when they go to File > New File > R Markdown.

How to create a custom document template in R Markdown?

As we just mentioned, the document template.docx has to be generated from Pandoc. You can create this template from an arbitrary R Markdown document with the word_document output format (the actual content of this document does not matter, but it should contain the type of elements of which you want to style).

Can I customize the latex template used to create PDF documents?

Many aspects of the LaTeX template used to create PDF documents can be customized using top-level YAML metadata (note that these options do not appear underneath the output section, but rather appear at the top level along with title, author, and so on). For example:

How do I create a PDF document from R Markdown?

3.3. PDF document. To create a PDF document from R Markdown, you specify the pdf_document output format in the YAML metadata: --- title: "Habits" author: John Doe date: March 22, 2005 output: pdf_document ---. Within R Markdown documents that generate PDF output, you can use raw LaTeX, and even define LaTeX macros.


1 Answers

As you are already driving this from R, you may as well use the (excellent) example set up by the rticles package which provides a larged number of LaTeX customizations for (academic) papers.

And you can then run with that them. For example, over the last two years I added these packages:

  • tint for a modern 'Tufte-alike' writeup
  • pinp for very nice two-column pdf vignettes
  • link for LaTeX letters and some extra
  • binb for variants of beaner packages

This allows you to

  • set a custom template.tex
  • include whichever LaTeX class files / style files you need
  • programmatically set options

I find this preferable to copying stanzas around which I did previously (eg for slides).

like image 139
Dirk Eddelbuettel Avatar answered Sep 24 '22 09:09

Dirk Eddelbuettel