# creating a contingency table in booktabs-look [closed]

How to format the rules of a contingency table?

The table should still fit the look of my other tables, which are created with the booktabs package.

I'm using the booktabs package and normally following the rule to not use vertical rules, but in this case I need one.

Here is my code using one midrule and one vertical rule:

\documentclass{article}

\usepackage{booktabs}
\usepackage{threeparttable}

\begin{document}

\begin{table}[h!]
\begin{threeparttable}
\caption{name}
\begin{tabular}{c|ccccccc}
\toprule
& B & C & D & E & F & G & H\\
\midrule
A & 1 & 2 & 3 & 4 & 5 & 6 & 7\\
B &   & 9 & 0 & 1 & 2 & 3 & 4\\
C &   &   & 5 & 6 & 7 & 8 & 9\\
D &   &   &   & 0 & 1 & 2 & 3\\
E &   &   &   &   & 4 & 5 & 6\\
F &   &   &   &   &   & 7 & 8\\
G &   &   &   &   &   &   & 9\\
\bottomrule
\end{tabular}
\begin{tablenotes}\footnotesize
\item[] notes
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}


## closed as primarily opinion-based by Henri Menke, TeXnician, Stefan Pinnow, Zarko, percusseAug 15 '17 at 6:07

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

Two other possibilities:

\documentclass{article}
\usepackage[flushleft]{threeparttable}
\usepackage[table, svgnames]{xcolor}
\usepackage{boldline, cellspace}
\setlength\cellspacetoplimit{1.5ex}
\setlength\cellspacebottomlimit{1.3ex}
\renewcommand\TPTtagStyle{\itshape}

%% Define a few struts
%% (from code by Claudio Beccari in TeX and TUG News, Vol. 2, 1993)
\newcommand\Tstrut{\rule{0pt}{2.7ex}} % "top" strut
\newcommand\Bstrut{\rule[-1.1ex]{0pt}{0pt}} % "bottom" strut
\newcommand\TBstrut{\Tstrut\Bstrut} % "top and bottom" strut

\begin{document}

\begin{table}[h!]
\centering
\begin{threeparttable}
\caption{Name}
\begin{tabular}{cV{3}*{7}{Sc}V{1.8}}
& B\tnote{a} & C & D & E & F & G & H \\
\hlineB{3}
A & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
B & & 9 & 0 & 1 & 2 & 3 & 4 \\
C & & & 5 & 6 & 7 & 8 & 9 \\
D & & & & 0 & 1 & 2 & 3 \\
E & & & & & 4 & 5 & 6 \\
F & & & & & & 7 & 8 \\
G\tnote{b} & & & & & & & 9 \\
\clineB{1-8}{1.8}
\end{tabular}
\footnotesize
\begin{tablenotes}
\item[a] The letter B'' is very important.
\item[b] So is the letter G''.
\end{tablenotes}
\end{threeparttable}
\end{table}

\begin{table}[h!]
\centering
\begin{threeparttable}
\caption{Name}
\begin{tabular}{>{\columncolor{Gainsboro!50}}c!{\hspace{0.4em}}*{7}{Sc}|}
\rowcolor{Gainsboro!50} \cellcolor{Yellow!60!OrangeRed!32! } & B\tnote{a} & C & D & E & F & G & \multicolumn{1}{c}{H} \\
\noalign{\vspace{0.5ex}}
A & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
B & & 9 & 0 & 1 & 2 & 3 & 4 \\
C & & & 5 & 6 & 7 & 8 & 9 \\
D & & & & 0 & 1 & 2 & 3 \\
E & & & & & 4 & 5 & 6 \\
F & & & & & & 7 & 8 \\
G\tnote{b} & & & & & & & 9 \\
\cline{2-8}
\end{tabular}
\footnotesize
\begin{tablenotes}
\item[a] The letter B'' is very important.
\item[b] So is the letter G''.
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}


Update:

I propose some improvements to the tablenotes part of the O.P.'s solution, using the paraoption (with a small patch):

\documentclass[preview, border={10pt 10pt -180pt 10pt}]{standalone}

\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\TPT@doparanotes}{%
\hskip 1em\@plus .3em}{%
\hskip 0.75em\@plus .25em}
\makeatother

%% Define a few struts
%% (from code by Claudio Beccari in TeX and TUG News, Vol. 2, 1993)
\newcommand\Tstrut{\rule{0pt}{2.7ex}} % "top" strut

