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

LaTeX Installation and Configuration

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, applicable to Windows, macOS, and Linux systems.


Installing LaTeX

Windows

On Windows, it is recommended to use MiKTeX or TeX Live to install LaTeX.

Installing MiKTeX

Installing TeX Live

macOS

On macOS, it is recommended to use MacTeX to install LaTeX.

Installing MacTeX

Linux

On Linux, it is recommended to use TeX Live to install LaTeX.

Installing TeX Live


LaTeX Configuration

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 improves efficiency. Here are some common LaTeX editors:

Compiling LaTeX Documents

LaTeX documents need to be compiled to produce the final PDF file. Common compilation commands are:

The compilation commands are used as follows:



pdflatex mydocument.tex

This will generate mydocument.pdf files.

Processing Chinese Documents

If you need to process Chinese documents, it is recommended to use xelatex or lualatex compilation engine, together with ctex package. Here is a simple example of a Chinese LaTeX document:

Example

\

documentclass

[

UTF8

]{

ctexart}
\begin{document}
Hello, world!
\end{document

}

Use xelatex Compile this document:



xelatex mydocument.tex

Installing and Managing Packages

LaTeX packages can be installed and managed via the package manager. In MiKTeX and TeX Live, you can use the following commands to install packages:


Compilation Workflow

The core workflow of LaTeX is:



你编写的 .tex 源文件  →  编译器(如 pdfLaTeX)  →  生成 .pdf 文件

The compilation process also generates some auxiliary files (such as .aux, .log, .toc) that record information like the table of contents and references; they usually do not need manual management.

Common compilation commands include:

The 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.

Analyzing the Basic Document Structure

A LaTeX document is usually divided into two parts:



\documentclass{article}   % 前导区(Preamble):设置文档类型、加载宏包



\begin{document}          % 正文区:实际显示的内容

    正文内容写在这里

\end{document}

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 a 12pt font size and A4 paper for typesetting.


The Basic Structure of a Document

Every LaTeX document consists of two parts: the preamble and the body.

Anatomy of LaTeX document structure: preamble and body

Before \begin{document} is the preamble, used to declare the document class, load packages, and define global settings such as title and author.

Between \begin{document} and \end{document} is the body, where titles, sections, text, and formulas are written.

The document class determines the overall shape of the document and is declared in the first line \documentclass.

Document Class Purpose Typical scenario
article Short articles and papers, no "chapter" level Journal papers, homework, short reports
report Long reports with a "chapter" hierarchy Lab reports, course projects
book Books, including preface, chapters, and appendix Textbooks, monographs
ctexart / ctexrep / ctexbook Chinese versions of the above three Any Chinese document, choose it directly
beamer Slides Academic presentations; Part 12 covers this specifically

Document classes can also take square-bracket options, such as specifying font size and paper:

Example: Document Class with Options

% 12pt: body font size 12 points (default 10pt)

% a4paper: A4 paper (default letterbox; for printing in China it is recommended to declare explicitly)

\

documentclass

[

12pt, a4paper

]{

ctexart

}


The First Complete Document

Hello World is too simple to show LaTeX's value. The document below adds a title block, a list, and a formula, and is closer to daily use.

Example: The First Complete Document

\

documentclass

{

ctexart}
\usepackage{geometry

}

             

% load the page-setup package (required before using \geometry)

\geometry

{

a4paper, margin=2.5cm

}

 

% A4 paper, 2.5 cm margins on all sides

\

title

{

My First Complete Document

}

         

% title

\

author

{

RUNOOB

}

                   

% author

\

date

{

\today

}

                     

% \today automatically fills in the compilation date

\begin

{

document

}

\

maketitle

                       

% generate the title block (title, author, date centered)

Hello,

\

LaTeX

{

} ! This document was generated by compiling with XeLaTeX.

I like to use LaTeX for three things:

 \begin{itemize

}

                   

% unordered list environment

 

\

item

Writing math formulas, such as the Pythagorean theorem

$a^2 + b^2 = c^2$

;

 

\

item

Automatically generating section numbering and a table of contents;

 

\

item

Keeping long documents format-consistent.

\end

{

itemize}

 \end{document

}

The compiled effect:

Compilation result of the first complete document

Note a few details: \maketitle renders the title information defined in the preamble into the title block; \today automatically generated "August 20, 2026" without manual writing; the itemize list automatically adds bullets.

The specific rules of these commands will be explained one by one in later parts.


Troubleshooting common problems

When setting up the environment for the first time, encountering problems is normal. Below are the most frequent errors for beginners.

Symptom Reason Solution
Chinese displays as blank or reports an error directly The compiler is not XeLaTeX Switch in Overleaf's Menu; compile locally with the xelatex command
Undefined control sequence Command spelling error, or using a command from a package that was not loaded Check the log to locate the line number and verify spelling; confirm the corresponding package is loaded with \usepackage
File 'xxx.sty' not found The local distribution is missing this package For TeX Live use tlmgr install xxx; MiKTeX usually downloads it automatically
Table of contents and references show as ?? Compiled only once Compile once more
Overleaf compilation timeout The free version has resource limits Simplify the document or switch to local compilation