1

Below is the table that I am trying to diagnose:

\documentclass{article}
\usepackage{array, makecell}

\begin{document}

\begin{table*}
  \begin{tabular}{@{}  >{\centering} m{1.67cm}| >{\centering} m{0.7cm} 
                       >{\centering} m{0.7cm} >{\centering} m{0.7cm} >{\centering} m{0.7cm} 
                       >{\centering} m{0.7cm} >{\centering} m{0.7cm} >{\centering} m{0.7cm}                 
                       >{\centering} m{0.7cm} @{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & $7$ \newline $8$ \newline $9$ & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table*}
\end{document}

If I try to compile this, it gives several Extra alignment tab has been changed to \cr errors and the entire table is a mess. However, if I just remove the final >{\centering} from the last column, everything looks fine. How can I include the centering and have the table show up fine? The @{} included in the tabular brackets came from a suggestion in this question, which didn't end up solving the problem.

As an additional side-question, if I change the document class from article to revtex4-1, the rows with entries that have newlines (the first row) is no longer vertically centered. Does anyone know what's up with that, and how to get vertical centering in tables with revtex?

1
  • What are these \newline commands for?
    – user194703
    Apr 2, 2020 at 6:47

2 Answers 2

3

Here are some suggestions. In the captions to the table, I have explained the changes:

enter image description here

\documentclass{article}
\usepackage{array, makecell}

\begin{document}

\begin{table}
\caption{corrected the use of centering by adding arraybackslash, simplified column declaration}
  \begin{tabular}{@{}  >{\centering\arraybackslash} m{1.67cm}| *{8}{>{\centering\arraybackslash} m{0.7cm}}@{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & $7$ \newline $8$ \newline $9$ & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table}


\begin{table}
\caption{Used makecell from the makecell package in order to get the linebreaks in column 8}
  \begin{tabular}{@{}  >{\centering\arraybackslash} p{1.67cm}| *{8}{>{\centering\arraybackslash} p{0.7cm}}@{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & \makecell{$7$ \\ $8$ \\ $9$} & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table}


\begin{table}
\caption{Since no automatic linebreaks in table cells seem to be needed, why not just go for simple c type columns?}
  \begin{tabular}{@{}  c| *{8}{c}@{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & \makecell{$7$ \\ $8$ \\ $9$} & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table}

\begin{table}
\caption{If you prefer wider cells but do not need automatic linebreaks, why not go for the new column type W from the array package?}
  \begin{tabular}{@{}  Wc{1.67cm}| *{8}{Wc{0.7cm}}@{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & \makecell{$7$ \\ $8$ \\ $9$} & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table}
\end{document}
1

Here is a variant that use tabular*, \extracolsep{fill} and nested tabular to create the multiline cell in the heading. threeparttable is a simple way to adjust the width of the caption to match the width of the table

enter image description here

\documentclass{article}
\usepackage{array, threeparttable}

\setlength{\extrarowheight}{2pt}
\renewcommand*{\arraystretch}{1.1}

\begin {document}
\begin{table}
    \begin{threeparttable}
    \centering
    \caption{A caption with lot of interesting information of how to interpret the complicated figures and mathematics in the tabular}
        \begin{tabular*}{0.8\linewidth}{@{\extracolsep{\fill}}@{}c| *{8}{c}@{}}
            Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & \begin{tabular}{@{}c@{}}
                                                       $7$ \\ $8$ \\ $9$\end{tabular} & $8$ \\
           \hline
           \hline
           1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
           \hline
           1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
           \hline
           1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
           \hline
        \end{tabular*}
    \end{threeparttable}
\end{table}

\end{document}

You must log in to answer this question.

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