Using a custom latex template for R markdown (and ggplot2?)

Viewed 259

So, I'm a university student and thus I'm often tasked to write short reports (usually a couple of pages) on different topics. Admittedly, using MS Word would be more than sufficient in most cases. However, I still like to give these short documents a nice layout so currently I am using LuaLaTeX and Overleaf with a bit of customization:

\documentclass[11pt]{article}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage[babelshorthands=true]{german}
\usepackage[vmargin=1in,hmargin=1.25in]{geometry}
\usepackage{xcolor}
\usepackage{titlesec}

\setmainfont{merriweather}
\setsansfont{Montserrat}

% Define  blue colours
\definecolor{Blue}{HTML}{2F5496}

% Set formats for each heading level
\titleformat{\section}{\Large\sffamily\color{Blue}}{}{0pt}{}
\titleformat{\subsection}{\large\sffamily\color{Blue}}{}{0pt}{}

\titlespacing\section{0pt}{24pt plus 4pt minus 2pt}{6pt plus 2pt minus 2pt}
\titlespacing\subsection{0pt}{12pt plus 4pt minus 2pt}{2pt plus 2pt minus 2pt}

\usepackage{parskip}

\title{Example}
\author{N N}
\date{\today}

\begin{document}
\section{Part 1}
\subsection{whats the answer to life the universe and everything?}
Lorem ipsum...

enter image description here

I usually tweak the headings a bit, depending on use case (the example above was used for a sort of "Q&A" document).

Now I am being tasked to write a short statistical analysis which I chose to do using R. The document is required to contain part of the source code as well graphs/plots. So my first thought was using an R notebook / R markdown.

Crucially though, I'd like to keep my current TeX-template as described above. Ideally, the created plots (ggplot2) should also follow along (e.g. using the merriweather font).

So, what would be the best way to integrate a (Lua)LaTeX template in R markdown? And what would be the best solution if one wanted to scale this issue up (i.e. writing a larger thesis were graphs, text and code are combined)?

On the other hand: Maybe the best way to do this, is to export the R-created graphs and insert them manually in LaTeX and copy&paste the source code.. I'm not sure.

1 Answers

You don't need a custom template. Just like you do in your tex document, you can make the desired configurations in the header of your rmarkdown document:

---
title: "Example"
author: "N N"
date: \today
output: 
  pdf_document:
    latex_engine: lualatex
    keep_tex: true
header-includes:
  - \usepackage{fontspec}
  - \usepackage{polyglossia}
  - \setmainlanguage[babelshorthands=true]{german}
  - \setmainfont{merriweather}
  - \setsansfont{Montserrat}
  - \geometry{vmargin=1in,hmargin=1.25in}
  - \usepackage{xcolor}
  - \usepackage{titlesec}
  - \definecolor{Blue}{HTML}{2F5496}
  - \titleformat{\section}{\Large\sffamily\color{Blue}}{}{0pt}{}
  - \titleformat{\subsection}{\large\sffamily\color{Blue}}{}{0pt}{}
  - \titlespacing\section{0pt}{24pt plus 4pt minus 2pt}{6pt plus 2pt minus 2pt}
  - \titlespacing\subsection{0pt}{12pt plus 4pt minus 2pt}{2pt plus 2pt minus 2pt}
  - \usepackage{parskip}
---

# Part 1

## whats the answer to life the universe and everything?

Lorem ipsum...

enter image description here

Related