跳到内容

Documents usually have some levels of chapters and/or sections to keep its contents organized. LaTeX supports this type of organization and also customization of the sectioning and numbering.

Introduction

Commands to organize a document vary depending on the document type, the simplest form of organization is the sectioning, available in all formats.


\documentclass{article}
\usepackage{blindtext}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\title{Sections and Chapters}
\author{Gubert Farnsworth}
\date{\today}

\begin{document}

\maketitle

\section{Introduction}

This is the first section.

Lorem  ipsum  dolor  sit  amet,  consectetuer  adipiscing  
elit.   Etiam  lobortisfacilisis sem.  Nullam nec mi et 
neque pharetra sollicitudin.  Praesent imperdietmi nec ante. 
Donec ullamcorper, felis non sodales...

\section{Second Section}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  
Etiam lobortis facilisissem.  Nullam nec mi et neque pharetra 
sollicitudin.  Praesent imperdiet mi necante...

\end{document}

SectionsChaptersEx1.png


The command \section{} marks the beginning of a new section, inside the braces is set the title. Section numbering is automatic and can be disabled.

  Open an example in Overleaf

Document Sectioning

LaTeX can organize, number, and index chapters and sections of document. There are up to 7 levels of depth for defining sections depending on the document class:

-1 \part{part}
0 \chapter{chapter}
1 \section{section}
2 \subsection{subsection}
3 \subsubsection{subsubsection}
4 \paragraph{paragraph}
5 \subparagraph{subparagraph}

\part and \chapter are only available in report and book document classes.

  Open an example in Overleaf

Numbered and unnumbered sections

To get an unnumbered chapter, section, sub-section, etc. add an asterisk before the opening curly brace. These will not go into the table of contents.

\documentclass{article}
\usepackage{blindtext}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\title{Sections and Chapters}
\author{Gubert Farnsworth}
\date{\today}

\begin{document}

\maketitle

\section*{Introduction}

This is the first section.

Lorem  ipsum  dolor  sit  amet,  consectetuer  adipiscing  
elit.   Etiam  lobortisfacilisis sem.  Nullam nec mi et 
neque pharetra sollicitudin.  Praesent imperdietmi nec ante. 
Donec ullamcorper, felis non sodales...

\section*{Second Section}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  
Etiam lobortis facilisissem.  Nullam nec mi et neque pharetra 
sollicitudin.  Praesent imperdiet mi necante...

\end{document}

SectionsChaptersEx2.png


To add an unnumbered section to the table of contents, use the \addcontentsline command like this:

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


See the article Table of contents for an example and description of this command.

  Open an example in Overleaf

Document chapters and sections in a Book/Report

As mentioned before, \chapter can be used in documents and reports. Below you can see an example:

\documentclass{report}

\begin{document}

\tableofcontents{}

\chapter{Editing compile}

\section{First Compile}

how to compile basic hello world into a pdf.

Write your favorite text editor create file and copy/paste the following (with hello.tex):

\subsection{Output formats}

different output formats (dvi, pdf)

The output of this command \$latex hello.tex will be a dvi 
file (hello.dvi). This file (.dvi) can be converted by \$dvipdf 
hello.dvi The get an pdf file from tex file, run this 
command \$texi2pdf hello.tex

\chapter{Document Structure}

\section{Reserved Characters}

The following symbols characters are reserved by LATEX because 
they introduce a command and have a special meaning.

\end{document}

SectionsChaptersEx3.png


Books are the most complex type of documents when it comes to grouping the content in sections. Below a complete example with parts, chapters, sections and subsections

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\title{42}
\author{Jane Doe}
\date{\today}

\begin{document}

\maketitle

\tableofcontents

\part{First Part of this document}

\chapter{First}

\section{Introduction}
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...

\subsection{Sample subsection}
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..

\subsubsection{Sample subsubsection}
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 exercita...

SectionsChaptersEx4.png


  Open an example in Overleaf

Customize chapters and sections

You can use the titlesec package to customize chapters, sections and subsections style in an easy way.

\documentclass[a4paper,12pt]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}

\titleformat
{\chapter} % command
[display] % shape
{\bfseries\Large\itshape} % format
{Story No. \ \thechapter} % label
{0.5ex} % sep
{
    \rule{\textwidth}{1pt}
    \vspace{1ex}
    \centering
} % before-code
[
\vspace{-0.5ex}%
\rule{\textwidth}{0.3pt}
] % after-code


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

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


\begin{document}

\chapter{Let's begin}

\section{First Attempt}

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{Second attempt}

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}

SectionsChaptersEx5.png


There are two general commands:

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

where [<shape>] and [<after-code>] are optional parameters, and:

  • <command> is the sectioning command to be redefined: \part, \chapter, \section, \subsection, \subsubsection, \paragraph or \subparagraph.
  • <shape> is sectioning paragraph shape; possible values are: hang, block, display, runin, leftmargin, rightmargin, drop, wrap, frame.
  • <format> is the format to be applied to the title, label, and text; for example \normalfont\Large\bfseries
  • <label> specify sectioning label.
  • <sep> is the horizontal separation between label and title body and it must be a length and not be empty.
  • <before-code> is code preceding the title body.
  • <after-code> is code following the title body.

and

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

where:

  • <left> increases the left margin.
  • <before-sep> is the vertical space before the title.
  • <after-sep> is the separation between title and non-sectioning text.

The starred version of this command (\titlespacing*) kills the indentation of the paragraph following the title.

  Open an example of the titlesec package in Overleaf

Further reading

For more information see:

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX