Skip to main content

Getting Started with LaTeX

LaTeX is a typesetting program useful for mathematical and scientific writings or publications. This guide provides an overview of how to get started with LaTeX, as well as resources and exercise to help new users of the program.

Introduction

This section of the guide is intended for people looking to get started creating documents with LaTeX.  If you still need to install LaTeX please see the Getting Started: Installation section of the guide.  The focus of this portion of the guide is to explain the structure of a LaTeX document, introduce various functions and symbols, and provide the reader with an understanding of how to write, edit, and produce their first document.  When you are done with this page take a look at the Getting Started: A sample document to help you create your first document.  Check the exercise tab of this guide if you are looking for some practice exercises to help get you familiar with LaTeX.

LaTeX Structures

Commands

Commands are a vital structure for creating any kind of document in LaTeX.  Commands have many uses such as changing document/text formatting, creating equations, delineating sections of a document, and inputting symbols.  To use a command a back slash is placed before the command.  For example \pi is the command to insert the symbol π and \begin{matrix} is one of the commands needed to input a matrix.  Generally commands have the format \command[optional argument]{required argument}. Multiple arguments can be used with some commands by separating each argument with a comma, for example \hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue} sets the font color of certain hyperlinks in a document.  Some commands such as \pi have neither a optional nor required argument.  Others such as \begin{} will only work with a recognized required argument.  Some commands can also accept an optional argument which is used to specify what action the command will take, for example \documentclass[11pt, twocolumn]{article} tells LaTeX that the current project is an article with 11pt font and should be formatted with two columns, leaving out the optional argument, \documentclass{article}, will cause LaTeX to use the defaults associated with this command  Lastly, some commands may have more than one required argument for example \frac{numerator}{denominator} creates a faction where the first argument is the numerator and the second argument is the denominator.  A list of common commands can be seen in the Common Commands section of this page, there are also links to several useful websites with lists of commands in the resources tab.

Packages

Packages are used to supplement the standard features of LaTeX.  If you are trying to use a command and find that it is generating an error one of the most common issues is that you have not included the needed package in your document.  The command \usepackage{} with the package name as the argument will tell LaTeX to include a certain package in your document. Packages should be placed in the document preamble (see Document Structure below).  If you would like to include multiple packages in your document you can either include the \usepackage{} command multiple times or use it once and separate each package name with a comma i.e.:

\usepackage{geometry}

\usepackage{amsmath}                                  \usepackage{geometry, amsmath, graphicx}

\usepackage{graphicx}

 

It is better to list the packages separately if you intend to use an optional argument with any package, otherwise it just comes down to personal preference.  Many standard packages come with a MikTex or MacTex download, but you can also install new ones if needed.  Some instruction for installing new packages can be found online.  See the Common Packages part of this page for a list of some useful packages.

Text

Writing text in a LaTeX document is easy.  Once you are inside the body of the document, as described in the Document Structure section of this page, all you have to do is start typing.  When you compile the code LaTeX will take care of all the text formatting based on any commands and packages used.  

Math

Incorporating mathematical symbols and equations is one of the most popular features of LaTeX.  There are a number of ways to include math in a LaTeX document, for more information see the Document Structure section of this page.

‚ÄčComments

Anything that follows a % in a line of code will be treated as a comment and ignored by compiler.  If you want to include a percentage symbol in your text use the command \% for example of you want to write 100% in LaTeX you would write 100\%

Document Structure

To create PDFs with LaTeX the user must generate a .tex file.  The .tex file will consist of the code that is compiled by the back end LaTeX software to generate the end result PDF document.  With LaTeX installed .tex files can be created in notepad or textedit on Windows or OS X computers respectively, though it is strongly recommenced to generate these files directly in a LaTeX text editor as discussed in the Getting Started: Installation tab.

 

A LaTeX document consists of two main pieces, the preamble and the body, the bibliography can be considered a third piece depending on how you structure it, but that will be discussed in the BibTex tab of this guide.  It is recommended that the user creates a new folder on their computer for each LaTeX document they are working on.  When LaTeX compiles a .tex file it generates several files in order to create the PDF having a folder for the each individual document you are working on will help keep all of the files generated organized.  Additionally, it possible the user will want to insert images, figures, or other objects into their document (BibTex files for example), it is much simpler to insert items into the document when they are located in the same folder as the .tex file.

 

Preamble

The preamble to a LaTeX document is all the information that occurs before the document begins.  The preamble starts with the command \documentclass{} and ends with the command \begin{document}.  Below is an image of a document preamble:

Image of LaTeX code showing the preamble of a document.

