将所有单元格 ( 下方multicolumn
) 设置为相等宽度的最快方法是什么?这是我的表格代码。
\begin{frame}{Applications}
\begin{table}[h]
\begin{tabular}{|l|l|l|l|l|l|l|l|}
\hline
Origin & \multicolumn{7}{l|}{Development year} \\ \hline
Year &$1$ & 2 & ... & j & ... & n-1 & n \\ \hline
1 & $X_{11}$ & $X_{12}$ & ... & $X_{1j}$ & ... & $X_{1,n-1}$ & $X_{1,n}$ \\ \hline
2 & $X_{21}$ & $X_{22}$ & ... & $X_{2j}$ & ... & $X_{2,n-1}$ & \\ \hline
... & ... & ... & ... & ... & & & \\ \hline
i & $X_{i1}$ & $X_{i2}$ & ... & $X_{i,n+1-i}$ & & & \\ \hline
... & & & & & & & \\ \hline
n-1 & $X_{n-1,1}$ & $X_{n-2,1}$ & & & & & \\ \hline
n & $X_{n,1}$ & & & & & & \\ \hline
\end{tabular}
\end{table}
答案1
beamer
可以,但是除非您减小字体大小和列间空间,否则它将无法容纳在幻灯片中。
\documentclass{beamer}
\usepackage{array}
\newlength{\mytabcell}
\begin{document}
\begin{frame}
\frametitle{Applications}
\small
\settowidth{\mytabcell}{$X_{i,n+1-i}$}
\addtolength{\tabcolsep}{-4pt}
\newcolumntype{P}{>{\centering\arraybackslash}p{\mytabcell}}
\begin{tabular}{|l|*{7}{P|}}
\hline
Origin & \multicolumn{7}{l|}{Development year} \\ \hline
Year & $1$ & $2$ & \dots & $j$ & \dots & $n-1$ & $n$ \\ \hline
$1$ & $X_{11}$ & $X_{12}$ & \dots & $X_{1j}$ & \dots & $X_{1,n-1}$ & $X_{1,n}$ \\ \hline
$2$ & $X_{21}$ & $X_{22}$ & \dots & $X_{2j}$ & \dots & $X_{2,n-1}$ & \\ \hline
\dots & \dots & \dots & \dots & \dots & & & \\ \hline
$i$ & $X_{i1}$ & $X_{i2}$ & \dots & $X_{i,n+1-i}$ & & & \\ \hline
\dots & & & & & & & \\ \hline
$n-1$ & $X_{n-1,1}$ & $X_{n-2,1}$ & & & & & \\ \hline
$n$ & $X_{n,1}$ & & & & & & \\ \hline
\end{tabular}
\end{frame}
\end{document}
诀窍是使用一个p
列,通过告诉它最宽的列是什么来设置其大小。
请注意,table
环境没有任何用处。我还修复了数学模式中的条目以保持一致性。
可能的改进booktabs
\documentclass{beamer}
\usepackage{array,booktabs}
\newlength{\mytabcell}
\begin{document}
\begin{frame}
\frametitle{Applications}
\small
\settowidth{\mytabcell}{$X_{i,n+1-i}$}
\addtolength{\tabcolsep}{-4pt}
\newcolumntype{P}{>{\centering\arraybackslash}p{\mytabcell}}
\begin{tabular}{l*{7}{P}}
\toprule
\smash{\begin{tabular}[t]{@{}l@{}}Origin\\Year\end{tabular}}
& \multicolumn{7}{c}{Development year} \\
\cmidrule(l){2-8}
& $1$ & $2$ & \dots & $j$ & \dots & $n-1$ & $n$ \\
\midrule
$1$ & $X_{11}$ & $X_{12}$ & \dots & $X_{1j}$ & \dots & $X_{1,n-1}$ & $X_{1,n}$ \\
$2$ & $X_{21}$ & $X_{22}$ & \dots & $X_{2j}$ & \dots & $X_{2,n-1}$ & \\
\dots & \dots & \dots & \dots & \dots & & & \\
$i$ & $X_{i1}$ & $X_{i2}$ & \dots & $X_{i,n+1-i}$ & & & \\
\dots & & & & & & & \\
$n-1$ & $X_{n-1,1}$ & $X_{n-2,1}$ & & & & & \\
$n$ & $X_{n,1}$ & & & & & & \\
\bottomrule
\end{tabular}
\end{frame}
\end{document}
答案2
这是@egreg基于booktabs的答案的修改版本,其中我使用tabular*
环境让LaTeX完成确定将整个表格设置为的列间空白量的工作\textwidth
。有了这个设置,结果不是需要减小字体大小以使表格适合文本块。
\documentclass{beamer}
\usepackage{array,booktabs}
\newlength{\mytabcell}
\begin{document}
\begin{frame}{Applications}
%\small
\settowidth{\mytabcell}{$X_{i,n+1-i}$}
\setlength{\tabcolsep}{0.01pt} % let 'tabular*' figure out column separation
\newcolumntype{P}{>{\centering\arraybackslash}p{\mytabcell}}
\begin{tabular*}{\textwidth}{l @{\extracolsep{\fill}} *{7}{P}}
\toprule
\smash{\begin{tabular}[t]{@{}l@{}}Origin\\Year\end{tabular}}
& \multicolumn{7}{c}{Development year} \\
\cmidrule(l){2-8}
& $1$ & $2$ & \dots & $j$ & \dots & $n-1$ & $n$ \\
\midrule
$1$ & $X_{11}$ & $X_{12}$ & \dots & $X_{1j}$ & \dots & $X_{1,n-1}$ & $X_{1,n}$ \\
$2$ & $X_{21}$ & $X_{22}$ & \dots & $X_{2j}$ & \dots & $X_{2,n-1}$ & \\
\dots & \dots & \dots & \dots & \dots & & & \\
$i$ & $X_{i1}$ & $X_{i2}$ & \dots & $X_{i,n+1-i}$ & & & \\
\dots & & & & & & & \\
$n-1$ & $X_{n-1,1}$ & $X_{n-2,1}$ & & & & & \\
$n$ & $X_{n,1}$ & & & & & & \\
\bottomrule
\end{tabular*}
\end{frame}
\end{document}
答案3
{NiceTabular}
在 的环境下nicematrix
,您有一个键columns-width
可以固定所有列的宽度。在 的值为 时auto
,宽度将是数组最宽单元格的宽度。
\documentclass{beamer}
\usepackage{nicematrix}
\begin{document}
\begin{frame}
\frametitle{Applications}
\small
\setlength{\tabcolsep}{2pt}
\begin{NiceTabular}{lccccccc}[columns-width=auto,hvlines]
Origin & \Block[l]{1-*}{Development year} \\
Year & $1$ & $2$ & \dots & $j$ & \dots & $n-1$ & $n$ \\
$1$ & $X_{11}$ & $X_{12}$ & \dots & $X_{1j}$ & \dots & $X_{1,n-1}$ & $X_{1,n}$ \\
$2$ & $X_{21}$ & $X_{22}$ & \dots & $X_{2j}$ & \dots & $X_{2,n-1}$ & \\
\dots & \dots & \dots & \dots & \dots \\
$i$ & $X_{i1}$ & $X_{i2}$ & \dots & $X_{i,n+1-i}$ \\
\dots \\
$n-1$ & $X_{n-1,1}$ & $X_{n-2,1}$ \\
$n$ & $X_{n,1}$ \\
\end{NiceTabular}
\end{frame}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。