> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/zh-cn/wen-dang-jie-gou/01-sections-and-chapters.md).

# 章节和小节

## 引言

文档通常具有某种“逻辑结构”：划分为章节、节、小节等，以组织其内容。LaTeX 支持创建文档结构，也能自定义分节和编号。用于组织文档的命令取决于所使用的文档类，不过最简单的组织形式——分节——在所有格式中都可用。

## 基本示例

让我们从一个基本示例开始，来演示 `\section{*section title*}` 命令，它标记一个名为 `*section title*`。节编号是自动的，并且可以自定义，或禁用。

```latex
\documentclass{article}
\usepackage{blindtext}

\title{章节与分节}
\author{Overleaf}
\date{\today}

\begin{document}
\maketitle
\section{引言}

这是第一节。

\blindtext

\section{第二节}
这是第二个节

\blindtext
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+document+structure+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bblindtext%7D%0A%0A%5Ctitle%7BSections+and+Chapters%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7B%5Ctoday%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0A%0AThis+is+the+first+section.%0A%0A%5Cblindtext%0A%0A%5Csection%7BSecond+Section%7D%0AThis+is+the+second+section%0A%0A%5Cblindtext%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![章节和节示例](/files/6bc7aceafb79c4af0e06b238ec2dcf8ba255a3c9)

## 文档分节

LaTeX 可以组织、编号并建立文档章节和节的索引。根据文档类，定义节的深度最多可达 7 层：

|    |                       |
| -- | --------------------- |
| -1 | `\part{部分}`           |
| 0  | `\chapter{章}`         |
| 1  | `\section{节}`         |
| 2  | `\subsection{小节}`     |
| 3  | `\subsubsection{子小节}` |
| 4  | `\paragraph{段落}`      |
| 5  | `\subparagraph{子段落}`  |

通常， `\section` 是在大多数文档中的顶层文档命令。不过，在报告或书籍以及类似的长文档中，这将是 `\chapter` 或 `\part`.

## 带编号与不带编号的节

要获得不带编号的章、节、小节等，在命令末尾、左花括号前加一个星号（`*`）。这些内容不会进入目录。下面是我们的第一个示例（上面那个），但这次使用 `\section*` 而不是 `\section`:

```latex
\documentclass{article}
\usepackage{blindtext}

\title{章节与分节}
\author{Overleaf}
\date{\today}

\begin{document}
\maketitle
\section*{Introduction}

这是第一节。

\blindtext

\section*{Second Section}
这是第二个节

\blindtext
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+with+unnumbered+sections\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bblindtext%7D%0A%0A%5Ctitle%7BSections+and+Chapters%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7B%5Ctoday%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%2A%7BIntroduction%7D%0A%0AThis+is+the+first+section.%0A%0A%5Cblindtext%0A%0A%5Csection%2A%7BSecond+Section%7D%0AThis+is+the+second+section%0A%0A%5Cblindtext%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![不带编号的节示例](/files/334083b1a42a666b5ee243f05791d2f245cf6c68)

### 目录中的不带编号节

要将一个不带编号的节添加到目录中，请使用 `\addcontentsline` 命令，如下：

```latex
\addcontentsline{toc}{section}{Title of the section}
```

这里有一个使用 `\addcontentsline` 的示例，但请参见文章 [目录](/latex/zh-cn/wen-dang-jie-gou/02-table-of-contents.md) 以了解更多信息和示例。

