4

I want the code to give me a table with a fixed width, horizontally centered in the column. I've used >{\centering} to do this in the tabular definition, but adding this to the last column changes the table completely and throws up a bunch of errors (including the \hline being at the wrong position). What am I doing wrong/ better ways to do this?

\documentclass{article}
\usepackage{array,booktabs}% http://ctan.org/pkg/{array,booktabs}

\begin{document}

\begin{table}[htbp]
\center
\caption{a table}
\centering
%\hskip-2cm \begin{tabular}{p{0.9cm}| cccccc}
\begin{center}
\small
\begin{tabular}
{>{\centering}m{1.3cm}| >{\centering}p{2.14cm}>{\centering}p{2.14cm}>{\centering}p{2.13cm}>{\centering}p{2.13cm}p{2.13cm}}
Angle ($ ^{\circ}$) \newline $\dot{m}$ (g/m/s)   & 0 & 15 & 30 & 45 & 60 \\

\hline
\\

  70 
& 1
& 1 
& 1
& 1
& 1 \\
 

  70 
& 1
& 1 
& 1
& 1
& 1 \\



  70 
& 1
& 1 
& 1
& 1
& 1 \\
 \hline
\end{tabular}
\end{center}
\end{table}


\end{document}

This is the image after centering the last column enter image description here

4
  • Welcome to TeX.SE...
    – MadyYuvi
    Jul 7, 2020 at 9:19
  • 6
    For the last column you should do >{\centering\arraybackslash}, not just >{\centering}.
    – egreg
    Jul 7, 2020 at 9:22
  • 2
    Or use \tabularnewline instead of \\ to finish the row. Jul 7, 2020 at 9:24
  • Perfect. >{\centering\arraybackslash} fixes it! Jul 7, 2020 at 12:26

2 Answers 2

10

The problem is that >{\centering}p{2.13cm} in the last column changes the meaning of \\, so you have either to use \tabularnewline or do >{\centering\arraybackslash}p{2.13cm}.

However, since your cells, apart from the top left one, seem not to require line breaks, it's simpler if you use

w{c}{2.14cm}

that specifies a fixed width column with center alignment.

On the other hand, you'll have to guess the width in order to fit the given text width. So I'd suggest a different way to cope with the table.

\documentclass{article}
\usepackage{array,booktabs}% http://ctan.org/pkg/{array,booktabs}

\newcommand{\smashedcell}[1]{\smash{\begin{tabular}[t]{@{}c@{}}#1\end{tabular}}}

\begin{document}

\begin{table}[htbp]
\centering\small

\caption{a table}

\begin{tabular*}{\textwidth}{ @{\extracolsep{\fill}} *{6}{c} @{} }
\toprule
\smashedcell{$\dot{m}$ \\ (g/m/s)} & \multicolumn{5}{c}{Angle ($^{\circ}$)} \\
\cmidrule{2-6}
& 0 & 15 & 30 & 45 & 60 \\
\midrule
70 & 1 & 1 & 1 & 1 & 1 \\
70 & 1 & 1 & 1 & 1 & 1 \\
70 & 1 & 1 & 1 & 1 & 1 \\
\bottomrule
\end{tabular*}

\end{table}

enter image description here

Note that just a single \centering declaration is sufficient. Don't use \center, nor the center environment inside table, because they add vertical space (and the former should never be used).

1
  • I used >{\centering\arraybackslash}p{2.13cm} in the last column and it fixed the error. Mar 15 at 18:45
4

It is better to define a newcolumn instead of giving each column as >{\centering}m{1.3cm}, and include \arraybackslash command, and the modified MWE is:

\documentclass{article}
\usepackage{array,booktabs}% http://ctan.org/pkg/{array,booktabs}

\begin{document}

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}%

\begin{table}[htbp]
\center
\caption{a table}
\centering
%\hskip-2cm \begin{tabular}{p{0.9cm}| cccccc}
\begin{center}
\small
\begin{tabular}{C{1.3cm}| C{2.14cm}C{2.14cm}C{2.13cm}c C{2.13cm}}
Angle ($ ^{\circ}$) \newline $\dot{m}$ (g/m/s)   & 0 & 15 & 30 & 45 & 60 \\

\hline
\\

  70 
& 1
& 1 
& 1
& 1
& 1 \\
 

  70 
& 1
& 1 
& 1
& 1
& 1 \\



  70 
& 1
& 1 
& 1
& 1
& 1 \\
 \hline
\end{tabular}
\end{center}
\end{table}


\end{document}

Output

enter image description here

You must log in to answer this question.

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