原文: Learn LaTeX in 30 minutes – Overleaf, Online LaTeX Editor
Writing your first piece of LATEX
The first step is to create a new LATEX project. You can do this on your own computer by creating a new .tex
file
main.tex
\documentclass{article}
\begin{document}
First document. This is a simple example, with no
extra parameters or packages included.
\end{document}
This example produces the following output:
![[Pasted image 20230410224152.png]]
The first line of code, \documentclass{article}
, declares the document type known as its class, which controls the overall appearance of the document. Different types of documents require different classes
To get some idea of the many LATEX class types available, visit the relevant page on CTAN (Comprehensive TeX Archive Network).
Having set the document class, our content, known as the body of the document, is written between the \begin{document}
and \end{document}
tags.
The preamble of a document
The previous example showed how document content was entered after the \begin{document}
command; however, everything in your .tex
file appearing before that point is called the preamble, which acts as the document’s “setup” section.
Within the preamble you define the document class (type) together with specifics such as languages to be used when writing the document; loading packages you would like to use (more on this later), and it is where you’d apply other types of configuration.
A minimal document preamble might look like this:
\documentclass[12pt, letterpaper]{article}
\usepackage{graphicx}
where \documentclass[12pt, letterpaper]{article}
defines the overall class (type) of document. Additional parameters, which must be separated by commas, are included in square brackets ([...]
) and used to configure this instance of the article class; i.e., settings we wish to use for this particular article
-class-based document.
In this example, the two parameters do the following:
12pt
sets the font sizeletterpaper
sets the paper size
Of course other font sizes, 9pt
, 11pt
, 12pt
, can be used, but if none is specified, the default size is 10pt
.
As for the paper size, other possible values are a4paper
and legalpaper
.
For further information see the article about page size and margins.
The preamble line
\usepackage{graphicx}
is an example of loading an external package (here, graphicx
) to extend LATEX’s capabilities, enabling it to import external graphics files.
LATEX packages are discussed in the section Finding and using LATEX packages.
Including title, author and date information
Adding a title, author and date to our document requires three more lines in the preamble (not the main body of the document). Those lines are:
\title{My first LaTeX document}
: the document title\author{Hubert Farnsworth}
: here you write the name of the author(s) and, optionally, the\thanks
command within the curly braces:\thanks{Funded by the Overleaf team.}
: can be added after the name of the author, inside the braces of theauthor
command. It will add a superscript and a footnote with the text inside the braces. Useful if you need to thank an institution in your article.
\date{August 2022}
: you can enter the date manually or use the command\today
to typeset the current date every time the document is compiled
With these lines added, your preamble should look something like this:
\documentclass[12pt, letterpaper]{article}
\title{My first LaTeX document}
\author{Hubert Farnsworth\thanks{Funded by the Overleaf team.}}
\date{August 2022}
To typeset the title, author and date use the \maketitle
command within the body of the document:
\begin{document}
\maketitle
We have now added a title, author and date to our first \LaTeX{} document!
\end{document}
The preamble and body can now be combined to produce a complete document which can be opened in Overleaf:
\documentclass[12pt, letterpaper]{article}
\title{My first LaTeX document}
\author{Hubert Farnsworth\thanks{Funded by the Overleaf team.}}
\date{August 2022}
\begin{document}
\maketitle
We have now added a title, author and date to our first \LaTeX{} document!
\end{document}
LaTeX is a form of “program code”, but one which specializes in document typesetting; consequently, as with code written in any other programming language, it can be very useful to include comments within your document.
A LaTeX comment is a section of text that will not be typeset or affect the document in any way—often used to add “to do” notes; include explanatory notes; provide in-line explanations of tricky macros or comment-out lines/sections of LaTeX code when debugging.
To make a comment in LaTeX, simply write a %
symbol at the beginning of the line, as shown in the following code which uses the example above:
\documentclass[12pt, letterpaper]{article}
\title{My first LaTeX document}
\author{Hubert Farnsworth\thanks{Funded by the Overleaf team.}}
\date{August 2022}
\begin{document}
\maketitle
We have now added a title, author and date to our first \LaTeX{} document!
% This line here is a comment. It will not be typeset in the document.
\end{document}
This example produces output that is identical to the previous LaTeX code which did not contain the comment.
Bold, italics and underlining
Next, we will now look at some text formatting commands:
- Bold: bold text in LaTeX is typeset using the
\textbf{...}
command. - Italics: italicised text is produced using the
\textit{...}
command. - Underline: to underline text use the
\underline{...}
command.
The next example demonstrates these commands:
Some of the \textbf{greatest}
discoveries in \underline{science}
were made by \textbf{\textit{accident}}.
This example produces the following output:
Another very useful command is \emph{_argument_}
, whose effect on its _argument_
depends on the context. Inside normal text, the emphasized text is italicized, but this behaviour is reversed if used inside an italicized text—see the next example:
Some of the greatest \emph{discoveries} in science
were made by accident.
\textit{Some of the greatest \emph{discoveries}
in science were made by accident.}
\textbf{Some of the greatest \emph{discoveries}
in science were made by accident.}
This example produces the following output:
- Note: some packages, such as Beamer, change the behaviour of the
\emph
command.
Adding images
The following example demonstrates how to include a picture:
\documentclass{article}
\usepackage{graphicx} %LaTeX package to import graphics
\graphicspath{{images/}} %configuring the graphicx package
\begin{document}
The universe is immense and it seems to be homogeneous,
on a large scale, everywhere we look.
% The \includegraphcs command is
% provided (implemented) by the
% graphicx package
\includegraphics{universe}
There's a picture of a galaxy above.
\end{document}
This example produces the following output:
Importing graphics into a LaTeX document needs an add-on package which provides the commands and features required to include external graphics files. The above example loads the graphicx
package which, among many other commands, provides \includegraphics{...}
to import graphics and \graphicspath{...}
to advise LaTeX where the graphics are located.
To use the graphicx
package, include the following line in your Overleaf document preamble:
\usepackage{graphicx}
In our example the command \graphicspath{{images/}}
informs LaTeX that images are kept in a folder named images
, which is contained in the current directory:
The \includegraphics{universe}
command does the actual work of inserting the image in the document. Here, universe
is the name of the image file but without its extension.
Note:
- Although the full file name, including its extension, is allowed in the
\includegraphics
command, it’s considered best practice to omit the file extension because it will prompt LaTeX to search for all the supported formats. - Generally, the graphic’s file name should not contain white spaces or multiple dots; it is also recommended to use lowercase letters for the file extension.
More information on LaTeX packages can be found at the end of this tutorial in the section Finding and using LaTeX packages.
Captions, labels and references
Images can be captioned, labelled and referenced by means of the figure
environment, as shown below:
\documentclass{article}
\usepackage{graphicx}
\graphicspath{{images/}}
\begin{document}
\begin{figure}[h]
\centering
\includegraphics[width=0.75\textwidth]{mesh}
\caption{A nice plot.}
\label{fig:mesh1}
\end{figure}
As you can see in figure \ref{fig:mesh1}, the function grows near the origin. This example is on page \pageref{fig:mesh1}.
\end{document}
This example produces the following output:
There are several noteworthy commands in the example:
\includegraphics[width=0.75\textwidth]{mesh}
: This form of\includegraphics
instructs LaTeX to set the figure’s width to 75% of the text width—whose value is stored in the\textwidth
command.\caption{A nice plot.}
: As its name suggests, this command sets the figure caption which can be placed above or below the figure. If you create a list of figures this caption will be used in that list.\label{fig:mesh1}
: To reference this image within your document you give it a label using the\label
command. The label is used to generate a number for the image and, combined with the next command, will allow you to reference it.\ref{fig:mesh1}
: This code will be substituted by the number corresponding to the referenced figure.
Images incorporated in a LaTeX document should be placed inside a figure
environment, or similar, so that LaTeX can automatically position the image at a suitable location in your document.
Further guidance is contained in the following Overleaf help articles:
Creating lists in LaTeX
You can create different types of list using environments, which are used to encapsulate the LaTeX code required to implement a specific typesetting feature.
An environment starts with \begin{_environment-name_}
and ends with \end{_environment-name_}
where _environment-name_
might be figure
, tabular
or one of the list types: itemize
for unordered lists or enumerate
for ordered lists.
Unordered lists
Unordered lists are produced by the itemize
environment. Each list entry must be preceded by the \item
command, as shown below:
\documentclass{article}
\begin{document}
\begin{itemize}
\item The individual entries are indicated with a black dot, a so-called bullet.
\item The text in the entries may be of any length.
\end{itemize}
\end{document}
Ordered lists
Ordered lists use the same syntax as unordered lists but are created using the enumerate
environment:
\documentclass{article}
\begin{document}
\begin{enumerate}
\item This is the first entry in our list.
\item The list numbers increase with each entry we add.
\end{enumerate}
\end{document}
As with unordered
lists, each entry must be preceded by the \item
command which, here, automatically generates the numeric ordered-list label value, starting at 1.
For further information you can open this larger Overleaf project which demonstrates various types of LaTeX list or visit our dedicated help article on LaTeX lists, which provides many more examples and shows how to create customized lists.
Adding math to LaTeX
LaTeX provides two writing modes for typesetting mathematics:
- inline math mode used for writing formulas that are part of a paragraph
- display math mode used to write expressions that are not part of a text or paragraph and are typeset on separate lines
Inline math mode
Let’s see an example of inline math mode:
\documentclass[12pt, letterpaper]{article}
\begin{document}
In physics, the mass-energy equivalence is stated
by the equation $E=mc^2$, discovered in 1905 by Albert Einstein.
\end{document}
To typeset inline-mode math you can use one of these delimiter pairs: \( ... \)
, $ ... $
or \begin{math} ... \end{math}
, as demonstrated in the following example:
\documentclass[12pt, letterpaper]{article}
\begin{document}
\begin{math}
E=mc^2
\end{math} is typeset in a paragraph using inline math mode---as is $E=mc^2$, and so too is \(E=mc^2\).
\end{document}
Display math mode
Equations typeset in display mode can be numbered or unnumbered, as in the following example:
\documentclass[12pt, letterpaper]{article}
\begin{document}
The mass-energy equivalence is described by the famous equation
\[ E=mc^2 \] discovered in 1905 by Albert Einstein.
In natural units ($c = 1$), the formula expresses the identity
\begin{equation}
E=m
\end{equation}
\end{document}
To typeset display-mode math you can use one of these delimiter pairs:
\[ ... \]
,\begin{displaymath} ... \end{displaymath}
or\begin{equation} ... \end{equation}
.
Historically, typesetting display-mode math required use of $$
characters delimiters, as in $$ _... display math here ..._$$
, but this method is no longer recommended: use LaTeX’s delimiters \[ ... \]
instead.
More complete examples
The following examples demonstrate a range of mathematical content typeset using LaTeX.
\documentclass{article}
\begin{document}
Subscripts in math mode are written as $a_b$ and superscripts are written as $a^b$. These can be combined and nested to write expressions such as
\[ T^{i_1 i_2 \dots i_p}_{j_1 j_2 \dots j_q} = T(x^{i_1},\dots,x^{i_p},e_{j_1},\dots,e_{j_q}) \]
We write integrals using $\int$ and fractions using $\frac{a}{b}$. Limits are placed on integrals using superscripts and subscripts:
\[ \int_0^1 \frac{dx}{e^x} = \frac{e-1}{e} \]
Lower case Greek letters are written as $\omega$ $\delta$ etc. while upper case Greek letters are written as $\Omega$ $\Delta$.
Mathematical operators are prefixed with a backslash as $\sin(\beta)$, $\cos(\alpha)$, $\log(x)$ etc.
\end{document}
The next example uses the equation*
environment which is provided by the amsmath
package, so we need to add the following line to our document preamble:
\usepackage{amsmath}% For the equation* environment
For further information on using amsmath
see our help article.
\documentclass{article}
\usepackage{amsmath}% For the equation* environment
\begin{document}
\section{First example}
The well-known Pythagorean theorem \(x^2 + y^2 = z^2\) was proved to be invalid for other exponents, meaning the next equation has no integer solutions for \(n>2\):
\[ x^n + y^n = z^n \]
\section{Second example}
This is a simple math expression \(\sqrt{x^2+1}\) inside text.
And this is also the same:
\begin{math}
\sqrt{x^2+1}
\end{math}
but by using another command.
This is a simple math expression without numbering
\[\sqrt{x^2+1}\]
separated from text.
This is also the same:
\begin{displaymath}
\sqrt{x^2+1}
\end{displaymath}
\ldots and this:
\begin{equation*}
\sqrt{x^2+1}
\end{equation*}
\end{document}
The possibilities with math in LaTeX are endless so be sure to visit our help pages for advice and examples on specific topics:
- Mathematical expressions
- Subscripts and superscripts
- Brackets and Parentheses
- Fractions and Binomials
- Aligning Equations
- Operators
- Spacing in math mode
- Integrals, sums and limits
- Display style in math mode
- List of Greek letters and math symbols
- Mathematical fonts
Basic document structure
Next, we explore abstracts and how to partition a LaTeX document into different chapters, sections and paragraphs.
Abstracts
Scientific articles usually provide an abstract which is a brief overview/summary of their core topics, or arguments. The next example demonstrates typesetting an abstract using LaTeX’s abstract
environment:
\documentclass{article}
\begin{document}
\begin{abstract}
This is a simple paragraph at the beginning of the
document. A brief introduction about the main subject.
\end{abstract}
\end{document}
Open this example in Overleaf. This example produces the following output:
Paragraphs and new lines
With the abstract in place, we can begin writing our first paragraph. The next example demonstrates:
- how a new paragraph is created by pressing the "enter" key twice, ending the current line and inserting a subsequent blank line;
- how to start a new line without starting a new paragraph by inserting a manual line break using the
\\
command, which is a double backslash; alternatively, use the\newline
command.
\documentclass{article}
\begin{document}
\begin{abstract}
This is a simple paragraph at the beginning of the
document. A brief introduction about the main subject.
\end{abstract}
After our abstract we can begin the first paragraph, then press ``enter'' twice to start the second one.
This line will start a second paragraph.
I will start the third paragraph and then add \\ a manual line break which causes this text to start on a new line but remains part of the same paragraph. Alternatively, I can use the \verb|\newline|\newline command to start a new line, which is also part of the same paragraph.
\end{document}
Note how LaTeX automatically indents paragraphs—except immediately after document headings such as section and subsection—as we will see.
New users are advised that multiple \\
or \newline
s should not used to “simulate” paragraphs with larger spacing between them because this can interfere with LaTeX’s typesetting algorithms. The recommended method is to continue using blank lines for creating new paragraphs, without any \\
, and load the parskip
package by adding \usepackage{parskip}
to the preamble.
Further information on paragraphs can be found in the following articles:
- Paragraphs and new lines
- How to change paragraph spacing in LaTeX
- LaTeX Error: There’s no line here to end provides additional advice and guidance on using
\\
.
Chapters and sections
LaTeX also provides document-structuring commands but the available commands, and their implementations (what they do), can depend on the document class being used. By way of example, documents created using the book
class can be split into parts, chapters, sections, subsections and so forth but the letter
class does not provide (support) any commands to do that.
This next example demonstrates commands used to structure a document based on the book
class:
\documentclass{book}
\begin{document}
\chapter{First Chapter}
\section{Introduction}
This is the first section.
Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Etiam lobortisfacilisis sem. Nullam nec mi et
neque pharetra sollicitudin. Praesent imperdietmi nec ante.
Donec ullamcorper, felis non sodales...
\section{Second Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem. Nullam nec mi et neque pharetra
sollicitudin. Praesent imperdiet mi necante...
\subsection{First Subsection}
Praesent imperdietmi nec ante. Donec ullamcorper, felis non sodales...
\section*{Unnumbered Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem...
\end{document}
Open this example in Overleaf. This example produces the following output:
The names of sectioning commands are mostly self-explanatory; for example, \chapter{First Chapter}
creates a new chapter titled First Chapter
, \section{Introduction}
produces a section titled Introduction
, and so forth.
Sections can be further divided into \subsection{...}
and even \subsubsection{...}
. The numbering of sections, subsections etc. is automatic but can be disabled by using the so-called starred version of the appropriate command which has an asterisk (*) at the end, such as \section*{...}
and \subsection*{...}
.
Collectively, LaTeX document classes provide the following sectioning commands, with specific classes each supporting a relevant subset:
\part{part}
\chapter{chapter}
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\paragraph{paragraph}
\subparagraph{subparagraph}
In particular, the \part
and \chapter
commands are only available in the report
and book
document classes.
Visit the Overleaf article article about sections and chapters for further information about document-structure commands.
Creating tables
The following examples show how to create tables in LaTeX, including the addition of lines (rules) and captions.
Creating a basic table in LaTeX
We start with an example showing how to typeset a basic table:
\begin{center}
\begin{tabular}{c c c}
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9
\end{tabular}
\end{center}
The tabular
environment is the default LaTeX method to create tables.
You must specify a parameter to this environment, in this case {c c c}
which advises LaTeX that there will be three columns and the text inside each one must be centred. You can also use r
to right-align the text and l
to left-align it.
The alignment symbol &
is used to demarcate individual table cells within a table row. To end a table row use the new line command \\
.
Adding borders
The tabular
environment supports horizontal and vertical lines (rules) as part of the table:
- to add horizontal rules, above and below rows, use the
\hline
command - to add vertical rules, between columns, use the vertical line parameter
|
In this example the argument is {|c|c|c|}
which declares three (centred) columns each separated by a vertical line (rule); in addition, we use \hline
to place a horizontal rule above the first row and below the final row:
\begin{center}
\begin{tabular}{|c|c|c|}
\hline
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{center}
Here is a further example:
\begin{center}
\begin{tabular}{||c c c c||}
\hline
Col1 & Col2 & Col2 & Col3 \\ [0.5ex]
\hline\hline
1 & 6 & 87837 & 787 \\
\hline
2 & 7 & 78 & 5415 \\
\hline
3 & 545 & 778 & 7507 \\
\hline
4 & 545 & 18744 & 7560 \\
\hline
5 & 88 & 788 & 6344 \\ [1ex]
\hline
\end{tabular}
\end{center}
Tip!
- Creating tables in LaTeX can be time-consuming so you may want to use the TablesGenerator.com online tool to export LaTeX code for tabulars.
Captions, labels and references
You can caption and reference tables in much the same way as images. The only difference is that instead of the figure
environment, you use the table
environment.
Table \ref{table:data} shows how to add a table caption and reference a table.
\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||}
\hline
Col1 & Col2 & Col2 & Col3 \\ [0.5ex]
\hline\hline
1 & 6 & 87837 & 787 \\
2 & 7 & 78 & 5415 \\
3 & 545 & 778 & 7507 \\
4 & 545 & 18744 & 7560 \\
5 & 88 & 788 & 6344 \\ [1ex]
\hline
\end{tabular}
\caption{Table to test captions and labels.}
\label{table:data}
\end{table}
Open this example in Overleaf. This example produces the following output:
Adding a Table of Contents
Creating a table of contents is straightforward because the command \tableofcontents
does almost all the work for you:
\documentclass{article}
\title{Sections and Chapters}
\author{Gubert Farnsworth}
\date{August 2022}
\begin{document}
\maketitle
\tableofcontents
\section{Introduction}
This is the first section.
Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Etiam lobortisfacilisis sem. Nullam nec mi et
neque pharetra sollicitudin. Praesent imperdietmi nec ante.
Donec ullamcorper, felis non sodales...
\section*{Unnumbered Section}
\addcontentsline{toc}{section}{Unnumbered Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem. Nullam nec mi et neque pharetra
sollicitudin. Praesent imperdiet mi necante...
\section{Second Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem. Nullam nec mi et neque pharetra
sollicitudin. Praesent imperdiet mi necante...
\end{document}
Sections, subsections and chapters are automatically included in the table of contents. To manually add entries, such as an unnumbered section, use the command \addcontentsline
as shown in the example.
Finding and using LaTeX packages
LaTeX not only delivers significant typesetting capabilities but also provides a framework for extensibility through the use of add-on packages. Rather than attempting to provide commands and features that “try to do everything”, LaTeX is designed to be extensible, allowing users to load external bodies of code (packages) that provide more specialist typesetting capabilities or extend LaTeX’s built-in features—such as typesetting tables. As observed in the section Adding images, the graphicx
package extends LaTeX by providing commands to import graphics files and was loaded (in the preamble) by writing
Loading packages
As noted above, packages are loaded in the document preamble via the \usepackage
command but because (many) LaTeX packages provide a set of options, which can be used to configure their behaviour, the \usepackage
command often looks like this:
\usepackage[options]{somepackage}
The square brackets “[...]
” inform LaTeX which set of options should be applied when it loads somepackage
. Within the set of options requested by the user, individual options, or settings, are typically separated by a comma; for example, the geometry
package provides many options to configure page layout in LaTeX, so a typical use of geometry
might look like this:
\usepackage[total={6.5in,8.75in},
top=1.2in, left=0.9in, includefoot]{geometry}
The geometry
package is one example of a package written and contributed by members of the global LaTeX community and made available, for free, to anyone who wants to use it.
If a LaTeX package does not provide any options, or the user wants to use the default values of a package’s options, it would be loaded like this:
When you write \usepackage[...]{somepackage}
LaTeX looks for a corresponding file called _somepackage_.sty
, which it needs to load and process—to make the package commands available and execute any other code provided by that package. If LaTeX cannot find _somepackage_.sty
it will terminate with an error, as demonstrated in the following Overleaf example:
\documentclass[12pt, letterpaper]{article}
\usepackage{somepackage}% a NON-EXISTENT package
\begin{document}
This will fail!
\end{document}
Open this error-generating example on Overleaf This example produces the following output:
Finding information about packages: CTAN
Packages are distributed through the Comprehensive TeX Archive Network, usually referred to as CTAN, which, at the time of writing, hosts 6287 packages from 2881 contributors. CTAN describes itself as
… a set of Internet sites around the world that offer TEX-related material for download.
You can browse CTAN to look for useful packages; for example:
- by topic
- alphabetically (useful if you know the package name)
You can also use the search facility (at the top of the page).
Packages available on Overleaf: Introducing TeX Live
Once per year a (large) subset of packages hosted on CTAN, plus LaTeX-related fonts and other software, is collated and distributed as a system called TeX Live, which can be used to install your own (local) LaTeX setup. In fact, Overleaf’s servers also use TeX Live and are updated when a new version of TeX Live is released. Overleaf’s TeX Live updates are not immediate but take place a few months post-release, giving us time to perform compatibility tests of the new TeX Live version with the thousands of templates contained in our gallery. For example, here is our TeX Live 2022 upgrade announcement.
Although TeX Live contains a (large) subset of CTAN packages it is possible to find an interesting package, such as igo
for typesetting Go diagrams, which is hosted on CTAN but not included in (distributed by) TeX Live and thus unavailable on Overleaf. Some packages hosted on CTAN are not part of TeX Live due to a variety of reasons: perhaps a package is obsolete, has licensing problems, is extremely new (recently uploaded) or has platform dependencies, such as working on Windows but not Linux.
New packages, and updates to existing ones, are uploaded to CTAN all year round but updates to TeX Live are distributed annually; consequently, packages contained in the current version of TeX Live will not be as up-to-date as those hosted on CTAN. Because Overleaf’s servers use TeX Live it is possible that packages installed on our servers—i.e., ones available to our users—might not be the very latest versions available on CTAN but, generally, this is unlikely to be problematic.