Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to center a table on the page, not only the content. But I can't find the right keyparameter or command.

I have follwing table

\documentclass[11pt]{scrartcl}
\usepackage{multirow}

\usepackage[a4paper]{geometry} 
\geometry{a4paper,tmargin=3.5cm, bmargin=2.5cm, lmargin=2cm, rmargin=2.5cm, headheight=3em, headsep=1.5cm, footskip=1cm} 


\begin{document}
Erstellen Sie folgende Tabelle in der Datei \emph{A2.tex}. Verwenden sie
  dazu die Befehle \texttt{\textbackslash multirow},  \texttt{\textbackslash
  multicolumn}, \texttt{\textbackslash hline} und \texttt{\textbackslash cline}.

    \begin{tabular}{|c|c|c|c|}
        \hline
        \multirow{3}{*}{Zelle A}& Zelle B & \multicolumn{2}{|c|}{Zelle C} \\
        \cline{2-4}
        &\multicolumn{2}{|c|}{Zelle D}& Zelle F \\
        \cline{2-4}
         & Zelle H & Zelle I & Zelle J \\
         \cline{1-4}
        \multicolumn{4}{|c|}{Zelle K} \\
        \hline
    \end{tabular}

\end{document}

How can I center the whole table horizontally?

share|improve this question

5 Answers 5

up vote 7 down vote accepted

You can also enclose the tabularenvironment within a table environment. Not only will it allow you to center it (using the command \centering) but also to add a caption, a label for cross-reference, and to tweak the placement on the page.

So, for the most basic use :

\begin{table}[h]
\centering
  \begin{tabular}{|c|c|c|c|}
    \hline
    \multirow{3}{*}{Zelle A}& Zelle B & \multicolumn{2}{|c|}{Zelle C} \\
    \cline{2-4}
    &\multicolumn{2}{|c|}{Zelle D}& Zelle F \\
    \cline{2-4}
     & Zelle H & Zelle I & Zelle J \\
     \cline{1-4}
    \multicolumn{4}{|c|}{Zelle K} \\
    \hline
  \end{tabular}
\end{table}

If you really need the table to be where you added it in the source, the other answers (most notably the center environment) will probably prove less irritating.

share|improve this answer
    
What's the reason why a tabular environment should be inside a table environment? –  egreg Nov 10 '12 at 18:11
    
What's the reason why it shouldn't? Tables are usually added to a text as an illustration, not as part of the text itself (when I say "usually", I mean I can't find any counterexample -- it doesn't mean there is none). Not adding the table environment causes the table to be typeset as part of the text, and requires the user to use tricks for alignment, vertical spacing and such. –  T. Verron Nov 10 '12 at 18:16
    
There are cases where a table is part of the text flow and has no caption. Agreed that tabular material can cause troubles with pagination, it's not at all mandatory to enclose it in a table environment. I don't think that using center is so a big deal. –  egreg Nov 10 '12 at 18:19
    
@T.Verron This is such a case. The table is part of a LaTeX exercise. (“Create the following table […]. For that use the commands […].”) –  Qrrbrbirlbel Nov 10 '12 at 19:00
    
@Qrrbrbirlbel : Huhu... I shouldn't have quit german. Editting my answer in consequence. –  T. Verron Nov 10 '12 at 19:15

None of the suggestions here worked for me. My wider table always trailed to the right. But I found a solution here: http://www.latex-community.org/forum/viewtopic.php?f=45&t=19674 The \makebox command works beautifully.

\makebox[\linewidth]{
\begin{tabular}{|l|p{3cm}|p{6cm}|p{6cm}|}
%table contents
\end{tabular}
}
share|improve this answer
1  
Then it is possible similar to How can I center a too wide table? –  Werner Oct 1 '13 at 20:40

With the commands \centering and \hfill you can accomplish something like this quite easily. In this instance the center environment might be a better fit, as it adds vertical space before and after its conent. See the image to compare.

Note that \centering needs a paragraph to work.

I also added the line

