# \linewidth plus \cline plus centering using tabularx does not work out

I am trying to create a table using tabularx to stretch a table to over the full page width. Usually, it would not need the full width but I like it better if it's like this. In my table, I have some multirow cells, therefore I use \cline{2-9} instead of \hline at some points. Finally, I would like to have the content of my cells centered. However, I seem to be unable to combine all these aspects.

If I use \begin{tabularx}{\textwidth}{llXXXXXXX} %\textwidth or \linewith leads to the same result stretching works but centering of the cell values does not as can be seen in this example

If I try \begin{tabularx}{\textwidth}{llccccccc} centering works but the something is off with the \cline's and \hline's

Condensed code:

\begin{table}[H]
\centering
\begin{threeparttable} %because I'm using footnotes
\caption{some caption}
\label{tab:some table}
\begin{tabularx}{\textwidth}{llccccccc}
\toprule
&    &\multicolumn{7}{c}{some ID} \\
&    &1   &2   &3   &4  &5   &6   &7   \\ \midrule
\multirow{2}{*}{This is} &a test &$\sim$   &2   &+   &+   &+   &+   &+   \\ \cline{2-9}
&not a test    &+   &10   &+   &+   &+   &+   &--   \\ \hline
\end{tabularx}
\end{threeparttable}
\end{table}


Can anybody help me fix this? This would be great!

PS: I posted this question also on reddit but haven't received an answer yet. If a working solution is found there, I'll let you know.

Here are four ways to do the table.

First and foremost: you have to specify at least an X column in tabularx. But you don't really need it, as table 2 shows.

On the other hand, \multirow adds nothing to clarity, rather the converse, see table 3 (where also the left and right paddings are removed).

I'd simply use the natural width, unless the final size is not so different from the global text width.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tabularx,booktabs,multirow,array}

\begin{document}

\begin{table}
\centering

\caption{The table with \texttt{tabularx}}

\begin{tabularx}{\textwidth}{XX*{7}{c}}
\toprule
&    &\multicolumn{7}{c}{some ID} \\
&    &1   &2   &3   &4  &5   &6   &7   \\ \midrule
\multirow{2}{*}{This is} &a test &$\sim$   &2   &+   &+   &+   &+   &+   \\ \cmidrule{2-9}
&not a test    &+   &10   &+   &+   &+   &+   &--   \\ \bottomrule
\end{tabularx}

\bigskip

\caption{The table with \texttt{tabular*}}

\begin{tabular*}{\textwidth}{
@{\extracolsep{\fill}\hspace{\tabcolsep}}
ll
>{$}c<{$}
@{\extracolsep{0pt}\hspace{2\tabcolsep}}
*{6}{>{$}c<{$}}
}
\toprule
&    &\multicolumn{7}{c}{some ID} \\
&    &1   &2   &3   &4  &5   &6   &7   \\ \midrule
\multirow{2}{*}{This is} & a test     & \sim &  2 & + & + & + &+ & + \\
\cmidrule{2-9}
& not a test & +    & 10 & + & + & + &+ & - \\
\bottomrule
\end{tabular*}

\bigskip

\caption{The table with \texttt{tabular*} and no \texttt{\string\multirow}}

\begin{tabular*}{\textwidth}{
@{\extracolsep{\fill}}
ll
>{$}c<{$}
@{\extracolsep{0pt}\hspace{2\tabcolsep}}
*{6}{>{$}c<{$}}
@{}
}
\toprule
&    &\multicolumn{7}{c}{some ID} \\
\cmidrule(l){3-9}
&    &1   &2   &3   &4  &5   &6   &7   \\ \midrule
This is & a test     & \sim &  2 & + & + & + &+ & + \\
\cmidrule(l){2-9}
& not a test & +    & 10 & + & + & + &+ & - \\
\bottomrule
\end{tabular*}

\bigskip

\caption{The table as I would do}

\begin{tabular}{
@{}
ll
*{7}{>{$}c<{$}}
@{}
}
\toprule
&    &\multicolumn{7}{c}{some ID} \\
\cmidrule(l){3-9}
&    &1   &2   &3   &4  &5   &6   &7   \\ \midrule
This is & a test     & \sim &  2 & + & + & + &+ & + \\
\cmidrule(l){2-9}
& not a test & +    & 10 & + & + & + &+ & - \\
\bottomrule
\end{tabular}

\end{table}

\end{document}


• Thank you very much for extensive help! I haven't figured out yet which version I want to use but all of them work great! – starsnpixel Apr 23 at 13:59

As was in the meantime suggested in the comment, I added the following column type

\newcolumntype{Z}{>{\centering\arraybackslash}X}


so that the content is centered. I then changed the ccccccc columns with *{7}{Z} (syntax to define 7 consecutive columns of Z type).

I also added the \otoprule separator so that you can have the same line thickness of \toprule but equal spacing between the rows for the headings:

\newcommand{\otoprule}{\midrule[\heavyrulewidth]}


A MWE is as follows:

\documentclass{article}
\usepackage{tabularx}
\usepackage{threeparttable}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

\newcolumntype{Z}{>{\centering\arraybackslash}X}
\newcommand{\otoprule}{\midrule[\heavyrulewidth]}

\begin{table}
\centering
\begin{threeparttable} %because I'm using footnotes
\caption{some caption}
\label{tab:sometable}
\begin{tabularx}{\textwidth}{ll*{7}{Z}}
\toprule
&            & \multicolumn{7}{c}{some ID} \\
&            & 1                              & 2  & 3 & 4 & 5 & 6 & 7   \\ \midrule
\multirow{2}{*}{This is} & a test     & $\sim$                         & 2  & + & + & + & + & +   \\ \cline{2-9}
& not a test & +                              & 10 & + & + & + & + & --   \\ \hline
\end{tabularx}
\end{threeparttable}
\end{table}
\end{document}