4

I am trying to code a table in latex that looks like this:

enter image description here

and my attempt is this one:

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\begin{document}

    \begin{table}[h]
  \centering
  \caption{description}
    \begin{tabular}{r|ccc|ccc|}
    \toprule
          & \multicolumn{3}{c|}{\textbf{header 1}} & \multicolumn{3}{c|}{\textbf{header 2}} \\
    \midrule
          & \textbf{a} & \textbf{b} & \textbf{c} & \textbf{a} & \textbf{b} & \textbf{c} \\
     \midrule
    \textbf{row 1} & 1     & 1     & 1     & 1     & 1     & 1 \\
    \textbf{row 2} & 1     & 1     & 1     & 1     & 1     & 1 \\
    \textbf{row 3} & 1     & 1     & 1     & 1     & 1     & 1 \\
    \midrule
    \textbf{row 4} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 5} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 6} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 7} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 8} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 9} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \bottomrule
    \end{tabular}
  \label{tab:somelabel}
\end{table}
\end{document}

enter image description here

the problem is that some lines look incomplete and also I dont know how could some of the borders (top left) could be removed.

2
  • Please post complete code i.e. make it compilable.
    – cfr
    Commented Jan 4, 2015 at 3:45
  • You don't want [h] either.
    – cfr
    Commented Jan 4, 2015 at 3:56

2 Answers 2

6

You are trying to mix up the visual queues suggested by booktabs and what you're trying to achieve. booktabs mentions in its documentation:

You will not go far wrong if you remember two simple guidelines at all times:

  1. Never, ever use vertical rules.

  2. Never use double rules.

These guidelines may seem extreme but I have never found a good argument in favour of breaking them. For example, if you feel that the information in the left half of a table is so different from that on the right that it needs to be separated by a vertical line, then you should use two tables instead.

The following provides your original request and a pure booktabs presentation:

\documentclass{article}

\usepackage{array,booktabs}

\begin{document}

\begin{tabular}{r|ccc|ccc|}
  \cline{2-7}
        & \multicolumn{3}{c|}{\textbf{header 1}} & \multicolumn{3}{c|}{\textbf{header 2}} \\
  \cline{2-7}
        & \textbf{a} & \textbf{b} & \textbf{c} & \textbf{a} & \textbf{b} & \textbf{c} \\
  \cline{2-7}
  \textbf{row 1} & 1     & 1     & 1     & 1     & 1     & 1 \\
  \textbf{row 2} & 1     & 1     & 1     & 1     & 1     & 1 \\
  \textbf{row 3} & 1     & 1     & 1     & 1     & 1     & 1 \\
  \hline
  \textbf{row 4} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
  \textbf{row 5} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
  \textbf{row 6} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
  \textbf{row 7} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
  \textbf{row 8} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
  \textbf{row 9} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
  \cline{2-7}
\end{tabular}

\bigskip

\begin{tabular}{ >{\bfseries}r *{6}{c} }
  \toprule
        & \multicolumn{3}{c}{\bfseries header 1} & \multicolumn{3}{c}{\bfseries header 2} \\
  \cmidrule(lr){2-4}\cmidrule(lr){5-7}
        & \textbf{a} & \textbf{b} & \textbf{c} & \textbf{a} & \textbf{b} & \textbf{c} \\
  \midrule
  row 1 & 1     & 1     & 1     & 1     & 1     & 1 \\
  row 2 & 1     & 1     & 1     & 1     & 1     & 1 \\
  row 3 & 1     & 1     & 1     & 1     & 1     & 1 \\
  \midrule
  row 4 & & 0 & & & 0 & \\
  row 5 & & 0 & & & 0 & \\
  row 6 & & 0 & & & 0 & \\
  row 7 & & 0 & & & 0 & \\
  row 8 & & 0 & & & 0 & \\
  row 9 & & 0 & & & 0 & \\
  \bottomrule
\end{tabular}
\end{document}

The regular tabular:

enter image description here

booktabs yields

enter image description here

0
5

This is covered in the booktabs documentation: its enhancements are designed to be allergic to vertical rules. If you use \toprule, \bottomrule and friends, then do not use vertical rules in the same table as the effects will look 'incomplete' as you observed.

If that is how the table should look, do this:

\documentclass[a4paper]{article}
\usepackage{array}

\begin{document}
    \begin{table}
  \centering
    \begin{tabular}{r|ccc|ccc|}
      \cline{2-7}
          & \multicolumn{3}{c|}{\textbf{header 1}} & \multicolumn{3}{c|}{\textbf{header 2}} \\
          \cline{2-7}
          & \textbf{a} & \textbf{b} & \textbf{c} & \textbf{a} & \textbf{b} & \textbf{c} \\
          \cline{2-7}
    \textbf{row 1} & 1     & 1     & 1     & 1     & 1     & 1 \\
    \textbf{row 2} & 1     & 1     & 1     & 1     & 1     & 1 \\
    \textbf{row 3} & 1     & 1     & 1     & 1     & 1     & 1 \\
    \hline
    \textbf{row 4} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 5} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 6} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 7} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 8} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \textbf{row 9} & \multicolumn{3}{c|}{0} & \multicolumn{3}{c|}{0} \\
    \cline{2-7}
    \end{tabular}
  \caption{description\label{tab:somelabel}}
\end{table}
\end{document}

bog standard tabular

Otherwise, I think the best you can do is something like this:

enhanced booktabs tabular

\documentclass[a4paper]{article}
\usepackage{array,booktabs}

\begin{document}
  \begin{table}
    \centering
    \begin{tabular}{r*{6}{c}}
      \cmidrule{2-7}
      & \multicolumn{3}{c}{\textbf{header 1}} & \multicolumn{3}{c}{\textbf{header 2}} \\
      \cmidrule{2-7}
      & \textbf{a} & \textbf{b} & \textbf{c} & \textbf{a} & \textbf{b} & \textbf{c} \\
      \toprule
      \textbf{row 1} & 1     & 1     & 1     & 1     & 1     & 1 \\
      \textbf{row 2} & 1     & 1     & 1     & 1     & 1     & 1 \\
      \textbf{row 3} & 1     & 1     & 1     & 1     & 1     & 1 \\
      \midrule
      \textbf{row 4} & \multicolumn{3}{c}{0} & \multicolumn{3}{c}{0} \\
      \textbf{row 5} & \multicolumn{3}{c}{0} & \multicolumn{3}{c}{0} \\
      \textbf{row 6} & \multicolumn{3}{c}{0} & \multicolumn{3}{c}{0} \\
      \textbf{row 7} & \multicolumn{3}{c}{0} & \multicolumn{3}{c}{0} \\
      \textbf{row 8} & \multicolumn{3}{c}{0} & \multicolumn{3}{c}{0} \\
      \textbf{row 9} & \multicolumn{3}{c}{0} & \multicolumn{3}{c}{0} \\
      \bottomrule
    \end{tabular}
    \caption{description\label{tab:somelabel}}
  \end{table}
\end{document}
1
  • But if you want to use booktabs you should certainly not use this - use Werner's. Mine was just a close-as-you-can-get-trying-to-do-something-you-shouldn't.
    – cfr
    Commented Jan 4, 2015 at 4:02

You must log in to answer this question.

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