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'm trying to make a table that looks like this:

               +--------------+
               |   Class 1    |
               +--------------+
               |   A  |   B   |
+---------+----+------+-------+
|         | C  |   a  |   b   |
| Class 2 +----+------+-------+
|         | D  |   c  |   d   |
+---------+----+------+-------+

There are many things wrong with my code. First is the way that the word span appears in front of the \multicolumn{2}{|c|}{{\textbf{Class 1}}} entry (which doesn't span the line). Second the line between C and D goes all the way through class 2. I don't know how to get rid of the lines in the top right. It's a real mess, what am I doing wrong? Apologies for the not-very specific question.

\begin{tabular}{|r|c||c|c|}
  \hline
  \multicolumn{2}{c}{} & \multicolumn{2}{|c|}{{\textbf{Class 1}}} \\
  \multicolumn{2}{c}{} & A & B \\
  \multirow{2}{*}{{\textbf{Class 2}}}
    & C & a & b \\
  \hline
    & D & c & d \\
  \hline
\end{tabular}
share|improve this question
3  
Lucas, you need to finalize your previous questions to increase your accept rate as 0% accept rate is not good. For each of your previous questions, please decide the best answer and give it a green check mark. –  Who is crazy first Jul 7 '12 at 8:36
    
2  
Cool, I will do –  Lucas Jul 7 '12 at 11:30

2 Answers 2

up vote 5 down vote accepted

I stole most of the answer from here , but I think this is what you want

\begin{tabular}{rc|c|c|}
\cline{3-4}
& & \multicolumn{2}{|c|}{{\textbf{Class 1}}} \\
\cline{3-4}
& & A & B \\
\hline
\multicolumn{1}{|c|}{\multirow{2}{*}{{\textbf{Class 2}}}}& C & a & b \\
\cline{2-4}
\multicolumn{1}{|c|}{}& D & c & d \\
\hline
\end{tabular}

Screenshot

share|improve this answer

If you are typesetting sort of Cayley table, then I think the approach by Rick de Groot is good. If it's more a text table, you might use the package booktabs to enhance the look your table a little, omitting the verical lines and adding some vertical space:

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{tabular}{rccc}
\toprule
  & & \multicolumn{2}{c}{{\textbf{Class 1}}}
\\\cmidrule{3-4}
  & & A & B
\\\midrule[\heavyrulewidth]
  \textbf{Class 2}
    & C & a & b
\\\cmidrule{2-4}
    & D & c & d
\\\bottomrule
\end{tabular}

\end{document}

Variant two: As Jake pointed out, this solution cannot be used with the multirow package to center Class 2. More tweaking using calc package can be done:

\documentclass{article}

\usepackage{booktabs}
\usepackage{calc}

\begin{document}

\begin{tabular}{rccc}
\toprule
  & & \multicolumn{2}{c}{{\textbf{Class 1}}}
\\\cmidrule{3-4}
  & & A & B
\\\midrule[\heavyrulewidth]
  \begingroup\makeatletter
  \setlength{\@tempdima}{\totalheightof{\strut}}
  \smash{\raisebox{-0.5\@tempdima-0.5\aboverulesep-0.5\belowrulesep-0.5\lightrulewidth}{%
  \textbf{Class 2}%
  }}
  \endgroup
    & C & a & b
\\\cmidrule{2-4}
    & D & c & d
\\\bottomrule
\end{tabular}

\end{document}

Result Variant Two

share|improve this answer
1  
Not what I was thinking. But it does look nicer –  Lucas Jul 7 '12 at 11:58

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.