我的表格宽度有问题,它非常小,如下所示,我需要它适合两栏纸张。有什么帮助吗?
\begin{table}[H]
\centering
\begin{tabular}{|r|r|r|r|}
\hline
& Class 1 & Class 2 & Class 3 \\
\hline
Class 1 & 0 & 0 & 262 \\
\hline
Class 2 & 0 & 0 & 295 \\
\hline
Class 3 & 0 & 0 & 320 \\
\hline
\end{tabular}
\caption{Confusion Matrix}
\label{tab:template}
\end{table}
答案1
您可以使用tabularx
包装并使用的宽度\columnwidth
,以便您的表格将跨越整个列的宽度:
\documentclass[twocolumn]{article}
\usepackage{tabularx}
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
\begin{document}
\begin{table}
\centering
\begin{tabularx}{\columnwidth}{|Y|Y|Y|Y|}
\hline
& Class 1 & Class 2 & Class 3 \\
\hline
Class 1 & 0 & 0 & 262 \\
\hline
Class 2 & 0 & 0 & 295 \\
\hline
Class 3 & 0 & 0 & 320 \\
\hline
\end{tabularx}
\caption{Confusion Matrix}
\label{tab:template}
\end{table}
\end{document}
您还可以手动设置适当的列宽:
\documentclass[twocolumn]{article}
\usepackage{array}
\begin{document}
\begin{table}
\centering
\begin{tabular}{*{4}{|>{\raggedleft\arraybackslash}p{\dimexpr\columnwidth/4-2\tabcolsep-\fboxrule\relax}}|}
\hline
& Class 1 & Class 2 & Class 3 \\
\hline
Class 1 & 0 & 0 & 262 \\
\hline
Class 2 & 0 & 0 & 295 \\
\hline
Class 3 & 0 & 0 & 320 \\
\hline
\end{tabular}
\caption{Confusion Matrix}
\label{tab:template}
\end{table}
\end{document}