LaTeX Installation and Configuration
LaTeX is a typesetting system based on TeX. Before using it, you need to install a TeX distribution and an editor.
This chapter introduces LaTeX installation and configuration, covering Windows, macOS, and Linux systems.
Installing LaTeX
Windows System
On Windows, it is recommended to useMiKTeXorTeX Liveto install LaTeX.
Installing MiKTeX
- Download MiKTeX: VisitMiKTeX official website, and download the installer suitable for your system.
- Run the installer: Double-click the downloaded installer and follow the prompts to install. It is recommended to choose "Complete Installation" to ensure all common packages are installed.
- Configure environment variables: After installation, make sure that MiKTeX's
bindirectory has been added to the system's PATH environment variable. This allows you to use LaTeX commands directly in the command line.
Installing TeX Live
- Download TeX Live: VisitTeX Live official website, and download the installer suitable for your system.
- Run the installer: Double-click the downloaded installer and follow the prompts to install. The TeX Live installation process is time-consuming because it includes a large number of packages and tools.
- Configure environment variables: After installation, make sure that TeX Live's
bindirectory has been added to the system's PATH environment variable.
macOS System
On macOS, it is recommended to useMacTeXto install LaTeX.
Installing MacTeX
- Download MacTeX: VisitMacTeX official website, and download the installer suitable for your system.
- Run the installer: Double-click the downloaded
.pkgfile and follow the prompts to install. MacTeX includes TeX Live and related GUI tools. - Configure environment variables: After installation, make sure that TeX Live's
bindirectory has been added to the system's PATH environment variable.
Linux System
On Linux, it is recommended to useTeX Liveto install LaTeX.
Installing TeX Live
- Install using a package manager: Most Linux distributions provide TeX Live packages. You can use a package manager to install it. For example, on Ubuntu you can use the following command:
sudo apt-get install texlive-full
This installs the full TeX Live distribution, including all commonly used packages and tools. - Manual installation: If you need the latest version of TeX Live, you can download the installation script from theTeX Live official websiteand install it manually.
Configuring LaTeX
Choosing an Editor
LaTeX documents are plain text files, so you can use any text editor to write LaTeX code. However, using a dedicated LaTeX editor can improve efficiency. Here are some common LaTeX editors:
- TeXstudio: A cross-platform LaTeX editor with powerful features, suitable for both beginners and advanced users.
- Overleaf: An online LaTeX editor that requires no installation, suitable for collaboration and quick editing.
- VS Code: A general-purpose code editor that can support LaTeX editing and compilation by installing LaTeX plugins (such as LaTeX Workshop).
Compiling LaTeX Documents
LaTeX documents need to be compiled to generate the final PDF file. Common compilation commands include:
pdflatex: Generates PDF files, suitable for most documents.xelatex: Supports Unicode and TrueType fonts, suitable for handling Chinese documents.lualatex: A compilation engine based on Lua, supporting modern fonts and advanced features.
The compilation command is used as follows:
pdflatex mydocument.tex
This will generatemydocument.pdffile.
Handling Chinese Documents
If you need to process Chinese documents, it is recommended to usexelatexorlualatexcompilation engine along with thectexpackage. Here is a simple LaTeX Chinese document example:
Example
\begin{document}
Hello, World!
\end{document}
Usexelatexto compile the document:
xelatex mydocument.tex
Installing and Managing Packages
LaTeX packages can be installed and managed through package managers. In MiKTeX and TeX Live, you can use the following commands to install packages:
- MiKTeX: Use
MiKTeX Consoleto search for and install packages. - TeX Live: Use the
tlmgrcommand to install packages. For example:tlmgr install <package-name>
Compilation Workflow
The core workflow of LaTeX is:
你编写的 .tex 源文件 → 编译器(如 pdfLaTeX) → 生成 .pdf 文件
During compilation, some auxiliary files (such as .aux, .log, .toc) are also generated. They are used to record information such as table of contents and references, and usually do not need to be managed manually.
Common compilation commands include:
- pdflatex: The most commonly used, directly generates PDF, but has weak support for Chinese and fonts
- xelatex: Supports system fonts and Chinese, recommended for Chinese users
- lualatex: A more powerful new-generation engine, slightly slower
Your First "Hello World" Document
Create a new .tex file and enter the following content:
\documentclass{article}
\begin{document}
Hello, World! 这是我的第一份 LaTeX 文档。
\end{document}
After saving, click compile, and you will get a PDF document containing this sentence.
Understanding the Basic Document Structure
A LaTeX document is usually divided into two parts:
\documentclass{article} % 前导区(Preamble):设置文档类型、加载宏包
\begin{document} % 正文区:实际显示的内容
正文内容写在这里
\end{document} - Preamble: Located between \documentclass and \begin{document}, used to declare the document type, load packages, and define global settings. This part will not appear in the final PDF.
- Body: The content between \begin{document} and \end{document} is the part that will actually be typeset and output.
Common Document Classes
\documentclass{article} % 文章、短报告,无章节 chapter
\documentclass{report} % 长报告,支持 chapter
\documentclass{book} % 书籍,支持双面排版、chapter
\documentclass{letter} % 信件
\documentclass{beamer} % 演示文稿(幻灯片)
Options can be passed in square brackets, for example:
\documentclass[12pt, a4paper]{article}This means using 12pt font size and A4 paper for typesetting.
Basic Structure of a Document
Every LaTeX document consists of two parts: the preamble and the body.
Before \begin{document} is thepreamble, which is used to declare the document class, load packages, and define global settings such as title and author.
Between \begin{document} and \end{document} is thebody, where titles, chapters, text, and formulas are written.
The document class determines the overall form of the document and is declared in the first line \documentclass.
| Document class | Purpose | Typical scenarios |
|---|---|---|
| article | Short articles and papers, without "chapter" level | Journal papers, assignments, small reports |
| report | Long reports with "chapter" level | Lab reports, course projects |
| book | Books, including preface, chapters, appendices | Textbooks, monographs |
| ctexart / ctexrep / ctexbook | Chinese versions of the above three | For any Chinese document, just choose it |
| beamer | Slides | Academic presentations, covered in detail in Article/Lesson 12 |
Document classes can also take options in square brackets, such as specifying font size and paper:
Example: Document Class with Options
% a4paper: A4 paper (default is letter; explicit declaration is recommended for domestic printing)
\documentclass[12pt, a4paper]{ctexart}
Your First Complete Document
Hello World is too simple to demonstrate the value of LaTeX. The document below adds a title block, lists, and formulas, making it much closer to everyday use.
Example: Your First Complete Document
\usepackage{geometry} % Load the page setup package (required before using \geometry)
\geometry{a4paper, margin=2.5cm} % A4 paper, 2.5 cm margins on all four sides
\title{My First Complete Document} % Title
\author{EXAMPLE} % Author
\date{\today} % \today automatically fills in the compilation date
\begin{document}
\maketitle % Generate the title block (title, author, and date displayed centered)
Hello,\LaTeX{}! This document was compiled with XeLaTeX.
I like using LaTeX for three things:
\begin{itemize} % Unordered list environment
\itemWriting math formulas, such as the Pythagorean theorem$a^2 + b^2 = c^2$;
\itemAutomatically generating section numbers and a table of contents;
\itemKeeping long documents consistently formatted.
\end{itemize}
\end{document}
Compiled result:

Note a few details: \maketitle renders the title information defined in the preamble into a title block; \today automatically generates "August 20, 2026" without manual input; the itemize list automatically adds bullets.
The detailed rules for these commands will be explained one by one in the following articles.
Troubleshooting Common Issues
When setting up your environment for the first time, encountering problems is the norm. Below are the most frequent errors reported by beginners.
| Symptom | Cause | Solution |
|---|---|---|
| Chinese text displays as blank or directly throws an error | The compiler is not XeLaTeX | Overleaf: switch it in the Menu; locally, compile with the xelatex command |
| Undefined control sequence | The command is misspelled, or a command from a package that hasn't been loaded is used | Check the log to locate the line number and verify the spelling; confirm the corresponding package is loaded with \usepackage |
| File 'xxx.sty' not found | The local distribution lacks this package | TeX Live: install with tlmgr install xxx; MiKTeX usually downloads it automatically |
| Table of contents and references display as ?? | Only compiled once | Compile again |
| Overleaf compilation timed out | The free version has resource limits | Simplify the document or switch to local compilation |