8

I have the following table:

\begin{tabular}{|p{3cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|}
\hline
\centerline {image}
 &    \multicolumn{2}{c|}{ABC} & \multicolumn{2}{c|}{DEF} \\
\hline
 & \centering A&\centering B&\centering C&\centering D
 \\
\hline
\centerline 1&&&& \\
\hline
\centerline 1&&&& \\
\hline
\centerline 1&&&& \\
\hline
\centerline 1&&&& \\
\hline
\end{tabular}

When I try centering D, I have a problem in the shape of the table. Why is that? How can I center it appropriately?

Thanks.

5
  • 1
    This is very similar in nature to How do I set right alignment in a longtable column with a fixed width? Try also loading the array package and issue \centering\arraybackslash...
    – Werner
    Dec 7, 2013 at 22:51
  • 3
    You can use \tabularnewline instead of \\ then it will work (\centering redefines \\ and breaks its table use.) don't use \centerline in LaTeX. Dec 7, 2013 at 22:56
  • or maybe {\centering D}? (Not tested)
    – alexis
    Dec 7, 2013 at 22:57
  • 1
    @alexis no justifcation command such as \centering or \raggedright has any effect if the end of paragraph is not in the scope of the command. Dec 7, 2013 at 23:08
  • Right... even inside a table cell. I forgot about that.
    – alexis
    Dec 8, 2013 at 1:17

3 Answers 3

8

Don't use the PlainTeX \centerline macro in a LaTeX document.

If the contents of every single column should be typeset centered, it's better to define column types that do so automatically, without the user having to type lots and lots of \centering instructions. This idea is implemented in the example below.

By the way, the user guide of the array package explains that there are some deep deep reasons for why ordinary \centering instructions need not work always correctly in the final column of a table. The package also suggests a workaround, which involves providing the instruction \arraybackslash.

enter image description here

\documentclass{article}
\usepackage{array}
\newcolumntype{U}{>{\centering\arraybackslash}p{3cm}}
\newcolumntype{V}{>{\centering\arraybackslash}p{1.5cm}}
\begin{document}
\begin{tabular}{|U|V|V|V|V|}
\hline
image & \multicolumn{2}{c|}{ABC} & \multicolumn{2}{c|}{DEF} \\
\cline{2-5}
 &  A& B& C& D \\
\hline
 1&&&& \\
\hline
 1&&&& \\
\hline
 1&&&& \\
\hline
 1&&&& \\
\hline
\end{tabular}
\end{document}
4

Using array package would enable you to simplify the markup a bit, nut you can do

\begin{tabular}{|p{3cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|}
\hline
\centering {image}
 &    \multicolumn{2}{c|}{ABC} & \multicolumn{2}{c|}{DEF} \tabularnewline
\hline
 & \centering A&\centering B&\centering C&\centering D
 \tabularnewline
\hline
\centering 1&&&& \tabularnewline
\hline
\centering 1&&&& \tabularnewline
\hline
\centering 1&&&& \tabularnewline
\hline
\centering 1&&&& \tabularnewline
\hline
\end{tabular}

\tabularnewline and \\ normally mean the same thing in tables, but \tabularnewline is there even when \centering redefines \\ (and it was added for that reason).

2

If you are using \multicolumn, why not to use it for centering? And no \centerline's! They mean centered lines, so using them inside a tabular is strange.

A version without additional packages:

\documentclass{article}

\begin{document}

\begin{tabular}{|p{3cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|}
\hline
%\centerline {image}
\centering image
 &    \multicolumn{2}{c|}{ABC} & \multicolumn{2}{c|}{DEF} \\
\hline
& \centering A&\centering B&\centering C& \multicolumn{1}{c|}{ D} \\
\hline
%\centerline 1&&&& \\
\centering 1&&&& \\
\hline
%\centerline 1&&&& \\
\centering 1&&&& \\
\hline
%\centerline 1&&&& \\
\centering 1&&&& \\
\hline
%\centerline 1&&&& \\
\centering 1&&& &\\
\hline
\end{tabular}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .