[英]Latex Table Alignment and Width Issues
So I have this code: 所以我有这段代码:
\begin{center}
\footnotesize
\begin{tabular}{| c | c c c |}
\hline
\multirow{3}{*}{Equipment acquired at start of year} & \multicolumn{3}{c|}{Replacement cost (\$) for given years}\\
\cline{2-4}
& 1 & 2 & 3\\
\hline
1 & 4000 & \multicolumn{1}{|c|}{5400} & 9800\\
2 & 4300 & \multicolumn{1}{|c|}{6200} & 8700\\
3 & 4800 & \multicolumn{1}{|c|}{7100} & -\\
4 & 4900 & \multicolumn{1}{|c|}{-} & -\\
\hline
\end{tabular}
\end{center}
What I want is that the column 3 is evenly width like column under 1 and 2. Also I want "Equipment acquired at start of year" to be centered between the two multirows. 我想要的是,第3列的宽度像1和2下的列一样均匀。我也希望“在年初获得的设备”在两个多行之间居中。
Any ideas? 有任何想法吗? xD
xD
Here are two options, both of which use booktabs
for the construction of the tabular
. 这是两个选项,两个选项都使用
booktabs
来构建tabular
。 booktabs
suggests to avoid using vertical rules, since the columnar structure of a tabular
necessarily lends itself to a horizontal alignment of elements within columns with visual separation provided by the whitespace between columns. booktabs
建议避免使用垂直规则,因为tabular
的列结构必定会使其自身与列内元素的水平对齐,并通过列之间的空白提供视觉上的分隔。
\documentclass{article}
\usepackage{makecell,booktabs,array}
\begin{document}
% This construction does not require the array package
\begin{tabular}{ *{4}{c} }
\toprule
& \multicolumn{3}{c}{\makecell[c]{Replacement cost \\ for given years (\$)}} \\
\cmidrule{2-4}
\smash{\makecell[cb]{Equipment acquired \\ at start of year}} & 1 & 2 & 3 \\
\midrule
1 & 4000 & 5400 & 9800 \\
2 & 4300 & 6200 & 8700 \\
3 & 4800 & 7100 & -- \\
4 & 4900 & -- & -- \\
\bottomrule
\end{tabular}
\bigskip
% This construction requires the array package
\begin{tabular}{ c *{3}{>{\centering\arraybackslash}p{1cm}} }
\toprule
& \multicolumn{3}{c}{\makecell[c]{Replacement cost \\ for given years (\$)}} \\
\cmidrule{2-4}
\smash{\makecell[cb]{Equipment acquired \\ at start of year}} & 1 & 2 & 3 \\
\midrule
1 & 4000 & 5400 & 9800 \\
2 & 4300 & 6200 & 8700 \\
3 & 4800 & 7100 & -- \\
4 & 4900 & -- & -- \\
\bottomrule
\end{tabular}
\end{document}
In the first option, the header for columns 2-4 is stacked to naturally fit within the resulting width. 在第一个选项中,第2-4列的标题被堆叠以自然地适合结果宽度。 In the second option, a fixed column width of
1cm
is specified for columns 2-4. 在第二个选项中,为2-4列指定了
1cm
的固定列宽。 You can adjust this width as needed, depending on how much you want to stretch out these columns. 您可以根据需要调整此宽度,具体取决于您要拉伸这些列的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.