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 have this code:

\documentclass{article}
\usepackage{booktabs}

\begin{document}
\begin{center}
    \begin{tabular}{c|lccc|c|}
    & \multicolumn{5}{c}{\textbf{Political Party Preference}}\\\toprule
    & & \textbf{Democrat} & \textbf{Republican} & \textbf{Independent} & \textbf{Total}\\\midrule
    \textbf{Gender} & \textbf{Male} & 11 & 7 & 2 & 20\\
    & \textbf{Female} & 7 & 8 & 1 & 16\\\midrule
    & \textbf{Total}  & 18 & 15 & 3 &  36\\\bottomrule
    \end{tabular}
    \end{center}

\end{document}

which gives

enter image description here

But I'd like to produce the table shown at:

http://www.education.com/study-help/article/inference-two-way-tables/

Any suggestions?

share|improve this question
1  
Hi David, Welcome to TeX.SE! :) I edited your post to include the images, and made your snippet into a complete MWE by adding the booktabs package. I also removed thanks and your name- the best way to thank folks is to upvote answers :) and your name appears in the bottom right of your question :) Welcome! –  cmhughes Sep 14 '12 at 18:42
add comment

2 Answers

I would suggest you not to use vertical rules at all (the booktabs package documentation has some arguments in favor of this recommendation); if you add vertical rules, they won't interact well with the booktabs package (as you've noticed) and trying to make the rules work will spoil the table layout:

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

\begin{document}

\begin{center}
\begin{tabular}{@{}c>{\bfseries}lcccc@{}}
\toprule
 & \multicolumn{5}{c}{\textbf{Political Party Preference}} \\
& & \textbf{Democrat} & \textbf{Republican} & \textbf{Independent} &   
  \textbf{Total} \\
\cmidrule(r){1-2}\cmidrule(lr){3-3}\cmidrule(lr){4-4}\cmidrule(lr){5-5}\cmidrule(l){6-6}
\textbf{Gender} & Male & 11 & 7 & 2 & 20 \\
& Female & 7 & 8 & 1 & 16\\
\midrule
& Total  & 18 & 15 & 3 &  36\\
\bottomrule
\end{tabular}
\end{center}

\end{document}

enter image description here

Or even like this:

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

\begin{document}

\begin{center}
\begin{tabular}{@{}>{\bfseries}lcccc@{}}
\toprule
Gender & \multicolumn{4}{c}{\textbf{Political Party Preference}} \\
\cmidrule(l){2-5}
& \textbf{Democrat} & \textbf{Republican} & \textbf{Independent} &   
  \textbf{Total} \\
\cmidrule(r){1-1}\cmidrule(lr){2-2}\cmidrule(lr){3-3}\cmidrule(lr){4-4}\cmidrule(l){5-5}
Male & 11 & 7 & 2 & 20 \\
Female & 7 & 8 & 1 & 16\\
\midrule
Total  & 18 & 15 & 3 &  36\\
\bottomrule
\end{tabular}
\end{center}

\end{document}

enter image description here

share|improve this answer
    
That looks nice! And I learned quite a bit. –  David Sep 14 '12 at 19:02
    
@user16203 I am glad to hear my answer helped. Please consider accepting it (different from up-voting it) after a sensible wait (perhaps another better answer can appear). In case of doubt, please see How do you accept an answer?. –  Gonzalo Medina Sep 14 '12 at 19:05
    
Best answer is added by @cv-radhakrishnan, exactly as the OP wanted. –  Noble P. Abraham Sep 15 '12 at 1:42
    
@NobleP.Abraham I guess that's up to the OP. –  Gonzalo Medina Sep 15 '12 at 1:45
    
@GonzaloMedina But the best answer was added later than your comment. How can one be sure, a better answer is not possible? A discussion here meta.tex.stackexchange.com/questions/2753/… –  Noble P. Abraham Sep 15 '12 at 1:52
show 2 more comments

@user16203 You might try this code:

 \documentclass{article}
 \usepackage{tabls,pxfonts}

 \begin{document}

 \begin{center}
 \begin{tabular}{c|lccc|c|}
 \multicolumn{1}{c}{} & \multicolumn{5}{c}{\textbf{Political Party Preference}} \\\cline{2-6}
                 & & \textsf{Democrat}  & \textsf{Republican} 
                 & \textsf{Independent} & \textsf{Total} \\ \cline{2-6}
 \multicolumn{1}{c|}{\textbf{Gender}}& \textbf{Male}   & 11 & 7  & 2 & 20 \\
            & \textbf{Female} & 7  & 8  & 1 & 16\\ \cline{2-6}
            & \textbf{Total}  & 18 & 15 & 3 & 36\\ \cline{2-6}
 \end{tabular}
 \end{center}

 \end{document}

Please see the output

share|improve this answer
    
Was wondering how to control \hline to get the desired output. Great answer, Thank you. By the way, why is pxfonts used? –  Noble P. Abraham Sep 15 '12 at 1:25
1  
pxfonts are not necessary at all, I have used to generate non-cmr looking graphic to upload. –  CV Radhakrishnan Sep 15 '12 at 5:08
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.