\newcommand*{\befehl}[1]{\texttt{\textbackslash #1}}

to the preamble. Now you don't have to write \texttt{\textbackslash <befehl>} everytime but the shorter \befehl{<befehl>}.


Putting the tabular (with our without \centering) inside the table environment makes it a float (What are floats and how to position them? → How to influence the position of float environments like figure and table in LaTeX?) which means that LaTeX put your table probably at a place where it doesn't follow your paragraph, but it offers you a caption. (Caption without floating? → Label and caption without float)

Code

\documentclass[11pt]{scrartcl}
\usepackage{multirow}

\usepackage[a4paper]{geometry} 
\geometry{a4paper,tmargin=3.5cm, bmargin=2.5cm, lmargin=2cm, rmargin=2.5cm, headheight=3em, headsep=1.5cm, footskip=1cm} 
\newcommand*{\befehl}[1]{\texttt{\textbackslash #1}}

\begin{document}
Erstellen Sie folgende Tabelle in der Datei \emph{A2.tex}. Verwenden Sie dazu die Befehle \befehl{multirow},  \befehl{multicolumn}, \befehl{hline} und \befehl{cline}.

Text before Text before Text before Text before Text before Text before Text before Text before

{\centering% !
    \begin{tabular}{|c|c|c|c|}
        \hline
        \multirow{3}{*}{Zelle A} & Zelle B & \multicolumn{2}{|c|}{Zelle C} \\ \cline{2-4}
                                 & \multicolumn{2}{|c|}{Zelle D} & Zelle F \\ \cline{2-4}
                                 & Zelle H &       Zelle I       & Zelle J \\ \cline{1-4}
        \multicolumn{4}{|c|}{Zelle K}                                      \\ \hline
    \end{tabular}\par% !
}% !
Text after Text after Text after Text after Text after Text after Text after Text after Text after 

Text before Text before Text before Text before Text before Text before Text before Text before 
\begin{center}
    \begin{tabular}{|c|c|c|c|}
        \hline
        \multirow{3}{*}{Zelle A} & Zelle B & \multicolumn{2}{|c|}{Zelle C} \\ \cline{2-4}
                                 & \multicolumn{2}{|c|}{Zelle D} & Zelle F \\ \cline{2-4}
                                 & Zelle H &       Zelle I       & Zelle J \\ \cline{1-4}
        \multicolumn{4}{|c|}{Zelle K}                                      \\ \hline
    \end{tabular}%
\end{center}
Text after Text after Text after Text after Text after Text after Text after Text after Text after 

Text before Text before Text before Text before Text before Text before Text before Text before 

{\noindent\hfill% !
    \begin{tabular}{|c|c|c|c|}
        \hline
        \multirow{3}{*}{Zelle A} & Zelle B & \multicolumn{2}{|c|}{Zelle C} \\ \cline{2-4}
                                 & \multicolumn{2}{|c|}{Zelle D} & Zelle F \\ \cline{2-4}
                                 & Zelle H &       Zelle I       & Zelle J \\ \cline{1-4}
        \multicolumn{4}{|c|}{Zelle K}                                      \\ \hline
    \end{tabular}%
 \hfill} % !

Text after Text after Text after Text after Text after Text after Text after Text after Text after 
\end{document}

Output

enter image description here

share|improve this answer
    
A center environment seems the best choice, because it also provides some vertical space. –  egreg Nov 10 '12 at 18:12

In order to horizontally center a big object, the center environment is very useful, as it also provides automatically some vertical space above and below the object; so your input could be

\documentclass[11pt]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{multirow}

\usepackage{geometry} 
\geometry{a4paper,tmargin=3.5cm, bmargin=2.5cm, lmargin=2cm, rmargin=2.5cm, headheight=3em, headsep=1.5cm, footskip=1cm} 


\begin{document}
Erstellen Sie folgende Tabelle in der Datei \emph{A2.tex}. Verwenden sie
  dazu die Befehle \texttt{\textbackslash multirow},  \texttt{\textbackslash
  multicolumn}, \texttt{\textbackslash hline} und \texttt{\textbackslash cline}.
\begin{center}
\begin{tabular}{|c|c|c|c|}
    \hline
    \multirow{3}{*}{Zelle A}& Zelle B & \multicolumn{2}{|c|}{Zelle C} \\
    \cline{2-4}
    &\multicolumn{2}{|c|}{Zelle D}& Zelle F \\
    \cline{2-4}
     & Zelle H & Zelle I & Zelle J \\
     \cline{1-4}
    \multicolumn{4}{|c|}{Zelle K} \\
    \hline
\end{tabular}
\end{center}
Erstellen Sie folgende Tabelle in der Datei \emph{A2.tex}. Verwenden sie
  dazu die Befehle \texttt{\textbackslash multirow},  \texttt{\textbackslash
  multicolumn}, \texttt{\textbackslash hline} und \texttt{\textbackslash cline}.

\end{document}

(I've added also some useful packages, you can notice that the rendering of \textbackslash is better.)

enter image description here

However, if the table is very big you can consider giving it a caption and let it float:

\documentclass[11pt]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{multirow}

\usepackage{geometry} 
\geometry{a4paper,tmargin=3.5cm, bmargin=2.5cm, lmargin=2cm, rmargin=2.5cm, headheight=3em, headsep=1.5cm, footskip=1cm} 


\begin{document}
Erstellen Sie folgende Tabelle in der Datei \emph{A2.tex}. Verwenden sie
  dazu die Befehle \texttt{\textbackslash multirow},  \texttt{\textbackslash
  multicolumn}, \texttt{\textbackslash hline} und \texttt{\textbackslash cline}.
    \begin{table}[htp]
        \centering
        \begin{tabular}{|c|c|c|c|}
            \hline
            \multirow{3}{*}{Zelle A}& Zelle B & \multicolumn{2}{|c|}{Zelle C} \\
            \cline{2-4}
            &\multicolumn{2}{|c|}{Zelle D}& Zelle F \\
            \cline{2-4}
             & Zelle H & Zelle I & Zelle J \\
             \cline{1-4}
            \multicolumn{4}{|c|}{Zelle K} \\
            \hline
        \end{tabular}
        \caption{Tabelle mit \texttt{\textbackslash multirow}}
        \label{tab:multirow}
    \end{table}
Erstellen Sie folgende Tabelle in der Datei \emph{A2.tex}. Verwenden sie
  dazu die Befehle \texttt{\textbackslash multirow},  \texttt{\textbackslash
  multicolumn}, \texttt{\textbackslash hline} und \texttt{\textbackslash cline}.

\end{document}

You can refer to it in the text with \ref{tab:multirow}.

share|improve this answer

Consider wrapping the tabular in the center environment:

  \begin{center}
    \begin{tabular}{|c|c|c|c|}
        ...
    \end{tabular}
  \end{center}

Or use grouping in {} with the help of \centering and \hfill:

{\centering\hfill
    \begin{tabular}{|c|c|c|c|}
        ...
        \hline
    \end{tabular}
\hfill}
share|improve this answer
2  
\hfill is redundant; the most important thing is to issue a \par before closing the brace. –  egreg Nov 10 '12 at 18:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.