有没有与数组兼容的 sgame 替代品?

有没有与数组兼容的 sgame 替代品?

正如所指出的Christian Hupfer 的回答,该包sgame与 不兼容array。在博弈论中,是否有更宽容的排版收益矩阵的替代方案?

MNWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{array}
\usepackage{sgame}

\begin{document}

\begin{game}{2}{2}[You][Your partner][Prisoner's Dilemma]
    & Talk      & Quiet \\
    Talk  & $-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ 
\end{game}

\end{document}

正如预期的那样,产生了错误:

! Missing # inserted in alignment preamble.
<to be read again> 
                   \cr 
l.26 ...{2}[You][Your partner][Prisoner's Dilemma]

?

请注意,我意识到我可以通过多种方式做到这一点,而无需专用包。我问的是是否有人已经发明了我想要的那种轮子。

答案1

你是指这样的事情吗?

\documentclass{article}
\usepackage{xparse,hhline,array,multirow}
\usepackage[table]{xcolor} % necessary only for \cellcolor in the last example

\newcommand\doubleactivetilde{~~} % hhline wants ~ to mean 'column with no line'

\ExplSyntaxOn

\seq_new:N \l_cfr_game_body_seq
\seq_new:N \l_cfr_game_first_seq
\tl_new:N \l_cfr_game_first_tl
\cs_generate_variant:Nn \seq_set_split:Nnn { NnV }

\cs_new_protected:Nn \__cfr_game_first:n
 {
  & \multicolumn{1}{c}{#1}
 }

\NewDocumentEnvironment{game}{mmooob}
 {% #1 = number of rows, #2 = number of columns (besides the label column)
  % #3 = left label, #4 = top label, #5 = bottom label
  % #6 = body

  % split the body at \\
  \seq_set_split:Nnn \l_cfr_game_body_seq { \\ } { #6 }
  % in case there's a trailing \\
  \seq_pop_right:NN \l_cfr_game_body_seq \l_tmpa_tl
  \tl_if_empty:NF \l_tmpa_tl { \seq_put_right:NV \l_cfr_game_body_seq \l_tmpa_tl }
  %
  \seq_pop_left:NN \l_cfr_game_body_seq \l_cfr_game_first_tl
  \seq_set_split:NnV \l_cfr_game_first_seq { & } \l_cfr_game_first_tl
  % discard the first (empty) item
  \seq_pop_left:NN \l_cfr_game_first_seq \l_tmpa_tl
  \begin{tabular}{@{}c@{}r*{#2}{|c}|}
  % top label
  \IfValueT{#4}{\multicolumn{2}{@{}c}{} & \multicolumn{#2}{c}{#4} \\}
  % first row
  \multicolumn{2}{c}{} \seq_map_function:NN \l_cfr_game_first_seq \__cfr_game_first:n \\
  \exp_args:No \hhline{\doubleactivetilde*{#2}{|-}|}
  \IfValueTF{#3}{\multirow{#1}{*}{#3\quad}}{\multicolumn{1}{@{}c@{}}{}}
  & \seq_use:Nn \l_cfr_game_body_seq
   { \\ \exp_args:No \hhline{\doubleactivetilde*{#2}{|-}|} & } \\
  \exp_args:No \hhline{\doubleactivetilde*{#2}{|-}|}
  \IfValueT{#5}
   {
    \multicolumn{2}{c}{\rule{0pt}{1.25\normalbaselineskip}} &
    \multicolumn{#2}{c}{\makebox[0pt]{#5}} \\
   }
  \end{tabular}
}{}

\ExplSyntaxOff

\begin{document}

\begin{game}{2}{2}[You][Your partner][Prisoner's Dilemma]
    & Talk      & Quiet \\
    Talk  & $-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\bigskip
\hrule
\bigskip

\begin{game}{2}{2}[You][Your partner]
    & Talk      & Quiet \\
    Talk  & $-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\bigskip
\hrule
\bigskip

\begin{game}{2}{2}[You]
    & Talk      & Quiet \\
    Talk  & $-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\bigskip
\hrule
\bigskip

\begin{game}{2}{2}
    & Talk      & Quiet \\
    Talk  &\cellcolor{yellow}$-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\end{document}

在此处输入图片描述

修改后的版本使列宽相等。

\documentclass{article}
\usepackage{xparse,hhline,array,multirow}
\usepackage[table]{xcolor} % necessary only for \cellcolor in the last example

\newcommand\doubleactivetilde{~~} % hhline wants ~ to mean 'column with no line'

\ExplSyntaxOn

\seq_new:N \l_cfr_game_body_seq
\seq_new:N \l_cfr_game_first_seq
\tl_new:N \l_cfr_game_first_tl
\dim_new:N \l__cfr_game_dim
\cs_generate_variant:Nn \seq_set_split:Nnn { NnV }

\cs_new_protected:Nn \__cfr_game_first:n
 {
  & \multicolumn{1}{c}{#1}
 }

\NewDocumentEnvironment{game}{mmooob}
 {% #1 = number of rows, #2 = number of columns (besides the label column)
  % #3 = left label, #4 = top label, #5 = bottom label
  % #6 = body

  % split the body at \\
  \seq_set_split:Nnn \l_cfr_game_body_seq { \\ } { #6 }
  % in case there's a trailing \\
  \seq_pop_right:NN \l_cfr_game_body_seq \l_tmpa_tl
  \tl_if_empty:NF \l_tmpa_tl { \seq_put_right:NV \l_cfr_game_body_seq \l_tmpa_tl }
  %
  \seq_pop_left:NN \l_cfr_game_body_seq \l_cfr_game_first_tl
  \seq_set_split:NnV \l_cfr_game_first_seq { & } \l_cfr_game_first_tl
  % discard the first (empty) item
  \seq_pop_left:NN \l_cfr_game_first_seq \l_tmpa_tl
  \__cfr_game_equalize_columns:
  \begin{tabular}{@{}c@{}r|*{#2}{w{c}{\l__cfr_game_dim}|}}
  % top label
  \IfValueT{#4}{\multicolumn{2}{@{}c}{} & \multicolumn{#2}{c}{#4} \\}
  % first row
  \multicolumn{2}{c}{} \seq_map_function:NN \l_cfr_game_first_seq \__cfr_game_first:n \\
  \exp_args:No \hhline{\doubleactivetilde*{#2}{|-}|}
  \IfValueTF{#3}{\multirow{#1}{*}{#3\quad}}{\multicolumn{1}{@{}c@{}}{}}
  & \seq_use:Nn \l_cfr_game_body_seq
   { \\ \exp_args:No \hhline{\doubleactivetilde*{#2}{|-}|} & } \\
  \exp_args:No \hhline{\doubleactivetilde*{#2}{|-}|}
  \IfValueT{#5}
   {
    \multicolumn{2}{c}{\rule{0pt}{1.25\normalbaselineskip}} &
    \multicolumn{#2}{c}{\makebox[0pt]{#5}} \\
   }
  \end{tabular}
}{}

\cs_new_protected:Nn \__cfr_game_equalize_columns:
 {
  \dim_zero:N \l__cfr_game_dim
  % measure the header cells
  \seq_map_function:NN \l_cfr_game_first_seq \__cfr_game_measure_cell:n
  % measure the cells in the body
  \seq_map_inline:Nn \l_cfr_game_body_seq
   {
    \__cfr_game_measure_columns:n { ##1 }
   }
 }
\cs_new_protected:Nn \__cfr_game_measure_columns:n
 {
  \seq_set_split:Nnn \l_tmpa_seq { & } { #1 }
  \seq_indexed_map_inline:Nn \l_tmpa_seq
   {
    \int_compare:nT { ##1 > 1 }
     {
      \__cfr_game_measure_cell:n { ##2 }
     }
   }
 }
\cs_new_protected:Nn \__cfr_game_measure_cell:n
 {
  \hbox_set:Nn \l_tmpa_box { #1 }
  \dim_set:Nn \l__cfr_game_dim { \dim_max:nn { \l__cfr_game_dim } { \box_wd:N \l_tmpa_box } }
 }
\ExplSyntaxOff

\begin{document}

\begin{game}{2}{2}[You][Your partner][Prisoner's Dilemma]
    & Talk      & Quiet \\
    Talk  & $-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\bigskip
\hrule
\bigskip

\begin{game}{2}{2}[You][Your partner]
    & Talk      & Quiet \\
    Talk  & $-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\bigskip
\hrule
\bigskip

\begin{game}{2}{2}[You]
    & Talk      & Quiet \\
    Talk  & $-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\bigskip
\hrule
\bigskip

\begin{game}{2}{2}
    & Talk      & Quiet \\
    Talk  &\cellcolor{yellow}$-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\bigskip
\hrule
\bigskip

\begin{game}{2}{2}
    & Talk      & Quiet \\
    Talk  &\cellcolor{yellow}$-10,-10$ & $-1,-20$ aaaaaaaaaaaaa \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\bigskip
\hrule
\bigskip

\begin{game}{2}{2}
    & Talk      & Quiet, quiet, quiet, quiet \\
    Talk  &\cellcolor{yellow}$-10,-10$ & $-1,-20$ \\
    Quiet & $-20,-1$  & $-5,-5$ \\
\end{game}

\end{document}

在此处输入图片描述

相关内容