You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建reStructuredText子章节后,如何将后续段落归属至父章节?

在reStructuredText中,子章节后能否添加主章节内容?

在reStructuredText的默认规则里,子章节之后的内容会自动归属到最后一个子章节,直接编写无法实现你想要的效果。不过有两种可行方法能让后续内容归属于主章节:

方法一:重复主章节标题并隐藏

在子章节之后重复主章节的标题,再通过格式设置隐藏这个重复标题。以Sphinx环境为例,可以添加隐藏类:

Main Section
============

This is some main section content.

Subsection 1
------------

This is the content of subsection 1.

Subsection 2
------------

This is the content of subsection 2.

.. rst-class:: hidden
Main Section
============

This is some more main section content -- I want this sentence to be "part of" "Main Section", not "Subsection 2".

渲染后重复的标题会被隐藏,后续段落就会归属到主章节。

方法二:用容器块标记内容

通过container指令给目标段落添加标记,再配合样式让它和主章节内容保持一致:

Main Section
============

This is some main section content.

Subsection 1
------------

This is the content of subsection 1.

Subsection 2
------------

This is the content of subsection 2.

.. container:: main-section-content

This is some more main section content -- I want this sentence to be "part of" "Main Section", not "Subsection 2".

之后在CSS中设置.main-section-content的样式与主章节内容对齐即可。

当然,最规范的做法还是把主章节的所有内容放在子章节之前——毕竟reStructuredText的章节结构是线性的,默认逻辑就是后续内容归属最近的上级章节。

内容的提问来源于stack exchange,提问作者StoneThrow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 01:05:28