首页 > 解决方案 > 在 RMarkdown PDF 中,将节编号从“X Section”更改为“Section X”

问题描述

我正在使用 RMarkdown 做课程笔记,每个主要部分都对应一个给定的讲座。我想让章节标题自动格式化为“第 1 讲”、“第 2 讲”等。这基本上就是我要找的。

第1讲

翻过课程表。

第2讲

实际学习一些东西

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

1场讲座

翻过课程表。

2 讲座

确实学到了一些东西。

如何获得自动编号:

(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

标签: rlatexr-markdownknitr

解决方案


根据关于更改部分标题格式的 TeX 答案,您可以使用titlesec TeX 包更改部分格式,如下所示:

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

但是,titlesec不能与 Pandoc 一起使用:另一个问答显示您需要添加subparagraph: yes到 YAML 标头才能使其正常工作。

综上所述,以下修改应该可以为您提供所需的结果:

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

推荐阅读