Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I wish to create a table that looks like the following:

+-------++--------------------------++--------------------------+
|       ||          Category A      ||         Category B       |
| Title ++--------+--------+--------++--------+--------+--------+
|       || Item 1 | Item 2 | Item 3 || Item 1 | Item 2 | Item 3 |
+-------++--------+--------+--------++--------+--------+--------+
|   X   || 1      | 2      | 3      || 1      | 2      | 3      |
+-------++--------+--------+--------++--------+--------+--------+
|   Y   || 1      | 2      | 3      || 1      | 2      | 3      |
+-------++--------+--------+--------++--------+--------+--------+

but I am having great difficulty in formatting it like this.

What latex code structures the headings like that shown above?

I have looked at the reference page here: http://en.wikibooks.org/wiki/LaTeX/Tables but despite my experimentations I was unable to obtain this effect.

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

Here is a duplicate of what you're after:

enter image description here

\documentclass{article}
\usepackage{multirow}% http://ctan.org/pkg/multirow
\begin{document}
\begin{tabular}{|c||l|l|l||l|l|l|}
  \hline
  \multirow{2}{*}{Title} & \multicolumn{3}{c||}{Category~A} & \multicolumn{3}{|c|}{Category~B} \\
  \cline{2-7}
  & Item~1 & Item~2 & Item~3 & Item~1 & Item~2 & Item~3 \\
  \hline
  $X$ & 1 & 2 & 3 & 1 & 2 & 3 \\
  \hline
  $Y$ & 1 & 2 & 3 & 1 & 2 & 3 \\
  \hline
\end{tabular}
\end{document}

Here is a consideration:

The booktabs package provides a really beautiful typesetting environment for tables, but enforces some strict rules to obtain them. One of these is to never, ever use vertical lines. The reason for this is that the columnar alignment of all the entries should visually provide some form of horizontal separation, therefore foregoing the need to add vertical lines (or making them somewhat superfluous). Your table, moulded into a booktabs environment might look something like this:

enter image description here

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{multirow}% http://ctan.org/pkg/multirow
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\begin{document}
\begin{tabular}{c@{\qquad}ccc@{\qquad}ccc}
  \toprule
  \multirow{2}{*}{\raisebox{-\heavyrulewidth}{Title}} & \multicolumn{3}{c}{Category~A} & \multicolumn{3}{c}{Category~B} \\
  \cmidrule{2-7}
  & Item~1 & Item~2 & Item~3 & Item~1 & Item~2 & Item~3 \\
  \midrule
  $X$ & 1 & 2 & 3 & 1 & 2 & 3 \\
  $Y$ & 1 & 2 & 3 & 1 & 2 & 3 \\
  \bottomrule
\end{tabular}
\end{document}

You'll notice the use of \toprule, \midrule (\cmidrule) and \bottomrule as complements to the traditional \hline and \cline commands.

I've modified the columns to be all centered c for visual appeal. Additionally, separating the Category headings I've inserted a \qquad separation between the respective columns. Since booktabs also add thicker-than-normal rules, graphicx was required to drop Title a little lower than what the regular multirow package provides with \multirow{2}{*}{...}. This drop was obtained using \raisebox{-\heavyrulewidth}{...}. \heavyrulewidth is a width (or length) definition from booktabs.

share|improve this answer
1  
I must say thank you for the explanation below the booktabs example. Many contributors give a solution but sometimes the code can be a bit esoteric to the non-expert, as can the examples in the manuals. Again, thanks for the clear precise expalantion of the \raisebox.. ps I tried to use the @werner at the beginning of this comment but it didn't work but it does here... –  Leeser Nov 4 '11 at 11:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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