2

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?

David Carlisle
  • 757,742
guest
  • 23
  • 1
  • 1
  • 4

3 Answers3

8

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}
David Carlisle
  • 757,742
  • That's not my problem. My problem is the table is positioned incorrectly. I want it to be at the center of the page instead of starting from the left and going over the right edge. – guest Mar 12 '13 at 00:04
  • See what happens if you change line "1 & 2 \" to "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 \". And you will see my problem – guest Mar 12 '13 at 00:09
  • 2
    It only goes over the right edge if it is wider than the page. If it is wider than the page it can not be centred. Arggg your problem is not as you described at all, I will update my answer – David Carlisle Mar 12 '13 at 00:10
  • Great! That works! Thank you very much and sorry for my description. I'm new to latex and do not know how to describe the problem professionally. – guest Mar 12 '13 at 00:18
  • 1
    @guest It's best not to describe it at all but to provide an example that demonstrates it. – David Carlisle Mar 12 '13 at 00:20
  • @guest If you like an aswer you can upvote it, in addition to accept it – JLDiaz Mar 12 '13 at 00:35
4

Try surrounding the whole thing with

\begin{center}
\end{center}
david
  • 465
  • 1
    does not work. The table starts from the left margin and even goes over the right edge of the page. I tried many ways but still cannot figure out. – guest Mar 11 '13 at 23:53
  • 2
    If the entire table is going over the edge you may need to decrease the size of font/table in order to make it fit, and then align it how you want.

    See this page for that...http://tex.stackexchange.com/questions/4139/how-to-change-font-size-mid-document

    – david Mar 11 '13 at 23:58
  • I'm using the default 12pt size. If I make it smaller enough to center the table I can't read my document. – guest Mar 12 '13 at 00:01
  • @guest No. You are not. You are setting the table at \large size not the 12pt document default. – David Carlisle Mar 12 '13 at 00:06
  • 1
    well I'll +1 as you answered the question asked, even if it wasn't the OP's problem as it finally turned out:-) – David Carlisle Mar 12 '13 at 00:37
  • 1
    It's not recommendable together with a table environment. Would you perhaps consider removing the suggestion? – Stefan Kottwitz Mar 13 '13 at 14:15
  • This answer could be improved by expanding it to a complete example, with the center environment containing a tabular. – Andrew Swann Mar 15 '13 at 10:58
0

Use \begin{center} like this. It will work. If you don't want to align content to center try changing |c| to |p{50mm}|.

\begin{table}[ht]
\caption{caption}
\begin{center}
\begin{tabular}{| c | c |}

...

\end{tabular} 
\end{center}
\label{label}
\end{table}
Nalan
  • 1