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