[Tex/LaTex] Vertical and Horizontal alignment in table

tables

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}      

Best Answer

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.

enter image description here

\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.

enter image description here

\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}