【问题标题】:How to customize header with Rmarkdown?如何使用 Rmarkdown 自定义标题?
【发布时间】:2020-01-28 23:34:19
【问题描述】:

我目前正在使用RmarkdownRStudio 上处理一个pdf 文档,但我对如何自定义我的YAML 部分感到非常困惑。这是我的实际代码:

---
title: "The very basics of R"
author: "Alejandro C."
date: "March, 2020"
output:
  pdf_document: 
    latex_engine: xelatex
    toc: true

mainfont: Times New Roman
fontsize: 12pt
header-includes:
  - \usepackage{titling}
  - \pretitle{\begin{flushleft}}
  - \posttitle{\end{flushleft}}  
  - \preauthor{\begin{flushleft}}
  - \postauthor{\end{flushleft}}  
  - \predate{\begin{flushleft}}
  - \postdate{\end{flushleft}}  

---

我想要达到的目标如下:

  • 在我的标题中使用更大的字体(例如,14)并加粗
  • 作者和日期使用 12 号字体
  • 我的文档使用 11 字体大小(我不知道这是否必须在 YAML 部分中指定)

我是这个话题的新手,所以除了R Markdown Reference Guide,我真的不知道从哪里开始。任何帮助和建议将不胜感激。

【问题讨论】:

  • 一旦您通过自定义 PDF 输出达到某个点,您使用的是 Latex,而不是 Rmarkdown,因此您最好在 tex.stackexchange.com 上查看 these 之类的问题跨度>
  • @AlejandroCarrera 我有一个答案here,不需要您编辑.tex 文件。
  • @Marius 是对的:这更像是一个 LateX 而非 RMarkdown 问题。你可以在任何你想要的地方使用 LateX 命令,当你编织 PDF 时——只需要转义一些字符。

标签: r pdf pdf-generation r-markdown


【解决方案1】:

您可以在 YAML 部分中包含LaTeX 命令,以便您可以更改字体的大小、形状和外观。请注意,您需要在 YAML 部分中复制 \(反斜杠)。

在下面的 MWE 中,使用了以下命令:

  • \textit{}
    • \\textit{} 在 YAML 中
    • 使字母变为斜体
  • \textbf{}
    • \\textbf{} 在 YAML 中
    • 使字母变为粗体
  • \fontsize{14pt}{3pt}\\selectfont
    • \\fontsize{14pt}{3pt}\\selectfont 在 YAML 中
    • 用 3pt 换行使字母为 14pt

正如@Marius here 所建议的那样,您可能希望进一步自定义输出,而不是简单的降价语法。所以,这种场合最好设置keep_tex: true

MWE

---
title: "\\fontsize{14pt}{3pt}\\selectfont \\textbf{\\textit{The very basics of R}}"
author: "\\fontsize{12pt}{3pt}\\selectfont Alejandro C."
date: "\\fontsize{12pt}{3pt}\\selectfont March, 2020"
output:
  pdf_document: 
    latex_engine: xelatex
    toc: true
    keep_tex: true
mainfont: Times New Roman
fontsize: 11pt
header-includes:
  - \usepackage{titling}
  - \pretitle{\begin{flushleft}}
  - \posttitle{\end{flushleft}}  
  - \preauthor{\begin{flushleft}}
  - \postauthor{\end{flushleft}}  
  - \predate{\begin{flushleft}}
  - \postdate{\end{flushleft}}  
---

# Lorem Ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint 
obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est 
laborum.

输出

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 2021-08-21
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2018-11-16
    相关资源
    最近更新 更多