水平子表

水平子表

我怎样才能使这些子表水平放置而不是垂直放置?

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[(margin=1in)]{geometry}
\usepackage{setspace}
\setstretch{1}
\begin{table}[ht!]

\begin{subtable}[t]{\textwidth}
\centering
\begin{tabular}{cc|cc|}
\cline{3-4}
                                         &            & \multicolumn{2}{c|}{B}                      \\ \cline{3-4} 
                                         &            & \multicolumn{1}{c|}{Free Flow} & Regulation \\ \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{A}} & Free Flow  & \multicolumn{1}{c|}{(3, 3)}    & (2, 5)     \\ \cline{2-4} 
\multicolumn{1}{|c|}{}                   & Regulation & \multicolumn{1}{c|}{(5, 2)}    & (1, 1)     \\ \hline
\end{tabular}

\caption{Prisoner's dilemma}
\label{f1}
\end{subtable}

\hfill

\begin{subtable}[t]{\textwidth}
\centering
\begin{tabular}{cl|cc|}
\cline{3-4}
                                         &            & \multicolumn{2}{c|}{B}                                           \\ \cline{3-4} 
\multicolumn{1}{l}{}                     &            & \multicolumn{1}{l|}{Free Flow} & \multicolumn{1}{l|}{Regulation} \\ \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{A}} & Free Flow  & \multicolumn{1}{c|}{(5, 5)}    & (1, 3)                          \\ \cline{2-4} 
\multicolumn{1}{|c|}{}                   & Regulation & \multicolumn{1}{c|}{(3, 1)}    & (2, 2)                          \\ \hline
\end{tabular}
\caption{Stag Hunt}
\label{f2}
\end{subtable}

\caption{Policy Decision Matrix}
\label{F1}
\end{table}

答案1

为了实现您的排版目标,您应该做出的两个最重要的改变是(a)消除 之前和 之后的全空行\hfill,以及(b)将环境的宽度subtable从更改\textwidth0.45\textwidth

\begin{tabular}{cc|cc|}在以下示例中,我还对您的代码进行了一些精简和简化。通过将 更改为,可以实现部分简化\begin{tabular}{|l|l|c|c|}

在此处输入图片描述

\documentclass[12pt]{article}

\usepackage{array,subcaption,multirow}
%\usepackage[utf8]{inputenc} % that's the default nowadays
\usepackage[margin=1in]{geometry}
\usepackage{setspace}
\setstretch{1}
\newcommand\blank[1]{\multicolumn{#1}{l|}{}}

\begin{document}
\begin{table}[ht!]
\setlength\extrarowheight{2pt}
\setlength\tabcolsep{4pt} % default: 6pt

\begin{subtable}[t]{0.45\textwidth}
\centering
\begin{tabular}{|l|l|c|c|}
\cline{3-4}
\blank{2} & \multicolumn{2}{c|}{B} \\ 
\cline{3-4} 
\blank{2} & Free Flow & Regulation \\ 
\hline
\multirow{2}{*}{A} & Free Flow  & (3, 3) & (2, 5) \\ 
\cline{2-4} 
                   & Regulation & (5, 2) & (1, 1) \\ 
\hline
\end{tabular}
\caption{Prisoner's dilemma}
\label{f1}
\end{subtable}
\hfill
\begin{subtable}[t]{0.45\textwidth}
\centering
\begin{tabular}{|l|l|c|c|}
\cline{3-4}
\blank{2} & \multicolumn{2}{c|}{B} \\ 
\cline{3-4} 
\blank{2} & Free Flow & Regulation \\ 
\hline
\multirow{2}{*}{A} & Free Flow  & (5, 5) & (1, 3) \\ 
\cline{2-4} 
                   & Regulation & (3, 1) & (2, 2) \\ 
\hline
\end{tabular}
\caption{Stag Hunt}
\label{f2}
\end{subtable}

\caption{A table with two subtables}
\end{table}
\end{document}

答案2

我知道这可能被认为太重了,但更好的渲染是一种优点。

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}

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

% from https://tex.stackexchange.com/a/512953/4427
\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

\captionsetup[table]{position=bottom}

\begin{document}

\begin{table}[!htp]

%\small

\subcaptionbox{Prisoner's dilemma}{%
  \begin{game}{2}{2}[A][B]
    & Free Flow & Regulation \\
    Free Flow  & $(3,3)$ & $(2,5)$ \\
    Regulation & $(5,2)$ & $(1,1)$ \\
  \end{game}%
}%
\hspace*{\fill}%
\subcaptionbox{Stag hunt}{%
  \begin{game}{2}{2}[A][B]
    & Free Flow & Regulation \\
    Free Flow  & $(5,5)$ & $(1,3)$ \\
    Regulation & $(3,1)$ & $(2,2)$ \\
  \end{game}%
}

\caption{A table with two subtables}

\end{table}

\end{document}

在此处输入图片描述

确保\subcaptionbox对象只占用它们所需的空间,无需猜测它们的大小;此外,标题将垂直对齐。

重要的是不要在两个对象之间插入空行。小心使用%可以避免出现不必要的空格。

如果我取消注释该\small声明,我认为输出会更具吸引力。

在此处输入图片描述

相关内容