> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-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{Introduction}

这是第一节。

\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{part}`                   |
| 0  | `\chapter{chapter}`             |
| 1  | `\section{section}`             |
| 2  | `\subsection{subsection}`       |
| 3  | `\subsubsection{subsubsection}` |
| 4  | `\paragraph{paragraph}`         |
| 5  | `\subparagraph{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{Introduction}
这是第一节（带编号）。

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

\shortlorem

\section{Second 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` 可用于书籍和报告中。

### 报告类

下面你可以看到一个示例 `报告` ，使用了摘自 Overleaf 文章的文本 [LuaTeX 入门（第 1 部分）：它是什么——以及它为何如此不同？](/latex/zh-cn/shen-du-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{Introduction}
就 \TeX{} 而言，Lua\TeX{} 尽管已经活跃开发了 10 多年，但仍是“刚到场的新成员”。

\subsection{Lua\TeX：打开 \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)

### 书籍类

下面的示例重现了 `报告` 示例中的文本，但使用了 `\documentclass{book}`，包含部分、章、节、小节和小小节。如果你在 Overleaf 中打开该示例，你应该会看到由 `\subsubsection` 生成的三级小节是 *不* 带编号的。这是 `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{Introduction}
就 \TeX{} 而言，Lua\TeX{} 尽管已经活跃开发了 10 多年，但仍是“刚到场的新成员”。

\subsection{Lua\TeX：打开 \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{第一次尝试}

洛雷姆·伊普苏姆 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{第二次尝试}

洛雷姆·伊普苏姆 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}
```

[在 `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>]
```

其中 `[<shape>]` 和 `[<after-code>]` 是可选参数，并且：

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

和

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

其中：

* `<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://ayakaleaf-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.
