% MACRO FOR CREATING bimatrix games
% bimatrixgame.sty version 1.1 
% (c) 2012 Bernhard von Stengel,  stengel@nash.lse.ac.uk
% requires  \usepackage{color}
%
% example usage:
%
% \bimatrixgame{4mm}{3}{4}{I}{II}{TMB}{lcr{\it out}}
% {
% \payoffpairs{1}{00{$a^2$}0}{1{\fbox 3}{\fbox 3}2}
% \payoffpairs{2}{0000}{1111}
% \singlepayoffs{3}{5555}
% % \multiput(0,-0.13)(.16,-.16){75}{\tiny.} 
% } 
%
% The following can be changed with \renewcommand 
% before the \bimatrixgame macro, as in e.g.
%  \renewcommand{\bimatrixrowcolor}{Red}
%  \renewcommand{\bimatrixcolumncolor}{Blue}
%
\newcommand{\bimatrixdiag}{2}
\newcommand{\bimatrixpairfont}{\normalsize}
\newcommand{\bimatrixsinglefont}{\large}
\definecolor{Black}{rgb}{0,0,0}
\definecolor{Blue}{rgb}{0,0,1}
\definecolor{Red}{rgb}{1,0,0}
\newcommand{\bimatrixrowcolor}{Black}
\newcommand{\bimatrixcolumncolor}{Black}
%
\newcount\bimarows
\newcount\bimacols
\newcount\bimarowcoord
\newcount\bimacolcoord
\newcount\bimam
\newcount\biman
% the crucial variable-length-parameter macro \dosth
\newcommand{\dosth}[1]{\ifx###1##\else\dofirst#1\anytoken\fi}
%\def\dosth#1{\ifx###1##\else\dofirst#1\emptyset\fi}
\def\doagain#1\anytoken{\dosth{#1}}
% example of \dofirst
% \def\dofirst#1{{$\langle#1\rangle$}\doagain} 
% example of \dosth
% \dosth{1234x{x^3}y}
\def\payoffpairs#1#2#3{\bimam=#1\multiply\bimam by 4\advance\bimam by -1 \biman=1
  \def\dofirst##1{\put(\biman,-\bimam){\makebox(0,0){\color
  {\bimatrixrowcolor}\strut##1}}\advance\biman by 4\doagain}%
  \bimatrixpairfont\dosth{#2\strut}%
  \def\dofirst##1{\put(\biman,-\bimam){\makebox(0,0){\color
  {\bimatrixcolumncolor}\strut##1}}\advance\biman by 4\doagain}%
  \bimam=#1\multiply\bimam by 4 \advance\bimam by -3\biman=3
  \bimatrixpairfont\dosth{#3\strut}}
\def\singlepayoffs#1#2{\bimam=#1\multiply\bimam by 4\advance\bimam by -2\biman=2
  \def\dofirst##1{\put(\biman,-\bimam){\makebox(0,0){\color{\bimatrixrowcolor}\strut##1}}\advance\biman by 4\doagain}%
{\bimatrixsinglefont\dosth{#2\strut}}}
% the bimatrix game command
\newcommand{\bimatrixgame}[8]{%
\setlength{\unitlength}{#1}%
\bimarows=#2
\bimacols=#3
\bimarowcoord=\bimarows
\bimacolcoord=\bimacols
\multiply\bimarowcoord by 4
\multiply\bimacolcoord by 4
\bimam=\bimarowcoord
\biman=\bimacolcoord
\advance\bimam by 2 % 2 units left of payoff table
\advance\biman by 2 % 2 units above payoff table
\begin{picture}(\biman,\bimam)(-2,-\bimarowcoord)
\bimam=\bimarows
\biman=\bimacols
\advance\bimam by 1
\advance\biman by 1 
\thinlines
\multiput(0,0)(0,-4){\bimam}{\line(1,0){\bimacolcoord}}
\multiput(0,0)(4,0){\biman}{\line(0,-1){\bimarowcoord}}
\put(0,0){\line(-1,1){\bimatrixdiag}}
\put(-1.5,0.5){\makebox(0,0)[r]{\color{\bimatrixrowcolor}#4}}  % name player I
\put(-.7,1.7){\makebox(0,0)[l]{\color{\bimatrixcolumncolor}#5}}   % name player II
%row annotations - even with long strategy names, stick out to the left
\biman=2
\def\dofirst##1{\put(-0.8,-\biman){\makebox(0,0)[r]{\color
   {\bimatrixrowcolor}\strut##1}}\advance\biman by 4\doagain}
\dosth{#6\strut} 
%column annotations
\biman=2
\def\dofirst##1{\put(\biman,1.0){\makebox(0,0){\color
   {\bimatrixcolumncolor}\strut##1}}\advance\biman by 4\doagain}
\dosth{#7\strut}#8%
\end{picture}}
