4

My text "Left" and "Right" in the table below is not centered even with the c command in tabular. How do I fix this issue?

\documentclass[twocolumn]{book}
\usepackage   {lipsum}
\usepackage   {showframe} % just for this example
\usepackage   {subfig}
\usepackage   {tikz}

\begin{document}
\begin{table}
\setlength{\tabcolsep}{3pt}
\scriptsize
\centering
\caption{XXX}

\begin{tabular}{l|cc|cc|cc}
\toprule
& \multicolumn{2}{c|}{AAAAAAAAAAAAAA} & \multicolumn{2}{c|}{AAAAAAAAAAAAAA} & \multicolumn{2}{c}{AAAAAAAAAAAAAA} \\
\textbf{Recognition} & Left & Right & Left & Right & Left & Right \\
\midrule
Without Context & 0.65 & 0.17 & - & 0.87 & 0.82 & 0.50 \\
With Context & 0.60 & 0.58 & - & 0.94 & 0.77 & 0.38 \\
\bottomrule
& & & & & & \\
\toprule
& \multicolumn{2}{c|}{AAAAAAAAAAAAAA} & \multicolumn{2}{c|}{AAAAAAAAAAAAAA} & \multicolumn{2}{c}{AAAAAAAAAAAAAA} \\
\textbf{Anticipation} & Left & Right & Left & Right & Left & Right \\
\midrule
Without Context & 0.25 & - & 0.84 & 0.60 & 0.85 & 0.34 \\
With Context & 0.24 & - & 0.98 & 0.65 & 0.72 & 0.13 \\
\bottomrule
\end{tabular}

\end{table}
\end{document}

enter image description here

1
  • 3
    Upvoting answers as they come in is fine. However, if you quickly accept the very first answer that's been posted, you basically discourage others from posting additional, and potentially superior, answers. The site guidelines therefore recommend waiting several hours, and maybe even a day or more, before "accepting" an answer.
    – Mico
    Nov 29 '21 at 1:18
6

The text "Left" and "Right" are actually centered, it is just that the "Left" and "Right" columns are of different size. We can remedy this with a fixed width centered column type.

\documentclass[twocolumn]{book}
\usepackage   {lipsum}
\usepackage   {subfig}
\usepackage   {tikz}
\usepackage{booktabs, array}%<--
\newcolumntype{x}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}}%<--

\begin{document}
\begin{table}
\setlength{\tabcolsep}{3pt}
\scriptsize
\centering
\caption{XXX}

\begin{tabular}{l|x{.5in}x{.5in}|x{.5in}x{.5in}|x{.5in}x{.5in}}%<--
\toprule
& \multicolumn{2}{c|}{AAAAAAAAAAAAAA} & \multicolumn{2}{c|}{AAAAAAAAAAAAAA} & \multicolumn{2}{c}{AAAAAAAAAAAAAA} \\
\textbf{Recognition} & Left & Right & Left & Right & Left & Right \\
\midrule
Without Context & 0.65 & 0.17 & - & 0.87 & 0.82 & 0.50 \\
With Context & 0.60 & 0.58 & - & 0.94 & 0.77 & 0.38 \\
\bottomrule
& & & & & & \\
\toprule
& \multicolumn{2}{c|}{AAAAAAAAAAAAAA} & \multicolumn{2}{c|}{AAAAAAAAAAAAAA} & \multicolumn{2}{c}{AAAAAAAAAAAAAA} \\
\textbf{Anticipation} & Left & Right & Left & Right & Left & Right \\
\midrule
Without Context & 0.25 & - & 0.84 & 0.60 & 0.85 & 0.34 \\
With Context & 0.24 & - & 0.98 & 0.65 & 0.72 & 0.13 \\
\bottomrule
\end{tabular}

\end{table}
\end{document}

enter image description here

EDIT: Note my choice of .5in was arbitrary but fortunately close to @Mico's subsequent actual calculation of ~.53 inches. The difference in output is slight, but noticeable with calipers. Please see @Mico's excellent answer for those computations.

4
  • Could you explain how you arrived at 0.5in as the "correct" width of the centered p-type columns?
    – Mico
    Nov 29 '21 at 1:08
  • @Mico I didn't do any calculations (as I see you did in your answer), I simply tried .5 and it seemed to work nicely.
    – Dan
    Nov 29 '21 at 1:10
  • .53in would have been closer to the mark... If nothing else, you may want to note somewhere that the choice of .5in was based on eyeballing and thus slightly arbitrary (though, happily, not too far off from the exact value).
    – Mico
    Nov 29 '21 at 1:12
  • 1
    @Mico Thanks. I edited in a note in my answer. +1 for doing the calculations out.
    – Dan
    Nov 29 '21 at 1:43
6

(Since you're using the line-drawing macros of the booktabs package, the following solution will assume that the table does not contain any vertical rules.)

Since you're pre-defining the (fixed) combined width of two columns, you need to obtain the (usable) width of each underlying column by an explicit calculation. Here goes. The usable width of the combination of two columns is given by the width of the string of AAAAAAAAAAAAAA, say, Y. To arrive at the total width of the pair of columns, one needs to add 2\tabcolsep to Y. Now the usable width of each of the underlying columns is X, and the total width of each underlying column is X+2\tabcolsep. A quick calculation then shows that X=(Y-2\tabcolsep)/2. Finally, use the wc column type, which is provided by the array package, for each of the 6 data columns.

enter image description here

\documentclass[twocolumn]{book}
\usepackage{subfig,booktabs,calc}
\usepackage{array} % for 'w' column type

\begin{document}
\begin{table*}
\setlength{\tabcolsep}{3pt} % default: 6pt
\scriptsize

\newlength\mylen % calculate width of 'wc' columns:
\setlength\mylen{(\widthof{AAAAAAAAAAAAAA}-2\tabcolsep)/2}

\centering
\caption{XXX}

\begin{tabular}{ l *{6}{wc{\mylen}} }
\toprule
& \multicolumn{2}{c}{AAAAAAAAAAAAAA} 
& \multicolumn{2}{c}{AAAAAAAAAAAAAA} 
& \multicolumn{2}{c}{AAAAAAAAAAAAAA} \\
\textbf{Recognition} & Left & Right & Left & Right & Left & Right \\
\midrule
Without Context & 0.65 & 0.17 & -- & 0.87 & 0.82 & 0.50 \\
With Context    & 0.60 & 0.58 & -- & 0.94 & 0.77 & 0.38 \\
\bottomrule
\\
\toprule
& \multicolumn{2}{c}{AAAAAAAAAAAAAA} 
& \multicolumn{2}{c}{AAAAAAAAAAAAAA}
& \multicolumn{2}{c}{AAAAAAAAAAAAAA} \\
\textbf{Anticipation} & Left & Right & Left & Right & Left & Right \\
\midrule
Without Context & 0.25 & -- & 0.84 & 0.60 & 0.85 & 0.34 \\
With Context    & 0.24 & -- & 0.98 & 0.65 & 0.72 & 0.13 \\
\bottomrule
\end{tabular}

\end{table*}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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