If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

## 1. Introduction

In this tutorial, we’ll discuss how to draw horizontal and vertical tables in LaTex.

## 2. Basic LaTex Tables

In LaTex, we can use the environment to create a table:

\begin{tabular}{c c c}
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9 \\
\end{tabular}


In this example, we use to tell LaTeX that the table has three columns and the text inside each table cell is centered. We can replace with to align the text to the right or with for left alignment.

For the table content, we use to separate cells and double-backslash to end a table row.

We can also add horizontal and vertical border lines into a table:

\begin{tabular}{|c|c|c|}
\hline
cell1 & cell2 & cell3 \\
\hline
cell4 & cell5 & cell6 \\
\hline
cell7 & cell8 & cell9 \\
\hline
\end{tabular}


In the table description , we declare three columns and separate them by vertical lines. Also, between every two rows, we use to separate them.

## 3. LaTex Table Position and Caption

In LaTex, we use the environment to position the table inside it. For example, we can use to position the table exactly here, right after the text content:

\begin{table}[h]
\begin{tabular}{|c|c|c|}
\hline
cell1 & cell2 & cell3 \\
\hline
cell4 & cell5 & cell6 \\
\hline
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{table}


In this example, the environment is a container to include a floating table. The position description tells LaTex where to place the table. We can also use to place the table at the top of the page or to place the table at the bottom of the page.

Also, we can set a caption for the table inside the container:

\begin{table}[h]
\begin{table}[h]
\centering
\caption{LaTex Table Caption}
\begin{tabular}{|c|c|c|}
\hline
cell1 & cell2 & cell3 \\
\hline
cell4 & cell5 & cell6 \\
\hline
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{table}


In this example, we first use to center both the caption and the table content. Then, we use to make a caption for the table.

## 4. Booktabs Table

Professional tables usually use three horizontal lines, i.e., top line, middle line, and bottom line, to separate table content. The three lines are thicker than the other lines that appear in the table. We can use the booktabs package to provide the three lines:

\usepackage{booktabs}

\begin{table}[h]
\centering
\begin{tabular}{ccc}
\toprule
\textbf{Topic 1} & \textbf{Topic 2} &\textbf{Topic 3} \\
\midrule
cell1&cell2 & cell3\\
cell4&cell5 & cell6\\
cell7&cell8 & cell9\\
\bottomrule
\end{tabular}
\end{table}


In this example, we first add into our LaTex content. Then, we use , , and to represent the three lines.

Also, we can use to draw a line that only spans a few columns:

\usepackage{booktabs}

\begin{table}[h]
\centering
\begin{tabular}{cccccc}
\toprule
\multicolumn{1}{c}{} & \multicolumn{3}{c}{\textbf{Topic 2}} & \multicolumn{2}{c}{\textbf{Topic 3}} \\
\cmidrule(rl){2-4} \cmidrule(rl){5-6}
\textbf{Topic 1} & {A} & {B} & {C} & {D} & {E} \\
\midrule
row1 & A.1 & B.1 & C.1 & D.1 & E.1 \\
row2 & A.2 & B.2 & C.2 & D.2 & E.2 \\
row3 & A.3 & B.3 & C.3 & D.3 & E.3 \\
row4 & A.4 & B.4 & C.4 & D.4 & E.4 \\
row5 & A.5 & B.5 & C.5 & D.5 & E.5 \\
\bottomrule
\end{tabular}
\end{table}


In this example, we use to define the spanning columns of each text block. Then, we use to draw horizontal lines under each block.

## 5. Table Colors

We can add colors to LaTex tables with and packages. For example, we can use to set the background color of a row:

\usepackage{booktabs}
\usepackage{xcolor,colortbl}
\definecolor{lavender}{rgb}{0.9, 0.9, 0.98}

