Aller au contenu

LaTeX is a great tool to create documents. It's based on the 'WYSIWYM' (what you see is what you mean) idea, meaning you only have to focus on the contents of your document and the computer will take care of the formatting. With LaTeX, it's very easy to create professional-looking material. This article presents the basics of how to create a document.

Introduction

Let's start with the simplest working example:

\documentclass{article}

\begin{document}
First document. This is a simple example, with no 
extra parameters or packages included.
\end{document}

BasicsEx1.png


The input file is just a plain text file, with the extension .tex. It will contain code that the computer interprets to produce a PDF file. The first line of code declares the type of document, in this case is an article. Then, between the \begin{document} \end{document} tags you must write the text of your document.

Note: To learn how to generate the output file see our article on compiling.

  Open an example in Overleaf

The preamble of a document

In the previous example the text was entered after the \begin{document} command. The part of your .tex file before this point is called the preamble. In the preamble, you define the type of document you are writing and the language, load extra packages you will need, and set several parameters. For instance, a normal document preamble would look like this:

\documentclass[12pt, letterpaper]{article}
\usepackage[utf8]{inputenc}

\title{First document}
\author{Hubert Farnsworth \thanks{funded by the ShareLaTeX team}}
\date{February 2014}

Below a detailed description of each line:

\documentclass[12pt, letterpaper]{article}
As said before, this defines the type of document. Some additional parameters inside brackets and comma-separated can be passed to the command. In the example, the extra parameters set the font size (12pt) and the paper size (letterpaper). Of course other font sizes (9pt, 11pt, 12pt) can be used, the default size is 10pt. As for the paper size, other possible values are included A4 and legalpaper. Note that Overleaf uses a European LaTeX distribution, which produces documents in A4 size by default. See our [Page size and margins]] article for for information about using the Geometry package to set further formatting parameters.
\usepackage[utf8]{inputenc}
This is the encoding for the document, to allow characters beyond ASCII (e.g. à, ü, č ...) to be used in the text. It can be omitted or changed to another encoding but utf-8 is recommended. Unless you specifically need another encoding, or if you are unsure about it, add this line to the preamble.

The next three lines are self-descriptive. Anyway, you can see a description of what they actually do in the next section.

Another important parameter that can be passed to the \documentclass command is twocolumn if you want your text in a two-column format and twoside for two-side paper sheet printing.

  Open an example in Overleaf

Displaying the title of your document

To display the title of your document you have to declare its components in the preamble and then use some additional code:


\documentclass[12pt, letterpaper, twoside]{article}
\usepackage[utf8]{inputenc}

\title{First document}
\author{Hubert Farnsworth \thanks{funded by the ShareLaTeX team}}
\date{February 2014}

\begin{document}

\begin{titlepage}
\maketitle
\end{titlepage}

In this document some extra packages and parameters
were added. There is an encoding package
and pagesize and fontsize parameters.

\end{document}

BasicsEx2.png


There is a block with three lines in the preamble that defines the information to be included on the title page.

\title{First document}
This is the title.
\author{Hubert Farnsworth}
Here you put the name(s) of the author(s) and, as an optional parameter, you can add the next command:
\thanks{funded by the ShareLaTeX team}
This can be added after the name of the author, inside the braces of the title 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{February 2014}
You can enter the date manually or use the command \today so the date will be updated automatically at the time you compile your document.

Once you have that in the preamble now in the body of your document you can use the next commands for the information to be printed.

\begin{titlepage} \end{titlepage}
This declares an environment, a block of code with a specific behaviour depending on its type. In this case whatever you include in this titlepage environment will appear in the first page of your document.
\maketitle
This command will print the title, the author and the date in the format shown in the example. If it's not enclosed in a titlepage environment, it will be shown at the beginning of the document, above the first line.

  Open an example in Overleaf

Basic formatting: abstract, paragraphs and newlines

Everything included inside the \begin{document} \end{document} commands will be rendered in the final document.

\documentclass[12pt, letterpaper, twoside]{article}
\usepackage[utf8]{inputenc}

\begin{document}

\begin{abstract}
This is a simple paragraph at the beginning of the document. A brief introduction to the main subject.
\end{abstract}

In this document some extra packages and parameters
were added. There is an encoding package,
and pagesize and fontsize parameters.

This line will start a second paragraph. And I can
 brake\\ the lines \\ and continue on a new line.

\end{document}

BasicEx3.1.png


In scientific documents, it's a common practice to include a brief overview of the main subject of the paper. In LaTeX, the abstract environment is for this purpose. The abstract environment will put the text in a special format at the top of your document.

When writing the contents of your document, if you need to start a new paragraph you must hit the "Enter" key twice (to insert a double blank line). Notice that paragraphs have a white space before the first line.

To start a new line without actually starting a new paragraph insert a break line point, this can be done by \\ (a double backslash as in the example) or the \newline command

You can find more information in the Paragraphs and new lines article.

For a more complete discussion about document structuring see the article about sections and chapters.

  Open an example in Overleaf

Comments

Sometimes it's necessary to add comments to your LaTeX code for readability. This is straightforward, put a % before the comment and LaTeX will ignore that text.

\documentclass{article}
\usepackage[utf8]{inputenc} %codification of the document

\usepackage{comment}

%Here begins the body of the document
\begin{document}
This document contains a lot of comments, none of them
will appear here, only this text.

This document contains a lot of comments, none of them
will appear here, only this text.

\begin{comment}
This text won't show up in the compiled pdf
this is just a multi-line comment. Useful
to, for instance, comment out slow-rendering parts
while working on a draft.
\end{comment}

\end{document}

BasicEx4.1.png


In the last part of the example, you can see a comment environment. This helps in multi-line comments instead of putting a % at the beginning of each line. For this to work you must add the next line to your preamble:

\usepackage{comment}

The % symbols is a reserved character, if you actually need this symbol to be printed in your document, use \%. See the reference guide for a full list of reserved characters.

Reference guide

Document types available in the \documentclass command.

Document type Description
article For short documents and journal articles. Is the most commonly used.
report For longer documents and dissertations.
book Useful to write books
letter For letters
slides For slides, rarely used
beamer Slides in the Beamer class format. See the beamer documentation for a better description

Reserved characters

The following symbol characters are reserved by LaTeX because they introduce a command and have a special meaning.

# $ % ^ & _ { } ~ \

These symbols and can be printed with special commands (in some cases - inside mathematical environment).

Character Function How to print it
# Macro parameter \#
$ Math mode \$
% Comment \%
^ Superscript (in math mode) \^{} or $\textasciicircum$
& Separate column entries in tables \&
_ Subscript (in math mode) \_
{ } Processing block \{ \}
~ Unbreakable space, use it whenever you want to leave a space which is unbreakable $\textasciitilde$ or \~{}
\ Starting commands, which extend until the first non-alphanumerical character $\textbackslash$ or $\backslash$

Further reading

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX