Center (horizontally and vertically) a table in LaTeX
In this tutorial, you will learn how to center a table vertically and horizontally on a page in LaTeX. Also, how can you center a table both vertically and horizontally.
Center a table horizontally on a page
In order to center a table horizontally you can use the \centering
command before the tabular
environment or you can write the tabular
inside the center
environment. But personally, I would recommend using \centering
.
\documentclass{article} \usepackage{lipsum} \begin{document} \section{Center table with \textbackslash centering} \lipsum[1][1-5]\par \begin{table}[h] \centering \begin{tabular}{|c|c|c|} \hline Column 1 & Column 2 & Column 3 \\ \hline A & B & C \\ \hline D & E & F \\ \hline \end{tabular} \caption{Center table with \textbackslash centering} \end{table} \section{Center table with center environment} \lipsum[1][1-5]\par \begin{table}[h] \begin{center} \begin{tabular}{|c|c|c|} \hline Column 1 & Column 2 & Column 3 \\ \hline A & B & C \\ \hline D & E & F \\ \hline \end{tabular} \caption{Center table with \texttt{center} environment} \end{center} \end{table} \end{document}
Output:
Center a table vertically on a page
In order to center a table vertically you can use the \vspace{\fill}
command above a below the table environment. If you create a table only by tabular
then you can also use this command above and below tabular
environment.
\documentclass{article} \usepackage[ singlelinecheck=false ]{caption} % To align the caption on left \usepackage{lipsum} \begin{document} \section{Center table with \textbackslash vspace\{fill\}} \vspace{\fill} \begin{table}[h] \begin{tabular}{|c|c|c|} \hline Column 1 & Column 2 & Column 3 \\ \hline A & B & C \\ \hline D & E & F \\ \hline \end{tabular} \caption{Center table with vertically with \textbackslash vspace } \end{table} \vspace{\fill} \end{document}
Output:
Center a table both vertically and horizontally
If your page contains no text except the table, the table
environment and \centering
commands will center the table vertically and horizontally.
You can also use the \vspace{\fill}
command above and below the table
environment to center the table vertically and the \centering
command to center the table horizontally.
\documentclass{article} \usepackage{lipsum} \begin{document} \section{Center table with \textbackslash vspace\{fill\} and \textbackslash centering} \vspace{\fill} \begin{table}[h] \centering \begin{tabular}{|c|c|c|} \hline Column 1 & Column 2 & Column 3 \\ \hline A & B & C \\ \hline D & E & F \\ \hline \end{tabular} \caption{Center table both vertically and horizontally} \end{table} \vspace{\fill} \end{document}
Output:
If the page contains no content other than the table, the \centering
command is sufficient to center the table both vertically and horizontally. Try this code.
\documentclass{article} \usepackage{lipsum} \begin{document} \begin{table} \centering \begin{tabular}{|c|c|c|} \hline Column 1 & Column 2 & Column 3 \\ \hline A & B & C \\ \hline D & E & F \\ \hline \end{tabular} \caption{Center table both vertically and horizontally with \textbackslash centering} \end{table} \end{document}
Also read:
- How to fix figure position in LaTeX?
- Add vertical space forcefully at the start of a page in LaTeX
- Set table width same size of text width in LaTeX
Leave a Reply