0% found this document useful (0 votes)
14 views8 pages

Unit 2 Module 4

This document provides a comprehensive guide on creating and formatting tables and images in LaTeX, detailing the syntax and commands used for various table structures, including captions, labels, and references. It also explains how to include images, adjust their size, and rotate them, along with best practices for organizing image files. Key commands and examples are provided to illustrate the functionality and usage of these features.

Uploaded by

raasikhashaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Unit 2 Module 4

This document provides a comprehensive guide on creating and formatting tables and images in LaTeX, detailing the syntax and commands used for various table structures, including captions, labels, and references. It also explains how to include images, adjust their size, and rotate them, along with best practices for organizing image files. Key commands and examples are provided to illustrate the functionality and usage of these features.

Uploaded by

raasikhashaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit 2 Module 4

Tables
Tables are created using the “table” environment given below:
\begin{table}[cols]
table
\end{table}
In the above syntax, table stands for the contents of the ‘tabular’ environment together with a possible \caption
command. The argument where specifies the allowed locations for the table. For example, when \begin{table}[t]
is typed, it means that the table will appear on the top of the page.
Eaxmple:
\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{center}
Output:

It was already said that the tabular environment is used to type tables. To be more clear about how it works
below is a description of each command.
{ |c|c|c| }: This declares that three columns, separated by a vertical line, are going to be used in the table. Each c
means that the contents of the column will be centred, you can also use r to align the text to the right and l for
left alignment.
\hline: This will insert a horizontal line on top of the table and at the bottom too. There is no restriction on the
number of times you can use \hline.
cell1 & cell2 & cell3 \\: Each & is a cell separator and the double-backslash \\ sets the end of this row.
\begin{center}
\begin{tabular}{||c c c c||}
\hline
Col1 & Col2 & Col2 & Col3 \\ [0.5ex]
\hline\hline
1 & 6 & 87837 & 787 \\
\hline
2 & 7 & 78 & 5415 \\
\hline
3 & 545 & 778 & 7507 \\
\hline
4 & 545 & 18744 & 7560 \\
\hline
\end{tabular}
\end{center}
Output:
This example shows double vertical and horizontal lines, when properly used help to keep the
information within the table well organized.
Tables with fixed length:
When formatting a table you might require a fixed length either for each column or for the entire table. In the
example below a fixed column width is established.
\begin{center}
\begin{tabular}{ | m{5em} | m{1cm}| m{1cm} | }
\hline
cell1 dummy text dummy text dummy text& cell2 & cell3 \\
\hline
cell1 dummy text dummy text dummy text & cell5 & cell6 \\
\hline
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{center}
Output:

Combining rows and columns:


Rows and columns can be combined in a bigger cell. The example below is an example of the \multicolumn
command to combine columns.
\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}| }
\hline
\multicolumn{4}{|c|}{Country List} \\
\hline
Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 Code&ISO
numeric Code\\
\hline
Afghanistan & AF &AFG& 004\\
Aland Islands& AX & ALA &248\\
Albania &AL & ALB& 008\\
Algeria &DZ & DZA& 012\\
\hline
\end{tabular}
Output:

Let's see each part of the command \multicolumn{4}{|c|}{Country List} \\: {4} the number of columns to be
combined, 4 in this case. {|c|} Delimiters and alignment of the resulting cell, in this case the text will be centered
and a vertical line will be drawn at each side of the cell. {Country List}
Text to be displayed inside the cell.
To combine rows the package multirow must be imported with \usepackage{multirow} in your preamble, then
you can use the \multirow command in your document.
\begin{center}
\begin{tabular}{ |c|c|c|c| }
\hline
col1 & col2 & col3 \\
\hline
\multirow{3}{4em}{Multiple row} & cell2 & cell3 \\
& cell5 & cell6 \\
& cell8 & cell9 \\
\hline
\end{tabular}
\end{center}
Output:

Captions, labels and references:


Tables can be captioned, labelled and referenced by means of the table environment.
The table \ref{table:1} is an example of referenced \LaTeX elements.
\begin{table}
\centering
\begin{tabular}{||c c c c||}
\hline
Col1 & Col2 & Col2 & Col3 \\ [0.5ex]
\hline\hline
1 & 6 & 87837 & 787 \\
2 & 7 & 78 & 5415 \\
3 & 545 & 778 & 7507 \\
4 & 545 & 18744 & 7560 \\
5 & 88 & 788 & 6344 \\ [1ex]
\hline
\end{tabular}
\caption{Table to test captions and labels}
\label{table:1}
\end{table}
Output:

