Latex: the first document

2018-06-17 LaTeX

So let’s start! You need a basic tutorial to get your hands a bit dirty with LaTeX but you don’t know where to start? I’m here for you.

Install LaTeX

Well that’s easy enough! If you’re under Debian or one of it’s derived distribution you just have to type:

sudo apt install texlive-full

and wait quite some time ;) If you really want to install an IDE specialized in LaTeX:

sudo apt install texmaker

I’ve been told there’s another editor for LaTeX TexStudio that’s way better but I haven’t tried it (and I don’t use TeXmaker either).

Document types in LaTeX

There is a multitude of document types available in LaTeX, here are some of the most famous:

  • the article class, which is the base one. It is used for scientific articles, reports, software documentation, and in general for a lot of things,
  • the report class, which is used for long reports containing chapters, thesis, …
  • the beamer class, which is used for presentations,
  • the tikzposter class, which is used for posters,
  • the lettre class, which is used for letters,
  • the book class, which is used you know… for books.

I will mainly focus on the article throughout the articles.

Let’s get practical!

Hello World!

In TeXMaker, click on the icon for a new document and please write:

\documentclass[paper=a4, fontsize=11pt, ]{article}<br />\begin{document}<br />Hello world!<br />\end{document}

What did we just do? We just told LaTeX to work with the class article, with A4 paper and with a fontsize of 11pt.

Then we have the tags

\begin{document}
and
\end{document}
and between those we write the document, here the string “Hello world!”. Thereafter, we will call preamble the part between the \documentclass and the \begin{document}.

Now you can save your document and click on “Run” and select “PDFLaTeX” which is the name of the compiler we’re gonna use. You can then click on the small blue arrow to compile your first LaTeX document and then click on the blue arrow next to “View PDF”.

What about an author, a date and a title?

Sure thing! We’re gonna use the following commands: \author, \date and \title.

\author{Maria Climent-Pommeret}
\title{My first \LaTeX document}
\date{\today}

We use the command \today that puts the date of the day of compilation which might come in handy and the command \LaTeX that pretty-prints LaTeX. Please put this in your preamble and compile again. Do you notice any difference? No, because we didn’t tell LaTeX to display the title… How do we do this? By putting:

\maketitle

in the body of the document (that is, right after the \begin{document} tag).

But the date isn’t displayed in my language!

Damn. You’re not writing in English. Well we have to tell LaTeX that you’re not writing in english then… I’ll take the French example. We have to add a new command to the preamble: one that makes it use the package babel with the french option:

\usepackage[frenchb]{babel}

Re run the compilation et display again your PDF, everything should be in place by now: the date will be written in your language. Did everything run smoothly? No, it did not. Damn.

Warning: OT1 encoding should not be used for French

You’ll get your fair share of LaTeX warnings and will soon learn to ignore some of them, but you can’t possibly ignore this one. Why you might ask? Well try replacing your “Hello World!” by the french holoalphabetic sentence “Voix ambiguë d’un cœur qui, au zéphyr, préfère les jattes de kiwis.” and take a look at your PDF.

Quick to the preamble!

Yup, well spotted, we have to add some new things to our preamble. In fact, I’m gonna make you add a couple of things so that everything will run smoothly from now on. Since you’ve probably guessed, we’ll have to add a tons of stuff in our preamble in order to make LaTeX do what we expect it of doing. Therefore, we’re going to put some parts of our preamble in a separate document that we will be able to link when we will need to create a new document. So please, copy/paste this block to a new file called

preamble.tex
that you will put in the same directory as your
helloworld.tex
:) You don’t need to understand every bit of it, just know that the line correcting the warning is the fontenc one.

%---------------
% Basic packages
%---------------
%-% Hello there LaTeX. I'm speaking a strange language with weird
%-% characters, could you be kind enough to deal with it properly?
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{lmodern}

%-% Hi LaTeX please apply french rules!
%-% or any other languages rules, really: \usepackage[british]{babel}
\usepackage[frenchb]{babel}

