Structure and Compilation

Software

This tutorial uses the program latexmk. This is a shell program that simplifies using LaTeX. It automatically runs as many passes of the compiler as needed. And, during these runs, it also runs the program BibTeX as needed. Furthermore, it helps with file management and has various other utilities. For example, the next two code blocks are roughly equivalent.

latexmk -pdf main.tex
pdflatex main.tex
bibtex main
pdflatex main.tex
pdflatex main.tex

A shell program is recommended instead of a GUI. It allows using a general-purpose text editor of one that is specific to LaTeX. If you do not yet have a text editor of your choice, then I recommend VS Code.

On Linux, program latexmk comes with the package texlive-full. The package texlive-full is installed with the next shell command.

sudo apt install texlive-full

On Mac, this program comes with the package MacTeX. The package MacTeX is installed with the next shell command.

brew install --cask mactex

On Windows, this program comes with the package MiKTeX. The package MiKTeX is installed with the next shell command.

winget install MiKTeX.MiKTeX

Also, on Windows, if Perl is not already installed, install it with the next shell command. Perl is required by latexmk.

winget install StrawberryPerl.StrawberryPerl

Minimal Example

The next code block is a minimal working example.

\documentclass[12pt]{article}
\begin{document}

The body of the document.

\end{document}

The preamble is everything before \begin{document}. The preamble is for specifying format, including packages and defining commands. The body is everything between \begin{document} and \end{document}. The body is for the actual contents.

The document class is specified with \documentclass{}. This is required. In this example, the document class is article. Similarly, the document class book is for books. Some prefer the document classes amsart and amsbook from AMS instead of article and book. These classes suffice for most purposes.

The document class beamer is for presentations. I recommend this instead of PowerPoint for mathematical presentations.

Journals provide their own document class. The extension of such a file .cls. Most people will never write their own document class. .. The command texdoc <class> will bring up the documentation for the document class <class>.

Compiling

If the above minimal working example is in the file main.tex, then the command next command latexmk -pdf file.tex compiles it. The option -pdf specifies the compiler pdflatex. Alternatives are -xelatex and -lualatex. Some packages require a certain compiler. But, any of these compilers suffice for most purposes.

The command latexmk -pdf file.tex makes several files in the directory. The main output is the file main.pdf. The other files are saved to help the compiler. The command latexmk -c removes these other files. Because a failed compilation may damage these files and prevent compilations, this command is useful for debugging.

Titles

The next code block expands the above minimal working example by adding a title, date and author. Each is optional. The command \maketitle formats them in the body.

\documentclass[12pt]{article}

\title{My First Document}
\author{Dustin Enyeart}
\date{5 October 2024}

\begin{document}
\maketitle

The body of the document text.

\end{document}

Other document classes may have additional items that are formatted with \maketitle, such as \institution{} or \affliation{}. For an informal application such as a course, you can hijack the command \date{} to add additional information.

\documentclass[12pt]{article}

\title{My First Document}
\author{Dustin Enyeart}
\date{5 October 2024 \\ \vspace{1em}
      MA 341 \\ \vspace{1em}
      University of Nowhere}

\begin{document}
\maketitle

This is a clever homework solution.

\end{document}

The command \maketitle is not necessary. That is, the information can be formatted directly in the body.

\documentclass[12pt]{article}

\begin{document}

\begin{center}
    \textbf{My First Document} \\
    Dustin Enyeart \\
    5 October 2024 \\
    MA 341 \\
    University of Nowhere \\
\end{center}

This is a clever homework solution.

\end{document}

Divisions

Sections and subsections are specified by the commands \section{} and \subsection{}, respectively. In books, chapters are specified by the command \chapter{}. These are automatically numbered. Use an asterisk * to not number them, such as in \section*{}. The command \tableofcontents puts in the table of contents. Bibliographies are explained in References.

\documentclass[12pt]{article}

\begin{document}

\tableofcontents

\section{First section}

Some text.


\section{Second section}

Some text.


\end{document}

Packages

A package is included with the command \usepackage{}. The next code block includes the package amsmath. This package is used in Markup to format mathematical equations.

\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}

The body of the document.

\end{document}

Some Formatting

The output so far may look poorly formatted for normal A1 paper. In the next code block, the margins are set with the package geometry, and the command \renewcommand{\baselinestretch}{1.0} sets the spacing between lines.

\documentclass[12pt]{article}

\usepackage[letterpaper, margin=1.0in, footskip=30pt]{geometry}
\renewcommand{\baselinestretch}{1.0}

\usepackage{amsmath}

\title{My First Document}
\author{Dustin Enyeart}
\date{5 October 2024}

\begin{document}
\maketitle

\section{First section}

The body of the document text.

\end{document}

Inputs

The command \input{} includes another file. For example, the line \input{otherfile} inputs the contents of otherfile.tex exactly. This directly copies the contents of the file. This can be used anywhere in the document. A common use is separating chapters, sections or proofs. If the command \input is in the preamble for formatting, then the inputted file is a style file, and, by convention, its extension .sty.