有一个像下面这样的表格,具有自定义宽度,但我无法运行它,有什么想法吗?非常感谢
\documentclass{article}
\usepackage{array}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{p{0.015\textwidth}|R{0.37\textwidth}|R{0.12\textwidth}|R{0.08\textwidth}|R{0.02\textwidth}|p{0.35\textwidth}|}
\hline
& Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
\hline
1 & 5 & 4 & 1 & 0 & setosa \\
2 & 5 & 3 & 1 & 0 & setosa \\
3 & 5 & 3 & 1 & 0 & setosa \\
4 & 5 & 3 & 2 & 0 & setosa \\
5 & 5 & 4 & 1 & 0 & setosa \\
6 & 5 & 4 & 2 & 0 & setosa \\
\hline
\end{tabular}
\end{table}
\end{document}
答案1
您无需指定列宽。此外,您可以通过对列进行分组来避免表格过分分散。
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{
@{}
r % number
ccccc
@{}
}
\toprule
& \multicolumn{2}{c}{Sepal} & \multicolumn{2}{c}{Petal} & Species \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& Length & Width & Length & Width & \\
\midrule
1 & 5 & 4 & 1 & 0 & setosa \\
2 & 5 & 3 & 1 & 0 & setosa \\
3 & 5 & 3 & 1 & 0 & setosa \\
4 & 5 & 3 & 2 & 0 & setosa \\
5 & 5 & 4 & 1 & 0 & setosa \\
6 & 5 & 4 & 2 & 0 & setosa \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
您尚未定义R
列类型。如果您希望它按自己的想法执行操作,我猜您的意思是您可以使用wr
(使用相对较新版本的软件包array
)但这会将列宽强制为不适合列内容的值,并强制总表格宽度不适合页面。只需使用自然宽度即可使表格更易于阅读,或者最好删除垂直线并使用 booktabs 软件包。
\documentclass{article}
\usepackage{array,booktabs}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{wl{0.015\textwidth}|wr{0.37\textwidth}|wr{0.12\textwidth}|wr{0.08\textwidth}|wr{0.02\textwidth}|wr{0.35\textwidth}|}
\hline
& Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
\hline
1 & 5 & 4 & 1 & 0 & setosa \\
2 & 5 & 3 & 1 & 0 & setosa \\
3 & 5 & 3 & 1 & 0 & setosa \\
4 & 5 & 3 & 2 & 0 & setosa \\
5 & 5 & 4 & 1 & 0 & setosa \\
6 & 5 & 4 & 2 & 0 & setosa \\
\hline
\end{tabular}
\end{table}
\begin{table}[ht]
\centering
\begin{tabular}{r|r|r|r|r|l|}
\hline
& Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
\hline
1 & 5 & 4 & 1 & 0 & setosa \\
2 & 5 & 3 & 1 & 0 & setosa \\
3 & 5 & 3 & 1 & 0 & setosa \\
4 & 5 & 3 & 2 & 0 & setosa \\
5 & 5 & 4 & 1 & 0 & setosa \\
6 & 5 & 4 & 2 & 0 & setosa \\
\hline
\end{tabular}
\end{table}
\begin{table}[ht]
\centering
\begin{tabular}{cccccl}
\toprule
& Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
\midrule
1 & 5 & 4 & 1 & 0 & setosa \\
2 & 5 & 3 & 1 & 0 & setosa \\
3 & 5 & 3 & 1 & 0 & setosa \\
4 & 5 & 3 & 2 & 0 & setosa \\
5 & 5 & 4 & 1 & 0 & setosa \\
6 & 5 & 4 & 2 & 0 & setosa \\
\bottomrule
\end{tabular}
\end{table}
\end{document}