%-% If you want another font, please uncomment
%\usepackage{helvet}
%\usepackage{courier}

%-% To rotate a page
%-% http://tex.stackexchange.com/questions/111648/rotating-vs-lscape-vs-pdflscape
\usepackage{pdflscape}

%-% Let's assume you want to create the following macro:
%-% \newcommand{\apples}{AppleS}
%-% If you leave it that way, the space is gonna be eaten:
%-% "I like eating \apples from my garden -> "I like eating AppleSfrom my garden."
%-% This macro won't save you:
%-% \newcommand{\apples}{AppleS }
%-% "I like \apples." -> "I like AppleS ."
%-% Conclusion: you need \xspace at the end of your macro
%-% \newcommand{\apples}{\textsf{AppleS}\xspace}
\usepackage{xspace}

%-% To fill up space with Lorem Ipsum:
%-% \lipsum or \lipsum[1-6] or \lipsum[1-2]…
\usepackage{lipsum}

Note: if you use babel > 3.0 you have to invoke it that way:

\documentclass[french]
\usepackage{babel}

Changes in the body of the document

Now that we have a preamble.tex, we can include it in the body of the document, that will only contain the interesting part of the document: the real content.

\documentclass[paper=a4, fontsize=11pt,]{article}

\input{preamble}

\author{Alice Climent-Pommeret}
\title{My first \LaTeX document}
\date{\today}

\begin{document}
\maketitle

Voix ambiguë d'un cœur qui, au zéphyr, préfère les jattes de kiwis.

\lipsum

\end{document}

Re run and display your PDF. Everything went as expected!

Note the use of the \input command that will become very handy as your documents grow: we will input some sections of the document to the main one, and, this way, hierarchize our content. Therefore, you would be able to comment out some chapters of your thesis, in order to compile only the chapters you’re currently working on, decreasing the compilation time.

A (very simple) front page

To get a very very simple front page for your document (I promise we’ll get to the part where we get to create fancy front pages) you can add the command \clearpage right after \maketitle. By doing so, we’re telling LaTeX to end the page, and start a new one with whatever is after the command.

\documentclass[paper=a4, fontsize=11pt,]{article}

\input{preamble}

\author{Alice Climent-Pommeret}
\title{My first \LaTeX document}
\date{\today}

\begin{document}
\maketitle
\clearpage

Voix ambiguë d'un cœur qui, au zéphyr, préfère les jattes de kiwis.

\lipsum

\end{document}

Alternatively, you can pass the option titlepage to your documentclass.

Let’s add some structure to the document!

There exists several levels of structuration in LaTeX. Let’s focus on the ones that are available in the class we’re currently using: article.

Here there are, from the biggest to the smallest:

  • \part{Name of the part}
  • \section{Name of the section}
  • \subsection{Name of the sub-section}
  • \subsubsection{Name of the sub-sub-section}
  • \paragraph{Name of the paragraph}
  • \subparagraph{Name of the sub-paragraph}

The granularity level is already quite impressive. Test them out to see how they are rendered in your document!

By the way, if you want an un-numbered structure element, you just have to put a * at the end of it’s name: \part*{Un-numbered part}.

Table of contents

Structuring is fun and cool, but it’s not very useful if we don’t get a table of contents to navigate within the document.

You just have to add, after your \maketitle (or wherever, really, I just like to have my table of content right after the title) the following command:

\tableofcontents

Re-run compilation twice (table of contents usually needs two successive two compilations to be accurate, that’s just how it works) and reload your PDF to admire the result!

I bet you’re a bit disappointed ;) Where the hell are the paragraphs and sub-paragraphs?! Well that’s just LaTeX we need to tell it to display those. Let’s put the following code in the preamble.tex and play a bit with the number between {}:

%--------------------------------
% Headers, footers, TOC, Appendix
%--------------------------------

%-% Depth of the TOC
\setcounter{tocdepth}{6}

Here you go ;)

Let’s stop here for now, see you soon!