【问题标题】:How to indent numbered sections and subsections in R Markdown (bookdown)?如何在 R Markdown(bookdown)中缩进编号的部分和小节?
【发布时间】:2022-01-18 11:18:32
【问题描述】:

我知道如何缩进文本,例如项目符号和数字列表,但我也对缩进编号的部分和子部分感兴趣。给定以下代码,它会呈现到代码之后的屏幕截图。目录缩进了小节,我也想缩进内容正文。

是否也可以缩进文档正文中的小节?之后查看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)

【问题讨论】:

  • 你可以尝试使用类似的东西: ` header-includes: - \renewcommand{\thesubsection}{\hspace{1cm}\arabic{section}.\arabic{subsection}} ` 这里,这个进一步讨论:tex.stackexchange.com/questions/60209/…
  • @Lucas 谢谢。我需要更好地理解如何将完整的 TeX 解决方案翻译成 R Markdown。

标签: r r-markdown


【解决方案1】:

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

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

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

toc 中的缩进更直接:它是为 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

【讨论】:

  • 问题作者想要不同的输出。
  • @manro OP 声明:“目录有缩进的小节,我也想~缩进内容主体~。”我的斜体,你如何解释这个问题?
  • 我不完全理解他。但我知道如何修正你的答案;)
  • @manro 您是否认为 OP 希望目录显示到第 4 级(可能是第 6 级),并且每个级别都比上一个缩进更多?这可能是对问题的理解。
  • 我想,是的...
【解决方案2】:

我不知道,但是在

bookdown::pdf_document2:

...toc_depth 只能是max = 2

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

但是,对于

output: pdf_document

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

---
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,你能解释一下这个案子吗?谢谢;)

【讨论】:

  • 感谢您的快速回答。使用titlesec 的上述解决方案也适用于bookdown::pdf_document2,但正如您所提到的,Contents 的缩进仅达到两个级别。但是,它确实深入到文档正文中的更深层次,这正是我正在寻找的。我开始使用bookdown::pdf_document2,因为它允许我通过块引用来引用图形和表格,所以我犹豫要不要离开bookdown::pdf_document2
  • @steveb 是的,虽然您可以使用此材料。我希望,Yihie 稍后会告诉我们一些关于 bookdown 中只有两个级别的内容......
猜你喜欢
  • 2022-01-14
  • 2020-12-19
  • 1970-01-01
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
相关资源
最近更新 更多