Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

LaTeX Document Structure and Typesetting

LaTeX Document Structure and Typesetting

This section solves a document's "skeleton" problem: how to typeset the title, divide sections, generate the table of contents, and set page margins and headers/footers.

The characteristic of this content: set once, take effect throughout - this is exactly LaTeX's core advantage for long documents.


Title, Author, and Date

Three steps: declare information in the preamble, and call \maketitle in the body to render.

Example: Title Block

\

documentclass

{

ctexart}

  \title{论文标题

}

                 

% title (required; must be defined to render the title block)

\

author

{

Zhang San \and Li Si

}

         

% author, \and separates multiple authors

\

date

{

\today

}

                   

% date: \today is the current day; {} empty means not shown

\begin

{

document

}

\

maketitle

                       

% render the title block here (centered title, author, date)

\end

{

document

}

Command Purpose Common Syntax
\title{...} Declare title A manual line break \ is allowed in the title
\author{...} Declare author Zhang San \and Li Si; \thanks can add a footnote for the affiliation
\date{...} Declare date \today for the current day; handwrite a fixed date; {} to hide
\maketitle Render the title block Place at the start of the body

Section Commands and Automatic Numbering

Sections are the basic units of a structured document; LaTeX uses a set of commands to express hierarchy, with all numbering automatic.

Command Level Applicable Document Class
\part{...} Part (level 0, independent numbering) article / report / book
\chapter{...} Chapter (level 1, starts on its own page) Only report / book
\section{...} Section (level 1 in article) All
\subsection{...} Subsection (level 2) All
\subsubsection{...} Subsubsection (level 3) All
\paragraph{...} Titled paragraph (not numbered) All

Two things best show the value of "automatic": first, inserting a new section in the middle automatically shifts later numbers; second, adding an asterisk to a command (such as \section*{...}) means no number and not in the table of contents.

Example: Sections and Footnotes

\

documentclass

{

ctexart}
\begin{document}

  \section{引言}       

This is the introduction content. All section numbers are automatically generated by LaTeX.

  \subsection{研究背景}       

Here we introduce the research background\footnote{This is a footnote; numbering and typesetting are automatic.}.

  \subsection{研究意义}       

Here we explain the research significance.

  \section{方法}       

    

  \subsection{数据采集}       

    

  \subsubsection{样本选择}       

Hierarchical numbering goes down to subsubsection as the third level.

  \subsection{模型设计}       

    

  \section{结论}       

    

  \end{document

}

The compilation effect, note the auto-generated numbers 1, 1.1, 2.1.1, and the footnote at the bottom of the page:

Compilation result of auto-numbered sections and footnotes

\footnote{footnote content} places a number at the location and typeset the content at the bottom of the page; numbering is continuous across the document. Do not nest \footnote inside a footnote - that is asking for trouble.


Table of Contents Generation

The table of contents needs only one command: \tableofcontents, which scans all section commands and auto-generates a page-numbered table of contents.

Example: Generating the Table of Contents

\

documentclass

{

ctexart}
\begin{document

}

\

tableofcontents

 

% generate the table of contents here

\

newpage

         

% the table of contents occupies its own page; the body starts on the next page

\

section

{

Introduction}
Introduction content.

  \subsection{研究背景}       

Here we introduce the research background.

  \section{方法}       

Method content.

  \section{结论}       

Conclusion content.

  \end{document

}

Automatically generated table of contents

The table of contents page numbers are passed via the .aux file, so after adding sections the TOC is not immediately correct; compile once more (the old issue from Part 2).

To control TOC depth, set in the preamble: \setcounter{tocdepth}{2} means only include up to subsection; together with \setcounter{secnumdepth}{2} you can also control how deep numbering goes in the body.


Page Settings: The geometry Package

Page parameters such as margins and paper size are uniformly managed by the geometry package.

Example: Common Page Settings

% style one: options directly in the bracket before the package name (most common when few options)

\

usepackage

[

a4paper, margin=2.5cm

]{

geometry

}

% style two: load first, then set centrally with \geometry (clearer when many options)

\

usepackage

{

geometry

}

\geometry

{

  a4paper,          

% paper: A4

  margin=2.5cm,    

% uniform 2.5 cm margin on all four sides

  includefoot      

% include the footer in the text area, avoiding the footer "hanging" outside the margin

}

The two styles have exactly the same effect; choose either. Note that package options always go in square brackets [ ] - putting options in braces { } is a high-frequency beginner's mistake.

Option Meaning Example
margin Uniform margin on four sides margin=2.5cm
top / bottom / left / right Control one side separately left=3cm, right=3cm
a4paper / a5paper / letterpaper Paper size a4paper
landscape Landscape page Landscape slides or wide tables
textwidth / textheight Specify the text area width and height directly textwidth=15cm

Domestic theses commonly use "symmetric binding for odd/even pages": adding bindingoffset=1cm reserves an extra 1 cm on the binding side.


Headers and Footers: The fancyhdr Package

fancyhdr divides the header and footer each into left, center, and right slots; you fill in whatever you want.

Command Controlled Slot
\fancyhead[L]{...} (\lhead) Header left
\fancyhead[C]{...} (\chead) Header center
\fancyhead[R]{...} (\rhead) Header right
\fancyfoot[L]{...} (\lfoot) Footer left
\fancyfoot[C]{...} (\cfoot) Footer center
\fancyfoot[R]{...} (\rfoot) Footer right

Example: Classic Academic Header and Footer

\

documentclass

{

ctexart}
\usepackage[a4paper, margin=2.5cm]{geometry}
\usepackage{fancyhdr}

  \pagestyle{fancy

}

                   

% enable the fancyhdr page style

\fancyhf

{

}

                         

% first clear all slots to avoid leftover default page numbers

\fancyhead

[

L

]{

\nouppercase\leftmark

}

% header left: current level-1 section name

\fancyhead

[

R

]{

Page \thepage

}

       

% header right: page number

\fancyfoot

[

C

]{

RUNOOB LaTeX Tutorial

}

   

% footer center: custom text

\

renewcommand

{

\headrulewidth}{0.4pt

}

% thickness of the rule below the header

\begin

{

document}

  \section{页面布局}       

Body content......

  \end{document

}

Full-page effect (header left "2 Page Layout", right "Page 1", footer center is custom text):

Full-page effect of fancyhdr header and footer

\leftmark and \rightmark are two "dynamic bookmarks"; LaTeX automatically records the current section name while typesetting each page, writes it into the corresponding bookmark, and the header updates accordingly.


Page Breaks and Line Break Control

In-paragraph line breaks use \\ (Part 3); for page-level breaks there are two commands.

Command Purpose Usage recommendation
\newpage End the current page and continue from a new page Use this for daily page breaks
\clearpage Break the page and first output all pending floating figures/tables Use it at the end of a chapter or document for safety
\linebreak Force a line break here and justify Use sparingly; let automatic line breaking handle it.
\pagebreak Consider a page break here (compare with \newpage to see the difference). Use sparingly.

Do not frequently use manual \newpage to "align" the page - let LaTeX paginate automatically, so the document does not shift out of place when edited. Use manual page breaks only for structural breaks, such as after the table of contents or between chapters.


Summary

Requirements Command Location
Title area \title, \author, \date + \maketitle Declared in the preamble, rendered in the body
Section \section / \subsection / \subsubsection Body area
Footnote \footnote{...} Body area
directory \tableofcontents Body area (usually right after \begin{document})
Page margins geometry package Preamble
Headers and Footers fancyhdr package Preamble