Creating your first LaTeX document is a relatively straightforward process. This guide will show you the various components of a LaTeX document and how to get started with creating your first one.
Table of Contents
What is a LaTeX Document?
LaTeX is a special document preparation system that is useful for creating and publishing scientific documents. This software offers many capabilities beyond that of standard document creation software such as MS Word.
A LaTeX document is simply a document that is created in LaTeX. These documents are created and structured using commands, packages, and text. Before we look at how to create a basic LaTeX document, it is important to know about commands, packages, and text.
What are LaTeX Document Commands?
Commands help structure your LaTeX document in different ways. You can use them to change your document text or to make format changes. Commands are also useful for creating equations, inputting symbols, or to delineate sections in your preferred manner.
Users can use commands by placing a backlash before them. For example, the command for making text bold can be written as \textbf{…} command. Commands generally follow a format of \command{optional argument}{required argument}.
It is possible to use multiple arguments in a command by separating each one using a comma. For example the command hypersetup{colorlinks=true, linkcolor=blue, filecolor=magenta, urlcolor=cyan} would set the color for hyperlinks contained within your LaTeX document.
Users that wish to set their text font as well as the number of columns in their document can use the command \documentclass{12pt, twocolumn}. This command sets the font size as 12 and structures the text in two columns. The command \documentclass{article} informs LaTeX to use the default settings associated with the command.
What are LaTeX Document Packages?
LaTeX also utilizes “packages” frequently during the document creation process. These packages are essentially extensions of the software’s mechanics. They offer additional functionality and customization options for document creation.
Many commands rely on packages. So if you encounter an error when attempting to use a command, it is likely because the correct package hasn’t been included in the document.
You can tell LaTeX to include a certain package in your document by using the command \usepackage{}. The package for customizing headers and footers in a document is called fancyhdr. So the command for using this package would be \usepackage{fancyhdr}.
What is LaTeX Document Text?
The document text in LaTeX is similar to the document text in other document creation software. Users can start adding text to their document once they are inside the body section.
Understanding LaTeX Document Structure
A LaTeX document can typically be broken down into two sections: preamble and body.
Adding the LaTeX Document Preamble.
Preamble refers to the information that is added before the document begins. The preamble typically starts with the command \documentclass{} and ends with \begin{document}.
An example of a document preamble is shown below:
\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\title{My First Document}
\author{Alan Matthew}
\date{}
\begin{document}
In the above example, the preamble mentions the packages to be used as well as the document title, the author name, and document date. You can use any number of packages you wish to in the document preamble. In-fact, it is vital to use certain packages if you wish to use a particular command.
Adding the LaTeX Document Body
The body of a LaTeX document is where your text, figures, equations, and tables are added. You can signal the start of your document body using the command \begin{document} and end the body with the command \end{document}. After the adding a body to the above example, the sample code output resembles the following:
\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\title{My First Document}
\author{Alan Matthew}
\date{}
\begin{document}
\maketitle
This is the text for my first document
\end{document}
The final document for the above example should resemble:
Adding Maths to Your LaTeX Document
LaTeX is favored by scientists and mathematicians over traditional document creation softwares because it allows users to add mathematical equations and symbols with ease.
You should keep two important things in mind when attempting to add mathematical formulas to your document. The first being to ensure all the right LaTeX packages are included.
For example, a popular package for using mathematical formulas is amsmath. This package enables users to include many mathematical symbols and commands in their document. You should note that if you run into an error when attempting to use a certain mathematical command, it is likely because you require an additional package.
The second thing to ensure is that “math mode” has been enabled. This mode lets you enter complex mathematical formulas with ease. You can enter math mode in a variety of different ways. The easiest is to use “$…..$”, or where your mathematical formula is bookended by the “$” symbol. Alternatively, you can use the commands \begin{equation} and \end{equation}.
For example, you can turn the equation f(x) = 5x + 15 into a proper formula with the command $f(x) = 5x + 15$. The final formula in the document will resemble:
Common Preamble Commands
Now that we have looked at the structure for LaTeX documents, let’s look at some common preamble commands and how to use them.
\documentclass{}
This is the first command used in a LaTeX document. It can be used to specify the type of document being created. Common arguments for documentclass are article, book, and slides. So if you intend to create an article for a scientific journal, you would use the command \documentclass{article} at the start of your document.
\title{}
This command is for specifying the title of your document. If you want to name your document “My First Document”, the command would resemble \title{My First Document}.
\author{}
This command is for specifying the author name in your document. If you would like to include multiple author names, use the \and command. So if you wanted to mention Alan Matthew and Sarah Smith as authors, the code would resemble \author{Alan Matthew \and Sarah Parker}
\usepackage{}
This command enables you to import a package into your LaTeX document. This is usually needed when you wish to use commands that aren’t part of LaTeX’s basic system.
Common Body Commands
Some common body commands include:
\maketitle
This command creates a document title using the \title, \author, and \date you provided in the preamble.
\tableofcontents
This command creates a table of contents for your document.
\listoffigures
This command creates a list of figures for your document.
\listoftables
This command creates a list of tables for the document.
\begin{} and \end{}
The \begin{} and \end{} commands signal the start and end of a certain environment in the LaTeX document. In the above example, we used \begin{document} and \end{document} to begin and end the document body.
How to Create a LaTeX Document in 6 Easy Steps
Now that we have looked at the various components of a LaTeX document, let’s summarize the process into 5 simple steps.
Step 1: Specify the Document Class
Start by specifying the documentclass in the preamble as \documentclass{}
Step 2: Specify the Packages to Use
Next, mention any packages you wish LaTeX to include in your preamble as \usepackage{}.
Step 3: Specify Title, Author, and Date
Next, specify the document title, author, and date in the preamble as:
\title{}
\author{}
\date{}
Step 4: Add Your Document Body
Add \begin{document} to start your document body. Fill out your document’s text and equations as needed. Then end the body with \end{document}. You should ensure that no text is added after the \end{document} command.
Step 5: Save the Document
The next step is to save your document. You can do this by pressing Ctrl+S or clicking “File” then “Save”. Select the destination folder for your file and allow it to save. This should produce a .tex file.
Step 6: Compile Your Document
Next, you should compile your document. You can do this on TeXmaker by selecting the “Quick Build” command in the toolbar and clicking the “Run” button. Your completed document will then be compiled and show up on the right side of the software.
Frequently Asked Questions
Some frequently asked questions related to LaTeX documents are shown below.
Q1. How Do I Start a LaTeX Document?
You can start a LaTeX document by opening TeXworks and typing \documentclass{}.
Q2. Why is LaTeX used?
LaTeX is used for both quality and aesthetics. Users can add mathematical equations and formulas into their documents easily, and get them looking professional with minimal effort.
Q3. Is LaTeX better than Word?
Microsoft Word may be more popular than LaTeX. However, the software isn’t capable of inserting and handling mathematical equations and formulas with the ease that LaTeX can. This is one of the reasons why so many scientists and mathematicians use LaTeX for their papers.
Further Reading
LaTex Tutorial
- 27 Pros and Cons of Using LaTex for Scientific Writing
- How to add circuit diagrams in Latex
- How to change Latex font and font size
- How to create a Latex table of contents
- How to create footnotes in LaTeX and how to refer to them, using the builtin commands
- How to create Glossaries in LaTeX
- How to create plots in Latex – codes and examples
- How to create symbols in LaTeX – commands for Latex greek alphabet
- How to create tables in LaTeX – rows, columns, pages and landscape tables
- How to drawing graphs in Latex – vector graphics with tikz
- How to highlight source code in LaTeX
- How to insert an image in LaTeX – Managing Latex figure and picture
- How to Itemize and Number List – Adding Latex Bullet Points
- How to make hyperlink in latex – Clickable links
- How to reference in Latex – 5 steps to bibliography with Bibtex
- How to use Latex Packages with examples
- How to use LaTeX paragraphs and sections
- LaTeX Installation Guide – Easy to follow steps to install LaTex
- Learn to typeset and align Latex equations and math