[Tex/LaTex] centering a line in table’s cell

tables

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 enter image description here

Best Answer

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}

\renewcommand\theadfont{\bfseries}

\begin{document}

\begin{table}[h]
\caption{An Example of a Table}
\centering\label{table_example}
\begin{tabular}{|Sc*{8}{c|}}
\hline
\thead{A} &\thead{ B} & \thead{C} & \thead{D} & \thead{Q} & \thead{W} & \thead{error \% in x} & \thead{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{table}

\begin{table}[h]
\renewcommand\theadalign{bc}
\caption{An Example of a Table}
\centering\label{table_example}
\begin{tabular}{|*{8}{c|}}
\hline
\thead{A} &\thead{ B} & \thead{C} & \thead{D} & \thead{Q} & \thead{W} & \thead{error \% in x} & \thead{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{table}

\begin{table}[h]
\renewcommand\theadalign{bc}
\caption{An Example of a Table}
\centering\label{table_example}
\begin{tabular}{*{8}{c}}
\toprule
\thead{A} &\thead{ B} & \thead{C} & \thead{D} & \thead{Q} & \thead{W} & \thead{error \% in x} & \thead{error \% \\ in x } \\
\cmidrule(lr){1-8}
\addlinespace
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\addlinespace
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\addlinespace
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\addlinespace
23 & 1 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\addlinespace
23 & 3 & 80.9281 & 80.9281 & 80.9281 & aa & aa &\\
\bottomrule
\end{tabular}
\end{table}

\end{document} 

enter image description here