如何在使用表格的表中使用多列和多行?

如何在使用表格的表中使用多列和多行?

这个问题我学会了如何创建带有行和列标签的表格。但是,我仍然在努力让它看起来完全符合我的要求。

我想将此解决方案扩展到任意数量的列,例如,我尝试在下面的乳胶代码中将其扩展到 3 列。但是,似乎我无法让线条也延伸。有人能帮我完成剩下的工作吗?

\begin{tabular}{*6c}
& &\multicolumn{4}{c}{$P$}\\
& & $A$ &$B$ & $C$ \\\cline{3-4}
\multirow{2}{*}& $A$ & \multicolumn{1}{|c|}{$0$} & \multicolumn{1}{c|}{$500$} & \multicolumn{1}{c|}{$500$} \\\cline{3-4}
& $D$ & \multicolumn{1}{|c|}{($100$)} &\multicolumn{1}{c|}{($200$)} & \multicolumn{1}{c|}{($200$)}\\\cline{3-4}
\end{tabular}

桌子

答案1

您可以使用tabularray

\documentclass{article} 
\usepackage{tabularray}

\begin{document}
\begin{table}[ht]
\caption{Caption text here}
\[
\begin{tblr}{
  colspec={*{4}{c}}, 
  hline{3-Z}={2-Z}{solid},
  vline{2-Z}={3-Z}{solid},
  }
& \SetCell[c=3]{c} P \\
& A & B & C \\
A     & 0      & 500    & 500    \\
D     & (100)  &(200)   & (200)  \\
\end{tblr}
\]
\end{table}
\begin{table}[ht]
\caption{If you need one column less}
\[
\begin{tblr}{
  colspec={*{3}{c}}, 
  hline{3-Z}={2-Z}{solid},
  vline{2-Z}={3-Z}{solid},
  }
& \SetCell[c=2]{c} P \\
& A & B  \\
A     & 0      & 500    \\
D     & (100)  &(200)   \\
\end{tblr}
\]
\end{table}
\end{document}

在此处输入图片描述

答案2

使用{NiceArray}ofnicematrix及其内置命令\Block

\documentclass{article}
\usepackage{nicematrix}
\setlength\extrarowheight{2pt}

\begin{document}

\begin{table}[ht]
\caption{Caption text here}
\centering
$\begin{NiceArray}{ccccc}
  & \Block{1-3}{P}\\[1ex]
  & A      & B      & C & \\ 
A & \Block[hvlines]{2-3}{}
    0      & 500    & 500    \\ 
D & (100)  &(200)   & (200)  \\ 
\end{NiceArray}$
\end{table}

\end{document}

上述代码的输出

答案3

您的代码指定表格有 6 列;但是,屏幕截图显示只有 4 列。我检测不到需要\multirow。而且,由于表格的所有内容似乎都“数学化”,因此我会使用array环境 而不是tabular环境。

以下代码进行了必要的调整。

在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage{array} % for '\extrarowheight' macro
\setlength\extrarowheight{2pt} % for a less cramped "look"
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro


\begin{document}

\begin{table}[ht]
\caption{Caption text here}
\centering
$\begin{array}{*{4}{c|}}
\mc{} & \multicolumn{3}{c}{P}\\[1ex]
\mc{} & \mc{A} & \mc{B} & \mc{C} \\ \cline{2-4}
A     & 0      & 500    & 500    \\ \cline{2-4}
D     & (100)  &(200)   & (200)  \\ \cline{2-4}
\end{array}$
\end{table}

\end{document}

相关内容