What LaTeX is and how it builds a document
LaTeX is a document preparation system. You write a plain-text source file, usually ending in .tex, and a LaTeX engine turns that file into a finished document, most often a PDF. The source describes the structure and content: this is a section heading, this is an equation, this is a list. LaTeX handles the layout.
That is different from a word processor such as Microsoft Word, where you see the formatted page as you type. LaTeX is often described as “what you see is what you mean.” You mark up what each piece of text is, and the compiler decides how it looks on the page.
Three parts work together:
- The source file is your
.textext. - The engine is a program such as
pdflatexthat reads the source and produces a PDF. - The distribution is the installed collection of engines, fonts, and packages. Common examples are TeX Live, MiKTeX, and MacTeX.
An editor is where you write the source. Many editors also run the engine for you with a build button. Overleaf does all of this in a browser, so there is nothing to install.
Does anyone still use LaTeX?
Yes. LaTeX is still widely used in academic work, especially in mathematics, physics, computer science, and engineering. Many journals and conferences publish LaTeX templates, and theses are often written in LaTeX.
What is LaTeX code?
LaTeX code is the markup inside a .tex file. It mixes ordinary text with commands. Four pieces appear in almost every document:
- Commands start with a backslash, such as
\sectionor\textbf. - Arguments go in curly braces after a command, such as
\section{Introduction}. - Environments start with
\begin{...}and end with\end{...}, such as\begin{itemize}. - Comments start with a percent sign
%. The compiler ignores everything after a%on that line.
\section{Results} % a comment, ignored by the compiler
\textbf{Bold text} is written with a command.
Visual tools can write this code for you. LyX, covered below, shows a document in a window that looks like a word processor, and it generates LaTeX underneath.
A minimal document
Here is a complete small document. Save it as main.tex:
\documentclass[11pt]{article}
\title{My First Document}
\author{Your Name}
\date{\today}
\begin{document}
\maketitle
\section{Introduction}
Hello, world. This is my first LaTeX document.
Inline math such as $E = mc^{2}$ sits inside a sentence.
Display math gets its own line:
\[
a^{2} + b^{2} = c^{2}
\]
\end{document}
Preamble and body
- The preamble is everything before
\begin{document}. It sets the document class with\documentclass, loads packages with\usepackage, and holds the title, author, and date. Common classes includearticlefor short papers,reportandbookfor longer work,letterfor letters, andbeamerfor slides. See chapters and document classes, writing letters in LaTeX, and making a presentation with Beamer. - The body is everything between
\begin{document}and\end{document}. Text, sections, equations, tables, and figures go here.
Compiling the file
Run the engine on your file from a terminal:
pdflatex main.tex
This creates main.pdf in the same folder. Most editors have a build or compile button that runs the same command. Overleaf has a “Recompile” button that runs it on its servers.
For projects with references, many people use latexmk, which runs the engine as many times as needed:
latexmk -pdf main.tex
Why you sometimes compile twice
LaTeX numbers sections, figures, and equations, and it writes cross-references and the table of contents to auxiliary files during a run. On the first pass, those values can be out of date. Running the compile again fixes references such as “see Section 2.” If a number or reference looks wrong, compile once more before you assume something is broken.
Symbols and math: γ and %
Two questions come up almost immediately.
- The percent sign. Because
%starts a comment, write\%to print a percent sign. For example,50\% offprints “50% off”. - Greek letters such as γ. Put them in math mode between dollar signs:
$\gamma$prints γ. Other lowercase Greek letters follow the same pattern, such as\alphaand\beta. For more symbols and operators, see advanced mathematics in LaTeX. Typing the character itself can fail depending on your setup, so the command form is the safer choice.
Where to write LaTeX
You have three main choices: an online editor, a desktop editor with a local install, or a visual editor such as LyX.
Overleaf in the browser
Overleaf runs LaTeX in your browser. It includes templates, a live preview, and easy sharing with co-authors. It has a free plan with limits, and paid plans add features. Overleaf is a good first step for a new user. Your files are stored on its servers, so check that this suits your work.
Desktop editors
Desktop editors need a TeX distribution installed on your computer. The common choices are:
- TeXstudio: cross-platform, with code completion, error checking, and a built-in PDF viewer.
- Texmaker: cross-platform, with similar features.
- TeXworks: a simple editor that ships with TeX Live and MiKTeX.
- TeXShop: the macOS editor that comes with MacTeX.
- VS Code with the LaTeX Workshop extension: a good choice if you already use VS Code.
For setup, follow the install guide for your system: install LaTeX on Windows 10, how to install LaTeX on Ubuntu with TeX Live, or can I use LaTeX on a Mac.
LyX: writing without raw commands
LyX is a document processor that looks more like a word processor. You type and format text in its window, and LyX writes LaTeX for you. It suits people who want LaTeX’s output without writing every command by hand. You can also export the document to LaTeX and open the .tex file to see the code the program produced, which is a good way to learn commands. LyX still needs a LaTeX installation underneath to make a PDF.
Gummi: a lightweight option
Gummi is a small, lightweight LaTeX editor with a simple interface. It is an older project. Before you rely on it, check that it installs on your system and still works with a current TeX distribution. The workflow is the same as in other editors: open or create a .tex file, write, compile, and view the PDF.
A simple project workflow
- Keep each project in its own folder, such as
thesis/. Put the.texfile, any.bibbibliography file, and images in that folder or in a subfolder such asfigures/. - Start from the minimal document and make sure it compiles before you add content. If a new error appears, the most recent change is the likely cause.
- Compile with your editor’s button, with
pdflatex main.tex, or withlatexmk -pdf main.tex. - Keep a copy of each version that compiles. Version control is a good habit once a project grows.
Common beginner errors
Read the first error in the log and fix it before the others. Later errors are often side effects of the first one. These messages are the most common:
| Message | Usual cause |
|---|---|
! Undefined control sequence. |
A command is misspelled, or the package that defines it is not loaded. |
! Missing $ inserted. |
A math symbol such as _ or ^ appears outside math mode. |
! Missing } inserted. |
A curly brace is unmatched. |
! LaTeX Error: File 'xyz.sty' not found. |
A package is missing. See the guide to installing packages. |
Characters such as #, $, %, &, _, {, and } have special meanings in LaTeX. To print one, put a backslash in front of it, such as \$, \&, or \_. To print a backslash, use \textbackslash.
If an error refers to a missing .sty file, see how to install LaTeX packages.
Where to go next
Once you can compile a document, pick the next step that fits your goal:
- Choose an editor: which software is best for LaTeX
- Install on Windows: how to install LaTeX on Windows 10
- Install on Ubuntu: how to install LaTeX on Ubuntu with TeX Live
- Install on a Mac: can I use LaTeX on a Mac
- Use LaTeX on a tablet: can I use LaTeX on an iPad
- Add packages: how to install LaTeX packages
- Judge the learning curve: is LaTeX difficult to learn
- Understand the names: what is the difference between TeX and LaTeX
- Move equations into Word: using LaTeX equations in Microsoft Word