LaTeX Basics¶
LaTeX (pronounced “LAY-tek” or “LAH-tek”) is a typesetting system for producing professional, precisely formatted documents. Unlike a word processor such as Microsoft Word or Google Docs, which shows the formatted document as it is edited (“what you see is what you get”), LaTeX uses source code that must be compiled to produce a formatted document, usually in Portable Document Format (PDF). LaTeX is especially useful for typesetting mathematics and brings many of the advantages of computer programming to document creation.
LaTeX Syntax
Because LaTeX is a programming language, is has its own syntax that differ from Python syntax. Here are a few of the general rules.
Commands begin with a backslash
\.Curly braces,
{and}, group content together.Some characters, including
$,%,&,^, and_, have special meanings. To use one of these symbols in text, precede it with a backslash, for example\$or\%.
Overleaf¶
There are various editors and applications for writing LaTeX code. In this introduction we will use Overleaf, a free online LaTeX editor that compiles documents in the cloud. Overleaf makes it easy to use LaTeX without installing any packages on your personal machine.
Problem 0
Register an account with Overleaf and start a new project.
Go to overleaf.com and click Register. Create a free account with your university email.
You may also use an existing account if you have one.From your dashboard, click New Project \(\rightarrow\) Blank Project, give it a name, and open it.
The Overleaf editing interface has two main panels.
The editor on the left contains the LaTeX source code. This file is called
main.tex.The PDF preview on the right shows the document compiled from the source code. Click the green Recompile button to update it.
There are also buttons and tools to download the PDF, share the project, and view the file tree.
A Minimal Document¶
A blank Overleaf project starts with the following LaTeX file.
1\documentclass{article}
2\usepackage{graphicx} % Required for inserting images
3
4\title{Name of Project}
5\author{Name of Author}
6\date{Month Year}
7
8\begin{document}
9
10\maketitle
11
12\section{Introduction}
13
14\end{document}
Comments
In LaTeX, any text after a % character is a comment and is completely ignored by the program.
In the above document, for example, the text
% Required for inserting images
is not processed by the LaTeX compiler and does not appear in the PDF.
Comments are important in LaTeX, just as they are in Python, because they help the author outline the document and pick the project back up after a break.
Comments also make it easy to temporarily hide entire lines of content: add % to the beginning of a line to remove it from the compiled document, or remove % to put the line back.
A LaTeX file has two main parts:
the preamble, which sets up and configures the document, and
the body, which contains the actual content to be displayed in the finished document.
Preamble¶
The preamble is everything before \begin{document}.
1\documentclass{article}
2\usepackage{graphicx} % Required for inserting images
3
4\title{Name of Project}
5\author{Name of Author}
6\date{Month Year}
7
8\begin{document}
9
10\maketitle
11
12\section{Introduction}
13
14\end{document}
This preamble contains the following commands.
\documentclass{article}declares the document type. Thearticleclass is a good choice for homework and short reports.\usepackage{}loads a package that adds features and makes a set of commands available. It is good practice to keep all\usepackage{}commands together at the top of the file.\title{},\author{}, and\date{}set up a title block that is displayed if\maketitleis included in the body.
Problem 1
Customize the title block of your new LaTeX project.
Use
\title{}to set the document title toEMC2 Lab 1: Introduction to \LaTeX.Use
\author{}with your first and last name, followed by\\ Department of Mathematics, Brigham Young University
(all within the curly braces). The\\command inserts a newline.Use
\date{\today}, which automatically sets the date to the current day.
Recompile the document and make adjustments as needed.
Page Setup¶
The \documentclass{} command at the beginning of the file establishes several default page settings.
For example, \documentclass{article} establishes wide margins and adds page numbers to the bottom of each page.
Some of these settings can be changed by passing arguments to \documentclass{} using square brackets, [ and ], before the curly braces.
For example,
\documentclass[11pt,a4paper,twocolumn,landscape]{article}
sets the regular font size to 11pt (instead of the default 10pt), changes the page size from standard 8.5-by-11-inch paper to 210-by-297-millimeter (A4) paper, sets the entire document in two columns, and rotates the document from portrait to landscape.
Some page settings are most easily controlled through packages.
For example, the geometry package provides a convenient way to set margins.
% Set all four margins at once.
\usepackage[margin=1in]{geometry}
% ...or set each side separately:
\usepackage[top=1in, bottom=1in, left=1.25in, right=1.25in]{geometry}
Problem 2
Add some dummy text to your document with the following commands.
Add
\usepackage{lipsum}to the preamble.Add
\lipsumon a new line between\section{Introduction}and\end{document}.
Recompile the document, then format the page settings as follows.
Set the default font size to 11pt.
Set all margins to 1 inch.
Recompile the document and ensure that the formatting changes were applied.
Body¶
The body of the document is everything between \begin{document} and \end{document}.
1\documentclass{article}
2\usepackage{graphicx} % Required for inserting images
3
4\title{Name of Project}
5\author{Name of Author}
6\date{Month Year}
7
8\begin{document}
9
10\maketitle
11
12\section{Introduction}
13
14\end{document}
Environments
The \begin{} command starts an environment and must eventually be followed by a matching \end{} command.
The document environment constitutes the document body; we will introduce other environments later for mathematics and other formatting elements.
Plain text written in the body is rendered in the document. Text on consecutive lines is grouped into the same paragraph; a blank line between lines of text creates a paragraph break.
She sells seashells by the seashore, % First paragraph.
The shells she sells are seashells, I'm sure. % First paragraph.
So if she sells seashells on the seashore, % First paragraph.
Then I'm sure she sells seashore shells. % New paragraph.
Sectioning¶
The body is often organized into sections and nested subsections (and even subsubsections).
\section{}(or\subsection{}) creates a new section (or subsection) with automatic numbering.\section*{}(or\subsection*{}) creates a new section (or subsection) without any numbering.
Problem 3
Add sections and subsections to match the following outline.
Introduction (without a number)
1Mathematics1.1Equations1.2Common notation1.3Vectors, matrices, and arrays
2Page elements2.1Lists2.2Figures2.3Tables2.4Definitions and theorems
Finally, comment out the \lipsum command and add \tableofcontents on a new line just after \maketitle to automatically generate a table of contents listing the numbered sections.
Bold, Italics, and Underline¶
Basic text formatting is done with the following commands:
Bold text with
\textbf{...}, for example,\textbf{That was BOLD!}Italicize text with
\textit{...}or\emph{...}.Underline text with
\underline{...}.
Problem 4
Under the section Introduction, write two to three sentences about yourself, including your hometown, your current major, and a few of your hobbies and interests. Bold your hometown and major, italicize your hobbies and interests, and underline the last word in each sentence.