如何缩进 R Markdown(bookdown)中的编号部分和小节?

How to indent numbered sections and subsections in R Markdown (bookdown)?

我知道如何缩进项目符号和数字列表等文本,但我也对缩进编号部分和小节感兴趣。给定以下代码,它将按照代码呈现到屏幕截图。 table 的内容有小节缩进,我也想缩进内容正文。

有没有办法在文档正文中也缩进小节?之后查看 Desired Output Example 项。

---
title: "R Markdown Example With Numbered Sections"
output:
  bookdown::pdf_document2:
  toc: true
toc_depth: 6
number_sections: true
---

# Section A

## Level 2 A

### Level 3 A

#### Level 4 A

## Level 2 A

# Section B

## Level 2 B

呈现时没有缩进小节

期望的输出示例

... table of contents as above ...

1 Section A
  1.1 Level 2 A
    1.1.1 Level 3 A
      1.1.1.1 Level 4 A
  1.2 Level 2 A
2 Section B
  2.1 Level 2 B

编辑:2021-12-15

@Peter 的解决方案在 Mac 上为我工作,但需要针对我正在使用的 Linux 系统的解决方法。我发现解决方案在我的 Linux 机器上不起作用的问题是 titlesec 的 2.10 版(我有)中的错误造成的。修复可以是更新 titlesec 或使用变通方法; link 中描述了两者:titlesec: loss of section numbering with the new update (2016/03/15)

不完全确定是目录 (toc) table 中的标题还是文档 body 中的标题要缩进。

您可以使用 latex titlesec 包和 titlespacing 命令缩进文本中的标题(如最初发布的那样,然后删除@manro 的以下评论)。

然而,这仅适用于前三个级别 out of the box,在 latex-land 中通常被认为足够了。更新评论@manro 找到了解决方案。

目录中的缩进更直接:这是为 yaml 命令获取正确缩进的情况...

---
title: "R Markdown Example With Numbered Sections"
output:
  bookdown::pdf_document2:
    toc: true
    toc_depth: 6
number_sections: true

header-includes:
- \usepackage{titlesec}
---


\titlespacing{\section}{0pt}{*4}{*1.5}
\titlespacing{\subsection}{20pt}{*4}{*1.5}
\titlespacing{\subsubsection}{40pt}{*4}{*1.5}
\titlespacing{\subsubsubsection}{60pt}{*4}{*1.5} # does not work


# Section A


## Level 2 A

### Level 3 A

#### Level 4 A

## Level 2 A

# Section B

## Level 2 B

我不知道,但在

bookdown::pdf_document2:

...toc_depth 只能是 max = 2.

也许,这是一个错误?或者我做错了什么?

但是,对于

output: pdf_document

...我们可以解决这个问题(Peter,我记得你的贡献!)

---
title: "Toc is Toc"
header-includes:
- \usepackage{titlesec}

output:
  pdf_document:
    latex_engine: lualatex
    toc: yes
    number_sections: yes
    toc_depth: 4
  
documentclass: article
---

\titlespacing{\section}{0pt}{*4}{*1.5} 
\titlespacing{\subsection}{20pt}{*4}{*1.5}
\titlespacing{\subsubsection}{40pt}{*4}{*1.5} 
\titlespacing{\paragraph}{60pt}{*4}{*1.5}



\section{I don't know}
\subsection{Why}
\subsubsection{In bookdown}
\paragraph{TocDepth is only two}

P.S. Mr. Yihui Xie,你能解释一下这个案例吗?谢谢 ;)