The \multicolumn is an important option used in the tables. The general command is \multicolumn{num_of_colunm}{align}{content},
where the num_of_colunm is the number of columns over which the multicolumn will be placed, align is a input of tabular enviroment, e.g. p,c,l,r, |; and content
is the content of the cell.
\begin{tabular}{|l|r|c|}\hline
\multicolumn{3}{|p{2cm}}{Examples}\\\hline
1 & example & table\\\hline
2 & example1 & table\\\hline
3 & example22 & table\\\hline
\multicolumn{2}{|p{2cm}|}{multicolumn is used here} & table\\\hline
5 & example5 & table\\\hline
\multicolumn{3}{c}{ending}\\
\end{tabular}
In the first table, the \multicolumn is used, over the first 2 columns, with the center alignment. Also, the two | symbols
were added, in order to get the vertical lines in that cell. In the last row, the \multicolumn is used over one row, in order to cancel one vertical line in that cell.
The second table shows some examples of using \multicolumn command.
Multirows
The \multirow perform the same operation as \multicolumn but with the rows. The new command requries \usepackage{multirow}.
General command is \multirow{num_of_rows}{widht}{content}, where the num_of_rows is the number of rows, over which will overlay the \multirow.
The widht is the width of the row. Note: use * to mantain the same width as the other cells in the column.
CODE\usepackage{multirow}
\begin{document}
\begin{table}
\begin{tabular}{|l|c|c|r|}\hline
example & big example & 1234 & \multirow{2}{*}{multirow01}\\ \hline
example & big example & 123 & \\\hline
example & big example & 123455 & my table\\\hline
\multirow{3}{3cm}{multirow02 not very long text} & example & big example & 1234 \\\hline
test & example & big example & 1234\\\hline
111 & example & big example & 1234\\\hline
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{|l|c|c|r|}\hline
example & big example & 1234 & \multirow{2}{*}{multirow01} \\ \cline{1-3}
example & big example & 123 & \\\hline
example & big example & 123455 & my table\\\hline
\multirow{3}{3cm}{multirow02 not very long tex} & example & big example & 1234 \\\cline{2-4}
& example & big example & 1234\\\cline{2-4}
& example & big example & 1234\\\hline
\end{tabular}
\end{table}
The fisrt example shows a non appropriate use of \multirow. In the first two rows there is an \hline, which
draws the line over all the width of the table. In order to avoid the effect in the first lines of the first table, the \cline
is used, as shown in the second table.
In the last lines of the first table, the content is overflowed. So, the content of the following rows in selected column
must be empty. In the example above, the multirow is performed over 3 rows, so the two next lower rows of the same column must be empty, as shown
in the second table.
Empty column
The easiest way to create an empty column is to insert nothing inside the two & &. In this section, the new type of column is introduced: @{content}. This
symbol is used to insert a constant content in one column, putting a content inside the brackets. In order to get an emoty column using @{content}, leave the brackets empty.
If you are using LaTeX version before 4.15, in order to fix the error shown in the section Enviroment: spacing, it is necessary to create an
empty column: @{}m{width}@{}.
The m attribute will align the content of all cells vertically in the middle, add one empty column m{width} to the tabular enviroment.
CODE\begin{table}
\setlength{\tabcolsep}{20pt}
\renewcommand{\arraystretch}{4}
\centering{
\begin{tabular}{| l | r | c|}\hline
table & example & example \\ \hline
example& not long example & table \\ \hline
123456789 & table & empty \\ \hline
\end{tabular}}
\end{table}
\begin{table}[h]
\setlength{\tabcolsep}{20pt}
\renewcommand{\arraystretch}{4}
\centering{
\begin{tabular}{| l | r | c|@{}m{0cm}@{}}\hline
table & example & example &\\ \hline
example& not long example & table &\\ \hline
123456789 & table & empty &\\ \hline
\end{tabular}}
\end{table}
Both tables are equal, the unique difference is the empty column in the second one, which is used to put the content almost in the middle. The tabular enviroment
in the second table is \begin{tabular}{| l | r | c|@{}m{0cm}@{}}.
Also, it is necessary to add & after the last column containing content. The last empty cell must not contain anything.
After the latest update(4.15), the \usepackage[table]{xcolor} is needed. The better version of this table
is shown in the page Positioning: Position of the content in the cell.
Combining
In this section, an example of combining of \multirow and \multicolumn will be shown.
CODE\usepackage{multirow}
\begin{document}
\begin{table}
\begin{tabular}{|l|c|c|r|}\hline
example & big example & \multicolumn{2}{c} {\multirow{2}{*}{multirow01} }\\ \cline{1-2}
example & big example & & \\\hline
example & big example & 123455 & my table\\\hline
\multirow{3}{*}{multirow02 very large} & example & big example & 1234 \\\hline
& example & big example & 1234\\\hline
& example & big example & 1234\\\hline
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{|l|c|c|r|}\hline
example & big example & \multicolumn{2}{c|} {\multirow{2}{*}{multirow01} }\\ \cline{1-2}
example & big example & \multicolumn{1}{c}{}& \\\hline
example & big example & 123455 & my table\\\hline
\multirow{3}{*}{multirow02 very large} & example & big example & 1234 \\\hline
& example & big example & 1234\\\hline
& example & big example & 1234\\\hline
\end{tabular}
\end{table}
The goal is to put "multirow01" inside the last 2 cells of the first 2 rows. So, the command \multirow is put inside the \multicolumn.
It is better to use * for width of the \multirow. In order to repair the hole in the rigth corner, the specification {c|} is used for \multicolumn.
While, to cut the vertical line in the middle of the row, the \multicolumn{1}{c}{} is used.