我想并排显示两个大表格(每个表格 9 列)。为此,我编写了以下代码:
\begin{table}
\parbox{.45\linewidth}{
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
& Class 1 & Class 2 & Class 3 & Class 4 & Class 5 & Class 6 & Class 7 & Class 8\\\hline
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\\hline
\end{tabular}
\caption{Foo}
}
\hfill
\parbox{.45\linewidth}{
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
& Class 1 & Class 2 & Class 3 & Class 4 & Class 5 & Class 6 & Class 7 & Class 8\\\hline
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\\hline
\end{tabular}
\caption{Bar}
}
\end{table}
表格相互重叠。我无法缩小表格、其内部文本和相应标题的尺寸。
第 264 行和第 277 行仅包含右括号}
。
这些是我迄今为止使用过的软件包:
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage[paper=portrait,pagesize]{typearea}
\usepackage{mathtools}% ctan.org/pkg/mathtools
\usepackage{amsmath}
\usepackage{graphicx}
编辑
通过遵循评论部分 Pluton 的建议,我几乎解决了我的问题,只是右侧表格的右端与文档的文本不对齐。
答案1
试试这个代码:
\documentclass{article}
\usepackage{array}
\usepackage{geometry}
\begin{document}
\begin{table}
\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{2pt}
\noindent\parbox{.45\linewidth}{
\centering
\begin{tabular}{|*{9}{c|}}
\hline
\textbf{Class}& \bfseries 1 & \bfseries 2 & \bfseries 3 & \bfseries4 & \bfseries 5 & \bfseries 6 & \bfseries 7 & \bfseries 8\\\hline
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\\hline
\end{tabular}
\caption{Foo}
}
\hfill
\parbox{.45\linewidth}{
\centering
\begin{tabular}{|*{9}{c|}}
\hline
\textbf{Class}& \bfseries 1 & \bfseries 2 & \bfseries 3 & \bfseries 4 & \bfseries 5 & \bfseries 6 & \bfseries 7 & \bfseries 8\\\hline
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\\hline
\end{tabular}
\caption{Bar}
}
\end{table}
\end{document}
编辑:我还提出了这种变体,它使用S
以下列类型siunitx
:
\begin{table}
\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{2pt}
\sisetup{table-number-alignment=center}
\noindent\parbox{.45\linewidth}{
\centering
\begin{tabular}{|*{5}{c|}S[table-format=1.4]|S[table-format=1.3]|c|c|}
\hline
\multicolumn{1}{|c}{\bfseries Class}& \head{1} & \head{2} & \head{3} & \head{4} & \head{5}& \head{6} & \head{7}& \multicolumn{1}{c|}{\bfseries 8}\\ \hline
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\\hline
\end{tabular}
\caption{Foo}
}
\hfill
\parbox{.45\linewidth}{
\centering
\begin{tabular}{|*{5}{c|}S[table-format=1.4]|S[table-format=1.3]|c|c|}
\hline
\multicolumn{1}{|c}{\bfseries Class}& \head{1} & \head{2} & \head{3} & \head{4} & \head{5}& \head{6} & \head{7}&\multicolumn{1}{c|}{\bfseries 8}\\
\hline
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\\hline
\end{tabular}
\caption{Bar}
}
\end{table}
答案2
以下是我建议使用的siunitx
数字对齐、booktabs
水平线以及tabular*
确保表格适合可用空间。此外,我\parbox
用两个minipage
环境替换了命令:
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{caption}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\begin{document}
\begin{table}
\begin{minipage}{0.45\textwidth}
\setlength{\tabcolsep}{0pt}
\sisetup{table-format=1}
\caption{Foo}
\label{tab:foo}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}cSSSSS[table-format=1.4]S[table-format=1.3]SS}
\toprule
& \multicolumn{8}{c}{Class}\\
\cmidrule{2-9}
& {1} & {2} & {3} & {4} & {5} & {6} & {7} & {8}\\
\midrule
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\
\bottomrule
\end{tabular*}
\end{minipage}
\hfill
\begin{minipage}{0.45\textwidth}
\setlength{\tabcolsep}{0pt}
\sisetup{table-format=1}
\caption{Bar}
\label{tab:bar}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}cSSSSS[table-format=1.4]S[table-format=1.3]SS}
\toprule
& \multicolumn{8}{c}{Class}\\
\cmidrule{2-9}
& {1} & {2} & {3} & {4} & {5} & {6} & {7} & {8}\\
\midrule
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\
\bottomrule
\end{tabular*}
\end{minipage}
\end{table}
\end{document}
答案3
我建议你重新组织标题材料,以便每个表格只能看到一个“Class”实例。我还会删除所有垂直线和大多数水平线,以便让表格看起来更开放、更吸引人。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
%%\usepackage{amsfonts} % is loaded automatically by amssymb
\usepackage{amssymb}
\usepackage[margin=2cm]{geometry}
\usepackage[paper=portrait,pagesize]{typearea}
\usepackage{mathtools} % ctan.org/pkg/mathtools
%%\usepackage{amsmath} % is loaded automatically by mathtools
\usepackage{graphicx}
\usepackage{booktabs} % <-- new
\begin{document}
\begin{table}
\setlength\tabcolsep{0pt}
\begin{minipage}{0.475\textwidth}
\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}} l *{8}{c}}
\toprule
& \multicolumn{8}{c}{Class} \\
\cmidrule{2-9}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
\midrule
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\
\bottomrule
\end{tabular*}
\caption{Foo}
\end{minipage}
\hfill
\begin{minipage}{0.475\textwidth}
\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}} l *{8}{c}}
\toprule
& \multicolumn{8}{c}{Class} \\
\cmidrule{2-9}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
\midrule
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\
\bottomrule
\end{tabular*}
\caption{Bar}
\end{minipage}
\end{table}
\end{document}