```latex
\documentclass{article}
\title{章节与分节}
\author{Overleaf}
\date{\today}

\begin{document}
\maketitle
\tableofcontents

\newcommand\shortlorem{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}

\section{引言}
这是第一个节（带编号）。

\shortlorem
\addcontentsline{toc}{section}{未编号节}
\section*{未编号节}
一个不带编号的节

\shortlorem

\section{第二节}
第二个带编号的节。

\shortlorem
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+unnumbered+sections+in+the+TOC\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7BSections+and+Chapters%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7B%5Ctoday%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Ctableofcontents%0A%0A%5Cnewcommand%5Cshortlorem%7BLorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit%2C+sed+do+eiusmod+tempor+incididunt+ut+labore+et+dolore+magna+aliqua.+Ut+enim+ad+minim+veniam%2C+quis+nostrud+exercitation+ullamco+laboris+nisi+ut+aliquip+ex+ea+commodo+consequat.%7D%0A%0A%5Csection%7BIntroduction%7D%0AThis+is+the+first+section+%28numbered%29.%0A%0A%5Cshortlorem%0A%5Caddcontentsline%7Btoc%7D%7Bsection%7D%7BUnnumbered+Section%7D%0A%5Csection%2A%7BUnnumbered+Section%7D%0AAn+unnumbered+section%0A%0A%5Cshortlorem%0A%0A%5Csection%7BSecond+section%7D%0AThe+second+numbered+section.%0A%0A%5Cshortlorem%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![目录中的不带编号节](/files/adf0592f7b641ea8d233e0fb941edfa3e425bde6)

## 书籍/报告中的文档章和节

如前所述， `\chapter` 可用于书籍和报告。

### report 类

下面你可以看到一个示例 `报告` 使用取自 Overleaf 文章的文本 [LuaTeX 简介（第 1 部分）：它是什么——又是什么让它如此不同？](/latex/zh-cn/shen-ru-wen-zhang/07-an-introduction-to-luatex-part-1-what-is-it-and-what-makes-it-so-different.md)

```latex
\documentclass{report}
\title{章节与分节}
\author{Overleaf}
\date{\today}
\begin{document}
\maketitle
\tableofcontents
\chapter{Lua\TeX 简介}

\section{它是什么——又是什么让它如此不同？}
Lua\TeX{} 是一个 \textit{工具箱}——它包含复杂的软件工具和组件，借助它们你可以构建（排版）各种各样的文档。本文的副标题也提出了两个关于 Lua\TeX 的问题：它是什么——又是什么让它如此不同？对于“它是什么？”这个问题，答案似乎很明显：“它是一个 \TeX{} 排版引擎！”确实如此，但更广阔的视角、也是本文作者所认同的观点是，Lua\TeX{} 是一个极其灵活的、基于 \TeX{} 的文档构建与工程系统。

\subsection{解释 Lua\TeX：从何开始？}
本文关于 Lua\TeX{} 的第一篇文章旨在提供一个理解这个 TeX 引擎所提供功能的背景，并说明其设计为何/如何使用户能够构建/设计/创建各种复杂排版与设计问题的解决方案——或许还提供某种程度的“未来适应性”

\chapter{Lua\TeX：背景与历史}
\section{引言}
从 \TeX{} 的角度看，Lua\TeX{} 虽然已在积极开发超过 10 年，但仍可算是“新来的”。

\subsection{揭开 \TeX 的“黑盒”}
Knuth 最初的 \TeX{} 程序是当今所有现代 \TeX{} 引擎的共同祖先，而 Lua\TeX{} 实际上是最新的演化步骤：它源自 pdf\TeX{} 程序，但增加了一些强大的软件组件，带来了大量额外功能。
\end{document}
```

[在 Overleaf 中打开这个示例（使用 `lualatex`)](<https://www.overleaf.com/docs?engine=lualatex\&snip_name=Example+of+a+LaTeX+report\&snip=\documentclass{report}&#xA;\title{Sections+and+Chapters}&#xA;\author{Overleaf}&#xA;\date{\today}&#xA;\begin{document}&#xA;\maketitle&#xA;\tableofcontents&#xA;\chapter{An+Introduction+to+Lua\TeX}&#xA;&#xA;\section{What+is+it—and+what+makes+it+so+different?}&#xA;Lua\TeX{}+is+a+\textit{toolkit}—it+contains+sophisticated+software+tools+and+components+with+which+you+can+construct+(typeset)+a+wide+range+of+documents.+The+sub-title+of+this+article+also+poses+two+questions+about+Lua\TeX:+What+is+it—and+what+makes+it+so+different?+The+answer+to+“What+is+it?”+may+seem+obvious:+“It’s+a+\TeX{}+typesetting+engine!”+Indeed+it+is,+but+a+broader+view,+and+one+to+which+this+author+subscribes,+is+that+Lua\TeX{}+is+an+extremely+versatile+\TeX-based+document+construction+and+engineering+system.&#xA;&#xA;\subsection{Explaining+Lua\TeX:+Where+to+start?}&#xA;The+goal+of+this+first+article+on+Lua\TeX{}+is+to+offer+a+context+for+understanding+what+this+TeX+engine+provides+and+why/how+its+design+enables+users+to+build/design/create+a+wide+range+of+solutions+to+complex+typesetting+and+design+problems—perhaps+also+offering+some+degree+of+“future+proofing”+&#xA;&#xA;\chapter{Lua\TeX:+Background+and+history}&#xA;\section{Introduction}&#xA;Lua\TeX{}+is,+in+\TeX{}+terms,+“the+new+kid+on+the+block”+despite+having+been+in+active+development+for+over+10+years.&#xA;&#xA;\subsection{Lua\TeX:+Opening+up+\TeX’s+“black+box”}&#xA;Knuth’s+original+\TeX{}+program+is+the+common+ancestor+of+all+modern+\TeX{}+engines+in+use+today+and+Lua\TeX{}+is,+in+effect,+the+latest+evolutionary+step:+derived+from+the+pdf\TeX{}+program+but+with+the+addition+of+some+powerful+software+components+which+bring+a+great+deal+of+extra+functionality.&#xA;\end{document}>)

本示例产生如下输出——这里展示第 2–4 页，其中页面图像已重叠以便展示：

![一个典型的 LaTeX 报告](/files/feb548127a6e0fa8d5d96657bbf61e3f189f0da0)

### book 类

下面的示例重现了 `报告` 示例中的文本，但使用 `\documentclass{book}`，包含 part、chapter、section、subsection 和 sub-subsection。如果你在 Overleaf 中打开该示例，你应该会看到由 `\subsubsection` 已 *不* 产生的 sub-subsection 会被 `book` 类的设计如此：如果你想更改这一行为，请在文档导言区加入以下命令：

```latex
\setcounter{secnumdepth}{3}
```

```latex
\documentclass{book}
\title{章节与分节}
\author{Overleaf}
\date{\today}
\begin{document}
\maketitle
\tableofcontents
\part{Lua\TeX 的历史}

