# Vertical and Horizontal alignment in table

Question: Here i made a simple table. My question is very simple how can i align text inside the table horizontally and vertically?

MWE:

\documentclass[12pt, a4paper]{article}
\usepackage[a4paper,top=1 in,bottom=1 in,left=0.7 in,right=0.7 in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[misc]{ifsym}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{parskip}
\usepackage{array,booktabs}
\begin{document}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|}
\hline
2 & 120 \\ \hline
2 & 60  \\ \hline
2 & 30  \\ \hline
3 & 15  \\ \hline
5 & 5   \\ \hline
& 1   \\ \hline
\end{tabular}
\end{table}
\end{document}

• Welcome to TeX.SE! A quick hint: If you right-indent lines of code by four spaces -- done most easily by highlighting the line of code in question and clicking on the {} "button" in the editing ribbon above the editing window -- the site software will automatically pretty-print the code. – Mico May 27 '16 at 4:57

For horizontal centering, use the c ("center") column type instead of the l ("left-align") column type.

Vertical centering may be achieved in a number of ways. Since the table appears to contain mostly numbers, I suggest you provide the instruction \setlength\extrarowheight{2.5pt} immediately after \begin{table}. By the way, "vertical centering" has both geometric as well as aesthetic components: What may be perfectly centered from a narrow, i.e., purely geometric point of view may not "look" like it's centered when viewed by (many) humans. Experienced framers of pictures, and experienced typesetters, will usually provided a bit more whitespace at the upper edge than at the lower edge in order to make the object look centered.

\documentclass[12pt,a4paper]{article}
\usepackage[a4paper,vmargin=1in,hmargin=0.7in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[misc]{ifsym}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{parskip,array,booktabs}

\begin{document}
\begin{table}
\setlength\extrarowheight{2.5pt}
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|c|c|}
\hline
2 & 120 \\ \hline
2 & 60  \\ \hline
2 & 30  \\ \hline
3 & 15  \\ \hline
5 & 5   \\ \hline
& 1   \\ \hline
\end{tabular}
\end{table}
\end{document}


Addendum: As indicated above, the issue of vertical "centering" between horizontal lines has both geometric and aesthetic aspects. The following screenshot shows four variations of the same tabular environment, with \extrarowheight set to 0, 2, 2.5, and 3 pt, respectively. To my eye, entirely subjectively, the version with 3pt of extra vertical row height looks "best", even though there's quite clearly more vertical whitespace above than below the numerals.

However, it's even more important not to lose sight of what really matters, which is to create a visually attractive and easily understandable table. In my view, worrying about how to center the rows properly (both horizontally and vertically) is both an unnecessary and self-inflicted distraction, caused by the presence of the multitude of vertical and horizontal lines that are, upon reflection, not even needed. To wit, the table on the far right of the screenshot dispenses with all vertical lines and most horizontal lines. It is evident that the issue of perfect spacing doesn't even arise in this table, simply because there are no vertical lines and no interior horizontal lines to distract the reader. As a result of this aethetic simplification, the reader will (hopefully) find it easier to concentrate on the contents of the table.

\documentclass[12pt,a4paper]{article}
\usepackage[a4paper,vmargin=1in,hmargin=0.7in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{array,booktabs}

\newcommand\mytab{%
\begin{tabular}[t]{|c|c|}\hline
2 & 120 \\ \hline
2 & 60  \\ \hline
2 & 30  \\ \hline
3 & 15  \\ \hline
5 & 5   \\ \hline
& 1   \\ \hline
\end{tabular}}

\begin{document}
\begin{table}
\centering
\setlength\extrarowheight{0pt}   \mytab
\setlength\extrarowheight{2pt}   \mytab
\setlength\extrarowheight{2.5pt} \mytab
\setlength\extrarowheight{3pt}   \mytab
\setlength\extrarowheight{0pt} % back to zero
\begin{tabular}[t]{cr}
\toprule
2 & 120 \\
2 & 60  \\
2 & 30  \\
3 & 15  \\
5 & 5   \\
& 1   \\
\bottomrule
\end{tabular}
\end{table}
\end{document}


Another solution is to load the cellspace package, which defines minimal vertical spacing between a cell and the above and below cells in columns with specifier prefixed with the letter S (or C if you use siunitx). I added the caption package to have a decent vertical spacing between caption and table (basic latex doesn't permutes the values of \abovecaptionskip and \belowcaptionskip when caption is above table):

\documentclass[12pt, a4paper]{article}
\usepackage[a4paper,top=1 in,bottom=1 in,left=0.7 in,right=0.7 in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts, amssymb}
\usepackage{parskip}
\usepackage{array}
\usepackage{caption}
\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}

\begin{document}

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|Sc|Sc|}
\hline
2 & 120 \\ \hline
2 & 60 \\ \hline
2 & 30 \\ \hline
3 & 15 \\ \hline
5 & 5 \\ \hline
& 1 \\ \hline
\end{tabular}
\end{table}

\end{document}