Using a custom template for Rmd pdf without changing current setup

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).


The rmarkdown::pdf_output function seems to do a few things differently when the default template is used. For example, it sets the variable graphics=yes. I suspect that it also sets geometry, but I have not seen where it does that. Anyway, if you want to use a custom template based on the default template you are "fighting" with this block from the template:

$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$

The geometry package is only loaded, when the geometry header is defined. One simple solution to your problem is to move the geometry definition from the header include to the YAML header:

---
geometry:
  - a4paper
  - top=38mm
  - left=45mm
  - right=45mm
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.

With this you can remove \geometry{...} from in_header.tex.