\chapter{Lua\TeX 简介}

\section{它是什么——又是什么让它如此不同？}
Lua\TeX{} 是一个 \textit{工具箱}——它包含复杂的软件工具和组件，借助它们你可以构建（排版）各种各样的文档。本文的副标题也提出了两个关于 Lua\TeX 的问题：它是什么——又是什么让它如此不同？对于“它是什么？”这个问题，答案似乎很明显：“它是一个 \TeX{} 排版引擎！”确实如此，但更广阔的视角、也是本文作者所认同的观点是，Lua\TeX{} 是一个极其灵活的、基于 \TeX{} 的文档构建与工程系统。

\subsection{解释 Lua\TeX：从何开始？}
本文关于 Lua\TeX{} 的第一篇文章旨在提供一个理解这个 TeX 引擎所提供功能的背景，并说明其设计为何/如何使用户能够构建/设计/创建各种复杂排版与设计问题的解决方案——或许还提供某种程度的“未来适应性”

\chapter{Lua\TeX：背景与历史}
\section{引言}
从 \TeX{} 的角度看，Lua\TeX{} 虽然已在积极开发超过 10 年，但仍可算是“新来的”。

\subsection{揭开 \TeX 的“黑盒”}
Knuth 最初的 \TeX{} 程序是当今所有现代 \TeX{} 引擎的共同祖先，而 Lua\TeX{} 实际上是最新的演化步骤：它源自 pdf\TeX{} 程序，但增加了一些强大的软件组件，带来了大量额外功能。

