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 \textbf{tabular} environment to create a table:

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

Rendered by QuickLaTeX.com

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

For the table content, we use \& to separate cells and double-backslash \backslash \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}

Rendered by QuickLaTeX.com

In the table description \{ |c|c|c|\}, we declare three columns and separate them by vertical lines. Also, between every two rows, we use \backslash hline to separate them.

3. LaTex Table Position and Caption

In LaTex, we use the \textbf{table} environment to position the table inside it. For example, we can use \backslash begin\{table\}[h]} 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}

Rendered by QuickLaTeX.com

In this example, the table environment is a container to include a floating table. The position description [h] tells LaTex where to place the table. We can also use \backslash begin\{table\}[t]} to place the table at the top of the page or \backslash begin\{table\}[b]} to place the table at the bottom of the page.

Also, we can set a caption for the table inside the table 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}

Rendered by QuickLaTeX.com

In this example, we first use \backslash centering to center both the caption and the table content. Then, we use \backslash caption 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}

Rendered by QuickLaTeX.com

In this example, we first add \backslash usepackage\{booktabs\} into our LaTex content. Then, we use \backslash toprule, \backslash midrule, and \backslash bottomrule to represent the three lines.

Also, we can use \backslash cmidrule 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}

Rendered by QuickLaTeX.com

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

5. Table Colors

We can add colors to LaTex tables with color and colortbl packages. For example, we can use \backslash rowcolor 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}

Rendered by QuickLaTeX.com

In this example, we use \backslash definecolor to define a customized color. Then, we use \backslash rowcolor to set the background color of a table row.
Similarly, we can use \backslash columncolor 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}

Rendered by QuickLaTeX.com

We can also use \backslash cellcolor to set the background color of an individual cell and \backslash textcolor 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}

Rendered by QuickLaTeX.com

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 \textbf{lscape} package. Firstly, we add \backslash usepackage\{lscape\} into our LaTex content. Then, we can use the landscape 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}

Rendered by QuickLaTeX.com

In this example, we put \backslash begin\{landscape\} before the table 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.