\begin{document}

\begin{table}[h!]
\begin{threeparttable}
\caption{Name}
\begin{tabular}{c|*{7}{c}}
\toprule
& B & C & D & E & F & G & H\\
\hline
A & 1 & 2 & 3 & 4 & 5 & 6 & 7\Tstrut\\
B & & 9 & 0 & 1 & 2 & 3 & 4\\
C & & & 5 & 6 & 7 & 8 & 9\\
D & & & & 0 & 1 & 2 & 3\\
E & & & & & 4 & 5 & 6\\
F & & & & & & 7 & 8\\
G & & & & & & & 9\\
\bottomrule
\end{tabular}
\begin{tablenotes}[para, online]\footnotesize%
\def\tnote#1{\protect\TPToverlap{\TPTtagStyle{#1}}}%
\item[A]--- Apple,
\item[B]--- Banana,
\item[C]--- Cherry,
\item[D]--- Date,
\item[E]--- Elderberry,
\item[F]--- Fig,
\item[G]--- Grape,
\item[H]--- Honeydew melon.
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}


Since vertical rules appear to be a necessity, I suggest you not use the macros of the booktabs package. To get adequate spacing above and below the horizontal rules created by \hline, I suggest you insert typographic struts, as is done in the following example.

\documentclass{article}
\usepackage[flushleft]{threeparttable}
\renewcommand\TPTtagStyle{\itshape}

%% Define a few struts
%% (from code by Claudio Beccari in TeX and TUG News, Vol. 2, 1993)
\newcommand\Tstrut{\rule{0pt}{2.7ex}} % "top" strut
\newcommand\Bstrut{\rule[-1.1ex]{0pt}{0pt}} % "bottom" strut
\newcommand\TBstrut{\Tstrut\Bstrut}   % "top and bottom" strut

\begin{document}

\begin{table}[h!]
\renewcommand\arraystretch{1.25}
\begin{threeparttable}
\caption{Name}
\begin{tabular}{|c|ccccccc|}
\hline
& B\tnote{a} & C & D & E & F & G & H\TBstrut\\
\hline
A & 1 & 2 & 3 & 4 & 5 & 6 & 7\Tstrut\\
B &   & 9 & 0 & 1 & 2 & 3 & 4\\
C &   &   & 5 & 6 & 7 & 8 & 9\\
D &   &   &   & 0 & 1 & 2 & 3\\
E &   &   &   &   & 4 & 5 & 6\\
F &   &   &   &   &   & 7 & 8\\
G\tnote{b} &   &   &   &   &   &   & 9\Bstrut\\
\hline
\end{tabular}
\footnotesize
\begin{tablenotes}
\item[a] The letter B'' is very important.
\item[b] So is the letter G''.
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}


I'm providing an own answer, using code that Mico and Bernard came of with.

I think this formatting looks nice, especially when it's used with other tables in booktabs-threeparttable-layout.

PS: I'm unsure, if the spacing is nice at every point.

\documentclass[preview, border={10pt 10pt -180pt 10pt}]{standalone}

\usepackage{booktabs}
\usepackage{threeparttable}

%% Define a few struts
%% (from code by Claudio Beccari in TeX and TUG News, Vol. 2, 1993)
\newcommand\Tstrut{\rule{0pt}{2.7ex}} % "top" strut

\begin{document}

\begin{table}[h!]
\begin{threeparttable}
\caption{Name}
\begin{tabular}{c|ccccccc}
\toprule
& B & C & D & E & F & G & H\\
\hline
A & 1 & 2 & 3 & 4 & 5 & 6 & 7\Tstrut\\
B &   & 9 & 0 & 1 & 2 & 3 & 4\\
C &   &   & 5 & 6 & 7 & 8 & 9\\
D &   &   &   & 0 & 1 & 2 & 3\\
E &   &   &   &   & 4 & 5 & 6\\
F &   &   &   &   &   & 7 & 8\\
G &   &   &   &   &   &   & 9\\
\bottomrule
\end{tabular}
\begin{tablenotes}\footnotesize
\item[] A --- Apple, B --- Banana, C --- Cherry, D --- Date, E --- Elderberry, F --- Fig, G --- Grape, H --- Honeydew melon
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}


• Nice, but I propose an improvement to the tablenotes part, based on the para option, so the notes are left-aligned. Please take a look at my updated answer. – Bernard Aug 14 '17 at 20:55