\subsubsection{Lua\TeX{} 如何处理 \texttt{\string\directlua}：初窥}
提供给 \verb|\directlua{<code>}| 的 ⟨code⟩ 会先使用上面讨论的过程和计算转换为记号；该记号序列会存储在一个记号列表中。
\end{document}
```

[要查看输出， **在 Overleaf 中打开这个示例** (它使用 `**lualatex**`)](<https://www.overleaf.com/docs?engine=lualatex\&snip_name=Example+of+a+LaTeX+book\&snip=\documentclass{book}&#xA;\title{Sections+and+Chapters}&#xA;\author{Overleaf}&#xA;\date{\today}&#xA;\begin{document}&#xA;\maketitle&#xA;\tableofcontents&#xA;\part{History+of+Lua\TeX}&#xA;&#xA;\chapter{An+Introduction+to+Lua\TeX}&#xA;&#xA;\section{What+is+it—and+what+makes+it+so+different?}&#xA;Lua\TeX{}+is+a+\textit{toolkit}—it+contains+sophisticated+software+tools+and+components+with+which+you+can+construct+(typeset)+a+wide+range+of+documents.+The+sub-title+of+this+article+also+poses+two+questions+about+Lua\TeX:+What+is+it—and+what+makes+it+so+different?+The+answer+to+“What+is+it?”+may+seem+obvious:+“It’s+a+\TeX{}+typesetting+engine!”+Indeed+it+is,+but+a+broader+view,+and+one+to+which+this+author+subscribes,+is+that+Lua\TeX{}+is+an+extremely+versatile+\TeX-based+document+construction+and+engineering+system.&#xA;&#xA;\subsection{Explaining+Lua\TeX:+Where+to+start?}&#xA;The+goal+of+this+first+article+on+Lua\TeX{}+is+to+offer+a+context+for+understanding+what+this+TeX+engine+provides+and+why/how+its+design+enables+users+to+build/design/create+a+wide+range+of+solutions+to+complex+typesetting+and+design+problems—perhaps+also+offering+some+degree+of+“future+proofing”+&#xA;&#xA;\chapter{Lua\TeX:+Background+and+history}&#xA;\section{Introduction}&#xA;Lua\TeX{}+is,+in+\TeX{}+terms,+“the+new+kid+on+the+block”+despite+having+been+in+active+development+for+over+10+years.&#xA;&#xA;\subsection{Lua\TeX:+Opening+up+\TeX’s+“black+box”}&#xA;Knuth’s+original+\TeX{}+program+is+the+common+ancestor+of+all+modern+\TeX{}+engines+in+use+today+and+Lua\TeX{}+is,+in+effect,+the+latest+evolutionary+step:+derived+from+the+pdf\TeX{}+program+but+with+the+addition+of+some+powerful+software+components+which+bring+a+great+deal+of+extra+functionality.&#xA;&#xA;\subsubsection{How+Lua\TeX{}+processes+\texttt{\string\directlua}:+A+first+look}&#xA;The+⟨code⟩+provided+to+\verb|\directlua{\<code\>}|+is+first+converted+to+tokens+using+the+processes+and+calculations+discussed+above;+that+sequence+of+tokens+is+stored+in+a+token+list.&#xA;\end{document}>)

## 自定义章节和节

你可以使用 [`titlesec`](https://ctan.org/pkg/titlesec?lang=en) 宏包以一种简单的方式自定义章节、节和小节的样式。

```latex
\documentclass[a4paper,12pt]{book}
\usepackage[T1]{fontenc}
\usepackage{titlesec}

\titleformat
{\chapter} % 命令
[display] % 形状
{\bfseries\Large\itshape} % 格式
{故事编号。\ \thechapter} % 标签
{0.5ex} % 间距
{
    \rule{\textwidth}{1pt}
    \vspace{1ex}
    \centering
} % 前置代码
[
\vspace{-0.5ex}%
\rule{\textwidth}{0.3pt}
] % 后置代码

\titleformat{\section}[wrap]
{\normalfont\bfseries}
{\thesection.}{0.5em}{}

\titlespacing{\section}{12pc}{1.5ex plus .1ex minus .2ex}{1pc}

\begin{document}
\chapter{开始吧}
\section{第一次尝试}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris...

\section{第二次尝试}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris...

