[Tex/LaTex] How to center align the whole table

horizontal alignment

I created a table and it starts from the left and goes all the way to right, which looks really skewed. So I want to center align the whole table but couldn't figure out how to do it. I'm new to latex so I don't know how to use other plugin or macros, but I suppose there's a command for this basic functionality. Can anyone tell me?

This is my code:

\documentclass[a4paper, 12pt]{article}
\author{My Name}
\setlength\parindent{0pt}
\addtolength{\topmargin}{-1in}
\begin{document}
\begin{table}[h]
    \centering
    \large{Something:}
    \centerline{Table 1:}
    \begin{tabular}{c c}
        \hline
        1 & 2 \\
        3 & 4 \\
        5 & 6 \\
        7 & 8 \\
    \end{tabular}

    \large{Table 2}

    \begin{tabular}{c c}
        \hline
        1 & 2 \\
    \end{tabular}

\end{table}
\end{document}

Note here my numbers from 1 – 8 and 1,2 in table 2 are actually long text, which are long enough that it goes over the right edge of the paper. How can I fix it?

Best Answer

You should use \caption and let LaTeX number the tables rather than using \centerline and numbering by hand.

\large does not take an argument so \large{something} makes all the following text large, including the table.

You should never use [h] Just using [h] on its own is really an error; LaTeX issues a warning and changes it to [th] but even then it makes it very likely the table goes to the end of the document as it disallows p positioning (float pages).

Finally You provide an example that shows the problem you have. c columns are like \mbox and single line you want p columns that allow line breaking to a specified width. Then the columns are narrower and the table fits on a page and can be centred. Note changing c to p was one of the suggestions in the linked question about making a table smaller.

enter image description here

\documentclass[a4paper, 12pt]{article}
\author{My Name}
\setlength\parindent{0pt}
\addtolength{\topmargin}{-1in}

\begin{document}

\begin{table}
    \centering
    Something:

\caption{blah blah}

\smallskip

    \begin{tabular}{c c}
        \hline
        1 & 2 \\
        3 & 4 \\
        5 & 6 \\
        7 & 8 \\
    \end{tabular}

 \caption{blah blah}

\smallskip
    \begin{tabular}{p{3cm} p{3cm}}
        \hline
        This is a very long text and it goes over the edge & and I can not figure out how to align it at the center \\
    \end{tabular}

\end{table}

\end{document}

\documentclass[a4paper, 12pt]{article}
\author{My Name}
\setlength\parindent{0pt}
\addtolength{\topmargin}{-1in}

\begin{document}

\begin{table}
    \centering
    Something:

\caption{blah blah}

\smallskip

    \begin{tabular}{c c}
        \hline
        1 & 2 \\
        3 & 4 \\
        5 & 6 \\
        7 & 8 \\
    \end{tabular}

 \caption{blah blah}

\smallskip
    \begin{tabular}{c c}
        \hline
        1 & 2 \\
    \end{tabular}

\end{table}

\end{document}
Related Question