What is LaTeX?
Why to Learn LaTeX?
LaTeX is used for writing scientific documents, and many other forms of publication. It helps in drafting documents that are legible, beautifully formatted without the user making an effort.
It helps in typesetting documents, putting the formulae, creating a table of contents, references, etc. and having a uniform look throughout. Also, particular style of one document can be used for other documents too. This acts as a big help while creating journals.
Writing your First Code in LaTeX: Best Latex Tutorial for Beginners
Create a new LaTeX project. Do this by creating a new .tex file.
Sample document:
1 2 3 4 | \documentclass{article} \begin{document} Let’s begin working in LaTeX. \end{document} |
OUTPUT:
Here, you can see that LaTex has formatted the first line. Now, let’s understand this simple piece of code.
- Line 1 declares the type of document, known as the class. The class controls the overall appearance of the document. Different types of classes will be required for different types of documents. For example, a research paper will have a different class than a lesson of the book. These may require the class as book or report. Here, the class for this piece of code was article.
- Following this, we write the content of the document. It starts with \begin{document} and ends with \end{document} tags. The content within the two tags is called the body of the document.
- Everytime we change the body, the document has to be compiled or you can set the mode of AutoCompile to be On.
- In case you are using a basic text editor like gedit, emacs, vim, sublime, notepad etc., there will be manual compilation. To compile, use the following command in the terminal or command line: pdflatex < your document>.
If you are using a dedicated LaTeX editor such as TeXmaker or TeXworks, click on the Recompile button.
Preamble of a Document : Best Latex Tutorial Point
In the example stated above, we entered the text entered after \begin{document} command. Everything in the .tex file till this point is called the preamble.
The preamble is a place where you define the language you will use to write, the packages that you will use and several other elements.
Preamble of a simple document would look like the following:
1 2 | \documentclass[16pt, letterpaper]{article} \usepackage[utf7]{inputenc} |
\documentclass[16pt, a4paper]{article}
This line defines the type of document. We can include some additional parameters, separated by comma, in the square bracket, which can be passed to the command.
In the example above, we have specified the font size as one of the parameter (16pt) and the size of the paper as A4paper.
This is just an example and other parameters like some other font size like 11pt, 14pt etc can be used. But if we don’t specify anything, the default size is 10pt. Similarly, for the paper sizeOf course other font sizes (9pt, 11pt, 12pt) can be used, but if none is specified, the default size is 10pt. Similarly, for the paper size other value can be legalpaper or letterpaper.
\usepackage[utf8]{inputenc}
This is the encoding for the document. We can change this to another encoding but utf-8 is recommended. If you are not sure about it or require some particular encoding , add the above line to the preamble.
Adding a Title, Author and Date
For adding the title, author and date to the document, add the following lines to the preamble, not to the main body of the document.
For title: \title{Chapter one}
For author: \author{Tom Cruise}
The underlined parts above can be changed according to the user for the title and name of the author respectively.
We can also add the following command:
\thanks{sponsored by the Resurchify team}
This line 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. This is helpful if you want to express your gratitude.
For date: \date{May 2019}
The date can be entered manually or for automatic updation of the present date use the command: \today. The date then, updates automatically, as you compile.
All this put together in the preamble will look like:
1 2 3 4 5 6 | \documentclass[16pt, a4paper, twoside]{article} \usepackage[utf8]{inputenc} \title{Chapter one} \author{ Tom Cruise \thanks{sponsored by the Resurchify team}} \date{May 2019} |
To get the title, name of author and date in the document, use \maketitle command. This has to be included in the body of the document where the title has to be printed.
1 2 3 4 5 6 | \begin{document} \maketitle This is our first document and we have the title, name of the author and the date for this \LATEX{} document. \end{document} |
Adding comments
Comments are often added in the code for later references and are not to be printed. They are helpful when one wants to add notes, or comments few sections.
To write a comment in LaTeX, start the statement with % symbol. For example,
1 2 3 4 5 6 7 | \begin{document} \maketitle This is our first document and we have the title, name of the author and the date for this \LATEX{} document. % This is a comment. It will not get printed. \end{document} |
Bold, italics and underlining
Let’s learn how to format the text by applying simple commands like bold, italic and underline.
Bold: Text can be made bold by writing \textbf{...} command.
Italics: Text can be made italic by writing \textit{...} command.
Underline: Text can be made underline by writing \underline{...} command.
An example for the above commands is shown below:
1 2 | \textit{\textbf{Life}} is what happens when you are \underline{busy} making other \textit{plans} |
Another formatting command is \emph{...} command. Its activity depends on the context. For normal text, it makes it italic. And the process is reversed if it is applied on italicized text. For example,
1 2 3 4 5 | 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.} |
Adding images: Insert Image Latex Overleaf From Folder
To include the images, following is the command.
1 2 3 4 5 6 7 8 9 10 11 | \documentclass{article} \usepackage{graphicx} \graphicspath{ {pictures/} } \begin{document} Sun rises in the east and sets in the west. \includegraphics{sun} There's a picture of a sunrise above. \end{document} |
To manage the images in LaTeX, we have to make use of a package. Here, to include am image, you should use graphicx package. Through this package, we have included new commands: \includegraphics{...} and \graphicspath{...}.
To make use of the graphicx package, include the following line in your preamble: \usepackage{graphicx}
The command \graphicspath{ {pictures/} } helps in giving the location of the folder called pictures under the current directory.
The \includegraphics{sun} command denotes the actual image which has to be included in the document. Here, sun is the name of the image without extension. There should not be any space within the filename of the image.
Visit Images Tutorial to learn more about it.Captions, labels and references
For adding captions, labels and references to the images, we have to make use of figure environment. An example is shown below.
1 2 3 4 5 6 | \begin{figure}[h] \centering \includegraphics[width=0.45\textwidth]{pictures} \caption{Sunrise} \label{fig:Sun} \end{figure} |
Following is an explanation of the above code:
-
\caption{Sunrise}
: This command helps in giving the caption to the image. It can be placed above or below the image. -
\label{fig:Sun}
: This helps in setting a label to the image, so that it can be referred within the document. The label will number the image, and combined with the next command will allow you to reference it. -
ref{fig:Sun}
: This code will be substituted by the number corresponding to the referenced figure.
Please note that if captions and references are being used, compile the document twice so that the references work.
Creating lists in LaTeX
Lists help in putting the text in a sequence. Lists in LaTeX can be created by using various list environments.
There are two types of lists:
- Unordered
- Ordered
Each list has a different environment.
Unordered lists
Unordered lists are created with the help of itemize environment. Each line item of the list must be preceded by the control sequence \item. For example,
1 2 3 4 | \begin{itemize} \item List Item 1 \item List Item 2 \end{itemize} |
You can add as many entries in this list. By default, there is a black coloured dot as bullet.
Ordered lists
Ordered list has a similar kind of syntax but with a different environment. Ordered lists are created with the enumerate environment. For example,
1 2 3 4 | \begin{enumerate} \item List Item 1 \item List Item 2 \end{enumerate} |
Similar to unordered lists, each entry or line item has to be preceded by \item.
For Ordered lists, the sequential number starts from one.
Adding math to LaTeX
One of the greatest advantages of LATEX is the clarity with which mathematical expressions can be printed. LATEX allows two writing modes for mathematical expressions: the inline mode and the display mode. Inline mode is used to write formulas that are part of a text. Display mode is used to write expressions that are not part of a text or paragraph, and are therefore put on separate lines. Let's see an example of the inline mode:
1 2 3 | Below is an example of inline mode. One of the famous physics equation:$E=mc^2$ |
To put the equations in inline mode, make use of one of these delimiters: \( ... \), $ ... $
or \begin{math} ... \end{math}. Any one can be used based on the choice of the user.
1 2 3 4 5 6 7 8 | One of the famous physics equation: \[E=mc^2\] If c=1, the equation becomes: \begin{equation} E=m \end{equation} |
To print your equations in display mode use one of these delimiters: \[ ... \], \begin{displaymath} ... \end{displaymath} or \begin{equation} ... \end{equation}
.
$$ ... $$ is
usually recommended not to use as it gives inconsistent spacing and may not work well with some math packages.
Important Note: equation* environment is provided by an external package.
This is just a beginning of mathematical expressions. There is lot to explore in this.
Basic Formatting
Under this section, we will learn how to write abstract and divide the document into different section like chapters, sections and paragraphs.
Abstracts
For research papers or scientific documents, writing an abstract is very important. In LaTeX to implement this, we have the abstract environment. It helps in placing the text in a different format at the top of the document.
1 2 3 4 5 | \begin{document} \begin{abstract} This is a brief overview of the paper. \end{abstract} \end{document} |
Now, we have written the abstract of the paper. We start with writing a new paragraph now.
Paragraphs and newlines
1 2 3 4 5 6 | \begin{document} \begin{abstract} This is a brief overview of the paper. \end{abstract} To begin with, this is our first paragraph. \end{document} |
LaTeX will indent the paragraph automatically. To start a new paragraph, Enter key has to be hit twice for inserting a double blank line.
If you don’t want to start a new paragraph but a new line, insert a break line point by typing \\ (a double backslash) or the \newline command.
Chapters and Sections
1 2 3 4 5 6 7 8 9 10 11 12 13 | \chapter{Chapter One} \section{Section One} This is the first section. \section{Second Section} This is the second section. \subsection{First Subsection} This is the first sub-section of second section. \section*{Unnumbered Section} This will be an unnumbered section. |
For a new section, we use the command \section{} . Inside the braces, we give the name of the section. Numbering of the section takes place automatically. And can be stopped by using a * in the section command as \section*{} , as used in the example above.
To add a subsection, use \subsection{name of subsection}
Creating tables
Creating a simple table in LaTeX
Below is an example to insert values in a tabular format.
1 2 3 4 5 6 7 | \begin{center} \begin{tabular}{ c c c } value1 & value2 & value3 \\ value4 & value5 & value6 \\ value7 & value8 & value9 \end{tabular} \end{center} |
tabular environment is the default method to create tables. To define a table, we need to specify the number of columns. This will go as a parameter as in the above example as {c c c}.
{c c c} defines that there will be three columns and the alignment of these columns is centre. To align text to the left and right, use l and r respectively. To specify breaks while making entries in table, this symbol &
is used.
After completing entry of first line and moving to next line, use the new line command \\.
Adding borders in table
Horizontal and vertical lines can be added as borders to the table. Also, separator lines can be placed between columns.
1 2 3 4 5 6 7 8 9 | \begin{center} \begin{tabular}{ |c|c|c| } \hline value1 & value2 & value3 \\ value4 & value5 & value6 \\ value7 & value8 & value9 \\ \hline \end{tabular} \end{center} |
For horizonal line, command is \hline and for the vertical line, parameter is |.
- { |c|c|c| } : This declares that there are three columns, separated by a vertical line. The | symbol specifies the vertical line and that these columns have to be separated by a vertical line.
- \hline: This command inserts a horizontal line.
Below is an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | \begin{center} \begin{tabular}{||c c c c||} \hline Column1 & Column2 & Column3 & Column4 \\ [0.5ex] \hline\hline 10 & 6 & 37 & 87 \\ \hline 20 & 7 & 8 & 15 \\ \hline 30 & 54 & 78 & 75 \\ \hline 40 & 45 & 1844 & 560 \\ \hline 50 & 98 & 1788 & 634 \\ [1ex] \hline \end{tabular} \end{center} |
Captions, labels and references
To add captions, labels and reference in tables, we need to use the table environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Table \ref{table:data} is an example of referenced \LaTeX{} elements. \begin{table}[h!] \centering \begin{tabular}{||c c c c||} \hline Column1 & Column2 & Column3 & Column4 \\ [0.5ex] \hline\hline 10 & 6 & 37 & 87 \\ 20 & 7 & 8 & 15 \\ 30 & 54 & 78 & 75 \\ 40 & 45 & 1844 & 560 \\ 50 & 98 & 1788 & 634 \\ [1ex] \end{tabular} \caption{Table for testing captions and labels} \label{table:data} \end{table} |
Please note that if captions and references are being used, compile the document twice so that the references work.
Adding a Table of Contents
Please use the command \tableofcontents to create the table of contents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | \documentclass{article} \usepackage[utf8]{inputenc} \title{Computer Networks} \author{Frankfurt} \date{ } \begin{document} \maketitle \tableofcontents \section{Introduction} Greetings for the day! \addcontentsline{toc}{section}{Unnumbered Section} \section*{Unnumbered Section} Hi! How are you? \section{ Section Two} What is your plan for the day? \end{document} |
In the Table of Contents, section, subsection and chapter get automatically included. For making manual entries, use the command \addcontentsline.
Visit Table of Contents Tutorial to learn more about it.