\end{document}
```

[在 Overleaf 中打开此 `titlesec` Overleaf 中的示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=titlesec+example\&snip=%5Cdocumentclass%5Ba4paper%2C12pt%5D%7Bbook%7D%0A%5Cusepackage%5BT1%5D%7Bfontenc%7D%0A%5Cusepackage%7Btitlesec%7D%0A%0A%5Ctitleformat%0A%7B%5Cchapter%7D+%25+command%0A%5Bdisplay%5D+%25+shape%0A%7B%5Cbfseries%5CLarge%5Citshape%7D+%25+format%0A%7BStory+No.+%5C+%5Cthechapter%7D+%25+label%0A%7B0.5ex%7D+%25+sep%0A%7B%0A++++%5Crule%7B%5Ctextwidth%7D%7B1pt%7D%0A++++%5Cvspace%7B1ex%7D%0A++++%5Ccentering%0A%7D+%25+before-code%0A%5B%0A%5Cvspace%7B-0.5ex%7D%25%0A%5Crule%7B%5Ctextwidth%7D%7B0.3pt%7D%0A%5D+%25+after-code%0A%0A%0A%5Ctitleformat%7B%5Csection%7D%5Bwrap%5D%0A%7B%5Cnormalfont%5Cbfseries%7D%0A%7B%5Cthesection.%7D%7B0.5em%7D%7B%7D%0A%0A%5Ctitlespacing%7B%5Csection%7D%7B12pc%7D%7B1.5ex+plus+.1ex+minus+.2ex%7D%7B1pc%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Cchapter%7BLet%27s+begin%7D%0A%5Csection%7BFirst+Attempt%7D%0A%0ALorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit%2C+sed+do+%0Aeiusmod+tempor+incididunt+ut+labore+et+dolore+magna+aliqua.+Ut+%0Aenim+ad+minim+veniam%2C+quis+nostrud+exercitation+ullamco+laboris...%0A%0A%5Csection%7BSecond+attempt%7D%0A%0ALorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit%2C+sed+do+%0Aeiusmod+tempor+incididunt+ut+labore+et+dolore+magna+aliqua.+Ut+%0Aenim+ad+minim+veniam%2C+quis+nostrud+exercitation+ullamco+laboris...%0A%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![Titlesecolv2.png](/files/ddc4d791fbf3fb56825a1bd3f86a31e998da6003)

### titlesec 命令

有两个通用命令：

```latex
 \titleformat{<command>}[<shape>]{<format>}{<label>}{<sep>}{<before-code>}[<after-code>]
```

其中 `[<形状>]` 和 `[<后置代码>]` 是可选参数，并且：

* `<命令>` 是要重新定义的分节命令： `\part`, `\chapter`, `\section`, `\subsection`, `\subsubsection`, `\paragraph` 或 `\subparagraph`.
* `<形状>` 是分节段落的形状；可取值为： `hang`, `block`, `显示`, `runin`, `leftmargin`, `rightmargin`, `drop`, `wrap` 和 `frame`.
* `<格式>` 是应用于标题、标签和正文的格式；例如 `\normalfont\Large\bfseries`
* `<标签>` 指定分节标签。
* `<间距>` 是标签与标题正文之间的水平间距，它必须是一个长度值且不能为空。
* `<前置代码>` 是标题正文前的代码。
* `<后置代码>` 是标题正文后的代码。

和

```latex
 \titlespacing{<command>}{<left>}{<before-sep>}{<after-sep>}
```

其中：

* `<左侧>` 增加左边距。
* `<前间距>` 是标题前的垂直空间。
* `<后间距>` 是标题与非分节文本之间的间距。

该命令的带星号版本（`\titlespacing*`）会取消标题后段落的首行缩进。

## 进一步阅读

更多信息请参见：

* [在 LaTeX 中创建文档](/latex/zh-cn/latex-ji-chu/01-learn-latex-in-30-minutes.md)
* [加粗、斜体和下划线](/latex/zh-cn/latex-ji-chu/03-bold-italics-and-underlining.md)
* [目录](/latex/zh-cn/wen-dang-jie-gou/02-table-of-contents.md)
* [章节和公式的交叉引用](/latex/zh-cn/wen-dang-jie-gou/03-cross-referencing-sections-equations-and-floats.md)
* [大型项目中的管理](/latex/zh-cn/wen-dang-jie-gou/07-management-in-a-large-project.md)
* [多文件 LaTeX 项目](/latex/zh-cn/wen-dang-jie-gou/08-multi-file-latex-projects.md)
* [超链接](/latex/zh-cn/wen-dang-jie-gou/09-hyperlinks.md)
* [页码编号](/latex/zh-cn/ge-shi-hua/03-page-numbering.md)
* [单面和双面文档](/latex/zh-cn/ge-shi-hua/08-single-sided-and-double-sided-documents.md)
* [多栏](/latex/zh-cn/ge-shi-hua/09-multiple-columns.md)
* [计数器](/latex/zh-cn/ge-shi-hua/10-counters.md)
* [字体大小、字体族和样式](/latex/zh-cn/zi-ti/01-font-sizes-families-and-styles.md)
* [`titlesec` 宏包手册](http://mirrors.ctan.org/macros/latex/contrib/titlesec/titlesec.pdf)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/zh-cn/wen-dang-jie-gou/01-sections-and-chapters.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
