如何在R Markdown(bookdown)中缩进编号的章节和子章节?

4
我知道如何缩进文本,例如项目符号和数字列表,但我还想缩进编号的章节和子章节。 给定以下代码,它呈现为代码后面的屏幕截图。目录有子标题缩进,我也想缩进内容正文。

是否有一种方法也可以缩进文档正文中的子标题?请参见之后的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 或使用一个变通方法;这两种方法都在此链接中描述:titlesec: loss of section numbering with the new update (2016/03/15)


1
你可以尝试使用以下代码:` header-includes:
  • \renewcommand{\thesubsection}{\hspace{1cm}\arabic{section}.\arabic{subsection}} `
在这里,这个问题有更详细的讨论:https://tex.stackexchange.com/questions/60209/how-to-add-an-extra-level-of-sections-with-headings-below-subsubsection
- Lucas
@Lucas 谢谢。我需要更好地理解如何将完整的 TeX 解决方案翻译为 R Markdown。 - steveb
2个回答

2
我不确定是目录中的标题还是文档正文中的标题需要缩进。
您可以使用latex标题包“titlesec”和“titlespacing”命令来缩进文本中的标题(如最初发布的内容,然后根据@manro的评论进行了删除)。
但是,这仅适用于前三级,“开箱即用”,在latex领域中通常被认为足够。更新注释@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

enter image description here


问题的作者希望得到不同的输出。 - manro
@manro OP说:“目录有子节标题缩进,我也想缩进正文内容。” 我的斜体字,你如何解释这个问题? - Peter
我不完全理解他。但是我知道如何稍微修正你的答案 ;) - manro
@manro,你认为OP想要目录显示到第4级(可能是第6级),每个级别比上一个缩进更多吗?这可能是对问题的理解。 - Peter
@Peter 上面的答案在我在 Mac 上运行时有效,但是当我将 - \usepackage{titlesec} 添加到 header-includes 部分后,在我的 Linux 系统上节号就消失了。我不确定如何解决这个问题,但是 bookdown 的版本是相同的。 - steveb
显示剩余4条评论

1
我不知道,但是在IT技术中,“

”表示段落。

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. 谢益辉先生,您能为这个案例提供一些解释吗?感谢;)


1
感谢您的快速回答。上面使用 titlesec 的解决方案同样适用于 bookdown::pdf_document2,但正如您所提到的,Contents 的缩进仅限于两个级别。然而,它确实在文档正文的更深层级中起作用,这正是我想要的。我开始使用 bookdown::pdf_document2 是因为它允许我通过块引用引用图表,因此我不愿意放弃 bookdown::pdf_document2 - steveb
1
@steveb 是的,您可以使用这个材料。我希望 Yihie 以后能告诉我们关于 bookdown 中仅有两个级别的事情... - manro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接