There are three important commands in the example:


\caption{Table to test captions and labels} As you may expect this command sets the caption for the table, if you
create a list of tables this caption will be used there. You can place it above or below the table.
\label{table:1} if you need to refer the table within your document, set a label with this command. The label will
number the table, and combined with the next command will allow you to reference it.
\ref{table:1} this code will be substituted by the number corresponding to the referenced table.
Note: The document may need to be compiled more than once for the labels to work.
List of tables:
To create a list of tables is straightforward.
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\listoftables
...
\end{document
Output:

Quick description of parameters in the tabular environment:


\begin{tabular}[pos]{cols}
table content
\end{tabular}
Where options can be:
pos : Vertical position. It can assume the following values:
t->the line at the top is aligned with the text baseline
b->the line at the bottom is aligned with the text baseline
c or none->the table is centred to the text baseline
cols : Defines the alignment and the borders of each column. It can have the following values:
l->left-justified column
c->centred column
r->right-justified column
p{'width'}->paragraph column with text vertically aligned at the top
m{'width'}->paragraph column with text vertically aligned in the middle (requires array package)
b{'width'}->paragraph column with text vertically aligned at the bottom (requires array package)
|->vertical line
||->double vertical line
*{num}{form}->the format form is repeated num times; for example *{3}{|l}| is equal to |l|l|l|
To separate between cells and introducing new lines use the following commands:
&->column separator
\\->start new row (additional space may be specified after \\ using square brackets, such as \\[6pt])
\hline->horizontal line between rows

Inserting Images
Images are essential elements in most of the scientific documents. LATEX provides several options to handle
images and make them look exactly what you need. In this article is explained how to include images in the most
common formats, how to shrink, enlarge and rotate them, and how to reference them within your document.
\documentclass{article}
\usepackage{graphicx}
\graphicspath{ {./images/} }
\begin{document}
The universe is immense and it seems to be homogeneous,
in a large scale, everywhere we look at.
\includegraphics{universe}
There's a picture of a galaxy above
\end{document}
Output:
Latex can not manage images by itself, so we need to use the graphicx package. To use it, we include the
following line in the preamble: \usepackage{graphicx}
The command \graphicspath{ {./images/} } tells LATEX that the images are kept in a folder named images under
the directory of the main document.
The \includegraphics{universe} command is the one that actually included the image in the document. Here
universe is the name of the file containing the image without the extension, then universe.PNG becomes
universe. The file name of the image should not contain white spaces nor multiple dots.
Note: The file extension is allowed to be included, but it's a good idea to omit it. If the file extension is omitted it
will prompt LaTeX to search for all the supported formats.
The folder path to images:
When working on a document which includes several images it's possible to keep those images in one or more
separated folders so that your project is more organized.
The command \graphicspath{ {images/} } tells LATEX to look in the images folder. The path is relative to the
current working directory - so, the compiler will look for the file in the same folder as the code where the image is
included. The path to the folder is relative by default, if there is no initial directory specified, for instance,
%Path relative to the .tex file containing the \includegraphics command
\graphicspath{ {images/} }
This is a typically straightforward way to reach the graphics folder within a file tree, but can leads to
complications when .tex files within folders are included in the mail .tex file. Then, the compiler may end up
looking for the images folder in the wrong place. Thus, it is best practice to specify the graphics path to be relative
to the main .tex file, denoting the main .tex file directory as ./ , for instance as in the introduction.
%Path relative to the main .tex file
\graphicspath{ {./images/} }
The path can also be absolute, if the exact location of the file on your system is specified. For example:
%Path in Windows format:
\graphicspath{ {c:/user/images/} }
%Path in Unix-like (Linux, Mac OS) format
\graphicspath{ {/home/user/images/} }
Notice that this command requires a trailing slash / and that the path is in between double braces.
You can also set multiple paths if the images are saved in more than one folder. For instance, if there are two
folders named images1 and images2, use the command.
\graphicspath{ {./images1/}{./images2/} }
If no path is set LATEX will look for pictures in the folder where the .tex file the image is included in is saved.
Changing the image size and rotating the picture:
If we want to further specify how LATEX should include our image in the document (length, height, etc), we can
pass those settings in the following format:
\begin{document}
\includegraphics[scale=1.5]{lion-logo}
The command \includegraphics[scale=1.5]{lion-logo} will include the image lion-logo in the document, the extra
parameter scale=1.5 will do exactly that, scale the image 1.5 of its real size. You can also scale the image to a
some specific width and height.
\begin{document}
\includegraphics[width=3cm, height=4cm]{lion-logo}

As you probably have guessed, the parameters inside the brackets [width=3cm, height=4cm] define the width and
the height of the picture. You can use different units for these parameters. If only the width parameter is passed,
the height will be scaled to keep the aspect ratio.
The length units can also be relative to some elements in document. If you want, for instance, make a picture the
same width as the text:
\begin{document}
The universe is immense and it seems to be homogeneous,
in a large scale, everywhere we look at.
\includegraphics[width=\textwidth]{universe}

There is another common option when including a picture within your document, to rotate it. This can easily
accomplished in LATEX:
\begin{document}
\includegraphics[scale=1.2, angle=45]{lion-logo}

The parameter angle=45 rotates the picture 45 degrees counter-clockwise. To rotate the picture clockwise use a
negative number.
Captioning, labelling and referencing
Captioning images to add a brief description and labelling them for further reference are two important tools
when working on a lengthy text.
Captions
Let's start with a caption example:
\begin{figure}[h]
\caption{Example of a parametric plot ($\sin (x), \cos(x), x$)}
\centering
\includegraphics[width=0.5\textwidth]{spiral}
\end{figure}

It's really easy, just add the \caption{Some caption} and inside the braces write the text to be shown. The
placement of the caption depends on where you place the command; if it'a above the includegraphics then the
caption will be on top of it, if it's below then the caption will also be set below the figure.
Captions can also be placed right after the figures. The sidecap package uses similar code to the one in the
previous example to accomplish this.
\documentclass{article}
\usepackage[rightcaption]{sidecap}
\usepackage{graphicx} %package to manage images
\graphicspath{ {images/} }
\begin{SCfigure}[0.5][h]
\caption{Using again the picture of the universe.
This caption will be on the right}
\includegraphics[width=0.6\textwidth]{universe}
\end{SCfigure}

There are two new commands


\usepackage[rightcaption]{sidecap}
As you may expect this line will import a package named sidecap, but there is an additional parameter:
rightcaption. This parameter establishes the placement of the caption at the right of the picture, you can also use
leftcaption. In book-like documents outercaption and innercaption are also available. The names of these are self-
descriptive.
\begin{SCfigure}[0.5][h] \end{SCfigure}
Defines an environment similar to figure. The first parameter is the width of the caption relative to the size of the
image, as declared in includegraphics. The second parameter h works exactly as in the figure environment. See
the placement section for more information.
Labels and cross-references:
Figures, just as many other elements in a LATEX document (equations, tables, plots, etc) can be referenced within
the text. This is very easy, just add a label to the figure or SCfigure environment, and then later use that label to
refer the picture.
\begin{figure}[h]
\centering
\includegraphics[width=0.25\textwidth]{mesh}
\caption{a nice plot}
\label{fig:mesh1}
\end{figure}
As you can see in the figure \ref{fig:mesh1}, the
function grows near 0. Also, in the page \pageref{fig:mesh1}
is the same example.

There are three commands that generate cross-references in this example.


\label{fig:mesh1}
This will set a label for this figure. Since labels can be used in several types of elements within the document, it's a
good practice to use a prefix, such as fig: in the example.
\ref{fig:mesh1}
This command will insert the number assigned to the figure. It's automatically generated and will be updated if
insert some other figure before the referenced one.
\pageref{fig:mesh1}
This prints out the page number where the referenced image appears.
The \caption is mandatory to reference a figure.
Another great characteristic in a LATEX document is the ability to automatically generate a list of figures. This is
straightforward.
\listoffigures

This command only works on captioned figures, since it uses the caption in the table. The example above lists the
images in this article.

You might also like