Sections and chapters
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}
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.
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.
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}
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.
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}
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...
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}
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:
- Creating a document in LaTeX
- Bold, italics and underlining
- Table of contents
- Cross referencing sections and equations
- Management in a large project
- Multi-file LaTeX projects
- Hyperlinks
- Page numbering
- Single sided and double sided documents
- Multiple columns
- Counters
- Font sizes, families, and styles
titlesec
package manual
Overleaf guides
- Creating a document in Overleaf
- Uploading a project
- Copying a project
- Creating a project from a template
- Including images in Overleaf
- Exporting your work from Overleaf
- Working offline in Overleaf
- Using Track Changes in Overleaf
- Using bibliographies in Overleaf
- Sharing your work with others
- Debugging Compilation timeout errors
- How-to guides
LaTeX Basics
- Creating your first LaTeX document
- Choosing a LaTeX Compiler
- Paragraphs and new lines
- Bold, italics and underlining
- Lists
- Errors
Mathematics
- Mathematical expressions
- Subscripts and superscripts
- Brackets and Parentheses
- Fractions and Binomials
- Aligning Equations
- Operators
- Spacing in math mode
- Integrals, sums and limits
- Display style in math mode
- List of Greek letters and math symbols
- Mathematical fonts
Figures and tables
- Inserting Images
- Tables
- Positioning Images and Tables
- Lists of Tables and Figures
- Drawing Diagrams Directly in LaTeX
- TikZ package
References and Citations
- Bibliography management in LaTeX
- Bibliography management with biblatex
- Biblatex bibliography styles
- Biblatex citation styles
- Bibliography management with natbib
- Natbib bibliography styles
- Natbib citation styles
- Bibliography management with bibtex
- Bibtex bibliography styles
Languages
- Multilingual typesetting on Overleaf using polyglossia and fontspec
- International language support
- Quotations and quotation marks
- Arabic
- Chinese
- French
- German
- Greek
- Italian
- Japanese
- Korean
- Portuguese
- Russian
- Spanish
Document structure
- Sections and chapters
- Table of contents
- Cross referencing sections and equations
- Indices
- Glossaries
- Nomenclatures
- Management in a large project
- Multi-file LaTeX projects
- Hyperlinks
Formatting
- Lengths in LaTeX
- Headers and footers
- Page numbering
- Paragraph formatting
- Line breaks and blank spaces
- Text alignment
- Page size and margins
- Single sided and double sided documents
- Multiple columns
- Counters
- Code listing
- Code Highlighting with minted
- Using colours in LaTeX
- Footnotes
- Margin notes
Fonts
Presentations
Commands
Field specific
- Theorems and proofs
- Chemistry formulae
- Feynman diagrams
- Molecular orbital diagrams
- Chess notation
- Knitting patterns
- CircuiTikz package
- Pgfplots package
- Typing exams in LaTeX
- Knitr
- Attribute Value Matrices
Class files
- Understanding packages and class files
- List of packages and class files
- Writing your own package
- Writing your own class
- Tips