使用自定义乳胶模板进行 R 降价和 ggplot2?

问题描述

所以,我是一名大学生,因此我经常被要求撰写关于不同主题的简短报告(通常是几页)。诚然,在大多数情况下,使用 MS Word 就足够了。但是,我仍然喜欢给这些简短的文档一个漂亮的布局,所以目前我正在使用 LuaLaTeX 和 Overleaf 进行一些自定义

\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

我通常会根据用例稍微调整标题(上面的示例用于一种“问答”文档)。

现在我的任务是编写一个简短的统计分析,我选择使用 R 进行。该文档需要包含部分源代码以及图形/绘图。所以我的第一个想法是使用 R notebook / R markdown。

但至关重要的是,我想保留我当前的 TeX 模板,如上所述。理想情况下,创建的图 (ggplot2) 也应该跟上(例如使用 merriweather 字体)。

那么,在 R markdown 中集成 (Lua)LaTeX 模板的最佳方法是什么?如果有人想扩大这个问题的规模,最好的解决方案是什么(即写一篇更大的论文是将图表、文本和代码结合起来)?

另一方面:也许最好的方法是导出 R 创建的图形并在 LaTeX 中手动插入它们并复制并粘贴源代码.. 我不确定。

解决方法

您不需要自定义模板。就像在 tex 文档中所做的那样,您可以在 rmarkdown 文档的标题中进行所需的配置:

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