# centering a line in table's cell

I'm using two columns style. I would like to modify a table so it can fit with keeping clarity. Two of the cells their texts should be in two lines. The second line should be in the center.

\documentclass[11pt,twocolumn]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{amsmath}
\usepackage{pbox}
\begin{document}

\begin{table}[h]
\caption{An Example of a Table}
\label{table_example}
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|}
\hline
A  & B & C & D & Q & W &  error $\%$  in x  &  \pbox{20cm}{error $\%$ \\ in x } \\
\hline
23 & 1   & 80.9281   & 80.9281   &  80.9281          & aa                &      aa &\\
\hline
23 & 1   & 80.9281   & 80.9281   &  80.9281          & aa                &      aa &\\
\hline
23 & 1   & 80.9281   & 80.9281   &  80.9281          & aa                &      aa &\\
\hline
23 & 1   & 80.9281   & 80.9281   &  80.9281          & aa                &      aa &\\
\hline
23 & 3   & 80.9281   & 80.9281   &  80.9281          & aa                &      aa &\\
\hline
\end{tabular}
\end{center}
\end{table}

\end{document}


This is the picture of the code

• The \parbox IS centered; it takes up the whole space. To center the text inside the \parbox use \centering. – John Kormylo Sep 19 '14 at 14:37
• also use a p{2cm} in the tabular preamble instead of c rather than use \pbox – David Carlisle Sep 19 '14 at 14:43
• A \pbox{20cm} is almost wider than the paper size, I assume? – user31729 Sep 19 '14 at 14:46

You should use the makecell package for that. It's dedicated to common formatting of multiline cells and defines a \thead and a \makecell command a multirow version of these. The default alignment in such cells is centred (vertically and horizontally) with keywords to choose among t, b, c and l, r, c, independently of the column specifier. Here are two examples using \thead, for which I specify to use a boldface font.

Also I added the caption package to have a proper vertical spacing between caption and table, and the cellspace package to ensure a minimal spacing between the top of a cell and the line above, and between the bottom of a cell and the line below; it works defining an S pre-specifier for columns.

Another solution to get soacing between rows is to use the booktabs package; it defines new commands for horizontal rules of variable thickness, with some vertical spacing above and below; it this spacing is not enough, you have the \addlinespace command.

Finally I replaced the center environment with centering (center adds unwanted here vertical spacing):

\documentclass[11pt,twocolumn]{article}
\usepackage[margin=1cm,]{geometry}
\usepackage{amsmath}
\usepackage{pbox}
\usepackage{caption, booktabs}
\usepackage{makecell}
\usepackage{cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}

\begin{document}

\begin{table}[h]
\caption{An Example of a Table}
\centering\label{table_example}
\begin{tabular}{|Sc*{8}{c|}}
\hline
\hline
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
23 & 3 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
\end{tabular}
\end{table}

\begin{table}[h]
\caption{An Example of a Table}
\centering\label{table_example}
\begin{tabular}{|*{8}{c|}}
\hline
\hline
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
23 & 3 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\hline
\end{tabular}
\end{table}

\begin{table}[h]
\caption{An Example of a Table}
\centering\label{table_example}
\begin{tabular}{*{8}{c}}
\toprule
\cmidrule(lr){1-8}
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
23 & 3 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\bottomrule
\end{tabular}
\end{table}

\end{document}


• thanks for this answer. For table 1 in your answer, is there a way to decrease the height of the first row? – CroCo Sep 19 '14 at 15:36
• @ Croco: Yes, with the cellspace package, which has a different approach '(instead of adding vertical spacing uniformly, it defines a minimal vertical spacing). There is also another solution with booktabs (no vertical rules then) which is better-looking in my (and many's) opinion. I'll update my answer in a moment. – Bernard Sep 19 '14 at 16:49

Maybe I don't understand your setup, but I don't think it's possible to have a table with a column that's 20cm wide and still have the entire table fit in the allocated width (which is equal to \columnwidth, i.e., less than half of \textwidth).

I suggest you use the tabularx package, specify the total width of the tabularx environment to equal \columnwidth, and use two (modified) X column specifiers for the final two columns. The material in an X column automatically wraps across lines.

Since horizontal space is a scarce commodity, I would reduce the amount of intercolumn whitespace by 50%, by setting \tabcolsep to 3pt. (The default value is 6pt.) The whitespace that's saved in this manner is automatically allocated to the two X columns.

I further suggest that you get rid of all vertical bars -- trust me, they won't be missed -- and most horizontal lines, and employ the macros toprule, \midrule, and \bottomrule of the booktabs package for the remaining horizontal lines.

\documentclass[11pt,twocolumn]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{amsmath}
\usepackage{tabularx,ragged2e,booktabs,caption}
\newcolumntype{C}{>{\Centering\arraybackslash}X}  % modified X column
\begin{document}
\begin{table}[th]
\setlength\tabcolsep{3pt} % default value: 6pt
\caption{An Example of a Table} \label{table_example}
\begin{tabularx}{\columnwidth}{@{} cccccc CC @{}}
\toprule
A & B & C & D & Q & W & error $\%$ in x & error $\%$ in x \\
\midrule
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
23 & 3 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}