在 RMarkdown PDF 中,将章节编号从 "X Section" 更改为 "Section X"

In RMarkdown PDF, change section numbering from "X Section" to "Section X"

我正在使用 RMarkdown 做课程笔记,每个主要部分对应给定的讲座。我想让 headers 部分自动格式化为 "Lecture 1"、"Lecture 2" 等。这基本上就是我要找的内容。

Lecture 1

Going over syllabus.

Lecture 2

Actually learning some stuff

然而,当我使用 RMarkdown 的默认设置时,我得到以下格式(名称前面有节号):

1 Lecture

Going over syllabus.

2 Lecture

Actually learning some stuff.

如何将自动编号设置为:

(1) 跟随姓名(例如 "October 1st - Lecture 1")

(2) 在名称中被引用(例如,使用某种伪代码“October 1st - Lecture {%section_number%}”)?

下面是 RMarkdown 代码的最小可重现示例,可以编织成 PDF。

---
title: "Course_Notes"
output: 
  pdf_document:
    number_sections: true
---

# Lecture
Going over the syllabus.

# Lecture
Actually learning some stuff

根据a TeX answer on altering the section title format,您可以使用titlesec TeX 包来更改节格式如下:

\usepackage[explicit]{titlesec}
\titleformat{\section}{\normalfont\Large\bfseries}{}{0em}{#1\ \thesection}

但是,titlesec 不能与 Pandoc 开箱即用:another Q&A 显示您需要将 subparagraph: yes 添加到 YAML header 让它工作。

将它们放在一起,进行以下修改应该可以得到您想要的结果:

---
title: "Course_Notes"
output: 
  pdf_document:
    number_sections: true
header-includes:
  - \usepackage[explicit]{titlesec}
  - \titleformat{\section}{\normalfont\Large\bfseries}{}{0em}{#1\ \thesection}
subparagraph: yes
---

# Lecture
Going over the syllabus.

# Lecture
Actually learning some stuff