使用自定义 LaTeX class 更改 R Markdown 中的编号

Using custom LaTeX class changes numbering in R Markdown

我最近发现您可以在 R-Markdown 文档中包含您自己的 LaTeX class 以更改 PDF 的外观。这是一个最小的例子:

R 降价

---
title: "Test"
date: "`r format(Sys.time(), '%d %B, %Y')`"
documentclass: book
output:
  pdf_document:
    citation_package: natbib
    toc: yes
    toc_depth: 3
    number_sections: true
fontsize: 12pt
--- 

# A
## AA
### AAA
### AAA
## AB
# B

这按预期工作。

但是当我自己定义class时,编号前面加了0,页码没有了。

myclass.cls

我将一个名为 "myclass.cls" 的文件放在与上面的 RMD 文件相同的目录中并更改 documentclass: myclass:

\NeedsTeXFormat{LaTeX2e}

\ProvidesClass{glasgowthesis}

\LoadClass{book}

我的理解是,这应该简单地调用与上面相同的 class,但文件现在看起来像这样:

也许有人可以提示我做错了什么。我想先抄书class1:1再开始改东西

我在 bookdown book 中找到了解决方案,并想分享它以防有人通过 google 等问题转向这个问题

Note that when you change documentclass, you are likely to specify an additional Pandoc argument --top-level-division=chapter so that Pandoc knows the first-level headers should be treated as chapters instead of sections (this is the default when documentclass is book)

所以这个 YAML header 解决了这个问题:

---
title: "Test"
date: "`r format(Sys.time(), '%d %B, %Y')`"
documentclass: myclass
output:
  pdf_document:
    pandoc_args: --top-level-division=chapter
    citation_package: natbib
    toc: yes
    toc_depth: 3
    number_sections: true
fontsize: 12pt
--- 

# A
## AA
### AAA
### AAA
## AB
# B