1. LaTeX Basics

1. LaTeX Basics #

1.1 A Bit of History #

TeX, created by Donald E. Knuth, is the underlying typesetting engine. It decides how to assemble characters, lines, and pages, and is particularly strong at mathematical typesetting.

LaTeX, originally developed by Leslie Lamport, provides a document-oriented layer on top of TeX. Instead of specifying the position of every heading, you write \section{Introduction} and let the document class choose its appearance.

The distinction between an engine and a document format matters:

Name Role
TeX The original typesetting engine.
pdfTeX An extended engine that can produce PDF directly.
XeTeX An engine with Unicode and OpenType font support.
LuaTeX An extended engine with Unicode, OpenType fonts, and Lua scripting.
pdfLaTeX, XeLaTeX, LuaLaTeX LaTeX running on the corresponding engine.

This guide uses XeLaTeX or LuaLaTeX. Modern LaTeX also incorporates features developed by the LaTeX3 project, including the command-definition interface introduced later.

1.2 Basics: Content, Design, and Typesetting #

An author supplies meaning and structure; a book designer decides how that structure should look; a typesetter implements the layout. In LaTeX, the document class and packages supply much of the design, and the engine performs the typesetting.

This separation lets you change a document’s style while preserving its content. A heading should remain a heading even if you change its font or numbering. Emphasis should remain emphasis even if the surrounding text is italic.

LaTeX offers consistent layouts, strong mathematics support, automatic numbering, and reusable source files. The trade-off is that you must learn markup and compile the source to see the result. Designing an entirely new layout takes more knowledge than writing within an existing class.

1.3 LaTeX Input Files #

A source file is plain text, normally saved as UTF-8 with a .tex extension.

Spaces and paragraphs #

Consecutive spaces usually become one space. A line break in the source normally behaves like a space. An empty line ends a paragraph; several empty lines do not add extra vertical space.

These    words are separated by ordinary spaces.
This is still the same paragraph.

This starts a new paragraph.

Use paragraphs to separate ideas. Let LaTeX find line breaks instead of adding a manual break at the end of every source line.

Comments #

A percent sign starts a comment. LaTeX ignores the rest of that source line, including its line ending:

This is visible. % This is a note to the author.

Some%
thing

The second example produces “Something”, without a space. Comments are useful both for notes and for avoiding unwanted spaces in definitions.

Special characters #

These characters are part of LaTeX’s syntax:

# $ % ^ & _ { } ~ \

To print them as text, use:

Character Command Character Command
# \# $ \$
% \% & \&
_ \_ {, } \{, \}
^ \textasciicircum{} ~ \textasciitilde{}
\ \textbackslash{}

\\ is a line-break command, not a way to print a backslash.

Commands, groups, and optional arguments #

Commands are case-sensitive. A command name is usually a backslash followed by letters, such as \emph, or a backslash followed by one non-letter, such as \%.

Braces group several characters into one argument:

A \textbf{very important} result.

Some commands accept optional arguments in square brackets:

\documentclass[a4paper,11pt]{article}

Commands made of letters consume the spaces immediately after their name. An empty group preserves a following word space:

I use \LaTeX{} for my notes.

Groups also limit the scope of many settings:

Normal text, {\bfseries bold text}, normal text again.

1.4 Input File Structure #

A complete source file has a document class, a preamble, and a body:

\documentclass[a4paper,11pt]{article}

% Preamble: packages and document-wide settings
\title{A Small Document}
\author{Alex Writer}
\date{\today}

\begin{document}
\maketitle
\tableofcontents

\section{Introduction}
This is the first section.

\section{Conclusion}
This is the last section.
\end{document}

Everything before \begin{document} is the preamble. Everything between the document environment’s opening and closing commands is the body. Content after \end{document} is ignored.

Class Typical use
article Articles, notes, and short reports.
report Longer reports with chapters.
book Books with front matter and chapters.
beamer Presentations.
letter Letters.

Do not use \chapter with article: its highest numbered division is normally \section.

1.5 A Typical Compilation Session #

Save the example as main.tex. On your computer, run:

xelatex main.tex
xelatex main.tex

Or use lualatex for both passes. The result is main.pdf. The second pass lets LaTeX read the section information written by the first pass and fill the contents list.

In TeXPage, select the compiler in the project settings and use the Compile button. If references or contents are still incomplete after an otherwise successful compilation, compile again.

An editor is not the compiler: installing an editor alone does not install LaTeX.

1.6 Logical Structure of Your Document #

Commands should describe meaning whenever possible. \emph{important} marks emphasis. It usually appears italic in upright text and upright inside italic text:

This is \emph{important}.

\emph{This sentence contains \emph{nested emphasis}.}

A command such as \textit directly requests italic type instead. Both are useful, but they express different intentions.

An environment describes a block. It begins with \begin{name} and ends with \end{name}:

\begin{quote}
A short passage that should be set apart from the surrounding text.
\end{quote}

Environments must be properly nested: the last one opened is the first one closed. The document environment occurs once in a complete document.

1.7 Packages #

Packages add features. Load them in the preamble:

\usepackage{graphicx}
\usepackage[margin=2.5cm]{geometry}
Package Adds
graphicx Image inclusion and scaling.
mathtools Extended mathematical environments; also loads amsmath.
unicode-math Unicode mathematics and OpenType math fonts.
fontspec OpenType text font selection.
booktabs Rules and spacing for readable tables.
biblatex Bibliography and citation management.
hyperref Links, PDF metadata, and bookmarks.

A package can require a specific engine or conflict with another package. Follow its manual, especially when changing a publisher’s template. The command texdoc graphicx opens installed documentation; the log lists the package versions used in a build.

1.8 The Structure of Text and Language #

Good typography supports good writing. A paragraph should develop an idea; a section should group related ideas; a displayed equation should still read as part of a sentence.

Write the text before fine-tuning its appearance. Avoid creating apparent structure with repeated spaces, empty lines, bold paragraphs, or manually typed heading numbers. Logical commands preserve the relationship between headings, numbering, contents, and references.

1.9 Files You Might Encounter #

Extension Purpose
.tex Document source.
.cls Document class.
.sty Package.
.pdf Typeset output.
.log Compilation messages and diagnostics.
.aux Labels, citation information, and other data for later passes.
.toc, .lof, .lot Contents, list of figures, and list of tables.
.bib Bibliographic database.
.bcf, .bbl, .blg Bibliography processing instructions, results, and log.
.idx, .ind, .ilg Index entries, formatted index, and index-processing log.

Edit source files, not generated auxiliary files. Keep source images, bibliography databases, and custom packages with the project so it can be rebuilt.

← How to Use This Guide · Next: Real World LaTeX →

Sources and Licence