Different objects are placed in the preamble between the commands \documentclass{} and \begin{document}.  The most common of these items is the command \usepackage{}.  A more detailed explanation of what packages are can be found under the LaTeX Structures heading on this page, but briefly a package informs the compiler of certain commands that will be used and certain procedures to take while generating the final document.  Any number of packages can be included in the preamble, and one of the most common mistakes made by the new LaTeX user is to include a command in their document without including the proper package in the preamble.  Other commands such as \title, \author, and \date are included in the preamble and than may be incorporated within the document.  Lastly, new commands can be created in the preamble that will only be used in the current document.

Body

The body of a LaTeX document is where all of the text, equations, figures, tables, etc... will be placed.  The body of the document begins with the command \begin{document} and ends with the command \end{document}.  A very simple sample code and its output can be seen below.  The code contains both the preamble and body of the document.

Image showing the body of a LaTeX document

Image of a document produced by LaTeX.

Writing Math

One of the most popular features of LaTeX is its ability to easily incorporate and format mathematical equations and symbols.  There are two important things to remember to ensure LaTeX handles mathematical formula properly.  

First is to make sure that you have included all necessary packages.  The most common package for using mathematical formulas in a document is "amsmath", though this package allows the use of a multitude of mathematical symbols and commands it does not include everything.  If you try to compile a document and an error occurs related to a mathematical object you have included you will want to check if there is an additional package you need, for example if you try to include bold lettering in a mathematical equation without the package "bm" an error will occur.

Second is to entire math mode.  Math mode lets LaTeX know that something should be formatted as math and not regular text, there are also some symbols and commands that can not be used outside of math mode.  The image below shows the same text, f(x) = 2x+5, first not using math mode and second using it.  There is a very clear difference between how the equation is formatted inside and outside of math mode. 

Image comparing mathematics written in and out of LaTeX's math mode

There are several different ways to enter math mode.  If you would like to write mathematical expression inside a line of text all the mathematical text should be book ended by $.  For example writing $x+3$ would tell LaTeX to write x+3 as math. If you want math to appear on its own line it what is called display mode you can use either the command $$ on both sides of the math, or \[ and \].  The previous example would look like this: $$x+3$$ or \[x+3\].  Finally, some commands will automatically place the user in math mode for example any text appearing between the commands \begin{equation} and \end{equation} will be written in math mode.  Go to the exercises tab for some questions to let you practice using math mode. 

Common Commands

Here is a list of some useful and common commands for LaTeX.  Links to more exhaustive lists can be found on the resources tab of this guide.  If you are looking for specific mathematical symbols check the links on the resources tab.  If you are interested in more information about a specific command a simple Google search will often provide great results.   

 

Preamble commands:

\documentclass{}

The first command in any document.  Used to specify what type of document is being made.  Some common arguments are article (common for scientific journals), book, IEEEtran (for IEEE transaction format), slides, beamer (for presentations).

\usepackage{}

This command imports a package into the LaTeX document.  This is important for including any commands that are not standard in LaTeX.

\title{}

The intended title of the document should be used as the argument

\author{}

The author of the document should be used as the argument, for multiple authors use \and to separate authors name or use the authblk package.  For the authblk package uses the following format where \affil is used to entire the affiliation of the author and the optional argument denotes the whatever symbol should be used after the author's name:

\author[1]{1st Author} 
\author[1]{2nd Author} 
\author[1]{3rd Author} 
\author[2]{Nth Author}
\affil[1]{Institute 1}
\affil[2]{Institute 2} 

\date{}

The argument is the date associated with the document.  Note that leaving the argument empty will create a blank date in the title, and not including the command will default the title to the date compiled.

 

Body commands:

\maketitle

Creates a title based on the \title, \author, and \date provided.

\tableofcontents, \listoffigures\listoftables

Creates a table of content, list of figures, and/or list of tables.

\newpage{}

Creates a new page

\\

Creates a line break

\begin{} and \end{}

Whenever there is a \begin{} it must be paired with an \end{} with the same argument at some point later in the document.  The \begin{} command tells LaTeX to entire a certain environment while the \end{} command concludes that environment.  \begin{document} will start the body of the document (end the preamble) and \end{document} will end the document.  Other common arguments are equation, align, tabular, and figure

\section{}

Denotes a new section.  The title section should be used as the argument.  Sections will be automatically included in the table of contents. Use the commands \subsection{} and \subsubsection{} to create sections within sections.

Common Packages

Packages allow for additional features to be used within a LaTeX document.  Both MikTex and MacTex come pre-installed with a large number of standard packages and instructions for installing new packages.  To use a package include the command \usepackage{} where the argument is the package name i.e. \usepackage{amsmath}.  Below is a brief list of some useful LaTeX packages:

amsmath

This package allows for more complicated mathematical structures within a document such as matrices.

geometry

This package is for the more advanced user.  It allows the user to better control the formatting of the document.

graphicx

This package allows graphics to be integrated into a document.

hyperref

This package allows for hyperlinks inside a document.  Use the command \hypersetup{} to control different link colors.

booktabs

A useful package if you are having trouble with spacing inside of tables.