3

I'm having the problem that \cline overshoots the end column of my table after forcing it to have \textwidth. Is there a way to force it to end in the same place as \hline?

\begin{table}[h]
\centering
\begin{tabularx}{\textwidth}{ c|c|c c|c c } 
\hline
\hline
 &  & \multicolumn{2}{c|}{Dog} & \multicolumn{2}{c}{Bear} \\
 \cline{3-6}
 & Cat & A & B & A & B \\
 \hline 
Value 1 & 0.70 & 0.57 & 0.42 & 0.41 & 0.48 \\ 
Value 2  & 0.10 & 0.051 & 0.07 & 0.03 & 0.022 \\ 
\hline
\end{tabularx}
\end{table}
1
  • 2
    You need at least one X column for a tabularx. However, in your case, a tabular makes more sense ...
    – user2478
    May 8, 2017 at 15:32

2 Answers 2

5

use

    \begin{tabularx}{\textwidth}{ X|c|c c|c c } 
        \hline
        \hline
        &  & \multicolumn{2}{c|}{Dog} & \multicolumn{2}{c}{Bear} \\
        \cline{3-6}
        & Cat & A & B & A & B \\
        \hline 
        Value 1 & 0.70 & 0.57 & 0.42 & 0.41 & 0.48 \\ 
        Value 2  & 0.10 & 0.051 & 0.07 & 0.03 & 0.022 \\ 
        \hline
    \end{tabularx}

or

    \begin{tabular}{ c|c|c c|c c } 
        \hline
        \hline
        &  & \multicolumn{2}{c|}{Dog} & \multicolumn{2}{c}{Bear} \\
        \cline{3-6}
        & Cat & A & B & A & B \\
        \hline 
        Value 1 & 0.70 & 0.57 & 0.42 & 0.41 & 0.48 \\ 
        Value 2  & 0.10 & 0.051 & 0.07 & 0.03 & 0.022 \\ 
        \hline
    \end{tabular}
2

A tabularx needs at least an X column.

Avoid vertical rules, they add nothing.

Here are four realizations of your table; the first is the wrong one, the next two are extended to the whole text width. The last one is how I'd do it.

\documentclass{article}
\usepackage{tabularx} % needed for the bad example
\usepackage{siunitx,booktabs} % for the better examples

\begin{document}

\begin{table}[!htp]
\centering
\begin{tabularx}{\textwidth}{ c|c|c c|c c } 
\hline
\hline
 &  & \multicolumn{2}{c|}{Dog} & \multicolumn{2}{c}{Bear} \\
 \cline{3-6}
 & Cat & A & B & A & B \\
 \hline 
Value 1 & 0.70 & 0.57 & 0.42 & 0.41 & 0.48 \\ 
Value 2  & 0.10 & 0.051 & 0.07 & 0.03 & 0.022 \\ 
\hline
\end{tabularx}

\bigskip

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}\hspace{\tabcolsep}} cccccc } 
\hline
\hline
 &  & \multicolumn{2}{c}{Dog} & \multicolumn{2}{c}{Bear} \\
 \cline{3-6}
 & Cat & A & B & A & B \\
 \hline 
Value 1 & 0.70 & 0.57 & 0.42 & 0.41 & 0.48 \\ 
Value 2  & 0.10 & 0.051 & 0.07 & 0.03 & 0.022 \\ 
\hline
\end{tabular*}

\bigskip

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}\hspace{\tabcolsep}}
  l
  S[table-format=1.2]
  S[table-format=1.3]
  S[table-format=1.2]
  S[table-format=1.2]
  S[table-format=1.3]
} 
\toprule
 &  & \multicolumn{2}{c}{Dog} & \multicolumn{2}{c}{Bear} \\
\cmidrule(lr){3-4}\cmidrule(lr){5-6}
 & {Cat} & {A} & {B} & {A} & {B} \\
\midrule 
Value 1 & 0.70 & 0.57 & 0.42 & 0.41 & 0.48 \\ 
Value 2  & 0.10 & 0.051 & 0.07 & 0.03 & 0.022 \\ 
\bottomrule
\end{tabular*}

\bigskip

\begin{tabular}{
  l
  S[table-format=1.2]
  S[table-format=1.3]
  S[table-format=1.2]
  S[table-format=1.2]
  S[table-format=1.3]
} 
\toprule
 &  & \multicolumn{2}{c}{Dog} & \multicolumn{2}{c}{Bear} \\
\cmidrule(lr){3-4}\cmidrule(lr){5-6}
 & {Cat} & {A} & {B} & {A} & {B} \\
\midrule 
Value 1 & 0.70 & 0.57 & 0.42 & 0.41 & 0.48 \\ 
Value 2  & 0.10 & 0.051 & 0.07 & 0.03 & 0.022 \\ 
\bottomrule
\end{tabular}

\end{table}

\end{document}

enter image description here

You must log in to answer this question.

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