A Quick Guide to LaTeX

A Quick Guide to LaTeX

6 Feb 2025

LaTeX is a powerful typesetting system hamstrung by a few decades-old decisions and some… ahem… questionable design decisions. Nevertheless, its ability to typeset technical documents remains unmatched, and it enjoys wide support across STEM fields. Learning LaTeX is a worthy use of your time, if you intend to pursue a career in science.

This is meant as a short and simple how-to guide for learning LaTeX. It is not meant to be comprehensive, but rather serve as a guide of where to look to get the information you need to know. It is organized as a problem → solution mapping.

Key resources #

These are the places where I learned LaTeX. They will serve you well.

Getting LaTeX #

There are several LaTeX distributions. The only one I have ever used is the TeX Live distribution, and I’ve never needed anything else.

macOS
brew install --cask mactex-no-gui to get just the CLI tools; I never use the GUI programs.
Debian
apt-get install texlive; see https://packages.debian.org/stable/texlive for details. You may need to install other packages like texlive-latex-recommended to get some more packages.
Other
The TeX Live website should have instructions for you.

The TeX Live distribution comes bundled with most, if not all, of the packages you will ever need. So, when I suggest using the e.g. geometry or enumitem package, you should be able to just put \usepackage{geometry} in your preamble without worrying about downloading anything.

Making a document #

Further reading: LaTeX Wiki: Document Structure

LaTeX documents have a preamble and then a document body. Roughly, the preamble specifies the look of the document through setting variables and importing libraries, and the document body holds all of the text.

LaTeX documents have this format:

\documentclass[...class options...]{class name}

%% This is the "preamble"
... packages and variables here ...

\begin{document}

... body of document ...

\end{document}

A document class header is the first line in the file, and it describes what kind of a document you are making. Most common form is article. Technical journals typically define their own document class, which is kind of like enforcing a standard style sheet out-of-the-box. Document classes typically have options to configure fine points of the document setup.

A standard article class might look like this:

\documentclass[letterpaper,draft]{article}

This says that the document is an article (other types are book, beamer for slides, and more), and the options letterpaper mean the PDF should size to a US letter sheet, and draft will highlight hyphenation errors for you. There are lots of options for each document class; consult the documentation as needed.

UTF-8 in source #

Put this in your preamble:

\usepackage[utf8]{inputenc}

This will let you put UTF-8 characters inside your document file.

Page layout #

Changing the size of the margins #

You want to use the geometry package. For example, to set the left and right margins to 1.8 inches, and the top and bottom margins to 1 inch, do this:

\usepackage[margin=1.8in,top=1in,bottom=1in]{geometry}

Changing paragraph styles #

The default paragraph style is to not indent the first paragraph in a section This is correct typographic practice, so don’t mess with that. and to indent every subsequent paragraph.

If you want to change this so that paragraphs have no indentation but have a bigger separation between them (my personal favorite paragraph style) then you can put something like this in your preamble:

\setlength{\parindent}{0pt}
\setlength{\parskip}{0.5\baselineskip}

The \parindent length controls how much to indent a paragraph. By setting it to 0pt we turn off paragraph indentation. The \parskip controls how much space to put in between paragraphs. I think its default value is 0. By setting it to half of \baselineskip we add about a line and a half of blank space between paragraphs. (See LaTeX Wiki: Lengths for more details.)

Fonts #

Changing the default font #

You need the fontspec package. Here is what I do to set the main font to Valkyrie, the sans-serif font to Concourse, and the monospace font to Iosevka:

\usepackage{fontspec}
\setmainfont[BoldFont = * Bold, RawFeature={+onum}]{Valkyrie B}
\setsansfont[RawFeature={+ss03,+ss05,+ss12,+ss13,+ss15,+ss18}]{Concourse 3}
\setmonofont{iosevka-output-extended}[Scale = 0.85, Extension = .ttf, Path = /Users/ashton/Library/Fonts/, UprightFont = *, ItalicFont = *italic, Ligatures={NoRequired,NoCommon,NoContextual}]

As you can see, there are quite a few options enabled for each of those. The BoldFont = * Bold option for Valkyrie means that the otf file I want LaTeX to use for bolded Valkyrie is at Valkyrie B Bold.otf. If the file were named Valkyrie B-Bold.otf I would have to say BoldFont = *-Bold.

You need to use LuaLaTeX to use the fontspec package! This should come with your TeX Live distribution. I will typically say latexmk -lualatex file.tex to build.

Tables #

Take a look at the LaTeX wiki and this Overleaf tutorial. LaTeX gives you a lot of control over the presentation of tables.

Source Code #

Further reading: LaTeX Wiki: Source Code Listings

Putting source code in LaTeX is a pain. Lower your expectations now and life will be easier.

First, put this in your preamble:

\usepackage{listings}
\usepackage{xcolor}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.95}

\lstdefinestyle{mystyle}{
  backgroundcolor=\color{backcolour},
  commentstyle=\color{codegray},
  keywordstyle=\color{magenta},
  numberstyle=\tiny\color{codegray},
  stringstyle=\color{codegreen},
  basicstyle=\ttfamily\footnotesize,
  breakatwhitespace=false,
  breaklines=true,
  captionpos=b,
  keepspaces=true,
  numbers=left,
  numbersep=5pt,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  tabsize=2
}

\lstset{style=mystyle}

The first two lines bring in the listings and the xcolor packages. The first lets you define code blocks, and the second lets you define custom colors, though some named colors (like “magenta” in this example) come built-in.

The \lstdefinestyle{mystyle}{...} lets you create something kind of like a stylesheet for your code. The most important big is setting the basicstyle=\ttfamily option. This sets your code in a monospace font. (Specifically, it sets it in the \ttfamily font. In the example it also sets it to be a little smaller at \footnotesize.)

Inside your document you can put your source code in a lstlisting environment like this:

\begin{lstlisting}[language=C]
int main() {
  printf("Hello, world!\n");
  return 0;
}
\end{lstlisting}

There are a lot of other ways to customize this package, but I will not cover those here.

LaTeX has a lot of packages. Here are some that I like using:

microtype
Little details to enhance the typography of your document.
stmaryrd
“St. Mary Road”: oodles of mathematical symbols.

Full example #

Here’s an example LaTeX document that uses a lot of the config above. Hope it helps you get started with LaTeX!

\documentclass[11pt,letterpaper]{article}

\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{microtype}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.95}

\lstdefinestyle{mystyle}{
  backgroundcolor=\color{backcolour},
  commentstyle=\color{codegray},
  keywordstyle=\color{magenta},
  numberstyle=\tiny\color{codegray},
  stringstyle=\color{codegreen},
  basicstyle=\ttfamily\footnotesize,
  breakatwhitespace=false,
  breaklines=true,
  captionpos=b,
  keepspaces=true,
  numbers=left,
  numbersep=5pt,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  tabsize=2
}

\lstset{style=mystyle}

\setlength{\parindent}{0pt}
\setlength{\parskip}{0.5\baselineskip}

\begin{document}

\noindent                       % Don't indent the first line; happens automatically after a \section{}
When writing a paper, I \emph{always} load the \texttt{microtype} package. I like how it lets you get hanging indentation.

Here is an example of some code:

\begin{lstlisting}[language=C]
int main() {
  printf("Hello, world!\n");
  return 0;
}
\end{lstlisting}

Hustle and bustle were once very popular. This cannot be denied. Also, I think Strong Bad should decrease The Cheat's allowance.

\end{document}
Mastodon