\begin{table}[h]
\centering
\begin{tabular}{cccccc}
\toprule
\multicolumn{1}{c}{} & \multicolumn{3}{c}{\textbf{Topic 2}} & \multicolumn{2}{c}{\textbf{Topic 3}} \\
\cmidrule(rl){2-4} \cmidrule(rl){5-6}
\textbf{Topic 1} & {A} & {B} & {C} & {D} & {E} \\
\midrule
\rowcolor{lavender}
row1 & A.1 & B.1 & C.1 & D.1 & E.1 \\
row2 & A.2 & B.2 & C.2 & D.2 & E.2 \\
\rowcolor{lavender}
row3 & A.3 & B.3 & C.3 & D.3 & E.3 \\
row4 & A.4 & B.4 & C.4 & D.4 & E.4 \\
\rowcolor{lavender}
row5 & A.5 & B.5 & C.5 & D.5 & E.5 \\
\bottomrule
\end{tabular}
\end{table}


In this example, we use to define a customized color. Then, we use to set the background color of a table row.
Similarly, we can use to set the background color of a column:

\begin{table}[h]
\centering
\begin{tabular}{>{\columncolor{lavender}}cccccc}
\toprule
\multicolumn{1}{c}{} & \multicolumn{3}{c}{\textbf{Topic 2}} & \multicolumn{2}{c}{\textbf{Topic 3}} \\
\cmidrule(rl){2-4} \cmidrule(rl){5-6}
\textbf{Topic 1} & {A} & {B} & {C} & {D} & {E} \\
\midrule
row1 & A.1 & B.1 & C.1 & D.1 & E.1 \\
row2 & A.2 & B.2 & C.2 & D.2 & E.2 \\
row3 & A.3 & B.3 & C.3 & D.3 & E.3 \\
row4 & A.4 & B.4 & C.4 & D.4 & E.4 \\
row5 & A.5 & B.5 & C.5 & D.5 & E.5 \\
\bottomrule
\end{tabular}
\end{table}


We can also use to set the background color of an individual cell and to set the text font color:

\begin{table}[h]
\centering
\begin{tabular}{cccccc}
\toprule
\multicolumn{1}{c}{} & \multicolumn{3}{c}{\textbf{Topic 2}} & \multicolumn{2}{c}{\textbf{Topic 3}} \\
\cmidrule(rl){2-4} \cmidrule(rl){5-6}
\textbf{Topic 1} & {A} & {B} & {C} & {D} & {E} \\
\midrule
row1 & A.1 & \cellcolor{red}{B.1} & C.1 & D.1 & E.1 \\
row2 & A.2 & B.2 & C.2 & D.2 & E.2 \\
row3 & A.3 & B.3 & \textcolor{blue}{C.3}& D.3 & E.3 \\
row4 & A.4 & B.4 & C.4 & D.4 & E.4 \\
row5 & A.5 & B.5 & C.5 & D.5 & E.5 \\
\bottomrule
\end{tabular}
\end{table}


## 6. Vertical LaTex Table

Sometimes we have a table with a large number of columns and a few rows. It’s hard to fit the table into the page width. In this case, we can draw the table vertically to fit the page.

To draw a table vertically, we can use the package. Firstly, we add into our LaTex content. Then, we can use the environment to render the table vertically:

\usepackage{lscape}

\begin{landscape}
\begin{table}[h]
\centering
\begin{tabular}{cccccc}
\toprule
\multicolumn{1}{c}{} & \multicolumn{3}{c}{\textbf{Topic 2}} & \multicolumn{2}{c}{\textbf{Topic 3}} \\
\cmidrule(rl){2-4} \cmidrule(rl){5-6}
\textbf{Topic 1} & {A} & {B} & {C} & {D} & {E} \\
\midrule
\rowcolor{lavender}
row1 & A.1 & B.1 & C.1 & D.1 & E.1 \\
row2 & A.2 & B.2 & C.2 & D.2 & E.2 \\
\rowcolor{lavender}
row3 & A.3 & B.3 & C.3 & D.3 & E.3 \\
row4 & A.4 & B.4 & C.4 & D.4 & E.4 \\
\rowcolor{lavender}
row5 & A.5 & B.5 & C.5 & D.5 & E.5 \\
\bottomrule
\end{tabular}
\end{table}
\end{landscape}


In this example, we put before the container so that LaTex can render the table vertically.

## 7. Conclusion

In this article, we showed how to render a table in LaTex, both horizontally and vertically. Also, we displayed various table formatting examples, such as three-line tables and table colors.

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.