Tables – Typesetting a Prisoner’s Dilemma Table in LaTeX

multirowtables

I am going to create a table, which should look like The Prisoner's Dilemma, but I can't create it.

It should look like this:
enter image description here

My preamble look like this:

\documentclass[a4paper,12pt,oneside]{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{shapes,arrows,positioning,calc} 
\usepackage{tabu}
\usepackage{hyperref}
\usepackage{longtable}
\usepackage{array}
\usepackage{siunitx,multirow,tabularx,booktabs}
\renewcommand{\arraystretch}{1.5}
\def\mathbi#1{\textbf{\em #1}}
\newcommand{\transp}{^{\mathsf{T}}}

\begin{document}

\begin{table}[h]
     \begin{center}
      \caption{The Prisoner’s Dilemma}
     \begin{tabular}{c | c | c |}
     &Factor1&Defect\\\hline%
     Factor2&5&4\\
     Defect&2&2\\\hline
     \end{tabular}
     \end{center}
     \end{table}

\end{document}

And the table look like this:

enter image description here

Can someone tell me how to make the first table with AND without the extra box? The cells should be the same size.

Best Answer

With TikZ it is far simpler:

enter image description here

The code:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}

\begin{document}

\begin{tikzpicture}[element/.style={minimum width=1.75cm,minimum height=0.85cm}]
\matrix (m) [matrix of nodes,nodes={element},column sep=-\pgflinewidth, row sep=-\pgflinewidth,]{
         & Factor 1  & Factor 2  \\
Factor 1 & |[draw]|5 & |[draw]|4 \\
Factor 2 & |[draw]|2 & |[draw]|2 \\    };

\node[draw,element, anchor=west,label={above:\textbf{Name 3}}] at ($(m-2-3)!0.5!(m-3-3)+(1.25,0)$) {5}; % setting the node midway to cell 4 and cell 2 with a horizontal shift of 1.25cm

\node[above=0.25cm] at ($(m-1-2)!0.5!(m-1-3)$){\textbf{Name 1}};
\node[rotate=90] at ($(m-2-1)!0.5!(m-3-1)+(-1.25,0)$){\textbf{Name 2}};
\end{tikzpicture}

\end{document}

The version within article

\documentclass[11pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}

\usepackage{lipsum}

\begin{document}
\lipsum[1]

\begin{table}[h]
\centering
\caption{bla bla bla}
\label{bla}
\begin{tikzpicture}[element/.style={minimum width=1.75cm,minimum height=0.85cm}]
\matrix (m) [matrix of nodes,nodes={element},column sep=-\pgflinewidth, row sep=-\pgflinewidth,]{
         & Factor 1  & Factor 2  \\
Factor 1 & |[draw]|5 & |[draw]|4 \\
Factor 2 & |[draw]|2 & |[draw]|2 \\    };

\node[draw,element, anchor=west,label={above:\textbf{Name 3}}] at ($(m-2-3)!0.5!(m-3-3)+(1.25,0)$) {5}; % setting the node midway to cell 4 and cell 2 with a horizontal shift of 1.25cm

\node[above=0.25cm] at ($(m-1-2)!0.5!(m-1-3)$){\textbf{Name 1}};
\node[rotate=90] at ($(m-2-1)!0.5!(m-3-1)+(-1.25,0)$){\textbf{Name 2}};
\end{tikzpicture}
\end{table}

\lipsum[2]

\end{document}

The result:

enter image description here

Explanation

A TikZ matrix is simply a node and as every node some anchors become available: incidentally this helps a lot in positioning other elements in the picture.

The first step is to give a name to the matrix: it is done through the syntax \matrix (m) where m is the name. Using the option matrix of nodes then also each cell has a name in the form <matrix name>-<row>-<column>.

Since we have a name we can access anchors as <matrix name>.<anchor>. But we have to know where these anchors are located. For debugging purposes we can add this code right after the previous \matrix definition:

% debugging purposes
\draw[red](m.south west)rectangle(m.north east);

\foreach \anchor/\placement in
    {north/above,  south/below, east/right, west/left,
     north west/above, north east/above,
     south west/below, south east/below}
     \draw[shift=(m.\anchor)] plot[mark=x] coordinates{(0,0)} node[\placement=0.15cm]{\scriptsize\texttt{(m.\anchor)}};

Now the table looks like:

enter image description here

In the picture there are two notes: they are added with

% Where to put the notes according to what we learnt about matrices
\node[below=0.5cm] at (m.south west) {Note};
\node[below=0.5cm, align=center,text width=5cm] at (m.south) {Another Note which is supposed to be longer};
Related Question