Tikz 收益矩阵

Tikz 收益矩阵

我正在尝试完全移除底部两行(C 和 D),以便创建一个 2*4 的收益矩阵。除此之外,我还想在移除“玩家 2”后将其稍微向上移动(大约在 A 和 B 之间)。我不知道该怎么做,想知道是否有人可以帮忙?

在此处输入图片描述

我希望它看起来或多或少是这样的(“玩家 2” 重新定位到 A 和 B 的中间):

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\usepackage{natbib}
\begin{document}
\begin{center}\begin{tikzpicture}
\def\myrowhead{{"T","F","G","T"}}
\def\mycolhead{{"A","B","C","D"}}

\matrix[matrix of math nodes,draw,
every odd row/.style={align=right},every evenrow/.style={align=left},
nodes={text width=1.5cm},row sep=0.2cm,column sep=0.2cm]
          (m) {2&3&6&5\\12&2&9&9\\-1&0&0&2\\0&0&0&3\\2&3&6&1\\4&2&-1&8\\4&2&4&8\\4&2&-1&7\\};
\foreach\x[count=\xi from 2,evaluate={\xx=int(2*\x);\xxi=int(\xx+1)}] in {1,2,3}{
  \draw ({$(m-1-\x)!0.5!(m-1-\xi)$}|-m.north) -- ({$(m-1-\x)!0.5!(m-1-\xi)$}|-m.south);
  \draw ({$(m-\xx-1)!0.5!(m-\xxi-1)$}-|m.west) -- ({$(m-\xx-1)!0.5!(m-\xxi-1)$}-|m.east);
}
\foreach\x in{0,1,2,3}{
  \node[text depth=0.25ex,above=2mm] at ($(m.north west)!{(2*\x+1)/8}!(m.north east)$)
     {\pgfmathparse{\myrowhead[\x]}\pgfmathresult};
  \node[left=2mm] at ($(m.north west)!{(2*\x+1)/8}!(m.south west)$)
     {\pgfmathparse{\mycolhead[\x]}\pgfmathresult};
}

\node[above=18pt of m.north] (firm b) {Player 1};
\node[left=1.6cm of m.west,align=center,anchor=center] {Player 2};
\end{tikzpicture}
\end{center}
\end{document}

答案1

我尝试保留您提供的代码框架。我可能更改的唯一内容是其中的两个空条目\def\mycolhead{{"A","B","",""}}(但它很好地完成了工作 :))。

较新的答案:

更新输出

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\usepackage{natbib}
\begin{document}
\begin{center}\begin{tikzpicture}
\def\myrowhead{{"T","F","G","T"}}
\def\mycolhead{{"A","B","",""}}
\matrix[matrix of math nodes, 
            draw, every odd row/.style={align=right},
            every evenrow/.style={align=left}, 
            nodes={text width=1.5cm},
            row sep=0.2cm,column sep=0.2cm]
          (m) {
            2&3&6&5\\
            12&2&9&9\\
            -1&0&0&2\\
            0&0&0&3\\
            };
% Horizontal line
\draw ({$(m-2-1)!0.5!(m-3-1)$}-|m.west) -- ({$(m-2-1)!0.5!(m-3-1)$}-|m.east);
% Vertical lines
\foreach\x[count=\xi from 2,evaluate={\xx=int(2*\x);\xxi=int(\xx+1)}] in {1,...,3}{
  \draw ({$(m-1-\x)!0.5!(m-1-\xi)$}|-m.north) -- ({$(m-1-\x)!0.5!(m-1-\xi)$}|-m.south);
}

\foreach\x in{0,1,2,3}{
  \node[text depth=0.25ex,above=2mm] at ($(m.north west)!{(2*\x+1)/8}!(m.north east)$)
     {\pgfmathparse{\myrowhead[\x]}\pgfmathresult};
  \node[left=2mm] at ($(m.north west)!{(2*\x+1)/4}!(m.south west)$)
     {\pgfmathparse{\mycolhead[\x]}\pgfmathresult};
}
\node[above=18pt of m.north] (firm b) {Player 1};
\node[left=1.6cm of m.west,align=center,anchor=center] {Player 2};
\end{tikzpicture}
\end{center}
\end{document}

上一个答案:

输出

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\usepackage{natbib}
\begin{document}
\begin{center}\begin{tikzpicture}
\def\myrowhead{{"T","F","G","T"}}
\def\mycolhead{{"A","B","",""}}
\matrix[matrix of math nodes, 
        draw, every odd row/.style={align=right},
        every evenrow/.style={align=left}, 
        nodes={text width=1.5cm},
        row sep=0.2cm,column sep=0.2cm]
        (m){
            2&3&6&5\\
            12&2&9&9\\
            -1&0&0&2\\
            0&0&0&3\\
          };
