In order to create a table, it is necessary to start the table's enviroment using the command \begin{table}.
Although, this is not enough to begin the table. In order to set the table: use the tabular enviroment, which generates the table.
The general command is \begin{tabular}[pos]{spec}.
The pos stays for the position of the table in the page. This is explained in the subsection Positioning: Position of the table in the page.
Instead of spec, it is necessary to insert commands, which determine the number of columns, their width and their alignment. The tables in LaTeX are
deteminated by the number of spec attributes.
l - the content of the table will be aligned to the left side
c - the content of the table will be aligned to the center
r - the content of the table will be aligned to the rigth side
Code\documentclass{article}
\begin{document}
\begin{table}
\begin{tabular}{lcr}
table & long table & not so long table \\
1 & 123456789000 & 3.14 \\
\end{tabular}
\end{table}
\end{document}
In the example above, the number of columns is 3: the content of first is aligned to the left, the second is aligned to the center
and the third to the left.
The width of the cell depends on the width of the content in the cell, so the biggest content will set the width of entire column. Despite,
it is possible to set the fix size of the column and the row (see subsection Special columns and Spacing).
\\ - signature is used to separate rows.
& - signature is used to separate columns.
In all following examples in section tables, the codes like, \documentclass{article}, \begin{document}, \begin{table}, \end{table}, \end{document} will not be shown in the code examples
in order to dedicate more attention to the details and to reduce the length of the code on the webpage. Although, these elements are mandatory in LaTeX. The commands
will be shown only in case they are needed to clarify the examples.
CODE
\begin{table}
\begin{tabular}{rcr}
example & & example\\
long example & not so long example & example\\
\end{tabular}
\caption{Example of the table with caption and one single empty column}
\end{table}
In this example, it is shown the \caption{text} command, which is used to write a small description of the table. In the
example, the caption is aligned to the left. For futher details, see Positioning: Alignment.
Also, there is an empty cell. In order to create it, the content of that cell must be empty, in other words place two & symbols without putting anything inside
e.g. example & & example.
Adding lines
In this subsection, is shown how to add lines, both vertical and horizontal. In order to add
vertical line, the symbol | must be added in the tabular enviroment, e.g. \begin{tabular}{|r|c|r}. If the vertical line must be on the right side of the column,
so the | symbol is put on the right, vice versa for the left. Note, that the vertical line is propagated all over the table.
In order to get a table with vertical lines and one cell without vertical lines, see Cell spanning: Multicolumns.
While, to get a horizontal line the procedure is different: the command \hline must be added after or before every row. Also, this command
designes a line all over the width of the table.
Using \cline{i-j}, it is possible to draw a horizontal line only for certain columns, e.g. only from column 2 to column 3.
CODE \begin{tabular}{|r|c|r}\hline
example & & examplemd\\ \hline
long example & not so long example & example\\ /cline{2-3}
\end{tabular}
In this example were added 3 vertical lines and 2 horizontal. Also, the \cline command is shown.
The \cline says to draw a horizontal line from cell 2 to cell 3.
Special columns
In this section, new types of columns are introduced.
p{width} - creates a column of maximum width, with the content of all other cells aligned vertically to the top,
while horizontally the content is aligned to the left
m{width} - creates a column of maximum width, with the content of all other cells aligned vertically at the middle,
while horizontally the content is aligned to the left. Note: use the \usepackage{array}
b{width} - creates a column of maximum width, with the content of all other cells aligned vertically at the bottom,
while hotizontally the content is aligned to the left. Note: use the \usepackage{array}
CODE \begin{tabular}{|r|p{2cm}|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\\hline
\end{tabular}
\begin{tabular}{|r|m{2cm}|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\\hline
\end{tabular}
\begin{tabular}{|r|b{2cm}|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\\hline
\end{tabular}
In the first table the p is used. In fact, all the content of other cells is aligned to the top. Command m
alignes the content to the middle(second table) and b to the bottom(third table).
Spacing
This subsection speaks about the space between columns and rows.
First, it is better to introduce the command \setlength{\tabcolsep}{width}, which modifies the
space between columns. The default width between columns is 6pt.
CODE \setlength{\tabcolsep}{1pt}
\begin{table}
\begin{tabular}{|r|m{2cm}|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\\hline
example& table & tables\\
\end{tabular}
\end{table}
\setlength{\tabcolsep}{7pt}
\begin{table}
\begin{tabular}{|r|m{2cm}|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\\hline
example& table & tables\\
\end{tabular}
\end{table}
\setlength{\tabcolsep}{25pt}
\begin{table}
\begin{tabular}{|r|m{2cm}|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\\hline
example& table & tables\\
\end{tabular}
\end{table}
The first table has 1pt of the width between columns, in fact it is very tight. The second one has almost normal width and the third one
has very large width (25pt).
Meanwhile, exists different ways of spacing between rows. One of the method is to redefine the space between rows (\arraystretch) by using \renewcommand{\arraystretch}{height} command.
The default spacing is 1.0.
CODE \begin{table}
\begin{tabular}{|r|c|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\\hline
\end{tabular}
\end{table}
\begin{table}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{|r|c|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\\hline
\end{tabular}
\end{table}
\begin{table}
\renewcommand{\arraystretch}{6}
\begin{tabular}{|r|c|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\\hline
\end{tabular}
\end{table}
The first table is the default one. The second one has a little spacing between the rows. While the last one uses large spacing. As you can see,
the more space is added, the more the misalignment is visible. In order to fix this, go to the subsection Cell spanning: Empty column.
Another way to add space between the rows, is to use \noalign{\smallskip} after or before the \hline or \cline
commands. The new command is almost useless, except in the examples below. Instead, insering [height] after the \\, is more practical.
CODE \begin{table}
\begin{tabular}{|r|c|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\ \hline
example& table & tables\\
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{|r|c|r|}\hline
example & & example for cell\\ \hline \noalign{\bigskip} \hline
long example & not so long example not very long& example\\ \noalign{\bigskip}
example& table & tables\\
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{rcr}\hline
example & & example for cell\\ \hline \noalign{\bigskip} \hline
long example & not so long example not very long& example\\ \noalign{\smallskip}
example& table & tables\\
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{|rcr|}\hline
example & & example for cell\\ [0.5cm] \hline
long example & not so long example not very long& example\\ [1.5cm] \hline
example& table & tables\\
\end{tabular}
\end{table}
The first table is default. In the second one the command \noalign{\bigskip} is used. As you can see,
it creates the space between rows but also between the lines, so it is more suitable to use double/hline or avoid
the vertical lines, like shown in the third table.
The fourth table uses [height] to create space between the rows. This command is more practical and it does
not influence the vertical lines.
Borders
In order to create beautiful border, LaTeX needs \usepackage{array, booktabs, arydshln, xcolor}.
CODE \usepackage{array, booktabs, arydshln, xcolor}
\newcommand\VRule[1][\arrayrulewidth]{\vrule width #1}
\begin{tabular}{!{\VRule[2pt]}r!{\VRule}c!{\color{green}\VRule[3pt]}c!{\VRule}} \hline
Column 1 & Column 2 & column3 \\ \specialrule{2pt}{0pt}{0pt}
second row & & \\ \hdashline
third row & & \\ \specialrule{4pt}{0pt}{0pt}
example & 1234 & super example\\ \specialrule{1pt}{3pt}{0pt}
\end{tabular}
Firstly, it is necessary to add 4 packages mentioned above and the new defined command: VRule.
Inside the tabular enviroment, the alignments c, l and r must be accompanied by ! symbol. The new command VRule[width]
is used to generate the vertical lines of certain width. Also, it is possible to change the color by adding \color{color} before the VRule[width] command.
The \hdashline command adds the dashed horizontal line.
At the end the \specialrule{pt}{pt}{pt} adds editable horizontal line: the first input is the width of the line, the second one is the vertical spacing and the last one is horizontal one.
Size of the table
In this last subsection is explained how to manage the size of the table. The most common way is to use package \usepackage{graphicx} and the command
\resizebox{width}{height}{object}, where the object, in case of the table, must be all the entire table enviroment.
CODE \usepackage{graphicx}
\begin{document}
\begin{table}
\resizebox{1.5\textwidth}{!}{\begin{minipage}{\textwidth}
\begin{tabular}{|r|c|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\ \hline
example& table & tables\\
\end{tabular}
\end{minipage}}
\end{table}
\resizebox{12cm}{!}{\begin{minipage}{\textwidth}
\begin{tabular}{|r|c|r|}\hline
example & & example for cell\\ \hline
long example & not so long example not very long& example\\ \hline
example& table & tables\\
\end{tabular}
\end{minipage}}
\end{table}
In the first table, the percentage of the width of the text area is imposed. While in the second one, a fix value in cm is used.