How to Use LaTeX for Typesetting: A Comprehensive Guide

How to use LaTeX for typesetting: Overleaf or a local install, your first document, sections, lists, special characters, packages, fonts and common errors.

By Scholarly11 min read

To use LaTeX for typesetting, you write your document as plain text with markup commands (such as \section{Introduction} or \emph{important}), then compile it, and LaTeX turns that source into a finished PDF with consistent spacing, line breaks, hyphenation, numbering and math. You can start in minutes with Overleaf in the browser or install a TeX distribution on your computer. This guide takes you from nothing to a working document: what typesetting means in LaTeX, how to set it up, a minimal document that compiles, the parts of a LaTeX file, sections, lists, emphasis, special characters, packages, fonts, page layout, and the errors every beginner hits.

How to Use LaTeX for Typesetting: A Comprehensive Guide editorial illustration

What typesetting means in LaTeX

In a word processor, you format text as you type and see the result immediately. In LaTeX, you describe what each piece of text is (a section heading, an emphasized phrase, an equation, a list item) and the document class decides how it looks. People call this "what you see is what you mean", as opposed to "what you see is what you get".

The typesetting engine underneath, Donald Knuth's TeX, does work most word processors do not:

  • Whole-paragraph line breaking. TeX chooses line breaks for the paragraph as a whole (the Knuth and Plass algorithm, published in 1981) instead of one line at a time, which gives evener spacing and fewer awkward gaps.
  • Hyphenation and kerning from language-specific patterns and the font's own spacing data.
  • Math typesetting with the spacing conventions of printed mathematics.
  • Automatic numbering of sections, figures, tables, equations and pages, with cross-references and a table of contents that update themselves.

The tradeoff is a compile step and a learning curve. In return, a 5-page assignment and a 300-page thesis use the same commands and look consistent from the first page to the last.

Overleaf or a local install

Overleaf (browser) Local install
Setup Create a free account; nothing to install Install a TeX distribution (several GB for the full version) plus an editor
Distributions Managed for you TeX Live (Windows, Linux), MacTeX (macOS), MiKTeX (Windows)
Editors Built in TeXstudio, VS Code with the LaTeX Workshop extension, Vim or Emacs
Collaboration Real-time shared editing and comments Through Git or a shared folder
Works offline No Yes
Best for Beginners, group projects, quick documents Large documents, Git workflows, working without internet

If you are new, start on Overleaf: you can follow this guide in a blank project without installing anything. If you want a local setup on Windows, follow our step-by-step guide to installing LaTeX on Windows. On macOS, install MacTeX; on Linux, install TeX Live from tug.org or your package manager (distribution packages can lag behind the current release).

Write your first LaTeX document

Create a file called first.tex (on Overleaf: New Project, Blank Project, then replace the contents of main.tex) and paste this:

\documentclass[11pt]{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[margin=1in]{geometry}
\usepackage{microtype}

\title{My First LaTeX Document}
\author{Your Name}
\date{\today}

\begin{document}
\maketitle

\section{Introduction}
This is a paragraph. LaTeX ignores single line breaks,
so this sentence joins the one above.

A blank line starts a new paragraph. Here is some math: $E = mc^2$.

\section{A list}
\begin{itemize}
  \item First point
  \item Second point, with \emph{emphasis} and \textbf{bold}
\end{itemize}

\end{document}

Compile it. On Overleaf, click Recompile. Locally, click the build button in your editor, or run pdflatex first.tex (or latexmk -pdf first.tex, which reruns as many times as needed) in a terminal. You should get a one-page PDF with a title, the date, two numbered sections, and a bulleted list.

Three things to notice:

  1. Line breaks in the source do not matter. A single line break is treated as a space. A blank line ends the paragraph. Extra spaces are collapsed into one.
  2. Commands start with a backslash. Required arguments go in {} and optional ones in [], as in \documentclass[11pt]{article}.
  3. Environments wrap content between \begin{name} and \end{name}. Every \begin needs a matching \end.

Preamble and body

Every LaTeX file has two parts:

  • The preamble is everything before \begin{document}. It sets the document class (article, report, book, letter, or a journal's class), loads packages, and defines settings such as the title and page margins. Nothing in the preamble prints directly.
  • The body is everything between \begin{document} and \end{document}. That is your actual content. Anything after \end{document} is ignored.

Choose the class by the shape of the document: article for papers, assignments and short reports; report for longer reports and theses with chapters; book for books with front matter and two-sided layout; beamer for slides.

Sections and structure

\section{Methods}                 % 1 Methods
\subsection{Sample preparation}   % 1.1 Sample preparation
\subsubsection{Cleaning}          % 1.1.1 Cleaning
\paragraph{A run-in heading.} Text continues on the same line.
\section*{Acknowledgments}        % the star removes the number

\tableofcontents                  % put this after \maketitle

report and book add \chapter{...} above \section. The table of contents fills in on the second compile, because LaTeX first has to record where each heading lands.

Lists

\begin{enumerate}
  \item Install a TeX distribution.
  \item Write the document.
  \begin{enumerate}
    \item Preamble first.
    \item Then the body.
  \end{enumerate}
  \item Compile.
\end{enumerate}

\begin{description}
  \item[Preamble] Everything before \verb|\begin{document}|.
  \item[Body] Everything between \verb|\begin{document}| and \verb|\end{document}|.
\end{description}

itemize gives bullets, enumerate gives numbers, and description gives bold labels. Lists nest up to four levels. To change spacing or numbering style, load the enumitem package and write, for example, \begin{itemize}[noitemsep] for a tight list.

Emphasis and text styles

You want Write Notes
Emphasis \emph{the key idea} Usually italic; inside italic text it switches back to upright
Bold \textbf{Warning:}
Italic \textit{Nature} For titles of works and foreign words
Monospace \texttt{main.tex} For file names and code
Small caps \textsc{Small Caps}
Underline \underline{text} Rare in typeset documents; prefer \emph

Prefer \emph for emphasis: it describes meaning, so a class or style change can restyle every instance at once.

Special characters to escape

Ten characters have special meaning in LaTeX. Typing them directly causes errors or odd output:

Character Meaning in LaTeX To print it, write
% Starts a comment \%
$ Starts and ends inline math \$
& Column separator in tables \&
# Macro parameter \#
_ Subscript (math) \_
{ } Group arguments \{ \}
~ Non-breaking space \textasciitilde
^ Superscript (math) \textasciicircum
\ Starts a command \textbackslash

So "A 15% increase cost $20 for R&D" becomes A 15\% increase cost \$20 for R\&D. A few typing conventions are also worth learning early:

  • Quotation marks: write ``quoted'' (two backticks, then two apostrophes). A plain " key gives the wrong opening quote in standard LaTeX.
  • Dashes: - for a hyphen, -- for number ranges (pages 10--20).
  • Non-breaking spaces: Figure~1, Dr.~Smith, so the number or name never starts a new line alone.

Packages

Packages extend LaTeX, and you load them in the preamble with \usepackage[options]{name}. TeX Live and MiKTeX include thousands of them from CTAN (the Comprehensive TeX Archive Network), and Overleaf has the full TeX Live set installed. A sensible starter set:

Package What it does
fontenc with [T1], and lmodern Better font encoding for accented characters and copy-paste from the PDF (pdfLaTeX only)
geometry Page size and margins
microtype Subtle character spacing adjustments that improve line breaks
amsmath, amssymb Math environments and symbols
graphicx Images with \includegraphics (see inserting images in LaTeX)
booktabs Professional table rules (see creating tables in LaTeX)
enumitem Control over list spacing and labels
babel Language-specific hyphenation and terms, for example \usepackage[french]{babel}
hyperref Clickable links and PDF bookmarks; load it near the end of the preamble

Load what you need, not a long list copied from a template. Each package adds compile time and a chance of conflicts.

Fonts

With pdfLaTeX, the default font is Computer Modern, the font Knuth designed for TeX. lmodern loads Latin Modern, its modern version, and other font packages switch the whole document at once: for example \usepackage{newtxtext,newtxmath} for a Times-like text and math font, or \usepackage{libertinus} for Libertinus.

To use any OpenType or TrueType font, including fonts installed on your computer, compile with XeLaTeX or LuaLaTeX and load fontspec:

% Compile with lualatex (or xelatex)
\documentclass[11pt]{article}
\usepackage{fontspec}
\setmainfont{TeX Gyre Pagella}
\setsansfont{TeX Gyre Heros}
\usepackage{microtype}

\begin{document}
Unicode text works directly: café, Straße, naïve.
\end{document}

Do not load fontenc or lmodern with fontspec. LuaLaTeX finds fonts that ship with TeX Live by name. XeLaTeX finds fonts installed on your system by name; for a font that only ships with TeX Live, XeLaTeX may need the file name instead, for example \setmainfont{texgyrepagella}[Extension=.otf, UprightFont=*-regular, BoldFont=*-bold, ItalicFont=*-italic, BoldItalicFont=*-bolditalic]. On Overleaf, change the compiler in the Menu to XeLaTeX or LuaLaTeX. These engines also handle non-Latin scripts better than pdfLaTeX.

Page layout

geometry controls paper size and margins in one line:

\usepackage[a4paper, top=2.5cm, bottom=2.5cm, left=3cm, right=3cm]{geometry}
% or: \usepackage[letterpaper, margin=1in]{geometry}

Other layout settings that come up often:

  • Line spacing. \usepackage{setspace} then \onehalfspacing or \doublespacing, common requirements for coursework and theses.
  • Paragraph style. LaTeX indents paragraphs by default. For space between paragraphs instead, use \usepackage{parskip} rather than setting the lengths by hand.
  • Font size. Set it once in the class options: \documentclass[12pt]{article} (standard classes accept 10pt, 11pt, 12pt).
  • Two columns. \documentclass[twocolumn]{article}.
  • microtype. Keep it loaded. It slightly adjusts letter spacing and lets punctuation hang into the margin, which reduces bad line breaks and overfull lines with no visible downside.

Preparing a complete document

Real documents add a title page, an abstract, figures, tables, references and cross-references. The pieces fit together like this:

\documentclass[12pt]{report}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[margin=1in]{geometry}
\usepackage{microtype}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{hyperref}

\title{Project Report}
\author{Your Name}

\begin{document}
\maketitle
\begin{abstract}
A short summary of the report.
\end{abstract}
\tableofcontents

\chapter{Introduction}\label{ch:intro}
Results are in \autoref{ch:results}, and \autoref{tab:data} lists the data.

\chapter{Results}\label{ch:results}
\begin{table}[tbp]
  \centering
  \caption{Measured values.}\label{tab:data}
  \begin{tabular}{lr}
    \toprule
    Trial & Time (s) \\
    \midrule
    1 & 2.31 \\
    2 & 2.28 \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document}

The habits that make document preparation in LaTeX work:

  • Label everything you refer to with \label (after \caption in floats) and refer with \ref, \autoref or \cref. Never type a figure or section number.
  • Split long documents into files: keep the preamble in main.tex and pull in chapters with \include{chapters/intro} or \input{chapters/intro}.
  • Keep references in a .bib file and cite with \cite; see the LaTeX bibliography guide.
  • Use a template when one is required. Universities and journals often supply a class file; use it from the first draft. For papers and journals, see how to use LaTeX for academic writing, and for long documents, writing a thesis in LaTeX.
  • Use version control for anything you will work on for weeks. .tex files are plain text, so Git shows exactly what changed.

Common errors and how to fix them

Message What it usually means Fix
Undefined control sequence A misspelled command, or one from a package you did not load Check the spelling on the reported line; add the package
Missing $ inserted Math-only syntax (_, ^, \alpha) used in normal text Wrap it in $...$, or escape _ as \_
File 'name.sty' not found Package not installed (local) or misspelled Install it with your distribution's package manager (MiKTeX can do this automatically) or fix the name
Missing } inserted or Runaway argument An unclosed { somewhere before the reported line Check braces in the lines just before the error
Environment ... undefined Typo in \begin{...} or a missing package Fix the name or load the package
\begin{itemize} on input line 12 ended by \end{document} A missing \end{...} Add the matching \end
?? in the PDF Reference not resolved yet, or label missing Compile again; check the \label name
Overfull \hbox (warning) A line sticks into the margin, often a long URL or word Load microtype, rephrase, or allow a break with \-

When an error appears, fix the first one in the log first: later errors are often side effects of it. Compile often while you learn, so you always know which change caused a problem.

Where to go next

  • Keep a reference of math commands open while you write: the LaTeX equations cheat sheet collects the common ones.
  • Learn tables and images next; they are the parts beginners spend the most time on.
  • If you mainly want equations in notes rather than typeset documents, a Markdown app with LaTeX math (Obsidian, Jupyter) may be simpler. If you want a newer typesetting system with simpler syntax, Typst is the main alternative, though journals and universities rarely accept it yet. LyX gives you LaTeX output with a visual editor.

Once your document is written, you can also study from it. Scholarly takes the compiled PDF and turns it into flashcards, a practice quiz, a summary, or a narrated video lecture, with chat answers cited to your own pages. It does not open or compile your own .tex files, so the writing stays in Overleaf or your editor.