\foreach\x[count=\xi from 2,evaluate={\xx=int(2*\x);\xxi=int(\xx+1)}] in {1}{
  \draw ({$(m-1-\x)!0.5!(m-1-\xi)$}|-m.north) -- ({$(m-1-\x)!0.5!(m-1-\xi)$}|-m.south);
  \draw ({$(m-\xx-1)!0.5!(m-\xxi-1)$}-|m.west) -- ({$(m-\xx-1)!0.5!(m-\xxi-1)$}-|m.east);
}
\foreach\x in{0,1,2,3}{
  \node[text depth=0.25ex,above=2mm] at ($(m.north west)!{(2*\x+1)/8}!(m.north east)$)
     {\pgfmathparse{\myrowhead[\x]}\pgfmathresult};
  \node[left=2mm] at ($(m.north west)!{(2*\x+1)/4}!(m.south west)$)
     {\pgfmathparse{\mycolhead[\x]}\pgfmathresult};
}
\node[above=18pt of m.north] (firm b) {Player 1};
\node[left=1.6cm of m.west,align=center,anchor=center] {Player 2};
\end{tikzpicture}
\end{center}
\end{document}

答案2

只是为了证明你不需要 TiZ 矩阵来绘制你的表格,一些嵌套的tabular就足够了。

为了方便起见,我使用了一个宏来处理几个收益,就像 marmot 的回答一样。

