4

How can I make these subtables horizontally instead of vertically positioned?

\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
  • 2
    Off-topic: \usepackage[(margin=1in)]{geometry} is a syntax error. It should be \usepackage[margin=1in]{geometry}.
    – Mico
    Feb 14, 2023 at 3:30

2 Answers 2

3

The two most important changes you should make in order to achieve you typesetting objective are (a) eliminate the all-blank lines right before and right after \hfill, and (b) change the widths of the subtable environments from \textwidth to, say, 0.45\textwidth.

In the following example, I've also streamlined and simplified your code a bit. Some of the simplifications are made possible by changing \begin{tabular}{cc|cc|} to \begin{tabular}{|l|l|c|c|}.

enter image description here

\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}
3

I know that this might be considered too heavy, but better rendering is a virtue.

\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}

enter image description here

With \subcaptionbox you ensure that the objects only take the space they need, no guess about their size; also, captions will be vertically aligned.

The important thing is not to insert blank lines between the two objects. Careful usage of % avoids unwanted spaces to creep in.

If I uncomment the \small declaration, the output is even more attractive, in my opinion.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .