19

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.

21

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};
  • Thank you very much. When I am trying to run this, the two cells are separated why? And is it possible to include note to the figure? – A. F. Dec 6 '13 at 10:07
  • 1
    @A.F.: I don't have clues about your problem. I used simple TikZ code (no CVS or strange libraries) so it is supposed to run everywhere. For the second question, yes, of course. The document here is a standalone one, but you are free to re-use the code in your real document including the picture into a table environment and to add a caption. – Claudio Fiandrino Dec 6 '13 at 10:13
  • The space between Factor 1 and Factor 2 and the cells under "Name 1" is huge. Can I reduce this? – A. F. Dec 6 '13 at 10:46
  • 2
    @A.F.: sorry, these comments seems to me a "do this for me" questions. For the space between rows, you can open the pgfmanual and look to the matrix environment: you will discover how to change row/column separator, even for just one row/column. For the table, see my edit. For the footnotes, see Footnotes in tables – Claudio Fiandrino Dec 6 '13 at 11:02
  • 1
    @A.F.: fine. I will add some more explanation. – Claudio Fiandrino Dec 6 '13 at 12:37
13

There is a package for typesetting strategic form games: sgame, by Martin Osborne.

Code

\documentclass{article}
\usepackage{sgame}

% \renewcommand\gamestretch{2} % double row height

\begin{document}
\begin{game}{2}{2}[\textbf{Name 2}][\textbf{Name 1}]
           & Factor 1 & Factor 2 \\
  Factor 1 & 5        & 4        \\
  Factor 2 & 2        & 2        
\end{game}
\quad
\begin{game}{1}{1}
    & \textbf{Name 3}  \\
    & 1 
\end{game}
\end{document}

Output

enter image description here

12

You could use \cline instructions and use \multicolumn{1}{c}{...} in the header row to suppress vertical lines. I've provided some visual formatting hooks (such as \quad and \lower in the MWE below; I trust you'll manage to figure out how to move the elements around some more using these hooks.

enter image description here

\documentclass[a4paper,12pt]{article}
\usepackage{graphicx,multirow}
\begin{document}
\begin{table}[h]
\renewcommand\arraystretch{2.5} % provide a bit taller rows
\centering
\caption{The Prisoner's Dilemma}
\begin{tabular}{ll | c | c |}
&\multicolumn{1}{c}{}&\multicolumn{2}{c}{\textbf{Name1}}\\[-2ex]
&\multicolumn{1}{c}{}
&\multicolumn{1}{c}{Factor1}&\multicolumn{1}{c}{Factor2}\\
\cline{3-4}
\multirow{2}{*}{\rotatebox{90}{\textbf{Name2}}}
&Factor1&5&4\\
\cline{3-4}
&Factor2&2&2\\
\cline{3-4}
\end{tabular}
\qquad
\lower3ex\hbox{\begin{tabular}{|c|}
\multicolumn{1}{c}{\textbf{Name3}}\\
\hline
5\\
\hline
\end{tabular}}
\end{table}
\end{document}

Addendum: If you have two column headers of different widths -- say, ExtremelyLongHeader and LilHeader-- and if the widths of these headers determine the overall column widths, you could equalize the column widths as follows:

  • In the preamble, insert the following instructions:

    \newcommand\LongestHeader{ExtremelyLongHeader} % replace "ExtremelyLongHeader" with actual header...
    \newlength\mylen
    \settowidth\mylen{\LongestHeader} % \mylen measures width of \LongestHeader
    
  • ITo set up the tabular environment's header, you'd type:

    \begin{tabular}{ll | c | c |}
    & \multicolumn{1}{c}{} &\multicolumn{2}{c}{\textbf{Name1}}\\[-2ex]
    & \multicolumn{1}{c}{}
    & \multicolumn{1}{c}{\LongestHeader}
    & \multicolumn{1}{c}{\parbox{\mylen}{\centering LilHeader}}\\ % replace "LilHeader" with real column header
    \cline{3-4}
    ...
    

    instead of just

    \begin{tabular}{ll | c | c |}
    & \multicolumn{1}{c}{}&\multicolumn{2}{c}{\textbf{Name1}}\\[-2ex]
    & \multicolumn{1}{c}{}
    & \multicolumn{1}{c}{ExtremelyLongHeader}
    & \multicolumn{1}{c}{LilHeader}\\
    \cline{3-4}
    ...
    

    The \parbox command serves to typeset a "paragraph box" of a prespecified width.

  • Hi Mico. Thank you for your answer. As you can see I would like to know, how I create the first table, where "Name 1" and "Name 2" exist and where I also have a small box to the right. Can you help me with that? – A. F. Dec 5 '13 at 23:58
  • Ah, I am sorry, that I didn't express me clear enough. :-) Yes, I would like the "Name 3" as well. – A. F. Dec 6 '13 at 0:01
  • btw. is it possible to make all sides in the same length (like a square)? – A. F. Dec 6 '13 at 0:03
  • 1
    @A.F. - See the revised addendum regarding your first question. On the second question: Given that the table only has four columns, one should probably write \multicolumn{4}{@{}l@{}}{\strut\footnotesize Note: Blah blah}. – Mico Dec 6 '13 at 10:48
  • 1
    @A.F. - I would like to suggest you post your latest follow-up comment as a new question, both because it's substantially unconnected to your original question and because more people will have a chance to see your question and provide answers if you set up a new posting. Also, be sure to state how the macro \specialcell is defined. – Mico Dec 6 '13 at 11: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.