2

How do I make a table like this in LaTeX?

enter image description here

  • 1
    Your question leaves all the effort to our community, even typing the essentials of a TeX document such as \documentclass{}...\begin{document} etc. As it is, most of our users will be very reluctant to touch your question, and you are left to the mercy of our procrastination team who are very few in number and very picky about selecting questions. You can improve your question by adding a minimal working example (MWE) that more users can copy/paste onto their systems to work on. If no hero takes the challenge we might have to close your question. – Adam Liter Sep 1 '14 at 4:37
  • And welcome to TeX.SX! – Adam Liter Sep 1 '14 at 4:37
  • Sorry about that Adam, didn't know that was protocol here but now I understand. I understand now how it can help a lot and will do so in the future. – shane Sep 1 '14 at 8:32
2

The command \cline gets used extensively in the code below. Unlike \hline, \cline needn't span the full width of a tabular environment. I also use \multirow (from the multirow package) to center "AB" between two rows.

enter image description here

\documentclass{article}
\usepackage{multirow}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\begin{document}
\centering
\begin{tabular}{cc|c|c|c|c|}
& \mc{}& \multicolumn{4}{c}{\textit{CD}}\\
&\mc{}& \mc{00} & \mc{01} & \mc{11} & \mc{10} \\
\cline{3-6}
& 00 & & & & \\
\cline{3-6}
\multirow{2}{*}{\textit{AB}} & 01 & & & & \\
\cline{3-6}
& 11 & & & & \\
\cline{3-6}
& 10 & & & & \\
\cline{3-6}
\end{tabular}
\end{document}

Addendum, to address the OP's follow-up question about how to achieve better vertical centering. Vertical centering of the cell contents is actually not a simple criterion, as it depends importantly on the cells' contents. Do the cells in your table contain mostly lowercase letters without ascenders or descenders, or do they contain a mix of uppercase and lowercase letters? If it's the latter, loading the array package and adding the instruction \setlength\extrarowheight{3pt} may satisfy your needs; see the first example below. Note that while vertical alignment of the cells in rows 2 and 3 looks roughly correct, there's now too much whitespace above the "ace" element (which has no ascenders) in row 1 and simultaneously not enough whitespace below the "pqy" element in row 4 (which has several descenders). Getting "perfect" vertical alignment for each and every row turns out to be far more involved than one might have thought to be the case.

However, the need for better vertical centering would never arise if your table didn't contain so many horizontal and vertical lines. If you can get yourself to get rid of the three middle horizontal lines, replace the two remaining \cline instructions with \cmidrule instructions, and also get rid of all vertical lines, you'll find that vertical alignment quickly becomes a non-issue. This is illustrated in the second example below.

enter image description here

\documentclass{article}
\usepackage{multirow}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\usepackage{array}    % for "\extrarowheight" macro
\usepackage{booktabs} % for "\cmidrule" macro
\begin{document}

\begingroup
\setlength\extrarowheight{3pt}
\begin{tabular}{cc|c|c|c|c|}
& \mc{}& \multicolumn{4}{c}{\textit{CD}}\\
&\mc{}& \mc{00} & \mc{01} & \mc{11} & \mc{10} \\
\cline{3-6}
& 00 &  ace & & & \\
\cline{3-6}
\multirow{2}{*}{\textit{AB}} & 01 & & ABC & & \\
\cline{3-6}
& 11 & & & 123 & \\
\cline{3-6}
& 10 & & & & pqy\\
\cline{3-6}
\end{tabular}
\endgroup

\bigskip
\begin{tabular}{cccccc} % note: no vertical bars!
& & \multicolumn{4}{c}{\textit{CD}}\\
& & 00 & 01 & 11 & 10 \\  % and no need for \mc{...} macro either!
\cmidrule[\heavyrulewidth]{3-6}
& 00 &  ace & & & \\
\multirow{2}{*}{\textit{AB}} & 01 & & ABC & & \\
& 11 & & & 123 & \\
& 10 & & & & pqy\\
\cmidrule[\heavyrulewidth]{3-6}
\end{tabular}
\end{document}
  • Thank you for the help! I've got one last question - how do you center items in cells vertically? I've looked around and can't find out how to do that. Thanks again. – shane Sep 1 '14 at 8:32
  • @sbarratt - Please see the addendum to my answer for a discussion of how one might go about centering the items in the rows vertically. – Mico Sep 1 '14 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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