The basics of LaTeX

Apr 25, 2016 - ment, Word did not exist at all, and it was difficult to create document with complex ... Then, we define some constants for the file: the authors, the title and the .... begin is to download it, either the TeXLive or MikTeX distribution.
145KB taille 3 téléchargements 323 vues
The basics of LaTeX C´edric Buron April 25, 2016

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!

1

Some historic elements

LATEXhas been created in 1983 by Leslie Lamport (a famous American researcher), but actually relies on TEXa language created by Donald Knuth (another very famous American researcher). The goal of LATEXis 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 LATEXis 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.

2

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 1

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.

3

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.

3.1

Basics

The general structure of a LaTeX document is as follows: \ documentclass [ option ] { c l a s s } \ usepackage [ utf8 ] { inputenc } \ u s e p a c k a g e [ OT1 ] { f o n t e n c } \ 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.

2

• 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.

3.2

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

3

• \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.

4

Basics of Maths

To introduce inline maths in LaTeX, we surround the maths with $. For a instance, $\dfrac{a}{2}$ will display . It is also possible to write maths 2 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: 4

• \times: × • \cdot: · • ^{text} will put the text as superscript: • _{text} will put the text as subscript:

text

text

• it is possible P to Q 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 5

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 (N), real numbers (R), calligraphic math letters (A, 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.

6