The basics of LaTeX
Hello fellows! Today, I introduce a very important language in my PhD, LaTeX (what a logo!). In this article, I’m going to introduce very quickly the purpose, advantages and mechanisms of this language, and then give some examples and extension. Finally, I’ll try to give a sample file with some indications so that you are able to create your own!
Some historic elements
LaTeX has been created in 1983 by Leslie Lamport (a famous American researcher), but actually relies on TeX a language created by Donald Knuth (another very famous American researcher). The goal of LaTeX is to help create scientific articles. It is a word processor, but it is different from Word for instance. Indeed, Word is what we usually call a WYSIWYG (“What You See Is What You Get”), while LaTeX is not.
The creation date of TeX and LaTeX are very important. At that moment, Word did not exist at all, and it was difficult to create document with complex mathematic formulas. Remember that the equation functionnalitiees in Word apppeared quite recently, in 2003. Before TeX, the common way of typing documents was… typewriter! Specific typewriters with mathematics symbols were used by secretaries to type under the dictation of author.
Principles and distributions of LaTeX
As I said above, LaTeX is a word processor. The idea is that you type some code (a good comparison is html; as in html, the elements and the structure of a LaTeX document are represented by tags) and a program (pdfLaTeX for instance) processes the file and creates a document (pdf for instance).
Several versions of LaTeX exist, with different feature. The very basic version of LaTeX is pdfLaTeX. This verison is the most widely used. A second version of LaTeX is XeTeX. XeTeX is another version of LaTeX which is more efficient with utf-8 special characters. However, XeTeX is less compatible with some LaTeX packages, including widely used ones. A third version of LaTeX is LuaLaTeX. This version has been considered by many people (and for years) as the future of LaTeX. I must admit that I don’t know much about this distribution, except that it is based on a scirpting language called Lua. This version, however, is supposed to solve many problems of LaTeX, as compatibility, utf-8 and so on.
The extension for latex files is “.tex”. To compile it with PdfLaTeX, just type pdflatex yourfile.tex. This will produce several files, an auxiliary file (.aux) with information about your document (figures etc.), a log file and the most important a pdf file! This pdf file is the output of your .tex file, and shoud correspond to what is inside. Smoe numberings should not be fine. If so, compile a second time. When compiling, LaTeX also creates indexes inside the aux file, but you may need to compile a second time so that the output file takes it into consideration. I will go back to some advanced features, as tables of content and bibliography in a future post.
Editing with LaTeX
In this section, we give basics of edition with LaTeX. In the first part, we give the very basics to create a latex document. In the second part of this section, we give some notes on more advanced formatting, as layout or fonts.
Basics
The general structure of a LaTeX document is as follows:
\documentclass[option]{class}
\usepackage[utf8]{inputenc}
\usepackage[OT1]{fontenc}\usepackage[option]{package}\title{title}
\author{author}
\date{date}\begin{document}document\end{document}
Let’s analyze this sample file in more details. The first line defines the type of document (article, presentation, report, PhD thesis, book…). Some options can also be defined here.
- First, the format of the pages (a4paper, a3paper, letterpaper…), and the font size (10pt, 11pt or 12pt).
- Second, some packages, to define the encoding, the font etc. of the file. Here, we use two packages: fontenc and inputenc. The inputenc package gives the encoding of the file (ISO, UTF8 etc.), and the fontenc package gives the kind of font that is used (I recommend OT1). Finally, we can add other packages for anything: figures (tikz), math special symbols (amsmath, amssymb) etc.
- Then, we define some constants for the file: the authors, the title and the date (if the date is not defined, it will be set as today); these constants will be used in the title page
- The \begin{document} \end{document} environment defines the output of the latex file. It usually begins with a \titlepage, which will generate a title page or section.
Commands and environment
To write special characters and display specific elements, it is necessayry to use two kinds of special features, commands and environments. Basically, we can say that these two features are closely related, and can be used to change the family of the font, create lists and color text. The main difference is that commands are often aimed at elements of less that one line, while environment are aimed at elements of more than one line. For instance, creatig lists is performed through an environment, while putting text in italics is performed by using a command.
Some commands and environments are available only after loading some packages. Commands are used by \commandname{parameter1}{parameter2}… For instance, displaying the LaTeX logo is done by writing \LaTeX (no parameter). The environment are defined using the keywords \begin{environmentname}{parameter1…}[options] and \end{environmentname}. For instance, to create a figure, we type \begin{figure}our figure\end{figure}. Now, let have a look to the most common commands and environments.
Basic commands and environments include:
- \textit{text} to put text in italics (changes the font)
- \textsl{text} to slope the text (does not change the font)
- \texttt{text} to write the text in a monospace typemachine style
- \textbf{text} to write in bold
- \underline{text} to underline the text
- \begin{itemize}
\item an item
\end{itemize} creates an unordered list. The bullets are “\item” - \begin{enumerate}
\item an item
\end{enumerate} creates an ordered list. The numbers are “\item” - \begin{figure}
my figure
\caption{my caption}
\label{my label}
\end{figure} creates a floating environment for figures. The figure can be an outside image, using \includegraphics (see below), but also a graphics created thanks to Tikz, - \includegraphics{path/to/image} includes an outside picture (in png, jpg, jpeg or pdf format), needs to include the graphicx package,
- \label{key} and \ref{key} is used to reference another part of the document. To crate a reference, put a \label{key} wherever you want (inside text, figure…) and to make a reference, put a \ref{key} with the corresponding key. As a result, you will see the corresponding numbering appears in the output.
- \colortext{color}{text} needs xcolor package and allows to color text (basic colors are white, black, red, green, blue, cyan, magenta and yellow.
Basics of Maths
To introduce inline maths in LaTeX, we surround the maths with $. For instance, $\dfrac{a}{2}$ will display a/2. It is also possible to write maths in a new line surrounding maths by $$. For instance $$a=5$$ will display a=5 on a new line. However, by using the amsmath package, it is also possible to enumerate equations, by using the environment equation. This environment will do the same as $$, but will also numerate the equations, which is a good practice. The tags will totally replace the dollar signs, which are not needed at all.
Some commands are particular to the math mode, and allow writing equations. Basic math commands include:
- \times: ×
- \cdot ·
- ^{2} will put the text as superscript: a²
- _{2} will put the text as subscript: a₂
- it is possible to write the sum symbol with \sum, and the product one with \prod: Σ, Π
If I give here the very basics of the math commands and environment are here, there is much more: Greek letters, logic symbols… There is much more to explore, and a specific Stackexchange forum is dedicated to Latex.
Extensions and libraries
OK. Now, let talk about something very important: libraries. LaTeX is designed to work with libraries, and this is one of its main advatages. Drawing, defining theorems, or even write runic symbols. There are libraries for all these purcposes. Let first talk about drawing
Tikz and Pgf
Tikz and Pgf, as Beamer (see below) have been created by the same contributor, Till Tantau.
Tikz is the most used LaTeX library for drawing. It is used to create many figures, and create them as vectorial representation. Vectorial representation represents image as lines and shapes, not as pixels, which means that images cannot be pixelled, and quality is infinite. Tikz allow to create anything that can be represented by shapes, and is based on a lower level library, Pgf. It is for instance possible to create curves, graphs and diagrams.
Tikz is quite a high level library. It is por instance possible to manipulate objects as nodes (for graphs), edges, arrows, curved lines, and shapes as circles, ellipses and diamonds. Tikz uses its own libraries, to avoid compiling too much code (as all code loaded in a LaTeX document is compiled, even if it is not used). It is then necessary to indicate Tikz what library you want to use if you want to use advanced features. You can find an example of a Tikz figure in the section example below.
As I mentionned, it is possible to create any kind of figures in Tikz, and Tikz is based on Pgf. However, it is also possible to use other libraries based on Pgf. It can be useful when Tikz seems too low level, for instance if you want to create charts. One of the most efficient libraries in this case is pgfplots. This library enables the creation of various charts, lines as much as bar, combo areas etc. PgfPlots is even able to crate 3D charts. Once again, you can have a look on several figures created by PgfPlots in the example section, below.
Before looking at other libraries, I let you several documents and pages. First, the official manual of pgf and Tikz, written by Till Tantau himself. Then, and this is a present for my French fellows, a very, very good manual for Tikz, Tikz pour l’impatient. Then, for PgfPlots, the official manual of this library,. And to conclude, a very good website with wonderful examples of Tikz figures.
Maths and theorems
Now, let assume you want to write maths and redact theorems. A very good package for that is amsthm. This package enables writing personalized theorems, definitions and proofs. As usual, an example is given below, in the example section. Another package, which enables writing some complex math is amsmath. This package makes it possible to write sets as naturals (ℕ), real numbers (ℝ), calligraphic math letters (𝒜, ℬ for instance).
As I told, this article is only aimed at introducing the basics of LaTeX, and there much more to see and to Explore. From time to time, I’ll give some examples and additionnal informations on features I have found, but the best way to begin is to download it, either the TeXLive or MikTeX distribution. TeXLive comes with an editor, TeXMaker, but for Linux users, I would recommend Kile. Finally, for French learners, the Openclassroom tutorial is quite good and takers it all from the beginning.
Before leaving you, I give you an example of use of LaTeX: this very post (until this line to avoid LaTinception). The tex file looks like:
\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[OT1]{fontenc}
\title{The basics of LaTeX}
\author{Cédric Buron}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{listings}
\lstset{language=Pascal}\begin{document}
\maketitleHello fellows! Today, I introduce a very important language in my PhD, \LaTeX (what a logo!). In this article, I’m going to introduce very quickly the purpose, advantages and mechanisms of this language, and then give some examples and extension. Finally, I’ll try to give a sample file with some indications so that you are able to create your own!\section{Some historic elements}\LaTeX has been created in 1983 by Leslie Lamport (a famous American researcher), but actually relies on \TeX a language created by Donald Knuth (another very famous American researcher). The goal of \LaTeX is to help create scientific articles. It is a word processor, but it is different from Word for instance. Indeed, Word is what we usually call a WYSIWYG (``What You See Is What You Get''), while \LaTeX is not.The creation date of TeX and LaTeX are very important. At that moment, Word did not exist at all, and it was difficult to create document with complex mathematic formulas. Remember that the equation functionnalitiees in Word apppeared quite recently, in 2003. Before TeX, the common way of typing documents was… typewriter! Specific typewriters with mathematics symbols were used by secretaries to type under the dictation of author.\section{Principles and distributions of LaTeX}As I said above, LaTeX is a word processor. The idea is that you type some code (a good comparison is html; as in html, the elements and the structure of a LaTeX document are represented by tags) and a program (pdfLaTeX for instance) processes the file and creates a document (pdf for instance).
Several versions of LaTeX exist, with different feature. The very basic version of LaTeX is pdfLaTeX. This verison is the most widely used. A second version of LaTeX is XeTeX. XeTeX is another version of LaTeX which is more efficient with utf-8 special characters. However, XeTeX is less compatible with some LaTeX packages, including widely used ones. A third version of LaTeX is LuaLaTeX. This version has been considered by many people (and for years) as the future of LaTeX. I must admit that I don’t know much about this distribution, except that it is based on a scirpting language called Lua. This version, however, is supposed to solve many problems of LaTeX, as compatibility, utf-8 and so on.The extension for latex files is ``.tex''. To compile it with PdfLaTeX, just type pdflatex yourfile.tex. This will produce several files, an auxiliary file (.aux) with information about your document (figures etc.), a log file and the most important a pdf file! This pdf file is the output of your .tex file, and shoud correspond to what is inside. Smoe numberings should not be fine. If so, compile a second time. When compiling, LaTeX also creates indexes inside the aux file, but you may need to compile a second time so that the output file takes it into consideration. I will go back to some advanced features, as tables of content and bibliography in a future post.\section{Editing with LaTeX}In this section, we give basics of edition with LaTeX. In the first part, we give the very basics to create a latex document. In the second part of this section, we give some notes on more advanced formatting, as layout or fonts.
\subsection{Basics}The general structure of a LaTeX document is as follows:
\begin{lstlisting}
\documentclass[option]{class}
\usepackage[utf8]{inputenc}
\usepackage[OT1]{fontenc}\usepackage[option]{package}\title{title}
\author{author}
\date{date}\begin{document}document\end{document}
\end{lstlisting}Let’s analyze this sample file in more details. The first line defines the type of document (article, presentation, report, PhD thesis, book…). Some options can also be defined here.
\begin{itemize}
\item First, the format of the pages (a4paper, a3paper, letterpaper…), and the font size (10pt, 11pt or 12pt).
\item Second, some packages, to define the encoding, the font etc. of the file. Here, we use two packages: fontenc and inputenc. The inputenc package gives the encoding of the file (ISO, UTF8 etc.), and the fontenc package gives the kind of font that is used (I recommend OT1). Finally, we can add other packages for anything: figures (tikz), math special symbols (amsmath, amssymb) etc.
\item Then, we define some constants for the file: the authors, the title and the date (if the date is not defined, it will be set as today); these constants will be used in the title page
The \verb|\begin{document}| \verb|\end{document}| environment defines the output of the latex file. It usually begins with a \verb|\titlepage|, which will generate a title page or section.
\end{itemize}\subsection{Commands and environment}To write special characters and display specific elements, it is necessayry to use two kinds of special features, commands and environments. Basically, we can say that these two features are closely related, and can be used to change the family of the font, create lists and color text. The main difference is that commands are often aimed at elements of less that one line, while environment are aimed at elements of more than one line. For instance, creatig lists is performed through an environment, while putting text in italics is performed by using a command.
Some commands and environments are available only after loading some packages. Commands are used by \verb|\commandname{parameter1}{parameter2}…| For instance, displaying the LaTeX logo is done by writing \verb|\LaTeX| (no parameter). The environment are defined using the keywords \verb|\begin{environmentname}{parameter1…}[options]| and \verb|\end{environmentname}|. For instance, to create a figure, we type \verb|\begin{figure}our figure\end{figure}|. Now, let have a look to the most common commands and environments.
Basic commands and environments include\begin{itemize}
\item \verb|\textit{text}| to put text in italics (changes the font)
\item \verb|\textsl{text}| to slope the text (does not change the font)
\item \verb|\texttt{text}| to write the text in a monospace typemachine style
\item \verb|\textbf{text}| to write in bold
\item \verb|\underline{text}| to underline the text
\item \begin{verbatim}
\begin{itemize}
\item an item
\end{itemize}
\end{verbatim} creates an unordered list. The bullets are ``\verb|\item|''
\item \begin{verbatim}\begin{enumerate}
\item an item
\end{enumerate}
\end{verbatim} creates an ordered list. The numbers are ``\verb|\item|''
\item \begin{verbatim}\begin{figure}
my figure
\caption{my caption}
\label{my label}
\end{figure}\end{verbatim} creates a floating environment for figures. The figure can be an outside image, using \verb|\includegraphics| (see below), but also a graphics created thanks to Tikz,
\item \verb|\includegraphics{path/to/image} |includes an outside picture (in png, jpg, jpeg or pdf format), needs to include the graphicx package,
\item \verb|\label{key}| and \verb|\ref{key}| is used to reference another part of the document. To crate a reference, put a \verb|\label{key}| wherever you want (inside text, figure…) and to make a reference, put a \verb|\ref{key}| with the corresponding key. As a result, you will see the corresponding numbering appears in the output.
\item \verb|\colortext{color}{text}| needs xcolor package and allows to color text (basic colors are white, black, red, green, blue, cyan, magenta and yellow.
\end{itemize}
\section{Basics of Maths}
To introduce inline maths in LaTeX, we surround the maths with \verb|$|. For instance, \verb|$\dfrac{a}{2}$| will display $\dfrac{a}{2}$. It is also possible to write maths in a new line surrounding maths by \verb|$|.
For instance \verb|$a=5$| will display $a=5$ on a new line. However, by using the amsmath package, it is also possible to enumerate equations, by using the environment equation. This environment will do the same as \verb|$|,
but will also numerate the equations, which is a good practice. The tags will totally replace the dollar signs, which are not needed at all.Some commands are particular to the math mode, and allow writing equations. Basic math commands include:
\begin{itemize}
\item \verb|\times|: $\times$
\item \verb|\cdot|: $\cdot$
\item \verb|^{text}| will put the text as superscript: $^{text}$
\item \verb|_{text}| will put the text as subscript: $_{text}$
\item it is possible to write the sum symbol with \verb|\sum|, and the product one with \verb|\prod|: $\sum\prod$
\end{itemize}
If I give here the very basics of the math commands and environment are here, there is much more: Greek letters, logic symbols… There is much more to explore, and a specific Stackexchange forum is dedicated to Latex.
Extensions and librariesOK. Now, let talk about something very important: libraries. LaTeX is designed to work with libraries, and this is one of its main advatages. Drawing, defining theorems, or even write runic symbols. There are libraries for all these purcposes. Let first talk about drawing
Tikz and PgfTikz and Pgf, as Beamer (see below) have been created by the same contributor, Till Tantau.
Tikz is the most used LaTeX library for drawing. It is used to create many figures, and create them as vectorial representation. Vectorial representation represents image as lines and shapes, not as pixels, which means that images cannot be pixelled, and quality is infinite. Tikz allow to create anything that can be represented by shapes, and is based on a lower level library, Pgf. It is for instance possible to create curves, graphs and diagrams.
Tikz is quite a high level library. It is por instance possible to manipulate objects as nodes (for graphs), edges, arrows, curved lines, and shapes as circles, ellipses and diamonds. Tikz uses its own libraries, to avoid compiling too much code (as all code loaded in a LaTeX document is compiled, even if it is not used). It is then necessary to indicate Tikz what library you want to use if you want to use advanced features. You can find an example of a Tikz figure in the section example below.
As I mentionned, it is possible to create any kind of figures in Tikz, and Tikz is based on Pgf. However, it is also possible to use other libraries based on Pgf. It can be useful when Tikz seems too low level, for instance if you want to create charts. One of the most efficient libraries in this case is pgfplots. This library enables the creation of various charts, lines as much as bar, combo areas etc. PgfPlots is even able to crate 3D charts. Once again, you can have a look on several figures created by PgfPlots in the example section, below.
Before looking at other libraries, I let you several documents and pages. First, the official manual of pgf and Tikz, written by Till Tantau himself. Then, and this is a present for my French fellows, a very, very good manual for Tikz, Tikz pour l’impatient. Then, for PgfPlots, the official manual of this library,. And to conclude, a very good website with wonderful examples of Tikz figures.
Maths and theoremsNow, let assume you want to write maths and redact theorems. A very good package for that is amsthm. This package enables writing personalized theorems, definitions and proofs. As usual, an example is given below, in the example section. Another package, which enables writing some complex math is amsmath. This package makes it possible to write sets as naturals ($\mathbb{N}$), real numbers ($\mathbb{R}$), calligraphic math letters ($\mathcal{A}, \mathcal{B}$ for instance).
As I told, this article is only aimed at introducing the basics of LaTeX, and there much more to see and to Explore. From time to time, I’ll give some examples and additionnal informations on features I have found, but the best way to begin is to download it, either the TeXLive or MikTeX distribution. TeXLive comes with an editor, TeXMaker, but for Linux users, I would recommend Kile. Finally, for French learners, the Openclassroom tutorial is quite good and takers it all from the beginning.
\end{document}
I will make some posts later on LaTeX drawing and presenting tools. See you then!