LaTeX for Beginners

People who want a high-level overview of how TeX works may want to read A High-Level Overview of TeX instead, or in addition to, this post. This guide prioritizes people who bother to locally install LaTeX (do your homework, people). All the information for Overleaf users is still there, but it’s included as an afterthought.

Equations

Writing equations in TeX is quite simple. All you have to do is surround whatever math you want to write with the $ symbol. For example, $1+1=2$ will return 1+1=21+1=2.

The main draw of TeX for math, though, is the ability to render complex equations with precise (even if not always so simple) syntax. For instance, instead of writing (1+1/sqrt(x))/(2+1/(2x)) and expecting your reader to parse it, you write \frac{1+\frac{1}{\sqrt{x}}}{2+\frac{1}{2x}} and get 1+1x2+12x\frac{1+\frac{1}{\sqrt{x}}}{2+\frac{1}{2x}}. The tradeoff, though, is that when you’re writing is much harder for you to parse. (Although over time you get used to it.)

When you’re new to writing equations, expect to Google aggressively. The AoPS LaTeX Symbols Page provides a decent selection of some common symbols (as well as some symbols frankly no one cares about). Sometimes there’s a symbol whose name you don’t know, making it hard to Google — in that case, use Detexify. There’s also the Comprehensive LaTeX Symbol List, which is exactly what it sounds like.

I suspect that most people reading this already know how to write equations in LaTeX though. (And if you don’t, it’s pretty easy to learn, it takes a week to master tops.) Generating a document, though, is a little harder…

Document Structure

A LaTeX document is contained in a single .tex file, which has exactly one documentclass and any number of packages.1

Here is a simple example of a LaTeX document.

\documentclass{article}
\begin{document}
    Did you know that $1+1=2$?
\end{document}

Notice that we have a documentclass — we must have one, though it does not need to be article — and we surround the contents of the document with \begin{document} and \end{document}. All of this is mandatory.

Anything that comes before \begin{document} is part of the preamble. You should import packages in the preamble, after \documentclass but before \begin{document} What you do after \end{document} almost never affects the compilation process, but don’t put anything there anyway.

This document is a bit boring. You can include a title, author, and date like such:

\documentclass{article}

\title{Title}
\author{Author}
\date{Date}

\begin{document}
    \maketitle

    Did you know that $1+1=2$?
\end{document}

Sections

You can split your article into sections with the \section command like thus:

\begin{document}
\section{Did you know...}

Did you know that elephants are pink? That $1+1=2$?

\end{document}

Compiling

Compiling is the process of turning your TeX document (the .tex file) into a PDF. The compiler has nothing to do with the text editor. Personally, I think you do yourself a disservice if you are invoking the compiler through a graphical interface; just type pdflatex path/to/tex in the terminal instead.

(I will not explain how to compile Overleaf documents since it is self-explanatory.)

To get the compiler, install TeX Live. It really isn’t that hard. If you’re using Windows, it will take around 4 hours for a full install.2

If you are using Linux, you have the choice of installing TeX Live with your package manager. I don’t think it really matters if you use the “vanilla” installer or use your package manager, but if you want things to “just work” I recommend using the official installer.3

Then, with your terminal in the same directory as your tex file, run pdflatex file.tex (where file.tex is replaced by the name of the actual file). You can in theory run it in another directory but that’s also where all the compiled files will go, and that’s not great.

If you want to compile asymptote, make the ~/.latexmkrc file and copy the following text into it:

sub asy {return system("asy \"$_[0]\"");}
add_cus_dep("asy","eps",0,"asy");
add_cus_dep("asy","pdf",0,"asy");
add_cus_dep("asy","tex",0,"asy");

Then, instead of running pdflatex file.tex, run latexmk file.tex.

Where is latexmkrc? (On paths and the home directory)

When I write ~ in ~/.latexmkrc, it means the home directory.4 This is a Unix convention and will not work for Windows! However, it is convenient as a shorthand so I use it anyway.

If you open up a new shell, it will start in the home directory, you can just run notepad .latexmkrc (Windows) or nano .latexmkrc (Mac/Linux) from there and copy in the contents.

Pretty Documents

If you want your documents to be pretty, typically you will want to import custom .cls or .sty files. The scrartcl class also has good defaults.

To import a custom .cls or .sty file, you have two choices:

  1. Put the package in the same directory as the TeX file. (This is one of many reasons you should run LaTeX in the same directory as your TeX file.)

    I only recommend this if you plan on using the .cls or .sty file only for that project, AND that project is a throwaway.

  2. Put the package in the ~/texmf/tex/latex directory. (If this directory does not exist, create it.)

    A package does not have to be a .cls or a .sty file. It can be a directory containing sty and cls files. In this case, you copy the entire directory into ~/texmf/tex/latex.

I recommend using https://gitlab.com/mathadvance/tex/mast, which actually has some (admittedly bad) documentation, or https://gitlab.com/dennistex/bounce. Note that the latter has no documentation, but the commands are all the same as mast.cls.

If you are using Overleaf, upload the files to your project. You have to do this for every project, there’s no way around it.

Gotchas

Watch out for the following potential “gotchas” that might make your document unreadable.

You will notice that the second half relates to the WYSIWYM philosophy of LaTeX. That comprises more than half the bad TeX people write.