\documentclass[12pt]{article} 
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcommand{\pft}[2]{\begin{tabular}{@{}C{1em}@{\hspace{1.5em}}C{1em}@{}} \rule{0pt}{3ex} & #1 \\[1ex] #2 & \\[1ex] \end{tabular}} 
\newcommand{\myh}[2]{\multicolumn{1}{C{#1}}{#2}} 
\begin{document} 
\begin{center} 
\begin{tabular}{c@{}C{1.5em}|*4{C{4em}}} 
  & \myh{1.5em}{} & \multicolumn{4}{c}{Player 1} \\[1ex] 
  & \myh{1.5em}{} & \myh{4em}{T} & \myh{4em}{F} & \myh{4em}{G} & \myh{4em}{T} \\[1ex] 
  Player 2 & 
  \multicolumn{5}{c}{% 
    \begin{tabular}{@{}C{1.5em}|*4{C{4em}|}@{}} 
      \cline{2-5} 
      A & \pft{2}{12} & \pft{3}{2} & \pft{6}{9} & \pft{5}{9} \\ 
      \cline{2-5} 
      B & \pft{-1}{0} & \pft{0}{0} & \pft{0}{0} & \pft{2}{3} \\ 
      \cline{2-5} 
    \end{tabular}} 
\end{tabular} 
\end{center} 
\end{document}

在此处输入图片描述

如果您喜欢“玩家 2”旋转,您可以使用带有正确对齐选项的\rotatebox包:graphicxorigin=c

\documentclass[12pt]{article}
\usepackage{graphicx} 
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcommand{\pft}[2]{\begin{tabular}{@{}C{1em}@{\hspace{1.5em}}C{1em}@{}} \rule{0pt}{3ex} & #1 \\[1ex] #2 & \\[1ex] \end{tabular}} 
\newcommand{\myh}[2]{\multicolumn{1}{C{#1}}{#2}} 
\begin{document} 
\begin{center} 
\begin{tabular}{c@{}C{1.5em}|*4{C{4em}}} 
  & \myh{1.5em}{} & \multicolumn{4}{c}{Player 1} \\[1ex] 
  & \myh{1.5em}{} & \myh{4em}{T} & \myh{4em}{F} & \myh{4em}{G} & \myh{4em}{T} \\[1ex] 
  \rotatebox[origin=c]{90}{Player 2} & 
  \multicolumn{5}{c}{% 
    \begin{tabular}{@{}C{1.5em}|*4{C{4em}|}@{}} 
      \cline{2-5} 
      A & \pft{2}{12} & \pft{3}{2} & \pft{6}{9} & \pft{5}{9} \\ 
      \cline{2-5} 
      B & \pft{-1}{0} & \pft{0}{0} & \pft{0}{0} & \pft{2}{3} \\ 
      \cline{2-5} 
    \end{tabular}} 
\end{tabular} 
\end{center} 
\end{document}

在此处输入图片描述

答案3

这个答案带有一种风格payoff matrix,可以为你完成所有工作。你只需要说

\matrix [payoff matrix]{& T & F & G & T\\
 A & \pft{2}{12}  & \pft{3}{2} & \pft{6}{9} & \pft{5}{9}  \\
 B & \pft{-1}{0}  & \pft{0}{0} & \pft{0}{0} & \pft{2}{3}  \\
};

其中\pft有两个参数,单元格右上角和左下角的条目。球员的名字存储在 pgf 键中,因此您可以轻松更改它们。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}
\tikzset{payoff matrix/.style={matrix of nodes,column sep=-\pgflinewidth,row sep=-\pgflinewidth,
 nodes={/utils/exec=\ifnum\the\pgfmatrixcurrentrow>1
  \ifnum\the\pgfmatrixcurrentcolumn>1
  \tikzset{
  text height=1.2em,text width=4em,draw}
  \fi
 \fi,
 align=center,anchor=center},
 column 1/.style={text width=1em},
 row 1/.style={text depth=0.3em},
 execute at end matrix={%
 \path (\tikzmatrixname-2-2.west) -- (\tikzmatrixname-2-\the\pgfmatrixcurrentcolumn.east)
 coordinate[midway] (aux) 
 node[anchor=south,draw=none] at (\tikzmatrixname.north-|aux){\pgfkeysvalueof{/tikz/payoff
 matrix/player 1}};
 \path (\tikzmatrixname-2-2.north) -- 
 (\tikzmatrixname-\the\pgfmatrixcurrentrow-2.south)
 coordinate[midway] (aux) 
 node[anchor=south,draw=none,rotate=90] at (aux-|\tikzmatrixname.west){\pgfkeysvalueof{/tikz/payoff
 matrix/player 2}};}},
 payoff matrix/.cd,player 1/.initial={Player 1},player 2/.initial={Player 2}}
\begin{document}
\begin{tikzpicture}
\newcommand{\pft}[2]{{\hfill$#1$ \\ $#2$\hfill\mbox{}}}
\matrix [payoff matrix]{& T & F & G & T\\
A & \pft{2}{12}  & \pft{3}{2} & \pft{6}{9} & \pft{5}{9}  \\
B & \pft{-1}{0}  & \pft{0}{0} & \pft{0}{0} & \pft{2}{3}  \\
};
\end{tikzpicture}
\end{document}

在此处输入图片描述

这就是为什么我相信使用 Ti这里是 Z:添加一些功能很简单(而且Player 2旋转和居中也毫不费力)。(当然,我同意,如果你不想自由添加功能,也不想旋转Player 2,那么普通的表格就可以了。)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}
\tikzset{payoff matrix/.style={matrix of nodes,column sep=-\pgflinewidth,row sep=-\pgflinewidth,
 nodes={/utils/exec=\ifnum\the\pgfmatrixcurrentrow>1
  \ifnum\the\pgfmatrixcurrentcolumn>1
  \tikzset{
  text height=1.2em,text width=4em,draw,
  path picture={\fill[blue!20] (path picture bounding box.north west) -|
   (path picture bounding box.south east);
  \fill[red!20] (path picture bounding box.north west) |-
   (path picture bounding box.south east);}}
  \fi
 \fi,
 align=center,anchor=center},
 column 1/.style={text width=1em},
 row 1/.style={text depth=0.3em},
 execute at end matrix={%
 \path (\tikzmatrixname-2-2.west) -- (\tikzmatrixname-2-\the\pgfmatrixcurrentcolumn.east)
 coordinate[midway] (aux) 
 node[anchor=south,draw=none,path picture={}] at (\tikzmatrixname.north-|aux){\pgfkeysvalueof{/tikz/payoff
 matrix/player 1}};
 \path (\tikzmatrixname-2-2.north) -- 
 (\tikzmatrixname-\the\pgfmatrixcurrentrow-2.south)
 coordinate[midway] (aux) 
 node[anchor=south,draw=none,path picture={},rotate=90] at (aux-|\tikzmatrixname.west){\pgfkeysvalueof{/tikz/payoff
 matrix/player 2}};}},
 payoff matrix/.cd,player 1/.initial={Player 1},player 2/.initial={Player 2}}
\begin{document}
\begin{tikzpicture}
\newcommand{\pft}[2]{{\hfill$#1$ \\ $#2$\hfill\mbox{}}}
\matrix [payoff matrix]{& T & F & G & T\\
A & \pft{2}{12}  & \pft{3}{2} & \pft{6}{9} & \pft{5}{9}  \\
B & \pft{-1}{0}  & \pft{0}{0} & \pft{0}{0} & \pft{2}{3}  \\
};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容